1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
|
{
Copyright (c) 1998-2002 by Florian Klaempfl
Type checking and register allocation for nodes that influence
the flow
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit nflw;
{$i fpcdefs.inc}
interface
uses
cclasses,
node,cpubase,
symconst,symtype,symbase,symdef,symsym,
optloop;
type
{ flags used by loop nodes }
tloopflag = (
{ set if it is a for ... downto ... do loop }
lnf_backward,
{ Do we need to parse childs to set var state? }
lnf_varstate,
{ Do a test at the begin of the loop?}
lnf_testatbegin,
{ Negate the loop test? }
lnf_checknegate,
{ Should the value of the loop variable on exit be correct. }
lnf_dont_mind_loopvar_on_exit,
{ Loop simplify flag }
lnf_simplify_processing,
{ set if in a for loop the counter is not used, so an easier exit check
can be carried out }
lnf_counter_not_used);
tloopflags = set of tloopflag;
const
{ loop flags which must match to consider loop nodes equal regarding the flags }
loopflagsequal = [lnf_backward];
type
tlabelnode = class;
tloopnode = class(tbinarynode)
t1,t2 : tnode;
loopflags : tloopflags;
constructor create(tt : tnodetype;l,r,_t1,_t2 : tnode);virtual;
destructor destroy;override;
function dogetcopy : tnode;override;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
procedure buildderefimpl;override;
procedure derefimpl;override;
procedure insertintolist(l : tnodelist);override;
procedure printnodetree(var t:text);override;
{$ifdef DEBUG_NODE_XML}
procedure XMLPrintNodeInfo(var T: Text); override;
procedure XMLPrintNodeTree(var T: Text); override;
{$endif DEBUG_NODE_XML}
function docompare(p: tnode): boolean; override;
end;
twhilerepeatnode = class(tloopnode)
{ l: condition; r: body; tab: test at begin; cn: negate condition
x,y,true,false: while loop
x,y,false,true: repeat until loop }
constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
{$ifdef state_tracking}
function track_state_pass(exec_known:boolean):boolean;override;
{$endif}
end;
twhilerepeatnodeclass = class of twhilerepeatnode;
tifnode = class(tloopnode)
constructor create(l,r,_t1 : tnode);virtual;reintroduce;
constructor create_internal(l,r,_t1 : tnode);virtual;reintroduce;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function simplify(forinline : boolean) : tnode;override;
private
function internalsimplify(warn: boolean) : tnode;
end;
tifnodeclass = class of tifnode;
tfornode = class(tloopnode)
{ if count isn divisable by unrolls then
the for loop must jump to this label to get the correct
number of executions }
entrylabel,
{ this is a dummy node used by the dfa to store life information for the loop iteration }
loopiteration : tnode;
loopvar_notid:cardinal;
constructor create(l,r,_t1,_t2 : tnode;back : boolean);virtual;reintroduce;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function makewhileloop : tnode;
function simplify(forinline : boolean) : tnode;override;
end;
tfornodeclass = class of tfornode;
texitnode = class(tunarynode)
constructor create(l:tnode);virtual;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
property resultexpr : tnode read left write left;
end;
texitnodeclass = class of texitnode;
tbreaknode = class(tnode)
constructor create;virtual;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
end;
tbreaknodeclass = class of tbreaknode;
tcontinuenode = class(tnode)
constructor create;virtual;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
end;
tcontinuenodeclass = class of tcontinuenode;
tgotonode = class(tnode)
private
labelnodeidx : longint;
public
{ * Set when creating the gotonode (since that's all we know at that
point).
* Used in pass_typecheck to find the corresponding labelnode (when a
labelnode is created for a tlabelsym, the label assigns itself to
the "code" field of the labelsym), which is then assigned to the
labelnode field
* After this, the labelsym is (and must) never be used anymore, and
instead the labelnode must always be used. The reason is that the
labelsym may not be owned by anything, and will be freed by the
label node when it gets freed
* The above is the reason why the labelsym field does not get copied
by tgotonode.dogetcopy, but instead the copy of the labelnode gets
tracked (both the labelnode and its goto nodes must always all be
copied).
The labelnode itself will not copy the labelsym either in dogetcopy.
Instead, since the link between the gotos and the labels gets
tracked via node tree references, the label node will generate a new
asmlabel on the fly and the goto node will get it from there (if the
goto node gets processed before the label node has been processed,
it will ask the label node to generate the asmsymbol at that point).
The original tlabelsym will get emitted only for the original
label node. It is only actually used if there is a reference to it
from
* an inline assembly block. Since inline assembly blocks cannot be
inlined at this point, it doesn't matter that this would break
in case the node gets copied
* global goto/label. Inlining is not supported for these, so no
problem here either for now.
* a load node (its symtableentry field). Since the symtableentry
of loadnodes is always expected to be valid, we cannot do like
with the goto nodes. Instead, we will create a new labelsym
when performing a dogetcopy of such a load node and assign this
labelsym to the copied labelnode (and vice versa)
}
labelsym : tlabelsym;
labelnode : tlabelnode;
exceptionblock : integer;
constructor create(p : tlabelsym);virtual;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
procedure buildderefimpl;override;
procedure derefimpl;override;
procedure resolveppuidx;override;
function dogetcopy : tnode;override;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function docompare(p: tnode): boolean; override;
end;
tgotonodeclass = class of tgotonode;
tlabelnode = class(tunarynode)
exceptionblock : integer;
{ when copying trees, this points to the newly created copy of a label }
copiedto : tlabelnode;
labsym : tlabelsym;
constructor create(l:tnode;alabsym:tlabelsym);virtual;
destructor destroy;override;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
procedure ppuwrite(ppufile:tcompilerppufile);override;
procedure buildderefimpl;override;
procedure derefimpl;override;
function dogetcopy : tnode;override;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function docompare(p: tnode): boolean; override;
end;
tlabelnodeclass = class of tlabelnode;
traisenode = class(ttertiarynode)
constructor create(l,taddr,tframe:tnode);virtual;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
end;
traisenodeclass = class of traisenode;
ttryexceptnode = class(tloopnode)
constructor create(l,r,_t1 : tnode);virtual;reintroduce;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function simplify(forinline: boolean): tnode; override;
protected
procedure adjust_estimated_stack_size; virtual;
end;
ttryexceptnodeclass = class of ttryexceptnode;
{ the third node is to store a copy of the finally code for llvm:
it needs one copy to execute in case an exception occurs, and
one in case no exception occurs }
ttryfinallynode = class(ttertiarynode)
implicitframe : boolean;
constructor create(l,r:tnode);virtual;reintroduce;
constructor create_implicit(l,r:tnode);virtual;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function simplify(forinline:boolean): tnode;override;
protected
procedure adjust_estimated_stack_size; virtual;
public
function dogetcopy: tnode;override;
end;
ttryfinallynodeclass = class of ttryfinallynode;
tonnode = class(tbinarynode)
excepTSymtable : TSymtable;
excepttype : tobjectdef;
constructor create(l,r:tnode);virtual;
destructor destroy;override;
constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
function pass_typecheck:tnode;override;
function pass_1 : tnode;override;
function dogetcopy : tnode;override;
function docompare(p: tnode): boolean; override;
end;
tonnodeclass = class of tonnode;
var
cwhilerepeatnode : twhilerepeatnodeclass=twhilerepeatnode;
cifnode : tifnodeclass = tifnode;
cfornode : tfornodeclass = tfornode;
cexitnode : texitnodeclass = texitnode;
cgotonode : tgotonodeclass = tgotonode;
clabelnode : tlabelnodeclass = tlabelnode;
craisenode : traisenodeclass = traisenode;
ctryexceptnode : ttryexceptnodeclass = ttryexceptnode;
ctryfinallynode : ttryfinallynodeclass = ttryfinallynode;
connode : tonnodeclass = tonnode;
cbreaknode : tbreaknodeclass = tbreaknode;
ccontinuenode : tcontinuenodeclass = tcontinuenode;
// for-in loop helpers
function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
{ converts all for nodes in the tree into while nodes,
returns true if something was converted }
function ConvertForLoops(var n : tnode) : Boolean;
implementation
uses
globtype,systems,constexp,compinnr,
cutils,verbose,globals,ppu,
symtable,paramgr,defcmp,defutil,htypechk,pass_1,
ncal,nadd,ncon,nmem,nld,ncnv,nbas,nutils,ninl,nset,ngenutil,
{$ifdef state_tracking}
nstate,
{$endif}
{$ifdef i8086}
cpuinfo,
{$endif i8086}
{$if defined(xtensa) or defined(i386)}
cpuinfo,
{$endif defined(xtensa) or defined(i386)}
cgbase,procinfo
;
// for-in loop helpers
function create_type_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
begin
result:=cfornode.create(hloopvar,
cinlinenode.create(in_low_x,false,expr.getcopy),
cinlinenode.create(in_high_x,false,expr.getcopy),
hloopbody,
false);
end;
function create_objc_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
var
mainstatement, outerloopbodystatement, innerloopbodystatement, tempstatement: tstatementnode;
state, mutationcheck, currentamount, innerloopcounter, items, expressiontemp: ttempcreatenode;
outerloop, innerloop, hp: tnode;
itemsarraydef: tarraydef;
sym: tsym;
begin
{ Objective-C enumerators require Objective-C 2.0 }
if not(m_objectivec2 in current_settings.modeswitches) then
begin
result:=cerrornode.create;
MessagePos(expr.fileinfo,parser_e_objc_enumerator_2_0);
exit;
end;
{ Requires the NSFastEnumeration protocol and NSFastEnumerationState
record }
maybeloadcocoatypes;
if not assigned(objc_fastenumeration) or
not assigned(objc_fastenumerationstate) then
begin
result:=cerrornode.create;
MessagePos(expr.fileinfo,parser_e_objc_missing_enumeration_defs);
exit;
end;
(* Original code:
for hloopvar in expression do
<hloopbody>
Pascal code equivalent into which it has to be transformed
(sure would be nice if the compiler had some kind of templates ;) :
var
state: NSFastEnumerationState;
expressiontemp: NSFastEnumerationProtocol;
mutationcheck,
currentamount,
innerloopcounter: culong;
{ size can be increased/decreased if desired }
items: array[1..16] of id;
begin
fillchar(state,sizeof(state),0);
expressiontemp:=expression;
repeat
currentamount:=expressiontemp.countByEnumeratingWithState_objects_count(@state,@items,length(items));
if currentamount=0 then
begin
{ "The iterating variable is set to nil when the loop ends by
exhausting the source pool of objects" }
hloopvar:=nil;
break;
end;
mutationcheck:=state.mutationsptr^;
innerloopcounter:=culong(-1);
repeat
{ at the start so that "continue" in <loopbody> works correctly }
{ don't use for-loop, because then the value of the iteration
counter is undefined on exit and we have to check it in the
outer repeat/until condition }
{$push}
{$r-,q-}
inc(innerloopcounter);
{$pop}
if innerloopcounter=currentamount then
break;
if mutationcheck<>state.mutationsptr^ then
{ raises Objective-C exception... }
objc_enumerationMutation(expressiontemp);
hloopvar:=state.itemsPtr[innerloopcounter];
{ if continue in loopbody -> jumps to start, increases count and checks }
{ if break in loopbody: goes to outer repeat/until and innerloopcount
will be < currentamount -> stops }
<hloopbody>
until false;
{ if the inner loop terminated early, "break" was used and we have
to stop }
{ "If the loop is terminated early, the iterating variable is left
pointing to the last iteration item." }
until innerloopcounter<currentamount;
end;
*)
result:=internalstatements(mainstatement);
{ the fast enumeration state }
state:=ctempcreatenode.create(objc_fastenumerationstate,objc_fastenumerationstate.size,tt_persistent,false);
typecheckpass(tnode(state));
addstatement(mainstatement,state);
{ the temporary items array }
itemsarraydef:=carraydef.create(1,16,u32inttype);
itemsarraydef.elementdef:=objc_idtype;
items:=ctempcreatenode.create(itemsarraydef,itemsarraydef.size,tt_persistent,false);
addstatement(mainstatement,items);
typecheckpass(tnode(items));
{ temp for the expression/collection through which we iterate }
expressiontemp:=ctempcreatenode.create(objc_fastenumeration,objc_fastenumeration.size,tt_persistent,true);
addstatement(mainstatement,expressiontemp);
{ currentamount temp (not really clean: we use ptruint instead of
culong) }
currentamount:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
typecheckpass(tnode(currentamount));
addstatement(mainstatement,currentamount);
{ mutationcheck temp (idem) }
mutationcheck:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
typecheckpass(tnode(mutationcheck));
addstatement(mainstatement,mutationcheck);
{ innerloopcounter temp (idem) }
innerloopcounter:=ctempcreatenode.create(ptruinttype,ptruinttype.size,tt_persistent,true);
typecheckpass(tnode(innerloopcounter));
addstatement(mainstatement,innerloopcounter);
{ initialise the state with 0 }
addstatement(mainstatement,ccallnode.createinternfromunit('SYSTEM','FILLCHAR',
ccallparanode.create(genintconstnode(0),
ccallparanode.create(genintconstnode(objc_fastenumerationstate.size),
ccallparanode.create(ctemprefnode.create(state),nil)
)
)
));
{ this will also check whether the expression (potentially) conforms
to the NSFastEnumeration protocol (use expr.getcopy, because the
caller will free expr) }
addstatement(mainstatement,cassignmentnode.create(ctemprefnode.create(expressiontemp),expr.getcopy));
{ we add the "repeat..until" afterwards, now just create the body }
outerloop:=internalstatements(outerloopbodystatement);
{ the countByEnumeratingWithState_objects_count call }
hp:=ccallparanode.create(cinlinenode.create(in_length_x,false,ctypenode.create(itemsarraydef)),
ccallparanode.create(caddrnode.create(ctemprefnode.create(items)),
ccallparanode.create(caddrnode.create(ctemprefnode.create(state)),nil)
)
);
sym:=search_struct_member(objc_fastenumeration,'COUNTBYENUMERATINGWITHSTATE_OBJECTS_COUNT');
if not assigned(sym) or
(sym.typ<>procsym) then
internalerror(2010061901);
hp:=ccallnode.create(hp,tprocsym(sym),sym.owner,ctemprefnode.create(expressiontemp),[],nil);
addstatement(outerloopbodystatement,cassignmentnode.create(
ctemprefnode.create(currentamount),hp));
{ if currentamount = 0, bail out (use copy of hloopvar, because we
have to use it again below) }
hp:=internalstatements(tempstatement);
addstatement(tempstatement,cassignmentnode.create(
hloopvar.getcopy,cnilnode.create));
addstatement(tempstatement,cbreaknode.create);
addstatement(outerloopbodystatement,cifnode.create(
caddnode.create(equaln,ctemprefnode.create(currentamount),genintconstnode(0)),
hp,nil));
{ initial value of mutationcheck }
hp:=ctemprefnode.create(state);
typecheckpass(hp);
hp:=cderefnode.create(genloadfield(hp,'MUTATIONSPTR'));
addstatement(outerloopbodystatement,cassignmentnode.create(
ctemprefnode.create(mutationcheck),hp));
{ initialise innerloopcounter }
addstatement(outerloopbodystatement,cassignmentnode.create(
ctemprefnode.create(innerloopcounter),cordconstnode.create(-1,ptruinttype,false)));
{ and now the inner loop, again adding the repeat/until afterwards }
innerloop:=internalstatements(innerloopbodystatement);
{ inc(innerloopcounter) without range/overflowchecking (because
we go from culong(-1) to 0 during the first iteration }
hp:=cinlinenode.create(
in_inc_x,false,ccallparanode.create(
ctemprefnode.create(innerloopcounter),nil));
hp.localswitches:=hp.localswitches-[cs_check_range,cs_check_overflow];
addstatement(innerloopbodystatement,hp);
{ if innerloopcounter=currentamount then break to the outer loop }
addstatement(innerloopbodystatement,cifnode.create(
caddnode.create(equaln,
ctemprefnode.create(innerloopcounter),
ctemprefnode.create(currentamount)),
cbreaknode.create,
nil));
{ verify that the collection didn't change in the mean time }
hp:=ctemprefnode.create(state);
typecheckpass(hp);
addstatement(innerloopbodystatement,cifnode.create(
caddnode.create(unequaln,
ctemprefnode.create(mutationcheck),
cderefnode.create(genloadfield(hp,'MUTATIONSPTR'))
),
ccallnode.createinternfromunit('OBJC','OBJC_ENUMERATIONMUTATION',
ccallparanode.create(ctemprefnode.create(expressiontemp),nil)),
nil));
{ finally: actually get the next element }
hp:=ctemprefnode.create(state);
typecheckpass(hp);
hp:=genloadfield(hp,'ITEMSPTR');
typecheckpass(hp);
{ don't simply use a vecn, because indexing a pointer won't work in
non-FPC modes }
if hp.resultdef.typ<>pointerdef then
internalerror(2010061904);
inserttypeconv(hp,
carraydef.create_from_pointer(tpointerdef(hp.resultdef)));
hp:=cvecnode.create(hp,ctemprefnode.create(innerloopcounter));
addstatement(innerloopbodystatement,
cassignmentnode.create(hloopvar,hp));
{ the actual loop body! }
addstatement(innerloopbodystatement,hloopbody);
{ create the inner repeat/until and add it to the body of the outer
one }
hp:=cwhilerepeatnode.create(
{ repeat .. until false }
cordconstnode.create(0,pasbool1type,false),innerloop,false,true);
addstatement(outerloopbodystatement,hp);
{ create the outer repeat/until and add it to the the main body }
hp:=cwhilerepeatnode.create(
{ repeat .. until innerloopcounter<currentamount }
caddnode.create(ltn,
ctemprefnode.create(innerloopcounter),
ctemprefnode.create(currentamount)),
outerloop,false,true);
addstatement(mainstatement,hp);
{ release the temps }
addstatement(mainstatement,ctempdeletenode.create(state));
addstatement(mainstatement,ctempdeletenode.create(mutationcheck));
addstatement(mainstatement,ctempdeletenode.create(currentamount));
addstatement(mainstatement,ctempdeletenode.create(innerloopcounter));
addstatement(mainstatement,ctempdeletenode.create(items));
addstatement(mainstatement,ctempdeletenode.create(expressiontemp));
end;
function create_string_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
var
loopstatement, loopbodystatement: tstatementnode;
loopvar, stringvar: ttempcreatenode;
stringindex, loopbody, forloopnode: tnode;
begin
{ result is a block of statements }
result:=internalstatements(loopstatement);
{ create a temp variable for expression }
stringvar := ctempcreatenode.create(
expr.resultdef,
expr.resultdef.size,
tt_persistent,true);
addstatement(loopstatement,stringvar);
addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(stringvar),expr.getcopy));
{ create a loop counter: signed integer with size of string length }
loopvar := ctempcreatenode.create(
sinttype,
sinttype.size,
tt_persistent,true);
addstatement(loopstatement,loopvar);
stringindex:=ctemprefnode.create(loopvar);
loopbody:=internalstatements(loopbodystatement);
// for-in loop variable := string_expression[index]
addstatement(loopbodystatement,
cassignmentnode.create(hloopvar, cvecnode.create(ctemprefnode.create(stringvar),stringindex)));
{ add the actual statement to the loop }
addstatement(loopbodystatement,hloopbody);
forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
genintconstnode(1),
cinlinenode.create(in_length_x,false,ctemprefnode.create(stringvar)),
loopbody,
false);
addstatement(loopstatement,forloopnode);
{ free the loop counter }
addstatement(loopstatement,ctempdeletenode.create(loopvar));
{ free the temp variable for expression }
addstatement(loopstatement,ctempdeletenode.create(stringvar));
end;
function create_array_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
var
loopstatement, loopbodystatement: tstatementnode;
loopvar, arrayvar: ttempcreatenode;
arrayindex, lowbound, highbound, loopbody, forloopnode, expression: tnode;
is_string: boolean;
tmpdef, convertdef: tdef;
elementcount: aword;
begin
expression := expr;
{ result is a block of statements }
result:=internalstatements(loopstatement);
is_string:=ado_IsConstString in tarraydef(expr.resultdef).arrayoptions;
// if array element type <> loovar type then create a conversion if possible
if compare_defs(tarraydef(expression.resultdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible then
begin
tmpdef:=expression.resultdef;
elementcount:=1;
while assigned(tmpdef) and (tmpdef.typ=arraydef) and
(tarraydef(tmpdef).arrayoptions = []) and
(compare_defs(tarraydef(tmpdef).elementdef,hloopvar.resultdef,nothingn)=te_incompatible) do
begin
elementcount:=elementcount*tarraydef(tmpdef).elecount;
tmpdef:=tarraydef(tmpdef).elementdef;
end;
if assigned(tmpdef) and (tmpdef.typ=arraydef) and (tarraydef(tmpdef).arrayoptions = []) then
begin
elementcount:=elementcount*tarraydef(tmpdef).elecount;
convertdef:=carraydef.create(0,elementcount-1,s32inttype);
tarraydef(convertdef).elementdef:=tarraydef(tmpdef).elementdef;
expression:=expr.getcopy;
expression:=ctypeconvnode.create_internal(expression,convertdef);
typecheckpass(expression);
addstatement(loopstatement,expression);
end;
end;
if (node_complexity(expression) > 1) and
not(is_open_array(expression.resultdef)) and not(is_array_of_const(expression.resultdef)) then
begin
{ create a temp variable for expression }
arrayvar := ctempcreatenode.create(
expression.resultdef,
expression.resultdef.size,
tt_persistent,true);
if is_string then
begin
lowbound:=genintconstnode(1);
highbound:=cinlinenode.create(in_length_x,false,ctemprefnode.create(arrayvar))
end
else
begin
lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
end;
addstatement(loopstatement,arrayvar);
addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(arrayvar),expression.getcopy));
end
else
begin
arrayvar:=nil;
if is_string then
begin
lowbound:=genintconstnode(1);
highbound:=cinlinenode.create(in_length_x,false,expression.getcopy);
end
else
begin
lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
end;
end;
{ create a loop counter }
loopvar := ctempcreatenode.create(
tarraydef(expression.resultdef).rangedef,
tarraydef(expression.resultdef).rangedef.size,
tt_persistent,true);
addstatement(loopstatement,loopvar);
arrayindex:=ctemprefnode.create(loopvar);
loopbody:=internalstatements(loopbodystatement);
// for-in loop variable := array_expression[index]
if assigned(arrayvar) then
addstatement(loopbodystatement,
cassignmentnode.create(hloopvar,cvecnode.create(ctemprefnode.create(arrayvar),arrayindex)))
else
addstatement(loopbodystatement,
cassignmentnode.create(hloopvar,cvecnode.create(expression.getcopy,arrayindex)));
{ add the actual statement to the loop }
addstatement(loopbodystatement,hloopbody);
forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
lowbound,
highbound,
loopbody,
false);
addstatement(loopstatement,forloopnode);
{ free the loop counter }
addstatement(loopstatement,ctempdeletenode.create(loopvar));
{ free the temp variable for expression if needed }
if arrayvar<>nil then
addstatement(loopstatement,ctempdeletenode.create(arrayvar));
end;
function create_set_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
var
loopstatement, loopbodystatement: tstatementnode;
loopvar, setvar: ttempcreatenode;
loopbody, forloopnode: tnode;
begin
// first check is set is empty and if it so then skip other processing
if not Assigned(tsetdef(expr.resultdef).elementdef) then
begin
result:=cnothingnode.create;
// free unused nodes
hloopvar.free;
hloopbody.free;
exit;
end;
{ result is a block of statements }
result:=internalstatements(loopstatement);
{ create a temp variable for expression }
setvar := ctempcreatenode.create(
expr.resultdef,
expr.resultdef.size,
tt_persistent,true);
addstatement(loopstatement,setvar);
addstatement(loopstatement,cassignmentnode.create(ctemprefnode.create(setvar),expr.getcopy));
{ create a loop counter }
loopvar := ctempcreatenode.create(
tsetdef(expr.resultdef).elementdef,
tsetdef(expr.resultdef).elementdef.size,
tt_persistent,true);
addstatement(loopstatement,loopvar);
// if loopvar in set then
// begin
// hloopvar := loopvar
// for-in loop body
// end
loopbody:=cifnode.create(
cinnode.create(ctemprefnode.create(loopvar),ctemprefnode.create(setvar)),
internalstatements(loopbodystatement),
nil);
addstatement(loopbodystatement,cassignmentnode.create(hloopvar,ctemprefnode.create(loopvar)));
{ add the actual statement to the loop }
addstatement(loopbodystatement,hloopbody);
forloopnode:=cfornode.create(ctemprefnode.create(loopvar),
cinlinenode.create(in_low_x,false,ctemprefnode.create(setvar)),
cinlinenode.create(in_high_x,false,ctemprefnode.create(setvar)),
loopbody,
false);
addstatement(loopstatement,forloopnode);
{ free the loop counter }
addstatement(loopstatement,ctempdeletenode.create(loopvar));
{ free the temp variable for expression }
addstatement(loopstatement,ctempdeletenode.create(setvar));
end;
function create_enumerator_for_in_loop(hloopvar, hloopbody, expr: tnode;
enumerator_get, enumerator_move: tprocdef; enumerator_current: tpropertysym): tnode;
var
loopstatement, loopbodystatement: tstatementnode;
enumvar: ttempcreatenode;
loopbody, whileloopnode,
enum_get, enum_move, enum_current, enum_get_params: tnode;
propaccesslist: tpropaccesslist;
enumerator_is_class: boolean;
enumerator_destructor: tprocdef;
begin
{ result is a block of statements }
result:=internalstatements(loopstatement);
enumerator_is_class := is_class(enumerator_get.returndef);
{ create a temp variable for enumerator }
enumvar := ctempcreatenode.create(
enumerator_get.returndef,
enumerator_get.returndef.size,
tt_persistent,true);
addstatement(loopstatement,enumvar);
if enumerator_get.proctypeoption=potype_operator then
begin
enum_get_params:=ccallparanode.create(expr.getcopy,nil);
enum_get:=ccallnode.create(enum_get_params, tprocsym(enumerator_get.procsym), nil, nil, [],nil);
tcallnode(enum_get).procdefinition:=enumerator_get;
addsymref(enumerator_get.procsym,enumerator_get);
end
else
enum_get:=ccallnode.create(nil, tprocsym(enumerator_get.procsym), enumerator_get.owner, expr.getcopy, [],nil);
addstatement(loopstatement,
cassignmentnode.create(
ctemprefnode.create(enumvar),
enum_get
));
loopbody:=internalstatements(loopbodystatement);
{ for-in loop variable := enumerator.current }
if enumerator_current.getpropaccesslist(palt_read,propaccesslist) then
begin
case propaccesslist.firstsym^.sym.typ of
fieldvarsym :
begin
{ generate access code }
enum_current:=ctemprefnode.create(enumvar);
propaccesslist_to_node(enum_current,enumerator_current.owner,propaccesslist);
include(enum_current.flags,nf_isproperty);
end;
procsym :
begin
{ generate the method call }
enum_current:=ccallnode.create(nil,tprocsym(propaccesslist.firstsym^.sym),enumerator_current.owner,ctemprefnode.create(enumvar),[],nil);
include(enum_current.flags,nf_isproperty);
end
else
begin
enum_current:=cerrornode.create;
Message(type_e_mismatch);
end;
end;
end
else
enum_current:=cerrornode.create;
addstatement(loopbodystatement,
cassignmentnode.create(hloopvar, enum_current));
{ add the actual statement to the loop }
addstatement(loopbodystatement,hloopbody);
enum_move:=ccallnode.create(nil, tprocsym(enumerator_move.procsym), enumerator_move.owner, ctemprefnode.create(enumvar), [],nil);
whileloopnode:=cwhilerepeatnode.create(enum_move,loopbody,true,false);
if enumerator_is_class then
begin
{ insert a try-finally and call the destructor for the enumerator in the finally section }
enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
if assigned(enumerator_destructor) then
begin
whileloopnode:=ctryfinallynode.create(
whileloopnode, // try node
ccallnode.create(nil,tprocsym(enumerator_destructor.procsym), // finally node
enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
end;
{ if getenumerator <> nil then do the loop }
whileloopnode:=cifnode.create(
caddnode.create(unequaln, ctemprefnode.create(enumvar), cnilnode.create),
whileloopnode,
nil);
end;
addstatement(loopstatement, whileloopnode);
if is_object(enumerator_get.returndef) then
begin
// call the object destructor too
enumerator_destructor:=tobjectdef(enumerator_get.returndef).find_destructor;
if assigned(enumerator_destructor) then
begin
addstatement(loopstatement,
ccallnode.create(nil,tprocsym(enumerator_destructor.procsym),
enumerator_destructor.procsym.owner,ctemprefnode.create(enumvar),[],nil));
end;
end;
{ free the temp variable for enumerator }
addstatement(loopstatement,ctempdeletenode.create(enumvar));
end;
function create_for_in_loop(hloopvar, hloopbody, expr: tnode): tnode;
var
pd, movenext: tprocdef;
helperdef: tobjectdef;
current: tpropertysym;
storefilepos: tfileposinfo;
begin
storefilepos:=current_filepos;
current_filepos:=hloopvar.fileinfo;
if expr.nodetype=typen then
begin
if (expr.resultdef.typ=enumdef) and tenumdef(expr.resultdef).has_jumps then
begin
result:=cerrornode.create;
hloopvar.free;
hloopbody.free;
MessagePos1(expr.fileinfo,parser_e_for_in_loop_cannot_be_used_for_the_type,expr.resultdef.typename);
end
else
result:=create_type_for_in_loop(hloopvar, hloopbody, expr);
end
else
begin
{ loop is made for an expression }
// Objective-C uses different conventions (and it's only supported for Objective-C 2.0)
if is_objc_class_or_protocol(hloopvar.resultdef) or
is_objc_class_or_protocol(expr.resultdef) then
begin
result:=create_objc_for_in_loop(hloopvar,hloopbody,expr);
if result.nodetype=errorn then
begin
hloopvar.free;
hloopbody.free;
end;
end
{ "for x in [] do ..." always results in a never executed loop body }
else if (is_array_constructor(expr.resultdef) and
(tarraydef(expr.resultdef).elementdef=voidtype)) then
begin
if assigned(hloopbody) then
MessagePos(hloopbody.fileinfo,cg_w_unreachable_code);
result:=cnothingnode.create;
end
else
begin
// search for operator first
pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
// if there is no operator then search for class/object enumerator method
if (pd=nil) and (expr.resultdef.typ in [objectdef,recorddef]) then
begin
{ first search using the helper hierarchy }
if search_last_objectpascal_helper(tabstractrecorddef(expr.resultdef),nil,helperdef) then
repeat
pd:=helperdef.search_enumerator_get;
helperdef:=helperdef.childof;
until (pd<>nil) or (helperdef=nil);
{ we didn't find an enumerator in a helper, so search in the
class/record/object itself }
if pd=nil then
pd:=tabstractrecorddef(expr.resultdef).search_enumerator_get;
end;
if pd<>nil then
begin
// seach movenext and current symbols
movenext:=tabstractrecorddef(pd.returndef).search_enumerator_move;
if movenext = nil then
begin
result:=cerrornode.create;
hloopvar.free;
hloopbody.free;
MessagePos1(expr.fileinfo,sym_e_no_enumerator_move,pd.returndef.typename);
end
else
begin
current:=tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
if current = nil then
begin
result:=cerrornode.create;
hloopvar.free;
hloopbody.free;
MessagePos1(expr.fileinfo,sym_e_no_enumerator_current,pd.returndef.typename);
end
else
result:=create_enumerator_for_in_loop(hloopvar, hloopbody, expr, pd, movenext, current);
end;
end
else
begin
{ prefer set if loop var could be a set var and the loop
expression can indeed be a set }
if (expr.nodetype=arrayconstructorn) and
(hloopvar.resultdef.typ in [enumdef,orddef]) and
arrayconstructor_can_be_set(expr) then
begin
expr:=arrayconstructor_to_set(expr,false);
typecheckpass(expr);
end;
case expr.resultdef.typ of
stringdef:
result:=create_string_for_in_loop(hloopvar, hloopbody, expr);
arraydef:
result:=create_array_for_in_loop(hloopvar, hloopbody, expr);
setdef:
result:=create_set_for_in_loop(hloopvar, hloopbody, expr);
undefineddef:
result:=cnothingnode.create;
else
begin
result:=cerrornode.create;
hloopvar.free;
hloopbody.free;
MessagePos1(expr.fileinfo,sym_e_no_enumerator,expr.resultdef.typename);
end;
end;
end;
end;
end;
current_filepos:=storefilepos;
end;
function _ConvertForLoops(var n: tnode; arg: pointer): foreachnoderesult;
var
hp : tnode;
begin
Result:=fen_false;
if n.nodetype=forn then
begin
Result:=fen_true;
hp:=n;
n:=tfornode(n).makewhileloop;
do_firstpass(n);
hp.Free;
end;
end;
function ConvertForLoops(var n : tnode) : boolean;
begin
result:=foreachnodestatic(pm_postprocess,n,@_ConvertForLoops,nil);
end;
{****************************************************************************
TLOOPNODE
*****************************************************************************}
constructor tloopnode.create(tt : tnodetype;l,r,_t1,_t2 : tnode);
begin
inherited create(tt,l,r);
t1:=_t1;
t2:=_t2;
fileinfo:=l.fileinfo;
end;
destructor tloopnode.destroy;
begin
t1.free;
t2.free;
inherited destroy;
end;
constructor tloopnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
t1:=ppuloadnode(ppufile);
t2:=ppuloadnode(ppufile);
ppufile.getset(tppuset1(loopflags));
end;
procedure tloopnode.ppuwrite(ppufile:tcompilerppufile);
begin
inherited ppuwrite(ppufile);
ppuwritenode(ppufile,t1);
ppuwritenode(ppufile,t2);
ppufile.putset(tppuset1(loopflags));
end;
procedure tloopnode.buildderefimpl;
begin
inherited buildderefimpl;
if assigned(t1) then
t1.buildderefimpl;
if assigned(t2) then
t2.buildderefimpl;
end;
procedure tloopnode.derefimpl;
begin
inherited derefimpl;
if assigned(t1) then
t1.derefimpl;
if assigned(t2) then
t2.derefimpl;
end;
function tloopnode.dogetcopy : tnode;
var
p : tloopnode;
begin
p:=tloopnode(inherited dogetcopy);
if assigned(t1) then
p.t1:=t1.dogetcopy
else
p.t1:=nil;
if assigned(t2) then
p.t2:=t2.dogetcopy
else
p.t2:=nil;
p.loopflags:=loopflags;
dogetcopy:=p;
end;
procedure tloopnode.insertintolist(l : tnodelist);
begin
end;
procedure tloopnode.printnodetree(var t:text);
begin
write(t,printnodeindention,'(');
printnodeindent;
printnodeinfo(t);
writeln(t);
printnode(t,left);
printnode(t,right);
printnode(t,t1);
printnode(t,t2);
printnodeunindent;
writeln(t,printnodeindention,')');
end;
{$ifdef DEBUG_NODE_XML}
procedure TLoopNode.XMLPrintNodeInfo(var T: Text);
var
i: TLoopFlag;
First: Boolean;
begin
inherited XMLPrintNodeInfo(T);
First := True;
for i := Low(TLoopFlag) to High(TLoopFlag) do
if i in loopflags then
begin
if First then
begin
Write(T, ' loopflags="', i);
First := False;
end
else
Write(T, ',', i)
end;
if not First then
Write(T, '"');
end;
procedure TLoopNode.XMLPrintNodeTree(var T: Text);
begin
Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
XMLPrintNodeInfo(T);
WriteLn(T, '>');
PrintNodeIndent;
if Assigned(Left) then
begin
if nodetype = forn then
WriteLn(T, PrintNodeIndention, '<counter>')
else
WriteLn(T, PrintNodeIndention, '<condition>');
PrintNodeIndent;
XMLPrintNode(T, Left);
PrintNodeUnindent;
if nodetype = forn then
WriteLn(T, PrintNodeIndention, '</counter>')
else
WriteLn(T, PrintNodeIndention, '</condition>');
end;
if Assigned(Right) then
begin
case nodetype of
ifn:
WriteLn(T, PrintNodeIndention, '<then>');
forn:
WriteLn(T, PrintNodeIndention, '<first>');
else
WriteLn(T, PrintNodeIndention, '<right>');
end;
PrintNodeIndent;
XMLPrintNode(T, Right);
PrintNodeUnindent;
case nodetype of
ifn:
WriteLn(T, PrintNodeIndention, '</then>');
forn:
WriteLn(T, PrintNodeIndention, '</first>');
else
WriteLn(T, PrintNodeIndention, '</right>');
end;
end;
if Assigned(t1) then
begin
case nodetype of
ifn:
WriteLn(T, PrintNodeIndention, '<else>');
forn:
WriteLn(T, PrintNodeIndention, '<last>');
else
WriteLn(T, PrintNodeIndention, '<t1>');
end;
PrintNodeIndent;
XMLPrintNode(T, t1);
PrintNodeUnindent;
case nodetype of
ifn:
WriteLn(T, PrintNodeIndention, '</else>');
forn:
WriteLn(T, PrintNodeIndention, '</last>');
else
WriteLn(T, PrintNodeIndention, '</t1>');
end;
end;
if Assigned(t2) then
begin
if nodetype <> forn then
begin
WriteLn(T, PrintNodeIndention, '<loop>');
PrintNodeIndent;
end;
XMLPrintNode(T, t2);
if nodetype <> forn then
begin
PrintNodeUnindent;
WriteLn(T, PrintNodeIndention, '</loop>');
end;
end;
PrintNodeUnindent;
WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
end;
{$endif DEBUG_NODE_XML}
function tloopnode.docompare(p: tnode): boolean;
begin
docompare :=
inherited docompare(p) and
(loopflags*loopflagsequal=tloopnode(p).loopflags*loopflagsequal) and
t1.isequal(tloopnode(p).t1) and
t2.isequal(tloopnode(p).t2);
end;
{****************************************************************************
TWHILEREPEATNODE
*****************************************************************************}
constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
begin
inherited create(whilerepeatn,l,r,nil,nil);
if tab then
include(loopflags, lnf_testatbegin);
if cn then
include(loopflags,lnf_checknegate);
end;
function twhilerepeatnode.pass_typecheck:tnode;
var
t:Tunarynode;
begin
result:=nil;
resultdef:=voidtype;
typecheckpass(left);
{ tp procvar support }
maybe_call_procvar(left,true);
{A not node can be removed.}
if left.nodetype=notn then
begin
t:=Tunarynode(left);
left:=Tunarynode(left).left;
t.left:=nil;
t.destroy;
{Symdif operator, in case you are wondering:}
loopflags:=loopflags >< [lnf_checknegate];
end;
{ loop instruction }
if assigned(right) then
typecheckpass(right);
set_varstate(left,vs_read,[vsf_must_be_valid]);
if codegenerror then
exit;
if not(is_boolean(left.resultdef)) and
not(is_typeparam(left.resultdef)) then
inserttypeconv(left,pasbool1type);
{ Give warnings for code that will never be executed for
while false do }
if (lnf_testatbegin in loopflags) and
(left.nodetype=ordconstn) and
(tordconstnode(left).value.uvalue=0) and
not(nf_internal in left.flags) and
assigned(right) then
CGMessagePos(right.fileinfo,cg_w_unreachable_code);
end;
{$ifdef prefetchnext}
type
passignmentquery = ^tassignmentquery;
tassignmentquery = record
towhat: tnode;
source: tassignmentnode;
statementcount: cardinal;
end;
function checkassignment(var n: tnode; arg: pointer): foreachnoderesult;
var
query: passignmentquery absolute arg;
temp, prederef: tnode;
begin
result := fen_norecurse_false;
if (n.nodetype in [assignn,inlinen,forn,calln,whilerepeatn,casen,ifn]) then
inc(query^.statementcount);
{ make sure there's something else in the loop besides going to the }
{ next item }
if (query^.statementcount > 1) and
(n.nodetype = assignn) then
begin
{ skip type conversions of assignment target }
temp := tassignmentnode(n).left;
while (temp.nodetype = typeconvn) do
temp := ttypeconvnode(temp).left;
{ assignment to x of the while assigned(x) check? }
if not(temp.isequal(query^.towhat)) then
exit;
{ right hand side of assignment dereferenced field of }
{ x? (no derefn in case of class) }
temp := tassignmentnode(n).right;
while (temp.nodetype = typeconvn) do
temp := ttypeconvnode(temp).left;
if (temp.nodetype <> subscriptn) then
exit;
prederef := tsubscriptnode(temp).left;
temp := prederef;
while (temp.nodetype = typeconvn) do
temp := ttypeconvnode(temp).left;
{ see tests/test/prefetch1.pp }
if (temp.nodetype = derefn) then
temp := tderefnode(temp).left
else
temp := prederef;
if temp.isequal(query^.towhat) then
begin
query^.source := tassignmentnode(n);
result := fen_norecurse_true;
end
end
{ don't check nodes which can't contain an assignment or whose }
{ final assignment can vary a lot }
else if not(n.nodetype in [calln,inlinen,casen,whilerepeatn,forn]) then
result := fen_false;
end;
function findassignment(where: tnode; towhat: tnode): tassignmentnode;
var
query: tassignmentquery;
begin
query.towhat := towhat;
query.source := nil;
query.statementcount := 0;
if foreachnodestatic(where,@checkassignment,@query) then
result := query.source
else
result := nil;
end;
{$endif prefetchnext}
function twhilerepeatnode.pass_1 : tnode;
{$ifdef prefetchnext}
var
runnernode, prefetchcode: tnode;
assignmentnode: tassignmentnode;
prefetchstatements: tstatementnode;
{$endif prefetchnext}
begin
result:=nil;
expectloc:=LOC_VOID;
firstpass(left);
if codegenerror then
exit;
{ loop instruction }
if assigned(right) then
begin
firstpass(right);
if codegenerror then
exit;
end;
{$ifdef prefetchnext}
{ do at the end so all complex typeconversions are already }
{ converted to calln's }
if (cs_opt_level1 in current_settings.optimizerswitches) and
(lnf_testatbegin in loopflags) then
begin
{ get first component of the while check }
runnernode := left;
while (runnernode.nodetype in [andn,orn,notn,xorn,typeconvn]) do
runnernode := tunarynode(runnernode).left;
{ is it an assigned(x) check? }
if ((runnernode.nodetype = inlinen) and
(tinlinenode(runnernode).inlinenumber = in_assigned_x)) or
((runnernode.nodetype = unequaln) and
(taddnode(runnernode).right.nodetype = niln)) then
begin
runnernode := tunarynode(runnernode).left;
{ in case of in_assigned_x, there's a callparan in between }
if (runnernode.nodetype = callparan) then
runnernode := tcallparanode(runnernode).left;
while (runnernode.nodetype = typeconvn) do
runnernode := ttypeconvnode(runnernode).left;
{ is there an "x := x(^).somefield"? }
assignmentnode := findassignment(right,runnernode);
if assigned(assignmentnode) then
begin
prefetchcode := internalstatements(prefetchstatements);
addstatement(prefetchstatements,geninlinenode(in_prefetch_var,false,
cderefnode.create(ctypeconvnode.create(assignmentnode.right.getcopy,voidpointertype))));
addstatement(prefetchstatements,right);
right := prefetchcode;
typecheckpass(right);
end;
end;
end;
{$endif prefetchnext}
end;
{$ifdef state_tracking}
function Twhilerepeatnode.track_state_pass(exec_known:boolean):boolean;
var condition:Tnode;
code:Tnode;
done:boolean;
value:boolean;
change:boolean;
firsttest:boolean;
factval:Tnode;
begin
track_state_pass:=false;
done:=false;
firsttest:=true;
{For repeat until statements, first do a pass through the code.}
if not(lnf_testatbegin in flags) then
begin
code:=right.getcopy;
if code.track_state_pass(exec_known) then
track_state_pass:=true;
code.destroy;
end;
repeat
condition:=left.getcopy;
code:=right.getcopy;
change:=condition.track_state_pass(exec_known);
factval:=aktstate.find_fact(left);
if factval<>nil then
begin
condition.destroy;
condition:=factval.getcopy;
change:=true;
end;
if change then
begin
track_state_pass:=true;
{Force new resultdef pass.}
condition.resultdef:=nil;
do_typecheckpass(condition);
end;
if is_constboolnode(condition) then
begin
{Try to turn a while loop into a repeat loop.}
if firsttest then
exclude(flags,testatbegin);
value:=(Tordconstnode(condition).value<>0) xor checknegate;
if value then
begin
if code.track_state_pass(exec_known) then
track_state_pass:=true;
end
else
done:=true;
end
else
begin
{Remove any modified variables from the state.}
code.track_state_pass(false);
done:=true;
end;
code.destroy;
condition.destroy;
firsttest:=false;
until done;
{The loop condition is also known, for example:
while i<10 do
begin
...
end;
When the loop is done, we do know that i<10 = false.
}
condition:=left.getcopy;
if condition.track_state_pass(exec_known) then
begin
track_state_pass:=true;
{Force new resultdef pass.}
condition.resultdef:=nil;
do_typecheckpass(condition);
end;
if not is_constboolnode(condition) then
aktstate.store_fact(condition,
cordconstnode.create(byte(checknegate),pasbool1type,true))
else
condition.destroy;
end;
{$endif}
{*****************************************************************************
TIFNODE
*****************************************************************************}
constructor tifnode.create(l,r,_t1 : tnode);
begin
inherited create(ifn,l,r,_t1,nil);
end;
constructor tifnode.create_internal(l,r,_t1 : tnode);
begin
create(l,r,_t1);
include(flags,nf_internal);
end;
function tifnode.internalsimplify(warn: boolean) : tnode;
{$if defined(i386) or defined(x86_64) or defined(xtensa)}
var
thenstmnt, elsestmnt: tnode;
in_nr: tinlinenumber;
paratype: tdef;
{$endif}
begin
result:=nil;
{ optimize constant expressions }
if (left.nodetype=ordconstn) then
begin
if tordconstnode(left).value.uvalue<>0 then
begin
if assigned(right) then
result:=right
else
result:=cnothingnode.create;
right:=nil;
if warn and assigned(t1) and not(nf_internal in left.flags) then
CGMessagePos(t1.fileinfo,cg_w_unreachable_code);
end
else
begin
if assigned(t1) then
result:=t1
else
result:=cnothingnode.create;
t1:=nil;
if warn and assigned(right) and not(nf_internal in left.flags) then
CGMessagePos(right.fileinfo,cg_w_unreachable_code);
end;
end;
{$ifndef llvm}
{$if defined(i386) or defined(x86_64) or defined(xtensa)}
{ use min/max intrinsic?
convert (with <op> being <, >, >=, <=
if a <op> b then
x:=a
else
x:=b;
and
if a <op> b then
x:=a;
into appropriate min/max intrinsics
}
elsestmnt:=nil;
in_nr:=Default(tinlinenumber);
if (cs_opt_level2 in current_settings.optimizerswitches) and
(left.nodetype in [gtn,gten,ltn,lten]) and IsSingleStatement(right,thenstmnt) and
((t1=nil) or IsSingleStatement(t1,elsestmnt)) and
(thenstmnt.nodetype=assignn) and ((t1=nil) or (elsestmnt.nodetype=assignn)) and
not(might_have_sideeffects(left)) and
((t1=nil) or tassignmentnode(thenstmnt).left.isequal(tassignmentnode(elsestmnt).left)) and
{$if defined(i386) or defined(x86_64)}
{$ifdef i386}
(((current_settings.fputype>=fpu_sse) and is_single(tassignmentnode(thenstmnt).left.resultdef)) or
((current_settings.fputype>=fpu_sse2) and is_double(tassignmentnode(thenstmnt).left.resultdef))
) and
{$else i386}
(is_single(tassignmentnode(thenstmnt).left.resultdef) or is_double(tassignmentnode(thenstmnt).left.resultdef)) and
{$endif i386}
{$endif defined(i386) or defined(x86_64)}
{$if defined(xtensa)}
(CPUXTENSA_HAS_MINMAX in cpu_capabilities[current_settings.cputype]) and is_32bitint(tassignmentnode(thenstmnt).right.resultdef) and
{$endif defined(xtensa)}
(
{ the right size of the assignment in the then clause must either }
{ equal to the left ... }
(tassignmentnode(thenstmnt).right.isequal(taddnode(left).left) and
{ ... and the else clause must be either not exist }
{ and the left side of the assignment in the then clause must be }
{ equal to the right operand of the comparison operator }
(
((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).right))) or
{ or the else clause exists and the right side of the assignment in the else clause }
{ must be equal to the right side of the comparison operator }
(assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).right)))
) or
{ ... or right operand of the comparison operator }
(tassignmentnode(thenstmnt).right.isequal(taddnode(left).right) and
{ ... and the else clause must be either not exist }
{ and the left side of the assignment in the then clause must be }
{ equal to the left operand of the comparison operator }
(
((t1=nil) and (tassignmentnode(thenstmnt).left.isequal(taddnode(left).left))) or
{ or the else clause exists and the right side of the assignment in the else clause }
{ must be equal to the left side of the comparison operator }
(assigned(elsestmnt) and tassignmentnode(elsestmnt).right.isequal(taddnode(left).left))
)
)
) then
begin
paratype:=tassignmentnode(thenstmnt).left.resultdef;
if ((left.nodetype in [gtn,gten]) and
tassignmentnode(thenstmnt).right.isequal(taddnode(left).left)) or
((left.nodetype in [ltn,lten]) and
tassignmentnode(thenstmnt).right.isequal(taddnode(left).right)) then
begin
if is_double(paratype) then
in_nr:=in_max_double
else if is_single(paratype) then
in_nr:=in_max_single
else if is_u32bitint(paratype) then
in_nr:=in_max_dword
else if is_s32bitint(paratype) then
in_nr:=in_max_longint;
end
else
begin
if is_double(paratype) then
in_nr:=in_min_double
else if is_single(paratype) then
in_nr:=in_min_single
else if is_u32bitint(paratype) then
in_nr:=in_min_dword
else if is_s32bitint(paratype) then
in_nr:=in_min_longint;
end;
{ for inline nodes, the first parameter is the last one in the linked list
Due to the defined behaviour for the min/max intrinsics that in case of a NaN
the second parameter is taken, we have to put the else part into the second parameter
thus pass it to the first callparanode call }
if t1=nil then
Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(thenstmnt).left.getcopy,
ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
)
else
Result:=cassignmentnode.create_internal(tassignmentnode(thenstmnt).left.getcopy,
cinlinenode.create(in_nr,false,ccallparanode.create(tassignmentnode(elsestmnt).right.getcopy,
ccallparanode.create(tassignmentnode(thenstmnt).right.getcopy,nil)))
);
end;
{$endif defined(i386) or defined(x86_64) or defined(xtensa)}
{$endif llvm}
end;
function tifnode.simplify(forinline : boolean) : tnode;
begin
result:=internalsimplify(false);
end;
function tifnode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
typecheckpass(left);
{ tp procvar support }
maybe_call_procvar(left,true);
{ if path }
if assigned(right) then
typecheckpass(right);
{ else path }
if assigned(t1) then
typecheckpass(t1);
set_varstate(left,vs_read,[vsf_must_be_valid]);
if codegenerror then
exit;
if not(is_boolean(left.resultdef)) and
not(is_typeparam(left.resultdef)) then
inserttypeconv(left,pasbool1type);
result:=internalsimplify(not(nf_internal in flags));
end;
function tifnode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
firstpass(left);
{ if path }
if assigned(right) then
firstpass(right);
{ else path }
if assigned(t1) then
firstpass(t1);
{ leave if we've got an error in one of the paths }
if codegenerror then
exit;
end;
{*****************************************************************************
TFORNODE
*****************************************************************************}
constructor tfornode.create(l,r,_t1,_t2 : tnode;back : boolean);
begin
inherited create(forn,l,r,_t1,_t2);
if back then
include(loopflags,lnf_backward);
include(loopflags,lnf_testatbegin);
end;
function tfornode.simplify(forinline : boolean) : tnode;
begin
result:=nil;
{ Can we spare the first comparision? }
if (t1.nodetype=ordconstn) and
(right.nodetype=ordconstn) and
(
(
(lnf_backward in loopflags) and
(Tordconstnode(right).value>=Tordconstnode(t1).value)
) or
(
not(lnf_backward in loopflags) and
(Tordconstnode(right).value<=Tordconstnode(t1).value)
)
) then
exclude(loopflags,lnf_testatbegin);
if (t1.nodetype=ordconstn) and
(right.nodetype=ordconstn) and
(
(
(lnf_backward in loopflags) and
(tordconstnode(right).value<tordconstnode(t1).value)
) or
(
not(lnf_backward in loopflags) and
(tordconstnode(right).value>tordconstnode(t1).value)
)
) then
result:=cnothingnode.create;
end;
function tfornode.pass_typecheck:tnode;
var
res : tnode;
rangedef: tdef;
begin
result:=nil;
resultdef:=voidtype;
{ process the loopvar, from and to, varstates are already set }
typecheckpass(left);
typecheckpass(right);
typecheckpass(t1);
set_varstate(left,vs_written,[]);
{ Make sure that the loop var and the
from and to values are compatible types }
if not(m_iso in current_settings.modeswitches) then
rangedef:=left.resultdef
else
rangedef:=get_iso_range_type(left.resultdef);
check_ranges(right.fileinfo,right,rangedef);
inserttypeconv(right,rangedef);
check_ranges(t1.fileinfo,t1,rangedef);
inserttypeconv(t1,rangedef);
if assigned(t2) then
typecheckpass(t2);
result:=simplify(false);
{ loop unrolling }
if not(assigned(result)) and
(cs_opt_loopunroll in current_settings.optimizerswitches) and
assigned(t2) and
{ statements must be error free }
not(nf_error in t2.flags) then
begin
typecheckpass(t2);
res:=t2.simplify(false);
if assigned(res) then
t2:=res;
res:=unroll_loop(self);
if assigned(res) then
begin
typecheckpass(res);
result:=res;
exit;
end;
end;
end;
function tfornode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
firstpass(left);
firstpass(right);
firstpass(t1);
if assigned(t2) then
firstpass(t2);
end;
function checkcontinue(var n:tnode; arg: pointer): foreachnoderesult;
begin
if n.nodetype=continuen then
result:=fen_norecurse_true
else
result:=fen_false;
end;
function tfornode.makewhileloop : tnode;
var
ifblock,loopblock : tblocknode;
ifstatements,statements,loopstatements : tstatementnode;
fromtemp,totemp : ttempcreatenode;
do_loopvar_at_end : Boolean;
{ if the lower and/or upper bound are variable, we need a surrounding if }
needsifblock : Boolean;
cond : tnodetype;
fromexpr : tnode;
toexpr, leftcopy: tnode;
{ if the upper bound is not constant, it must be store in a temp initially }
usetotemp : boolean;
{ if the lower bound is not constant, it must be store in a temp before calculating the upper bound }
usefromtemp : boolean;
storefilepos: tfileposinfo;
countermin, countermax: Tconstexprint;
procedure iterate_counter(var s : tstatementnode;fw : boolean);
var
leftcopy: tnode;
begin
{ get rid of nf_write etc. as the left node is now only read }
leftcopy:=left.getcopy;
node_reset_flags(leftcopy,[nf_pass1_done,nf_modify,nf_write]);
if fw then
addstatement(s,
cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_succ_x,false,leftcopy)))
else
addstatement(s,
cassignmentnode.create_internal(left.getcopy,cinlinenode.createintern(in_pred_x,false,leftcopy)));
end;
function iterate_counter_func(arg : tnode;fw : boolean) : tnode;
begin
if fw then
result:=cinlinenode.createintern(in_succ_x,false,arg)
else
result:=cinlinenode.createintern(in_pred_x,false,arg);
end;
begin
result:=nil;
totemp:=nil;
fromtemp:=nil;
storefilepos:=current_filepos;
current_filepos:=fileinfo;
case left.resultdef.typ of
enumdef:
begin
countermin:=tenumdef(left.resultdef).min;
countermax:=tenumdef(left.resultdef).max;
end;
orddef:
begin
countermin:=torddef(left.resultdef).low;
countermax:=torddef(left.resultdef).high;
end;
else
Internalerror(2020012601);
end;
{ check if we can pred/succ the loop var at the end }
do_loopvar_at_end:=(lnf_dont_mind_loopvar_on_exit in loopflags) and
is_constnode(right) and is_constnode(t1) and
{ we cannot test at the end after the pred/succ if the to value is equal to the max./min. value of the counter variable
because we either get an overflow/underflow or the compiler removes the check as it never can be true }
{ checking just the min./max. value depending on the pure size of the counter does not work as the check might
get optimized away
not(not(lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=((1 shl (left.resultdef.size*8))-1))) and
not(not(lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=((1 shl (left.resultdef.size*8-1))-1))) and
not((lnf_backward in loopflags) and not(is_signed(left.resultdef)) and (get_ordinal_value(t1)=0)) and
not((lnf_backward in loopflags) and is_signed(left.resultdef) and (get_ordinal_value(t1)=(-Tconstexprint(1 shl (left.resultdef.size*8-1))))) and
}
not(not(lnf_backward in loopflags) and (get_ordinal_value(t1)=countermax)) and
not((lnf_backward in loopflags) and (get_ordinal_value(t1)=countermin)) and
{ neither might the for loop contain a continue statement as continue in a while loop would skip the increment at the end
of the loop, this could be overcome by replacing the continue statement with an pred/succ; continue sequence }
not(foreachnodestatic(t2,@checkcontinue,nil)) and
{ if the loop is unrolled and there is a jump into the loop,
then we can't do the trick with incrementing the loop var only at the
end
}
not(assigned(entrylabel));
needsifblock:=not(is_constnode(right)) or not(is_constnode(t1));
{ convert the for loop into a while loop }
result:=internalstatements(statements);
ifblock:=internalstatements(ifstatements);
loopblock:=internalstatements(loopstatements);
usefromtemp:=(might_have_sideeffects(t1) and not(is_const(right))) or (node_complexity(right)>1);
usetotemp:=not(is_const(t1));
if needsifblock then
begin
{ do not generate a temp. for the from node, if it is a const, it can be copied directly since
no side effect might change it }
if usefromtemp then
begin
fromtemp:=ctempcreatenode.create(right.resultdef,right.resultdef.size,tt_persistent,true);
{ the if block might be optimized out, so we put the deletetempnode after the if-block, however,
this causes a long life time of the fromtemp. If the final regsync is left away, the reg. allocator
figures out the needed life time. As their are no loops involved between the uses of the fromtemp,
this does no hurt }
fromtemp.includetempflag(ti_no_final_regsync);
addstatement(statements,fromtemp);
{ while it would be beneficial to fold the initial reverse succ/pred into this assignment, this is
not possible because it might wrap around and the if check later on goes wrong }
addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(fromtemp),right.getcopy));
end;
if usetotemp then
begin
totemp:=ctempcreatenode.create(t1.resultdef,t1.resultdef.size,tt_persistent,true);
addstatement(statements,totemp);
addstatement(statements,cassignmentnode.create_internal(ctemprefnode.create(totemp),t1.getcopy));
end;
if usefromtemp then
begin
addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,ctemprefnode.create(fromtemp)));
if not(do_loopvar_at_end) then
iterate_counter(ifstatements,lnf_backward in loopflags);
end
else
begin
if not(do_loopvar_at_end) then
addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
else
addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
end;
end
else
begin
if not(do_loopvar_at_end) then
addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,
iterate_counter_func(right.getcopy,lnf_backward in loopflags)))
else
addstatement(ifstatements,cassignmentnode.create_internal(left.getcopy,right.getcopy));
end;
if assigned(entrylabel) then
addstatement(ifstatements,cgotonode.create(tlabelnode(entrylabel).labsym));
if not(do_loopvar_at_end) then
iterate_counter(loopstatements,not(lnf_backward in loopflags));
{ avoid copying t2, it is used only once and it might be big }
addstatement(loopstatements,t2);
t2:=nil;
if do_loopvar_at_end then
iterate_counter(loopstatements,not(lnf_backward in loopflags));
if do_loopvar_at_end then
begin
if lnf_backward in loopflags then
cond:=ltn
else
cond:=gtn;
end
else
begin
if lnf_backward in loopflags then
cond:=lten
else
cond:=gten;
end;
{ get rid of nf_write etc. as the left node is now only read }
leftcopy:=left.getcopy;
node_reset_flags(leftcopy,[nf_pass1_done,nf_modify,nf_write]);
if needsifblock then
begin
if usetotemp then
toexpr:=ctemprefnode.create(totemp)
else
toexpr:=t1.getcopy;
addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,toexpr),loopblock,false,true));
if usefromtemp then
fromexpr:=ctemprefnode.create(fromtemp)
else
fromexpr:=right.getcopy;
if usetotemp then
toexpr:=ctemprefnode.create(totemp)
else
toexpr:=t1.getcopy;
if lnf_backward in loopflags then
addstatement(statements,cifnode.create(caddnode.create_internal(gten,
fromexpr,toexpr),ifblock,nil))
else
addstatement(statements,cifnode.create(caddnode.create_internal(lten,
fromexpr,toexpr),ifblock,nil));
if usetotemp then
addstatement(statements,ctempdeletenode.create(totemp));
if usefromtemp then
addstatement(statements,ctempdeletenode.create(fromtemp));
end
else
begin
{ is a simple comparision for equality sufficient? }
if do_loopvar_at_end and (lnf_backward in loopflags) and (lnf_counter_not_used in loopflags) then
addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(equaln,leftcopy,
caddnode.create_internal(subn,t1.getcopy,cordconstnode.create(1,t1.resultdef,false))),loopblock,false,true))
else
addstatement(ifstatements,cwhilerepeatnode.create(caddnode.create_internal(cond,leftcopy,t1.getcopy),loopblock,false,true));
addstatement(statements,ifblock);
end;
current_filepos:=storefilepos;
end;
{*****************************************************************************
TEXITNODE
*****************************************************************************}
constructor texitnode.create(l:tnode);
begin
inherited create(exitn,l);
if assigned(left) then
begin
{ add assignment to funcretsym }
left:=ctypeconvnode.create(left,current_procinfo.procdef.returndef);
left:=cassignmentnode.create(
cloadnode.create(current_procinfo.procdef.funcretsym,current_procinfo.procdef.funcretsym.owner),
left);
end;
end;
constructor texitnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
end;
procedure texitnode.ppuwrite(ppufile:tcompilerppufile);
begin
inherited ppuwrite(ppufile);
end;
function texitnode.pass_typecheck:tnode;
var
newstatement : tstatementnode;
ressym: tsym;
resdef: tdef;
begin
result:=nil;
newstatement:=nil;
if assigned(left) then
begin
result:=internalstatements(newstatement);
addstatement(newstatement,left);
left:=nil;
end;
{ if the function result has been migrated to the parentfpstruct,
we have to load it back to the original location (from which the
code generator will load it into the function result location),
because the code to this that we add in tnodeutils.wrap_proc_body()
gets inserted before the exit label to which this node will jump }
if (target_info.system in systems_fpnestedstruct) and
not(nf_internal in flags) and
current_procinfo.procdef.get_funcretsym_info(ressym,resdef) and
(tabstractnormalvarsym(ressym).inparentfpstruct) then
begin
if not assigned(result) then
result:=internalstatements(newstatement);
cnodeutils.load_parentfpstruct_nested_funcret(ressym,newstatement);
end;
if assigned(result) then
begin
addstatement(newstatement,self.getcopy);
{ ensure we don't insert the function result loading code again for
this node }
include(newstatement.left.flags,nf_internal);
end;
resultdef:=voidtype;
end;
function texitnode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
if assigned(left) then
internalerror(2011052801);
end;
{*****************************************************************************
TBREAKNODE
*****************************************************************************}
constructor tbreaknode.create;
begin
inherited create(breakn);
end;
function tbreaknode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
end;
function tbreaknode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
end;
{*****************************************************************************
TCONTINUENODE
*****************************************************************************}
constructor tcontinuenode.create;
begin
inherited create(continuen);
end;
function tcontinuenode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
end;
function tcontinuenode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
end;
{*****************************************************************************
TGOTONODE
*****************************************************************************}
constructor tgotonode.create(p : tlabelsym);
begin
inherited create(goton);
exceptionblock:=current_exceptblock;
labelnode:=nil;
labelsym:=p;
end;
constructor tgotonode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
labelnodeidx:=ppufile.getlongint;
exceptionblock:=ppufile.getbyte;
end;
procedure tgotonode.ppuwrite(ppufile:tcompilerppufile);
begin
inherited ppuwrite(ppufile);
labelnodeidx:=labelnode.ppuidx;
ppufile.putlongint(labelnodeidx);
ppufile.putbyte(exceptionblock);
end;
procedure tgotonode.buildderefimpl;
begin
inherited buildderefimpl;
end;
procedure tgotonode.derefimpl;
begin
inherited derefimpl;
end;
procedure tgotonode.resolveppuidx;
begin
labelnode:=tlabelnode(nodeppuidxget(labelnodeidx));
if labelnode.nodetype<>labeln then
internalerror(200809021);
end;
function tgotonode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
end;
function tgotonode.pass_1 : tnode;
var
p2 : tprocinfo;
begin
result:=nil;
expectloc:=LOC_VOID;
{ The labelnode can already be set when
this node was copied }
if not(assigned(labelnode)) then
begin
{ inner procedure goto? }
if assigned(labelsym.code) and
((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
{ generated by the optimizer? }
not(assigned(labelsym.owner))) then
labelnode:=tlabelnode(labelsym.code)
else if ((m_non_local_goto in current_settings.modeswitches) and
assigned(labelsym.owner)) or
{ nested exits don't need the non local goto switch }
(labelsym.realname='$nestedexit') then
begin
if current_procinfo.procdef.parast.symtablelevel>=labelsym.owner.symtablelevel then
begin
{ don't mess with the exception blocks, global gotos in/out side exception blocks are not allowed }
if exceptionblock>0 then
CGMessage(cg_e_goto_inout_of_exception_block);
{ goto across procedures using exception?
this is not allowed because we cannot
easily unwind the exception frame
stack
}
p2:=current_procinfo;
while true do
begin
if ((cs_implicit_exceptions in current_settings.moduleswitches) and ((p2.flags*[pi_needs_implicit_finally,pi_has_implicit_finally])<>[])) or
((p2.flags*[pi_uses_exceptions])<>[]) then
Message(cg_e_goto_across_procedures_with_exceptions_not_allowed);
if labelsym.owner=p2.procdef.localst then
break;
p2:=p2.parent
end;
if assigned(labelsym.jumpbuf) then
begin
result:=ccallnode.createintern('fpc_longjmp',
ccallparanode.create(cordconstnode.create(1,sinttype,true),
ccallparanode.create(cloadnode.create(labelsym.jumpbuf,labelsym.jumpbuf.owner),
nil)));
end
else
CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
end
else
CGMessagePos(self.fileinfo,cg_e_interprocedural_goto_only_to_outer_scope_allowed);
end
else
CGMessage1(cg_e_goto_label_not_found,labelsym.realname);
end;
{ check if we don't mess with exception blocks }
if assigned(labelnode) and
(exceptionblock<>labelnode.exceptionblock) then
CGMessage(cg_e_goto_inout_of_exception_block);
end;
function tgotonode.dogetcopy : tnode;
var
p : tgotonode;
begin
p:=tgotonode(inherited dogetcopy);
p.exceptionblock:=exceptionblock;
{ generate labelnode if not done yet }
if not(assigned(labelnode)) then
begin
if assigned(labelsym) and assigned(labelsym.code) then
labelnode:=tlabelnode(labelsym.code)
end;
p.labelsym:=labelsym;
{ do not copy the label node here as we do not know if the label node is part of the tree or not,
this will be fixed after the copying in node.setuplabelnode: if the labelnode has copiedto set,
labelnode of the goto node is update }
if assigned(labelnode) then
p.labelnode:=labelnode
else
begin
{ don't trigger IE when there was already an error, i.e. the
label is not defined. See tw11763 (PFV) }
if (errorcount=0) and
{ don't trigger IE if it's a global goto }
((assigned(labelsym.owner) and (current_procinfo.procdef.parast.symtablelevel=labelsym.owner.symtablelevel)) or
not(assigned(labelsym.owner))) then
internalerror(200610291);
end;
result:=p;
end;
function tgotonode.docompare(p: tnode): boolean;
begin
docompare := false;
end;
{*****************************************************************************
TLABELNODE
*****************************************************************************}
constructor tlabelnode.create(l:tnode;alabsym:tlabelsym);
begin
inherited create(labeln,l);
exceptionblock:=current_exceptblock;
labsym:=alabsym;
{ Register labelnode in labelsym }
labsym.code:=self;
end;
constructor tlabelnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
exceptionblock:=ppufile.getbyte;
end;
destructor tlabelnode.destroy;
begin
if assigned(labsym) then
begin
if not assigned(labsym.Owner) then
labsym.Free // Free labelsym if it has no owner
else
if labsym.code=pointer(self) then
begin
{ Remove reference in labelsym, this is to prevent
goto's to this label }
labsym.code:=nil;
end;
end;
inherited destroy;
end;
procedure tlabelnode.ppuwrite(ppufile:tcompilerppufile);
begin
inherited ppuwrite(ppufile);
ppufile.putbyte(exceptionblock);
end;
procedure tlabelnode.buildderefimpl;
begin
inherited buildderefimpl;
end;
procedure tlabelnode.derefimpl;
begin
inherited derefimpl;
end;
function tlabelnode.pass_typecheck:tnode;
begin
result:=nil;
{ left could still be unassigned }
if assigned(left) then
typecheckpass(left);
resultdef:=voidtype;
end;
function tlabelnode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
include(current_procinfo.flags,pi_has_label);
if assigned(left) then
firstpass(left);
if (m_non_local_goto in current_settings.modeswitches) and
{ the owner can be Nil for internal labels }
assigned(labsym.owner) and
(current_procinfo.procdef.parast.symtablelevel<>labsym.owner.symtablelevel) then
CGMessage(cg_e_labels_cannot_defined_outside_declaration_scope)
end;
function tlabelnode.dogetcopy : tnode;
begin
if not(assigned(copiedto)) then
copiedto:=tlabelnode(inherited dogetcopy);
copiedto.exceptionblock:=exceptionblock;
result:=copiedto;
end;
function tlabelnode.docompare(p: tnode): boolean;
begin
docompare := false;
end;
{*****************************************************************************
TRAISENODE
*****************************************************************************}
constructor traisenode.create(l,taddr,tframe:tnode);
begin
inherited create(raisen,l,taddr,tframe);
end;
function traisenode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
if assigned(left) then
begin
{ first para must be a _class_ }
typecheckpass(left);
set_varstate(left,vs_read,[vsf_must_be_valid]);
if codegenerror then
exit;
if not is_class(left.resultdef) and
not is_javaclass(left.resultdef) then
CGMessage1(type_e_class_type_expected,left.resultdef.typename);
{ insert needed typeconvs for addr,frame }
if assigned(right) then
begin
{ addr }
typecheckpass(right);
set_varstate(right,vs_read,[vsf_must_be_valid]);
inserttypeconv(right,voidcodepointertype);
{ frame }
if assigned(third) then
begin
typecheckpass(third);
set_varstate(third,vs_read,[vsf_must_be_valid]);
inserttypeconv(third,voidpointertype);
end;
end;
end;
end;
function traisenode.pass_1 : tnode;
var
statements : tstatementnode;
current_addr : tlabelnode;
raisenode : tcallnode;
begin
result:=internalstatements(statements);
if assigned(left) then
begin
{ first para must be a class }
firstpass(left);
{ insert needed typeconvs for addr,frame }
if assigned(right) then
begin
{ addr }
firstpass(right);
{ frame }
if assigned(third) then
firstpass(third)
else
third:=cpointerconstnode.Create(0,voidpointertype);
end
else
begin
third:=cinlinenode.create(in_get_frame,false,nil);
current_addr:=clabelnode.create(cnothingnode.create,clabelsym.create('$raiseaddr'));
addstatement(statements,current_addr);
right:=caddrnode.create(cloadnode.create(current_addr.labsym,current_addr.labsym.owner));
{ raise address off by one so we are for sure inside the action area for the raise }
if tf_use_psabieh in target_info.flags then
right:=caddnode.create_internal(addn,right,cordconstnode.create(1,sizesinttype,false));
end;
raisenode:=ccallnode.createintern('fpc_raiseexception',
ccallparanode.create(third,
ccallparanode.create(right,
ccallparanode.create(left,nil)))
);
include(raisenode.callnodeflags,cnf_call_never_returns);
addstatement(statements,raisenode);
end
else
begin
addstatement(statements,ccallnode.createintern('fpc_popaddrstack',nil));
raisenode:=ccallnode.createintern('fpc_reraise',nil);
include(raisenode.callnodeflags,cnf_call_never_returns);
addstatement(statements,raisenode);
end;
left:=nil;
right:=nil;
third:=nil;
end;
{*****************************************************************************
TTRYEXCEPTNODE
*****************************************************************************}
constructor ttryexceptnode.create(l,r,_t1 : tnode);
begin
inherited create(tryexceptn,l,r,_t1,nil);
end;
function ttryexceptnode.pass_typecheck:tnode;
begin
result:=nil;
typecheckpass(left);
{ on statements }
if assigned(right) then
typecheckpass(right);
{ else block }
if assigned(t1) then
typecheckpass(t1);
resultdef:=voidtype;
end;
function ttryexceptnode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
firstpass(left);
{ on statements }
if assigned(right) then
firstpass(right);
{ else block }
if assigned(t1) then
firstpass(t1);
include(current_procinfo.flags,pi_do_call);
include(current_procinfo.flags,pi_uses_exceptions);
adjust_estimated_stack_size;
end;
function ttryexceptnode.simplify(forinline: boolean): tnode;
begin
result:=nil;
{ empty try -> can never raise exception -> do nothing }
if has_no_code(left) then
result:=cnothingnode.create;
end;
procedure ttryexceptnode.adjust_estimated_stack_size;
begin
inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size*2);
end;
{*****************************************************************************
TTRYFINALLYNODE
*****************************************************************************}
constructor ttryfinallynode.create(l,r:tnode);
begin
inherited create(tryfinallyn,l,r,nil);
third:=nil;
implicitframe:=false;
end;
constructor ttryfinallynode.create_implicit(l,r:tnode);
begin
inherited create(tryfinallyn,l,r,nil);
third:=nil;
implicitframe:=true;
end;
function ttryfinallynode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
typecheckpass(left);
// "try block" is "used"? (JM)
set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
typecheckpass(right);
// "except block" is "used"? (JM)
set_varstate(right,vs_readwritten,[vsf_must_be_valid]);
if assigned(third) then
begin
typecheckpass(third);
set_varstate(third,vs_readwritten,[vsf_must_be_valid]);
end;
end;
function ttryfinallynode.pass_1 : tnode;
begin
result:=nil;
expectloc:=LOC_VOID;
firstpass(left);
firstpass(right);
if assigned(third) then
firstpass(third);
include(current_procinfo.flags,pi_do_call);
{ pi_uses_exceptions is an information for the optimizer and it
is only interested in exceptions if they appear inside the body,
so ignore implicit frames when setting the flag }
if not(implicitframe) then
include(current_procinfo.flags,pi_uses_exceptions);
adjust_estimated_stack_size;
end;
function ttryfinallynode.simplify(forinline : boolean): tnode;
begin
result:=nil;
{ if the try contains no code, we can kill
the try and except and return only the
finally part }
if has_no_code(left) then
begin
result:=right;
right:=nil;
end;
{ if the finally block contains no code, we can kill
it and just return the try part }
if has_no_code(right) and not(assigned(third)) and not(implicitframe) then
begin
result:=left;
left:=nil;
end;
end;
function ttryfinallynode.dogetcopy: tnode;
begin
result:=inherited dogetcopy;
ttryfinallynode(result).implicitframe:=implicitframe;
end;
procedure ttryfinallynode.adjust_estimated_stack_size;
begin
inc(current_procinfo.estimatedtempsize,rec_jmp_buf.size);
end;
{*****************************************************************************
TONNODE
*****************************************************************************}
constructor tonnode.create(l,r:tnode);
begin
inherited create(onn,l,r);
excepTSymtable:=nil;
excepttype:=nil;
end;
destructor tonnode.destroy;
begin
{ copied nodes don't need to release the symtable }
if assigned(excepTSymtable) then
excepTSymtable.free;
inherited destroy;
end;
constructor tonnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
begin
inherited ppuload(t,ppufile);
excepTSymtable:=nil;
excepttype:=nil;
end;
function tonnode.dogetcopy : tnode;
var
n : tonnode;
begin
n:=tonnode(inherited dogetcopy);
if assigned(exceptsymtable) then
n.exceptsymtable:=exceptsymtable.getcopy
else
n.exceptsymtable:=nil;
n.excepttype:=excepttype;
result:=n;
end;
function tonnode.pass_typecheck:tnode;
begin
result:=nil;
resultdef:=voidtype;
if not is_class(excepttype) and
not is_javaclass(excepttype) then
CGMessage1(type_e_class_type_expected,excepttype.typename);
if assigned(left) then
typecheckpass(left);
if assigned(right) then
typecheckpass(right);
end;
function tonnode.pass_1 : tnode;
begin
result:=nil;
include(current_procinfo.flags,pi_do_call);
expectloc:=LOC_VOID;
if assigned(left) then
firstpass(left);
if assigned(right) then
firstpass(right);
end;
function tonnode.docompare(p: tnode): boolean;
begin
docompare := false;
end;
end.
|