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
|
/* Lower the Algol 68 parse tree to GENERIC.
Copyright (C) 2025 Jose E. Marchesi.
Written by Jose E. Marchesi.
GCC 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 3, or (at your option)
any later version.
GCC 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 COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#define INCLUDE_MEMORY
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "fold-const.h"
#include "diagnostic.h"
#include "langhooks.h"
#include "tm.h"
#include "function.h"
#include "cgraph.h"
#include "toplev.h"
#include "varasm.h"
#include "predict.h"
#include "stor-layout.h"
#include "tree-iterator.h"
#include "stringpool.h"
#include "print-tree.h"
#include "gimplify.h"
#include "dumpfile.h"
#include "convert.h"
#include "a68.h"
/* Return a tree with an identifier for the mangled version of a given
name.
Publicized symbols feature the module name publishing them.
Internal symbols don't.
Bold words, i.e. mode indicants, module indicants and operator indicants,
are mangled to upper-case.
Tags, i.e. identifiers are mangled to lower-case.
Monads and nomads are mangled to letter codes symbolizing the symbols:
% (p)ercentage
^ (c)aret
& (a)mpersand
+ pl(u)s
- (m)inus
~ (t)ilde
! (b)ang
? (q)uestion mark
> bi(g)ger than
< (l)ess than
/ (s)lash
= (e)qual
: c(o)lon
* sta(r)
Each letter code is followed by a single underscore character. */
static tree
get_mangled_identifier_or_indicant (const char *name, bool indicant,
const char *mname, bool internal,
bool numbered)
{
/* First determine the size of the mangled symbol. */
size_t mangled_size = strlen (name) + 1;
if (mname)
{
/* Add size for MNAME_ */
mangled_size += strlen (mname) + 1;
if (internal)
/* Another _ */
mangled_size += 1;
}
for (const char *p = name; *p; ++p)
{
mangled_size += 1;
if (strchr (MONADS, *p) != NULL
|| strchr (NOMADS, *p) != NULL
|| *p == ':')
/* Each monad or nomad requires two chars to encode. */
mangled_size += 1;
}
char *number_buf = NULL;
if (numbered)
{
static unsigned int cnt;
number_buf = xasprintf ("%d", cnt++);
mangled_size += strlen (number_buf);
}
/* Now fill-in the mangled symbol. */
char *mangled_name = (char *) alloca (mangled_size);
size_t pos = 0;
if (mname)
{
for (const char *p = mname; *p; ++p)
/* Module names are bold words. Make sure to emit them in upper-case. */
mangled_name[pos++] = TOUPPER (*p);
mangled_name[pos++] = '_';
if (internal)
mangled_name[pos++] = '_';
}
for (const char *p = name; *p; ++p)
{
if (strchr (MONADS, *p) != NULL
|| strchr (NOMADS, *p) != NULL
|| *p == ':')
{
char c;
switch (*p)
{
case '%': c = 'p'; break;
case '^': c = 'c'; break;
case '&': c = 'a'; break;
case '+': c = 'u'; break;
case '-': c = 'm'; break;
case '~': c = 't'; break;
case '!': c = 'b'; break;
case '?': c = 'q'; break;
case '>': c = 'g'; break;
case '<': c = 'l'; break;
case '/': c = 's'; break;
case '=': c = 'e'; break;
case ':': c = 'o'; break;
case '*': c = 'r'; break;
default:
/* Should not happen. */
gcc_unreachable ();
}
mangled_name[pos++] = c;
mangled_name[pos++] = '_';
}
else
{
if (indicant)
mangled_name[pos++] = TOUPPER (*p);
else
mangled_name[pos++] = TOLOWER (*p);
}
}
if (numbered)
{
for (char *p = number_buf; *p; ++p)
mangled_name[pos++] = *p;
free (number_buf);
}
mangled_name[pos++] = '\0';
return get_identifier (mangled_name);
}
tree
a68_get_mangled_identifier (const char *name, const char *mname,
bool internal, bool numbered)
{
return get_mangled_identifier_or_indicant (name, false /* indicant */, mname,
internal, numbered);
}
tree
a68_get_mangled_indicant (const char *name, const char *mname,
bool internal, bool numbered)
{
return get_mangled_identifier_or_indicant (name, true /* indicant */, mname,
internal, numbered);
}
/* Demangle a given SYMBOL.
This function does the reverse operation than
get_mangled_identifier_or_indicant. */
char *
a68_demangle_symbol (const char *mname, const char *symbol,
bool is_operator)
{
gcc_assert (strlen (symbol) >= strlen (mname) + 1);
/* First get rid of the module name and underscore. */
symbol += strlen (mname) + 1;
/* Now demangle the rest. */
size_t size = strlen (symbol) + 1;
char *demangled = (char *) xmalloc (size + 1);
size_t o = 0;
for (size_t i = 0; i < size; ++i)
{
if (symbol[i+1] == '_')
{
switch (symbol[i])
{
case 'p': demangled[o++] = '+'; break;
case 'c': demangled[o++] = '^'; break;
case 'a': demangled[o++] = '&'; break;
case 'u': demangled[o++] = '+'; break;
case 'm': demangled[o++] = '-'; break;
case 't': demangled[o++] = '~'; break;
case 'b': demangled[o++] = '!'; break;
case 'q': demangled[o++] = '?'; break;
case 'g': demangled[o++] = '>'; break;
case 'l': demangled[o++] = '<'; break;
case 's': demangled[o++] = '/'; break;
case 'e': demangled[o++] = '='; break;
case 'o': demangled[o++] = ':'; break;
case 'r': demangled[o++] = '*'; break;
default:
/* Invalid mangling. */
// XXX this should be checked at import time in extract.
gcc_unreachable ();
}
i += 1;
}
else
demangled[o++] = symbol[i];
}
demangled[o] = '\0';
if (is_operator)
{
/* Remove trailing digits. */
for (size_t i = strlen (demangled) - 1; i > 0; --i)
{
if (ISDIGIT (demangled[i]))
demangled[i] = '\0';
else
break;
}
}
return demangled;
}
/* Return a tree with the EMPTY value.
EMPTY is the only denotation of the VOID mode. It is used in unions to
denote "no value". It must have size zero, so it lowers into an empty
constructor with zero elements of type void. This is what GNU C uses to
implement the empty struct extension. */
tree
a68_get_empty (void)
{
return build_constructor (a68_void_type, NULL);
}
/* Return a tree with the yielding of SKIP of a given mode.
SKIP stands for some value of some given mode. It shall be used only in a
context where the compiler can determine the mode.
The particular value to which it elaborates is non-important, but this
compiler always uses the same values. See the a68_get_ref_*_tree functions
for details on what values are these. */
tree
a68_get_skip_tree (MOID_T *m)
{
tree expr = NULL_TREE;
while (EQUIVALENT (m) != NO_MOID)
m = EQUIVALENT (m);
if (IS_INTEGRAL (m))
expr = a68_get_int_skip_tree (m);
else if (m == M_CHAR)
expr = a68_get_char_skip_tree ();
else if (m == M_BOOL)
expr = a68_get_bool_skip_tree ();
else if (IS_REAL (m))
expr = a68_get_real_skip_tree (m);
else if (IS_BITS (m))
expr = a68_get_bits_skip_tree (m);
else if (IS_REF (m))
expr = a68_get_ref_skip_tree (m);
else if (IS (m, PROC_SYMBOL))
expr = a68_get_proc_skip_tree (m);
else if (IS_STRUCT (m))
expr = a68_get_struct_skip_tree (m);
else if (IS_UNION (m))
expr = a68_get_union_skip_tree (m);
else if (IS_FLEXETY_ROW (m))
expr = a68_get_multiple_skip_tree (m);
else if (m == M_STRING)
expr = a68_get_string_skip_tree ();
else if (m == M_ROWS || IS (m, SERIES_MODE))
{
/* XXX assert that all modes in the series are rows? */
tree rows_type = CTYPE (M_ROWS);
tree dim_field = TYPE_FIELDS (rows_type);
tree triplets_field = TREE_CHAIN (dim_field);
tree null_pointer = build_int_cst (TREE_TYPE (triplets_field), 0);
expr = build_constructor_va (rows_type, 2,
dim_field, size_zero_node,
triplets_field, null_pointer);
}
else if (m == M_VOID || m == M_HIP)
expr = a68_get_empty ();
else
{
fatal_error (UNKNOWN_LOCATION,
"get skip tree: cannot compute SKIP for mode %s",
a68_moid_to_string (m, MOID_ERROR_WIDTH, NODE (m), true));
gcc_unreachable ();
}
return expr;
}
/* Given a tree node EXP holding a value of mode M:
*NUM_REFS is set to the number of REFs in M.
*NUM_POINTERS is set to the number of pointers in the type of EXP that
correspond to the REFs in M. */
void
a68_ref_counts (tree exp, MOID_T *m, int *num_refs, int *num_pointers)
{
/* Count REFs in M and pointers in the type of EXP. Note that VAR_DECLs
corresponding to REF PROC are of type pointer, so these should not count
for the count! */
/* Make sure we are accessing the real mode definition. */
while (EQUIVALENT (m) != NO_MOID)
m = EQUIVALENT (m);
*num_refs = 0;
*num_pointers = 0;
for (MOID_T *s = m; s != NO_MOID && IS_REF (s); s = SUB (s))
*num_refs += 1;
for (tree p = TREE_TYPE (exp);
p != NULL_TREE && POINTER_TYPE_P (p) && TREE_CODE (TREE_TYPE (p)) != FUNCTION_TYPE;
p = TREE_TYPE (p))
*num_pointers += 1;
gcc_assert (*num_refs >= *num_pointers);
}
/* The Algol 68 variable declaration
[LOC|HEAP] AMODE foo;
Is in principle equivalent to the identity declaration
REF AMODE foo = [LOC|HEAP] AMODE;
In both cases the object ascribed to the defining identifier `foo' is of
mode REF AMODE. The ascribed object is a name which is created by a
generator implied in the actual declarer in the first case, and an explicit
generator in the initialization expression in the second case.
However, this front-end implements these two cases differently in order to
reduce the amount of both indirect addressing and of storage:
- The variable declaration `[LOC|HEAP] AMODE foo;' lowers into a VAR_DECL
with type ATYPE provided that the generator is LOC and that it contains no
rows. Accessing it requires direct addressing. When its address is
required, an ADDR_EXPR shall be used.
- The identity declaration `REF AMODE foo = LOC AMODE;' lowers into a
VAR_DECL with type *ATYPE. Accessing it requires indirect addressing. It
is effectively a pointer.
This introduces the complication that an expression (the VAR_DECL) whose
type is TYPE can appear in a place where *TYPE is expected. This function,
given the required mode and an expression, adds as many ADDR_EXPR to EXPR as
necessary so the resulting value is of the required type. Other than this
nuisance, the parser guarantees that the entities have the right type at the
location they appear, so a call to a68_consolidate_ref is all must be needed
at any point in the lowering process to guarantee a valid value for the
context.
This function expects:
- That the type of EXPR is zero or more pointers to a base type BTYPE.
- That the mode M is zero or more REFs to a base non-ref mode AMODE.
- That the number of pointers in the type of EXPR is less or equal than the
number of REFs in the mode M.
- That BTYPE and AMODE are equivalent. */
tree
a68_consolidate_ref (MOID_T *m, tree expr)
{
int num_refs, num_pointers;
a68_ref_counts (expr, m, &num_refs, &num_pointers);
/* Address EXPR as many times as necessary to match the number of REFs in the
desired mode. */
while (num_pointers < num_refs)
{
if (TREE_CODE (expr) == COMPOUND_EXPR)
{
/* (..., x) -> (..., &x) */
// gcc_assert (TREE_CODE (TREE_OPERAND (expr, 0)) == MODIFY_EXPR);
// gcc_assert (VAR_P (TREE_OPERAND (expr, 1)));
TREE_OPERAND (expr, 1) = a68_consolidate_ref (m, TREE_OPERAND (expr, 1));
TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1));
}
else
{
/* x -> &x */
if (TREE_CODE (expr) == INDIRECT_REF)
/* expr is an indirection. Remove the pointer rather than adding
an addr. This avoids &* situations and marking stuff as
addressable unnecessarily. */
expr = TREE_OPERAND (expr,0);
else
{
TREE_ADDRESSABLE (expr) = true;
expr = fold_build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
}
}
num_pointers += 1;
}
return expr;
}
/* Make a declaration for an anonymous routine of mode MODE. */
tree
a68_make_anonymous_routine_decl (MOID_T *mode)
{
/* The CTYPE of MODE is a pointer to a function. We need the pointed
function type for the FUNCTION_DECL. */
tree func_type = TREE_TYPE (CTYPE (mode));
tree func_decl = build_decl (UNKNOWN_LOCATION,
FUNCTION_DECL,
NULL_TREE /* name, set below. */,
func_type);
char *name = xasprintf ("routine%d", DECL_UID (func_decl));
DECL_NAME (func_decl) = a68_get_mangled_identifier (name);
free (name);
DECL_EXTERNAL (func_decl) = 0;
DECL_STATIC_CHAIN (func_decl) = !a68_in_global_range ();
/* Nested functions should be addressable.
XXX this should be propagated to their containing functions, so for now
we mark them all as addressable. */
TREE_ADDRESSABLE (func_decl) = 1;
/* A nested function is not global. */
TREE_PUBLIC (func_decl) = a68_in_global_range ();
TREE_STATIC (func_decl) = 1;
return func_decl;
}
/* Make a declaration for a constant procedure or operator. */
tree
a68_make_proc_identity_declaration_decl (NODE_T *identifier,
const char *module_name,
bool indicant, bool external,
const char *extern_symbol)
{
/* The CTYPE of MODE is a pointer to a function. We need the pointed
function type for the FUNCTION_DECL. */
tree func_type = TREE_TYPE (CTYPE (MOID (identifier)));
bool public_range = PUBLIC_RANGE (TABLE (TAX (identifier)));
tree func_decl = build_decl (UNKNOWN_LOCATION,
FUNCTION_DECL,
NULL_TREE, /* name, set below. */
func_type);
if (public_range)
{
bool publicized = PUBLICIZED (TAX (identifier));
DECL_EXTERNAL (func_decl) = 0;
if (publicized)
TREE_PUBLIC (func_decl) = 1;
else
TREE_PUBLIC (func_decl) = 0;
if (indicant)
DECL_NAME (func_decl)
= a68_get_mangled_indicant (NSYMBOL (identifier), module_name,
false /* internal */,
(IS (identifier, DEFINING_OPERATOR)
|| IS (identifier, OPERATOR)));
else
DECL_NAME (func_decl)
= a68_get_mangled_identifier (NSYMBOL (identifier), module_name,
false /* internal */,
(IS (identifier, DEFINING_OPERATOR)
|| IS (identifier, OPERATOR)));
}
else if (external)
{
DECL_EXTERNAL (func_decl) = 1;
TREE_PUBLIC (func_decl) = 1;
DECL_NAME (func_decl) = get_identifier (extern_symbol);
}
else
{
DECL_EXTERNAL (func_decl) = 0;
DECL_STATIC_CHAIN (func_decl) = !a68_in_global_range ();
/* Nested functions should be addressable.
XXX this should be propagated to their containing functions, so for now
we mark them all as addressable. */
TREE_ADDRESSABLE (func_decl) = 1;
/* A nested function is not global. */
TREE_PUBLIC (func_decl) = a68_in_global_range ();
if (indicant)
DECL_NAME (func_decl) = a68_get_mangled_indicant (NSYMBOL (identifier));
else
DECL_NAME (func_decl) = a68_get_mangled_identifier (NSYMBOL (identifier));
}
TREE_STATIC (func_decl) = 1;
return func_decl;
}
/* Make a declaration for an identity declaration. */
tree
a68_make_identity_declaration_decl (NODE_T *identifier,
const char *module_name,
bool indicant, bool external,
const char *extern_symbol)
{
tree type = CTYPE (MOID (identifier));
bool public_range = PUBLIC_RANGE (TABLE (TAX (identifier)));
tree decl = build_decl (a68_get_node_location (identifier),
VAR_DECL,
NULL_TREE, /* name, set below. */
type);
if (public_range)
{
bool publicized = PUBLICIZED (TAX (identifier));
DECL_EXTERNAL (decl) = 0;
TREE_STATIC (decl) = 1;
if (publicized)
TREE_PUBLIC (decl) = 1;
else
TREE_PUBLIC (decl) = 0;
if (indicant)
DECL_NAME (decl) = a68_get_mangled_indicant (NSYMBOL (identifier), module_name,
false /* internal */,
(IS (identifier, DEFINING_OPERATOR)
|| IS (identifier, OPERATOR)));
else
DECL_NAME (decl) = a68_get_mangled_identifier (NSYMBOL (identifier), module_name,
false /* internal */,
(IS (identifier, DEFINING_OPERATOR)
|| IS (identifier, OPERATOR)));
}
else if (external)
{
DECL_EXTERNAL (decl) = 1;
TREE_PUBLIC (decl) = 1;
DECL_NAME (decl) = get_identifier (extern_symbol);
}
else
{
DECL_EXTERNAL (decl) = 0;
TREE_PUBLIC (decl) = 0;
DECL_NAME (decl) = a68_get_mangled_identifier (NSYMBOL (identifier));
}
DECL_INITIAL (decl) = a68_get_skip_tree (MOID (identifier));
return decl;
}
/* Make a declaration for a variable declaration.
The mode of the given identifier is expected to be a REF AMODE. */
tree
a68_make_variable_declaration_decl (NODE_T *identifier,
const char *module_name,
bool external,
const char *extern_symbol)
{
gcc_assert (IS_REF (MOID (identifier)));
MOID_T *mode = MOID (identifier);
bool use_pointer = (HEAP (TAX (identifier)) != STATIC_SYMBOL
&& ((HEAP (TAX (identifier)) == HEAP_SYMBOL)
|| HAS_ROWS (SUB (MOID (identifier)))));
bool public_range = PUBLIC_RANGE (TABLE (TAX (identifier)));
tree type = use_pointer ? CTYPE (mode) : CTYPE (SUB (mode));
tree decl = build_decl (a68_get_node_location (identifier),
VAR_DECL,
NULL_TREE, /* name, set below. */
type);
if (public_range)
{
bool publicized = PUBLICIZED (TAX (identifier));
DECL_EXTERNAL (decl) = 0;
TREE_STATIC (decl) = 1;
if (publicized)
TREE_PUBLIC (decl) = 1;
else
TREE_PUBLIC (decl) = 0;
DECL_NAME (decl) = a68_get_mangled_identifier (NSYMBOL (identifier), module_name,
false /* internal */,
(IS (identifier, DEFINING_OPERATOR)
|| IS (identifier, OPERATOR)));
}
else if (external)
{
DECL_EXTERNAL (decl) = 1;
TREE_PUBLIC (decl) = 1;
DECL_NAME (decl) = get_identifier (extern_symbol);
}
else
{
TREE_PUBLIC (decl) = 0;
DECL_NAME (decl) = a68_get_mangled_identifier (NSYMBOL (identifier));
}
DECL_INITIAL (decl) = a68_get_skip_tree (use_pointer ? mode : SUB (mode));
return decl;
}
/* Do a checked indirection.
P is a tree node used for its location information.
EXP is an expression that gets indirected.
EXP_MODE is the mode of exp. */
tree
a68_checked_indirect_ref (NODE_T *p, tree exp, MOID_T *exp_mode)
{
tree exp_type = TREE_TYPE (exp);
tree nil_check = NULL_TREE;
if (OPTION_NIL_CHECKING (&A68_JOB))
{
exp = save_expr (exp);
tree consolidated_exp = a68_consolidate_ref (exp_mode, exp);
/* Check whether we are dereferencing NIL. */
unsigned int lineno = NUMBER (LINE (INFO (p)));
const char *filename_str = FILENAME (LINE (INFO (p)));
tree filename = build_string_literal (strlen (filename_str) + 1,
filename_str);
tree call = a68_build_libcall (A68_LIBCALL_DEREFNIL,
void_type_node, 2,
filename,
build_int_cst (unsigned_type_node, lineno));
call = fold_build2 (COMPOUND_EXPR, a68_bool_type, call, boolean_false_node);
nil_check = fold_build2 (NE_EXPR, exp_type,
consolidated_exp,
build_int_cst (exp_type, 0));
nil_check = fold_build2 (TRUTH_ORIF_EXPR, exp_type,
nil_check, call);
}
tree deref = fold_build1 (INDIRECT_REF, TREE_TYPE (exp_type), exp);
if (nil_check == NULL_TREE)
return deref;
else
return fold_build2 (COMPOUND_EXPR, TREE_TYPE (deref),
nil_check, deref);
}
/* Deref a given expression EXP whose mode is MOID (P).
The value to dereference always corresponds to a name, but it may consist
of:
- Not a pointer, in which case corresponds to a name lowered to a VAR_DECL.
- A pointer to a function, in which case corresponds to a name of mode REF
PROC, lowered to a VAR_DECL.
- Any other pointer corresponds to a name lowered to a VAR_DECL that is a
pointer.
In the first two cases, in both r-value and l-value situations the expected
result is achieved by just returning the value: in r-value the decl denotes
the value, in l-value the decl denotes the (direct) address of the
value. */
tree
a68_low_deref (tree exp, NODE_T *p)
{
int num_refs, num_pointers;
a68_ref_counts (exp, MOID (p), &num_refs, &num_pointers);
if (num_refs > num_pointers)
return exp;
else
{
gcc_assert (num_refs == num_pointers);
return a68_checked_indirect_ref (p, exp, MOID (p));
}
}
/* Get a deep-copy of a given Algol 68 value EXP. */
tree
a68_low_dup (tree expr, bool use_heap)
{
tree dup = NULL_TREE;
tree type = TREE_TYPE (expr);
/* XXX */
use_heap = true;
/* Determine the mode corresponding to the type of EXPR. */
MOID_T *m = a68_type_moid (type);
gcc_assert (m != NO_MOID);
while (EQUIVALENT (m) != NO_MOID)
m = EQUIVALENT (m);
if (A68_ROW_TYPE_P (type))
{
/* We need to copy the elements as well as the descriptor. There is no
need to check bounds. */
/* Deflexe the mode as appropriate. */
while (IS_FLEX (m))
m = SUB (m);
gcc_assert (IS_ROW (m) || m == M_STRING);
a68_push_range (NULL);
/* First allocate space for the dupped elements. */
expr = save_expr (expr);
tree elements = a68_multiple_elements (expr);
tree element_pointer_type = TREE_TYPE (elements);
tree element_type = TREE_TYPE (element_pointer_type);
tree new_elements_size = save_expr (a68_multiple_elements_size (expr));
tree new_elements = a68_lower_tmpvar ("new_elements%",
TREE_TYPE (elements),
(use_heap
? a68_lower_malloc (TREE_TYPE (TREE_TYPE (elements)),
new_elements_size)
: a68_lower_alloca (TREE_TYPE (TREE_TYPE (elements)),
new_elements_size)));
/* Then copy the elements.
If the mode of the elements stored in the multiple dont have rows,
then we can just use memcpy. Otherwise, we have to loop and recurse
to dup all the elements in the multiple one by one.
The above applies to multiples of any number of dimensions. */
if (m == M_STRING || !HAS_ROWS (SUB (m)))
{
a68_add_stmt (a68_lower_memcpy (new_elements,
elements,
new_elements_size));
a68_add_stmt (new_elements);
}
else
{
/* Note that num_elems includes elements that are not accessible due
to trimming. */
tree num_elems = a68_lower_tmpvar ("numelems%", size_type_node,
fold_build2 (TRUNC_DIV_EXPR, sizetype,
new_elements_size,
size_in_bytes (element_type)));
tree orig_elements = a68_lower_tmpvar ("orig_elements%",
element_pointer_type, elements);
tree index = a68_lower_tmpvar ("index%", size_type_node, size_zero_node);
/* Begin of loop body. */
a68_push_range (NULL);
/* if (index == num_elems) break; */
a68_add_stmt (fold_build1 (EXIT_EXPR,
void_type_node,
fold_build2 (EQ_EXPR,
size_type_node,
index, num_elems)));
/* new_elements[index] = elements[index] */
tree offset = fold_build2 (MULT_EXPR, sizetype,
index, size_in_bytes (element_type));
tree new_elem_lvalue = fold_build2 (MEM_REF, element_type,
fold_build2 (POINTER_PLUS_EXPR,
element_pointer_type,
new_elements,
offset),
fold_convert (element_pointer_type,
integer_zero_node));
tree elem = fold_build2 (MEM_REF, element_type,
fold_build2 (POINTER_PLUS_EXPR,
element_pointer_type,
orig_elements,
offset),
fold_convert (element_pointer_type,
integer_zero_node));
a68_add_stmt (fold_build2 (MODIFY_EXPR, element_type,
new_elem_lvalue,
a68_low_dup (elem, use_heap)));
/* index++ */
a68_add_stmt (fold_build2 (POSTINCREMENT_EXPR,
size_type_node,
index, size_one_node));
tree loop_body = a68_pop_range ();
/* End of loop body. */
a68_add_stmt (fold_build1 (LOOP_EXPR,
void_type_node,
loop_body));
a68_add_stmt (new_elements);
}
new_elements = a68_pop_range ();
TREE_TYPE (new_elements) = element_pointer_type;
/* Now build a descriptor pointing to the dupped elements and return it.
Note that the descriptor is always allocated on the stack. */
dup = a68_row_value_raw (type,
a68_multiple_triplets (expr),
new_elements,
new_elements_size);
}
else if (!HAS_ROWS (m))
{
/* Non-multiple values that do not contain rows do not need to be dupped,
since they can be just moved around using the semantics of
MODIFY_EXPR. */
dup = expr;
}
else if (A68_STRUCT_TYPE_P (type))
{
/* Since struct value can contain multiples and unions and other values
that require deep copy, we cannot simply rely on the C semantics of a
MODIFY_EXPR. */
tree struct_type = type;
vec <constructor_elt, va_gc> *ce = NULL;
expr = save_expr (expr);
for (tree field = TYPE_FIELDS (struct_type);
field;
field = TREE_CHAIN (field))
{
CONSTRUCTOR_APPEND_ELT (ce, field,
a68_low_dup (fold_build3 (COMPONENT_REF,
TREE_TYPE (field),
expr,
field,
NULL_TREE),
use_heap));
}
dup = build_constructor (struct_type, ce);
}
else if (A68_UNION_TYPE_P (type))
{
/* We need to recurse in whatever type corresponding to the active mode
in the united value. This shall be done at run-time by using a series
of
IF overhead IS index of mode blah in union
THEN dup = dup_type (CTYPE (mode blah in union))
FI
*/
MOID_T *union_mode = a68_type_moid (type);
a68_push_range (union_mode);
dup = a68_lower_tmpvar ("dup%", type, expr);
tree cunion_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type)));
tree field_decl = TYPE_FIELDS (cunion_type);
while (EQUIVALENT (union_mode) != NO_MOID)
union_mode = EQUIVALENT (union_mode);
for (PACK_T *pack = PACK (union_mode); pack != NO_PACK; FORWARD (pack))
{
tree continue_label_decl = build_decl (UNKNOWN_LOCATION,
LABEL_DECL,
NULL, /* Set below. */
void_type_node);
char *label_name = xasprintf ("continue%d%%", DECL_UID (continue_label_decl));
DECL_NAME (continue_label_decl) = get_identifier (label_name);
free (label_name);
a68_add_decl (continue_label_decl);
a68_add_stmt (fold_build2 (TRUTH_ORIF_EXPR,
integer_type_node,
fold_build2 (EQ_EXPR,
integer_type_node,
a68_union_overhead (dup),
size_int (a68_united_mode_index (union_mode, MOID (pack)))),
fold_build2 (COMPOUND_EXPR,
integer_type_node,
build1 (GOTO_EXPR, void_type_node, continue_label_decl),
integer_zero_node)));
a68_add_stmt (fold_build2 (MODIFY_EXPR, type,
fold_build3 (COMPONENT_REF,
TREE_TYPE (field_decl),
a68_union_cunion (dup),
field_decl,
NULL_TREE),
a68_low_dup (fold_build3 (COMPONENT_REF,
TREE_TYPE (field_decl),
a68_union_cunion (dup),
field_decl,
NULL_TREE),
use_heap)));
a68_add_stmt (build1 (LABEL_EXPR, void_type_node, continue_label_decl));
field_decl = TREE_CHAIN (field_decl);
}
a68_add_stmt (dup);
dup = a68_pop_range ();
}
else
/* Not an Algol 68 value. */
gcc_unreachable ();
return dup;
}
/* Lower code to ascribe the value yielded by the expression in RHS to the
defining identifier implied by the LHS, which is a VAR_DECL tree. MODE is
the mode of the value to be ascribed. */
tree
a68_low_ascription (MOID_T *mode, tree lhs, tree rhs)
{
gcc_assert (VAR_P (lhs));
tree type = CTYPE (mode);
if (IS (mode, PROC_SYMBOL))
{
/* A pointer to a function, or a function, is expected at the right hand
side. We need a pointer for the left hand side.. */
if (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
{
type = build_pointer_type (type);
rhs = fold_build1 (ADDR_EXPR, type, rhs);
}
}
if (HAS_ROWS (mode))
rhs = a68_low_dup (rhs);
return fold_build2 (MODIFY_EXPR, type, lhs, rhs);
}
/* Perform an assignation of RHS to LHS.
MODE_RHS is the mode of the rhs.
MODE_LHS is the mode of the lhs.
MODE_LHS shall be REF [FLEX] MODE_LHS. */
tree
a68_low_assignation (NODE_T *p,
tree lhs, MOID_T *mode_lhs,
tree rhs, MOID_T *mode_rhs)
{
NODE_T *lhs_node = SUB (p);
tree assignation = NULL_TREE;
tree orig_rhs = rhs;
if (IS_FLEXETY_ROW (mode_rhs))
{
/* Make a deep copy of the rhs. Note that we have to use the heap
because the scope of the lhs may be older than the scope of the rhs.
XXX this can be ommitted if a68_multiple_copy_elems below supports
overlapping multiples. */
if (HAS_ROWS (mode_rhs))
rhs = a68_low_dup (rhs, true /* use_heap */);
rhs = save_expr (rhs);
/* Determine whether the REF [FLEX] MODE_LHS is flexible. */
if (SUB (mode_lhs) == M_STRING || IS_FLEX (SUB (mode_lhs)))
{
/* Assigning to a flexible name updates descriptor with new bounds
and also sets the elements to the dup of the rhs. No boundscheck
is peformed. XXX but bound checking in contained values may be
necessary, ghost elements. */
if (POINTER_TYPE_P (TREE_TYPE (lhs))
&& TREE_TYPE (TREE_TYPE (lhs)) == TREE_TYPE (rhs))
{
/* Make sure to not evaluate the expression yielding the pointer
more than once. */
lhs = save_expr (lhs);
tree deref_lhs = a68_checked_indirect_ref (lhs_node, lhs, mode_lhs);
assignation = fold_build2 (COMPOUND_EXPR,
TREE_TYPE (lhs),
fold_build2 (MODIFY_EXPR, TREE_TYPE (lhs),
deref_lhs, rhs),
lhs);
}
else
{
/* The lhs is either a variable or a component ref as a l-value. It
is ok to evaluate it as an r-value as well as doing so inroduces
no side-effects. */
assignation = fold_build2 (COMPOUND_EXPR,
TREE_TYPE (lhs),
fold_build2 (MODIFY_EXPR, TREE_TYPE (lhs),
lhs, rhs),
lhs);
}
}
else
{
/* Dereference the multiple at the left-hand side. This may require
indirection. */
tree effective_lhs;
if (POINTER_TYPE_P (TREE_TYPE (lhs)))
{
/* The name at the lhs is a pointer. */
gcc_assert (TREE_TYPE (TREE_TYPE (lhs)) == TREE_TYPE (rhs));
lhs = save_expr (lhs);
effective_lhs = a68_checked_indirect_ref (lhs_node, lhs, mode_lhs);
}
else
{
/* The name at the lhs is either a variable or a component ref as
a l-value. It is ok to evaluate it as an r-value as well as
doing so introduces no side-effects. */
effective_lhs = lhs;
}
/* Copy over the elements in a loop. The space occupied by the
previous elements stored in the lhs multiple will be recovered by
either stack shrinkage or garbage collected. */
tree copy_elements = a68_multiple_copy_elems (mode_rhs, effective_lhs, rhs);
assignation = fold_build2 (COMPOUND_EXPR,
TREE_TYPE (lhs),
copy_elements,
lhs);
/* Check the bounds of the multiple at the rhs to make sure they are
the same than the bounds of the multiple already referred by the
lhs. If the bounds don't match then emit a run-time error. */
if (OPTION_BOUNDS_CHECKING (&A68_JOB))
assignation = fold_build2 (COMPOUND_EXPR,
TREE_TYPE (assignation),
a68_multiple_bounds_check_equal (p,
effective_lhs,
rhs),
assignation);
}
}
else
{
/* First make sure we got a pointer in the RHS in case it is a name. */
rhs = a68_consolidate_ref (mode_rhs, rhs);
/* The assignation implies copying the entire value being assigned, so
make sure we do a deep copy whenever needed. Note that we have to use
the heap because the scope of the lhs may be older than the scope of
the rhs. */
if (HAS_ROWS (mode_rhs))
rhs = a68_low_dup (rhs, true /* use_heap */);
if (POINTER_TYPE_P (TREE_TYPE (lhs))
&& TREE_TYPE (TREE_TYPE (lhs)) == TREE_TYPE (rhs))
{
/* If the left hand side is a pointer, deref it, but return the
pointer. Make sure to not evaluate the expression yielding the
pointer more than once. */
lhs = save_expr (lhs);
tree deref_lhs = a68_checked_indirect_ref (lhs_node, lhs, mode_lhs);
assignation = fold_build2 (COMPOUND_EXPR,
TREE_TYPE (lhs),
fold_build2 (MODIFY_EXPR, TREE_TYPE (lhs),
deref_lhs, rhs),
lhs);
}
else
{
/* Otherwise the lhs is either a variable or a component ref as an
l-value. It is ok to evaluate it as an r-value as well as doing
so introduces no side-effects. */
assignation = fold_build2 (COMPOUND_EXPR,
TREE_TYPE (lhs),
fold_build2 (MODIFY_EXPR, TREE_TYPE (lhs),
lhs, rhs),
lhs);
}
}
/* Since it is been assigned to a name, the rhs is no longer constant. */
if (A68_ROW_TYPE_P (TREE_TYPE (orig_rhs)) || A68_STRUCT_TYPE_P (TREE_TYPE (orig_rhs)))
TREE_CONSTANT (orig_rhs) = 0;
return assignation;
}
/* Build a tree that copies SIZE bytes from SRC into DST. */
tree
a68_lower_memcpy (tree dst, tree src, tree size)
{
return build_call_expr (builtin_decl_explicit (BUILT_IN_MEMCPY), 3,
dst, src, size);
}
/* Build a tree that allocates SIZE bytes on the stack and returns a *TYPE
pointer to it. */
tree
a68_lower_alloca (tree type, tree size)
{
tree call = builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN);
call = build_call_expr_loc (UNKNOWN_LOCATION, call, 2,
size,
size_int (TYPE_ALIGN (type)));
call = fold_convert (build_pointer_type (type), call);
return call;
}
/* Build a tree that allocates SIZE bytes on the heap and returns a *TYPE
pointer to it. */
tree
a68_lower_malloc (tree type, tree size)
{
return fold_convert (build_pointer_type (type),
a68_build_libcall (A68_LIBCALL_MALLOC, ptr_type_node,
1, size));
}
/* Build code for a temporary variable named NAME, of type TYPE and initialized
to INIT. Returns the decl node for the temporary. */
tree
a68_lower_tmpvar (const char *name, tree type, tree init)
{
tree tmpvar = build_decl (UNKNOWN_LOCATION,
VAR_DECL,
get_identifier (name),
type);
DECL_ARTIFICIAL (tmpvar) = 1;
DECL_IGNORED_P (tmpvar) = 1;
a68_add_decl (tmpvar);
a68_add_decl_expr (fold_build1 (DECL_EXPR, type, tmpvar));
a68_add_stmt (fold_build2 (INIT_EXPR, type, tmpvar, init));
return tmpvar;
}
/* Build a FUNC_DECL for a top-level non-public function and return it. */
tree
a68_low_toplevel_func_decl (const char *name, tree fntype)
{
tree fndecl = build_decl (UNKNOWN_LOCATION,
FUNCTION_DECL,
NULL /* set below */,
fntype);
char *_name = xasprintf ("__ga68_%s%d", name, DECL_UID (fndecl));
DECL_NAME (fndecl) = get_identifier (_name);
free (_name);
DECL_EXTERNAL (fndecl) = 0;
TREE_PUBLIC (fndecl) = 0;
TREE_STATIC (fndecl) = 1;
return fndecl;
}
/* Build a PARM_DECL whose context is TYPE with the given NAME. */
tree
a68_low_func_param (tree fndecl, const char *name, tree type)
{
tree param = build_decl (UNKNOWN_LOCATION, PARM_DECL,
get_identifier (name), type);
DECL_CONTEXT (param) = fndecl;
DECL_ARG_TYPE (param) = TREE_TYPE (param);
layout_decl (param, 0);
return param;
}
/* Lower revelations, to calls to either its prelude or poslude.
This function always returns NULL_TREE. */
static tree
lower_revelations (NODE_T *p, LOW_CTX_T ctx, bool prelude)
{
for (; p != NO_NODE; FORWARD (p))
{
lower_revelations (SUB (p), ctx, prelude);
if (IS (p, MODULE_INDICANT))
{
tree decl = build_decl (a68_get_node_location (p),
FUNCTION_DECL,
NULL_TREE, /* name, set below. */
build_function_type (void_type_node,
void_list_node));
DECL_NAME (decl)
= a68_get_mangled_identifier (prelude ? "_prelude" : "_postlude",
NSYMBOL (p));
DECL_EXTERNAL (decl) = 1;
TREE_PUBLIC (decl) = 1;
a68_add_decl (decl);
a68_add_stmt (build_call_expr_loc (a68_get_node_location (p),
decl, 0));
}
}
return NULL_TREE;
}
/* Lower a module text.
module text : revelation part, def part, postlude part, fed symbol ;
revelation part, def part, fed symbol ;
def part, postlude part, fed symbol ;
def part, postlude part, fed symbol.
def part : def symbol, enquiry clause.
postlude part : postlude symbol, serial clause.
Each module text lowers to two functions which are callable from outside the
current compilation unit:
MODULENAME__prelude
MODULENAME__postlude
Declarations inside prelude and postlude are lowered into global decl trees.
The non-PUBlicized ones are marked as static.
This handler always returns NULL_TREE. */
static tree
lower_module_text (NODE_T *p, LOW_CTX_T ctx)
{
NODE_T *def_part = (IS (SUB (p), REVELATION_PART)
? NEXT_SUB (p)
: SUB (p));
NODE_T *revelation_part = (IS (SUB (p), REVELATION_PART)
? SUB (p)
: NO_NODE);
NODE_T *postlude_part = (IS (NEXT (def_part), FED_SYMBOL)
? NO_NODE
: NEXT (def_part));
NODE_T *prelude_enquiry = NEXT_SUB (def_part);
/* The global sentinel of the module, initialized to 0. */
tree sentinel_decl = build_decl (UNKNOWN_LOCATION,
VAR_DECL, NULL /* name */,
sizetype);
char *sentinel_name = xasprintf ("%s__sentinel", ctx.module_definition_name);
DECL_NAME (sentinel_decl) = get_identifier (sentinel_name);
free (sentinel_name);
TREE_PUBLIC (sentinel_decl) = 0;
TREE_STATIC (sentinel_decl) = 1;
DECL_CONTEXT (sentinel_decl) = NULL_TREE; /* File scope. */
make_decl_rtl (sentinel_decl);
varpool_node::finalize_decl (sentinel_decl);
/* Create the prelude function. */
tree prelude_decl = build_decl (a68_get_node_location (def_part),
FUNCTION_DECL,
NULL_TREE, /* name, set below. */
build_function_type (void_type_node,
void_list_node));
DECL_NAME (prelude_decl)
= a68_get_mangled_identifier ("_prelude",
ctx.module_definition_name);
DECL_EXTERNAL (prelude_decl) = 0;
TREE_PUBLIC (prelude_decl) = 1;
TREE_STATIC (prelude_decl) = 1;
a68_push_function_range (prelude_decl,
void_type_node /* result_type */, true /* top_level */);
{
/* Increase sentinel. */
a68_add_stmt (fold_build2 (POSTINCREMENT_EXPR,
sizetype,
sentinel_decl, size_one_node));
a68_push_stmt_list (NULL);
{
a68_push_stmt_list (NULL);
{
/* Add calls to preludes of modules in REVELATION_PART. */
lower_revelations (revelation_part, ctx, true /* prelude */);
a68_add_stmt (a68_lower_tree (prelude_enquiry, ctx));
}
tree do_prelude = a68_pop_stmt_list ();
a68_push_stmt_list (M_VOID);
tree do_nothing = a68_pop_stmt_list ();
/* Do the prelude work only if sentinel is 1. */
a68_add_stmt (fold_build3 (COND_EXPR, void_type_node,
fold_build2 (EQ_EXPR, sizetype,
sentinel_decl, size_one_node),
do_prelude, do_nothing));
}
tree prelude_body = a68_pop_stmt_list ();
a68_pop_function_range (prelude_body);
}
/* Create the postlude function. This is done even if the module definition
has no postlude in the source code. */
location_t postlude_loc = UNKNOWN_LOCATION;
if (postlude_part != NO_NODE)
postlude_loc = a68_get_node_location (postlude_part);
tree postlude_decl = build_decl (postlude_loc,
FUNCTION_DECL,
NULL_TREE, /* name, set below. */
build_function_type (void_type_node,
void_list_node));
DECL_NAME (postlude_decl)
= a68_get_mangled_identifier ("_postlude",
ctx.module_definition_name);
DECL_EXTERNAL (postlude_decl) = 0;
TREE_PUBLIC (postlude_decl) = 1;
TREE_STATIC (postlude_decl) = 1;
a68_push_function_range (postlude_decl,
void_type_node /* result_type */, true /* top_level */);
{
/* Decrease sentinel. */
a68_add_stmt (fold_build2 (POSTINCREMENT_EXPR,
sizetype,
sentinel_decl, size_one_node));
a68_push_stmt_list (NULL);
{
a68_push_stmt_list (NULL);
{
/* Add calls to postludes of modules in REVELATION_PART. */
lower_revelations (revelation_part, ctx, false /* prelude */);
/* Perhaps the postlude code, if there is one. */
NODE_T *postlude_serial = NO_NODE;
if (postlude_part != NO_NODE)
postlude_serial = NEXT_SUB (postlude_part);
if (postlude_serial != NO_NODE)
a68_add_stmt (a68_lower_tree (postlude_serial, ctx));
}
tree do_postlude = a68_pop_stmt_list ();
a68_push_stmt_list (M_VOID);
tree do_nothing = a68_pop_stmt_list ();
/* Do the postlude work only if sentinel is 0. */
a68_add_stmt (fold_build3 (COND_EXPR, void_type_node,
fold_build2 (EQ_EXPR, sizetype,
sentinel_decl, size_zero_node),
do_postlude, do_nothing));
}
tree postlude_body = a68_pop_stmt_list ();
a68_pop_function_range (postlude_body);
}
return NULL_TREE;
}
/* Lower a set of module declarations.
module declaration : module symbol, defining module, equals symbol, module text ;
module_declaration, comma symbol, defining module,
equals symbol, module text.
Each module declaration lowers into the side-effects of emitting prelude and
postlude global functions, and emitting global declarations for the
declarations in the module definition prelude.
This handler always returns NULL_TREE. */
static tree
lower_module_declaration (NODE_T *p, LOW_CTX_T ctx)
{
for (; p != NO_NODE; FORWARD (p))
{
if (IS (p, DEFINING_MODULE_INDICANT))
{
ctx.module_definition_name = NSYMBOL (p);
A68_MODULE_DEFINITION_DECLS->truncate (0);
vec_alloc (A68_MODULE_DEFINITION_DECLS, 16);
lower_module_text (NEXT (NEXT (p)), ctx);
for (tree d : A68_MODULE_DEFINITION_DECLS)
{
if (TREE_CODE (d) == FUNCTION_DECL)
cgraph_node::finalize_function (d, true);
else
{
rest_of_decl_compilation (d, 1, 0);
make_decl_rtl (d);
}
}
}
else
lower_module_declaration (SUB (p), ctx);
}
return NULL_TREE;
}
/* Lower a prelude packet.
prelude packet : module declaration.
This handler always returns NULL_TREE. */
static tree
lower_prelude_packet (NODE_T *p, LOW_CTX_T ctx)
{
a68_lower_tree (SUB (p), ctx);
return NULL_TREE;
}
/* Lower a particular program.
particular program : label, enclosed clause; enclosed clause.
This handler always returns NULL_TREE. */
static tree
lower_particular_program (NODE_T *p, LOW_CTX_T ctx)
{
/* Create the main function that conforms the particular program. */
tree main_decl = build_decl (a68_get_node_location (p),
FUNCTION_DECL,
get_identifier ("__algol68_main"),
build_function_type (void_type_node,
void_list_node));
DECL_EXTERNAL (main_decl) = 0;
TREE_PUBLIC (main_decl) = 1;
TREE_STATIC (main_decl) = 1;
a68_push_function_range (main_decl,
void_type_node /* result_type */);
/* Lower the body of the function. */
NODE_T *enclosed_clause = (IS (SUB (p), ENCLOSED_CLAUSE)
? SUB (p) : NEXT (SUB (p)));
tree body_expr = a68_lower_tree (enclosed_clause, ctx);
a68_pop_function_range (body_expr);
return NULL_TREE;
}
/* Lower a packet.
packet : particular program ; prelude packet.
*/
static tree
lower_packet (NODE_T *p,
LOW_CTX_T ctx)
{
return a68_lower_tree (SUB (p), ctx);
}
/* Lower the given tree P using the given context CTX. */
tree
a68_lower_tree (NODE_T *p, LOW_CTX_T ctx)
{
#if 0
for (int i = 0; i < ctx.level; ++i)
printf (" ");
printf ("LOWER TREE: %d::%s\n",
NUMBER (p), a68_attribute_name (ATTRIBUTE (p)));
#endif
ctx.level++;
tree res = NULL_TREE;
if (p == NO_NODE)
gcc_unreachable ();
switch (ATTRIBUTE (p))
{
case PACKET:
res = lower_packet (p, ctx);
break;
case PRELUDE_PACKET:
res = lower_prelude_packet (p, ctx);
break;
case MODULE_DECLARATION:
res = lower_module_declaration (p, ctx);
break;
case MODULE_TEXT:
res = lower_module_text (p, ctx);
break;
case PARTICULAR_PROGRAM:
res = lower_particular_program (p, ctx);
break;
/* Clauses */
case ENCLOSED_CLAUSE:
res = a68_lower_enclosed_clause (p, ctx);
break;
case CLOSED_CLAUSE:
res = a68_lower_closed_clause (p, ctx);
break;
case ACCESS_CLAUSE:
res = a68_lower_access_clause (p, ctx);
break;
case PARALLEL_CLAUSE:
res = a68_lower_parallel_clause (p, ctx);
break;
case COLLATERAL_CLAUSE:
res = a68_lower_collateral_clause (p, ctx);
break;
case UNIT_LIST:
res = a68_lower_unit_list (p, ctx);
break;
case CONDITIONAL_CLAUSE:
res = a68_lower_conditional_clause (p, ctx);
break;
case ENQUIRY_CLAUSE:
res = a68_lower_enquiry_clause (p, ctx);
break;
case CASE_CLAUSE:
res = a68_lower_case_clause (p, ctx);
break;
case CONFORMITY_CLAUSE:
res = a68_lower_conformity_clause (p, ctx);
break;
case LOOP_CLAUSE:
res = a68_lower_loop_clause (p, ctx);
break;
case SERIAL_CLAUSE:
res = a68_lower_serial_clause (p, ctx);
break;
case INITIALISER_SERIES:
res = a68_lower_initialiser_series (p, ctx);
break;
case EXIT_SYMBOL:
res = a68_lower_completer (p, ctx);
break;
case LABELED_UNIT:
res = a68_lower_labeled_unit (p, ctx);
break;
case LABEL:
res = a68_lower_label (p, ctx);
break;
/* Declarations. */
case DECLARATION_LIST:
res = a68_lower_declaration_list (p, ctx);
break;
case DECLARER:
res = a68_lower_declarer (p, ctx);
break;
case IDENTITY_DECLARATION:
res = a68_lower_identity_declaration (p, ctx);
break;
case VARIABLE_DECLARATION:
res = a68_lower_variable_declaration (p, ctx);
break;
case PROCEDURE_DECLARATION:
res = a68_lower_procedure_declaration (p, ctx);
break;
case PROCEDURE_VARIABLE_DECLARATION:
res = a68_lower_procedure_variable_declaration (p, ctx);
break;
case PRIORITY_DECLARATION:
res = a68_lower_priority_declaration (p, ctx);
break;
case BRIEF_OPERATOR_DECLARATION:
res = a68_lower_brief_operator_declaration (p, ctx);
break;
case OPERATOR_DECLARATION:
res = a68_lower_operator_declaration (p, ctx);
break;
case MODE_DECLARATION:
res = a68_lower_mode_declaration (p, ctx);
break;
/* Units. */
case UNIT:
res = a68_lower_unit (p, ctx);
break;
case ROUTINE_TEXT:
res = a68_lower_routine_text (p, ctx);
break;
case ASSIGNATION:
res = a68_lower_assignation (p, ctx);
break;
case TERTIARY:
res = a68_lower_tertiary (p, ctx);
break;
case MONADIC_FORMULA:
res = a68_lower_monadic_formula (p, ctx);
break;
case FORMULA:
res = a68_lower_formula (p, ctx);
break;
case SECONDARY:
res = a68_lower_secondary (p, ctx);
break;
case SLICE:
res = a68_lower_slice (p, ctx);
break;
case SELECTION:
res = a68_lower_selection (p, ctx);
break;
case PRIMARY:
res = a68_lower_primary (p, ctx);
break;
case GENERATOR:
res = a68_lower_generator (p, ctx);
break;
case CALL:
res = a68_lower_call (p, ctx);
break;
case CAST:
res = a68_lower_cast (p, ctx);
break;
case AND_FUNCTION:
case OR_FUNCTION:
res = a68_lower_logic_function (p, ctx);
break;
case IDENTITY_RELATION:
res = a68_lower_identity_relation (p, ctx);
break;
case EMPTY_SYMBOL:
res = a68_lower_empty (p, ctx);
break;
case NIHIL:
res = a68_lower_nihil (p, ctx);
break;
case SKIP:
res = a68_lower_skip (p, ctx);
break;
case DENOTATION:
res = a68_lower_denotation (p, ctx);
break;
case IDENTIFIER:
res = a68_lower_identifier (p, ctx);
break;
/* Coercions. */
case ROWING:
res = a68_lower_rowing (p, ctx);
break;
case WIDENING:
res = a68_lower_widening (p, ctx);
break;
case DEPROCEDURING:
res = a68_lower_deproceduring (p, ctx);
break;
case PROCEDURING:
res = a68_lower_proceduring (p, ctx);
break;
case VOIDING:
res = a68_lower_voiding (p, ctx);
break;
case DEREFERENCING:
res = a68_lower_dereferencing (p, ctx);
break;
/* Others. */
case UNITING:
res = a68_lower_uniting (p, ctx);
break;
case JUMP:
res = a68_lower_jump (p, ctx);
break;
case PARAMETER:
res = a68_lower_parameter (p, ctx);
break;
case PARAMETER_LIST:
res = a68_lower_parameter_list (p, ctx);
break;
case PARAMETER_PACK:
res = a68_lower_parameter_pack (p, ctx);
break;
case OPERATOR:
res = a68_lower_operator (p, ctx);
break;
case ASSERTION:
res = a68_lower_assertion (p, ctx);
break;
case STOP:
res = NULL_TREE;
break;
default:
fatal_error (a68_get_node_location (p), "cannot lower node %s",
a68_attribute_name (ATTRIBUTE (p)));
gcc_unreachable ();
break;
}
return res;
}
/* Lower an Algol 68 complete parse tree to a GENERIC tree. */
tree
a68_lower_top_tree (NODE_T *p)
{
LOW_CTX_T top_ctx;
top_ctx.declarer = NULL;
top_ctx.proc_decl_identifier = NO_NODE;
top_ctx.proc_decl_operator = false;
top_ctx.level = 0;
top_ctx.module_definition_name = NULL;
vec_alloc (A68_MODULE_DEFINITION_DECLS, 16);
return a68_lower_tree (p, top_ctx);
}
|