aboutsummaryrefslogtreecommitdiffstats
path: root/src/synth/netlists.adb
blob: 59c77e831f963a21b30a29cdb8b6c0b4c68ae493 (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
--  Netlist.
--  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, write to the Free Software
--  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
--  MA 02110-1301, USA.

with Std_Names;
with Name_Table;
with Tables;
with Simple_IO;

with Netlists.Utils; use Netlists.Utils;
with Netlists.Gates;

package body Netlists is

   --  Names

   package Snames_Table is new Tables
     (Table_Component_Type => Sname_Record,
      Table_Index_Type => Sname,
      Table_Low_Bound => 0,
      Table_Initial => 1024);

   function New_Sname_User (Id : Name_Id; Prefix : Sname) return Sname is
   begin
      Snames_Table.Append ((Kind => Sname_User,
                            Prefix => Prefix,
                            Suffix => Uns32 (Id)));
      return Snames_Table.Last;
   end New_Sname_User;

   function New_Sname_Artificial (Id : Name_Id; Prefix : Sname) return Sname is
   begin
      Snames_Table.Append ((Kind => Sname_Artificial,
                            Prefix => Prefix,
                            Suffix => Uns32 (Id)));
      return Snames_Table.Last;
   end New_Sname_Artificial;

   function New_Sname_Version (Ver : Uns32; Prefix : Sname) return Sname is
   begin
      Snames_Table.Append ((Kind => Sname_Version,
                            Prefix => Prefix,
                            Suffix => Ver));
      return Snames_Table.Last;
   end New_Sname_Version;

   function Is_Valid (Name : Sname) return Boolean is
   begin
      return Name > No_Sname and Name <= Snames_Table.Last;
   end Is_Valid;

   function Get_Sname_Kind (Name : Sname) return Sname_Kind is
   begin
      pragma Assert (Is_Valid (Name));
      return Snames_Table.Table (Name).Kind;
   end Get_Sname_Kind;

   function Get_Sname_Prefix (Name : Sname) return Sname is
   begin
      pragma Assert (Is_Valid (Name));
      return Snames_Table.Table (Name).Prefix;
   end Get_Sname_Prefix;

   procedure Set_Sname_Prefix (Name : Sname; Prefix : Sname) is
   begin
      pragma Assert (Is_Valid (Name));
      Snames_Table.Table (Name).Prefix := Prefix;
   end Set_Sname_Prefix;

   function Get_Sname_Suffix (Name : Sname) return Name_Id
   is
      subtype Snames_Suffix is Sname_Kind range Sname_User .. Sname_Artificial;
   begin
      pragma Assert (Is_Valid (Name));
      pragma Assert (Get_Sname_Kind (Name) in Snames_Suffix);
      return Name_Id (Snames_Table.Table (Name).Suffix);
   end Get_Sname_Suffix;

   function Get_Sname_Version (Name : Sname) return Uns32 is
   begin
      pragma Assert (Is_Valid (Name));
      pragma Assert (Get_Sname_Kind (Name) = Sname_Version);
      return Snames_Table.Table (Name).Suffix;
   end Get_Sname_Version;


   --  Modules

   package Modules_Table is new Tables
     (Table_Component_Type => Module_Record,
      Table_Index_Type => Module,
      Table_Low_Bound => No_Module,
      Table_Initial => 1024);

   package Port_Desc_Table is new Tables
     (Table_Component_Type => Port_Desc,
      Table_Index_Type => Port_Desc_Idx,
      Table_Low_Bound => No_Port_Desc_Idx,
      Table_Initial => 1024);

   function New_Design (Name : Sname) return Module
   is
      Res : Module;
      Self : Instance;
   begin
      Modules_Table.Append ((Parent => No_Module,
                             Name => Name,
                             Id => Id_Design,
                             First_Port_Desc => No_Port_Desc_Idx,
                             Nbr_Inputs => 0,
                             Nbr_Outputs => 0,
                             First_Param_Desc => No_Param_Desc_Idx,
                             Nbr_Params => 0,
                             First_Sub_Module => No_Module,
                             Last_Sub_Module => No_Module,
                             Next_Sub_Module => No_Module,
                             First_Instance => No_Instance,
                             Last_Instance => No_Instance,
                             Attrs => null));
      Res := Modules_Table.Last;
      Self := Create_Self_Instance (Res);
      pragma Unreferenced (Self);

      return Res;
   end New_Design;

   function Is_Valid (M : Module) return Boolean is
   begin
      return M > No_Module and then M <= Modules_Table.Last;
   end Is_Valid;

   function New_User_Module (Parent : Module;
                             Name : Sname;
                             Id : Module_Id;
                             Nbr_Inputs : Port_Nbr;
                             Nbr_Outputs : Port_Nbr;
                             Nbr_Params : Param_Nbr := 0)
                            return Module
   is
      pragma Assert (Is_Valid (Parent));
      Parent_Rec : Module_Record renames Modules_Table.Table (Parent);
      Ports_Desc : Port_Desc_Idx;
      Res : Module;
   begin
      Ports_Desc := Port_Desc_Table.Last + 1;
      for I in 1 .. Nbr_Inputs + Nbr_Outputs loop
         Port_Desc_Table.Append
           ((Name => No_Sname, Is_Inout => False, W => 0));
      end loop;

      Modules_Table.Append
        ((Parent => Parent,
          Name => Name,
          Id => Id,
          First_Port_Desc => Ports_Desc,
          Nbr_Inputs => Nbr_Inputs,
          Nbr_Outputs => Nbr_Outputs,
          First_Param_Desc => No_Param_Desc_Idx,
          Nbr_Params => Nbr_Params,
          First_Sub_Module => No_Module,
          Last_Sub_Module => No_Module,
          Next_Sub_Module => No_Module,
          First_Instance => No_Instance,
          Last_Instance => No_Instance,
          Attrs => null));
      Res := Modules_Table.Last;

      --  Append
      if Parent_Rec.First_Sub_Module = No_Module then
         Parent_Rec.First_Sub_Module := Res;
      else
         Modules_Table.Table (Parent_Rec.Last_Sub_Module).Next_Sub_Module :=
           Res;
      end if;
      Parent_Rec.Last_Sub_Module := Res;

      return Res;
   end New_User_Module;

   function Get_Module_Name (M : Module) return Sname is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).Name;
   end Get_Module_Name;

   function Get_Id (M : Module) return Module_Id is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).Id;
   end Get_Id;

   function Get_Nbr_Inputs (M : Module) return Port_Nbr is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).Nbr_Inputs;
   end Get_Nbr_Inputs;

   function Get_Nbr_Outputs (M : Module) return Port_Nbr is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).Nbr_Outputs;
   end Get_Nbr_Outputs;

   function Get_Nbr_Params (M : Module) return Param_Nbr is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).Nbr_Params;
   end Get_Nbr_Params;

   function Get_First_Port_Desc (M : Module) return Port_Desc_Idx is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).First_Port_Desc;
   end Get_First_Port_Desc;

   function Get_Input_First_Desc (M : Module) return Port_Desc_Idx
   is
      pragma Assert (Is_Valid (M));
   begin
      return Modules_Table.Table (M).First_Port_Desc;
   end Get_Input_First_Desc;

   function Get_Output_First_Desc (M : Module) return Port_Desc_Idx
   is
      pragma Assert (Is_Valid (M));
   begin
      return Modules_Table.Table (M).First_Port_Desc
        + Port_Desc_Idx (Modules_Table.Table (M).Nbr_Inputs);
   end Get_Output_First_Desc;

   function Get_Self_Instance (M : Module) return Instance is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).First_Instance;
   end Get_Self_Instance;

   function Get_First_Instance (M : Module) return Instance
   is
      First : constant Instance := Get_Self_Instance (M);
   begin
      if First = No_Instance then
         --  Empty module.
         return No_Instance;
      else
         return Get_Next_Instance (First);
      end if;
   end Get_First_Instance;

   function Get_First_Sub_Module (M : Module) return Module is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).First_Sub_Module;
   end Get_First_Sub_Module;

   function Get_Next_Sub_Module (M : Module) return Module is
   begin
      pragma Assert (Is_Valid (M));
      return Modules_Table.Table (M).Next_Sub_Module;
   end Get_Next_Sub_Module;

   --  Instances

   --  List of free instances, linked by Next_Instance.
   Free_Instances : Instance := No_Instance;

   package Instances_Table is new Tables
     (Table_Component_Type => Instance_Record,
      Table_Index_Type => Instance,
      Table_Low_Bound => No_Instance,
      Table_Initial => 1024);

   --  List of free nets.
   --  As most of gates have only one output, just keep a single list.
   Free_Nets : Net := No_Net;

   package Nets_Table is new Tables
     (Table_Component_Type => Net_Record,
      Table_Index_Type => Net,
      Table_Low_Bound => No_Net,
      Table_Initial => 1024);

   --  List of free consecutive inputs.
   Free_Inputs : array (Port_Idx range 1 .. 4) of Input :=
     (others => No_Input);

   package Inputs_Table is new Tables
     (Table_Component_Type => Input_Record,
      Table_Index_Type => Input,
      Table_Low_Bound => No_Input,
      Table_Initial => 1024);

   Free_Params : array (Param_Idx range 1 .. 32) of Param_Idx :=
     (others => No_Param_Idx);
   Free_Params2 : Param_Idx := No_Param_Idx;

   package Params_Table is new Tables
     (Table_Component_Type => Uns32,
      Table_Index_Type => Param_Idx,
      Table_Low_Bound => No_Param_Idx,
      Table_Initial => 256);

   function Hash (Inst : Instance) return Hash_Value_Type is
   begin
      return Hash_Value_Type (Inst);
   end Hash;

   procedure Extract_All_Instances (M : Module; First_Instance : out Instance)
   is
      pragma Assert (Is_Valid (M));
      M_Ent : Module_Record renames Modules_Table.Table (M);
   begin
      First_Instance := M_Ent.First_Instance;

      --  Clear the instance list.
      M_Ent.First_Instance := No_Instance;
      M_Ent.Last_Instance := No_Instance;
   end Extract_All_Instances;

   procedure Append_Instance (M : Module; Inst : Instance)
   is
      M_Ent : Module_Record renames Modules_Table.Table (M);
   begin
      if M_Ent.First_Instance = No_Instance then
         M_Ent.First_Instance := Inst;
      else
         Instances_Table.Table (M_Ent.Last_Instance).Next_Instance := Inst;
      end if;
      Instances_Table.Table (Inst).Prev_Instance := M_Ent.Last_Instance;
      Instances_Table.Table (Inst).Next_Instance := No_Instance;
      M_Ent.Last_Instance := Inst;
   end Append_Instance;

   procedure Extract_Instance (Inst : Instance)
   is
      pragma Assert (Is_Valid (Inst));
      Inst_Ent : Instance_Record renames Instances_Table.Table (Inst);
      M : constant Module := Inst_Ent.Parent;
      M_Ent : Module_Record renames Modules_Table.Table (M);
   begin
      if Inst_Ent.Prev_Instance /= No_Instance then
         Set_Next_Instance (Inst_Ent.Prev_Instance, Inst_Ent.Next_Instance);
      else
         pragma Assert (M_Ent.First_Instance = Inst);
         M_Ent.First_Instance := Inst_Ent.Next_Instance;
      end if;

      if Inst_Ent.Next_Instance /= No_Instance then
         Set_Prev_Instance (Inst_Ent.Next_Instance, Inst_Ent.Prev_Instance);
      else
         pragma Assert (M_Ent.Last_Instance = Inst);
         M_Ent.Last_Instance := Inst_Ent.Prev_Instance;
      end if;

      Inst_Ent.Prev_Instance := No_Instance;
      Inst_Ent.Next_Instance := No_Instance;
   end Extract_Instance;

   function Check_Connected (Inst : Instance) return Boolean
   is
      Nbr_Outputs : constant Port_Idx := Get_Nbr_Outputs (Inst);
      Nbr_Inputs : constant Port_Idx := Get_Nbr_Inputs (Inst);
   begin
      --  Check that all outputs are unused.
      if Nbr_Outputs > 0 then
         for K in 0 .. Nbr_Outputs - 1 loop
            if Is_Connected (Get_Output (Inst, K)) then
               return True;
            end if;
         end loop;
      end if;

      --  First disconnect inputs.
      if Nbr_Inputs > 0 then
         for K in 0 .. Nbr_Inputs - 1 loop
            if Get_Driver (Get_Input (Inst, K)) /= No_Net then
               return True;
            end if;
         end loop;
      end if;

      return False;
   end Check_Connected;

   procedure Remove_Instance (Inst : Instance) is
   begin
      pragma Assert (not Check_Connected (Inst));
      Extract_Instance (Inst);
      Free_Instance (Inst);
   end Remove_Instance;

   function New_Instance_Internal (Parent : Module;
                                   M : Module;
                                   Name : Sname;
                                   Nbr_Inputs : Port_Nbr;
                                   Nbr_Outputs : Port_Nbr;
                                   Nbr_Params : Param_Nbr)
                                  return Instance
   is
      pragma Assert (Is_Valid (Parent));
      pragma Assert (Is_Valid (M));
      Res : Instance;
      Inputs : Input;
      Outputs : Net;
      Params : Param_Idx;
   begin
      if Free_Instances = No_Instance then
         Instances_Table.Increment_Last;
         Res := Instances_Table.Last;
      else
         Res := Free_Instances;
         Free_Instances := Instances_Table.Table (Res).Next_Instance;
      end if;

      if Nbr_Inputs > 0 then
         if Nbr_Inputs <= Free_Inputs'Last then
            if Free_Inputs (Nbr_Inputs) /= No_Input then
               --  Get a free input from the free list.
               Inputs := Free_Inputs (Nbr_Inputs);
               Free_Inputs (Nbr_Inputs) :=
                 Inputs_Table.Table (Inputs).Next_Sink;
            elsif Nbr_Inputs = 1 and then Free_Inputs (2) /= No_Input
            then
               --  Ok, common case: need just one input; get it from the list
               --  of free 2-inputs.
               pragma Assert (Free_Inputs (1) = No_Input);
               Inputs := Free_Inputs (2);
               Free_Inputs (2) := Inputs_Table.Table (Inputs).Next_Sink;
               Free_Inputs (1) := Inputs + 1;
               Inputs_Table.Table (Inputs + 1).Next_Sink := 0;
            else
               Inputs := Inputs_Table.Allocate (Natural (Nbr_Inputs));
            end if;
         else
            Inputs := Inputs_Table.Allocate (Natural (Nbr_Inputs));
         end if;
      else
         Inputs := No_Input;
      end if;

      if Nbr_Outputs > 0 then
         if Nbr_Outputs = 1 and then Free_Nets /= No_Net then
            Outputs := Free_Nets;
            Free_Nets := Net (Nets_Table.Table (Outputs).First_Sink);
         else
            Outputs := Nets_Table.Allocate (Natural (Nbr_Outputs));
         end if;
      else
         Outputs := No_Net;
      end if;

      if Nbr_Params > 0 then
         if Nbr_Params <= Free_Params'Last
           and then Free_Params (Nbr_Params) /= No_Param_Idx
         then
            Params := Free_Params (Nbr_Params);
            Free_Params (Nbr_Params) :=
              Param_Idx (Params_Table.Table (Params));
         else
            Params := Params_Table.Allocate (Natural (Nbr_Params));
         end if;
      else
         Params := No_Param_Idx;
      end if;

      Instances_Table.Table (Res) := ((Parent => Parent,
                                       Has_Attr => False,
                                       Flag4 => False,
                                       Next_Instance => No_Instance,
                                       Prev_Instance => No_Instance,
                                       Klass => M,
                                       Flag_Mark => False,
                                       Flag5 | Flag6 => False,
                                       Flag2 => False,
                                       Name => Name,
                                       First_Param => Params,
                                       First_Input => Inputs,
                                       First_Output => Outputs));

      --  Setup inputs.
      if Nbr_Inputs > 0 then
         for I in 0 .. Nbr_Inputs - 1 loop
            Inputs_Table.Table (Inputs + Input (I)) :=
              (Parent => Res,
               Driver => No_Net,
               Next_Sink => No_Input);
         end loop;
      end if;

      --  Setup nets.
      if Nbr_Outputs > 0 then
         for I in 0 .. Nbr_Outputs - 1 loop
            Nets_Table.Table (Outputs + Net (I)) := (Parent => Res,
                                                     First_Sink => No_Input,
                                                     W => 0);
         end loop;
      end if;

      --  Init params (to 0).
      if Nbr_Params > 0 then
         for I in 0 .. Nbr_Params - 1 loop
            Params_Table.Table (Params + I) := 0;
         end loop;
      end if;

      return Res;
   end New_Instance_Internal;

   procedure Set_Outputs_Width_From_Desc (Inst : Instance;
                                          Nbr_Outputs : Port_Nbr;
                                          Outputs_Desc : Port_Desc_Idx) is
   begin
      if Nbr_Outputs > 0 then
         for I in 0 .. Nbr_Outputs - 1 loop
            Set_Width
              (Get_Output (Inst, I),
               Get_Port_Desc (Outputs_Desc + Port_Desc_Idx (I)).W);
         end loop;
      end if;
   end Set_Outputs_Width_From_Desc;

   function New_Instance (Parent : Module; M : Module; Name : Sname)
                         return Instance
   is
      Nbr_Inputs : constant Port_Nbr := Get_Nbr_Inputs (M);
      Nbr_Outputs : constant Port_Nbr := Get_Nbr_Outputs (M);
      Nbr_Params : constant Param_Nbr := Get_Nbr_Params (M);
      Res : Instance;
   begin
      Res := New_Instance_Internal
        (Parent, M, Name, Nbr_Inputs, Nbr_Outputs, Nbr_Params);
      Set_Outputs_Width_From_Desc
        (Res, Nbr_Outputs, Get_Output_First_Desc (M));

      --  Link instance
      Append_Instance (Parent, Res);

      return Res;
   end New_Instance;

   function New_Var_Instance (Parent : Module;
                              M : Module;
                              Name : Sname;
                              Nbr_Inputs : Port_Nbr;
                              Nbr_Outputs : Port_Nbr;
                              Nbr_Params : Param_Nbr)
                             return Instance
   is
      Res : Instance;
   begin
      Res := New_Instance_Internal
        (Parent, M, Name, Nbr_Inputs, Nbr_Outputs, Nbr_Params);

      --  Link instance
      Append_Instance (Parent, Res);

      return Res;
   end New_Var_Instance;

   function Create_Self_Instance (M : Module) return Instance
   is
      --  Can be done only once.
      pragma Assert (Get_Self_Instance (M) = No_Instance);
      Nbr_Inputs : constant Port_Nbr := Get_Nbr_Inputs (M);
      Nbr_Outputs : constant Port_Nbr := Get_Nbr_Outputs (M);
      Res : Instance;
   begin
      --  Swap inputs and outputs; no parameters.
      Res := New_Instance_Internal
        (M, M, Get_Module_Name (M), Nbr_Outputs, Nbr_Inputs, 0);
      Set_Outputs_Width_From_Desc
        (Res, Nbr_Inputs, Get_Input_First_Desc (M));

      Append_Instance (M, Res);

      return Res;
   end Create_Self_Instance;

   function Is_Valid (I : Instance) return Boolean is
   begin
      return I > No_Instance and then I <= Instances_Table.Last;
   end Is_Valid;

   function Is_Self_Instance (I : Instance) return Boolean is
      Irec : Instance_Record renames Instances_Table.Table (I);
   begin
      return Irec.Parent = Irec.Klass;
   end Is_Self_Instance;

   type Module_Counter_Type is array (Module range 1 .. 128) of Natural;
   Free_Instances_Counter :  Module_Counter_Type :=
     (others => 0);

   procedure Free_Input (First : Input; Nbr : Port_Nbr)
   is
      pragma Assert (Nbr in Free_Inputs'Range);
   begin
      Inputs_Table.Table (First).Next_Sink := Free_Inputs (Nbr);
      Free_Inputs (Nbr) := First;
   end Free_Input;

   procedure Free_Instance (Inst : Instance)
   is
      pragma Assert (Is_Valid (Inst));
      Inst_Rec : Instance_Record renames Instances_Table.Table (Inst);
      Nbr_Outputs : Port_Nbr;
      Nbr_Inputs  : Port_Nbr;
      Nbr_Params : Param_Idx;
   begin
      pragma Assert (not Check_Connected (Inst));

      --  Instance must not be linked anymore.
      pragma Assert (Inst_Rec.Prev_Instance = No_Instance);
      pragma Assert (Inst_Rec.Next_Instance = No_Instance);

      if Inst_Rec.Klass <= Free_Instances_Counter'Last then
         Free_Instances_Counter (Inst_Rec.Klass) :=
           Free_Instances_Counter (Inst_Rec.Klass) + 1;
      end if;

      Nbr_Outputs := Get_Nbr_Outputs (Inst);
      for I in 1 .. Nbr_Outputs loop
         declare
            N : constant Net := Get_Output (Inst, I - 1);
         begin
            Nets_Table.Table (N).Parent := No_Instance;
         end;
      end loop;

      if Nbr_Outputs /= 0 then
         --  Put all nets, one by one, on the list of free nets.
         for I in 0 .. Net (Nbr_Outputs - 1) loop
            Nets_Table.Table (Inst_Rec.First_Output + I).First_Sink :=
              Input (Inst_Rec.First_Output + I + 1);
         end loop;
         Nets_Table.Table (Inst_Rec.First_Output
                           + Net (Nbr_Outputs - 1)).First_Sink :=
           Input (Free_Nets);
         Free_Nets := Inst_Rec.First_Output;
      end if;

      Nbr_Inputs := Get_Nbr_Inputs (Inst);
      for I in 1 .. Nbr_Inputs loop
         declare
            Inp : constant Input := Get_Input (Inst, I - 1);
         begin
            Inputs_Table.Table (Inp).Parent := No_Instance;
         end;
      end loop;

      if Nbr_Inputs /= 0 then
         if Nbr_Inputs <= Free_Inputs'Last then
            Free_Input (Inst_Rec.First_Input, Nbr_Inputs);
         else
            declare
               Num : Port_Nbr;
               First : Input;
            begin
               --  Free per pairs.
               Num := Nbr_Inputs;
               First := Inst_Rec.First_Input;
               while Num >= 2 loop
                  Free_Input (First, 2);
                  First := First + 2;
                  Num := Num - 2;
               end loop;
               --  Free the last one.
               if Num = 1 then
                  Free_Input (First, 1);
               end if;
            end;
         end if;
      end if;

      Nbr_Params := Get_Nbr_Params (Inst);
      if Nbr_Params /= 0 then
         if Nbr_Params <= Free_Params'Last then
            Params_Table.Table (Inst_Rec.First_Param) :=
              Uns32 (Free_Params (Nbr_Params));
            Free_Params (Nbr_Params) := Inst_Rec.First_Param;
         else
            Params_Table.Table (Inst_Rec.First_Param) :=
              Uns32 (Free_Params2);
            Params_Table.Table (Inst_Rec.First_Param + 1) :=
              Uns32 (Nbr_Params);
            Free_Params2 := Inst_Rec.First_Param;
         end if;
      end if;

      Inst_Rec.Klass := Free_Module;

      Inst_Rec.Next_Instance := Free_Instances;
      Free_Instances := Inst;
   end Free_Instance;

   function Get_Module (Inst : Instance) return Module is
   begin
      pragma Assert (Is_Valid (Inst));
      return Instances_Table.Table (Inst).Klass;
   end Get_Module;

   function Get_Instance_Name (Inst : Instance) return Sname is
   begin
      pragma Assert (Is_Valid (Inst));
      return Instances_Table.Table (Inst).Name;
   end Get_Instance_Name;

   function Get_Instance_Parent (Inst : Instance) return Module is
   begin
      pragma Assert (Is_Valid (Inst));
      return Instances_Table.Table (Inst).Parent;
   end Get_Instance_Parent;

   function Get_Next_Instance (Inst : Instance) return Instance is
   begin
      pragma Assert (Is_Valid (Inst));
      return Instances_Table.Table (Inst).Next_Instance;
   end Get_Next_Instance;

   procedure Set_Next_Instance (Inst : Instance; Next : Instance) is
   begin
      pragma Assert (Is_Valid (Inst));
      Instances_Table.Table (Inst).Next_Instance := Next;
   end Set_Next_Instance;

   procedure Set_Prev_Instance (Inst : Instance; Prev : Instance) is
   begin
      pragma Assert (Is_Valid (Inst));
      Instances_Table.Table (Inst).Prev_Instance := Prev;
   end Set_Prev_Instance;

   function Get_First_Output (Inst : Instance) return Net is
   begin
      pragma Assert (Is_Valid (Inst));
      return Instances_Table.Table (Inst).First_Output;
   end Get_First_Output;

   function Get_Output (Inst : Instance; Idx : Port_Idx) return Net is
   begin
      pragma Assert (Is_Valid (Inst));
      pragma Assert (Idx < Get_Nbr_Outputs (Inst));
      return Instances_Table.Table (Inst).First_Output + Net (Idx);
   end Get_Output;

   function Get_Input (Inst : Instance; Idx : Port_Idx) return Input is
   begin
      pragma Assert (Is_Valid (Inst));
      pragma Assert (Idx < Get_Nbr_Inputs (Inst));
      return Instances_Table.Table (Inst).First_Input + Input (Idx);
   end Get_Input;

   --  Nets

   function Is_Valid (N : Net) return Boolean is
   begin
      return N > No_Net and then N <= Nets_Table.Last;
   end Is_Valid;

   function Get_Net_Parent (O : Net) return Instance is
   begin
      pragma Assert (Is_Valid (O));
      return Nets_Table.Table (O).Parent;
   end Get_Net_Parent;

   function Get_Port_Idx (O : Net) return Port_Idx
   is
      pragma Assert (Is_Valid (O));
      Parent : constant Instance := Get_Net_Parent (O);
   begin
      return Port_Idx (O - Instances_Table.Table (Parent).First_Output);
   end Get_Port_Idx;

   function Get_First_Sink (O : Net) return Input is
   begin
      pragma Assert (Is_Valid (O));
      return Nets_Table.Table (O).First_Sink;
   end Get_First_Sink;

   function Get_Width (N : Net) return Width
   is
      pragma Assert (Is_Valid (N));
   begin
      return Nets_Table.Table (N).W;
   end Get_Width;

   procedure Set_Width (N : Net; W : Width)
   is
      pragma Assert (Is_Valid (N));
   begin
      if Nets_Table.Table (N).W /= No_Width then
         raise Internal_Error;
      end if;
      Nets_Table.Table (N).W := W;
   end Set_Width;


   --  Inputs

   function Is_Valid (N : Input) return Boolean is
   begin
      return N > No_Input and then N <= Inputs_Table.Last;
   end Is_Valid;

   function Get_Input_Parent (I : Input) return Instance is
   begin
      pragma Assert (Is_Valid (I));
      return Inputs_Table.Table (I).Parent;
   end Get_Input_Parent;

   function Get_Port_Idx (I : Input) return Port_Idx
   is
      pragma Assert (Is_Valid (I));
      Parent : constant Instance := Get_Input_Parent (I);
   begin
      return Port_Idx (I - Instances_Table.Table (Parent).First_Input);
   end Get_Port_Idx;

   function Get_Driver (I : Input) return Net is
   begin
      pragma Assert (Is_Valid (I));
      return Inputs_Table.Table (I).Driver;
   end Get_Driver;

   function Get_Next_Sink (I : Input) return Input is
   begin
      pragma Assert (Is_Valid (I));
      return Inputs_Table.Table (I).Next_Sink;
   end Get_Next_Sink;


   --  Port_Desc

   function Get_Port_Desc (Idx : Port_Desc_Idx) return Port_Desc is
   begin
      return Port_Desc_Table.Table (Idx);
   end Get_Port_Desc;

   procedure Set_Port_Desc (Idx : Port_Desc_Idx; Desc : Port_Desc) is
   begin
      Port_Desc_Table.Table (Idx) := Desc;
   end Set_Port_Desc;

   function Get_Input_Desc (M : Module; I : Port_Idx) return Port_Desc
   is
      F : constant Port_Desc_Idx := Get_Input_First_Desc (M);
      pragma Assert (I < Get_Nbr_Inputs (M));
   begin
      return Get_Port_Desc (F + Port_Desc_Idx (I));
   end Get_Input_Desc;

   function Get_Output_Desc (M : Module; O : Port_Idx) return Port_Desc
   is
      F : constant Port_Desc_Idx := Get_Output_First_Desc (M);
      pragma Assert (O < Get_Nbr_Outputs (M));
   begin
      return Get_Port_Desc (F + Port_Desc_Idx (O));
   end Get_Output_Desc;

   procedure Set_Input_Desc (M : Module; I : Port_Idx; Desc : Port_Desc)
   is
      F : constant Port_Desc_Idx := Get_Input_First_Desc (M);
      pragma Assert (I < Get_Nbr_Inputs (M));
      Idx : constant Port_Desc_Idx := F + Port_Desc_Idx (I);
   begin
      pragma Assert (Get_Port_Desc (Idx).Name = No_Sname);
      Set_Port_Desc (Idx, Desc);
   end Set_Input_Desc;

   procedure Set_Output_Desc (M : Module; O : Port_Idx; Desc : Port_Desc)
   is
      F : constant Port_Desc_Idx := Get_Output_First_Desc (M);
      pragma Assert (O < Get_Nbr_Outputs (M));
      Idx : constant Port_Desc_Idx := F + Port_Desc_Idx (O);
   begin
      pragma Assert (Get_Port_Desc (Idx).Name = No_Sname);
      Set_Port_Desc (Idx, Desc);
   end Set_Output_Desc;

   procedure Set_Ports_Desc (M : Module;
                             Input_Descs : Port_Desc_Array;
                             Output_Descs : Port_Desc_Array)
   is
      pragma Assert (Is_Valid (M));
      pragma Assert (Input_Descs'Length = Get_Nbr_Inputs (M));
      pragma Assert (Output_Descs'Length = Get_Nbr_Outputs (M));
   begin
      for I in Input_Descs'Range loop
         Set_Input_Desc (M, I - Input_Descs'First, Input_Descs (I));
      end loop;

      for O in Output_Descs'Range loop
         Set_Output_Desc (M, O - Output_Descs'First, Output_Descs (O));
      end loop;
   end Set_Ports_Desc;

   --  Param_Desc

   package Param_Desc_Table is new Tables
     (Table_Component_Type => Param_Desc,
      Table_Index_Type => Param_Desc_Idx,
      Table_Low_Bound => No_Param_Desc_Idx,
      Table_Initial => 256);

   procedure Set_Params_Desc (M : Module;
                              Params : Param_Desc_Array)
   is
      pragma Assert (Is_Valid (M));
      pragma Assert (Params'Length = Get_Nbr_Params (M));
   begin
      pragma Assert
        (Modules_Table.Table (M).First_Param_Desc = No_Param_Desc_Idx);

      Modules_Table.Table (M).First_Param_Desc := Param_Desc_Table.Last + 1;

      for P of Params loop
         Param_Desc_Table.Append (P);
      end loop;
   end Set_Params_Desc;

   function Get_Param_Desc (M : Module; Param : Param_Idx) return Param_Desc
   is
      use Netlists.Gates;
      pragma Assert (Is_Valid (M));
   begin
      case Get_Id (M) is
         when Id_Const_Bit
           | Id_Const_Log =>
            return (No_Sname, Param_Uns32);
         when others =>
            pragma Assert (Param < Get_Nbr_Params (M));
            return Param_Desc_Table.Table
              (Modules_Table.Table (M).First_Param_Desc
                 + Param_Desc_Idx (Param));
      end case;
   end Get_Param_Desc;

   function Get_Mark_Flag (Inst : Instance) return Boolean
   is
      pragma Assert (Is_Valid (Inst));
   begin
      return Instances_Table.Table (Inst).Flag_Mark;
   end Get_Mark_Flag;

   procedure Set_Mark_Flag (Inst : Instance; Flag : Boolean)
   is
      pragma Assert (Is_Valid (Inst));
   begin
      Instances_Table.Table (Inst).Flag_Mark := Flag;
   end Set_Mark_Flag;

   function Get_Param_Idx (Inst : Instance; Param : Param_Idx) return Param_Idx
   is
      pragma Assert (Is_Valid (Inst));
      pragma Assert (Param < Get_Nbr_Params (Inst));
   begin
      return Instances_Table.Table (Inst).First_Param + Param;
   end Get_Param_Idx;

   function Get_Param_Uns32 (Inst : Instance; Param : Param_Idx) return Uns32
   is
      pragma Assert (Is_Valid (Inst));
      M : constant Module := Get_Module (Inst);
      pragma Assert (Param < Get_Nbr_Params (Inst));
      pragma Assert (Get_Param_Desc (M, Param).Typ = Param_Uns32);
   begin
      return Params_Table.Table (Get_Param_Idx (Inst, Param));
   end Get_Param_Uns32;

   procedure Set_Param_Uns32 (Inst : Instance; Param : Param_Idx; Val : Uns32)
   is
      pragma Assert (Is_Valid (Inst));
      M : constant Module := Get_Module (Inst);
      pragma Assert (Param < Get_Nbr_Params (Inst));
      pragma Assert (Get_Param_Desc (M, Param).Typ = Param_Uns32);
   begin
      Params_Table.Table (Get_Param_Idx (Inst, Param)) := Val;
   end Set_Param_Uns32;

   function Get_Param_Pval (Inst : Instance; Param : Param_Idx) return Pval
   is
      M : constant Module := Get_Module (Inst);
      pragma Assert (Param < Get_Nbr_Params (Inst));
      pragma Assert (Get_Param_Desc (M, Param).Typ in Param_Types_Pval);
   begin
      return Pval (Params_Table.Table (Get_Param_Idx (Inst, Param)));
   end Get_Param_Pval;

   procedure Set_Param_Pval (Inst : Instance; Param : Param_Idx; Val : Pval)
   is
      M : constant Module := Get_Module (Inst);
      pragma Assert (Param < Get_Nbr_Params (Inst));
      pragma Assert (Get_Param_Desc (M, Param).Typ in Param_Types_Pval);
   begin
      Params_Table.Table (Get_Param_Idx (Inst, Param)) := Uns32 (Val);
   end Set_Param_Pval;

   procedure Connect (I : Input; O : Net)
   is
      pragma Assert (Is_Valid (I));
      pragma Assert (Is_Valid (O));
      --  Check Width compatibility
      --  pragma assert (get_width (i) = get_width (o));
      pragma Assert (Get_Driver (I) = No_Net);
      I_Ent : Input_Record renames Inputs_Table.Table (I);
      O_Ent : Net_Record renames Nets_Table.Table (O);
   begin
      I_Ent.Driver := O;
      I_Ent.Next_Sink := O_Ent.First_Sink;
      O_Ent.First_Sink := I;
   end Connect;

   procedure Disconnect (I : Input)
   is
      pragma Assert (Is_Valid (I));
      Drv : constant Net := Get_Driver (I);
      pragma Assert (Drv /= No_Net);
      Next_Sink : constant Input := Get_Next_Sink (I);
      I_Ent : Input_Record renames Inputs_Table.Table (I);
      D_Ent : Net_Record renames Nets_Table.Table (Drv);
      S, N_S : Input;
   begin
      I_Ent.Next_Sink := No_Input;
      I_Ent.Driver := No_Net;

      if D_Ent.First_Sink = I then
         --  Was the first sink.
         D_Ent.First_Sink := Next_Sink;
      else
         --  Walk
         S := D_Ent.First_Sink;
         loop
            pragma Assert (Is_Valid (S));
            N_S := Get_Next_Sink (S);
            if N_S = I then
               Inputs_Table.Table (S).Next_Sink := Next_Sink;
               exit;
            else
               S := N_S;
            end if;
         end loop;
      end if;
   end Disconnect;

   procedure Redirect_Inputs (Old : Net; N : Net)
   is
      First_I, I : Input;
      Last_I : Input;
   begin
      First_I := Get_First_Sink (Old);
      if First_I = No_Input then
         --  Nothing to do if no input.
         return;
      end if;

      I := First_I;
      Last_I := No_Input;
      while I /= No_Input loop
         declare
            I_Rec : Input_Record renames Inputs_Table.Table (I);
         begin
            pragma Assert (I_Rec.Driver = Old);
            I_Rec.Driver := N;

            Last_I := I;

            I := I_Rec.Next_Sink;
         end;
      end loop;
      Inputs_Table.Table (Last_I).Next_Sink := Get_First_Sink (N);
      Nets_Table.Table (N).First_Sink := First_I;

      --  Also disconnect OLD
      Nets_Table.Table (Old).First_Sink := No_Input;
   end Redirect_Inputs;

   type Pval_Record is record
      Len : Uns32;
      Va_Idx : Uns32;
      Zx_Idx : Uns32;
   end record;

   package Pval_Table is new Tables
     (Table_Component_Type => Pval_Record,
      Table_Index_Type => Pval,
      Table_Low_Bound => 0,
      Table_Initial => 32);

   package Pval_Word_Table is new Tables
     (Table_Component_Type => Uns32,
      Table_Index_Type => Uns32,
      Table_Low_Bound => 0,
      Table_Initial => 32);

   function Create_Pval4 (Len : Uns32) return Pval
   is
      pragma Assert (Len > 0);
      Nwords : constant Uns32 := (Len + 31) / 32;
      Idx : constant Uns32 := Pval_Word_Table.Last + 1;
      Res : Uns32;
   begin
      Pval_Table.Append ((Len => Len,
                          Va_Idx => Idx,
                          Zx_Idx => Idx + Nwords));
      Res := Pval_Word_Table.Allocate (Natural (2 * Nwords));
      pragma Assert (Res = Idx);
      return Pval_Table.Last;
   end Create_Pval4;

   function Create_Pval2 (Len : Uns32) return Pval
   is
      pragma Assert (Len > 0);
      Nwords : constant Uns32 := (Len + 31) / 32;
      Idx : constant Uns32 := Pval_Word_Table.Last + 1;
      Res : Uns32;
   begin
      Pval_Table.Append ((Len => Len,
                          Va_Idx => Idx,
                          Zx_Idx => 0));
      Res := Pval_Word_Table.Allocate (Natural (Nwords));
      pragma Assert (Res = Idx);
      return Pval_Table.Last;
   end Create_Pval2;

   function Get_Pval_Length (P : Pval) return Uns32
   is
      pragma Assert (P <= Pval_Table.Last);
   begin
      return Pval_Table.Table (P).Len;
   end Get_Pval_Length;

   function Read_Pval (P : Pval; Off : Uns32) return Logic_32
   is
      pragma Assert (P <= Pval_Table.Last);
      Pval_Rec : Pval_Record renames Pval_Table.Table (P);
      pragma Assert (Off <= (Pval_Rec.Len - 1) / 32);
      Res : Logic_32;
   begin
      Res.Val := Pval_Word_Table.Table (Pval_Rec.Va_Idx + Off);
      if Pval_Rec.Zx_Idx = 0 then
         Res.Zx := 0;
      else
         Res.Zx := Pval_Word_Table.Table (Pval_Rec.Zx_Idx + Off);
      end if;
      return Res;
   end Read_Pval;

   procedure Write_Pval (P : Pval; Off : Uns32; Val : Logic_32)
   is
      pragma Assert (P <= Pval_Table.Last);
      Pval_Rec : Pval_Record renames Pval_Table.Table (P);
      pragma Assert (Off <= (Pval_Rec.Len - 1) / 32);
   begin
      Pval_Word_Table.Table (Pval_Rec.Va_Idx + Off) := Val.Val;
      if Pval_Rec.Zx_Idx = 0 then
         pragma Assert (Val.Zx = 0);
         null;
      else
         Pval_Word_Table.Table (Pval_Rec.Zx_Idx + Off) := Val.Zx;
      end if;
   end Write_Pval;

   --  Attributes

   function Attribute_Hash (Params : Instance) return Hash_Value_Type is
   begin
      return Hash_Value_Type (Params);
   end Attribute_Hash;

   function Attribute_Build (Params : Instance) return Instance is
   begin
      return Params;
   end Attribute_Build;

   function Attribute_Build_Value (Obj : Instance) return Attribute
   is
      pragma Unreferenced (Obj);
   begin
      return No_Attribute;
   end Attribute_Build_Value;

   package Attributes_Table is new Tables
     (Table_Component_Type => Attribute_Record,
      Table_Index_Type     => Attribute,
      Table_Low_Bound      => 0,
      Table_Initial        => 64);

   procedure Set_Attribute
     (Inst : Instance; Id : Name_Id; Ptype : Param_Type; Pv : Pval)
   is
      pragma Assert (Is_Valid (Inst));
      M          : constant Module := Get_Instance_Parent (Inst);
      Module_Rec : Module_Record renames Modules_Table.Table (M);
      Attr       : Attribute;
      Idx        : Attribute_Maps.Index_Type;
      Prev       : Attribute;
   begin
      if Module_Rec.Attrs = null then
         Module_Rec.Attrs := new Attribute_Maps.Instance;
         Attribute_Maps.Init (Module_Rec.Attrs.all);
      end if;

      --  There is now at least one attribute for INST.
      Instances_Table.Table (Inst).Has_Attr := True;

      Attribute_Maps.Get_Index (Module_Rec.Attrs.all, Inst, Idx);

      Prev := Attribute_Maps.Get_Value (Module_Rec.Attrs.all, Idx);
      Attributes_Table.Append ((Name => Id,
                                Typ => Ptype,
                                Val => Pv,
                                Chain => Prev));
      Attr := Attributes_Table.Last;

      Attribute_Maps.Set_Value (Module_Rec.Attrs.all, Idx, Attr);
   end Set_Attribute;

   function Get_Attributes (M : Module) return Attribute_Map_Acc is
   begin
      return Modules_Table.Table (M).Attrs;
   end Get_Attributes;

   function Get_First_Attribute (Inst : Instance) return Attribute
   is
      pragma Assert (Is_Valid (Inst));
   begin
      if not Instances_Table.Table (Inst).Has_Attr then
         return No_Attribute;
      end if;
      declare
         M          : constant Module := Get_Instance_Parent (Inst);
         Attrs      : constant Attribute_Map_Acc := Get_Attributes (M);
         Idx        : Attribute_Maps.Index_Type;
         Res        : Attribute;
      begin
         pragma Assert (Attrs /= null);
         Attribute_Maps.Get_Index (Attrs.all, Inst, Idx);
         Res := Attribute_Maps.Get_Value (Attrs.all, Idx);
         return Res;
      end;
   end Get_First_Attribute;

   function Is_Valid (Attr : Attribute) return Boolean is
   begin
      return Attr > No_Attribute and then Attr <= Attributes_Table.Last;
   end Is_Valid;

   function Get_Attribute_Name (Attr : Attribute) return Name_Id
   is
      pragma Assert (Is_Valid (Attr));
   begin
      return Attributes_Table.Table (Attr).Name;
   end Get_Attribute_Name;

   function Get_Attribute_Type (Attr : Attribute) return Param_Type
   is
      pragma Assert (Is_Valid (Attr));
   begin
      return Attributes_Table.Table (Attr).Typ;
   end Get_Attribute_Type;

   function Get_Attribute_Pval (Attr : Attribute) return Pval
   is
      pragma Assert (Is_Valid (Attr));
   begin
      return Attributes_Table.Table (Attr).Val;
   end Get_Attribute_Pval;

   function Get_Attribute_Next (Attr : Attribute) return Attribute
   is
      pragma Assert (Is_Valid (Attr));
   begin
      return Attributes_Table.Table (Attr).Chain;
   end Get_Attribute_Next;

   --  Statistics

   function Count_Free_Inputs (Head : Input) return Natural
   is
      Unused : Natural;
      Inp : Input;
   begin
      Unused := 0;
      Inp := Head;
      while Inp /= No_Input loop
         Unused := Unused + 1;
         Inp := Inputs_Table.Table (Inp).Next_Sink;
      end loop;
      return Unused;
   end Count_Free_Inputs;

   procedure Disp_Stats
   is
      use Simple_IO;
      Unused : Natural;
      Nbr_Modules : Module_Counter_Type := (others => 0);
   begin
      Put_Line_Err ("Statistics for netlists:");
      Put_Line_Err
        (" snames:    " & Sname'Image (Snames_Table.Last));
      Put_Line_Err
        (" modules:   " & Module'Image (Modules_Table.Last));

      Put_Err
        (" instances: " & Instance'Image (Instances_Table.Last));
      Unused := 0;
      for I in No_Instance + 1 .. Instances_Table.Last loop
         if Get_Module (I) = Free_Module then
            Unused := Unused + 1;
         end if;
      end loop;
      Put_Line_Err
        (" (free:" & Natural'Image (Unused) & ')');

      Put_Err
        (" nets:      " & Net'Image (Nets_Table.Last));
      Unused := 0;
      for I in No_Net + 1 .. Nets_Table.Last loop
         if Get_Net_Parent (I) = No_Instance then
            Unused := Unused + 1;
         end if;
      end loop;
      Put_Line_Err
        (" (free:" & Natural'Image (Unused) & ')');

      Put_Err
        (" inputs:    " & Input'Image (Inputs_Table.Last));
      Unused := 0;
      for I in No_Input + 1 .. Inputs_Table.Last loop
         if Get_Input_Parent (I) = No_Instance then
            Unused := Unused + 1;
         end if;
      end loop;
      Put_Line_Err
        (" (free:" & Natural'Image (Unused) & ')');
      for I in Free_Inputs'Range loop
         Unused := Count_Free_Inputs (Free_Inputs (I));
         if Unused /= 0 then
            Put_Line_Err ("  free" & Port_Nbr'Image (I) & " inputs:"
                          & Natural'Image (Unused)
                          & " *" & Port_Nbr'Image (I)
                          & " =" & Natural'Image (Unused * Natural (I)));
         end if;
      end loop;

      Put_Line_Err
        (" params:    " & Param_Idx'Image (Params_Table.Last));

      for I in No_Instance + 1 .. Instances_Table.Last loop
         declare
            M : constant Module := Get_Module (I);
         begin
            if M <= Nbr_Modules'Last then
               Nbr_Modules (M) := Nbr_Modules (M) + 1;
            end if;
         end;
      end loop;

      for J in 1 .. 2 loop
         case J is
            when 1 =>
               Put_Line_Err (" Number of instances (per module):");
            when 2 =>
               Put_Line_Err (" Number of freed instances (per module):");
         end case;
         for I in Module_Counter_Type'Range loop
            case J is
               when 1 =>
                  Unused := Nbr_Modules (I);
               when 2 =>
                  Unused := Free_Instances_Counter (I);
            end case;

            if Unused /= 0 then
               declare
                  Name : constant Sname := Get_Module_Name (I);
               begin
                  case Get_Sname_Kind (Name) is
                     when Sname_User
                        | Sname_Artificial =>
                        Put_Err
                          ("  " & Name_Table.Image (Get_Sname_Suffix (Name)));
                     when others =>
                        Put_Err
                          ("  module " & Module_Id'Image (Get_Id (I)));
                  end case;
                  Put_Line_Err (":" & Natural'Image (Unused));
               end;
            end if;
         end loop;
      end loop;
   end Disp_Stats;

begin
   --  Initialize snames_table: create the first entry for No_Sname.
   Snames_Table.Append ((Kind => Sname_Artificial,
                         Prefix => No_Sname,
                         Suffix => 0));
   pragma Assert (Snames_Table.Last = No_Sname);

   Modules_Table.Append ((Parent => No_Module,
                          Name => No_Sname,
                          Id => Id_None,
                          First_Port_Desc => No_Port_Desc_Idx,
                          Nbr_Inputs => 0,
                          Nbr_Outputs => 0,
                          First_Param_Desc => No_Param_Desc_Idx,
                          Nbr_Params => 0,
                          First_Sub_Module => No_Module,
                          Last_Sub_Module => No_Module,
                          Next_Sub_Module => No_Module,
                          First_Instance => No_Instance,
                          Last_Instance => No_Instance,
                          Attrs => null));
   pragma Assert (Modules_Table.Last = No_Module);

   Modules_Table.Append ((Parent => No_Module,
                          Name => New_Sname_Artificial (Std_Names.Name_None,
                                                        No_Sname),
                          Id => Id_Free,
                          First_Port_Desc => No_Port_Desc_Idx,
                          Nbr_Inputs => 0,
                          Nbr_Outputs => 0,
                          First_Param_Desc => No_Param_Desc_Idx,
                          Nbr_Params => 0,
                          First_Sub_Module => No_Module,
                          Last_Sub_Module => No_Module,
                          Next_Sub_Module => No_Module,
                          First_Instance => No_Instance,
                          Last_Instance => No_Instance,
                          Attrs => null));
   pragma Assert (Modules_Table.Last = Free_Module);

   Instances_Table.Append ((Parent => No_Module,
                            Has_Attr => False,
                            Flag4 => False,
                            Next_Instance => No_Instance,
                            Prev_Instance => No_Instance,
                            Klass => No_Module,
                            Flag5 | Flag6 => False,
                            Flag_Mark => False,
                            Flag2 => False,
                            Name => No_Sname,
                            First_Param => No_Param_Idx,
                            First_Input => No_Input,
                            First_Output => No_Net));
   pragma Assert (Instances_Table.Last = No_Instance);

   Nets_Table.Append ((Parent => No_Instance,
                       First_Sink => No_Input,
                       W => 0));
   pragma Assert (Nets_Table.Last = No_Net);

   Inputs_Table.Append ((Parent => No_Instance,
                         Driver => No_Net,
                         Next_Sink => No_Input));
   pragma Assert (Inputs_Table.Last = No_Input);

   Port_Desc_Table.Append ((Name => No_Sname,
                            Is_Inout => False,
                            W => 0));
   pragma Assert (Port_Desc_Table.Last = No_Port_Desc_Idx);

   Param_Desc_Table.Append ((Name => No_Sname,
                             Typ => Param_Uns32));
   pragma Assert (Param_Desc_Table.Last = No_Param_Desc_Idx);

   Params_Table.Append (0);
   pragma Assert (Params_Table.Last = No_Param_Idx);

   Pval_Table.Append ((Len => 0,
                       Va_Idx => 0,
                       Zx_Idx => 0));
   pragma Assert (Pval_Table.Last = No_Pval);

   Pval_Word_Table.Append (0);

   Attributes_Table.Append ((Name => No_Name_Id,
                             Typ => Param_Invalid,
                             Val => No_Pval,
                             Chain => No_Attribute));
   pragma Assert (Attributes_Table.Last = No_Attribute);
end Netlists;