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
|
-- Ortho code compiler.
-- Copyright (C) 2005 Tristan Gingold
--
-- GHDL 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, or (at your option) any later
-- version.
--
-- GHDL 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 GCC; see the file COPYING. If not, write to the Free
-- Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
with Ada.Unchecked_Deallocation;
with Ortho_Nodes; use Ortho_Nodes;
with Ortho_Ident; use Ortho_Ident;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Interfaces; use Interfaces;
with Ada.Exceptions;
--with GNAT.Debug_Pools;
-- TODO:
-- uncomplete type: check for type redefinition
package body Ortho_Front is
-- If true, emit line number before each statement.
-- If flase, keep line number indication in the source file.
Flag_Renumber : Boolean := True;
procedure Init is
begin
null;
end Init;
function Decode_Option (Opt : String_Acc; Arg : String_Acc) return Natural
is
pragma Unreferenced (Arg);
begin
if Opt.all = "-r" or Opt.all = "--ghdl-r" then
Flag_Renumber := True;
return 1;
else
return 0;
end if;
end Decode_Option;
-- File buffer.
File_Name : String_Acc;
Buf : String (1 .. 2048 + 1);
Buf_Len : Natural;
Pos : Natural;
Lineno : Natural;
Fd : File_Descriptor;
Error : exception;
procedure Puterr (Msg : String)
is
L : Integer;
pragma Unreferenced (L);
begin
L := Write (Standerr, Msg'Address, Msg'Length);
end Puterr;
procedure Puterr (N : Natural)
is
Str : constant String := Natural'Image (N);
begin
Puterr (Str (Str'First + 1 .. Str'Last));
end Puterr;
procedure Newline_Err is
begin
Puterr ((1 => LF));
end Newline_Err;
procedure Scan_Error (Msg : String) is
begin
Puterr (File_Name.all);
Puterr (":");
Puterr (Lineno);
Puterr (": ");
Puterr (Msg);
Newline_Err;
raise Error;
end Scan_Error;
procedure Parse_Error (Msg : String);
pragma No_Return (Parse_Error);
procedure Parse_Error (Msg : String) is
begin
Puterr (File_Name.all);
Puterr (":");
Puterr (Lineno);
Puterr (": ");
Puterr (Msg);
Newline_Err;
raise Error;
end Parse_Error;
-- Uniq_Num : Natural := 0;
-- function Get_Uniq_Id return O_Ident
-- is
-- Str : String (1 .. 8);
-- V : Natural;
-- begin
-- V := Uniq_Num;
-- Uniq_Num := Uniq_Num + 1;
-- Str (1) := 'L';
-- Str (2) := '.';
-- for I in reverse 3 .. Str'Last loop
-- Str (I) := Character'Val ((V mod 10) + Character'Pos('0'));
-- V := V / 10;
-- end loop;
-- return Get_Identifier (Str);
-- end Get_Uniq_Id;
-- Get the next character.
-- Return NUL on end of file.
function Get_Char return Character
is
Res : Character;
begin
if Buf (Pos) = NUL then
-- Read line.
Buf_Len := Read (Fd, Buf'Address, Buf'Length - 1);
if Buf_Len = 0 then
-- End of file.
return NUL;
end if;
Pos := 1;
Buf (Buf_Len + 1) := NUL;
end if;
Res := Buf (Pos);
Pos := Pos + 1;
return Res;
end Get_Char;
procedure Unget_Char is
begin
if Pos = Buf'First then
raise Program_Error;
end if;
Pos := Pos - 1;
end Unget_Char;
type Token_Type is
(Tok_Eof,
Tok_Line_Number, Tok_File_Name, Tok_Comment,
Tok_Ident, Tok_Num, Tok_String, Tok_Float_Num,
Tok_Plus, Tok_Minus,
Tok_Star, Tok_Div, Tok_Mod, Tok_Rem,
Tok_Sharp,
Tok_Not, Tok_Abs,
Tok_Or, Tok_And, Tok_Xor,
Tok_Equal, Tok_Not_Equal,
Tok_Greater, Tok_Greater_Eq,
Tok_Less, Tok_Less_Eq,
Tok_Colon, Tok_Semicolon,
Tok_Comma, Tok_Dot, Tok_Tick, Tok_Arob, Tok_Elipsis,
Tok_Assign,
Tok_Left_Paren, Tok_Right_Paren,
Tok_Left_Brace, Tok_Right_Brace,
Tok_Left_Brack, Tok_Right_Brack,
Tok_Unsigned, Tok_Signed, Tok_Float,
Tok_Array, Tok_Subarray,
Tok_Access, Tok_Record, Tok_Union,
Tok_Boolean, Tok_Enum,
Tok_If, Tok_Then, Tok_Else, Tok_Elsif,
Tok_Loop, Tok_Exit, Tok_Next,
Tok_Is, Tok_Of, Tok_All,
Tok_Return,
Tok_Type,
Tok_External, Tok_Private, Tok_Public, Tok_Local,
Tok_Procedure, Tok_Function,
Tok_Constant, Tok_Var,
Tok_Declare, Tok_Begin, Tok_End,
Tok_Case, Tok_When, Tok_Default, Tok_Arrow,
Tok_Null);
type Hash_Type is new Unsigned_32;
type Name_Type;
type Name_Acc is access Name_Type;
-- Symbol table.
type Syment_Type;
type Syment_Acc is access Syment_Type;
type Syment_type is record
-- The hash for the symbol.
Hash : Hash_Type;
-- Identification of the symbol.
Ident : O_Ident;
-- Next symbol with the same collision.
Next : Syment_Acc;
-- Meaning of the symbol.
Name : Name_Acc;
end record;
-- Well known identifiers (used for attributes).
Id_Address : Syment_Acc;
Id_Unchecked_Address : Syment_Acc;
Id_Subprg_Addr : Syment_Acc;
Id_Conv : Syment_Acc;
Id_Sizeof : Syment_Acc;
Id_Alignof : Syment_Acc;
Id_Alloca : Syment_Acc;
Id_Offsetof : Syment_Acc;
Token_Number : Unsigned_64;
Token_Float : IEEE_Float_64;
Token_Ident : String (1 .. 256);
Token_Idlen : Natural;
Token_Hash : Hash_Type;
Token_Sym : Syment_Acc;
-- The symbol table.
type Syment_Acc_Array is array (Hash_Type range <>) of Syment_Acc;
Hash_Max : constant Hash_Type := 511;
Symtable : Syment_Acc_Array (0 .. Hash_Max - 1) := (others => null);
type Node_Kind is (Decl_Keyword, Decl_Type, Decl_Param,
Node_Function, Node_Procedure, Node_Object, Node_Field,
Node_Lit,
Type_Boolean, Type_Enum,
Type_Unsigned, Type_Signed, Type_Float,
Type_Array, Type_Subarray,
Type_Access, Type_Record, Type_Union);
subtype Nodes_Subprogram is Node_Kind range Node_Function .. Node_Procedure;
type Node (<>);
type Node_Acc is access Node;
type Node (Kind : Node_Kind) is record
case Kind is
when Decl_Keyword =>
-- Keyword.
-- A keyword is not a declaration since the identifier has only
-- one meaning (the keyword).
Keyword : Token_Type;
when Decl_Type
| Decl_Param
| Node_Function
| Node_Procedure
| Node_Object
| Node_Lit =>
-- Declarations
Decl_Storage : O_Storage;
-- For constants: True iff fully defined.
Decl_Defined : Boolean;
-- All declarations but NODE_PROCEDURE have a type.
Decl_Dtype : Node_Acc;
case Kind is
when Decl_Type =>
-- Type declaration.
null;
when Decl_Param =>
-- Parameter identifier.
Param_Name : Syment_Acc;
-- Parameter ortho node.
Param_Node : O_Dnode;
-- Next parameter of the parameters list.
Param_Next : Node_Acc;
when Node_Procedure
| Node_Function =>
-- Subprogram symbol name.
Subprg_Name : Syment_Acc;
-- List of parameters.
Subprg_Params : Node_Acc;
-- Subprogram ortho node.
Subprg_Node : O_Dnode;
when Node_Object =>
-- Name of the object (constant, variable).
Obj_Name : O_Ident;
-- Ortho node of the object.
Obj_Node : O_Dnode;
when Node_Lit =>
-- Name of the literal.
Lit_Name : O_Ident;
-- Enum literal
Lit_Cnode : O_Cnode;
-- Next literal for the type.
Lit_Next : Node_Acc;
when others =>
null;
end case;
when Node_Field =>
-- Record field.
Field_Ident : Syment_Acc;
Field_Fnode : O_Fnode;
Field_Type : Node_Acc;
Field_Next : Node_Acc;
when Type_Signed
| Type_Unsigned
| Type_Float
| Type_Array
| Type_Subarray
| Type_Record
| Type_Union
| Type_Access
| Type_Boolean
| Type_Enum =>
-- Ortho node type.
Type_Onode : O_Tnode;
case Kind is
when Type_Array =>
Array_Index : Node_Acc;
Array_Element : Node_Acc;
when Type_Subarray =>
Subarray_Base : Node_Acc;
--Subarray_Length : Natural;
when Type_Access =>
Access_Dtype : Node_Acc;
when Type_Record
| Type_Union =>
Record_Union_Fields : Node_Acc;
when Type_Enum
| Type_Boolean =>
Enum_Lits : Node_Acc;
when Type_Float =>
null;
when others =>
null;
end case;
end case;
end record;
type Scope_Type;
type Scope_Acc is access Scope_Type;
type Name_Type is record
-- Current interpretation of the symbol.
Inter : Node_Acc;
-- Next declaration in the current scope.
Next : Syment_Acc;
-- Interpretation in a previous scope.
Up : Name_Acc;
-- Current scope.
Scope : Scope_Acc;
end record;
type Scope_Type is record
-- Simply linked list of names.
Names : Syment_Acc;
-- Previous scope.
Prev : Scope_Acc;
end record;
-- Return the current declaration for symbol SYM.
function Get_Decl (Sym : Syment_Acc) return Node_Acc;
pragma Inline (Get_Decl);
procedure Scan_Char (C : Character)
is
R : Character;
begin
if C = '\' then
R := Get_Char;
case R is
when 'n' =>
R := LF;
when 'r' =>
R := CR;
when ''' =>
R := ''';
when '"' => -- "
R := '"'; -- "
when others =>
Scan_Error ("bad character sequence \" & R);
end case;
else
R := C;
end if;
Token_Idlen := Token_Idlen + 1;
Token_Ident (Token_Idlen) := R;
end Scan_Char;
function Get_Hash (Str : String) return Hash_Type
is
Res : Hash_Type;
begin
Res := 0;
for I in Str'Range loop
Res := Res * 31 + Character'Pos (Str (I));
end loop;
return Res;
end Get_Hash;
-- Previous token.
Tok_Previous : Token_Type;
function Scan_Number (First_Char : Character) return Token_Type
is
function To_Digit (C : Character) return Integer is
begin
case C is
when '0' .. '9' =>
return Character'Pos (C) - Character'Pos ('0');
when 'A' .. 'F' =>
return Character'Pos (C) - Character'Pos ('A') + 10;
when 'a' .. 'f' =>
return Character'Pos (C) - Character'Pos ('a') + 10;
when others =>
return -1;
end case;
end To_Digit;
function Is_Digit (C : Character) return Boolean is
begin
case C is
when '0' .. '9'
| 'A' .. 'F'
| 'a' .. 'f' =>
return True;
when others =>
return False;
end case;
end Is_Digit;
After_Point : Integer;
C : Character;
Exp : Integer;
Exp_Neg : Boolean;
Base : Unsigned_64;
begin
Token_Number := 0;
C := First_Char;
loop
Token_Number := Token_Number * 10 + Unsigned_64 (To_Digit (C));
C := Get_Char;
exit when not Is_Digit (C);
end loop;
if C = '#' then
Base := Token_Number;
Token_Number := 0;
C := Get_Char;
loop
if C /= '_' then
Token_Number :=
Token_Number * Base + Unsigned_64 (To_Digit (C));
end if;
C := Get_Char;
exit when C = '#';
end loop;
return Tok_Num;
end if;
if C = '.' then
-- A real number.
After_Point := 0;
Token_Float := IEEE_Float_64 (Token_Number);
loop
C := Get_Char;
exit when C not in '0' .. '9';
Token_Float := Token_Float * 10.0 + IEEE_Float_64 (To_Digit (C));
After_Point := After_Point + 1;
end loop;
if C = 'e' or C = 'E' then
Exp := 0;
C := Get_Char;
Exp_Neg := False;
if C = '-' then
Exp_Neg := True;
C := Get_Char;
elsif C = '+' then
C := Get_Char;
elsif not Is_Digit (C) then
Scan_Error ("digit expected");
end if;
while Is_Digit (C) loop
Exp := Exp * 10 + To_Digit (C);
C := Get_Char;
end loop;
if Exp_Neg then
Exp := -Exp;
end if;
Exp := Exp - After_Point;
else
Exp := - After_Point;
end if;
Unget_Char;
Token_Float := Token_Float * 10.0 ** Exp;
if Token_Float > IEEE_Float_64'Last then
Token_Float := IEEE_Float_64'Last;
end if;
return Tok_Float_Num;
else
Unget_Char;
return Tok_Num;
end if;
end Scan_Number;
procedure Scan_Comment
is
C : Character;
begin
Token_Idlen := 0;
loop
C := Get_Char;
exit when C = CR or C = LF;
Token_Idlen := Token_Idlen + 1;
Token_Ident (Token_Idlen) := C;
end loop;
Unget_Char;
end Scan_Comment;
-- Get the next token.
function Get_Token return Token_Type
is
C : Character;
begin
loop
C := Get_Char;
<< Again >> null;
case C is
when NUL =>
return Tok_Eof;
when ' ' | HT =>
null;
when LF =>
Lineno := Lineno + 1;
C := Get_Char;
if C /= CR then
goto Again;
end if;
when CR =>
Lineno := Lineno + 1;
C := Get_Char;
if C /= LF then
goto Again;
end if;
when '+' =>
return Tok_Plus;
when '-' =>
C := Get_Char;
if C = '-' then
C := Get_Char;
if C = '#' then
return Tok_Line_Number;
elsif C = 'F' then
Scan_Comment;
return Tok_File_Name;
elsif C = ' ' then
Scan_Comment;
return Tok_Comment;
else
Scan_Error ("bad comment");
end if;
else
Unget_Char;
return Tok_Minus;
end if;
when '/' =>
C := Get_Char;
if C = '=' then
return Tok_Not_Equal;
else
Unget_Char;
return Tok_Div;
end if;
when '*' =>
return Tok_Star;
when '#' =>
return Tok_Sharp;
when '=' =>
C := Get_Char;
if C = '>' then
return Tok_Arrow;
else
Unget_Char;
return Tok_Equal;
end if;
when '>' =>
C := Get_Char;
if C = '=' then
return Tok_Greater_Eq;
else
Unget_Char;
return Tok_Greater;
end if;
when '(' =>
return Tok_Left_Paren;
when ')' =>
return Tok_Right_Paren;
when '{' =>
return Tok_Left_Brace;
when '}' =>
return Tok_Right_Brace;
when '[' =>
return Tok_Left_Brack;
when ']' =>
return Tok_Right_Brack;
when '<' =>
C := Get_Char;
if C = '=' then
return Tok_Less_Eq;
else
Unget_Char;
return Tok_Less;
end if;
when ':' =>
C := Get_Char;
if C = '=' then
return Tok_Assign;
else
Unget_Char;
return Tok_Colon;
end if;
when '.' =>
C := Get_Char;
if C = '.' then
C := Get_Char;
if C = '.' then
return Tok_Elipsis;
else
Scan_Error ("'...' expected");
end if;
else
Unget_Char;
return Tok_Dot;
end if;
when ';' =>
return Tok_Semicolon;
when ',' =>
return Tok_Comma;
when '@' =>
return Tok_Arob;
when ''' =>
if Tok_Previous = Tok_Ident then
return Tok_Tick;
else
Token_Number := Character'Pos (Get_Char);
C := Get_Char;
if C /= ''' then
Scan_Error ("ending single quote expected");
end if;
return Tok_Num;
end if;
when '"' => -- "
-- Eat double quote.
C := Get_Char;
Token_Idlen := 0;
loop
Scan_Char (C);
C := Get_Char;
exit when C = '"'; -- "
end loop;
return Tok_String;
when '0' .. '9' =>
return Scan_Number (C);
when 'a' .. 'z'
| 'A' .. 'Z'
| '_' =>
Token_Idlen := 0;
Token_Hash := 0;
loop
Token_Idlen := Token_Idlen + 1;
Token_Ident (Token_Idlen) := C;
Token_Hash := Token_Hash * 31 + Character'Pos (C);
C := Get_Char;
exit when (C < 'A' or C > 'Z')
and (C < 'a' or C > 'z')
and (C < '0' or C > '9')
and (C /= '_');
end loop;
Unget_Char;
declare
H : Hash_Type;
S : Syment_Acc;
N : Node_Acc;
begin
H := Token_Hash mod Hash_Max;
S := Symtable (H);
while S /= null loop
if S.Hash = Token_Hash
and then Is_Equal (S.Ident,
Token_Ident (1 .. Token_Idlen))
then
-- This identifier is known.
Token_Sym := S;
-- It may be a keyword.
if S.Name /= null then
N := Get_Decl (S);
if N.Kind = Decl_Keyword then
return N.Keyword;
end if;
end if;
return Tok_Ident;
end if;
S := S.Next;
end loop;
Symtable (H) := new Syment_Type'
(Hash => Token_Hash,
Ident => Get_Identifier (Token_Ident (1 .. Token_Idlen)),
Next => Symtable (H),
Name => null);
Token_Sym := Symtable (H);
return Tok_Ident;
end;
when others =>
Scan_Error ("Bad character:"
& Integer'Image (Character'Pos (C))
& C);
return Tok_Eof;
end case;
end loop;
end Get_Token;
-- The current token.
Tok : Token_Type;
procedure Next_Token is
begin
Tok_Previous := Tok;
Tok := Get_Token;
end Next_Token;
procedure Expect (T : Token_Type; Msg : String := "") is
begin
if Tok /= T then
if Msg'Length = 0 then
case T is
when Tok_Left_Brace =>
Parse_Error ("'{' expected");
when others =>
if Tok = Tok_Ident then
Parse_Error
(Token_Type'Image (T) & " expected, found '" &
Token_Ident (1 .. Token_Idlen) & "'");
else
Parse_Error (Token_Type'Image (T) & " expected, found "
& Token_Type'Image (Tok));
end if;
end case;
else
Parse_Error (Msg);
end if;
end if;
end Expect;
procedure Next_Expect (T : Token_Type; Msg : String := "") is
begin
Next_Token;
Expect (T, Msg);
end Next_Expect;
-- Scopes and identifiers.
-- Current scope.
Scope : Scope_Acc := null;
-- Add a declaration for symbol SYM in the current scope.
-- INTER defines the meaning of the declaration.
-- There must be at most one declaration for a symbol in the current scope,
-- i.e. a symbol cannot be redefined.
procedure Add_Decl (Sym : Syment_Acc; Inter : Node_Acc);
-- Return TRUE iff SYM is already defined in the current scope.
function Is_Defined (Sym : Syment_Acc) return Boolean;
-- Create new scope.
procedure Push_Scope;
-- Close the current scope. Symbols defined in the scope regain their
-- previous declaration.
procedure Pop_Scope;
procedure Push_Scope
is
Nscope : Scope_Acc;
begin
Nscope := new Scope_Type'(Names => null, Prev => Scope);
Scope := Nscope;
end Push_Scope;
procedure Pop_Scope
is
procedure Free is new Ada.Unchecked_Deallocation
(Object => Name_Type, Name => Name_Acc);
procedure Free is new Ada.Unchecked_Deallocation
(Object => Scope_Type, Name => Scope_Acc);
Sym : Syment_Acc;
N_Sym : Syment_Acc;
Name : Name_Acc;
Old_Scope : Scope_Acc;
begin
Sym := Scope.Names;
while Sym /= null loop
Name := Sym.Name;
-- Check.
if Name.Scope /= Scope then
raise Program_Error;
end if;
-- Set the interpretation of this symbol.
Sym.Name := Name.Up;
N_Sym := Name.Next;
Free (Name);
Sym := N_Sym;
end loop;
-- Free scope.
Old_Scope := Scope;
Scope := Scope.Prev;
Free (Old_Scope);
end Pop_Scope;
function Is_Defined (Sym : Syment_Acc) return Boolean is
begin
if Sym.Name /= null
and then Sym.Name.Scope = Scope
then
return True;
else
return False;
end if;
end Is_Defined;
function New_Symbol (Str : String) return Syment_Acc
is
Ent : Syment_Acc;
H : Hash_Type;
begin
Ent := new Syment_Type'(Hash => Get_Hash (Str),
Ident => Get_Identifier (Str),
Next => null,
Name => null);
H := Ent.Hash mod Hash_Max;
Ent.Next := Symtable (H);
Symtable (H) := Ent;
return Ent;
end New_Symbol;
procedure Add_Keyword (Str : String; Token : Token_Type)
is
Kw : String (Str'Range);
Ent : Syment_Acc;
begin
-- Convert to uppercase.
for I in Str'Range loop
pragma Assert (Str (I) in 'a' .. 'z');
Kw (I) := Character'Val
(Character'Pos ('A')
+ Character'Pos (Str (I)) - Character'Pos ('a'));
end loop;
Ent := New_Symbol (Kw);
if Ent.Name /= null
or else Scope /= null
then
-- Redefinition of a keyword.
raise Program_Error;
end if;
Ent.Name := new Name_Type'(Inter => new Node'(Kind => Decl_Keyword,
Keyword => Token),
Next => null,
Up => null,
Scope => null);
end Add_Keyword;
procedure Add_Decl (Sym : Syment_Acc; Inter : Node_Acc)
is
Name : Name_Acc;
Prev : Node_Acc;
begin
Name := Sym.Name;
if Name /= null and then Name.Scope = Scope then
Prev := Name.Inter;
if Prev.Kind = Inter.Kind
and then Prev.Decl_Dtype = Inter.Decl_Dtype
and then Prev.Decl_Storage = O_Storage_External
and then Inter.Decl_Storage = O_Storage_Public
then
-- Redefinition
Name.Inter := Inter;
return;
end if;
Parse_Error ("redefinition of " & Get_String (Sym.Ident));
end if;
Name := new Name_Type'(Inter => Inter,
Next => Scope.Names,
Up => Sym.Name,
Scope => Scope);
Sym.Name := Name;
Scope.Names := Sym;
end Add_Decl;
function Get_Decl (Sym : Syment_Acc) return Node_Acc is
begin
if Sym.Name = null then
Parse_Error ("undefined identifier " & Get_String (Sym.Ident));
else
return Sym.Name.Inter;
end if;
end Get_Decl;
function Parse_Constant_Value (Atype : Node_Acc) return O_Cnode;
function Parse_Address (Prefix : Node_Acc) return O_Enode;
function Parse_Constant_Address (Prefix : Node_Acc) return O_Cnode;
procedure Parse_Declaration;
procedure Parse_Compound_Statement;
function Parse_Type return Node_Acc;
procedure Parse_Fields (Aggr_Type : Node_Acc;
Constr : in out O_Element_List)
is
F_Type : Node_Acc;
F : Syment_Acc;
Last_Field : Node_Acc;
Field : Node_Acc;
begin
Last_Field := null;
loop
exit when Tok = Tok_End;
if Tok /= Tok_Ident then
Parse_Error ("field name expected");
end if;
F := Token_Sym;
Next_Expect (Tok_Colon, "':' expected");
Next_Token;
F_Type := Parse_Type;
Field := new Node'(Kind => Node_Field,
Field_Ident => F,
Field_Fnode => O_Fnode_Null,
Field_Type => F_Type,
Field_Next => null);
case Aggr_Type.Kind is
when Type_Record =>
New_Record_Field (Constr, Field.Field_Fnode, F.Ident,
F_Type.Type_Onode);
when Type_Union =>
New_Union_Field (Constr, Field.Field_Fnode, F.Ident,
F_Type.Type_Onode);
when others =>
raise Program_Error;
end case;
if Last_Field = null then
Aggr_Type.Record_Union_Fields := Field;
else
Last_Field.Field_Next := Field;
end if;
Last_Field := Field;
Expect (Tok_Semicolon, "';' expected");
Next_Token;
end loop;
end Parse_Fields;
procedure Parse_Record_Type (Def : Node_Acc)
is
Constr : O_Element_List;
begin
if Def.Type_Onode = O_Tnode_Null then
Start_Record_Type (Constr);
else
Start_Uncomplete_Record_Type (Def.Type_Onode, Constr);
end if;
Parse_Fields (Def, Constr);
Next_Expect (Tok_Record, "end record expected");
Finish_Record_Type (Constr, Def.Type_Onode);
end Parse_Record_Type;
procedure Parse_Union_Type (Def : Node_Acc)
is
Constr : O_Element_List;
begin
Start_Union_Type (Constr);
Parse_Fields (Def, Constr);
Next_Expect (Tok_Union, "end union expected");
Finish_Union_Type (Constr, Def.Type_Onode);
end Parse_Union_Type;
function Parse_Type return Node_Acc
is
Res : Node_Acc;
T : Token_Type;
begin
T := Tok;
case T is
when Tok_Unsigned
| Tok_Signed =>
Next_Expect (Tok_Left_Paren, "'(' expected");
Next_Expect (Tok_Num, "number expected");
case T is
when Tok_Unsigned =>
Res := new Node'
(Kind => Type_Unsigned,
Type_Onode => New_Unsigned_Type (Natural
(Token_Number)));
when Tok_Signed =>
Res := new Node'
(Kind => Type_Signed,
Type_Onode => New_Signed_Type (Natural
(Token_Number)));
when others =>
raise Program_Error;
end case;
Next_Expect (Tok_Right_Paren, "')' expected");
when Tok_Float =>
Res := new Node'(Kind => Type_Float,
Type_Onode => New_Float_Type);
when Tok_Array =>
declare
Index_Node : Node_Acc;
El_Node : Node_Acc;
begin
Next_Expect (Tok_Left_Brack, "'[' expected");
Next_Token;
Index_Node := Parse_Type;
Expect (Tok_Right_Brack, "']' expected");
Next_Expect (Tok_Of, "'OF' expected");
Next_Token;
El_Node := Parse_Type;
Res := new Node'
(Kind => Type_Array,
Type_Onode => New_Array_Type (El_Node.Type_Onode,
Index_Node.Type_Onode),
Array_Index => Index_Node,
Array_Element => El_Node);
end;
return Res;
when Tok_Subarray =>
-- Grammar:
-- SUBARRAY type [ len ]
declare
Base_Node : Node_Acc;
Res_Type : O_Tnode;
begin
Next_Token;
Base_Node := Parse_Type;
if Base_Node.Kind /= Type_Array then
Parse_Error ("subarray base type is not an array type");
end if;
Expect (Tok_Left_Brack);
Next_Token;
Res_Type := New_Constrained_Array_Type
(Base_Node.Type_Onode,
Parse_Constant_Value (Base_Node.Array_Index));
Expect (Tok_Right_Brack);
Next_Token;
Res := new Node' (Kind => Type_Subarray,
Type_Onode => Res_Type,
Subarray_Base => Base_Node);
return Res;
end;
when Tok_Ident =>
declare
Inter : Node_Acc;
begin
Inter := Get_Decl (Token_Sym);
if Inter = null then
Parse_Error ("undefined type name symbol "
& Get_String (Token_Sym.Ident));
end if;
if Inter.Kind /= Decl_Type then
Parse_Error ("type declarator expected");
end if;
Res := Inter.Decl_Dtype;
end;
when Tok_Access =>
declare
Dtype : Node_Acc;
begin
Next_Token;
if Tok = Tok_Semicolon then
Res := new Node'
(Kind => Type_Access,
Type_Onode => New_Access_Type (O_Tnode_Null),
Access_Dtype => null);
else
Dtype := Parse_Type;
Res := new Node'
(Kind => Type_Access,
Type_Onode => New_Access_Type (Dtype.Type_Onode),
Access_Dtype => Dtype);
end if;
return Res;
end;
when Tok_Record =>
Next_Token;
if Tok = Tok_Semicolon then
-- Uncomplete record type.
Res := new Node'(Kind => Type_Record,
Type_Onode => O_Tnode_Null,
Record_Union_Fields => null);
New_Uncomplete_Record_Type (Res.Type_Onode);
return Res;
end if;
Res := new Node'(Kind => Type_Record,
Type_Onode => O_Tnode_Null,
Record_Union_Fields => null);
Parse_Record_Type (Res);
when Tok_Union =>
Next_Token;
Res := new Node'(Kind => Type_Union,
Type_Onode => O_Tnode_Null,
Record_Union_Fields => null);
Parse_Union_Type (Res);
when Tok_Boolean =>
declare
False_Lit, True_Lit : Node_Acc;
begin
Res := new Node'(Kind => Type_Boolean,
Type_Onode => O_Tnode_Null,
Enum_Lits => null);
Next_Expect (Tok_Left_Brace, "'{' expected");
Next_Expect (Tok_Ident, "identifier expected");
False_Lit := new Node'(Kind => Node_Lit,
Decl_Dtype => Res,
Decl_Storage => O_Storage_Public,
Decl_Defined => False,
Lit_Name => Token_Sym.Ident,
Lit_Cnode => O_Cnode_Null,
Lit_Next => null);
Next_Expect (Tok_Comma, "',' expected");
Next_Expect (Tok_Ident, "identifier expected");
True_Lit := new Node'(Kind => Node_Lit,
Decl_Dtype => Res,
Decl_Storage => O_Storage_Public,
Decl_Defined => False,
Lit_Name => Token_Sym.Ident,
Lit_Cnode => O_Cnode_Null,
Lit_Next => null);
Next_Expect (Tok_Right_Brace, "'}' expected");
False_Lit.Lit_Next := True_Lit;
Res.Enum_Lits := False_Lit;
New_Boolean_Type (Res.Type_Onode,
False_Lit.Lit_Name, False_Lit.Lit_Cnode,
True_Lit.Lit_Name, True_Lit.Lit_Cnode);
end;
when Tok_Enum =>
-- Grammar:
-- ENUM { LIT1, LIT2, ... LITN }
declare
List : O_Enum_List;
Lit : Node_Acc;
Last_Lit : Node_Acc;
begin
Res := new Node'(Kind => Type_Enum,
Type_Onode => O_Tnode_Null,
Enum_Lits => null);
Last_Lit := null;
Push_Scope;
Next_Expect (Tok_Left_Brace);
Next_Token;
-- FIXME: set a size to the enum.
Start_Enum_Type (List, 8);
loop
Expect (Tok_Ident);
Lit := new Node'(Kind => Node_Lit,
Decl_Dtype => Res,
Decl_Storage => O_Storage_Public,
Decl_Defined => False,
Lit_Name => Token_Sym.Ident,
Lit_Cnode => O_Cnode_Null,
Lit_Next => null);
Add_Decl (Token_Sym, Lit);
New_Enum_Literal (List, Lit.Lit_Name, Lit.Lit_Cnode);
if Last_Lit = null then
Res.Enum_Lits := Lit;
else
Last_Lit.Lit_Next := Lit;
end if;
Last_Lit := Lit;
Next_Token;
if Tok = Tok_Equal then
-- By compatibility, support '= N' after a literal.
Next_Expect (Tok_Num);
Next_Token;
end if;
exit when Tok = Tok_Right_Brace;
Expect (Tok_Comma);
Next_Token;
end loop;
Finish_Enum_Type (List, Res.Type_Onode);
Pop_Scope;
end;
when others =>
Parse_Error ("bad type " & Token_Type'Image (Tok));
return null;
end case;
Next_Token;
return Res;
end Parse_Type;
procedure Parse_Type_Completion (Decl : Node_Acc)
is
begin
case Tok is
when Tok_Record =>
Next_Token;
Parse_Record_Type (Decl.Decl_Dtype);
Next_Token;
when Tok_Access =>
Next_Token;
declare
Dtype : Node_Acc;
begin
Dtype := Parse_Type;
Decl.Decl_Dtype.Access_Dtype := Dtype;
Finish_Access_Type (Decl.Decl_Dtype.Type_Onode,
Dtype.Type_Onode);
end;
when others =>
Parse_Error ("'access' or 'record' expected");
end case;
end Parse_Type_Completion;
-- procedure Parse_Declaration;
procedure Parse_Expression (Expr_Type : Node_Acc;
Expr : out O_Enode;
Res_Type : out Node_Acc);
procedure Parse_Name (Prefix : Node_Acc;
Name : out O_Lnode; N_Type : out Node_Acc);
procedure Parse_Lvalue (N : in out O_Lnode; N_Type : in out Node_Acc);
-- Expect: '('
-- Let: next token.
procedure Parse_Association (Constr : in out O_Assoc_List;
Decl : Node_Acc);
function Find_Field_By_Name (Aggr_Type : Node_Acc) return Node_Acc
is
Field : Node_Acc;
begin
Field := Aggr_Type.Record_Union_Fields;
while Field /= null loop
exit when Field.Field_Ident = Token_Sym;
Field := Field.Field_Next;
end loop;
if Field = null then
Parse_Error ("no such field name");
end if;
return Field;
end Find_Field_By_Name;
-- expect: offsetof id.
function Parse_Offsetof (Atype : Node_Acc) return O_Cnode
is
Rec_Type : Node_Acc;
Rec_Field : Node_Acc;
begin
Next_Expect (Tok_Left_Paren);
Next_Expect (Tok_Ident);
Rec_Type := Get_Decl (Token_Sym);
if Rec_Type.Kind /= Decl_Type
or else Rec_Type.Decl_Dtype.Kind /= Type_Record
then
Parse_Error ("type name expected");
end if;
Next_Expect (Tok_Dot);
Next_Expect (Tok_Ident);
Rec_Field := Find_Field_By_Name (Rec_Type.Decl_Dtype);
Next_Expect (Tok_Right_Paren);
return New_Offsetof (Rec_Type.Decl_Dtype.Type_Onode,
Rec_Field.Field_Fnode,
Atype.Type_Onode);
end Parse_Offsetof;
function Parse_Sizeof (Atype : Node_Acc) return O_Cnode
is
Res : O_Cnode;
begin
Next_Expect (Tok_Left_Paren);
Next_Token;
if Tok /= Tok_Ident then
Parse_Error ("type name expected");
end if;
Res := New_Sizeof
(Get_Decl (Token_Sym).Decl_Dtype.Type_Onode,
Atype.Type_Onode);
Next_Expect (Tok_Right_Paren);
return Res;
end Parse_Sizeof;
function Parse_Alignof (Atype : Node_Acc) return O_Cnode
is
Res : O_Cnode;
begin
Next_Expect (Tok_Left_Paren);
Next_Token;
if Tok /= Tok_Ident then
Parse_Error ("type name expected");
end if;
Res := New_Alignof
(Get_Decl (Token_Sym).Decl_Dtype.Type_Onode,
Atype.Type_Onode);
Next_Expect (Tok_Right_Paren);
return Res;
end Parse_Alignof;
-- Parse a literal whose type is ATYPE.
function Parse_Typed_Literal (Atype : Node_Acc) return O_Cnode
is
Res : O_Cnode;
begin
case Tok is
when Tok_Num =>
case Atype.Kind is
when Type_Signed =>
Res := New_Signed_Literal
(Atype.Type_Onode, Integer_64 (Token_Number));
when Type_Unsigned =>
Res := New_Unsigned_Literal
(Atype.Type_Onode, Token_Number);
when others =>
Parse_Error ("bad type for integer literal");
end case;
when Tok_Minus =>
Next_Token;
case Tok is
when Tok_Num =>
declare
V : Integer_64;
begin
if Token_Number = Unsigned_64 (Integer_64'Last) + 1 then
V := Integer_64'First;
else
V := -Integer_64 (Token_Number);
end if;
Res := New_Signed_Literal (Atype.Type_Onode, V);
end;
when Tok_Float_Num =>
Res := New_Float_Literal (Atype.Type_Onode, -Token_Float);
when others =>
Parse_Error ("bad token after '-'");
end case;
when Tok_Float_Num =>
Res := New_Float_Literal (Atype.Type_Onode, Token_Float);
when Tok_Ident =>
declare
Pfx : Node_Acc;
N : Node_Acc;
begin
-- Note: we don't use get_decl, since the name can be a literal
-- name, which is not directly visible.
if Token_Sym.Name /= null
and then Token_Sym.Name.Inter.Kind = Decl_Type
then
-- A typed expression.
Pfx := Token_Sym.Name.Inter;
N := Pfx.Decl_Dtype;
if Atype /= null and then N /= Atype then
Parse_Error ("type mismatch");
end if;
Next_Expect (Tok_Tick);
Next_Token;
if Tok = Tok_Left_Brack then
Next_Token;
Res := Parse_Typed_Literal (N);
Expect (Tok_Right_Brack);
elsif Tok = Tok_Ident then
if Token_Sym = Id_Offsetof then
Res := Parse_Offsetof (N);
elsif Token_Sym = Id_Sizeof then
Res := Parse_Sizeof (N);
elsif Token_Sym = Id_Alignof then
Res := Parse_Alignof (N);
elsif Token_Sym = Id_Address
or Token_Sym = Id_Unchecked_Address
or Token_Sym = Id_Subprg_Addr
then
Res := Parse_Constant_Address (Pfx);
elsif Token_Sym = Id_Conv then
Next_Expect (Tok_Left_Paren);
Next_Token;
Res := Parse_Typed_Literal (N);
Expect (Tok_Right_Paren);
else
Parse_Error ("offsetof or sizeof attributes expected");
end if;
else
Parse_Error ("'[' or attribute expected");
end if;
else
if Atype.Kind /= Type_Enum
and then Atype.Kind /= Type_Boolean
then
Parse_Error ("name allowed only for enumeration");
end if;
N := Atype.Enum_Lits;
while N /= null loop
if Is_Equal (N.Lit_Name, Token_Sym.Ident) then
Res := N.Lit_Cnode;
exit;
end if;
N := N.Lit_Next;
end loop;
if N = null then
Parse_Error ("no matching literal");
return O_Cnode_Null;
end if;
end if;
end;
when Tok_Null =>
Res := New_Null_Access (Atype.Type_Onode);
when others =>
Parse_Error ("bad primary expression: " & Token_Type'Image (Tok));
return O_Cnode_Null;
end case;
Next_Token;
return Res;
end Parse_Typed_Literal;
-- expect: next token
-- Parse an expression starting with NAME.
procedure Parse_Named_Expression
(Atype : Node_Acc; Name : Node_Acc; Stop_At_All : Boolean;
Res : out O_Enode;
Res_Type : out Node_Acc)
is
begin
if Tok = Tok_Tick then
Next_Token;
if Tok = Tok_Left_Brack then
-- Typed literal.
Next_Token;
Res := New_Lit (Parse_Typed_Literal (Name.Decl_Dtype));
Res_Type := Name.Decl_Dtype;
Expect (Tok_Right_Brack);
Next_Token;
elsif Tok = Tok_Left_Paren then
-- Typed expression (used for comparaison operators)
Next_Token;
Parse_Expression (Name.Decl_Dtype, Res, Res_Type);
Expect (Tok_Right_Paren);
Next_Token;
elsif Tok = Tok_Ident then
-- Attribute.
if Token_Sym = Id_Conv then
Next_Expect (Tok_Left_Paren);
Next_Token;
Parse_Expression (null, Res, Res_Type);
-- Discard Res_Type.
Expect (Tok_Right_Paren);
Next_Token;
Res_Type := Name.Decl_Dtype;
Res := New_Convert_Ov (Res, Res_Type.Type_Onode);
-- Fall-through.
elsif Token_Sym = Id_Address
or Token_Sym = Id_Unchecked_Address
or Token_Sym = Id_Subprg_Addr
then
Res_Type := Name.Decl_Dtype;
Res := Parse_Address (Name);
-- Fall-through.
elsif Token_Sym = Id_Sizeof then
Res_Type := Name.Decl_Dtype;
Res := New_Lit (Parse_Sizeof (Res_Type));
Next_Token;
return;
elsif Token_Sym = Id_Alignof then
Res_Type := Name.Decl_Dtype;
Res := New_Lit (Parse_Alignof (Res_Type));
Next_Token;
return;
elsif Token_Sym = Id_Alloca then
Next_Expect (Tok_Left_Paren);
Next_Token;
Parse_Expression (null, Res, Res_Type);
-- Discard Res_Type.
Res_Type := Name.Decl_Dtype;
Res := New_Alloca (Res_Type.Type_Onode, Res);
Expect (Tok_Right_Paren);
Next_Token;
return;
elsif Token_Sym = Id_Offsetof then
Res_Type := Atype;
Res := New_Lit (Parse_Offsetof (Res_Type));
Next_Token;
return;
else
Parse_Error ("unknown attribute name");
end if;
-- Fall-through.
else
Parse_Error ("typed expression expected");
end if;
elsif Tok = Tok_Left_Paren then
if Name.Kind /= Node_Function then
Parse_Error ("function name expected");
end if;
declare
Constr : O_Assoc_List;
begin
Parse_Association (Constr, Name);
Res := New_Function_Call (Constr);
Res_Type := Name.Decl_Dtype;
-- Fall-through.
end;
elsif Name.Kind = Node_Object
or else Name.Kind = Decl_Param
then
-- Name.
declare
Lval : O_Lnode;
begin
Parse_Name (Name, Lval, Res_Type);
Res := New_Value (Lval);
if Atype /= null and then Res_Type /= Atype then
Parse_Error ("type mismatch");
end if;
end;
else
Parse_Error ("bad ident expression: "
& Token_Type'Image (Tok));
end if;
-- Continue.
-- R_TYPE and RES must be set.
if Tok = Tok_Dot then
if Stop_At_All then
return;
end if;
Next_Token;
if Tok = Tok_All then
if Res_Type.Kind /= Type_Access then
Parse_Error ("type of prefix is not an access");
end if;
declare
N : O_Lnode;
begin
Next_Token;
N := New_Access_Element (Res);
Res_Type := Res_Type.Access_Dtype;
Parse_Lvalue (N, Res_Type);
Res := New_Value (N);
end;
return;
else
Parse_Error ("'.all' expected");
end if;
end if;
end Parse_Named_Expression;
procedure Parse_Primary_Expression (Atype : Node_Acc;
Res : out O_Enode;
Res_Type : out Node_Acc)
is
begin
case Tok is
when Tok_Num
| Tok_Float_Num =>
if Atype = null then
Parse_Error ("numeric literal without type context");
end if;
Res_Type := Atype;
Res := New_Lit (Parse_Typed_Literal (Atype));
when Tok_Ident =>
declare
N : Node_Acc;
begin
N := Get_Decl (Token_Sym);
Next_Token;
Parse_Named_Expression (Atype, N, False, Res, Res_Type);
end;
when Tok_Left_Paren =>
Next_Token;
Parse_Expression (Atype, Res, Res_Type);
Expect (Tok_Right_Paren);
Next_Token;
when others =>
Parse_Error ("bad primary expression: " & Token_Type'Image (Tok));
end case;
end Parse_Primary_Expression;
-- Parse '-' EXPR, 'not' EXPR, 'abs' EXPR or EXPR.
procedure Parse_Unary_Expression (Atype : Node_Acc;
Res : out O_Enode;
Res_Type : out Node_Acc)
is
begin
case Tok is
when Tok_Minus =>
Next_Token;
Parse_Primary_Expression (Atype, Res, Res_Type);
Res := New_Monadic_Op (ON_Neg_Ov, Res);
when Tok_Not =>
Next_Token;
Parse_Unary_Expression (Atype, Res, Res_Type);
Res := New_Monadic_Op (ON_Not, Res);
when Tok_Abs =>
Next_Token;
Parse_Unary_Expression (Atype, Res, Res_Type);
Res := New_Monadic_Op (ON_Abs_Ov, Res);
when others =>
Parse_Primary_Expression (Atype, Res, Res_Type);
end case;
end Parse_Unary_Expression;
function Check_Sharp (Op_Ov : ON_Op_Kind) return ON_Op_Kind is
begin
Next_Expect (Tok_Sharp);
Next_Token;
return Op_Ov;
end Check_Sharp;
procedure Parse_Expression (Expr_Type : Node_Acc;
Expr : out O_Enode;
Res_Type : out Node_Acc)
is
Op_Type : Node_Acc;
L : O_Enode;
R : O_Enode;
Op : ON_Op_Kind;
begin
if Expr_Type = null or else Expr_Type.Kind = Type_Boolean then
-- The type of the expression isn't known, as this can be a
-- comparaison operator.
Op_Type := null;
else
Op_Type := Expr_Type;
end if;
Parse_Unary_Expression (Op_Type, L, Res_Type);
case Tok is
when Tok_Div =>
Op := Check_Sharp (ON_Div_Ov);
when Tok_Plus =>
Op := Check_Sharp (ON_Add_Ov);
when Tok_Minus =>
Op := Check_Sharp (ON_Sub_Ov);
when Tok_Star =>
Op := Check_Sharp (ON_Mul_Ov);
when Tok_Mod =>
Op := Check_Sharp (ON_Mod_Ov);
when Tok_Rem =>
Op := Check_Sharp (ON_Rem_Ov);
when Tok_Equal =>
Op := ON_Eq;
when Tok_Not_Equal =>
Op := ON_Neq;
when Tok_Greater =>
Op := ON_Gt;
when Tok_Greater_Eq =>
Op := ON_Ge;
when Tok_Less =>
Op := ON_Lt;
when Tok_Less_Eq =>
Op := ON_Le;
when Tok_Or =>
Op := ON_Or;
Next_Token;
when Tok_And =>
Op := ON_And;
Next_Token;
when Tok_Xor =>
Op := ON_Xor;
Next_Token;
when others =>
Expr := L;
return;
end case;
if Op in ON_Compare_Op_Kind then
Next_Token;
end if;
Parse_Unary_Expression (Res_Type, R, Res_Type);
case Op is
when ON_Dyadic_Op_Kind =>
Expr := New_Dyadic_Op (Op, L, R);
when ON_Compare_Op_Kind =>
if Expr_Type = null then
Parse_Error ("comparaison operator requires a type");
end if;
Expr := New_Compare_Op (Op, L, R, Expr_Type.Type_Onode);
Res_Type := Expr_Type;
when others =>
raise Program_Error;
end case;
end Parse_Expression;
-- Expect and leave: next token
procedure Parse_Lvalue (N : in out O_Lnode; N_Type : in out Node_Acc)
is
begin
loop
case Tok is
when Tok_Dot =>
Next_Token;
if Tok = Tok_All then
if N_Type.Kind /= Type_Access then
Parse_Error ("type of prefix is not an access");
end if;
N := New_Access_Element (New_Value (N));
N_Type := N_Type.Access_Dtype;
Next_Token;
elsif Tok = Tok_Ident then
if N_Type.Kind /= Type_Record and N_Type.Kind /= Type_Union
then
Parse_Error
("type of prefix is neither a record nor an union");
end if;
declare
Field : Node_Acc;
begin
Field := Find_Field_By_Name (N_Type);
N := New_Selected_Element (N, Field.Field_Fnode);
N_Type := Field.Field_Type;
Next_Token;
end;
else
Parse_Error
("'.' must be followed by 'all' or a field name");
end if;
when Tok_Left_Brack =>
declare
V : O_Enode;
Bt : Node_Acc;
Res_Type : Node_Acc;
begin
Next_Token;
if N_Type.Kind = Type_Subarray then
Bt := N_Type.Subarray_Base;
else
Bt := N_Type;
end if;
if Bt.Kind /= Type_Array then
Parse_Error ("type of prefix is not an array");
end if;
Parse_Expression (Bt.Array_Index, V, Res_Type);
if Tok = Tok_Elipsis then
N := New_Slice (N, Bt.Type_Onode, V);
Next_Token;
else
N := New_Indexed_Element (N, V);
N_Type := Bt.Array_Element;
end if;
Expect (Tok_Right_Brack);
Next_Token;
end;
when others =>
return;
end case;
end loop;
end Parse_Lvalue;
procedure Parse_Name (Prefix : Node_Acc;
Name : out O_Lnode; N_Type : out Node_Acc)
is
begin
case Prefix.Kind is
when Decl_Param =>
Name := New_Obj (Prefix.Param_Node);
N_Type := Prefix.Decl_Dtype;
when Node_Object =>
Name := New_Obj (Prefix.Obj_Node);
N_Type := Prefix.Decl_Dtype;
when Decl_Type =>
declare
Val : O_Enode;
begin
Parse_Named_Expression (null, Prefix, True, Val, N_Type);
if N_Type /= Prefix.Decl_Dtype then
Parse_Error ("type doesn't match");
end if;
if Tok = Tok_Dot then
Next_Token;
if Tok = Tok_All then
if N_Type.Kind /= Type_Access then
Parse_Error ("type of prefix is not an access");
end if;
Name := New_Access_Element (Val);
N_Type := N_Type.Access_Dtype;
Next_Token;
else
Parse_Error ("'.all' expected");
end if;
else
Parse_Error ("name expected");
end if;
end;
when others =>
Parse_Error ("invalid name");
end case;
Parse_Lvalue (Name, N_Type);
end Parse_Name;
-- Expect: '('
-- Let: next token.
procedure Parse_Association (Constr : in out O_Assoc_List; Decl : Node_Acc)
is
Param : Node_Acc;
Expr : O_Enode;
Expr_Type : Node_Acc;
begin
Start_Association (Constr, Decl.Subprg_Node);
if Tok /= Tok_Left_Paren then
Parse_Error ("'(' expected for a subprogram call");
end if;
Next_Token;
Param := Decl.Subprg_Params;
while Tok /= Tok_Right_Paren loop
if Param = null then
Parse_Error ("too many parameters");
end if;
Parse_Expression (Param.Decl_Dtype, Expr, Expr_Type);
New_Association (Constr, Expr);
Param := Param.Param_Next;
exit when Tok /= Tok_Comma;
Next_Token;
end loop;
if Param /= null then
Parse_Error ("missing parameters");
end if;
if Tok /= Tok_Right_Paren then
Parse_Error ("')' expected to finish a subprogram call, found "
& Token_Type'Image (Tok));
end if;
Next_Token;
end Parse_Association;
type Loop_Info;
type Loop_Info_Acc is access Loop_Info;
type Loop_Info is record
Num : Natural;
Blk : O_Snode;
Prev : Loop_Info_Acc;
end record;
procedure Free is new Ada.Unchecked_Deallocation
(Name => Loop_Info_Acc, Object => Loop_Info);
Loop_Stack : Loop_Info_Acc := null;
function Find_Loop (N : Natural) return Loop_Info_Acc
is
Res : Loop_Info_Acc;
begin
Res := Loop_Stack;
while Res /= null loop
if Res.Num = N then
return Res;
end if;
Res := Res.Prev;
end loop;
return null;
end Find_Loop;
Current_Subprg : Node_Acc := null;
procedure Parse_Statement;
-- Expect : next token
-- Let: next token
procedure Parse_Statements is
begin
loop
exit when Tok = Tok_End;
exit when Tok = Tok_Else;
exit when Tok = Tok_When;
Parse_Statement;
end loop;
end Parse_Statements;
-- Expect : next token
-- Let: next token
procedure Parse_Statement is
begin
if Flag_Renumber then
New_Debug_Line_Stmt (Lineno);
end if;
case Tok is
when Tok_Comment =>
Next_Token;
when Tok_Declare =>
Start_Declare_Stmt;
Parse_Compound_Statement;
Expect (Tok_Semicolon);
Next_Token;
Finish_Declare_Stmt;
when Tok_Line_Number =>
Next_Expect (Tok_Num);
if Flag_Renumber = False then
New_Debug_Line_Stmt (Natural (Token_Number));
end if;
Next_Token;
when Tok_If =>
declare
If_Blk : O_If_Block;
Cond : O_Enode;
Cond_Type : Node_Acc;
begin
Next_Token;
Parse_Expression (null, Cond, Cond_Type);
Start_If_Stmt (If_Blk, Cond);
Expect (Tok_Then);
Next_Token;
Parse_Statements;
if Tok = Tok_Else then
Next_Token;
New_Else_Stmt (If_Blk);
Parse_Statements;
end if;
Finish_If_Stmt (If_Blk);
Expect (Tok_End);
Next_Expect (Tok_If);
Next_Expect (Tok_Semicolon);
Next_Token;
end;
when Tok_Loop =>
-- Grammar:
-- LOOP n:
-- stmts
-- END LOOP;
declare
Info : Loop_Info_Acc;
Num : Natural;
begin
Next_Expect (Tok_Num);
Num := Natural (Token_Number);
if Find_Loop (Num) /= null then
Parse_Error ("loop label already defined");
end if;
Info := new Loop_Info;
Info.Num := Num;
Info.Prev := Loop_Stack;
Loop_Stack := Info;
Start_Loop_Stmt (Info.Blk);
Next_Expect (Tok_Colon);
Next_Token;
Parse_Statements;
Finish_Loop_Stmt (Info.Blk);
Next_Expect (Tok_Loop);
Next_Expect (Tok_Semicolon);
Loop_Stack := Info.Prev;
Free (Info);
Next_Token;
end;
when Tok_Exit
| Tok_Next =>
-- Grammar:
-- EXIT LOOP n;
-- NEXT LOOP n;
declare
Label : Loop_Info_Acc;
Etok : Token_Type;
begin
Etok := Tok;
Next_Expect (Tok_Loop);
Next_Expect (Tok_Num);
Label := Find_Loop (Natural (Token_Number));
if Label = null then
Parse_Error ("no such loop");
end if;
if Etok = Tok_Exit then
New_Exit_Stmt (Label.Blk);
else
New_Next_Stmt (Label.Blk);
end if;
Next_Expect (Tok_Semicolon);
Next_Token;
end;
when Tok_Return =>
-- Grammar:
-- RETURN;
-- RETURN expr;
declare
Res : O_Enode;
Res_Type : Node_Acc;
begin
Next_Token;
if Tok /= Tok_Semicolon then
Parse_Expression (Current_Subprg.Decl_Dtype, Res, Res_Type);
New_Return_Stmt (Res);
if Tok /= Tok_Semicolon then
Parse_Error ("';' expected at end of return statement");
end if;
else
New_Return_Stmt;
end if;
Next_Token;
end;
when Tok_Ident =>
-- This is either a procedure call or an assignment.
declare
Inter : Node_Acc;
begin
Inter := Get_Decl (Token_Sym);
Next_Token;
if Tok = Tok_Left_Paren then
-- A procedure call.
declare
Constr : O_Assoc_List;
begin
Parse_Association (Constr, Inter);
New_Procedure_Call (Constr);
if Tok /= Tok_Semicolon then
Parse_Error ("';' expected after call");
end if;
Next_Token;
return;
end;
else
-- An assignment.
declare
Name : O_Lnode;
Expr : O_Enode;
Expr_Type : Node_Acc;
N_Type : Node_Acc;
begin
Parse_Name (Inter, Name, N_Type);
if Tok /= Tok_Assign then
Parse_Error ("`:=' expected after a variable");
end if;
Next_Token;
Parse_Expression (N_Type, Expr, Expr_Type);
New_Assign_Stmt (Name, Expr);
if Tok /= Tok_Semicolon then
Parse_Error ("';' expected at end of assignment");
end if;
Next_Token;
return;
end;
end if;
end;
when Tok_Case =>
-- Grammar:
-- CASE expr IS
-- WHEN lit =>
-- WHEN lit ... lit =>
-- WHEN DEFAULT =>
-- END CASE;
declare
Case_Blk : O_Case_Block;
L : O_Cnode;
Choice : O_Enode;
Choice_Type : Node_Acc;
begin
Next_Token;
Parse_Expression (null, Choice, Choice_Type);
Start_Case_Stmt (Case_Blk, Choice);
Expect (Tok_Is);
Next_Token;
loop
exit when Tok = Tok_End;
Expect (Tok_When);
Next_Token;
Start_Choice (Case_Blk);
loop
if Tok = Tok_Default then
New_Default_Choice (Case_Blk);
Next_Token;
else
L := Parse_Typed_Literal (Choice_Type);
if Tok = Tok_Elipsis then
Next_Token;
New_Range_Choice
(Case_Blk, L, Parse_Typed_Literal (Choice_Type));
else
New_Expr_Choice (Case_Blk, L);
end if;
end if;
exit when Tok = Tok_Arrow;
Expect (Tok_Comma);
Next_Token;
end loop;
-- Skip '=>'.
Next_Token;
Finish_Choice (Case_Blk);
Parse_Statements;
end loop;
Finish_Case_Stmt (Case_Blk);
Expect (Tok_End);
Next_Expect (Tok_Case);
Next_Expect (Tok_Semicolon);
Next_Token;
end;
when others =>
Parse_Error ("bad statement: " & Token_Type'Image (Tok));
end case;
end Parse_Statement;
procedure Parse_Compound_Statement is
begin
if Tok /= Tok_Declare then
Parse_Error ("'declare' expected to start a statements block");
end if;
Next_Token;
Push_Scope;
-- Parse declarations.
while Tok /= Tok_Begin loop
Parse_Declaration;
end loop;
Next_Token;
-- Parse statements.
Parse_Statements;
Expect (Tok_End);
Next_Token;
Pop_Scope;
end Parse_Compound_Statement;
-- Parse (P1 : T1; P2: T2; ...)
function Parse_Parameter_List return Node_Acc
is
First, Last : Node_Acc;
P : Node_Acc;
begin
Expect (Tok_Left_Paren);
Next_Token;
if Tok = Tok_Right_Paren then
Next_Token;
return null;
end if;
First := null;
Last := null;
loop
Expect (Tok_Ident);
P := new Node'(Kind => Decl_Param,
Decl_Dtype => null,
Decl_Storage => O_Storage_Public,
Decl_Defined => False,
Param_Node => O_Dnode_Null,
Param_Name => Token_Sym,
Param_Next => null);
-- Link
if Last = null then
First := P;
else
Last.Param_Next := P;
end if;
Last := P;
Next_Expect (Tok_Colon);
Next_Token;
P.Decl_Dtype := Parse_Type;
exit when Tok = Tok_Right_Paren;
Expect (Tok_Semicolon);
Next_Token;
end loop;
Next_Token;
return First;
end Parse_Parameter_List;
procedure Create_Interface_List (Constr : in out O_Inter_List;
First_Inter : Node_Acc)
is
Inter : Node_Acc;
begin
Inter := First_Inter;
while Inter /= null loop
New_Interface_Decl (Constr, Inter.Param_Node, Inter.Param_Name.Ident,
Inter.Decl_Dtype.Type_Onode);
Inter := Inter.Param_Next;
end loop;
end Create_Interface_List;
procedure Check_Parameter_List (List : Node_Acc)
is
Param : Node_Acc;
begin
Next_Expect (Tok_Left_Paren);
Next_Token;
Param := List;
while Tok /= Tok_Right_Paren loop
if Param = null then
Parse_Error ("subprogram redefined with more parameters");
end if;
Expect (Tok_Ident);
if Token_Sym /= Param.Param_Name then
Parse_Error ("subprogram redefined with different parameter name");
end if;
Next_Expect (Tok_Colon);
Next_Token;
if Parse_Type /= Param.Decl_Dtype then
Parse_Error ("subprogram redefined with different parameter type");
end if;
Param := Param.Param_Next;
exit when Tok = Tok_Right_Paren;
Expect (Tok_Semicolon);
Next_Token;
end loop;
Expect (Tok_Right_Paren);
Next_Token;
if Param /= null then
Parse_Error ("subprogram redefined with less parameters");
end if;
end Check_Parameter_List;
procedure Parse_Subprogram_Body (Subprg : Node_Acc)
is
Param : Node_Acc;
Prev_Subprg : Node_Acc;
begin
Prev_Subprg := Current_Subprg;
Current_Subprg := Subprg;
Start_Subprogram_Body (Subprg.Subprg_Node);
Push_Scope;
-- Put parameters in the current scope.
Param := Subprg.Subprg_Params;
while Param /= null loop
Add_Decl (Param.Param_Name, Param);
Param := Param.Param_Next;
end loop;
Parse_Compound_Statement;
Pop_Scope;
Finish_Subprogram_Body;
Current_Subprg := Prev_Subprg;
end Parse_Subprogram_Body;
procedure Parse_Function_Definition (Storage : O_Storage)
is
Constr : O_Inter_List;
Sym : Syment_Acc;
N : Node_Acc;
begin
Expect (Tok_Function);
Next_Expect (Tok_Ident);
Sym := Token_Sym;
if Sym.Name /= null then
N := Get_Decl (Sym);
Check_Parameter_List (N.Subprg_Params);
Expect (Tok_Return);
Next_Expect (Tok_Ident);
Next_Token;
else
N := new Node'(Kind => Node_Function,
Decl_Dtype => null,
Decl_Storage => Storage,
Decl_Defined => False,
Subprg_Node => O_Dnode_Null,
Subprg_Name => Sym,
Subprg_Params => null);
Next_Token;
N.Subprg_Params := Parse_Parameter_List;
Expect (Tok_Return);
Next_Token;
N.Decl_Dtype := Parse_Type;
Start_Function_Decl (Constr, N.Subprg_Name.Ident, Storage,
N.Decl_Dtype.Type_Onode);
Create_Interface_List (Constr, N.Subprg_Params);
Finish_Subprogram_Decl (Constr, N.Subprg_Node);
Add_Decl (Sym, N);
end if;
if Tok = Tok_Declare then
Parse_Subprogram_Body (N);
end if;
end Parse_Function_Definition;
procedure Parse_Procedure_Definition (Storage : O_Storage)
is
Constr : O_Inter_List;
Sym : Syment_Acc;
N : Node_Acc;
begin
Expect (Tok_Procedure);
Next_Expect (Tok_Ident);
Sym := Token_Sym;
if Sym.Name /= null then
N := Get_Decl (Sym);
Check_Parameter_List (N.Subprg_Params);
else
N := new Node'(Kind => Node_Procedure,
Decl_Dtype => null,
Decl_Storage => Storage,
Decl_Defined => False,
Subprg_Node => O_Dnode_Null,
Subprg_Name => Sym,
Subprg_Params => null);
Next_Token;
N.Subprg_Params := Parse_Parameter_List;
Start_Procedure_Decl (Constr, N.Subprg_Name.Ident, Storage);
Create_Interface_List (Constr, N.Subprg_Params);
Finish_Subprogram_Decl (Constr, N.Subprg_Node);
Add_Decl (Sym, N);
end if;
if Tok = Tok_Declare then
Parse_Subprogram_Body (N);
end if;
end Parse_Procedure_Definition;
function Parse_Address (Prefix : Node_Acc) return O_Enode
is
Pfx : Node_Acc;
N : O_Lnode;
N_Type : Node_Acc;
Res : O_Enode;
Attr : Syment_Acc;
T : O_Tnode;
begin
Attr := Token_Sym;
Next_Expect (Tok_Left_Paren);
Next_Expect (Tok_Ident);
Pfx := Get_Decl (Token_Sym);
T := Prefix.Decl_Dtype.Type_Onode;
if Attr = Id_Subprg_Addr then
Expect (Tok_Ident);
Pfx := Get_Decl (Token_Sym);
if Pfx.Kind not in Nodes_Subprogram then
Parse_Error ("subprogram identifier expected");
end if;
Res := New_Lit (New_Subprogram_Address (Pfx.Subprg_Node, T));
Next_Token;
else
Next_Token;
Parse_Name (Pfx, N, N_Type);
if Attr = Id_Address then
Res := New_Address (N, T);
elsif Attr = Id_Unchecked_Address then
Res := New_Unchecked_Address (N, T);
else
Parse_Error ("address attribute expected");
end if;
end if;
Expect (Tok_Right_Paren);
Next_Token;
return Res;
end Parse_Address;
function Parse_Constant_Address (Prefix : Node_Acc) return O_Cnode
is
Pfx : Node_Acc;
Res : O_Cnode;
Attr : Syment_Acc;
T : O_Tnode;
begin
Attr := Token_Sym;
Next_Expect (Tok_Left_Paren);
Next_Expect (Tok_Ident);
Pfx := Get_Decl (Token_Sym);
T := Prefix.Decl_Dtype.Type_Onode;
if Attr = Id_Subprg_Addr then
Expect (Tok_Ident);
Pfx := Get_Decl (Token_Sym);
if Pfx.Kind not in Nodes_Subprogram then
Parse_Error ("subprogram identifier expected");
end if;
Res := New_Subprogram_Address (Pfx.Subprg_Node, T);
Next_Token;
else
Next_Token;
if Attr = Id_Address then
Res := New_Global_Address (Pfx.Obj_Node, T);
elsif Attr = Id_Unchecked_Address then
Res := New_Global_Unchecked_Address (Pfx.Obj_Node, T);
else
Parse_Error ("address attribute expected");
end if;
end if;
Expect (Tok_Right_Paren);
return Res;
end Parse_Constant_Address;
function Parse_Constant_Value (Atype : Node_Acc) return O_Cnode
is
Res : O_Cnode;
begin
case Atype.Kind is
when Type_Subarray =>
declare
El : constant Node_Acc := Atype.Subarray_Base.Array_Element;
Constr : O_Array_Aggr_List;
begin
Expect (Tok_Left_Brace);
Next_Token;
Start_Array_Aggr (Constr, Atype.Type_Onode);
for I in Natural loop
exit when Tok = Tok_Right_Brace;
if I /= 0 then
Expect (Tok_Comma);
Next_Token;
end if;
New_Array_Aggr_El (Constr, Parse_Constant_Value (El));
end loop;
Finish_Array_Aggr (Constr, Res);
Next_Token;
return Res;
end;
when Type_Unsigned
| Type_Signed
| Type_Enum
| Type_Float
| Type_Boolean
| Type_Access =>
--return Parse_Primary_Expression (Atype);
return Parse_Typed_Literal (Atype);
when Type_Record =>
declare
Constr : O_Record_Aggr_List;
Field : Node_Acc;
begin
Expect (Tok_Left_Brace);
Next_Token;
Start_Record_Aggr (Constr, Atype.Type_Onode);
Field := Atype.Record_Union_Fields;
while Field /= null loop
if Tok = Tok_Dot then
Next_Expect (Tok_Ident);
if Token_Sym /= Field.Field_Ident then
Parse_Error ("bad field name");
end if;
Next_Expect (Tok_Equal);
Next_Token;
end if;
New_Record_Aggr_El
(Constr, Parse_Constant_Value (Field.Field_Type));
Field := Field.Field_Next;
if Field /= null then
Expect (Tok_Comma);
Next_Token;
end if;
end loop;
Finish_Record_Aggr (Constr, Res);
Expect (Tok_Right_Brace);
Next_Token;
return Res;
end;
when Type_Union =>
declare
Field : Node_Acc;
begin
Expect (Tok_Left_Brace);
Next_Token;
Expect (Tok_Dot);
Next_Expect (Tok_Ident);
Field := Find_Field_By_Name (Atype);
Next_Expect (Tok_Equal);
Next_Token;
Res := New_Union_Aggr
(Atype.Type_Onode, Field.Field_Fnode,
Parse_Constant_Value (Field.Field_Type));
Expect (Tok_Right_Brace);
Next_Token;
return Res;
end;
when others =>
raise Program_Error;
end case;
end Parse_Constant_Value;
procedure Parse_Constant_Declaration (Storage : O_Storage)
is
N : Node_Acc;
Sym : Syment_Acc;
Val : O_Cnode;
begin
Expect (Tok_Constant);
Next_Expect (Tok_Ident);
Sym := Token_Sym;
N := new Node'(Kind => Node_Object,
Decl_Dtype => null,
Decl_Storage => Storage,
Decl_Defined => False,
Obj_Name => Sym.Ident,
Obj_Node => O_Dnode_Null);
Next_Expect (Tok_Colon);
Next_Token;
N.Decl_Dtype := Parse_Type;
New_Const_Decl (N.Obj_Node, Sym.Ident, Storage, N.Decl_Dtype.Type_Onode);
Add_Decl (Sym, N);
if Tok = Tok_Assign then
N.Decl_Defined := True;
Next_Token;
Start_Init_Value (N.Obj_Node);
Val := Parse_Constant_Value (N.Decl_Dtype);
Finish_Init_Value (N.Obj_Node, Val);
end if;
end Parse_Constant_Declaration;
-- Grammar:
-- CONSTANT ident := value ;
procedure Parse_Constant_Value_Declaration
is
N : Node_Acc;
Val : O_Cnode;
begin
Next_Expect (Tok_Ident);
N := Get_Decl (Token_Sym);
if N.Kind /= Node_Object then
Parse_Error ("name of a constant expected");
end if;
if N.Decl_Defined then
Parse_Error ("constant already defined");
else
N.Decl_Defined := True;
end if;
-- FIXME: should check storage,
-- should check the object is a constant,
-- should check the object has no value.
Next_Expect (Tok_Assign);
Next_Token;
Start_Init_Value (N.Obj_Node);
Val := Parse_Constant_Value (N.Decl_Dtype);
Finish_Init_Value (N.Obj_Node, Val);
end Parse_Constant_Value_Declaration;
procedure Parse_Var_Declaration (Storage : O_Storage)
is
N : Node_Acc;
Sym : Syment_Acc;
begin
Expect (Tok_Var);
Next_Expect (Tok_Ident);
Sym := Token_Sym;
N := new Node'(Kind => Node_Object,
Decl_Dtype => null,
Decl_Storage => Storage,
Decl_Defined => False,
Obj_Name => Sym.Ident,
Obj_Node => O_Dnode_Null);
Next_Expect (Tok_Colon);
Next_Token;
N.Decl_Dtype := Parse_Type;
New_Var_Decl (N.Obj_Node, Sym.Ident, Storage, N.Decl_Dtype.Type_Onode);
Add_Decl (Sym, N);
end Parse_Var_Declaration;
procedure Parse_Stored_Decl (Storage : O_Storage)
is
begin
Next_Token;
if Tok = Tok_Function then
Parse_Function_Definition (Storage);
elsif Tok = Tok_Procedure then
Parse_Procedure_Definition (Storage);
elsif Tok = Tok_Constant then
Parse_Constant_Declaration (Storage);
elsif Tok = Tok_Var then
Parse_Var_Declaration (Storage);
else
Parse_Error ("function or object declaration expected");
end if;
end Parse_Stored_Decl;
procedure Parse_Declaration
is
Inter : Node_Acc;
S : Syment_Acc;
begin
if Flag_Renumber then
New_Debug_Line_Decl (Lineno);
end if;
case Tok is
when Tok_Type =>
Next_Token;
if Tok /= Tok_Ident then
Parse_Error ("identifier for type expected");
end if;
S := Token_Sym;
Next_Expect (Tok_Is);
Next_Token;
if Is_Defined (S) then
Parse_Type_Completion (Get_Decl (S));
else
Inter := new Node'(Kind => Decl_Type,
Decl_Storage => O_Storage_Public,
Decl_Defined => False,
Decl_Dtype => Parse_Type);
Add_Decl (S, Inter);
New_Type_Decl (S.Ident, Inter.Decl_Dtype.Type_Onode);
end if;
when Tok_External =>
Parse_Stored_Decl (O_Storage_External);
when Tok_Private =>
Parse_Stored_Decl (O_Storage_Private);
when Tok_Public =>
Parse_Stored_Decl (O_Storage_Public);
when Tok_Local =>
Parse_Stored_Decl (O_Storage_Local);
when Tok_Constant =>
Parse_Constant_Value_Declaration;
when Tok_Comment =>
New_Debug_Comment_Decl (Token_Ident (1 .. Token_Idlen));
Next_Token;
return;
when Tok_File_Name =>
if Flag_Renumber = False then
New_Debug_Filename_Decl (Token_Ident (1 .. Token_Idlen));
end if;
Next_Token;
return;
when others =>
Parse_Error ("declaration expected");
end case;
Expect (Tok_Semicolon);
Next_Token;
end Parse_Declaration;
-- procedure Put (Str : String)
-- is
-- L : Integer;
-- begin
-- L := Write (Standout, Str'Address, Str'Length);
-- end Put;
function Parse (Filename : String_Acc) return Boolean
is
begin
-- Initialize symbol table.
Add_Keyword ("type", Tok_Type);
Add_Keyword ("return", Tok_Return);
Add_Keyword ("if", Tok_If);
Add_Keyword ("then", Tok_Then);
Add_Keyword ("else", Tok_Else);
Add_Keyword ("elsif", Tok_Elsif);
Add_Keyword ("loop", Tok_Loop);
Add_Keyword ("exit", Tok_Exit);
Add_Keyword ("next", Tok_Next);
Add_Keyword ("signed", Tok_Signed);
Add_Keyword ("unsigned", Tok_Unsigned);
Add_Keyword ("float", Tok_Float);
Add_Keyword ("is", Tok_Is);
Add_Keyword ("of", Tok_Of);
Add_Keyword ("all", Tok_All);
Add_Keyword ("not", Tok_Not);
Add_Keyword ("abs", Tok_Abs);
Add_Keyword ("or", Tok_Or);
Add_Keyword ("and", Tok_And);
Add_Keyword ("xor", Tok_Xor);
Add_Keyword ("mod", Tok_Mod);
Add_Keyword ("rem", Tok_Rem);
Add_Keyword ("array", Tok_Array);
Add_Keyword ("access", Tok_Access);
Add_Keyword ("record", Tok_Record);
Add_Keyword ("union", Tok_Union);
Add_Keyword ("end", Tok_End);
Add_Keyword ("boolean", Tok_Boolean);
Add_Keyword ("enum", Tok_Enum);
Add_Keyword ("external", Tok_External);
Add_Keyword ("private", Tok_Private);
Add_Keyword ("public", Tok_Public);
Add_Keyword ("local", Tok_Local);
Add_Keyword ("procedure", Tok_Procedure);
Add_Keyword ("function", Tok_Function);
Add_Keyword ("constant", Tok_Constant);
Add_Keyword ("var", Tok_Var);
Add_Keyword ("subarray", Tok_Subarray);
Add_Keyword ("declare", Tok_Declare);
Add_Keyword ("begin", Tok_Begin);
Add_Keyword ("end", Tok_End);
Add_Keyword ("null", Tok_Null);
Add_Keyword ("case", Tok_Case);
Add_Keyword ("when", Tok_When);
Add_Keyword ("default", Tok_Default);
Id_Address := New_Symbol ("address");
Id_Unchecked_Address := New_Symbol ("unchecked_address");
Id_Subprg_Addr := New_Symbol ("subprg_addr");
Id_Conv := New_Symbol ("conv");
Id_Sizeof := New_Symbol ("sizeof");
Id_Alignof := New_Symbol ("alignof");
Id_Alloca := New_Symbol ("alloca");
Id_Offsetof := New_Symbol ("offsetof");
-- Initialize the scanner.
Buf (1) := NUL;
Pos := 1;
Lineno := 1;
if Filename = null then
Fd := Standin;
File_Name := new String'("*stdin*");
else
declare
Name : String (1 .. Filename'Length + 1);
begin
Name (1 .. Filename'Length) := Filename.all;
Name (Name'Last) := NUL;
File_Name := Filename;
Fd := Open_Read (Name'Address, Text);
if Fd = Invalid_FD then
Puterr ("cannot open '" & Filename.all & ''');
Newline_Err;
return False;
end if;
end;
end if;
New_Debug_Filename_Decl (File_Name.all);
Push_Scope;
Next_Token;
while Tok /= Tok_Eof loop
Parse_Declaration;
end loop;
Pop_Scope;
if Fd /= Standin then
Close (Fd);
end if;
return True;
exception
when Error =>
return False;
when E : others =>
Puterr (Ada.Exceptions.Exception_Information (E));
raise;
end Parse;
end Ortho_Front;
|