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
|
-- Mcode back-end for ortho - Expressions and control handling.
-- Copyright (C) 2006 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.Text_IO;
with Ada.Unchecked_Deallocation;
with GNAT.Table;
with Ortho_Code.Types; use Ortho_Code.Types;
with Ortho_Code.Consts; use Ortho_Code.Consts;
with Ortho_Code.Decls; use Ortho_Code.Decls;
with Ortho_Code.Debug; use Ortho_Code.Debug;
with Ortho_Code.Abi; use Ortho_Code.Abi;
with Ortho_Code.Disps;
with Ortho_Code.Opts;
with Ortho_Code.Flags;
package body Ortho_Code.Exprs is
type Enode_Pad is mod 256;
type Enode_Common is record
Kind : OE_Kind; -- about 1 byte (6 bits)
Reg : O_Reg; -- 1 byte
Mode : Mode_Type; -- 4 bits
Ref : Boolean;
Flag1 : Boolean;
Flag2 : Boolean;
Flag3 : Boolean;
Pad : Enode_Pad;
Arg1 : O_Enode;
Arg2 : O_Enode;
Info : Int32;
end record;
pragma Pack (Enode_Common);
for Enode_Common'Size use 4*32;
for Enode_Common'Alignment use 4;
package Enodes is new GNAT.Table
(Table_Component_Type => Enode_Common,
Table_Index_Type => O_Enode,
Table_Low_Bound => 2,
Table_Initial => 1024,
Table_Increment => 100);
function Get_Expr_Kind (Enode : O_Enode) return OE_Kind is
begin
return Enodes.Table (Enode).Kind;
end Get_Expr_Kind;
function Get_Expr_Mode (Enode : O_Enode) return Mode_Type is
begin
return Enodes.Table (Enode).Mode;
end Get_Expr_Mode;
function Get_Enode_Type (Enode : O_Enode) return O_Tnode is
begin
return O_Tnode (Enodes.Table (Enode).Info);
end Get_Enode_Type;
function Get_Expr_Reg (Enode : O_Enode) return O_Reg is
begin
return Enodes.Table (Enode).Reg;
end Get_Expr_Reg;
procedure Set_Expr_Reg (Enode : O_Enode; Reg : O_Reg) is
begin
Enodes.Table (Enode).Reg := Reg;
end Set_Expr_Reg;
function Get_Expr_Operand (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg1;
end Get_Expr_Operand;
procedure Set_Expr_Operand (Enode : O_Enode; Val : O_Enode) is
begin
Enodes.Table (Enode).Arg1 := Val;
end Set_Expr_Operand;
function Get_Expr_Left (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg1;
end Get_Expr_Left;
function Get_Expr_Right (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg2;
end Get_Expr_Right;
procedure Set_Expr_Left (Enode : O_Enode; Val : O_Enode) is
begin
Enodes.Table (Enode).Arg1 := Val;
end Set_Expr_Left;
procedure Set_Expr_Right (Enode : O_Enode; Val : O_Enode) is
begin
Enodes.Table (Enode).Arg2 := Val;
end Set_Expr_Right;
function Get_Expr_Low (Cst : O_Enode) return Uns32 is
begin
return To_Uns32 (Int32 (Enodes.Table (Cst).Arg1));
end Get_Expr_Low;
function Get_Expr_High (Cst : O_Enode) return Uns32 is
begin
return To_Uns32 (Int32 (Enodes.Table (Cst).Arg2));
end Get_Expr_High;
function Get_Assign_Target (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg2;
end Get_Assign_Target;
procedure Set_Assign_Target (Enode : O_Enode; Targ : O_Enode) is
begin
Enodes.Table (Enode).Arg2 := Targ;
end Set_Assign_Target;
function Get_Expr_Lit (Lit : O_Enode) return O_Cnode is
begin
return O_Cnode (Enodes.Table (Lit).Arg1);
end Get_Expr_Lit;
function Get_Conv_Type (Enode : O_Enode) return O_Tnode is
begin
return O_Tnode (Enodes.Table (Enode).Arg2);
end Get_Conv_Type;
-- Leave node corresponding to the entry.
function Get_Entry_Leave (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg1;
end Get_Entry_Leave;
procedure Set_Entry_Leave (Enode : O_Enode; Leave : O_Enode) is
begin
Enodes.Table (Enode).Arg1 := Leave;
end Set_Entry_Leave;
function Get_Jump_Label (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg2;
end Get_Jump_Label;
procedure Set_Jump_Label (Enode : O_Enode; Label : O_Enode) is
begin
Enodes.Table (Enode).Arg2 := Label;
end Set_Jump_Label;
function Get_Addr_Object (Enode : O_Enode) return O_Dnode is
begin
return O_Dnode (Enodes.Table (Enode).Arg1);
end Get_Addr_Object;
function Get_Addrl_Frame (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg2;
end Get_Addrl_Frame;
procedure Set_Addrl_Frame (Enode : O_Enode; Frame : O_Enode) is
begin
Enodes.Table (Enode).Arg2 := Frame;
end Set_Addrl_Frame;
function Get_Call_Subprg (Enode : O_Enode) return O_Dnode is
begin
return O_Dnode (Enodes.Table (Enode).Arg1);
end Get_Call_Subprg;
function Get_Arg_Link (Enode : O_Enode) return O_Enode is
begin
return Enodes.Table (Enode).Arg2;
end Get_Arg_Link;
function Get_Block_Decls (Blk : O_Enode) return O_Dnode is
begin
return O_Dnode (Enodes.Table (Blk).Arg2);
end Get_Block_Decls;
function Get_Block_Parent (Blk : O_Enode) return O_Enode is
begin
return Enodes.Table (Blk).Arg1;
end Get_Block_Parent;
function Get_Block_Has_Alloca (Blk : O_Enode) return Boolean is
begin
return Enodes.Table (Blk).Flag1;
end Get_Block_Has_Alloca;
procedure Set_Block_Has_Alloca (Blk : O_Enode; Flag : Boolean) is
begin
Enodes.Table (Blk).Flag1 := Flag;
end Set_Block_Has_Alloca;
function Get_End_Beg (Blk : O_Enode) return O_Enode is
begin
return Enodes.Table (Blk).Arg1;
end Get_End_Beg;
function Get_Label_Info (Label : O_Enode) return Int32 is
begin
return Int32 (Enodes.Table (Label).Arg2);
end Get_Label_Info;
procedure Set_Label_Info (Label : O_Enode; Info : Int32) is
begin
Enodes.Table (Label).Arg2 := O_Enode (Info);
end Set_Label_Info;
function Get_Label_Block (Label : O_Enode) return O_Enode is
begin
return Enodes.Table (Label).Arg1;
end Get_Label_Block;
function Get_Spill_Info (Spill : O_Enode) return Int32 is
begin
return Int32 (Enodes.Table (Spill).Arg2);
end Get_Spill_Info;
procedure Set_Spill_Info (Spill : O_Enode; Info : Int32) is
begin
Enodes.Table (Spill).Arg2 := O_Enode (Info);
end Set_Spill_Info;
-- Get the statement link.
function Get_Stmt_Link (Stmt : O_Enode) return O_Enode is
begin
return O_Enode (Enodes.Table (Stmt).Info);
end Get_Stmt_Link;
procedure Set_Stmt_Link (Stmt : O_Enode; Next : O_Enode) is
begin
Enodes.Table (Stmt).Info := Int32 (Next);
end Set_Stmt_Link;
function Get_BB_Next (Stmt : O_Enode) return O_Enode is
begin
return Enodes.Table (Stmt).Arg1;
end Get_BB_Next;
pragma Unreferenced (Get_BB_Next);
procedure Set_BB_Next (Stmt : O_Enode; Next : O_Enode) is
begin
Enodes.Table (Stmt).Arg1 := Next;
end Set_BB_Next;
function Get_BB_Number (Stmt : O_Enode) return Int32 is
begin
return Int32 (Enodes.Table (Stmt).Arg2);
end Get_BB_Number;
function Get_Loop_Level (Stmt : O_Enode) return Int32 is
begin
return Int32 (Enodes.Table (Stmt).Arg1);
end Get_Loop_Level;
procedure Set_Loop_Level (Stmt : O_Enode; Level : Int32) is
begin
Enodes.Table (Stmt).Arg1 := O_Enode (Level);
end Set_Loop_Level;
procedure Set_Case_Branch (C : O_Enode; Branch : O_Enode) is
begin
Enodes.Table (C).Arg2 := Branch;
end Set_Case_Branch;
procedure Set_Case_Branch_Choice (Branch : O_Enode; Choice : O_Enode) is
begin
Enodes.Table (Branch).Arg1 := Choice;
end Set_Case_Branch_Choice;
function Get_Case_Branch_Choice (Branch : O_Enode) return O_Enode is
begin
return Enodes.Table (Branch).Arg1;
end Get_Case_Branch_Choice;
procedure Set_Case_Choice_Link (Choice : O_Enode; N_Choice : O_Enode) is
begin
Enodes.Table (Choice).Info := Int32 (N_Choice);
end Set_Case_Choice_Link;
function Get_Case_Choice_Link (Choice : O_Enode) return O_Enode is
begin
return O_Enode (Enodes.Table (Choice).Info);
end Get_Case_Choice_Link;
function Get_Ref_Field (Ref : O_Enode) return O_Fnode is
begin
return O_Fnode (Enodes.Table (Ref).Arg2);
end Get_Ref_Field;
function Get_Ref_Index (Ref : O_Enode) return O_Enode is
begin
return Enodes.Table (Ref).Arg2;
end Get_Ref_Index;
function Get_Expr_Line_Number (Stmt : O_Enode) return Int32 is
begin
return Int32 (Enodes.Table (Stmt).Arg1);
end Get_Expr_Line_Number;
function Get_Intrinsic_Operation (Stmt : O_Enode) return Int32 is
begin
return Int32 (Enodes.Table (Stmt).Arg1);
end Get_Intrinsic_Operation;
Last_Stmt : O_Enode := O_Enode_Null;
procedure Link_Stmt (Stmt : O_Enode) is
begin
if Last_Stmt = O_Enode_Null then
raise Program_Error;
end if;
Set_Stmt_Link (Last_Stmt, Stmt);
Last_Stmt := Stmt;
end Link_Stmt;
function New_Enode (Kind : OE_Kind;
Rtype : O_Tnode;
Arg1 : O_Enode;
Arg2 : O_Enode) return O_Enode
is
Mode : Mode_Type;
begin
Mode := Get_Type_Mode (Rtype);
Enodes.Append (Enode_Common'(Kind => Kind,
Reg => 0,
Mode => Mode,
Ref => False,
Flag1 => False,
Flag2 => False,
Flag3 => False,
Pad => 0,
Arg1 => Arg1,
Arg2 => Arg2,
Info => Int32 (Rtype)));
return Enodes.Last;
end New_Enode;
function New_Enode (Kind : OE_Kind;
Mode : Mode_Type;
Rtype : O_Tnode;
Arg1 : O_Enode;
Arg2 : O_Enode) return O_Enode
is
begin
Enodes.Append (Enode_Common'(Kind => Kind,
Reg => 0,
Mode => Mode,
Ref => False,
Flag1 => False,
Flag2 => False,
Flag3 => False,
Pad => 0,
Arg1 => Arg1,
Arg2 => Arg2,
Info => Int32 (Rtype)));
return Enodes.Last;
end New_Enode;
procedure New_Enode_Stmt (Kind : OE_Kind; Arg1 : O_Enode; Arg2 : O_Enode)
is
begin
Enodes.Append (Enode_Common'(Kind => Kind,
Reg => 0,
Mode => Mode_Nil,
Ref => False,
Flag1 => False,
Flag2 => False,
Flag3 => False,
Pad => 0,
Arg1 => Arg1,
Arg2 => Arg2,
Info => 0));
Link_Stmt (Enodes.Last);
end New_Enode_Stmt;
procedure New_Enode_Stmt
(Kind : OE_Kind; Mode : Mode_Type; Arg1 : O_Enode; Arg2 : O_Enode)
is
begin
Enodes.Append (Enode_Common'(Kind => Kind,
Reg => 0,
Mode => Mode,
Ref => False,
Flag1 => False,
Flag2 => False,
Flag3 => False,
Pad => 0,
Arg1 => Arg1,
Arg2 => Arg2,
Info => 0));
Link_Stmt (Enodes.Last);
end New_Enode_Stmt;
Bb_Num : Int32 := 0;
Last_Bb : O_Enode := O_Enode_Null;
procedure Create_BB is
begin
New_Enode_Stmt (OE_BB, Mode_Nil, O_Enode_Null, O_Enode (Bb_Num));
if Last_Bb /= O_Enode_Null then
Set_BB_Next (Last_Bb, Enodes.Last);
end if;
Last_Bb := Enodes.Last;
Bb_Num := Bb_Num + 1;
end Create_BB;
procedure Start_BB is
begin
if Flags.Flag_Opt_BB then
Create_BB;
end if;
end Start_BB;
pragma Inline (Start_BB);
procedure Check_Ref (E : O_Enode) is
begin
if Enodes.Table (E).Ref then
raise Syntax_Error;
end if;
Enodes.Table (E).Ref := True;
end Check_Ref;
procedure Check_Ref (E : O_Lnode) is
begin
Check_Ref (O_Enode (E));
end Check_Ref;
procedure Check_Value_Type (Val : O_Enode; Vtype : O_Tnode) is
begin
if Get_Enode_Type (Val) /= Vtype then
raise Syntax_Error;
end if;
end Check_Value_Type;
function New_Const_U32 (Val : Uns32; Vtype : O_Tnode) return O_Enode
is
begin
return New_Enode (OE_Const, Vtype,
O_Enode (To_Int32 (Val)), O_Enode_Null);
end New_Const_U32;
Last_Decl : O_Dnode := 2;
Cur_Block : O_Enode := O_Enode_Null;
procedure Start_Declare_Stmt
is
Res : O_Enode;
begin
New_Enode_Stmt (OE_Beg, Cur_Block, O_Enode_Null);
Res := Enodes.Last;
Enodes.Table (Res).Arg2 := O_Enode
(Ortho_Code.Decls.Start_Declare_Stmt);
Cur_Block := Res;
end Start_Declare_Stmt;
function New_Stack (Rtype : O_Tnode) return O_Enode is
begin
return New_Enode (OE_Get_Stack, Rtype, O_Enode_Null, O_Enode_Null);
end New_Stack;
procedure New_Stack_Restore (Blk : O_Enode)
is
Save_Asgn : O_Enode;
Save_Var : O_Dnode;
begin
Save_Asgn := Get_Stmt_Link (Blk);
Save_Var := Get_Addr_Object (Get_Assign_Target (Save_Asgn));
New_Enode_Stmt (OE_Set_Stack, New_Value (New_Obj (Save_Var)),
O_Enode_Null);
end New_Stack_Restore;
procedure Finish_Declare_Stmt
is
Parent : O_Dnode;
begin
if Get_Block_Has_Alloca (Cur_Block) then
New_Stack_Restore (Cur_Block);
end if;
New_Enode_Stmt (OE_End, Cur_Block, O_Enode_Null);
Cur_Block := Get_Block_Parent (Cur_Block);
if Cur_Block = O_Enode_Null then
Parent := O_Dnode_Null;
else
Parent := Get_Block_Decls (Cur_Block);
end if;
Ortho_Code.Decls.Finish_Declare_Stmt (Parent);
end Finish_Declare_Stmt;
function New_Label return O_Enode is
begin
return New_Enode (OE_Label, Mode_Nil, O_Tnode_Null,
Cur_Block, O_Enode_Null);
end New_Label;
procedure Start_Subprogram_Body (Func : O_Dnode)
is
Start : O_Enode;
D_Body : O_Dnode;
Data : Subprogram_Data_Acc;
begin
if Cur_Subprg = null then
Abi.Start_Body (Func);
end if;
Start := New_Enode (OE_Entry, Mode_Nil, O_Tnode_Null,
Last_Stmt, O_Enode_Null);
D_Body := Decls.Start_Subprogram_Body (Func, Start);
-- Create the corresponding decl.
Enodes.Table (Start).Arg2 := O_Enode (D_Body);
-- Create the data record.
Data := new Subprogram_Data'(Parent => Cur_Subprg,
First_Child => null,
Last_Child => null,
Brother => null,
Depth => Get_Decl_Depth (Func),
D_Decl => Func,
E_Entry => Start,
D_Body => D_Body,
Exit_Label => O_Enode_Null,
Last_Stmt => O_Enode_Null,
Stack_Max => 0);
if not Flag_Debug_Hli then
Data.Exit_Label := New_Label;
end if;
-- Link the record.
if Cur_Subprg = null then
-- A top-level subprogram.
if First_Subprg = null then
First_Subprg := Data;
else
Last_Subprg.Brother := Data;
end if;
Last_Subprg := Data;
else
-- A nested subprogram.
if Cur_Subprg.First_Child = null then
Cur_Subprg.First_Child := Data;
else
Cur_Subprg.Last_Child.Brother := Data;
end if;
Cur_Subprg.Last_Child := Data;
-- Also save last_stmt.
Cur_Subprg.Last_Stmt := Last_Stmt;
end if;
Cur_Subprg := Data;
Last_Stmt := Start;
Start_Declare_Stmt;
-- Create a basic block for the beginning of the subprogram.
Start_BB;
-- Disp declarations.
if Cur_Subprg.Parent = null then
if Ortho_Code.Debug.Flag_Debug_Body
or Ortho_Code.Debug.Flag_Debug_Code
then
while Last_Decl <= D_Body loop
case Get_Decl_Kind (Last_Decl) is
when OD_Block =>
-- Skip blocks.
Disp_Decl (1, Last_Decl);
Last_Decl := Get_Block_Last (Last_Decl) + 1;
when others =>
Disp_Decl (1, Last_Decl);
Last_Decl := Last_Decl + 1;
end case;
end loop;
end if;
end if;
end Start_Subprogram_Body;
procedure Finish_Subprogram_Body
is
Parent : Subprogram_Data_Acc;
begin
Finish_Declare_Stmt;
-- Create a new basic block for the epilog.
Start_BB;
if not Flag_Debug_Hli then
Link_Stmt (Cur_Subprg.Exit_Label);
end if;
New_Enode_Stmt (OE_Leave, O_Enode_Null, O_Enode_Null);
-- Save last statement.
Cur_Subprg.Last_Stmt := Enodes.Last;
-- Set Leave of Entry.
Set_Entry_Leave (Cur_Subprg.E_Entry, Enodes.Last);
Decls.Finish_Subprogram_Body;
Parent := Cur_Subprg.Parent;
if Flags.Flag_Optimize then
Opts.Optimize_Subprg (Cur_Subprg);
end if;
if Parent = null then
-- This is a top-level subprogram.
if Ortho_Code.Debug.Flag_Disp_Code then
Disps.Disp_Subprg (Cur_Subprg);
end if;
if Ortho_Code.Debug.Flag_Dump_Code then
Disp_Subprg_Body (1, Cur_Subprg.E_Entry);
end if;
if not Ortho_Code.Debug.Flag_Debug_Dump then
Abi.Finish_Body (Cur_Subprg);
end if;
end if;
-- Restore Cur_Subprg.
Cur_Subprg := Parent;
-- Restore Last_Stmt.
if Cur_Subprg = null then
Last_Stmt := O_Enode_Null;
else
Last_Stmt := Cur_Subprg.Last_Stmt;
end if;
end Finish_Subprogram_Body;
function Get_Inner_Alloca (Label : O_Enode) return O_Enode
is
Res : O_Enode := O_Enode_Null;
Blk : O_Enode;
Last_Blk : constant O_Enode := Get_Label_Block (Label);
begin
Blk := Cur_Block;
while Blk /= Last_Blk loop
if Get_Block_Has_Alloca (Blk) then
Res := Blk;
end if;
Blk := Get_Block_Parent (Blk);
end loop;
return Res;
end Get_Inner_Alloca;
procedure Emit_Jmp (Code : OE_Kind; Expr : O_Enode; Label : O_Enode)
is
begin
-- Discard jump after jump.
if Code /= OE_Jump or else Get_Expr_Kind (Last_Stmt) /= OE_Jump then
New_Enode_Stmt (Code, Expr, Label);
end if;
end Emit_Jmp;
-- If there is stack allocated memory to be freed, free it.
-- Then jump to LABEL.
procedure New_Allocb_Jump (Label : O_Enode)
is
Inner_Alloca : O_Enode;
begin
Inner_Alloca := Get_Inner_Alloca (Label);
if Inner_Alloca /= O_Enode_Null then
New_Stack_Restore (Inner_Alloca);
end if;
Emit_Jmp (OE_Jump, O_Enode_Null, Label);
end New_Allocb_Jump;
function New_Lit (Lit : O_Cnode) return O_Enode
is
L_Type : O_Tnode;
H, L : Uns32;
begin
L_Type := Get_Const_Type (Lit);
if Flag_Debug_Hli then
return New_Enode (OE_Lit, L_Type, O_Enode (Lit), O_Enode_Null);
else
case Get_Const_Kind (Lit) is
when OC_Signed
| OC_Unsigned
| OC_Float
| OC_Null
| OC_Lit =>
Get_Const_Bytes (Lit, H, L);
return New_Enode
(OE_Const, L_Type,
O_Enode (To_Int32 (L)), O_Enode (To_Int32 (H)));
when OC_Address
| OC_Subprg_Address =>
return New_Enode (OE_Addrg, L_Type,
O_Enode (Get_Const_Decl (Lit)), O_Enode_Null);
when OC_Array
| OC_Record
| OC_Union
| OC_Sizeof =>
raise Syntax_Error;
end case;
end if;
end New_Lit;
function Get_Static_Chain (Depth : O_Depth) return O_Enode
is
Cur_Depth : O_Depth := Cur_Subprg.Depth;
Subprg : Subprogram_Data_Acc;
Res : O_Enode;
begin
if Depth = Cur_Depth then
return New_Enode (OE_Get_Frame, Abi.Mode_Ptr, O_Tnode_Ptr,
O_Enode_Null, O_Enode_Null);
else
Subprg := Cur_Subprg;
Res := O_Enode_Null;
loop
-- The static chain is the first interface of the subprogram.
Res := New_Enode (OE_Addrl, Abi.Mode_Ptr, O_Tnode_Ptr,
O_Enode (Get_Subprg_Interfaces (Subprg.D_Decl)),
Res);
Res := New_Enode (OE_Indir, O_Tnode_Ptr, Res, O_Enode_Null);
Cur_Depth := Cur_Depth - 1;
if Cur_Depth = Depth then
return Res;
end if;
Subprg := Subprg.Parent;
end loop;
end if;
end Get_Static_Chain;
function New_Obj (Obj : O_Dnode) return O_Lnode
is
O_Type : O_Tnode;
Kind : OE_Kind;
Chain : O_Enode;
Depth : O_Depth;
begin
O_Type := Get_Decl_Type (Obj);
case Get_Decl_Kind (Obj) is
when OD_Local
| OD_Interface =>
Kind := OE_Addrl;
-- Local declarations are 1 deeper than their subprogram.
Depth := Get_Decl_Depth (Obj) - 1;
if Depth /= Cur_Subprg.Depth then
Chain := Get_Static_Chain (Depth);
else
Chain := O_Enode_Null;
end if;
when OD_Var
| OD_Const =>
Kind := OE_Addrg;
Chain := O_Enode_Null;
when others =>
raise Program_Error;
end case;
return O_Lnode (New_Enode (Kind, Abi.Mode_Ptr, O_Type,
O_Enode (Obj), Chain));
end New_Obj;
function New_Dyadic_Op (Kind : ON_Dyadic_Op_Kind; Left, Right : O_Enode)
return O_Enode
is
L_Type : O_Tnode;
begin
L_Type := Get_Enode_Type (Left);
if Flag_Debug_Assert then
if L_Type /= Get_Enode_Type (Right) then
raise Syntax_Error;
end if;
if Get_Type_Mode (L_Type) = Mode_Blk then
raise Syntax_Error;
end if;
Check_Ref (Left);
Check_Ref (Right);
end if;
return New_Enode (OE_Kind'Val (ON_Op_Kind'Pos (Kind)),
L_Type, Left, Right);
end New_Dyadic_Op;
function New_Monadic_Op (Kind : ON_Monadic_Op_Kind; Operand : O_Enode)
return O_Enode
is
O_Type : O_Tnode;
begin
O_Type := Get_Enode_Type (Operand);
if Flag_Debug_Assert then
if Get_Type_Mode (O_Type) = Mode_Blk then
raise Syntax_Error;
end if;
Check_Ref (Operand);
end if;
return New_Enode (OE_Kind'Val (ON_Op_Kind'Pos (Kind)), O_Type,
Operand, O_Enode_Null);
end New_Monadic_Op;
function New_Compare_Op
(Kind : ON_Compare_Op_Kind; Left, Right : O_Enode; Ntype : O_Tnode)
return O_Enode
is
Res : O_Enode;
begin
if Flag_Debug_Assert then
if Get_Enode_Type (Left) /= Get_Enode_Type (Right) then
raise Syntax_Error;
end if;
if Get_Expr_Mode (Left) = Mode_Blk then
raise Syntax_Error;
end if;
if Get_Type_Kind (Ntype) /= OT_Boolean then
raise Syntax_Error;
end if;
Check_Ref (Left);
Check_Ref (Right);
end if;
Res := New_Enode (OE_Kind'Val (ON_Op_Kind'Pos (Kind)), Ntype,
Left, Right);
if Flag_Debug_Hli then
return New_Enode (OE_Typed, Ntype, Res, O_Enode (Ntype));
else
return Res;
end if;
end New_Compare_Op;
function New_Sizeof (Atype : O_Tnode; Rtype : O_Tnode) return O_Enode is
begin
return New_Const_U32 (Get_Type_Size (Atype), Rtype);
end New_Sizeof;
function New_Offsetof (Field : O_Fnode; Rtype : O_Tnode) return O_Enode is
begin
return New_Const_U32 (Get_Field_Offset (Field), Rtype);
end New_Offsetof;
function Is_Pow2 (V : Uns32) return Boolean is
begin
return (V and -V) = V;
end Is_Pow2;
function Extract_Pow2 (V : Uns32) return Uns32 is
begin
for I in Natural range 0 .. 31 loop
if V = Shift_Left (1, I) then
return Uns32 (I);
end if;
end loop;
raise Program_Error;
end Extract_Pow2;
function New_Index_Slice_Element
(Arr : O_Lnode; Index : O_Enode; Res_Type : O_Tnode)
return O_Lnode
is
El_Type : O_Tnode;
In_Type : O_Tnode;
Sz : O_Enode;
El_Size : Uns32;
begin
El_Type := Get_Type_Array_Element (Get_Enode_Type (O_Enode (Arr)));
In_Type := Get_Enode_Type (Index);
if Flag_Debug_Assert then
Check_Ref (Index);
Check_Ref (Arr);
end if;
-- result := arr + index * sizeof (element).
El_Size := Get_Type_Size (El_Type);
if El_Size = 1 then
Sz := Index;
elsif Get_Expr_Kind (Index) = OE_Const then
-- FIXME: may recycle previous index?
Sz := New_Const_U32 (Get_Expr_Low (Index) * El_Size, In_Type);
else
if Is_Pow2 (El_Size) then
Sz := New_Const_U32 (Extract_Pow2 (El_Size), In_Type);
Sz := New_Enode (OE_Shl, In_Type, Index, Sz);
else
Sz := New_Const_U32 (El_Size, In_Type);
Sz := New_Enode (OE_Mul, In_Type, Index, Sz);
end if;
end if;
return O_Lnode (New_Enode (OE_Add, Abi.Mode_Ptr, Res_Type,
O_Enode (Arr), Sz));
end New_Index_Slice_Element;
function New_Hli_Index_Slice
(Kind : OE_Kind; Res_Type : O_Tnode; Arr : O_Lnode; Index : O_Enode)
return O_Lnode
is
begin
if Flag_Debug_Assert then
Check_Ref (Index);
Check_Ref (Arr);
end if;
return O_Lnode (New_Enode (Kind, Res_Type, O_Enode (Arr), Index));
end New_Hli_Index_Slice;
-- Get an element of an array.
-- INDEX must be of the type of the array index.
function New_Indexed_Element (Arr : O_Lnode; Index : O_Enode)
return O_Lnode
is
El_Type : O_Tnode;
begin
El_Type := Get_Type_Array_Element (Get_Enode_Type (O_Enode (Arr)));
if Flag_Debug_Hli then
return New_Hli_Index_Slice (OE_Index_Ref, El_Type, Arr, Index);
else
return New_Index_Slice_Element (Arr, Index, El_Type);
end if;
end New_Indexed_Element;
-- Get a slice of an array; this is equivalent to a conversion between
-- an array or an array subtype and an array subtype.
-- RES_TYPE must be an array_sub_type whose base type is the same as the
-- base type of ARR.
-- INDEX must be of the type of the array index.
function New_Slice (Arr : O_Lnode; Res_Type : O_Tnode; Index : O_Enode)
return O_Lnode
is
begin
if Flag_Debug_Hli then
return New_Hli_Index_Slice (OE_Slice_Ref, Res_Type, Arr, Index);
else
return New_Index_Slice_Element (Arr, Index, Res_Type);
end if;
end New_Slice;
function New_Selected_Element (Rec : O_Lnode; El : O_Fnode)
return O_Lnode
is
Offset : Uns32;
Off : O_Enode;
Res_Type : O_Tnode;
begin
if Flag_Debug_Assert then
Check_Ref (Rec);
end if;
Res_Type := Get_Field_Type (El);
if Flag_Debug_Hli then
return O_Lnode (New_Enode (OE_Record_Ref, Res_Type,
O_Enode (Rec), O_Enode (El)));
else
Offset := Get_Field_Offset (El);
if Offset = 0 then
return O_Lnode (New_Enode (OE_Conv_Ptr, Abi.Mode_Ptr, Res_Type,
O_Enode (Rec), O_Enode (Res_Type)));
else
Off := New_Enode (OE_Const, Mode_U32, O_Tnode_Null,
O_Enode (Offset), O_Enode_Null);
return O_Lnode (New_Enode (OE_Add, Abi.Mode_Ptr, Res_Type,
O_Enode (Rec), Off));
end if;
end if;
end New_Selected_Element;
function New_Access_Element (Acc : O_Enode) return O_Lnode
is
Acc_Type : O_Tnode;
Res_Type : O_Tnode;
begin
Acc_Type := Get_Enode_Type (Acc);
if Flag_Debug_Assert then
if Get_Type_Kind (Acc_Type) /= OT_Access then
raise Syntax_Error;
end if;
Check_Ref (Acc);
end if;
Res_Type := Get_Type_Access_Type (Acc_Type);
if Flag_Debug_Hli then
return O_Lnode (New_Enode (OE_Access_Ref, Abi.Mode_Ptr, Res_Type,
Acc, O_Enode_Null));
else
return O_Lnode (New_Enode (OE_Conv_Ptr, Abi.Mode_Ptr, Res_Type,
Acc, O_Enode (Res_Type)));
end if;
end New_Access_Element;
function New_Convert_Ov (Val : O_Enode; Rtype : O_Tnode) return O_Enode is
begin
if Flag_Debug_Assert then
Check_Ref (Val);
end if;
return New_Enode (OE_Conv, Rtype, Val, O_Enode (Rtype));
end New_Convert_Ov;
function New_Unchecked_Address (Lvalue : O_Lnode; Atype : O_Tnode)
return O_Enode is
begin
if Flag_Debug_Assert then
if Get_Type_Kind (Atype) /= OT_Access then
raise Syntax_Error;
end if;
Check_Ref (Lvalue);
end if;
return New_Enode (OE_Conv_Ptr, Abi.Mode_Ptr, Atype,
O_Enode (Lvalue), O_Enode (Atype));
end New_Unchecked_Address;
function New_Address (Lvalue : O_Lnode; Atype : O_Tnode) return O_Enode is
begin
if Flag_Debug_Assert then
if Get_Type_Kind (Atype) /= OT_Access then
raise Syntax_Error;
end if;
Check_Ref (Lvalue);
end if;
return New_Enode (OE_Conv_Ptr, Abi.Mode_Ptr, Atype,
O_Enode (Lvalue), O_Enode (Atype));
end New_Address;
function New_Subprogram_Address (Subprg : O_Dnode; Atype : O_Tnode)
return O_Enode is
begin
raise Program_Error;
return O_Enode_Null;
end New_Subprogram_Address;
function New_Value (Lvalue : O_Lnode) return O_Enode
is
V_Type : O_Tnode;
begin
V_Type := Get_Enode_Type (O_Enode (Lvalue));
if Flag_Debug_Assert then
Check_Ref (Lvalue);
end if;
return New_Enode (OE_Indir, V_Type, O_Enode (Lvalue), O_Enode_Null);
end New_Value;
function New_Alloca (Rtype : O_Tnode; Size : O_Enode) return O_Enode
is
Save_Var : O_Dnode;
Stmt : O_Enode;
St_Type : O_Tnode;
begin
if Flag_Debug_Assert then
Check_Ref (Size);
if Get_Type_Kind (Rtype) /= OT_Access then
raise Syntax_Error;
end if;
if Get_Type_Kind (Get_Enode_Type (Size)) /= OT_Unsigned then
raise Syntax_Error;
end if;
end if;
if not Get_Block_Has_Alloca (Cur_Block) then
Set_Block_Has_Alloca (Cur_Block, True);
if Stack_Ptr_Type /= O_Tnode_Null then
St_Type := Stack_Ptr_Type;
else
St_Type := Rtype;
end if;
-- Add a decl.
New_Var_Decl (Save_Var, O_Ident_Nul, O_Storage_Local, St_Type);
-- Add insn to save stack ptr.
Stmt := New_Enode (OE_Asgn, St_Type,
New_Stack (St_Type),
O_Enode (New_Obj (Save_Var)));
if Cur_Block = Last_Stmt then
Set_Stmt_Link (Last_Stmt, Stmt);
Last_Stmt := Stmt;
else
Set_Stmt_Link (Stmt, Get_Stmt_Link (Cur_Block));
Set_Stmt_Link (Cur_Block, Stmt);
end if;
end if;
return New_Enode (OE_Alloca, Rtype, Size, O_Enode (Rtype));
end New_Alloca;
procedure Start_Association (Assocs : out O_Assoc_List; Subprg : O_Dnode)
is
Depth : O_Depth;
Arg : O_Enode;
First_Inter : O_Dnode;
begin
First_Inter := Get_Subprg_Interfaces (Subprg);
if Get_Decl_Storage (Subprg) = O_Storage_Local then
Depth := Get_Decl_Depth (Subprg);
Arg := New_Enode (OE_Arg, Abi.Mode_Ptr, O_Tnode_Ptr,
Get_Static_Chain (Depth - 1), O_Enode_Null);
First_Inter := Get_Interface_Chain (First_Inter);
else
Arg := O_Enode_Null;
end if;
Assocs := (Subprg => Subprg,
First_Arg => Arg,
Last_Arg => Arg,
Next_Inter => First_Inter);
end Start_Association;
procedure New_Association (Assocs : in out O_Assoc_List; Val : O_Enode)
is
V_Type : O_Tnode;
Mode : Mode_Type;
N_Mode : Mode_Type;
Res : O_Enode;
begin
V_Type := Get_Enode_Type (Val);
if Flag_Debug_Assert then
if Assocs.Next_Inter = O_Dnode_Null then
-- More assocs than interfaces.
raise Syntax_Error;
end if;
Check_Value_Type (Val, Get_Decl_Type (Assocs.Next_Inter));
Check_Ref (Val);
end if;
-- Follow the C convention call: no parameters shorter than int.
Mode := Get_Type_Mode (V_Type);
case Mode is
when Mode_B2
| Mode_U8
| Mode_U16 =>
N_Mode := Mode_U32;
when Mode_I8
| Mode_I16 =>
N_Mode := Mode_I32;
when Mode_P32
| Mode_U32
| Mode_I32
| Mode_U64
| Mode_I64
| Mode_P64
| Mode_F32
| Mode_F64 =>
N_Mode := Mode;
when Mode_Blk
| Mode_Nil
| Mode_X1 =>
raise Program_Error;
end case;
if N_Mode /= Mode and not Flag_Debug_Hli then
Res := New_Enode (OE_Conv, N_Mode, V_Type, Val, O_Enode (V_Type));
else
Res := Val;
end if;
Res := New_Enode (OE_Arg, N_Mode, V_Type, Res, O_Enode_Null);
if Assocs.Last_Arg /= O_Enode_Null then
Enodes.Table (Assocs.Last_Arg).Arg2 := Res;
else
Assocs.First_Arg := Res;
end if;
Assocs.Last_Arg := Res;
Assocs.Next_Inter := Get_Interface_Chain (Assocs.Next_Inter);
end New_Association;
function New_Function_Call (Assocs : O_Assoc_List) return O_Enode
is
F_Type : O_Tnode;
begin
if Flag_Debug_Assert then
if Assocs.Next_Inter /= O_Dnode_Null then
-- Not enough assocs.
raise Syntax_Error;
end if;
end if;
F_Type := Get_Decl_Type (Assocs.Subprg);
return New_Enode (OE_Call, F_Type,
O_Enode (Assocs.Subprg), Assocs.First_Arg);
end New_Function_Call;
procedure New_Procedure_Call (Assocs : in out O_Assoc_List) is
begin
if Flag_Debug_Assert then
if Assocs.Next_Inter /= O_Dnode_Null then
-- Not enough assocs.
raise Syntax_Error;
end if;
end if;
New_Enode_Stmt (OE_Call, O_Enode (Assocs.Subprg), Assocs.First_Arg);
end New_Procedure_Call;
procedure New_Assign_Stmt (Target : O_Lnode; Value : O_Enode)
is
V_Type : O_Tnode;
begin
V_Type := Get_Enode_Type (Value);
if Flag_Debug_Assert then
Check_Value_Type (Value, Get_Enode_Type (O_Enode (Target)));
Check_Ref (Value);
Check_Ref (Target);
end if;
New_Enode_Stmt (OE_Asgn, Get_Type_Mode (V_Type),
Value, O_Enode (Target));
end New_Assign_Stmt;
procedure New_Return_Stmt (Value : O_Enode)
is
V_Type : O_Tnode;
begin
V_Type := Get_Enode_Type (Value);
if Flag_Debug_Assert then
Check_Ref (Value);
Check_Value_Type (Value, Get_Decl_Type (Cur_Subprg.D_Decl));
end if;
New_Enode_Stmt (OE_Ret, Get_Type_Mode (V_Type), Value, O_Enode_Null);
if not Flag_Debug_Hli then
Emit_Jmp (OE_Jump, O_Enode_Null, Cur_Subprg.Exit_Label);
end if;
end New_Return_Stmt;
procedure New_Return_Stmt is
begin
if Flag_Debug_Assert then
if Get_Decl_Kind (Cur_Subprg.D_Decl) /= OD_Procedure then
raise Syntax_Error;
end if;
end if;
if not Flag_Debug_Hli then
Emit_Jmp (OE_Jump, O_Enode_Null, Cur_Subprg.Exit_Label);
else
New_Enode_Stmt (OE_Ret, Mode_Nil, O_Enode_Null, O_Enode_Null);
end if;
end New_Return_Stmt;
procedure Start_If_Stmt (Block : out O_If_Block; Cond : O_Enode) is
begin
if Flag_Debug_Assert then
if Get_Expr_Mode (Cond) /= Mode_B2 then
-- COND must be a boolean.
raise Syntax_Error;
end if;
Check_Ref (Cond);
end if;
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_If, Cond, O_Enode_Null);
Block := (Label_End => O_Enode_Null,
Label_Next => Last_Stmt);
else
Block := (Label_End => O_Enode_Null,
Label_Next => New_Label);
Emit_Jmp (OE_Jump_F, Cond, Block.Label_Next);
Start_BB;
end if;
end Start_If_Stmt;
procedure New_Else_Stmt (Block : in out O_If_Block) is
begin
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_Else, O_Enode_Null, O_Enode_Null);
else
if Block.Label_End = O_Enode_Null then
Block.Label_End := New_Label;
end if;
Emit_Jmp (OE_Jump, O_Enode_Null, Block.Label_End);
Start_BB;
Link_Stmt (Block.Label_Next);
Block.Label_Next := O_Enode_Null;
end if;
end New_Else_Stmt;
procedure Finish_If_Stmt (Block : in out O_If_Block) is
begin
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_Endif, O_Enode_Null, O_Enode_Null);
else
-- Create a badic-block after the IF.
Start_BB;
if Block.Label_Next /= O_Enode_Null then
Link_Stmt (Block.Label_Next);
end if;
if Block.Label_End /= O_Enode_Null then
Link_Stmt (Block.Label_End);
end if;
end if;
end Finish_If_Stmt;
procedure Start_Loop_Stmt (Label : out O_Snode) is
begin
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_Loop, O_Enode_Null, O_Enode_Null);
Label := (Label_Start => Last_Stmt,
Label_End => O_Enode_Null);
else
-- Create a basic-block at the beginning of the loop.
Start_BB;
Label.Label_Start := New_Label;
Link_Stmt (Label.Label_Start);
Label.Label_End := New_Label;
end if;
end Start_Loop_Stmt;
procedure Finish_Loop_Stmt (Label : in out O_Snode)
is
begin
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_Eloop, Label.Label_Start, O_Enode_Null);
else
Emit_Jmp (OE_Jump, O_Enode_Null, Label.Label_Start);
Start_BB;
Link_Stmt (Label.Label_End);
end if;
end Finish_Loop_Stmt;
procedure New_Exit_Stmt (L : O_Snode)
is
begin
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_Exit, O_Enode_Null, L.Label_Start);
else
New_Allocb_Jump (L.Label_End);
end if;
end New_Exit_Stmt;
procedure New_Next_Stmt (L : O_Snode)
is
begin
if not Flag_Lower_Stmt then
New_Enode_Stmt (OE_Next, O_Enode_Null, L.Label_Start);
else
New_Allocb_Jump (L.Label_Start);
end if;
end New_Next_Stmt;
procedure Start_Case_Stmt (Block : out O_Case_Block; Value : O_Enode)
is
V_Type : O_Tnode;
Mode : Mode_Type;
Start : O_Enode;
begin
V_Type := Get_Enode_Type (Value);
Mode := Get_Type_Mode (V_Type);
if Flag_Debug_Assert then
Check_Ref (Value);
case Mode is
when Mode_U8 .. Mode_U64
| Mode_I8 .. Mode_I64
| Mode_B2 =>
null;
when others =>
raise Syntax_Error;
end case;
end if;
New_Enode_Stmt (OE_Case, Mode, Value, O_Enode_Null);
Start := Enodes.Last;
if Flag_Debug_Hli then
Block := (Expr => Start,
Expr_Type => V_Type,
Last_Node => O_Enode_Null,
Label_End => O_Enode_Null,
Label_Branch => Start);
else
Block := (Expr => Start,
Expr_Type => V_Type,
Last_Node => Start,
Label_End => New_Label,
Label_Branch => O_Enode_Null);
end if;
end Start_Case_Stmt;
procedure Start_Choice (Block : in out O_Case_Block)
is
B : O_Enode;
begin
if Flag_Debug_Hli then
B := New_Enode (OE_Case_Branch, Mode_Nil, O_Tnode_Null,
O_Enode_Null, O_Enode_Null);
Link_Stmt (B);
-- Link it.
Set_Case_Branch (Block.Label_Branch, B);
Block.Label_Branch := B;
else
-- Jump to the end of the case statement.
-- If there is already a branch open, this is ok
-- (do not fall-through).
-- If there is no branch open, then this is the default choice
-- (nothing to do).
Emit_Jmp (OE_Jump, O_Enode_Null, Block.Label_End);
-- Create a label for the code of this branch.
Block.Label_Branch := New_Label;
end if;
end Start_Choice;
procedure Insert_Choice_Stmt (Block : in out O_Case_Block; Stmt : O_Enode)
is
Prev : O_Enode;
begin
Prev := Get_Stmt_Link (Block.Last_Node);
Set_Stmt_Link (Block.Last_Node, Stmt);
Block.Last_Node := Stmt;
if Prev = O_Enode_Null then
Last_Stmt := Stmt;
else
Set_Stmt_Link (Stmt, Prev);
end if;
end Insert_Choice_Stmt;
procedure Emit_Choice_Jmp (Block : in out O_Case_Block;
Code : OE_Kind; Expr : O_Enode; Label : O_Enode)
is
Jmp : O_Enode;
begin
Jmp := New_Enode (Code, Mode_Nil, O_Tnode_Null, Expr, Label);
Insert_Choice_Stmt (Block, Jmp);
end Emit_Choice_Jmp;
-- Create a node containing the value of the case expression.
function New_Case_Expr (Block : O_Case_Block) return O_Enode is
begin
return New_Enode (OE_Case_Expr, Block.Expr_Type,
Block.Expr, O_Enode_Null);
end New_Case_Expr;
procedure New_Hli_Choice (Block : in out O_Case_Block;
Hi, Lo : O_Enode)
is
Res : O_Enode;
begin
Res := New_Enode (OE_Case_Choice, Mode_Nil, O_Tnode_Null, Hi, Lo);
if Block.Label_End = O_Enode_Null then
Set_Case_Branch_Choice (Block.Label_Branch, Res);
else
Set_Case_Choice_Link (Block.Label_End, Res);
end if;
Block.Label_End := Res;
end New_Hli_Choice;
procedure New_Expr_Choice (Block : in out O_Case_Block; Expr : O_Cnode)
is
Res : O_Enode;
begin
if Flag_Debug_Hli then
New_Hli_Choice (Block, New_Lit (Expr), O_Enode_Null);
else
Res := New_Enode (OE_Eq, Mode_B2, O_Tnode_Null,
New_Case_Expr (Block), New_Lit (Expr));
Emit_Choice_Jmp (Block, OE_Jump_T, Res, Block.Label_Branch);
end if;
end New_Expr_Choice;
procedure New_Range_Choice (Block : in out O_Case_Block;
Low, High : O_Cnode)
is
E1 : O_Enode;
E2 : O_Enode;
Label : O_Enode;
begin
if Flag_Debug_Hli then
New_Hli_Choice (Block, New_Lit (Low), New_Lit (High));
else
-- Internal label.
Label := New_Label;
E1 := New_Enode (OE_Lt, Mode_B2, O_Tnode_Null,
New_Case_Expr (Block), New_Lit (Low));
Emit_Choice_Jmp (Block, OE_Jump_T, E1, Label);
E2 := New_Enode (OE_Le, Mode_B2, O_Tnode_Null,
New_Case_Expr (Block), New_Lit (High));
Emit_Choice_Jmp (Block, OE_Jump_T, E2, Block.Label_Branch);
Insert_Choice_Stmt (Block, Label);
end if;
end New_Range_Choice;
procedure New_Default_Choice (Block : in out O_Case_Block) is
begin
if Flag_Debug_Hli then
New_Hli_Choice (Block, O_Enode_Null, O_Enode_Null);
else
-- Jump to the code.
Emit_Choice_Jmp (Block, OE_Jump, O_Enode_Null, Block.Label_Branch);
end if;
end New_Default_Choice;
procedure Finish_Choice (Block : in out O_Case_Block) is
begin
if Flag_Debug_Hli then
Block.Label_End := O_Enode_Null;
else
-- Put the label of the branch.
Start_BB;
Link_Stmt (Block.Label_Branch);
end if;
end Finish_Choice;
procedure Finish_Case_Stmt (Block : in out O_Case_Block) is
begin
if Flag_Debug_Hli then
New_Enode_Stmt (OE_Case_End, O_Enode_Null, O_Enode_Null);
else
-- Jump to the end of the case statement.
-- Note: this is not required, since the next instruction is the
-- label.
-- Emit_Jmp (OE_Jump, O_Enode_Null, Block.Label_End);
-- Put the label of the end of the case.
Start_BB;
Link_Stmt (Block.Label_End);
Block.Label_End := O_Enode_Null;
end if;
end Finish_Case_Stmt;
procedure New_Debug_Line_Stmt (Line : Natural) is
begin
New_Enode_Stmt (OE_Line, O_Enode (Line), O_Enode_Null);
end New_Debug_Line_Stmt;
procedure Debug_Expr (N : O_Enode)
is
use Ada.Text_IO;
use Ortho_Code.Debug.Int32_IO;
Indent : constant Count := Col;
begin
Put (Int32 (N), 0);
Set_Col (Indent + 7);
Disp_Mode (Get_Expr_Mode (N));
Put (" ");
Put (OE_Kind'Image (Get_Expr_Kind (N)));
Set_Col (Indent + 26);
-- Put (Abi.Image_Insn (Get_Expr_Insn (N)));
-- Put (" ");
Put (Abi.Image_Reg (Get_Expr_Reg (N)));
Put (" ");
Put (Int32 (Enodes.Table (N).Arg1), 7);
Put (Int32 (Enodes.Table (N).Arg2), 7);
Put (Enodes.Table (N).Info, 7);
New_Line;
end Debug_Expr;
procedure Disp_Subprg_Body (Indent : Natural; Subprg : O_Enode)
is
use Ada.Text_IO;
N : O_Enode;
N_Indent : Natural;
begin
N := Subprg;
if Get_Expr_Kind (N) /= OE_Entry then
raise Program_Error;
end if;
-- Display the entry.
Set_Col (Count (Indent));
Debug_Expr (N);
-- Display the subprogram, binding.
N_Indent := Indent;-- + 1;
N := N + 1;
loop
case Get_Expr_Kind (N) is
when OE_Entry =>
N := Get_Entry_Leave (N) + 1;
when OE_Leave =>
Set_Col (Count (Indent));
Debug_Expr (N);
exit;
when others =>
Set_Col (Count (N_Indent));
Debug_Expr (N);
case Get_Expr_Kind (N) is
when OE_Beg =>
Disp_Block (N_Indent + 2,
O_Dnode (Enodes.Table (N).Arg2));
N_Indent := N_Indent + 1;
when OE_End =>
N_Indent := N_Indent - 1;
when others =>
null;
end case;
N := N + 1;
end case;
end loop;
end Disp_Subprg_Body;
procedure Disp_All_Enode is
begin
for I in Enodes.First .. Enodes.Last loop
Debug_Expr (I);
end loop;
end Disp_All_Enode;
Max_Enode : O_Enode := O_Enode_Null;
procedure Mark (M : out Mark_Type) is
begin
M.Enode := Enodes.Last;
end Mark;
procedure Release (M : Mark_Type) is
begin
Max_Enode := O_Enode'Max (Max_Enode, Enodes.Last);
Enodes.Set_Last (M.Enode);
end Release;
procedure Disp_Stats
is
use Ada.Text_IO;
begin
Max_Enode := O_Enode'Max (Max_Enode, Enodes.Last);
Put ("Number of Enodes:" & O_Enode'Image (Enodes.Last));
Put (", max:" & O_Enode'Image (Max_Enode));
New_Line;
end Disp_Stats;
procedure Free_Subprogram_Data (Data : in out Subprogram_Data_Acc)
is
procedure Free is new Ada.Unchecked_Deallocation
(Subprogram_Data, Subprogram_Data_Acc);
Ch, N_Ch : Subprogram_Data_Acc;
begin
Ch := Data.First_Child;
while Ch /= null loop
N_Ch := Ch.Brother;
Free_Subprogram_Data (Ch);
Ch := N_Ch;
end loop;
Free (Data);
end Free_Subprogram_Data;
procedure Finish is
begin
Enodes.Free;
Free_Subprogram_Data (First_Subprg);
end Finish;
end Ortho_Code.Exprs;
|