aboutsummaryrefslogtreecommitdiffstats
path: root/ortho/gcc/ortho_gcc.adb
blob: 66a3f1af0e7e319ebad8f5d68b578019504c5da1 (plain)
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
--  Ortho implementation for GCC.
--  Copyright (C) 2002, 2003, 2004, 2005 Tristan Gingold
--
--  GHDL is free software; you can redistribute it and/or modify it under
--  the terms of the GNU General Public License as published by the Free
--  Software Foundation; either version 2, or (at your option) any later
--  version.
--
--  GHDL is distributed in the hope that it will be useful, but WITHOUT ANY
--  WARRANTY; without even the implied warranty of MERCHANTABILITY or
--  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
--  for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with GCC; see the file COPYING.  If not, write to the Free
--  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
--  02111-1307, USA.
--with Ada.Unchecked_Conversion;
with Agcc; use Agcc;
with Agcc.Hwint; use Agcc.Hwint;
with Agcc.Toplev; use Agcc.Toplev;
with Agcc.Tm; use Agcc.Tm;
with Agcc.Stor_Layout; use Agcc.Stor_Layout;
with Agcc.Machmode;
with System;
with Agcc.Autils; use Agcc.Autils;
with Agcc.Real; use Agcc.Real;
with Agcc.Fe; use Agcc.Fe;
with Agcc.Rtl; use Agcc.Rtl;
with Agcc.Input; use Agcc.Input;
with Agcc.Machmode;

