aboutsummaryrefslogtreecommitdiffstats
path: root/src/synth/elab-vhdl_objtypes.adb
blob: ad2d50fa2cdeab51b221ca8ca9c82867f958d215 (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
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
--  Values in synthesis.
--  Copyright (C) 2017 Tristan Gingold
--
--  This file is part of GHDL.
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 2 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <gnu.org/licenses>.

with Ada.Unchecked_Conversion;
with System; use System;

with Mutils; use Mutils;

package body Elab.Vhdl_Objtypes is
   function To_Rec_El_Array_Acc is new Ada.Unchecked_Conversion
     (System.Address, Rec_El_Array_Acc);

   function To_Type_Acc is new Ada.Unchecked_Conversion
     (System.Address, Type_Acc);

   function "+" (L, R : Value_Offsets) return Value_Offsets is
   begin
      return (L.Net_Off + R.Net_Off, L.Mem_Off + R.Mem_Off);
   end "+";

   function Is_Bounded_Type (Typ : Type_Acc) return Boolean is
   begin
      case Typ.Kind is
         when Type_Bit
            | Type_Logic
            | Type_Discrete
            | Type_Float
            | Type_Vector
            | Type_Slice
            | Type_Array
            | Type_Record
            | Type_Access
            | Type_File =>
            return True;
         when Type_Unbounded_Array
            | Type_Array_Unbounded
            | Type_Unbounded_Vector
            | Type_Unbounded_Record
            | Type_Protected =>
            return False;
      end case;
   end Is_Bounded_Type;

   function Are_Types_Equal (L, R : Type_Acc) return Boolean is
   begin
      if L.Kind /= R.Kind
        or else L.W /= R.W
      then
         return False;
      end if;
      if L = R then
         return True;
      end if;

      case L.Kind is
         when Type_Bit
           | Type_Logic =>
            return True;
         when Type_Discrete =>
            return L.Drange = R.Drange;
         when Type_Float =>
            return L.Frange = R.Frange;
         when Type_Array
            | Type_Array_Unbounded
            | Type_Vector =>
            if L.Alast /= R.Alast then
               return False;
            end if;
            if L.Abound /= R.Abound then
               return False;
            end if;
            return Are_Types_Equal (L.Arr_El, R.Arr_El);
         when Type_Unbounded_Array
            | Type_Unbounded_Vector =>
            if L.Ulast /= R.Ulast then
               return False;
            end if;
            --  Also check index ?
            return Are_Types_Equal (L.Uarr_El, R.Uarr_El);
         when Type_Slice =>
            return Are_Types_Equal (L.Slice_El, R.Slice_El);
         when Type_Record
           | Type_Unbounded_Record =>
            if L.Rec.Len /= R.Rec.Len then
               return False;
            end if;
            for I in L.Rec.E'Range loop
               if not Are_Types_Equal (L.Rec.E (I).Typ, R.Rec.E (I).Typ) then
                  return False;
               end if;
            end loop;
            return True;
         when Type_Access =>
            return Are_Types_Equal (L.Acc_Acc, R.Acc_Acc);
         when Type_File =>
            return Are_Types_Equal (L.File_Typ, R.File_Typ);
         when Type_Protected =>
            return False;
      end case;
   end Are_Types_Equal;

   function Is_Last_Dimension (Arr : Type_Acc) return Boolean is
   begin
      case Arr.Kind is
         when Type_Vector
            | Type_Array
            | Type_Array_Unbounded =>
            return Arr.Alast;
         when Type_Unbounded_Vector =>
            return True;
         when Type_Unbounded_Array =>
            return Arr.Ulast;
         when others =>
            raise Internal_Error;
      end case;
   end Is_Last_Dimension;

   function Is_Null_Range (Rng : Discrete_Range_Type) return Boolean is
   begin
      case Rng.Dir is
         when Dir_To =>
            return Rng.Left > Rng.Right;
         when Dir_Downto =>
            return Rng.Left < Rng.Right;
      end case;
   end Is_Null_Range;

   function Is_Scalar_Subtype_Compatible (L, R : Type_Acc) return Boolean is
   begin
      pragma Assert (L.Kind = R.Kind);
      case L.Kind is
         when Type_Bit
           | Type_Logic =>
            --  We have no bounds for that...
            return True;
         when Type_Discrete =>
            if Is_Null_Range (L.Drange) then
               return True;
            end if;
            return In_Range (R.Drange, L.Drange.Left)
              and then In_Range (R.Drange, L.Drange.Right);
         when Type_Float =>
            return L.Frange = R.Frange;
         when others =>
            raise Internal_Error;
      end case;
   end Is_Scalar_Subtype_Compatible;

   function Discrete_Range_Width (Rng : Discrete_Range_Type) return Uns32
   is
      Lo, Hi : Int64;
      W : Uns32;
   begin
      case Rng.Dir is
         when Dir_To =>
            Lo := Rng.Left;
            Hi := Rng.Right;
         when Dir_Downto =>
            Lo := Rng.Right;
            Hi := Rng.Left;
      end case;
      if Lo > Hi then
         --  Null range.
         W := 0;
      elsif Lo >= 0 then
         --  Positive.
         W := Uns32 (Clog2 (Uns64 (Hi) + 1));
      elsif Lo = Int64'First then
         --  Handle possible overflow.
         W := 64;
      elsif Hi < 0 then
         --  Negative only.
         W := Uns32 (Clog2 (Uns64 (-Lo))) + 1;
      else
         declare
            Wl : constant Uns32 := Uns32 (Clog2 (Uns64 (-Lo)));
            Wh : constant Uns32 := Uns32 (Clog2 (Uns64 (Hi) + 1));
         begin
            W := Uns32'Max (Wl, Wh) + 1;
         end;
      end if;
      return W;
   end Discrete_Range_Width;

   function In_Bounds (Bnd : Bound_Type; V : Int32) return Boolean is
   begin
      case Bnd.Dir is
         when Dir_To =>
            return V >= Bnd.Left and then V <= Bnd.Right;
         when Dir_Downto =>
            return V <= Bnd.Left and then V >= Bnd.Right;
      end case;
   end In_Bounds;

   function In_Range (Rng : Discrete_Range_Type; V : Int64) return Boolean is
   begin
      case Rng.Dir is
         when Dir_To =>
            return V >= Rng.Left and then V <= Rng.Right;
         when Dir_Downto =>
            return V <= Rng.Left and then V >= Rng.Right;
      end case;
   end In_Range;

   function In_Float_Range (Rng : Float_Range_Type; V : Fp64) return Boolean is
   begin
      case Rng.Dir is
         when Dir_To =>
            return V >= Rng.Left and then V <= Rng.Right;
         when Dir_Downto =>
            return V <= Rng.Left and then V >= Rng.Right;
      end case;
   end In_Float_Range;

   function Build_Discrete_Range_Type
     (L : Int64; R : Int64; Dir : Direction_Type) return Discrete_Range_Type is
   begin
      return (Dir => Dir,
              Left => L,
              Right => R,
              Is_Signed => L < 0 or R < 0);
   end Build_Discrete_Range_Type;

   procedure Realign (Res : in out Size_Type;
                      Align : Size_Type) is
   begin
      Res := (Res + Align - 1) and not (Align - 1);
   end Realign;

   --  For Compute_Size_Type.
   procedure Add_Size_Type (Typ : Type_Acc;
                            Sz : in out Size_Type;
                            Align : in out Size_Type);

   procedure Add_Array_Size_Type (El_Typ : Type_Acc;
                                  Sz : in out Size_Type;
                                  Align : in out Size_Type)
   is
      subtype T is Type_Type (Type_Array);
   begin
      Align := Size_Type'Max (Align, T'Alignment);
      Realign (Sz, Align);
      Sz := Sz + (T'Size / System.Storage_Unit);
      Add_Size_Type (El_Typ, Sz, Align);
   end Add_Array_Size_Type;

   procedure Add_Size_Type (Typ : Type_Acc;
                            Sz : in out Size_Type;
                            Align : in out Size_Type) is
   begin
      case Typ.Kind is
         when Type_Bit
            | Type_Logic
            | Type_Discrete
            | Type_Float =>
            --  Never copied.
            return;
         when Type_Access
            | Type_File
            | Type_Protected =>
            --  Never copied
            return;
         when Type_Array
            | Type_Array_Unbounded
            | Type_Vector =>
            Add_Array_Size_Type (Typ.Arr_El, Sz, Align);
         when Type_Unbounded_Array
            | Type_Unbounded_Vector =>
            Add_Array_Size_Type (Typ.Uarr_El, Sz, Align);
         when Type_Record
            | Type_Unbounded_Record =>
            declare
               subtype T is Type_Type (Type_Record);
               subtype T_El is Rec_El_Array (Typ.Rec.Len);
            begin
               --  The type
               Align := Size_Type'Max (Align, T'Alignment);
               Realign (Sz, Align);
               Sz := Sz + (T'Size / System.Storage_Unit);
               --  The el array
               Align := Size_Type'Max (Align, T_El'Alignment);
               Realign (Sz, Align);
               Sz := Sz + (T_El'Size / System.Storage_Unit);
               --  The elements
               for I in Typ.Rec.E'Range loop
                  Add_Size_Type (Typ.Rec.E (I).Typ, Sz, Align);
               end loop;
            end;
         when Type_Slice =>
            raise Internal_Error;
      end case;
   end Add_Size_Type;

   --  Compute the memory size needed to store T.
   function Compute_Size_Type (T : Type_Acc) return Size_Type
   is
      Align : Size_Type;
      Size : Size_Type;
   begin
      Size := 0;
      Align := 1;
      Add_Size_Type (T, Size, Align);
      return Size;
   end Compute_Size_Type;

   function Create_Bit_Type return Type_Acc
   is
      subtype Bit_Type_Type is Type_Type (Type_Bit);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Bit_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Bit,
                                                Wkind => Wkind_Net,
                                                Drange => (Left => 0,
                                                           Right => 1,
                                                           Dir => Dir_To,
                                                           Is_Signed => False),
                                                Al => 0,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => 1,
                                                W => 1)));
   end Create_Bit_Type;

   function Create_Logic_Type return Type_Acc
   is
      subtype Logic_Type_Type is Type_Type (Type_Logic);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Logic_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Logic,
                                                Wkind => Wkind_Net,
                                                Drange => (Left => 0,
                                                           Right => 8,
                                                           Dir => Dir_To,
                                                           Is_Signed => False),
                                                Al => 0,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => 1,
                                                W => 1)));
   end Create_Logic_Type;

   function Create_Discrete_Type (Rng : Discrete_Range_Type;
                                  Sz : Size_Type;
                                  W : Uns32)
                                 return Type_Acc
   is
      subtype Discrete_Type_Type is Type_Type (Type_Discrete);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Discrete_Type_Type);
      Al : Palign_Type;
   begin
      if Sz <= 1 then
         Al := 0;
      elsif Sz <= 4 then
         Al := 2;
      else
         pragma Assert (Sz <= 8);
         Al := 3;
      end if;
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Discrete,
                                                Wkind => Wkind_Net,
                                                Al => Al,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => Sz,
                                                W => W,
                                                Drange => Rng)));
   end Create_Discrete_Type;

   function Create_Float_Type (Rng : Float_Range_Type) return Type_Acc
   is
      subtype Float_Type_Type is Type_Type (Type_Float);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Float_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Float,
                                                Wkind => Wkind_Net,
                                                Al => 3,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => 8,
                                                W => 64,
                                                Frange => Rng)));
   end Create_Float_Type;

   function Create_Vector_Type (Bnd : Bound_Type;
                                Static_Bnd : Boolean;
                                El_Type : Type_Acc) return Type_Acc
   is
      subtype Vector_Type_Type is Type_Type (Type_Vector);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Vector_Type_Type);
   begin
      pragma Assert (El_Type.Kind in Type_Nets);
      return To_Type_Acc
        (Alloc (Current_Pool, (Kind => Type_Vector,
                               Wkind => El_Type.Wkind,
                               Al => El_Type.Al,
                               Is_Global => False,
                               Is_Static => Static_Bnd,
                               Is_Bnd_Static => Static_Bnd,
                               Sz => El_Type.Sz * Size_Type (Bnd.Len),
                               W => Bnd.Len,
                               Alast => True,
                               Abound => Bnd,
                               Arr_El => El_Type)));
   end Create_Vector_Type;

   function Create_Slice_Type
     (Base_Type : Type_Acc; Len : Uns32; El_Type : Type_Acc) return Type_Acc
   is
      subtype Slice_Type_Type is Type_Type (Type_Slice);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Slice_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool,
                                 (Kind => Type_Slice,
                                  Wkind => El_Type.Wkind,
                                  Al => El_Type.Al,
                                  Is_Global => False,
                                  Is_Static => False,
                                  Is_Bnd_Static => False,
                                  Sz => Size_Type (Len) * El_Type.Sz,
                                  W => Len * El_Type.W,
                                  Slice_Base => Base_Type,
                                  Slice_Len => Len,
                                  Slice_El => El_Type)));
   end Create_Slice_Type;

   function Create_Vec_Type_By_Length (Len : Uns32; El : Type_Acc)
                                      return Type_Acc is
   begin
      return Create_Vector_Type ((Dir => Dir_Downto,
                                  Left => Int32 (Len) - 1,
                                  Right => 0,
                                  Len => Len),
                                 False,
                                 El);
   end Create_Vec_Type_By_Length;

   function Create_Array_Type (Bnd : Bound_Type;
                               Static_Bnd : Boolean;
                               Last : Boolean;
                               El_Type : Type_Acc) return Type_Acc
   is
      subtype Array_Type_Type is Type_Type (Type_Array);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Array_Type_Type);
      Is_Static : constant Boolean := Static_Bnd and El_Type.Is_Static;
   begin
      return To_Type_Acc (Alloc (Current_Pool,
                                 (Kind => Type_Array,
                                  Wkind => El_Type.Wkind,
                                  Al => El_Type.Al,
                                  Is_Global => False,
                                  Is_Static => Is_Static,
                                  Is_Bnd_Static => Static_Bnd,
                                  Sz => El_Type.Sz * Size_Type (Bnd.Len),
                                  W => El_Type.W * Bnd.Len,
                                  Abound => Bnd,
                                  Alast => Last,
                                  Arr_El => El_Type)));
   end Create_Array_Type;

   function Create_Array_Unbounded_Type (Bnd : Bound_Type;
                                         Static_Bnd : Boolean;
                                         Last : Boolean;
                                         El_Type : Type_Acc) return Type_Acc
   is
      subtype Array_Unbounded_Type_Type is Type_Type (Type_Array_Unbounded);
      function Alloc is
         new Areapools.Alloc_On_Pool_Addr (Array_Unbounded_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool,
                                 (Kind => Type_Array_Unbounded,
                                  Wkind => El_Type.Wkind,
                                  Al => El_Type.Al,
                                  Is_Global => False,
                                  Is_Static => False,
                                  Is_Bnd_Static => Static_Bnd,
                                  Sz => 0,
                                  W => 0,
                                  Abound => Bnd,
                                  Alast => Last,
                                  Arr_El => El_Type)));
   end Create_Array_Unbounded_Type;

   function Create_Unbounded_Array
     (Idx : Type_Acc; Last : Boolean; El_Type : Type_Acc) return Type_Acc
   is
      subtype Unbounded_Type_Type is Type_Type (Type_Unbounded_Array);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Unbounded_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Unbounded_Array,
                                                Wkind => El_Type.Wkind,
                                                Al => El_Type.Al,
                                                Is_Global => False,
                                                Is_Static => False,
                                                Is_Bnd_Static => False,
                                                Sz => 0,
                                                W => 0,
                                                Ulast => Last,
                                                Uarr_El => El_Type,
                                                Uarr_Idx => Idx)));
   end Create_Unbounded_Array;

   function Create_Array_From_Array_Unbounded
     (Parent : Type_Acc; El : Type_Acc) return Type_Acc is
   begin
      if Parent.Alast then
         return Create_Array_Type
           (Parent.Abound, Parent.Is_Bnd_Static, True, El);
      else
         return Create_Array_Type
           (Parent.Abound, Parent.Is_Bnd_Static, False,
            Create_Array_From_Array_Unbounded (Parent.Arr_El, El));
      end if;
   end Create_Array_From_Array_Unbounded;

   function Create_Unbounded_Vector (El_Type : Type_Acc; Idx : Type_Acc)
                                    return Type_Acc
   is
      subtype Unbounded_Type_Type is Type_Type (Type_Unbounded_Vector);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Unbounded_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Unbounded_Vector,
                                                Wkind => El_Type.Wkind,
                                                Al => El_Type.Al,
                                                Is_Global => False,
                                                Is_Static => False,
                                                Is_Bnd_Static => False,
                                                Sz => 0,
                                                W => 0,
                                                Ulast => True,
                                                Uarr_El => El_Type,
                                                Uarr_Idx => Idx)));
   end Create_Unbounded_Vector;

   function Get_Array_Element (Arr_Type : Type_Acc) return Type_Acc is
   begin
      case Arr_Type.Kind is
         when Type_Vector
           | Type_Array
           | Type_Array_Unbounded =>
            return Arr_Type.Arr_El;
         when Type_Unbounded_Array
           | Type_Unbounded_Vector =>
            return Arr_Type.Uarr_El;
         when others =>
            raise Internal_Error;
      end case;
   end Get_Array_Element;

   function Get_Array_Bound (Typ : Type_Acc) return Bound_Type is
   begin
      case Type_Vectors_Arrays (Typ.Kind) is
         when Type_Vector
           | Type_Array_Unbounded
           | Type_Array =>
            return Typ.Abound;
         when others =>
            raise Internal_Error;
      end case;
   end Get_Array_Bound;

   function Get_Uarray_Index (Typ : Type_Acc) return Type_Acc is
   begin
      case Typ.Kind is
         when Type_Unbounded_Vector
           | Type_Unbounded_Array =>
            return Typ.Uarr_Idx;
         when others =>
            raise Internal_Error;
      end case;
   end Get_Uarray_Index;

   function Get_Range_Length (Rng : Discrete_Range_Type) return Uns32
   is
      Len : Int64;
   begin
      case Rng.Dir is
         when Dir_To =>
            Len := Rng.Right - Rng.Left + 1;
         when Dir_Downto =>
            Len := Rng.Left - Rng.Right + 1;
      end case;
      if Len < 0 then
         return 0;
      elsif Len > Int64 (Uns32'Last) then
         --  Truncate very large lengths, such objects should not exist.
         return Uns32'Last;
      else
         return Uns32 (Len);
      end if;
   end Get_Range_Length;

   function Create_Rec_El_Array (Nels : Iir_Index32; Pool : Areapool_Acc)
                                return Rec_El_Array_Acc
   is
      subtype Data_Type is Rec_El_Array (Nels);
      Res : Address;
   begin
      --  Manually allocate the array to handle large arrays without
      --  creating a large temporary value.
      Areapools.Allocate
        (Pool.all, Res,
         Data_Type'Size / Storage_Unit, Data_Type'Alignment);

      declare
         --  Discard the warnings for no pragma Import as we really want
         --  to use the default initialization.
         pragma Warnings (Off);
         Addr1 : constant Address := Res;
         Init : Data_Type;
         for Init'Address use Addr1;
         pragma Warnings (On);
      begin
         null;
      end;

      return To_Rec_El_Array_Acc (Res);
   end Create_Rec_El_Array;

   function Create_Rec_El_Array (Nels : Iir_Index32) return Rec_El_Array_Acc is
   begin
      return Create_Rec_El_Array (Nels, Current_Pool);
   end Create_Rec_El_Array;

   function Align (Off : Size_Type; Al : Palign_Type) return Size_Type
   is
      Mask : constant Size_Type := 2 ** Natural (Al) - 1;
   begin
      return (Off + Mask) and not Mask;
   end Align;

   procedure Layout_Element_Mem (El : in out Rec_El_Type;
                                 Sz : in out Size_Type;
                                 Al : in out Palign_Type) is
   begin
      --  For memory.
      Al := Palign_Type'Max (Al, El.Typ.Al);
      Sz := Align (Sz, El.Typ.Al);
      El.Offs.Mem_Off := Sz;
      Sz := Sz + El.Typ.Sz;
   end Layout_Element_Mem;

   procedure Layout_Element_Net (El : in out Rec_El_Type;
                                 W : in out Uns32;
                                 Wkind : in out Wkind_Type) is
   begin
      --  For nets.
      El.Offs.Net_Off := W;
      if El.Typ.Wkind /= Wkind_Net then
         Wkind := Wkind_Undef;
      end if;
      W := W + El.Typ.W;
   end Layout_Element_Net;

   function Create_Record_Type (Parent_Typ : Type_Acc;
                                Els : Rec_El_Array_Acc) return Type_Acc
   is
      subtype Record_Type_Type is Type_Type (Type_Record);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Record_Type_Type);
      Base : Type_Acc;
      Base_Els : Rec_El_Array_Acc;
      Wkind : Wkind_Type;
      W : Uns32;
      Al : Palign_Type;
      Sz : Size_Type;
      Res : Type_Acc;
   begin
      --  Layout the record.
      if Parent_Typ = null then
         Al := 0;
         Sz := 0;
         --  First elements with static types, then the others.
         for Static in reverse Boolean loop
            for I in Els.E'Range loop
               declare
                  El : Rec_El_Type renames Els.E (I);
               begin
                  if El.Typ.Is_Static = Static then
                     Layout_Element_Mem (El, Sz, Al);
                  end if;
               end;
            end loop;
         end loop;
         Sz := Align (Sz, Al);

      else
         Base := Parent_Typ.Rec_Base;
         Base_Els := Base.Rec;
         Al := Base.Al;
         Sz := Base.Sz;
         --  Only the non-static types.
         for I in Els.E'Range loop
            if Base_Els.E (I).Typ.Is_Static then
               Els.E (I).Offs.Mem_Off := Base_Els.E (I).Offs.Mem_Off;
            else
               Layout_Element_Mem (Els.E (I), Sz, Al);
            end if;
         end loop;
      end if;
      Sz := Align (Sz, Al);

      --  Layout nets.
      Wkind := Wkind_Net;
      W := 0;
      for I in Els.E'Range loop
         Layout_Element_Net (Els.E (I), W, Wkind);
      end loop;
      Res := To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Record,
                                                Wkind => Wkind,
                                                Al => Al,
                                                Is_Global => False,
                                                Is_Static => False,
                                                Is_Bnd_Static => False,
                                                Sz => Sz,
                                                W => W,
                                                Rec_Base => null,
                                                Rec => Els)));
      if Parent_Typ = null then
         Res.Rec_Base := Res;
      else
         Res.Rec_Base := Base;
      end if;

      return Res;
   end Create_Record_Type;

   function Create_Unbounded_Record (Parent_Typ : Type_Acc;
                                     Els : Rec_El_Array_Acc) return Type_Acc
   is
      subtype Unbounded_Record_Type_Type is Type_Type (Type_Unbounded_Record);
      function Alloc is
         new Areapools.Alloc_On_Pool_Addr (Unbounded_Record_Type_Type);
      Base : Type_Acc;
      Base_Els : Rec_El_Array_Acc;
      Wkind : Wkind_Type;
      W : Uns32;
      Al : Palign_Type;
      Sz : Size_Type;
      Res : Type_Acc;
   begin
      --  Layout the record.
      Wkind := Wkind_Net;
      W := 0;
      if Parent_Typ = null then
         --  Layout only static elements.
         Al := 0;
         Sz := 0;
         for I in Els.E'Range loop
            declare
               El : Rec_El_Type renames Els.E (I);
            begin
               if El.Typ.Is_Static then
                  Layout_Element_Mem (El, Sz, Al);
                  El.Offs.Net_Off := 0;
               else
                  El.Offs := No_Value_Offsets;
               end if;
            end;
         end loop;
      else
         --  Copy layout of base type.
         Base := Parent_Typ.Rec_Base;
         Base_Els := Base.Rec;
         Al := Base.Al;
         Sz := Base.Sz;
         for I in Els.E'Range loop
            Els.E (I).Offs := Base_Els.E (I).Offs;
         end loop;
      end if;

      Res := To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Unbounded_Record,
                                                Wkind => Wkind,
                                                Al => Al,
                                                Is_Global => False,
                                                Is_Static => False,
                                                Is_Bnd_Static => False,
                                                Sz => Sz,
                                                W => W,
                                                Rec_Base => null,
                                                Rec => Els)));
      if Parent_Typ = null then
         Res.Rec_Base := Res;
      else
         Res.Rec_Base := Parent_Typ.Rec_Base;
      end if;
      return Res;
   end Create_Unbounded_Record;

   --  Compute size and alignment for bounds of TYP.
   procedure Update_Bounds_Size (Typ : Type_Acc;
                                 Sz : in out Size_Type;
                                 Al : in out Palign_Type);

   procedure Update_Layout_Size (Typ : Type_Acc;
                                 Sz : in out Size_Type;
                                 Al : in out Palign_Type) is
   begin
      case Typ.Kind is
         when Type_Scalars
           | Type_Array
           | Type_Vector
           | Type_Record
           | Type_Access =>
            null;
         when Type_Unbounded_Vector
           | Type_Unbounded_Array =>
            declare
               B_Sz : Size_Type;
               B_Al : Palign_Type;
            begin
               --  Layout of an array is sizes + bounds.
               B_Sz := 2 * Ghdl_Index_Sz;
               B_Al := Ghdl_Index_Al;
               Update_Bounds_Size (Typ, B_Sz, B_Al);
               Sz := Align (Sz, B_Al);
               Sz := Sz + B_Sz;
               Al := Palign_Type'Max (Al, B_Al);
            end;
         when Type_Unbounded_Record
           | Type_Array_Unbounded =>
            --  TODO
            raise Internal_Error;
         when Type_Slice
           | Type_File
           | Type_Protected =>
            raise Internal_Error;
      end case;
   end Update_Layout_Size;

   procedure Update_Bounds_Size (Typ : Type_Acc;
                                 Sz : in out Size_Type;
                                 Al : in out Palign_Type) is
   begin
      case Typ.Kind is
         when Type_Scalars
           | Type_Array
           | Type_Vector
           | Type_Record
           | Type_Access =>
            null;
         when Type_Array_Unbounded =>
            Update_Bounds_Size (Typ.Arr_El, Sz, Al);
         when Type_Unbounded_Array
           | Type_Unbounded_Vector =>
            declare
               Idx : constant Type_Acc := Typ.Uarr_Idx;
               B_Sz : Size_Type;
               B_Al : Palign_Type;
            begin
               --  Compute size of left, right and dir fields.
               case Idx.Sz is
                  when 1 =>
                     B_Sz := 3;
                     B_Al := 0;
                  when 4 =>
                     B_Sz := 9;
                     B_Al := 2;
                  when 8 =>
                     B_Sz := 17;
                     B_Al := 2;
                  when others =>
                     raise Internal_Error;
               end case;
               --  Add length field.
               Sz := Align (Sz, Ghdl_Index_Al);
               B_Sz := B_Sz + Ghdl_Index_Sz;
               --  Compute whole alignment.
               B_Al := Palign_Type'Max (3, Ghdl_Index_Al);
               B_Sz := Align (B_Sz, B_Al);
               --  Add to the result.
               Sz := Align (Sz, B_Al);
               Sz := Sz + B_Sz;

               if not Typ.Ulast then
                  --  Continue with next index.
                  Update_Bounds_Size (Typ.Uarr_El, Sz, Al);
               else
                  --  Continue with the element.
                  Update_Layout_Size (Typ.Uarr_El, Sz, Al);
               end if;

            end;
         when Type_Unbounded_Record =>
            --  TODO
            raise Internal_Error;
         when Type_Slice
           | Type_File
           | Type_Protected =>
            raise Internal_Error;
      end case;
   end Update_Bounds_Size;

   function Compute_Bounds_Size (Typ : Type_Acc) return Size_Type
   is
      Res : Size_Type;
      Al : Palign_Type;
   begin
      Res := 0;
      Al := 0;
      Update_Bounds_Size (Typ, Res, Al);
      return Res;
   end Compute_Bounds_Size;

   function Create_Access_Type (Acc_Type : Type_Acc) return Type_Acc
   is
      subtype Access_Type_Type is Type_Type (Type_Access);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Access_Type_Type);
      Type_Sz : Size_Type;
      Bnd_Sz : Size_Type;
   begin
      if Acc_Type = null then
         --  For incomplete type.
         Type_Sz := 0;
         Bnd_Sz := 0;
      else
         Type_Sz := Compute_Size_Type (Acc_Type);
         Bnd_Sz := Compute_Bounds_Size (Acc_Type);
      end if;
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Access,
                                                Wkind => Wkind_Sim,
                                                Al => Heap_Ptr_Al,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => Heap_Ptr_Sz,
                                                W => 1,
                                                Acc_Acc => Acc_Type,
                                                Acc_Type_Sz => Type_Sz,
                                                Acc_Bnd_Sz => Bnd_Sz)));
   end Create_Access_Type;

   procedure Complete_Access_Type (Acc_Type : Type_Acc; Des_Typ : Type_Acc) is
   begin
      Acc_Type.Acc_Acc := Des_Typ;
      Acc_Type.Acc_Type_Sz := Compute_Size_Type (Des_Typ);
      Acc_Type.Acc_Bnd_Sz := Compute_Bounds_Size (Des_Typ);
   end Complete_Access_Type;

   function Create_File_Type (File_Type : Type_Acc) return Type_Acc
   is
      subtype File_Type_Type is Type_Type (Type_File);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (File_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_File,
                                                Wkind => Wkind_Sim,
                                                Al => 2,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => 4,
                                                W => 1,
                                                File_Typ => File_Type,
                                                File_Signature => null)));
   end Create_File_Type;

   function Create_Protected_Type return Type_Acc
   is
      subtype Protected_Type_Type is Type_Type (Type_Protected);
      function Alloc is new Areapools.Alloc_On_Pool_Addr (Protected_Type_Type);
   begin
      return To_Type_Acc (Alloc (Current_Pool, (Kind => Type_Protected,
                                                Wkind => Wkind_Sim,
                                                Al => 2,
                                                Is_Global => False,
                                                Is_Static => True,
                                                Is_Bnd_Static => True,
                                                Sz => 4,
                                                W => 1)));
   end Create_Protected_Type;

   function Vec_Length (Typ : Type_Acc) return Iir_Index32 is
   begin
      return Iir_Index32 (Typ.Abound.Len);
   end Vec_Length;

   function Get_Array_Flat_Length (Typ : Type_Acc) return Iir_Index32 is
   begin
      case Type_Vectors_Arrays (Typ.Kind) is
         when Type_Vector =>
            return Iir_Index32 (Typ.Abound.Len);
         when Type_Array
           | Type_Array_Unbounded =>
            declare
               Len : Uns32;
               T : Type_Acc;
            begin
               Len := 1;
               T := Typ;
               loop
                  Len := Len * T.Abound.Len;
                  exit when T.Alast;
                  T := T.Arr_El;
               end loop;
               return Iir_Index32 (Len);
            end;
         when others =>
            raise Internal_Error;
      end case;
   end Get_Array_Flat_Length;

   function Get_Type_Width (Atype : Type_Acc) return Uns32 is
   begin
      pragma Assert (Atype.Kind /= Type_Unbounded_Array);
      return Atype.W;
   end Get_Type_Width;

   function Get_Bound_Length (T : Type_Acc) return Uns32 is
   begin
      case T.Kind is
         when Type_Vector
           | Type_Array =>
            return T.Abound.Len;
         when Type_Slice =>
            return T.W;
         when others =>
            raise Internal_Error;
      end case;
   end Get_Bound_Length;

   function Is_Matching_Bounds (L, R : Type_Acc) return Boolean is
   begin
      case L.Kind is
         when Type_Bit
           | Type_Logic
           | Type_Discrete
           | Type_Float =>
            pragma Assert (L.Kind = R.Kind);
            return True;
         when Type_Vector
           | Type_Slice =>
            return Get_Bound_Length (L) = Get_Bound_Length (R);
         when Type_Array =>
            pragma Assert (L.Alast = R.Alast);
            if Get_Bound_Length (L) /= Get_Bound_Length (R) then
               return False;
            end if;
            if L.Alast then
               return True;
            end if;
            return Get_Bound_Length (L.Arr_El) = Get_Bound_Length (R.Arr_El);
         when Type_Array_Unbounded
            | Type_Unbounded_Array
            | Type_Unbounded_Vector
            | Type_Unbounded_Record =>
            raise Internal_Error;
         when Type_Record =>
            --  FIXME: handle vhdl-08
            return True;
         when Type_Access =>
            return True;
         when Type_File
           |  Type_Protected =>
            raise Internal_Error;
      end case;
   end Is_Matching_Bounds;

   function Read_U8 (Mt : Memtyp) return Ghdl_U8
   is
      pragma Assert (Mt.Typ.Sz = 1);
   begin
      return Read_U8 (Mt.Mem);
   end Read_U8;


   function Read_Fp64 (Mt : Memtyp) return Fp64 is
   begin
      return Read_Fp64 (Mt.Mem);
   end Read_Fp64;

   function Read_Discrete (Mem : Memory_Ptr; Typ : Type_Acc) return Int64 is
   begin
      case Typ.Sz is
         when 1 =>
            return Int64 (Read_U8 (Mem));
         when 4 =>
            return Int64 (Read_I32 (Mem));
         when 8 =>
            return Int64 (Read_I64 (Mem));
         when others =>
            raise Internal_Error;
      end case;
   end Read_Discrete;

   function Read_Discrete (Mt : Memtyp) return Int64 is
   begin
      return Read_Discrete (Mt.Mem, Mt.Typ);
   end Read_Discrete;

   procedure Write_Discrete (Mem : Memory_Ptr; Typ : Type_Acc; Val : Int64) is
   begin
      case Typ.Sz is
         when 1 =>
            Write_U8 (Mem, Ghdl_U8 (Val));
         when 4 =>
            Write_I32 (Mem, Ghdl_I32 (Val));
         when 8 =>
            Write_I64 (Mem, Ghdl_I64 (Val));
         when others =>
            raise Internal_Error;
      end case;
   end Write_Discrete;

   function Alloc_Memory (Sz : Size_Type;
                          Align2 : Natural;
                          Pool : Areapool_Acc) return Memory_Ptr
   is
      function To_Memory_Ptr is new Ada.Unchecked_Conversion
        (System.Address, Memory_Ptr);
      M : System.Address;
   begin
      Areapools.Allocate (Pool.all, M, Sz, Size_Type (2 ** Align2));
      return To_Memory_Ptr (M);
   end Alloc_Memory;

   function Alloc_Memory (Vtype : Type_Acc; Pool : Areapool_Acc)
                         return Memory_Ptr is
   begin
      return Alloc_Memory (Vtype.Sz, Natural (Vtype.Al), Pool);
   end Alloc_Memory;

   function Create_Memory (Vtype : Type_Acc) return Memtyp is
   begin
      return (Vtype, Alloc_Memory (Vtype, Current_Pool));
   end Create_Memory;

   function Create_Memory_Zero (Vtype : Type_Acc) return Memtyp
   is
      Mem : Memory_Ptr;
   begin
      Mem := Alloc_Memory (Vtype, Current_Pool);
      for I in 1 .. Vtype.Sz loop
         Write_U8 (Mem + (I - 1), 0);
      end loop;
      return (Vtype, Mem);
   end Create_Memory_Zero;

   function Create_Memory_U8 (Val : Ghdl_U8; Vtype : Type_Acc)
                             return Memtyp
   is
      pragma Assert (Vtype.Sz = 1);
      Res : Memory_Ptr;
   begin
      Res := Alloc_Memory (Vtype, Current_Pool);
      Write_U8 (Res, Val);
      return (Vtype, Res);
   end Create_Memory_U8;

   function Create_Memory_Fp64 (Val : Fp64; Vtype : Type_Acc)
                               return Memtyp
   is
      pragma Assert (Vtype.Sz = 8);
      Res : Memory_Ptr;
   begin
      Res := Alloc_Memory (Vtype, Current_Pool);
      Write_Fp64 (Res, Val);
      return (Vtype, Res);
   end Create_Memory_Fp64;

   function Create_Memory_Discrete (Val : Int64; Vtype : Type_Acc)
                                   return Memtyp
   is
      Res : Memory_Ptr;
   begin
      Res := Alloc_Memory (Vtype, Current_Pool);
      case Vtype.Sz is
         when 1 =>
            Write_U8 (Res, Ghdl_U8 (Val));
         when 4 =>
            Write_I32 (Res, Ghdl_I32 (Val));
         when 8 =>
            Write_I64 (Res, Ghdl_I64 (Val));
         when others =>
            raise Internal_Error;
      end case;
      return (Vtype, Res);
   end Create_Memory_Discrete;

   function Create_Memory_U32 (Val : Uns32) return Memtyp
   is
      Res : Memory_Ptr;
   begin
      Res := Alloc_Memory (4, 2, Current_Pool);
      Write_U32 (Res, Ghdl_U32 (Val));
      return (null, Res);
   end Create_Memory_U32;

   function Is_Equal (L, R : Memtyp) return Boolean is
   begin
      if L = R then
         return True;
      end if;

      if L.Typ.Sz /= R.Typ.Sz then
         return False;
      end if;

      case L.Typ.Kind is
         when Type_Bit
           | Type_Logic =>
            return L.Mem (0) = R.Mem (0);
         when Type_Discrete =>
            return Read_Discrete (L.Mem, L.Typ) = Read_Discrete (R.Mem, R.Typ);
         when Type_Float =>
            return Read_Fp64 (L.Mem) = Read_Fp64 (R.Mem);
         when Type_Vector =>
            pragma Assert (L.Typ.Arr_El.Sz = 1);
            for I in 1 .. Size_Type (L.Typ.Abound.Len) loop
               if L.Mem (I - 1) /= R.Mem (I - 1) then
                  return False;
               end if;
            end loop;
            return True;
         when Type_Array =>
            declare
               Etl, Etr : Type_Acc;
               Len : Uns32;
               Off : Size_Type;
            begin
               Len := 1;
               Etl := L.Typ;
               Etr := R.Typ;
               loop
                  if Etl.Abound.Len /= Etr.Abound.Len then
                     return False;
                  end if;
                  Len := Len * Etl.Abound.Len;
                  exit when Etl.Alast;
                  Etl := Etl.Arr_El;
                  Etr := Etr.Arr_El;
               end loop;
               Etl := Etl.Arr_El;
               Etr := Etr.Arr_El;
               Off := 0;
               for I in 1 .. Len loop
                  if not Is_Equal ((Etl, L.Mem + Off),
                                   (Etr, R.Mem + Off))
                  then
                     return False;
                  end if;
                  Off := Off + Etl.Sz;
               end loop;
               return True;
            end;
         when Type_Record =>
            for I in L.Typ.Rec.E'Range loop
               declare
                  El : Rec_El_Type renames L.Typ.Rec.E (I);
               begin
                  if not Is_Equal ((El.Typ, L.Mem + El.Offs.Mem_Off),
                                   (El.Typ, R.Mem + El.Offs.Mem_Off))
                  then
                     return False;
                  end if;
               end;
            end loop;
            return True;
         when Type_Access =>
            pragma Assert (L.Typ.Sz = 4);
            return Read_U32 (L.Mem) = Read_U32 (R.Mem);
         when Type_Slice =>
            raise Internal_Error;
         when Type_Unbounded_Vector
           | Type_Unbounded_Array
           | Type_Array_Unbounded
           | Type_Unbounded_Record
           | Type_Protected
           | Type_File =>
            raise Internal_Error;
      end case;
   end Is_Equal;

   procedure Copy_Memory (Dest : Memory_Ptr; Src : Memory_Ptr; Sz : Size_Type)
   is
   begin
      for I in 1 .. Sz loop
         Dest (I - 1) := Src (I - 1);
      end loop;
   end Copy_Memory;

   function Unshare (Src : Memtyp; Pool : Areapool_Acc) return Memtyp
   is
      Res : Memory_Ptr;
   begin
      Res := Alloc_Memory (Src.Typ, Pool);
      Copy_Memory (Res, Src.Mem, Src.Typ.Sz);
      return (Src.Typ, Res);
   end Unshare;

   function Unshare (Src : Memtyp) return Memtyp
   is
      Res : Memory_Ptr;
   begin
      Res := Alloc_Memory (Src.Typ, Current_Pool);
      Copy_Memory (Res, Src.Mem, Src.Typ.Sz);
      return (Src.Typ, Res);
   end Unshare;

   function Raw_Copy (T : Type_Acc; Pool : Areapool_Acc) return Type_Acc
   is
      Addr : System.Address;
      Sz : Size_Type;
   begin
      Sz := T.all'Size / Storage_Unit;
      Allocate (Pool.all, Addr, Sz, T.all'Alignment);
      Copy_Memory (To_Memory_Ptr (Addr), To_Memory_Ptr (T.all'Address), Sz);
      return To_Type_Acc (Addr);
   end Raw_Copy;

   function Unshare (T : Type_Acc; Pool : Areapool_Acc) return Type_Acc
   is
      Res : Type_Acc;
   begin
      if T.Is_Global then
         return T;
      end if;

      Res := Raw_Copy (T, Pool);
      Res.Is_Global := True;

      case Res.Kind is
         when Type_Bit
            | Type_Logic
            | Type_Discrete
            | Type_Float =>
            null;
         when Type_Slice =>
            Res.Slice_El := Unshare (T.Slice_El, Pool);
         when Type_Array
            | Type_Array_Unbounded
            | Type_Vector =>
            Res.Arr_El := Unshare (T.Arr_El, Pool);
         when Type_Unbounded_Array
            | Type_Unbounded_Vector =>
            Res.Uarr_El := Unshare (T.Uarr_El, Pool);
            Res.Uarr_Idx := Unshare (T.Uarr_Idx, Pool);
         when Type_Record
            | Type_Unbounded_Record =>
            Res.Rec := Create_Rec_El_Array (T.Rec.Len, Pool);
            for I in T.Rec.E'Range loop
               Res.Rec.E (I) := (Offs => T.Rec.E (I).Offs,
                                 Typ => Unshare (T.Rec.E (I).Typ, Pool));
            end loop;
            if T.Rec_Base = T then
               Res.Rec_Base := Res;
            end if;
         when Type_Access =>
            if T.Acc_Acc /= null then
               Res.Acc_Acc := Unshare (T.Acc_Acc, Pool);
            else
               --  For incomplete types
               Res.Acc_Acc := null;
            end if;
         when Type_File =>
            Res.File_Typ := Unshare (T.File_Typ, Pool);
         when Type_Protected =>
            raise Internal_Error;
      end case;
      return Res;
   end Unshare;

   function Unshare_Type (Typ : Type_Acc;
                          Base : Type_Acc;
                          Global : Boolean;
                          Pool : Areapool_Acc) return Type_Acc
   is
      Res : Type_Acc;
   begin
      if Typ = Base then
         return Typ;
      end if;
      if Typ.Is_Global /= Global then
         Res := Raw_Copy (Typ, Pool);
         Res.Is_Global := Global;
      elsif Global then
         return Typ;
      else
         --  We want a local copy, but the original local type can have
         --  global sub-elements.
         Res := Typ;
      end if;

      case Res.Kind is
         when Type_Bit
           | Type_Logic
           | Type_Discrete
           | Type_Float =>
            null;
         when Type_Slice =>
            Res.Slice_El := Unshare_Type (Typ.Slice_El,
                                          Get_Array_Element (Base),
                                          Global, Pool);
         when Type_Array
           | Type_Vector =>
            Res.Arr_El := Unshare_Type (Typ.Arr_El,
                                        Get_Array_Element (Base),
                                        Global, Pool);
         when Type_Array_Unbounded
           | Type_Unbounded_Array
           | Type_Unbounded_Vector
           | Type_Unbounded_Record =>
            raise Internal_Error;
         when Type_Record =>
            if Typ /= Res then
               Res.Rec := Create_Rec_El_Array (Typ.Rec.Len, Pool);
            end if;
            for I in Typ.Rec.E'Range loop
               Res.Rec.E (I) := (Offs => Typ.Rec.E (I).Offs,
                                 Typ => Unshare_Type (Typ.Rec.E (I).Typ,
                                                      Base.Rec.E (I).Typ,
                                                      Global, Pool));
            end loop;
         when Type_Access =>
            raise Internal_Error;
         when Type_File =>
            raise Internal_Error;
         when Type_Protected =>
            raise Internal_Error;
      end case;
      return Res;
   end Unshare_Type;

   function Unshare_Type_Expr (Typ : Type_Acc; Base : Type_Acc)
                              return Type_Acc is
   begin
      return Unshare_Type (Typ, Base, False, Expr_Pool'Access);
   end Unshare_Type_Expr;

   function Unshare_Type_Instance (Typ : Type_Acc; Base : Type_Acc)
                                  return Type_Acc is
   begin
      return Unshare_Type (Typ, Base, True, Instance_Pool);
   end Unshare_Type_Instance;

   procedure Save_Type (Typ : Type_Acc;
                        Res : out Type_Acc;
                        Mem : Memory_Ptr;
                        Off : in out Size_Type;
                        Mem_Sz : Size_Type)
   is
      Sz : constant Size_Type := Typ.all'Size / Storage_Unit;
      Raw_Res : Address;
   begin
      --  Don't copy scalar types.
      case Typ.Kind is
         when Type_Bit
           | Type_Logic
           | Type_Discrete
           | Type_Float
           | Type_Access =>
            Res := Typ;
            return;
         when others =>
            null;
      end case;

      --  Copy Typ.
      Realign (Off, Typ.all'Alignment);
      pragma Assert (Off + Sz <= Mem_Sz);
      Raw_Res := To_Address (Mem + Off);
      Off := Off + Sz;
      Res := To_Type_Acc (Raw_Res);
      Copy_Memory (To_Memory_Ptr (Raw_Res),
                   To_Memory_Ptr (Typ.all'Address), Sz);
      Res.Is_Global := True;

      --  Copy elements.
      case Res.Kind is
         when Type_Bit
            | Type_Logic
            | Type_Discrete
            | Type_Float =>
            raise Internal_Error;
         when Type_Slice =>
            raise Internal_Error;
         when Type_Array
            | Type_Vector =>
            Save_Type (Typ.Arr_El, Res.Arr_El, Mem, Off, Mem_Sz);
         when Type_Unbounded_Array
            | Type_Array_Unbounded
            | Type_Unbounded_Vector
            | Type_Unbounded_Record =>
            raise Internal_Error;
         when Type_Record =>
            declare
               subtype Data_Type is Rec_El_Array (Typ.Rec.Len);
               Rec_Sz : constant Size_Type := Data_Type'Size / Storage_Unit;
            begin
               Realign (Off, Data_Type'Alignment);
               pragma Assert (Off + Rec_Sz <= Mem_Sz);
               Raw_Res := To_Address (Mem + Off);
               Off := Off + Rec_Sz;
               Copy_Memory (To_Memory_Ptr (Raw_Res),
                            To_Memory_Ptr (Typ.Rec.all'Address), Rec_Sz);
               Res.Rec := To_Rec_El_Array_Acc (Raw_Res);
               for I in Typ.Rec.E'Range loop
                  Res.Rec.E (I).Offs := Typ.Rec.E (I).Offs;
                  Save_Type (Res.Rec.E (I).Typ,
                             Typ.Rec.E (I).Typ,
                             Mem, Off, Mem_Sz);
               end loop;
            end;
         when Type_Access =>
            raise Internal_Error;
         when Type_File =>
            raise Internal_Error;
         when Type_Protected =>
            raise Internal_Error;
      end case;
   end Save_Type;

   function Save_Type (Typ : Type_Acc;
                       Mem : Memory_Ptr;
                       Mem_Sz : Size_Type) return Type_Acc
   is
      Off : Size_Type;
      Res : Type_Acc;
   begin
      Off := 0;
      Save_Type (Typ, Res, Mem, Off, Mem_Sz);
      pragma Assert (Off <= Mem_Sz);
      return Res;
   end Save_Type;

   procedure Mark_Expr_Pool (M : out Mark_Type) is
   begin
      Mark (M, Expr_Pool);
   end Mark_Expr_Pool;

   procedure Release_Expr_Pool (M : Mark_Type) is
   begin
      Release (M, Expr_Pool);
   end Release_Expr_Pool;

   function Is_Expr_Pool_Empty return Boolean is
   begin
      return Is_Empty (Expr_Pool);
   end Is_Expr_Pool_Empty;

   Bit0_Mem : constant Memory_Element := 0;
   Bit1_Mem : constant Memory_Element := 1;

   function To_Memory_Ptr is new Ada.Unchecked_Conversion
     (Address, Memory_Ptr);

   procedure Initialize is
   begin
      if Boolean_Type /= null then
         --  Restarting.  Free the global pool.
         Release (Empty_Marker, Global_Pool);
      end if;

      --  Alloc fundamental types (on the global pool).
      Current_Pool := Global_Pool'Access;
      Boolean_Type := Create_Bit_Type;
      Logic_Type := Create_Logic_Type;
      Bit_Type := Create_Bit_Type;
      Protected_Type := Create_Protected_Type;

      Boolean_Type.Is_Global := True;
      Logic_Type.Is_Global := True;
      Bit_Type.Is_Global := True;
      Protected_Type.Is_Global := True;

      Current_Pool := Expr_Pool'Access;

      Bit0 := (Bit_Type, To_Memory_Ptr (Bit0_Mem'Address));
      Bit1 := (Bit_Type, To_Memory_Ptr (Bit1_Mem'Address));
   end Initialize;

   procedure Finalize is
   begin
      pragma Assert (Boolean_Type /= null);
      Release (Empty_Marker, Global_Pool);

      Instance_Pool := null;
      Boolean_Type := null;
      Logic_Type := null;
      Bit_Type := null;
      Protected_Type := null;

      Bit0 := Null_Memtyp;
      Bit1 := Null_Memtyp;
   end Finalize;
end Elab.Vhdl_Objtypes;