1 ! Test lowering of IO statement involving INTEGER(8) and INTEGER(16) external
2 ! unit number that may turn out at runtime to be too large to fit in a default
3 ! integer. Unit numbers must fit on default integers. This file tests that the
4 ! related generated runtime checks and error recovery code.
5 ! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s
8 ! -----------------------------------------------------------------------------
9 ! Test that runtime checks are not emitted when not needed
10 ! -----------------------------------------------------------------------------
12 ! CHECK-LABEL: func @_QPopen_4
15 ! CHECK-NOT: CheckUnitNumberInRange
20 ! CHECK-LABEL: func @_QPwrite_4
21 subroutine write_4(n4
)
23 ! CHECK-NOT: CheckUnitNumberInRange
28 ! CHECK-LABEL: func @_QPwrite_4_recovery
29 subroutine write_4_recovery(n4
, ios
)
31 ! CHECK-NOT: CheckUnitNumberInRange
32 write(n4
, *, iostat
=ios
) "hello"
35 ! -----------------------------------------------------------------------------
36 ! Test that runtime checks are emitted for integer(8/16) when there is
37 ! no user error recovery.
38 ! -----------------------------------------------------------------------------
40 ! CHECK-LABEL: func @_QPopen_8(
41 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
44 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
45 ! CHECK: %[[VAL_2:.*]] = arith.constant false
46 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
47 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
48 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
50 ! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_1]] : (i64) -> i32
51 ! CHECK: %[[VAL_13:.*]] = fir.call @_FortranAioBeginOpenUnit(%[[VAL_9]], %{{.*}}, %{{.*}}) {{.*}}: (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
52 ! CHECK: %[[VAL_14:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_13]]) {{.*}}: (!fir.ref<i8>) -> i32
56 ! CHECK-LABEL: func @_QPclose_8(
57 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
60 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
61 ! CHECK: %[[VAL_2:.*]] = arith.constant false
62 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
63 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
64 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
70 ! CHECK-LABEL: func @_QPrewind_8(
71 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
72 subroutine rewind_8(n
)
74 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
75 ! CHECK: %[[VAL_2:.*]] = arith.constant false
76 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
77 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
78 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
84 ! CHECK-LABEL: func @_QPbackspace_8(
85 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
86 subroutine backspace_8(n
)
89 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
90 ! CHECK: %[[VAL_2:.*]] = arith.constant false
91 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
92 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
93 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
95 ! CHECK: BeginBackspace
98 ! CHECK-LABEL: func @_QPinquire_8(
99 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
100 subroutine inquire_8(n
, fm
)
103 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
104 ! CHECK: %[[VAL_2:.*]] = arith.constant false
105 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
106 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
107 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
109 ! CHECK: BeginInquire
110 inquire(n
, formatted
=fm
)
113 ! CHECK-LABEL: func @_QPwrite_8(
114 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
115 subroutine write_8(n
)
117 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
118 ! CHECK: %[[VAL_2:.*]] = arith.constant false
119 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
120 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
121 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
123 ! CHECK: BeginExternalListOutput
127 ! CHECK-LABEL: func @_QPread_8(
128 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
129 subroutine read_8(n
, var
)
132 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
133 ! CHECK: %[[VAL_2:.*]] = arith.constant false
134 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
135 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
136 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
138 ! CHECK: BeginExternalListInput
142 ! CHECK-LABEL: func @_QPopen_16(
143 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i128>
144 subroutine open_16(n
)
146 ! CHECK: %[[VAL_1:.*]] = fir.load %[[VAL_0]] : !fir.ref<i128>
147 ! CHECK: %[[VAL_2:.*]] = arith.constant false
148 ! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ref<i8>
149 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
150 ! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAioCheckUnitNumberInRange128(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %{{.*}}, %{{.*}}) {{.*}}: (i128, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
154 ! -----------------------------------------------------------------------------
155 ! Test generation of user error recovery if-nests with INTEGER(8/16)
157 ! -----------------------------------------------------------------------------
159 ! CHECK-LABEL: func @_QPopen_8_error_recovery_1(
160 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
161 ! CHECK-SAME: , %[[VAL_1:.*]]: !fir.ref<i32>
162 subroutine open_8_error_recovery_1(n
, ios
)
165 ! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
166 ! CHECK: %[[VAL_3:.*]] = arith.constant true
167 ! CHECK: %[[VAL_4:.*]] = fir.zero_bits !fir.ref<i8>
168 ! CHECK: %[[VAL_5:.*]] = arith.constant 0 : i64
169 ! CHECK: %[[VAL_9:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_2]], %[[VAL_3]], %[[VAL_4]], %[[VAL_5]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
170 ! CHECK: %[[VAL_10:.*]] = arith.constant 0 : i32
171 ! CHECK: %[[VAL_11:.*]] = arith.cmpi eq, %[[VAL_9]], %[[VAL_10]] : i32
172 ! CHECK: %[[VAL_12:.*]] = fir.if %[[VAL_11]] -> (i32) {
173 ! CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_2]] : (i64) -> i32
174 ! CHECK: %[[VAL_17:.*]] = fir.call @_FortranAioBeginOpenUnit(%[[VAL_13]], %{{.*}}, {{.*}}) {{.*}}: (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
175 ! CHECK: %[[VAL_18:.*]] = arith.constant true
176 ! CHECK: %[[VAL_19:.*]] = arith.constant false
177 ! CHECK: %[[VAL_20:.*]] = arith.constant false
178 ! CHECK: %[[VAL_21:.*]] = arith.constant false
179 ! CHECK: %[[VAL_22:.*]] = arith.constant false
180 ! CHECK: %[[VAL_23:.*]] = fir.call @_FortranAioEnableHandlers(%[[VAL_17]], %[[VAL_18]], %[[VAL_19]], %[[VAL_20]], %[[VAL_21]], %[[VAL_22]]) {{.*}}: (!fir.ref<i8>, i1, i1, i1, i1, i1) -> none
181 ! CHECK: %[[VAL_24:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_17]]) {{.*}}: (!fir.ref<i8>) -> i32
182 ! CHECK: fir.result %[[VAL_24]] : i32
184 ! CHECK: fir.result %[[VAL_9]] : i32
186 ! CHECK: fir.store %[[VAL_25:.*]] to %[[VAL_1]] : !fir.ref<i32>
190 ! CHECK-LABEL: func @_QPopen_8_error_recovery_2(
191 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64>
192 ! CHECK-SAME: , %[[VAL_1:.*]]: !fir.boxchar<1>
193 subroutine open_8_error_recovery_2(n
, msg
)
196 ! CHECK: %[[VAL_2:.*]]:2 = fir.unboxchar %[[VAL_1]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
197 ! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_0]] : !fir.ref<i64>
198 ! CHECK: %[[VAL_4:.*]] = arith.constant true
199 ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_2]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
200 ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_2]]#1 : (index) -> i64
201 ! CHECK: %[[VAL_10:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(%[[VAL_3]], %[[VAL_4]], %[[VAL_5]], %[[VAL_6]], %{{.*}}, %{{.*}}) {{.*}}: (i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32
202 ! CHECK: %[[VAL_11:.*]] = arith.constant 0 : i32
203 ! CHECK: %[[VAL_12:.*]] = arith.cmpi eq, %[[VAL_10]], %[[VAL_11]] : i32
204 ! CHECK: %[[VAL_13:.*]] = fir.if %[[VAL_12]] -> (i32) {
205 ! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_3]] : (i64) -> i32
206 ! CHECK: %[[VAL_18:.*]] = fir.call @_FortranAioBeginOpenUnit(%[[VAL_14]], %{{.*}}, %{{.*}}) {{.*}}: (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
207 ! CHECK: %[[VAL_19:.*]] = arith.constant false
208 ! CHECK: %[[VAL_20:.*]] = arith.constant true
209 ! CHECK: %[[VAL_21:.*]] = arith.constant false
210 ! CHECK: %[[VAL_22:.*]] = arith.constant false
211 ! CHECK: %[[VAL_23:.*]] = arith.constant true
212 ! CHECK: %[[VAL_24:.*]] = fir.call @_FortranAioEnableHandlers(%[[VAL_18]], %[[VAL_19]], %[[VAL_20]], %[[VAL_21]], %[[VAL_22]], %[[VAL_23]]) {{.*}}: (!fir.ref<i8>, i1, i1, i1, i1, i1) -> none
213 ! CHECK: %[[VAL_25:.*]] = fir.convert %[[VAL_2]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
214 ! CHECK: %[[VAL_26:.*]] = fir.convert %[[VAL_2]]#1 : (index) -> i64
215 ! CHECK: %[[VAL_27:.*]] = fir.call @_FortranAioGetIoMsg(%[[VAL_18]], %[[VAL_25]], %[[VAL_26]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64) -> none
216 ! CHECK: %[[VAL_28:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_18]]) {{.*}}: (!fir.ref<i8>) -> i32
217 ! CHECK: fir.result %[[VAL_28]] : i32
219 ! CHECK: fir.result %[[VAL_10]] : i32
221 ! CHECK: %[[VAL_29:.*]] = fir.convert %[[VAL_30:.*]] : (i32) -> index
222 ! CHECK: fir.select %[[VAL_29]] : index [0, ^bb1, unit, ^bb2]
226 ! CHECK: fir.call @_QPi_failed() {{.*}}: () -> ()
230 open(n
, err
=30, iomsg
=msg
)
235 ! Torture test for temp clean-ups when user recovery is enabled.
236 ! Checks that temps are cleaned-up in the right nests.
237 subroutine temp_cleanup(n
, msg
, ios
)
239 function make_temp0()
240 integer, allocatable
:: make_temp0
242 function make_temp1()
243 integer, allocatable
:: make_temp1
245 function make_temp2()
246 integer, allocatable
:: make_temp2
248 function make_temp3()
249 integer, allocatable
:: make_temp3
251 function make_temp4()
252 integer, allocatable
:: make_temp4
254 function make_temp5()
255 integer, allocatable
:: make_temp5
261 write(n(make_temp0()), iostat
=ios(make_temp1()), iomsg
=msg(make_temp2():make_temp3())) make_temp4(), make_temp5()
262 ! CHECK-LABEL: func @_QPtemp_cleanup(
263 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<2xi64>> {fir.bindc_name = "n"},
264 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.boxchar<1> {fir.bindc_name = "msg"},
265 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<!fir.array<2xi32>> {fir.bindc_name = "ios"}) {
266 ! CHECK: %[[VAL_10:.*]] = fir.call @_QPmake_temp2() {{.*}}: () -> !fir.box<!fir.heap<i32>>
267 ! CHECK: fir.save_result %[[VAL_10]] to %[[VAL_8:.*]] : !fir.box<!fir.heap<i32>>, !fir.ref<!fir.box<!fir.heap<i32>>>
268 ! CHECK: %[[VAL_15:.*]] = fir.call @_QPmake_temp3() {{.*}}: () -> !fir.box<!fir.heap<i32>>
269 ! CHECK: fir.save_result %[[VAL_15]] to %[[VAL_7:.*]] : !fir.box<!fir.heap<i32>>, !fir.ref<!fir.box<!fir.heap<i32>>>
270 ! CHECK: fir.load %[[VAL_7]]
271 ! CHECK: %[[VAL_32:.*]] = fir.load %[[VAL_7]] : !fir.ref<!fir.box<!fir.heap<i32>>>
272 ! CHECK: %[[VAL_33:.*]] = fir.box_addr %[[VAL_32]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
273 ! CHECK: fir.freemem %[[VAL_33]] : !fir.heap<i32>
274 ! CHECK: %[[VAL_37:.*]] = fir.load %[[VAL_8]] : !fir.ref<!fir.box<!fir.heap<i32>>>
275 ! CHECK: %[[VAL_38:.*]] = fir.box_addr %[[VAL_37]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
276 ! CHECK: fir.freemem %[[VAL_38]] : !fir.heap<i32>
277 ! CHECK: %[[VAL_42:.*]] = fir.call @_QPmake_temp0() {{.*}}: () -> !fir.box<!fir.heap<i32>>
278 ! CHECK: fir.save_result %[[VAL_42]] to %[[VAL_6:.*]] : !fir.box<!fir.heap<i32>>, !fir.ref<!fir.box<!fir.heap<i32>>>
279 ! CHECK: %[[VAL_57:.*]] = fir.call @_FortranAioCheckUnitNumberInRange64(
280 ! CHECK: %[[VAL_58:.*]] = arith.constant 0 : i32
281 ! CHECK: %[[VAL_59:.*]] = arith.cmpi eq, %[[VAL_57]], %[[VAL_58]] : i32
282 ! CHECK: %[[VAL_60:.*]] = fir.if %[[VAL_59]] -> (i32) {
283 ! CHECK: fir.call @_FortranAioBeginUnformattedOutput(
284 ! CHECK: fir.call @_FortranAioEnableHandlers(
285 ! CHECK: %[[VAL_72:.*]] = fir.call @_QPmake_temp4() {{.*}}: () -> !fir.box<!fir.heap<i32>>
286 ! CHECK: fir.save_result %[[VAL_72]] to %[[VAL_5:.*]] : !fir.box<!fir.heap<i32>>, !fir.ref<!fir.box<!fir.heap<i32>>>
287 ! CHECK: %[[VAL_77:.*]] = fir.call @_FortranAioOutputDescriptor(
288 ! CHECK: %[[VAL_77_1:.*]] = fir.load %[[VAL_5]] : !fir.ref<!fir.box<!fir.heap<i32>>>
289 ! CHECK: %[[VAL_77_2:.*]] = fir.box_addr %[[VAL_77_1]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
290 ! CHECK: fir.freemem %[[VAL_77_2]] : !fir.heap<i32>
291 ! CHECK: fir.if %[[VAL_77]] {
292 ! CHECK: %[[VAL_78:.*]] = fir.call @_QPmake_temp5() {{.*}}: () -> !fir.box<!fir.heap<i32>>
293 ! CHECK: fir.save_result %[[VAL_78]] to %[[VAL_4:.*]] : !fir.box<!fir.heap<i32>>, !fir.ref<!fir.box<!fir.heap<i32>>>
294 ! CHECK: fir.call @_FortranAioOutputDescriptor(
295 ! CHECK: %[[VAL_84:.*]] = fir.load %[[VAL_4]] : !fir.ref<!fir.box<!fir.heap<i32>>>
296 ! CHECK: %[[VAL_85:.*]] = fir.box_addr %[[VAL_84]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
297 ! CHECK: fir.freemem %[[VAL_85]] : !fir.heap<i32>
299 ! CHECK-NOT: fir.call @_QPmake_temp3
300 ! CHECK: fir.call @_FortranAioGetIoMsg(
301 ! CHECK: %[[VAL_97:.*]] = fir.call @_FortranAioEndIoStatement(
302 ! CHECK: fir.result %[[VAL_97]] : i32
304 ! CHECK: fir.result %[[VAL_57]] : i32
306 ! CHECK: %[[VAL_98:.*]] = fir.call @_QPmake_temp1() {{.*}}: () -> !fir.box<!fir.heap<i32>>
307 ! CHECK: fir.save_result %[[VAL_98]] to %[[VAL_3:.*]] : !fir.box<!fir.heap<i32>>, !fir.ref<!fir.box<!fir.heap<i32>>>
308 ! CHECK: fir.load %[[VAL_3]]
309 ! CHECK: %[[VAL_107:.*]] = fir.load %[[VAL_3]] : !fir.ref<!fir.box<!fir.heap<i32>>>
310 ! CHECK: %[[VAL_108:.*]] = fir.box_addr %[[VAL_107]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
311 ! CHECK: fir.freemem %[[VAL_108]] : !fir.heap<i32>
312 ! CHECK: %[[VAL_112:.*]] = fir.load %[[VAL_6]] : !fir.ref<!fir.box<!fir.heap<i32>>>
313 ! CHECK: %[[VAL_113:.*]] = fir.box_addr %[[VAL_112]] : (!fir.box<!fir.heap<i32>>) -> !fir.heap<i32>
314 ! CHECK: fir.freemem %[[VAL_113]] : !fir.heap<i32>