package body Ortho_Gcc is
   type ON_Op_To_Tree_Code_Type is array (ON_Op_Kind) of Tree_Code;
   ON_Op_To_Tree_Code : constant ON_Op_To_Tree_Code_Type :=
     (
      --  Dyadic operations.
      ON_Add_Ov => PLUS_EXPR,
      ON_Sub_Ov => MINUS_EXPR,
      ON_Mul_Ov => MULT_EXPR,
      ON_Div_Ov => ERROR_MARK,
      ON_Rem_Ov => TRUNC_MOD_EXPR,
      ON_Mod_Ov => FLOOR_MOD_EXPR,

      --  Binary operations.
      ON_And => TRUTH_AND_EXPR,
      ON_Or => TRUTH_OR_EXPR,
      ON_Xor => TRUTH_XOR_EXPR,
      ON_And_Then => TRUTH_ANDIF_EXPR,
      ON_Or_Else => TRUTH_ORIF_EXPR,

      --  Monadic operations.
      ON_Not => TRUTH_NOT_EXPR,
      ON_Neg_Ov => NEGATE_EXPR,
      ON_Abs_Ov => ABS_EXPR,

      --  Comparaisons
      ON_Eq => EQ_EXPR,
      ON_Neq => NE_EXPR,
      ON_Le => LE_EXPR,
      ON_Lt => LT_EXPR,
      ON_Ge => GE_EXPR,
      ON_Gt => GT_EXPR,

      ON_Nil => ERROR_MARK
      );

   --  Constants used for FP rounding.
   Fp_Const_P5 : REAL_VALUE_TYPE;   -- 0.5
   Fp_Const_M_P5 : REAL_VALUE_TYPE; -- -0.5
   Fp_Const_Zero : REAL_VALUE_TYPE; -- 0.0

   procedure Init
   is
      use Agcc.Machmode;

      L, H : HOST_WIDE_INT;
      V : REAL_VALUE_TYPE;
   begin
      To_Host_Wide_Int (Integer_64'(1), L, H);
      REAL_VALUE_FROM_INT (V'Address, L, H, DFmode);
      Fp_Const_P5 := REAL_VALUE_LDEXP (V, -1);

      To_Host_Wide_Int (Integer_64'(-1), L, H);
      REAL_VALUE_FROM_INT (V'Address, L, H, DFmode);
      Fp_Const_M_P5 := REAL_VALUE_LDEXP (V, -1);

      To_Host_Wide_Int (Integer_64'(0), L, H);
      REAL_VALUE_FROM_INT (Fp_Const_Zero'Address, L, H, DFmode);
   end Init;

   procedure Chain_Init (Constr : out Chain_Constr_Type) is
   begin
      Constr.First := NULL_TREE;
      Constr.Last := NULL_TREE;
   end Chain_Init;

   procedure Chain_Append (Constr : in out Chain_Constr_Type; El : Tree)
   is
   begin
      if Constr.First = NULL_TREE then
         if Constr.Last /= NULL_TREE then
            raise Program_Error;
         end if;
         Constr.First := El;
      else
         Set_TREE_CHAIN (Constr.Last, El);
      end if;
      Constr.Last := El;
   end Chain_Append;


   procedure List_Init (Constr : out List_Constr_Type) is
   begin
      Constr := (First => NULL_TREE, Last => NULL_TREE);
   end List_Init;

   procedure List_Append (Constr : in out List_Constr_Type; El : Tree)
   is
      Res : Tree;
   begin
      Res := Tree_Cons (NULL_TREE, El, NULL_TREE);
      if Constr.First = NULL_TREE then
         Constr.First := Res;
      else
         Set_TREE_CHAIN (Constr.Last, Res);
      end if;
      Constr.Last := Res;
   end List_Append;


   function New_Dyadic_Op (Kind : ON_Dyadic_Op_Kind; Left, Right : O_Enode)
     return O_Enode
   is
      Left_Type : Tree;
      Code : Tree_Code;
   begin
      Left_Type := Get_TREE_TYPE (Tree (Left));
      if Left_Type /= Get_TREE_TYPE (Tree (Right)) then
         raise Type_Error;
      end if;
      case Kind is
         when ON_Div_Ov =>
            if Get_TREE_CODE (Left_Type) = REAL_TYPE then
               Code := RDIV_EXPR;
            else
               Code := TRUNC_DIV_EXPR;
            end if;
         when others =>
            Code := ON_Op_To_Tree_Code (Kind);
      end case;
      return O_Enode (Build (Code, Left_Type, Tree (Left), Tree (Right)));
   end New_Dyadic_Op;

   function New_Monadic_Op (Kind : ON_Monadic_Op_Kind; Operand : O_Enode)
     return O_Enode
   is
   begin
      return Build1 (ON_Op_To_Tree_Code (Kind),
                     Get_TREE_TYPE (Operand), Operand);
   end New_Monadic_Op;

   function New_Compare_Op
     (Kind : ON_Compare_Op_Kind; Left, Right : O_Enode; Ntype : O_Tnode)
     return O_Enode
   is
   begin
      if Get_TREE_CODE (Ntype) /= BOOLEAN_TYPE then
         raise Type_Error;
      end if;
      if Get_TREE_TYPE (Left) /= Get_TREE_TYPE (Right) then
         raise Type_Error;
      end if;
      return O_Enode (Build (ON_Op_To_Tree_Code (Kind),
                             Tree (Ntype), Tree (Left), Tree (Right)));
   end New_Compare_Op;

--   function Unchecked_Conversion is new
--   Ada.Unchecked_Conversion (Source => Unsigned_32, Target => HOST_WIDE_INT);

--    function High_Part (V : Unsigned_64) return HOST_WIDE_INT
--    is
--    begin
--       return Unchecked_Conversion (Unsigned_32 (Shift_Left (V, 32)));
--    end High_Part;

--    function Low_Part (V : Unsigned_64) return HOST_WIDE_INT
--    is
--    begin
--       return Unchecked_Conversion
--         (Unsigned_32 (V and (Unsigned_32'Modulus - 1)));
--    end Low_Part;

   function New_Signed_Literal (Ltype : O_Tnode; Value : Integer_64)
     return O_Cnode
   is
      L, H : HOST_WIDE_INT;
      Res : Tree;
   begin
      To_Host_Wide_Int (Value, L, H);
      Res := Build_Int_2 (L, H);
      Set_TREE_TYPE (Res, Tree (Ltype));
      return O_Cnode (Res);
   end New_Signed_Literal;

   function New_Unsigned_Literal (Ltype : O_Tnode; Value : Unsigned_64)
     return O_Cnode
   is
      Res : Tree;
      L, H : HOST_WIDE_INT;
   begin
      To_Host_Wide_Int (Value, L, H);
      Res := Build_Int_2 (L, H);
      Set_TREE_TYPE (Res, Tree (Ltype));
      return O_Cnode (Res);
   end New_Unsigned_Literal;

   function New_Null_Access (Ltype : O_Tnode) return O_Cnode
   is
      Res : Tree;
   begin
      Res := Build_Int_2 (0, 0);
      Set_TREE_TYPE (Res, Tree (Ltype));
      return O_Cnode (Res);
   end New_Null_Access;

   function New_Float_Literal (Ltype : O_Tnode; Value : IEEE_Float_64)
                              return O_Cnode
   is
      Res : REAL_VALUE_TYPE;
   begin
      Res := To_Real_Value_Type (Value);
      return O_Cnode (Build_Real (Tree (Ltype), Res));
   end New_Float_Literal;

   procedure Check_Constrained_Type (Atype : O_Tnode)
   is
      pragma Unreferenced (Atype);
   begin
      null;
   end Check_Constrained_Type;

   procedure Finish_Type_Def (Atype : O_Tnode) is
   begin
      Layout_Type (Atype);
      --Rest_Of_Type_Compilation (Tree (Atype), True);
   end Finish_Type_Def;

   procedure New_Uncomplete_Record_Type (Res : out O_Tnode) is
   begin
      Res := Make_Node (RECORD_TYPE);
   end New_Uncomplete_Record_Type;

   procedure Start_Record_Type (Elements : out O_Element_List) is
   begin
      Elements.Res := Make_Node (RECORD_TYPE);
      Chain_Init (Elements.Chain);
   end Start_Record_Type;

   procedure Start_Uncomplete_Record_Type (Res : O_Tnode;
                                           Elements : out O_Element_List) is
   begin
      Elements.Res := Tree (Res);
      Chain_Init (Elements.Chain);
   end Start_Uncomplete_Record_Type;

   procedure New_Record_Union_Field
     (Elements : in out O_Element_List;
      El : out O_Fnode;
      Ident : O_Ident; Etype : O_Tnode)
   is
      Res : Tree;
   begin
      Check_Constrained_Type (Etype);
      Res := Build_Decl (FIELD_DECL, Ident, Tree (Etype));
      Set_DECL_CONTEXT (Res, Elements.Res);
      Chain_Append (Elements.Chain, Res);
      El := O_Fnode (Res);
   end New_Record_Union_Field;

   procedure New_Record_Field
     (Elements : in out O_Element_List;
      El : out O_Fnode;
      Ident : O_Ident; Etype : O_Tnode)
     renames New_Record_Union_Field;

   procedure Finish_Record_Type
     (Elements : in out O_Element_List; Res : out O_Tnode) is
   begin
      Set_TYPE_FIELDS (Elements.Res, Elements.Chain.First);
      Finish_Type_Def (O_Tnode (Elements.Res));
      Res := O_Tnode (Elements.Res);
      if Get_TYPE_NAME (Elements.Res) /= NULL_TREE then
         --  The type was completed.
         Rest_Of_Type_Compilation (Elements.Res, C_True);
      end if;
   end Finish_Record_Type;

   procedure Start_Union_Type (Elements : out O_Element_List) is
   begin
      Elements.Res := Make_Node (UNION_TYPE);
      Chain_Init (Elements.Chain);
   end Start_Union_Type;

   procedure New_Union_Field
     (Elements : in out O_Element_List;
      El : out O_Fnode;
      Ident : O_Ident; Etype : O_Tnode)
     renames New_Record_Union_Field;

   procedure Finish_Union_Type
     (Elements : in out O_Element_List; Res : out O_Tnode) is
   begin
      Set_TYPE_FIELDS (Elements.Res, Elements.Chain.First);
      Finish_Type_Def (O_Tnode (Elements.Res));
      Res := O_Tnode (Elements.Res);
   end Finish_Union_Type;

   function New_Access_Type (Dtype : O_Tnode) return O_Tnode
   is
      Res : Tree;
   begin
      if Dtype = O_Tnode_Null then
         Res := Make_Node (POINTER_TYPE);
         Set_TREE_TYPE (Res, NULL_TREE);
         Set_TYPE_MODE (Res, Machmode.Ptr_Mode);
         Layout_Type (Res);
         return O_Tnode (Res);
      else
         return Build_Pointer_Type (Dtype);
      end if;
   end New_Access_Type;

   procedure Finish_Access_Type (Atype : O_Tnode; Dtype : O_Tnode) is
   begin
      if Get_TREE_CODE (Atype) /= POINTER_TYPE
        or else Get_TREE_TYPE (Atype) /= O_Tnode_Null
      then
         raise Syntax_Error;
      end if;
      Set_TREE_TYPE (Atype, Dtype);
   end Finish_Access_Type;

   function New_Array_Type (El_Type : O_Tnode; Index_Type : O_Tnode)
     return O_Tnode
   is
   begin
      Check_Constrained_Type (El_Type);
      return Build_Array_Type (El_Type, Index_Type);
   end New_Array_Type;

   function New_Constrained_Array_Type (Atype : O_Tnode; Length : O_Cnode)
     return O_Tnode
   is
      Range_Type : Tree;
      Index_Type : Tree;
      Len : Tree;
      One : Tree;
   begin
      --if Atype.Kind /= ON_Array_Type then
      --   raise Type_Error;
      --end if;
      Index_Type := Get_TYPE_DOMAIN (Tree (Atype));
      if +Integer_Zerop (Tree (Length)) then
         --  Handle null array, by creating a one-length array...
         Len := Size_Zero_Node;
      else
         One := Build_Int_2 (1, 0);
         Set_TREE_TYPE (One, Index_Type);
         Len := Build (MINUS_EXPR, Index_Type, Tree (Length), One);
         Len := Fold (Len);
      end if;
      Range_Type := Build_Range_Type (Index_Type, Size_Zero_Node, Len);
      return O_Tnode (Build_Array_Type (Get_TREE_TYPE (Tree (Atype)),
                                        Range_Type));
   end New_Constrained_Array_Type;

   function New_Unsigned_Type (Size : Natural) return O_Tnode
   is
   begin
      return Make_Unsigned_Type (Size);
   end New_Unsigned_Type;

   function New_Signed_Type (Size : Natural) return O_Tnode
   is
   begin
      return Make_Signed_Type (Size);
   end New_Signed_Type;

   function New_Float_Type return O_Tnode is
      Res : O_Tnode;
   begin
      Res := Make_Node (REAL_TYPE);
      Set_TYPE_PRECISION (Res, DOUBLE_TYPE_SIZE);
      Layout_Type (Res);
      return Res;
   end New_Float_Type;

   procedure New_Boolean_Type (Res : out O_Tnode;
                               False_Id : O_Ident;
                               False_E : out O_Cnode;
                               True_Id : O_Ident;
                               True_E : out O_Cnode)
   is
      pragma Unreferenced (False_Id);
      pragma Unreferenced (True_Id);
   begin
      --  see java/decl.c
      Res := O_Tnode'(Make_Node (BOOLEAN_TYPE));
      Set_TYPE_PRECISION (Tree (Res), 1);
      Fixup_Unsigned_Type (Tree (Res));
      False_E := O_Cnode (Get_TYPE_MIN_VALUE (Tree (Res)));
      True_E := O_Cnode (Get_TYPE_MAX_VALUE (Tree (Res)));
   end New_Boolean_Type;

   procedure Start_Enum_Type (List : out O_Enum_List; Size : Natural)
   is
   begin
      List.Res := Make_Node (ENUMERAL_TYPE);
      Chain_Init (List.Chain);
      List.Num := 0;
      List.Size := Size;
   end Start_Enum_Type;

   procedure New_Enum_Literal
     (List : in out O_Enum_List; Ident : O_Ident; Res : out O_Cnode)
   is
   begin
      Res := Build_Int_2 (HOST_WIDE_INT (List.Num), 0);
      Set_TREE_TYPE (Tree (Res), List.Res);
      Chain_Append (List.Chain, Tree_Cons (Ident, Tree (Res), NULL_TREE));
      List.Num := List.Num + 1;
   end New_Enum_Literal;

   procedure Finish_Enum_Type (List : in out O_Enum_List; Res : out O_Tnode) is
   begin
      Res := O_Tnode (List.Res);
      Set_TYPE_VALUES (List.Res, List.Chain.First);
      Set_TYPE_MIN_VALUE (List.Res, Get_TREE_VALUE (List.Chain.First));
      Set_TYPE_MAX_VALUE (List.Res, Get_TREE_VALUE (List.Chain.Last));
      Set_TREE_UNSIGNED (List.Res, C_True);
      Set_TYPE_PRECISION (List.Res, Integer (List.Size));
      Finish_Type_Def (Res);
   end Finish_Enum_Type;

   procedure Start_Record_Aggr (List : out O_Record_Aggr_List; Atype : O_Tnode)
   is
   begin
      List.Atype := Tree (Atype);
      Chain_Init (List.Chain);
   end Start_Record_Aggr;

   procedure New_Record_Aggr_El
     (List : in out O_Record_Aggr_List; Value : O_Cnode)
   is
   begin
      --  FIXME: should check type of value.
      Chain_Append (List.Chain,
                    Build_Tree_List (NULL_TREE, Tree (Value)));
   end New_Record_Aggr_El;

   procedure Finish_Record_Aggr
     (List : in out O_Record_Aggr_List; Res : out O_Cnode)
   is
   begin
      Res := O_Cnode (Build_Constructor (List.Atype, List.Chain.First));
   end Finish_Record_Aggr;

   procedure Start_Array_Aggr (List : out O_Array_Aggr_List; Atype : O_Tnode)
   is
   begin
      List.Atype := Tree (Atype);
      Chain_Init (List.Chain);
   end Start_Array_Aggr;

   procedure New_Array_Aggr_El
     (List : in out O_Array_Aggr_List; Value : O_Cnode)
   is
   begin
      --  FIXME: should check type of value.
      Chain_Append (List.Chain,
                    Build_Tree_List (NULL_TREE, Tree (Value)));
   end New_Array_Aggr_El;

   procedure Finish_Array_Aggr
     (List : in out O_Array_Aggr_List; Res : out O_Cnode)
   is
   begin
      Res := O_Cnode (Build_Constructor (List.Atype, List.Chain.First));
   end Finish_Array_Aggr;

   function New_Union_Aggr (Atype : O_Tnode; Field : O_Fnode; Value : O_Cnode)
                           return O_Cnode
   is
      El : Tree;
      Res : Tree;
   begin
      El := Build_Tree_List (Tree (Field), Tree (Value));
      Res := Build_Constructor (Tree (Atype), El);
      Set_TREE_CONSTANT (Res, C_True);
      return O_Cnode (Res);
   end New_Union_Aggr;

   function New_Indexed_Element (Arr : O_Lnode; Index : O_Enode)
     return O_Lnode
   is
      Res : Tree;
      V : C_Bool;
   begin
      V := Mark_Addressable (Tree (Arr));
      Res := Build (ARRAY_REF, Get_TREE_TYPE (Get_TREE_TYPE (Tree (Arr))),
                    Tree (Arr), Tree (Index));
      return O_Lnode (Res);
   end New_Indexed_Element;

   function New_Slice (Arr : O_Lnode; Res_Type : O_Tnode; Index : O_Enode)
     return O_Lnode
   is
      Res : Tree;
      Ptr_Type : Tree;
      V : C_Bool;
   begin
      --  *((RES_TYPE *)(&ARR[INDEX]))
      --  convert ARR to a pointer, add index, and reconvert to array ?
      if Get_TREE_CODE (Res_Type) /= ARRAY_TYPE then
         raise Type_Error;
      end if;
      V := Mark_Addressable (Tree (Arr));
      Ptr_Type := Build_Pointer_Type (Tree (Res_Type));
      Res := Build (ARRAY_REF, Get_TREE_TYPE (Get_TREE_TYPE (Tree (Arr))),
                    Tree (Arr), Tree (Index));
      Res := Build1 (ADDR_EXPR, Ptr_Type, Res);
      Res := Build1 (INDIRECT_REF, Tree (Res_Type), Res);
      return O_Lnode (Res);
   end New_Slice;

   function New_Selected_Element (Rec : O_Lnode; El : O_Fnode)
     return O_Lnode
   is
   begin
      if Get_TREE_CODE (Get_TREE_TYPE (Rec)) /= RECORD_TYPE then
         raise Type_Error;
      end if;
      return O_Lnode (Build (COMPONENT_REF, Get_TREE_TYPE (Tree (El)),
                             Tree (Rec), Tree (El)));
   end New_Selected_Element;

   function New_Access_Element (Acc : O_Enode) return O_Lnode
   is
      Acc_Type : Tree;
   begin
      Acc_Type := Get_TREE_TYPE (Tree (Acc));
      if Get_TREE_CODE (Acc_Type) /= POINTER_TYPE then
         raise Type_Error;
      end if;
      return O_Lnode (Build1 (INDIRECT_REF, Get_TREE_TYPE (Acc_Type),
                              Tree (Acc)));
   end New_Access_Element;

   function New_Convert_Ov (Val : O_Enode; Rtype : O_Tnode) return O_Enode
   is
      Val_Type : Tree;
      Val_Code : Tree_Code;
      Rtype_Code : Tree_Code;
      Code : Tree_Code;
   begin
      Val_Type := Get_TREE_TYPE (Tree (Val));
      if Val_Type = Tree (Rtype) then
         return Val;
      end if;
      --  FIXME: check conversions.
      Val_Code := Get_TREE_CODE (Val_Type);
      Rtype_Code := Get_TREE_CODE (Rtype);
      if Val_Code = POINTER_TYPE and then Rtype_Code = POINTER_TYPE then
         Code := NOP_EXPR;
      elsif Val_Code = INTEGER_TYPE and then Rtype_Code = INTEGER_TYPE then
         Code := CONVERT_EXPR;
      elsif Val_Code = REAL_TYPE and then Rtype_Code = INTEGER_TYPE then
         --  REAL to INTEGER
         --  Gcc only handles FIX_TRUNC_EXPR, but we need rounding.
         declare
            M_P5 : Tree;
            P5 : Tree;
            Zero : Tree;
            Saved : Tree;
            Comp : Tree;
            Adj : Tree;
            Res : Tree;
         begin
            M_P5 := Build_Real (Val_Type, Fp_Const_M_P5);
            P5 := Build_Real (Val_Type, Fp_Const_P5);
            Zero := Build_Real (Val_Type, Fp_Const_Zero);
            Saved := Build_Save_Expr (Tree (Val));
            Comp := Build (GE_EXPR, Integer_Type_Node, Saved, Zero);
            --  FIXME: instead of res = res + (comp ? .5 : -.5)
            --                 do: res = res (comp ? + : -) .5
            Adj := Build (COND_EXPR, Val_Type, Comp, P5, M_P5);
            Res := Build (PLUS_EXPR, Val_Type, Saved, Adj);
            Res := Build1 (FIX_TRUNC_EXPR, Tree (Rtype), Res);
            return O_Enode (Res);
         end;
      elsif Val_Code = INTEGER_TYPE and then Rtype_Code = ENUMERAL_TYPE then
         Code := CONVERT_EXPR;
      elsif Val_Code = ENUMERAL_TYPE and then Rtype_Code = INTEGER_TYPE then
         Code := CONVERT_EXPR;
      elsif Val_Code = INTEGER_TYPE and then Rtype_Code = REAL_TYPE then
         Code := FLOAT_EXPR;
      elsif Val_Code = BOOLEAN_TYPE and then Rtype_Code = BOOLEAN_TYPE then
         Code := NOP_EXPR;
      elsif Val_Code = BOOLEAN_TYPE and then Rtype_Code = INTEGER_TYPE then
         Code := CONVERT_EXPR;
      elsif Val_Code = INTEGER_TYPE and then Rtype_Code = BOOLEAN_TYPE then
         --  From integer to boolean.
         Code := NOP_EXPR;
      elsif Val_Code = REAL_TYPE and then Rtype_Code = REAL_TYPE then
         Code := CONVERT_EXPR;
      else
         raise Program_Error;
      end if;
      return O_Enode (Build1 (Code, Tree (Rtype), Tree (Val)));
   end New_Convert_Ov;

   function Build_Addr (Operand : Tree; Atype : Tree) return Tree
   is
      use Agcc.Machmode;
      Result : Tree;
   begin
      case Get_TREE_CODE (Operand) is
         when INDIRECT_REF =>
            --  This may be an unchecked conversion.
            Result := Get_TREE_OPERAND (Operand, 0);
            if Get_TREE_CODE (Get_TREE_TYPE (Result)) /= POINTER_TYPE then
               raise Program_Error;
            end if;
            return Result;

         when ARRAY_REF
           | COMPONENT_REF =>
            --  Find the address of the prefix and add the offset.
            declare
               Op_Type : Tree;
               Bitsize, Bitpos : HOST_WIDE_INT;
               Inner, Offset : Tree;
               Mode : Machine_Mode;
               Unsignedp, Volatilep : Integer;
            begin
               Op_Type := Get_TREE_TYPE (Get_TREE_OPERAND (Operand, 0));
               Inner := Get_Inner_Reference
                 (Operand, Bitsize'Address, Bitpos'Address, Offset'Address,
                  Mode'Address, Unsignedp'Address, Volatilep'Address);

               -- Compute the offset as a byte offset from INNER.  */
               if Offset = NULL_TREE then
                  Offset := Size_Zero_Node;
               end if;

               Offset := Size_Binop
                 (PLUS_EXPR, Offset,
                  Size_Int (Bitpos / HOST_WIDE_INT (BITS_PER_UNIT)));

               -- Take the address of INNER, convert the offset to void *, and
               -- add then.  It will later be converted to the desired result
               -- type, if any.
               Inner := Build_Addr (Inner, Ptr_Type_Node);
               Inner := Convert (Ptr_Type_Node, Inner);
               Offset := Convert (Ptr_Type_Node, Offset);
               Result := Fold
                 (Build (PLUS_EXPR, Ptr_Type_Node, Inner, Offset));
               return Result;
            end;

         when VAR_DECL =>
            --  This is like Mark_Addressable.
            Put_Var_Into_Stack (Operand, C_True);
            Set_TREE_ADDRESSABLE (Operand, C_True);
            return Build1 (ADDR_EXPR, Atype, Operand);

         when others =>
            raise Program_Error;
      end case;
   end Build_Addr;

   function New_Addr (Lvalue : Tree; Atype : O_Tnode)
                     return Tree
   is
      Result : Tree;
   begin
      Result := Build_Addr (Lvalue, Tree (Atype));
      if Get_TREE_TYPE (Result) /= Tree (Atype) then
         if Get_TREE_CODE (Get_TREE_TYPE (Result)) /= POINTER_TYPE then
            raise Program_Error;
         end if;
         Result := Build1 (NOP_EXPR, Tree (Atype), Result);
      end if;
      return Result;
      --return O_Enode (Build1 (ADDR_EXPR, Tree (Atype), Tree (Lvalue)));
   end New_Addr;

   function New_Unchecked_Address (Lvalue : O_Lnode; Atype : O_Tnode)
                                  return O_Enode
   is
   begin
      return O_Enode (New_Addr (Tree (Lvalue), Atype));
   end New_Unchecked_Address;

   function New_Address (Lvalue : O_Lnode; Atype : O_Tnode) return O_Enode
   is
   begin
      --if Get_TREE_TYPE (Lvalue) /= Get_TREE_TYPE (Atype) then
      --   raise Type_Error;
      --end if;
      return O_Enode (New_Addr (Tree (Lvalue), Atype));
   end New_Address;

   function New_Global_Unchecked_Address (Decl : O_Dnode; Atype : O_Tnode)
                                         return O_Cnode
   is
   begin
      return O_Cnode (New_Addr (Tree (Decl), Atype));
   end New_Global_Unchecked_Address;

   function New_Global_Address (Decl : O_Dnode; Atype : O_Tnode) return O_Cnode
   is
   begin
      --if Get_TREE_TYPE (Lvalue) /= Get_TREE_TYPE (Atype) then
      --   raise Type_Error;
      --end if;
      return O_Cnode (New_Addr (Tree (Decl), Atype));
   end New_Global_Address;

   function New_Subprogram_Address (Subprg : O_Dnode; Atype : O_Tnode)
     return O_Cnode
   is
   begin
      return O_Cnode (Build1 (ADDR_EXPR, Tree (Atype), Tree (Subprg)));
   end New_Subprogram_Address;

   function New_Value (Lvalue : O_Lnode) return O_Enode is
   begin
      return O_Enode (Lvalue);
   end New_Value;

   function New_Obj_Value (Obj : O_Dnode) return O_Enode is
   begin
      return O_Enode (Obj);
   end New_Obj_Value;

   function New_Obj (Obj : O_Dnode) return O_Lnode is
   begin
      return O_Lnode (Obj);
   end New_Obj;

   function New_Lit (Lit : O_Cnode) return O_Enode is
   begin
      return O_Enode (Lit);
   end New_Lit;

   function New_Offsetof (Field : O_Fnode; Rtype : O_Tnode) return O_Cnode
   is
      Off : Tree;
      Bit_Off : Tree;
      Pos : HOST_WIDE_INT;
      Res : Tree;
   begin
      Off := Get_DECL_FIELD_OFFSET (Tree (Field));
      if Host_Integerp (Off, 1) = 0 then
         --  The offset must be a constant.
         raise Program_Error;
      end if;
      Bit_Off := Get_DECL_FIELD_BIT_OFFSET (Tree (Field));
      if Host_Integerp (Bit_Off, 1) = 0 then
         --  The offset must be a constant.
         raise Program_Error;
      end if;
      Pos := Get_TREE_INT_CST_LOW (Off)
        + (Get_TREE_INT_CST_LOW (Bit_Off) / HOST_WIDE_INT (BITS_PER_UNIT));
      Res := Build_Int_2 (Pos, 0);
      Set_TREE_TYPE (Res, Tree (Rtype));
      return O_Cnode (Res);
   end New_Offsetof;

   function New_Sizeof (Atype : O_Tnode; Rtype : O_Tnode) return O_Cnode
   is
      Size : Tree;
   begin
      Size := Get_TYPE_SIZE_UNIT (Tree (Atype));
      --Size := Size_Binop (CEIL_DIV_EXPR, Size,
      --                    Size_Int (HOST_WIDE_INT (BITS_PER_UNIT)));

      return O_Cnode (Fold (Build1 (NOP_EXPR, Tree (Rtype), Size)));
   end New_Sizeof;

   function New_Alloca (Rtype : O_Tnode; Size : O_Enode) return O_Enode
   is
      Var : Tree;
      Var_Type : Tree;
      Res : Tree;
   begin
      --  Create a dummy variable of the correct size.
      --  This way, the storage will be deallocated at the end of the scope.
      Var_Type := Build_Array_Type (Char_Type_Node,
                                    Build_Index_Type (Tree (Size)));
      Var := Build_Decl (VAR_DECL, NULL_TREE, Var_Type);
      Set_TREE_STATIC (Var, C_False);
      Set_DECL_EXTERNAL (Var, C_False);
      Set_TREE_PUBLIC (Var, C_False);
      Pushdecl (Var);
      Expand_Decl (Var);
      Res := Build1 (ADDR_EXPR, Tree (Rtype), Var);
      return O_Enode (Res);

      -- Old code that use alloca.  This is not space efficient, since the
      -- storage will be freed only at the exit of the function.
      --Arg := Build1 (CONVERT_EXPR, Sizetype, Tree (Size));
      --Res := Build (CALL_EXPR, Ptr_Type_Node, Alloca_Function_Ptr,
      --              Tree_Cons (NULL_TREE, Arg, NULL_TREE));
      --return O_Enode (Build1 (NOP_EXPR, Tree (Rtype), Res));
   end New_Alloca;

   ---------------------
   --  Declarations.  --
   ---------------------

   type String_Acc is access String;
   Current_Filename : String_Acc := null;

   procedure New_Debug_Filename_Decl (Filename : String)
   is
      Len : Natural;
   begin
      Len := Filename'Length;
      if Current_Filename = null
        or else Current_Filename.all'Length /= Len + 1
        or else Current_Filename (1 .. Len) /= Filename
      then
         Current_Filename := new String (1 .. Len + 1);
         Current_Filename (1 .. Len) := Filename;
         Current_Filename (Len + 1) := Nul;
         Input_Location.File := Current_Filename (1)'Address;
      end if;
   end New_Debug_Filename_Decl;

   procedure New_Debug_Line_Decl (Line : Natural)
   is
   begin
      Input_Location.Line := Line;
   end New_Debug_Line_Decl;

   procedure New_Debug_Comment_Decl (Comment : String)
   is
      pragma Unreferenced (Comment);
   begin
      null;
   end New_Debug_Comment_Decl;

   procedure New_Type_Decl (Ident : O_Ident; Atype : O_Tnode)
   is
      Decl : Tree;
      Ttype : Tree := Tree (Atype);
   begin
--       if Atype.Decl /= null then
--          raise Type_Error;
--       end if;
      Set_TYPE_NAME (Ttype, Ident);
      Decl := Build_Decl (TYPE_DECL, Ident, Ttype);
      Set_TYPE_STUB_DECL (Ttype, Decl);
      Pushdecl (Decl);
      if Get_TYPE_SIZE (Ttype) /= NULL_TREE then
         --  Do not generate debug info for uncompleted types.
         Rest_Of_Type_Compilation (Ttype, C_True);
      end if;
   end New_Type_Decl;

   procedure Set_Storage (Node : Tree; Storage : O_Storage)
   is
   begin
      case Storage is
         when O_Storage_External =>
            Set_DECL_EXTERNAL (Node, C_True);
            Set_TREE_PUBLIC (Node, C_True);
            Set_TREE_STATIC (Node, C_False);
         when O_Storage_Public =>
            Set_DECL_EXTERNAL (Node, C_False);
            Set_TREE_PUBLIC (Node, C_True);
            Set_TREE_STATIC (Node, C_True);
         when O_Storage_Private =>
            Set_DECL_EXTERNAL (Node, C_False);
            Set_TREE_PUBLIC (Node, C_False);
            Set_TREE_STATIC (Node, C_True);
         when O_Storage_Local =>
            Set_DECL_EXTERNAL (Node, C_False);
            Set_TREE_PUBLIC (Node, C_False);
            Set_TREE_STATIC (Node, C_False);
      end case;
   end Set_Storage;

   procedure New_Const_Decl
     (Res : out O_Dnode;
      Ident : O_Ident;
      Storage : O_Storage;
      Atype : O_Tnode)
   is
      Cst : Tree;
   begin
      Cst := Build_Decl (VAR_DECL, Ident, Tree (Atype));
      Set_Storage (Cst, Storage);
      Set_TREE_READONLY (Cst, C_True);
      Pushdecl (Cst);
      case Storage is
         when O_Storage_Local =>
            raise Syntax_Error;
         when O_Storage_External =>
            --  We are at top level if Current_Function_Decl is null.
            Rest_Of_Decl_Compilation
              (Cst, NULL_Chars,
               Boolean'Pos (Current_Function_Decl = NULL_TREE), C_False);
         when O_Storage_Public
           | O_Storage_Private =>
            null;
      end case;
      Res := O_Dnode (Cst);
   end New_Const_Decl;

   procedure Start_Const_Value (Const : in out O_Dnode)
   is
      pragma Unreferenced (Const);
   begin
      null;
   end Start_Const_Value;

   procedure Finish_Const_Value (Const : in out O_Dnode; Val : O_Cnode)
   is
   begin
      Set_DECL_INITIAL (Tree (Const), Tree (Val));
      Set_TREE_CONSTANT (Val, C_True);
      Rest_Of_Decl_Compilation
        (Tree (Const), NULL_Chars,
         Boolean'Pos (Current_Function_Decl = NULL_TREE), C_False);
   end Finish_Const_Value;

   procedure New_Var_Decl
     (Res : out O_Dnode;
      Ident : O_Ident;
      Storage : O_Storage;
      Atype : O_Tnode)
   is
      Var : Tree;
   begin
      Var := Build_Decl (VAR_DECL, Ident, Tree (Atype));
      if Current_Function_Decl /= NULL_TREE  then
         --  Local variable.
         Set_TREE_STATIC (Var, C_False);
         Set_DECL_EXTERNAL (Var, C_False);
         Set_TREE_PUBLIC (Var, C_False);
      else
         Set_Storage (Var, Storage);
      end if;
      Pushdecl (Var);
      if Current_Function_Decl /= NULL_TREE  then
         Expand_Decl (Var);
      else
         Rest_Of_Decl_Compilation (Var, NULL_Chars, C_True, C_False);
      end if;
      Res := O_Dnode (Var);
   end New_Var_Decl;

   procedure Start_Function_Decl
     (Interfaces : out O_Inter_List;
      Ident : O_Ident;
      Storage : O_Storage;
      Rtype : O_Tnode)
   is
   begin
      Interfaces.Ident := Ident;
      Interfaces.Storage := Storage;
      Interfaces.Rtype := Rtype;
      Chain_Init (Interfaces.Param_Chain);
      List_Init (Interfaces.Param_List);
   end Start_Function_Decl;

   procedure Start_Procedure_Decl
     (Interfaces : out O_Inter_List; Ident : O_Ident; Storage : O_Storage) is
   begin
      Start_Function_Decl (Interfaces, Ident, Storage,
                           O_Tnode (Void_Type_Node));
   end Start_Procedure_Decl;

   procedure New_Interface_Decl
     (Interfaces : in out O_Inter_List;
      Res : out O_Dnode;
      Ident : O_Ident;
      Atype : O_Tnode)
   is
      R : Tree;
   begin
      R := Build_Decl (PARM_DECL, Ident, Tree (Atype));
      --Set_DECL_CONTEXT (Res, Xxx);

      --  Do type conversion: convert boolean and enums to int
      if +PROMOTE_PROTOTYPES then
         case Get_TREE_CODE (Tree (Atype)) is
            when ENUMERAL_TYPE
              | BOOLEAN_TYPE =>
               Set_DECL_ARG_TYPE (R, Integer_Type_Node);
            when others =>
               Set_DECL_ARG_TYPE (R, Tree (Atype));
         end case;
      else
         Set_DECL_ARG_TYPE (R, Tree (Atype));
      end if;

      Chain_Append (Interfaces.Param_Chain, R);
      List_Append (Interfaces.Param_List, Tree (Atype));
      Res := O_Dnode (R);
   end New_Interface_Decl;

   --  Current function nest level, or the number of parents.
   Function_Nest_Level : Natural := 0;

   procedure Finish_Subprogram_Decl
     (Interfaces : in out O_Inter_List; Res : out O_Dnode)
   is
      Decl : Tree;
      Result : Tree;
      Parm : Tree;
      Is_Global : Boolean;
   begin
      Decl := Build_Decl (FUNCTION_DECL, Interfaces.Ident,
                          Build_Function_Type (Tree (Interfaces.Rtype),
                                               Interfaces.Param_List.First));
      Is_Global := Function_Nest_Level = 0
        or Interfaces.Storage = O_Storage_External;
      if Is_Global then
         Set_Storage (Decl, Interfaces.Storage);
      else
         --  A nested subprogram.
         Set_DECL_EXTERNAL (Decl, C_False);
         Set_TREE_PUBLIC (Decl, C_False);
      end if;
      --  The function exist in static storage.
      Set_TREE_STATIC (Decl, C_True);
      Set_DECL_INITIAL (Decl, Error_Mark_Node);
      Set_TREE_ADDRESSABLE (Decl, C_True);

      --  Declare the result.
      --  FIXME: should be moved in start_function_body.
      Result := Build_Decl (RESULT_DECL, NULL_TREE, Tree (Interfaces.Rtype));
      Set_DECL_RESULT (Decl, Result);
      Set_DECL_CONTEXT (Result, Decl);

      Set_DECL_ARGUMENTS (Decl, Interfaces.Param_Chain.First);
      --  Set DECL_CONTEXT of parameters.
      Parm := Interfaces.Param_Chain.First;
      while Parm /= NULL_TREE loop
         Set_DECL_CONTEXT (Parm, Decl);
         Parm := Get_TREE_CHAIN (Parm);
      end loop;

      Pushdecl (Decl);

      if Is_Global then
         Rest_Of_Decl_Compilation (Decl, NULL_Chars, C_True, C_False);
      else
         Expand_Decl (Decl);
      end if;

      --Make_Function_Rtl (Decl);

      Res := O_Dnode (Decl);
   end Finish_Subprogram_Decl;

   procedure Start_Subprogram_Body (Func : O_Dnode)
   is
   begin
      if Function_Nest_Level /= 0 then
         --  For a nested subprogram:
         Push_Function_Context;
         --start_function (c-decl.c)
         --  announce_function
         --  current_function_decl = pushdecl (x)
         --  ??
         --XXX
         --finish_function(1) (c-decl.c)
         --  poplevel
         --pop_function_context
         --add_decl_stmt
      end if;
      Function_Nest_Level := Function_Nest_Level + 1;

      Current_Function_Decl := Tree (Func);
      Announce_Function (Tree (Func));

      --  Create a binding for the parameters.
      Pushlevel (C_False);
      --  FIXME: should push parameters.
      --Make_Function_Rtl (Current_Function_Decl);
      Init_Function_Start (Func, NULL_Chars, 0);
      Expand_Function_Start (Func, C_False);
      --  Create a binding for the function.
      --  This is necessary for compatibility.
      Pushlevel (C_False);
      Expand_Start_Bindings (0);
   end Start_Subprogram_Body;

   procedure Finish_Subprogram_Body
   is
   begin
      Expand_End_Bindings (Getdecls, C_True, C_False);
      Poplevel (C_True, C_False, C_False);
      Expand_Function_End (NULL_Chars, 0, C_False);
      Poplevel (C_True, C_False, C_True);
      --  The subprogram is not external anymore (extern inline is not
      --  supported).  As a result, code will be generated.
      Set_DECL_EXTERNAL (Current_Function_Decl, C_False);

      --  FIXME: protect against ggc.  See c-decl.c:c_expand_body
      Rest_Of_Compilation (Current_Function_Decl);
      Function_Nest_Level := Function_Nest_Level - 1;
      if Function_Nest_Level > 0 then
         Pop_Function_Context;
      else
         Current_Function_Decl := NULL_TREE;
      end if;
   end Finish_Subprogram_Body;

   -------------------
   --  Statements.  --
   -------------------

   procedure New_Debug_Line_Stmt (Line : Natural) is
   begin
      Input_Location.Line := Line;
      Emit_Line_Note (Input_Location);
   end New_Debug_Line_Stmt;

   procedure New_Debug_Comment_Stmt (Comment : String)
   is
      pragma Unreferenced (Comment);
   begin
      null;
   end New_Debug_Comment_Stmt;

   procedure Start_Declare_Stmt
   is
   begin
      Pushlevel (C_False);
      Expand_Start_Bindings (0);
   end Start_Declare_Stmt;

   procedure Finish_Declare_Stmt
   is
   begin
      Expand_End_Bindings (Getdecls, C_True, C_True);
      Poplevel (C_True, C_False, C_False);
   end Finish_Declare_Stmt;

   procedure Start_Association (Assocs : out O_Assoc_List; Subprg : O_Dnode)
   is
   begin
      Assocs.Subprg := Tree (Subprg);
      List_Init (Assocs.List);
   end Start_Association;

--    function Get_Base_Type (Atype : O_Tnode) return O_Tnode
--    is
--    begin
--       case Atype.Kind is
--          when ON_Array_Sub_Type =>
--             return Atype.Base_Type;
--          when others =>
--             return Atype;
--       end case;
--    end Get_Base_Type;

   procedure New_Association (Assocs : in out O_Assoc_List; Val : O_Enode)
   is
   begin
      List_Append (Assocs.List, Tree (Val));
   end New_Association;

   --  Return a pointer to function FUNC.
   function Build_Function_Ptr (Func : Tree) return Tree is
   begin
      return Build1 (ADDR_EXPR,
                     Build_Pointer_Type (Get_TREE_TYPE (Func)), Func);
   end Build_Function_Ptr;

   function New_Function_Call (Assocs : O_Assoc_List) return O_Enode
   is
   begin
      return O_Enode (Build (CALL_EXPR,
                             Get_TREE_TYPE (Get_TREE_TYPE (Assocs.Subprg)),
                             Build_Function_Ptr (Assocs.Subprg),
                             Assocs.List.First, NULL_TREE));
   end New_Function_Call;

   procedure New_Procedure_Call (Assocs : in out O_Assoc_List)
   is
      Res : Tree;
   begin
      Res := Build (CALL_EXPR,
                    Get_TREE_TYPE (Get_TREE_TYPE (Assocs.Subprg)),
                    Build_Function_Ptr (Assocs.Subprg),
                    Assocs.List.First, NULL_TREE);
      Set_TREE_SIDE_EFFECTS (Res, C_True);
      Expand_Expr_Stmt (Res);
   end New_Procedure_Call;


   procedure New_Assign_Stmt (Target : O_Lnode; Value : O_Enode)
   is
      N : Tree;
   begin
      N := Build (MODIFY_EXPR, Get_TREE_TYPE (Tree (Target)),
                  Tree (Target), Tree (Value));
      Set_TREE_SIDE_EFFECTS (N, C_True);
      Expand_Expr_Stmt (N);
   end New_Assign_Stmt;

   procedure New_Return_Stmt (Value : O_Enode)
   is
      Assign : Tree;
   begin
      Assign := Build (MODIFY_EXPR, Get_TREE_TYPE (Tree (Value)),
                       Get_DECL_RESULT (Current_Function_Decl),
                       Tree (Value));
      Set_TREE_SIDE_EFFECTS (Assign, C_True);
      --Set_TREE_USED (Assign, True);
      Expand_Expr_Stmt (Assign);
      Expand_Return (Value);
   end New_Return_Stmt;

   procedure New_Return_Stmt
   is
   begin
      Expand_Null_Return;
   end New_Return_Stmt;

   procedure Start_If_Stmt (Block : in out O_If_Block; Cond : O_Enode)
   is
      pragma Unreferenced (Block);
   begin
      Expand_Start_Cond (Cond, C_False);
   end Start_If_Stmt;

   procedure New_Elsif_Stmt (Block : in out O_If_Block; Cond : O_Enode)
   is
      pragma Unreferenced (Block);
   begin
      Expand_Start_Elseif (Cond);
   end New_Elsif_Stmt;

   procedure New_Else_Stmt (Block : in out O_If_Block)
   is
      pragma Unreferenced (Block);
   begin
      Expand_Start_Else;
   end New_Else_Stmt;

   procedure Finish_If_Stmt (Block : in out O_If_Block)
   is
      pragma Unreferenced (Block);
   begin
      Expand_End_Cond;
   end Finish_If_Stmt;

   procedure Start_Loop_Stmt (Label : out O_Snode)
   is
   begin
      Label := Expand_Start_Loop (C_True);
      --  This is required to avoid crash with goto fixup.
      Expand_Start_Bindings (0);
   end Start_Loop_Stmt;

   procedure Finish_Loop_Stmt (Label : in out O_Snode)
   is
      pragma Unreferenced (Label);
   begin
      Expand_End_Bindings (NULL_TREE, C_False, C_False);
      Expand_End_Loop;
   end Finish_Loop_Stmt;

   procedure New_Exit_Stmt (L : O_Snode) is
   begin
      Expand_Exit_Loop (L);
   end New_Exit_Stmt;

   procedure New_Next_Stmt (L : O_Snode) is
   begin
      Expand_Continue_Loop (L);
   end New_Next_Stmt;

   procedure Start_Case_Stmt (Block : in out O_Case_Block; Value : O_Enode)
   is
   begin
      Expand_Start_Case (C_True, Value, Get_TREE_TYPE (Value), NULL_Chars);
      Block := O_Case_Block'(Expr => Tree (Value),
                           First => True, Label => NULL_TREE);
   end Start_Case_Stmt;

   procedure Start_Choice (Block : in out O_Case_Block)
   is
   begin
      if Block.First then
         Block.First := False;
      else
         --  Add a "break" statement.
         if Expand_Exit_Something = 0 then
            raise Type_Error;
         end if;
      end if;
      if Block.Label /= NULL_TREE then
         raise Syntax_Error;
      end if;
      Block.Label := Build_Decl (LABEL_DECL, NULL_TREE, NULL_TREE);
      --Pushdecl (Choices.Label);
   end Start_Choice;

   procedure New_Expr_Choice (Block : in out O_Case_Block; Expr : O_Cnode)
   is
      Duplicate : Tree;
   begin
      if Pushcase (Tree (Expr), Agcc.Trees.Convert'Address,
                   Block.Label, Duplicate'Address) /= 0 then
         raise Syntax_Error;
      end if;
   end New_Expr_Choice;

   procedure New_Range_Choice (Block : in out O_Case_Block;
                               Low, High : O_Cnode)
   is
      Duplicate : Tree;
   begin
      if Pushcase_Range (Tree (Low), Tree (High), Agcc.Trees.Convert'Address,
                         Tree (Block.Label), Duplicate'Address) /= 0
      then
         raise Syntax_Error;
      end if;
   end New_Range_Choice;

   procedure New_Default_Choice (Block : in out O_Case_Block)
   is
      Duplicate : Tree;
   begin
      if Pushcase (NULL_TREE, System.Null_Address,
                   Block.Label, Duplicate'Address) /= 0
      then
         raise Syntax_Error;
      end if;
   end New_Default_Choice;

   procedure Finish_Choice (Block : in out O_Case_Block)
   is
   begin
      Block.Label := NULL_TREE;
   end Finish_Choice;

   procedure Finish_Case_Stmt (Block: in out O_Case_Block) is
   begin
      Expand_End_Case_Type (Block.Expr, NULL_TREE);
   end Finish_Case_Stmt;
end Ortho_Gcc;