aboutsummaryrefslogtreecommitdiffstats
path: root/src/grt/grt-avhpi.adb
blob: 32365ef82cb39308f26520350a777e7300c511b6 (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
--  GHDL Run Time (GRT) - VHPI implementation for Ada.
--  Copyright (C) 2002 - 2014 Tristan Gingold
--
--  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>.
--
--  As a special exception, if other files instantiate generics from this
--  unit, or you link this unit with other files to produce an executable,
--  this unit does not by itself cause the resulting executable to be
--  covered by the GNU General Public License. This exception does not
--  however invalidate any other reasons why the executable file might be
--  covered by the GNU Public License.
with Grt.Errors; use Grt.Errors;
with Grt.Vstrings; use Grt.Vstrings;
with Grt.Rtis_Utils; use Grt.Rtis_Utils;
with Grt.To_Strings;

package body Grt.Avhpi is
   procedure Get_Root_Inst (Res : out VhpiHandleT) is
   begin
      Res := (Kind => VhpiRootInstK,
              Ctxt => Get_Top_Context);
   end Get_Root_Inst;

   procedure Get_Root_Scope (Res : out VhpiHandleT) is
   begin
      Res := (Kind => AvhpiRootScopeK,
              Ctxt => Null_Context);
   end Get_Root_Scope;

   procedure Get_Package_Inst (Res : out VhpiHandleT) is
   begin
      --  Ctxt is the list of instantiated packages.
      Res := (Kind => VhpiIteratorK,
              Ctxt => (Base => Null_Address,
                       Block => To_Ghdl_Rti_Access (Ghdl_Rti_Top'Address)),
              Rel => VhpiPackInsts,
              It_Cur => 0,
              It2 => 0,
              Max2 => 0);
   end Get_Package_Inst;

   --  Number of elements in an array.
   function Ranges_To_Length (Rngs : Ghdl_Range_Array;
                              Indexes : Ghdl_Rti_Arr_Acc)
                             return Ghdl_Index_Type
   is
      Res : Ghdl_Index_Type;
   begin
      Res := 1;
      for I in Rngs'Range loop
         Res := Res * Range_To_Length
           (Rngs (I), Get_Base_Type (Indexes (I - Rngs'First)));
      end loop;
      return Res;
   end Ranges_To_Length;

   procedure Vhpi_Iterator (Rel : VhpiOneToManyT;
                            Ref : VhpiHandleT;
                            Res : out VhpiHandleT;
                            Error : out AvhpiErrorT) is
   begin
      --  Default value in case of success.
      Res := (Kind => VhpiIteratorK,
              Ctxt => Ref.Ctxt,
              Rel => Rel,
              It_Cur => 0,
              It2 => 0,
              Max2 => 0);
      Error := AvhpiErrorOk;

      case Rel is
         when VhpiInternalRegions =>
            case Ref.Kind is
               when VhpiRootInstK
                 | VhpiArchBodyK
                 | VhpiBlockStmtK
                 | VhpiIfGenerateK =>
                  return;
               when VhpiForGenerateK =>
                  Res.It2 := 1;
                  return;
               when VhpiCompInstStmtK =>
                  Get_Instance_Context (Ref.Inst, Ref.Ctxt, Res.Ctxt);
                  return;
               when AvhpiRootScopeK =>
                  Res := (Kind => AvhpiRootScopeIteratorK,
                          Ctxt => Ref.Ctxt,
                          Rel => Rel,
                          It_Cur => 0,
                          It2 => 0,
                          Max2 => 0);
                  return;
               when others =>
                  null;
            end case;
         when VhpiDecls =>
            case Ref.Kind is
               when VhpiArchBodyK
                 | VhpiBlockStmtK
                 | VhpiIfGenerateK
                 | VhpiForGenerateK =>
                  return;
               when VhpiRootInstK
                 | VhpiPackInstK =>
                  Res.It2 := 1;
                  return;
               when VhpiCompInstStmtK =>
                  Get_Instance_Context (Ref.Inst, Ref.Ctxt, Res.Ctxt);
                  Res.It2 := 1;
                  return;
               when others =>
                  null;
            end case;
         when VhpiIndexedNames =>
            case Ref.Kind is
               when VhpiGenericDeclK
                  | VhpiConstDeclK=>
                  Res := (Kind => AvhpiNameIteratorK,
                          Ctxt => Ref.Ctxt,
                          N_Addr => Avhpi_Get_Address (Ref),
                          N_Type => Ref.Obj.Obj_Type,
                          N_Idx => 0,
                          N_Obj => Ref.Obj);
               when VhpiIndexedNameK =>
                  Res := (Kind => AvhpiNameIteratorK,
                          Ctxt => Ref.Ctxt,
                          N_Addr => Ref.N_Addr,
                          N_Type => Ref.N_Type,
                          N_Idx => 0,
                          N_Obj => Ref.N_Obj);
               when others =>
                  Error := AvhpiErrorNotImplemented;
                  return;
            end case;
            case Res.N_Type.Kind is
               when Ghdl_Rtik_Subtype_Array =>
                  declare
                     St : constant Ghdl_Rtin_Subtype_Composite_Acc :=
                       To_Ghdl_Rtin_Subtype_Composite_Acc (Res.N_Type);
                     Bt : constant Ghdl_Rtin_Type_Array_Acc :=
                       To_Ghdl_Rtin_Type_Array_Acc (St.Basetype);
                     Rngs : Ghdl_Range_Array (0 .. Bt.Nbr_Dim - 1);
                     Layout : Address;
                  begin
                     Layout :=
                       Loc_To_Addr (St.Common.Depth, St.Layout, Res.Ctxt);
                     Bound_To_Range
                       (Array_Layout_To_Bounds (Layout), Bt, Rngs);
                     Res.N_Idx := Ranges_To_Length (Rngs, Bt.Indexes);
                  end;
               when others =>
                  Error := AvhpiErrorBadRel;
            end case;
            return;
         when others =>
            null;
      end case;
      --  Failure.
      Res := Null_Handle;
      Error := AvhpiErrorNotImplemented;
   end Vhpi_Iterator;

   --  OBJ_RTI is the RTI for the base name.
   function Add_Index (Ctxt : Rti_Context;
                       Obj_Base : Address;
                       Obj_Rti : Ghdl_Rtin_Object_Acc;
                       El_Type : Ghdl_Rti_Access;
                       Off : Ghdl_Index_Type) return Address
   is
      Is_Sig : Boolean;
      El_Size : Ghdl_Index_Type;
      El_Type1 : Ghdl_Rti_Access;
   begin
      case Obj_Rti.Common.Kind is
         when Ghdl_Rtik_Generic
            | Ghdl_Rtik_Constant =>
            Is_Sig := False;
         when Ghdl_Rtik_Signal =>
            Is_Sig := True;
         when others =>
            Internal_Error ("add_index(1)");
      end case;

      if El_Type.Kind = Ghdl_Rtik_Subtype_Scalar then
         El_Type1 := Get_Base_Type (El_Type);
      else
         El_Type1 := El_Type;
      end if;

      case El_Type1.Kind is
         when Ghdl_Rtik_Type_P64 =>
            if Is_Sig then
               El_Size := Address'Size / Storage_Unit;
            else
               El_Size := Ghdl_I64'Size / Storage_Unit;
            end if;
         when Ghdl_Rtik_Subtype_Array =>
            declare
               Sizes : Ghdl_Indexes_Ptr;
            begin
               Sizes := To_Ghdl_Indexes_Ptr
                 (Loc_To_Addr
                    (El_Type1.Depth,
                     To_Ghdl_Rtin_Subtype_Composite_Acc (El_Type1).Layout,
                     Ctxt));
               if Is_Sig then
                  El_Size := Sizes.Signal;
               else
                  El_Size := Sizes.Value;
               end if;
            end;
         when others =>
            Internal_Error ("add_index(2)");
      end case;
      return Obj_Base + Off * El_Size;
   end Add_Index;

   procedure Vhpi_Scan_Indexed_Name (Iterator : in out VhpiHandleT;
                                     Res : out VhpiHandleT;
                                     Error : out AvhpiErrorT)
   is
      El_Type : Ghdl_Rti_Access;
   begin
      if Iterator.N_Idx = 0 then
         Error := AvhpiErrorIteratorEnd;
         return;
      end if;

      El_Type := To_Ghdl_Rtin_Type_Array_Acc
        (Get_Base_Type (Iterator.N_Type)).Element;

      Res := (Kind => VhpiIndexedNameK,
              Ctxt => Iterator.Ctxt,
              N_Addr => Iterator.N_Addr,
              N_Type => El_Type,
              N_Idx => 0,
              N_Obj => Iterator.N_Obj);

      --  Increment Address.
      Iterator.N_Addr := Add_Index
        (Iterator.Ctxt, Iterator.N_Addr, Iterator.N_Obj, El_Type, 1);

      Iterator.N_Idx := Iterator.N_Idx - 1;
      Error := AvhpiErrorOk;
   end Vhpi_Scan_Indexed_Name;

   procedure Vhpi_Scan_Internal_Regions (Iterator : in out VhpiHandleT;
                                         Res : out VhpiHandleT;
                                         Error : out AvhpiErrorT)
   is
      Blk : Ghdl_Rtin_Block_Acc;
      Ch : Ghdl_Rti_Access;
      Nblk : Ghdl_Rtin_Block_Acc;
   begin
      Blk := To_Ghdl_Rtin_Block_Acc (Iterator.Ctxt.Block);
      if Blk = null then
         Error := AvhpiErrorIteratorEnd;
         return;
      end if;

      loop
         << Again >> null;
         if Iterator.It_Cur >= Blk.Nbr_Child then
            Error := AvhpiErrorIteratorEnd;
            return;
         end if;

         Ch := Blk.Children (Iterator.It_Cur);
         Nblk := To_Ghdl_Rtin_Block_Acc (Ch);

         if Iterator.Max2 /= 0 then
            --  A for generate.
            Iterator.It2 := Iterator.It2 + 1;
            if Iterator.It2 >= Iterator.Max2 then
               --  End of loop.
               Iterator.Max2 := 0;
               Iterator.It_Cur := Iterator.It_Cur + 1;
               goto Again;
            else
               pragma Assert (Ch.Kind = Ghdl_Rtik_For_Generate);
               declare
                  Gen : constant Ghdl_Rtin_Generate_Acc :=
                    To_Ghdl_Rtin_Generate_Acc (Ch);
                  Base : Address;
               begin
                  Base := To_Addr_Acc (Iterator.Ctxt.Base + Gen.Loc).all;
                  Base := Base + Iterator.It2 * Gen.Size;
                  Res := (Kind => VhpiForGenerateK,
                          Ctxt => (Base => Base,
                                   Block => Gen.Child));

                  Error := AvhpiErrorOk;
                  return;
               end;
            end if;
         end if;


         Iterator.It_Cur := Iterator.It_Cur + 1;

         case Ch.Kind is
            when Ghdl_Rtik_Process =>
               Res := (Kind => VhpiProcessStmtK,
                       Ctxt => (Base => Iterator.Ctxt.Base + Nblk.Loc,
                                Block => Ch));
               Error := AvhpiErrorOk;
               return;
            when Ghdl_Rtik_Block =>
               Res := (Kind => VhpiBlockStmtK,
                       Ctxt => (Base => Iterator.Ctxt.Base + Nblk.Loc,
                                Block => Ch));
               Error := AvhpiErrorOk;
               return;
            when Ghdl_Rtik_If_Generate =>
               Res := (Kind => VhpiIfGenerateK,
                       Ctxt => Get_If_Case_Generate_Child (Iterator.Ctxt, Ch));
               --  Return only if the condition is true.
               if Res.Ctxt.Base /= Null_Address then
                  Error := AvhpiErrorOk;
                  return;
               end if;
            when Ghdl_Rtik_For_Generate =>
               declare
                  Gen : constant Ghdl_Rtin_Generate_Acc :=
                    To_Ghdl_Rtin_Generate_Acc (Ch);
               begin
                  Res := (Kind => VhpiForGenerateK,
                          Ctxt => (Base => To_Addr_Acc (Iterator.Ctxt.Base
                                                          + Gen.Loc).all,
                                   Block => Gen.Child));
                  Iterator.Max2 :=
                    Get_For_Generate_Length (Gen, Iterator.Ctxt);
                  Iterator.It2 := 0;
                  if Iterator.Max2 > 0 then
                     Iterator.It_Cur := Iterator.It_Cur - 1;
                     Error := AvhpiErrorOk;
                     return;
                  end if;
                  --  If the iterator range is nul, then continue to scan.
               end;
            when Ghdl_Rtik_Instance =>
               Res := (Kind => VhpiCompInstStmtK,
                       Ctxt => Iterator.Ctxt,
                       Inst => To_Ghdl_Rtin_Instance_Acc (Ch));
               Error := AvhpiErrorOk;
               return;
            when others =>
               --  Next one.
               null;
         end case;
      end loop;
   end Vhpi_Scan_Internal_Regions;

   procedure Vhpi_Scan_Root_Design (Iterator : in out VhpiHandleT;
                                    Res : out VhpiHandleT;
                                    Error : out AvhpiErrorT) is
   begin
      if Iterator.It_Cur = 0 then
         Get_Root_Inst (Res);
         Iterator.It_Cur := 1;
         Error := AvhpiErrorOk;
      else
         Error := AvhpiErrorIteratorEnd;
      end if;
   end Vhpi_Scan_Root_Design;

   procedure Rti_To_Handle (Rti : Ghdl_Rti_Access;
                            Ctxt : Rti_Context;
                            Res : out VhpiHandleT)
   is
   begin
      case Rti.Kind is
         when Ghdl_Rtik_Signal =>
            Res := (Kind => VhpiSigDeclK,
                    Ctxt => Ctxt,
                    Obj => To_Ghdl_Rtin_Object_Acc (Rti));
         when Ghdl_Rtik_Port =>
            Res := (Kind => VhpiPortDeclK,
                    Ctxt => Ctxt,
                    Obj => To_Ghdl_Rtin_Object_Acc (Rti));
         when Ghdl_Rtik_Generic =>
            Res := (Kind => VhpiGenericDeclK,
                    Ctxt => Ctxt,
                    Obj => To_Ghdl_Rtin_Object_Acc (Rti));
         when Ghdl_Rtik_Constant =>
            Res := (Kind => VhpiConstDeclK,
                    Ctxt => Ctxt,
                    Obj => To_Ghdl_Rtin_Object_Acc (Rti));
         when Ghdl_Rtik_Subtype_Array =>
            declare
               Atype : constant Ghdl_Rtin_Subtype_Composite_Acc :=
                 To_Ghdl_Rtin_Subtype_Composite_Acc (Rti);
               Bt : constant Ghdl_Rtin_Type_Array_Acc :=
                 To_Ghdl_Rtin_Type_Array_Acc (Atype.Basetype);
            begin
               if Atype.Name = Bt.Name then
                  Res := (Kind => VhpiArrayTypeDeclK,
                          Ctxt => Ctxt,
                          Atype => Rti);
               else
                  Res := (Kind => VhpiSubtypeDeclK,
                          Ctxt => Ctxt,
                          Atype => Rti);
               end if;
            end;
         when Ghdl_Rtik_Type_Array =>
            Res := (Kind => VhpiArrayTypeDeclK,
                    Ctxt => Ctxt,
                    Atype => Rti);
         when Ghdl_Rtik_Type_B1
           | Ghdl_Rtik_Type_E8
           | Ghdl_Rtik_Type_E32 =>
            Res := (Kind => VhpiEnumTypeDeclK,
                    Ctxt => Ctxt,
                    Atype => Rti);
         when Ghdl_Rtik_Type_P32
           | Ghdl_Rtik_Type_P64 =>
            Res := (Kind => VhpiPhysTypeDeclK,
                    Ctxt => Ctxt,
                    Atype => Rti);
         when Ghdl_Rtik_Type_I32
           | Ghdl_Rtik_Type_I64 =>
            Res := (Kind => VhpiIntTypeDeclK,
                    Ctxt => Ctxt,
                    Atype => Rti);
         when Ghdl_Rtik_Subtype_Scalar =>
            Res := (Kind => VhpiSubtypeDeclK,
                    Ctxt => Ctxt,
                    Atype => Rti);
         when others =>
            Res := (Kind => VhpiUndefined,
                    Ctxt => Ctxt);
      end case;
   end Rti_To_Handle;

   procedure Vhpi_Scan_Decls (Iterator : in out VhpiHandleT;
                              Res : out VhpiHandleT;
                              Error : out AvhpiErrorT)
   is
      Blk : Ghdl_Rtin_Block_Acc;
      Ch : Ghdl_Rti_Access;
   begin
      Blk := To_Ghdl_Rtin_Block_Acc (Iterator.Ctxt.Block);

      --  If there is no context, returns now.
      --  This may happen for a unbound compinststmt.
      if Blk = null then
         Error := AvhpiErrorIteratorEnd;
         return;
      end if;

      if Iterator.It2 = 1 then
         case Blk.Common.Kind is
            when Ghdl_Rtik_Architecture =>
               --  Iterate on the entity.
               Blk := To_Ghdl_Rtin_Block_Acc (Blk.Parent);
            when Ghdl_Rtik_Package_Body =>
               --  Iterate on the package.
               Blk := To_Ghdl_Rtin_Block_Acc (Blk.Parent);
            when Ghdl_Rtik_Package =>
               --  Only for std.standard.
               Iterator.It2 := 0;
            when others =>
               Internal_Error ("vhpi_scan_decls");
         end case;
      end if;
      loop
         loop
            exit when Iterator.It_Cur >= Blk.Nbr_Child;

            Ch := Blk.Children (Iterator.It_Cur);

            Iterator.It_Cur := Iterator.It_Cur + 1;

            case Ch.Kind is
               when Ghdl_Rtik_Port
                 | Ghdl_Rtik_Generic
                 | Ghdl_Rtik_Constant
                 | Ghdl_Rtik_Signal
                 | Ghdl_Rtik_Type_Array
                 | Ghdl_Rtik_Subtype_Array
                 | Ghdl_Rtik_Type_E8
                 | Ghdl_Rtik_Type_E32
                 | Ghdl_Rtik_Type_B1
                 | Ghdl_Rtik_Subtype_Scalar =>
                  Rti_To_Handle (Ch, Iterator.Ctxt, Res);
                  if Res.Kind /= VhpiUndefined then
                     Error := AvhpiErrorOk;
                     return;
                  else
                     Internal_Error ("vhpi_scan_decls");
                  end if;
               when others =>
                  null;
            end case;
         end loop;
         case Iterator.It2 is
            when 1 =>
               --  Iterate on the architecture/package decl.
               Iterator.It2 := 0;
               Blk := To_Ghdl_Rtin_Block_Acc (Iterator.Ctxt.Block);
               Iterator.It_Cur := 0;
            when others =>
               exit;
         end case;
      end loop;
      Error := AvhpiErrorIteratorEnd;
   end Vhpi_Scan_Decls;

   procedure Vhpi_Scan_Pack_Insts (Iterator : in out VhpiHandleT;
                                   Res : out VhpiHandleT;
                                   Error : out AvhpiErrorT)
   is
      Blk : Ghdl_Rtin_Block_Acc;
   begin
      Blk := To_Ghdl_Rtin_Block_Acc (Iterator.Ctxt.Block);
      if Iterator.It_Cur >= Blk.Nbr_Child then
         Error := AvhpiErrorIteratorEnd;
         return;
      end if;
      Res := (Kind => VhpiPackInstK,
              Ctxt => (Base => Null_Address,
                       Block => Blk.Children (Iterator.It_Cur)));
      Iterator.It_Cur := Iterator.It_Cur + 1;
      Error := AvhpiErrorOk;
   end Vhpi_Scan_Pack_Insts;

   procedure Vhpi_Scan (Iterator : in out VhpiHandleT;
                        Res : out VhpiHandleT;
                        Error : out AvhpiErrorT)
   is
   begin
      case Iterator.Kind is
         when AvhpiNameIteratorK =>
            case Iterator.N_Type.Kind is
               when Ghdl_Rtik_Subtype_Array =>
                  Vhpi_Scan_Indexed_Name (Iterator, Res, Error);
               when others =>
                  Error := AvhpiErrorHandle;
                  Res := Null_Handle;
            end case;
         when VhpiIteratorK =>
            case Iterator.Rel is
               when VhpiPackInsts =>
                  Vhpi_Scan_Pack_Insts (Iterator, Res, Error);
               when VhpiInternalRegions =>
                  Vhpi_Scan_Internal_Regions (Iterator, Res, Error);
               when VhpiDecls =>
                  Vhpi_Scan_Decls (Iterator, Res, Error);
               when others =>
                  Res := Null_Handle;
                  Error := AvhpiErrorNotImplemented;
            end case;
         when AvhpiRootScopeIteratorK =>
            Vhpi_Scan_Root_Design (Iterator, Res, Error);
         when others =>
            Error := AvhpiErrorHandle;
            Res := Null_Handle;
      end case;
   end Vhpi_Scan;

   function Avhpi_Get_Base_Name (Obj : VhpiHandleT) return Ghdl_C_String
   is
   begin
      case Obj.Kind is
         when VhpiEnumTypeDeclK =>
            return To_Ghdl_Rtin_Type_Enum_Acc (Obj.Atype).Name;
         when VhpiPackInstK
           | VhpiArchBodyK
           | VhpiEntityDeclK
           | VhpiProcessStmtK
           | VhpiBlockStmtK =>
            return To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block).Name;
         when VhpiIfGenerateK
           | VhpiForGenerateK =>
            declare
               --  The context is a generate body.
               Gen : constant Ghdl_Rtin_Block_Acc :=
                 To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block);
            begin
               --  Get the name of the if/for/case generate.
               return To_Ghdl_Rtin_Generate_Acc (Gen.Parent).Name;
            end;
         when VhpiRootInstK =>
            declare
               Blk : Ghdl_Rtin_Block_Acc;
            begin
               --  Get top architecture.
               Blk := To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block);
               --  From architecture to entity.
               Blk := To_Ghdl_Rtin_Block_Acc (Blk.Parent);
               return Blk.Name;
            end;
         when VhpiCompInstStmtK =>
            return Obj.Inst.Name;
         when VhpiSigDeclK
           | VhpiPortDeclK
           | VhpiGenericDeclK
           | VhpiConstDeclK =>
            return Obj.Obj.Name;
         when VhpiSubtypeDeclK =>
            return To_Ghdl_Rtin_Subtype_Scalar_Acc (Obj.Atype).Name;
         when others =>
            return null;
      end case;
   end Avhpi_Get_Base_Name;

   procedure Vhpi_Get_Str (Property : VhpiStrPropertyT;
                           Obj : VhpiHandleT;
                           Res : out Ghdl_C_String) is
   begin
      Res := null;

      case Property is
         when VhpiFileNameP =>
            declare
               Parent : Ghdl_Rti_Access;
            begin
               Parent := Obj.Ctxt.Block;
               while Parent /= null loop
                  case Parent.Kind is
                     when Ghdl_Rtik_Package
                       | Ghdl_Rtik_Package_Body
                       | Ghdl_Rtik_Entity
                       | Ghdl_Rtik_Architecture =>
                        Res :=
                          To_Ghdl_Rtin_Block_Filename_Acc (Parent).Filename;
                        return;
                     when Ghdl_Rtik_Block
                       | Ghdl_Rtik_Process =>
                        Parent :=
                          To_Ghdl_Rtin_Block_Acc (Parent).Parent;
                     when others =>
                        return;
                  end case;
               end loop;
            end;
         when others =>
            null;
      end case;
   end Vhpi_Get_Str;

   procedure Vhpi_Get_Str (Property : VhpiStrPropertyT;
                           Obj : VhpiHandleT;
                           Res : out String;
                           Len : out Natural)
   is
      subtype R_Type is String (1 .. Res'Length);
      R : R_Type renames Res;

      procedure Add (C : Character) is
      begin
         Len := Len + 1;
         if Len <= R_Type'Last then
            R (Len) := C;
         end if;
      end Add;

      procedure Add (Str : String) is
      begin
         for I in Str'Range loop
            Add (Str (I));
         end loop;
      end Add;

      procedure Add (Str : Ghdl_C_String) is
      begin
         if Str = null then
            return;
         end if;
         for I in Str'Range loop
            exit when Str (I) = NUL;
            Add (Str (I));
         end loop;
      end Add;
   begin
      Len := 0;

      case Property is
         when VhpiFileNameP =>
            declare
               Str : Ghdl_C_String;
            begin
               Vhpi_Get_Str (Property, Obj, Str);
               Add (Str);
            end;
         when VhpiNameP =>
            case Obj.Kind is
               when VhpiEnumTypeDeclK =>
                  Add (To_Ghdl_Rtin_Type_Enum_Acc (Obj.Atype).Name);
               when VhpiIntTypeDeclK =>
                  Add (To_Ghdl_Rtin_Type_Scalar_Acc (Obj.Atype).Name);
               when VhpiSubtypeDeclK =>
                  Add (To_Ghdl_Rtin_Subtype_Scalar_Acc (Obj.Atype).Name);
               when VhpiArrayTypeDeclK =>
                  Add (To_Ghdl_Rtin_Type_Array_Acc (Obj.Atype).Name);
               when VhpiPackInstK
                 | VhpiArchBodyK
                 | VhpiEntityDeclK
                 | VhpiProcessStmtK
                 | VhpiBlockStmtK =>
                  Add (To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block).Name);
               when VhpiRootInstK =>
                  declare
                     Blk : Ghdl_Rtin_Block_Acc;
                  begin
                     Blk := To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block);
                     Blk := To_Ghdl_Rtin_Block_Acc (Blk.Parent);
                     Add (Blk.Name);
                  end;
               when VhpiCompInstStmtK =>
                  Add (Obj.Inst.Name);
               when VhpiSigDeclK
                 | VhpiPortDeclK
                 | VhpiGenericDeclK
                 | VhpiConstDeclK =>
                  Add (Obj.Obj.Name);
               when VhpiIfGenerateK =>
                  Add (To_Ghdl_Rtin_Generate_Acc
                         (To_Ghdl_Rtin_Block_Acc
                            (Obj.Ctxt.Block).Parent).Name);
               when VhpiForGenerateK =>
                  declare
                     Blk : constant Ghdl_Rtin_Block_Acc :=
                       To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block);
                     Iter : constant Ghdl_Rtin_Object_Acc :=
                       To_Ghdl_Rtin_Object_Acc (Blk.Children (0));
                     Vptr : constant Ghdl_Value_Ptr := To_Ghdl_Value_Ptr
                       (Loc_To_Addr (Iter.Common.Depth, Iter.Loc, Obj.Ctxt));
                     Iter_Type : Ghdl_Rti_Access;
                     Buf : String (1 .. 12);
                     Buf_Len : Natural;
                  begin
                     --  Add the name of the generate (need to skip the
                     --  generate body).
                     Add (To_Ghdl_Rtin_Generate_Acc (Blk.Parent).Name);
                     Add ('(');
                     Iter_Type := Iter.Obj_Type;
                     if Iter_Type.Kind = Ghdl_Rtik_Subtype_Scalar then
                        Iter_Type := To_Ghdl_Rtin_Subtype_Scalar_Acc
                          (Iter_Type).Basetype;
                     end if;
                     case Iter_Type.Kind is
                        when Ghdl_Rtik_Type_I32 =>
                           Grt.To_Strings.To_String (Buf, Buf_Len, Vptr.I32);
                           Add (Buf (Buf_Len .. Buf'Last));
--                         when Ghdl_Rtik_Type_E8 =>
--                            Disp_Enum_Value
--                              (Stream, Rti, Ghdl_Index_Type (Vptr.E8));
--                         when Ghdl_Rtik_Type_E32 =>
--                            Disp_Enum_Value
--                              (Stream, Rti, Ghdl_Index_Type (Vptr.E32));
--                         when Ghdl_Rtik_Type_B1 =>
--                            Disp_Enum_Value
--                              (Stream, Rti,
--                               Ghdl_Index_Type (Ghdl_B1'Pos (Vptr.B1)));
                        when others =>
                           Add ('?');
                     end case;
                     --Disp_Value (stdout, Iter.Obj_Type, Ctxt, Addr, False);
                     Add (')');
                  end;
               when others =>
                  null;
            end case;
         when VhpiCompNameP =>
            case Obj.Kind is
               when VhpiCompInstStmtK =>
                  declare
                     Comp : Ghdl_Rtin_Component_Acc;
                  begin
                     Comp := To_Ghdl_Rtin_Component_Acc (Obj.Inst.Instance);
                     if Comp.Common.Kind = Ghdl_Rtik_Component then
                        Add (Comp.Name);
                     end if;
                  end;
               when others =>
                  null;
            end case;
         when VhpiLibLogicalNameP =>
            case Obj.Kind is
               when VhpiPackInstK
                 | VhpiArchBodyK
                 | VhpiEntityDeclK =>
                  declare
                     Blk : Ghdl_Rtin_Block_Acc;
                     Lib : Ghdl_Rtin_Type_Scalar_Acc;
                  begin
                     Blk := To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block);
                     if Blk.Common.Kind = Ghdl_Rtik_Package_Body then
                        Blk := To_Ghdl_Rtin_Block_Acc (Blk.Parent);
                     end if;
                     Lib := To_Ghdl_Rtin_Type_Scalar_Acc (Blk.Parent);
                     if Lib.Common.Kind /= Ghdl_Rtik_Library then
                        Internal_Error ("VhpiLibLogicalNameP");
                     end if;
                     Add (Lib.Name);
                  end;
               when others =>
                  null;
            end case;
         when VhpiFullNameP =>
            declare
               Rstr : Rstring;
               Nctxt : Rti_Context;
            begin
               if Obj.Kind = VhpiCompInstStmtK then
                  Get_Instance_Context (Obj.Inst, Obj.Ctxt, Nctxt);
                  Get_Path_Name (Rstr, Nctxt, ':', False);
               else
                  Get_Path_Name (Rstr, Obj.Ctxt, ':', False);
               end if;
               Copy (Rstr, R, Len);
               Free (Rstr);
               case Obj.Kind is
                  when VhpiCompInstStmtK =>
                     null;
                  when VhpiPortDeclK
                    | VhpiSigDeclK =>
                     Add (':');
                     Add (Obj.Obj.Name);
                  when others =>
                     null;
               end case;
            end;
         when others =>
            null;
      end case;
   end Vhpi_Get_Str;

   procedure Vhpi_Handle (Rel : VhpiOneToOneT;
                          Ref : VhpiHandleT;
                          Res : out VhpiHandleT;
                          Error : out AvhpiErrorT)
   is
   begin
      --  Default error.
      Error := AvhpiErrorNotImplemented;
      Res := Null_Handle;

      case Rel is
         when VhpiDesignUnit =>
            case Ref.Kind is
               when VhpiRootInstK =>
                  case Ref.Ctxt.Block.Kind is
                     when Ghdl_Rtik_Architecture =>
                        Res := (Kind => VhpiArchBodyK,
                                Ctxt => Ref.Ctxt);
                        Error := AvhpiErrorOk;
                        return;
                     when others =>
                        return;
                  end case;
               when VhpiCompInstStmtK =>
                  Res := (Kind => VhpiArchBodyK,
                          Ctxt => Null_Context);
                  Get_Instance_Context (Ref.Inst, Ref.Ctxt, Res.Ctxt);
                  if Res.Ctxt = Null_Context then
                     --  Component is not bound.
                     Res := Null_Handle;
                  else
                     pragma Assert
                       (Ref.Ctxt.Block.Kind = Ghdl_Rtik_Architecture);
                     null;
                  end if;
                  Error := AvhpiErrorOk;
                  return;
               when others =>
                  return;
            end case;

         when VhpiPrimaryUnit =>
            case Ref.Kind is
               when VhpiArchBodyK =>
                  declare
                     Rti : Ghdl_Rti_Access;
                     Ent : Ghdl_Rtin_Block_Acc;
                  begin
                     Rti := To_Ghdl_Rtin_Block_Acc (Ref.Ctxt.Block).Parent;
                     Ent := To_Ghdl_Rtin_Block_Acc (Rti);
                     Res := (Kind => VhpiEntityDeclK,
                             Ctxt => (Base => Ref.Ctxt.Base + Ent.Loc,
                                      Block => Rti));
                     Error := AvhpiErrorOk;
                  end;
               when others =>
                  return;
            end case;

         when VhpiIterScheme =>
            case Ref.Kind is
               when VhpiForGenerateK =>
                  declare
                     Blk : constant Ghdl_Rtin_Block_Acc :=
                       To_Ghdl_Rtin_Block_Acc (Ref.Ctxt.Block);
                     Iter : Ghdl_Rtin_Object_Acc;
                  begin
                     Iter := To_Ghdl_Rtin_Object_Acc (Blk.Children (0));
                     Res := (Kind => VhpiConstDeclK,
                             Ctxt => Ref.Ctxt,
                             Obj => Iter);
                     Error := AvhpiErrorOk;
                  end;
               when others =>
                  return;
            end case;

         when VhpiSubtype =>
            case Ref.Kind is
               when VhpiPortDeclK
                  | VhpiSigDeclK
                  | VhpiGenericDeclK
                  | VhpiConstDeclK =>
                  Res := (Kind => VhpiSubtypeIndicK,
                          Ctxt => Ref.Ctxt,
                          Atype => Ref.Obj.Obj_Type);
                  Error := AvhpiErrorOk;
               when VhpiIndexedNameK =>
                  Res := (Kind => VhpiSubtypeIndicK,
                          Ctxt => Ref.Ctxt,
                          Atype => Ref.N_Type);
                  Error := AvhpiErrorOk;
               when others =>
                  return;
            end case;

         when VhpiTypeMark =>
            case Ref.Kind is
               when VhpiSubtypeIndicK =>
                  --  FIXME: if the subtype is anonymous, return the base type.
                  Rti_To_Handle (Ref.Atype, Ref.Ctxt, Res);
                  if Res.Kind /= VhpiUndefined then
                     Error := AvhpiErrorOk;
                  end if;
                  return;
               when others =>
                  return;
            end case;

         when VhpiBaseType =>
            declare
               Atype : Ghdl_Rti_Access;
            begin
               case Ref.Kind is
                  when VhpiSubtypeIndicK
                     | VhpiSubtypeDeclK
                     | VhpiArrayTypeDeclK =>
                     Atype := Ref.Atype;
                  when VhpiGenericDeclK
                     | VhpiConstDeclK
                     | VhpiSigDeclK =>
                     Atype := Ref.Obj.Obj_Type;
                  when VhpiIndexedNameK =>
                     Atype := Ref.N_Type;
                  when others =>
                     return;
               end case;
               case Atype.Kind is
                  when Ghdl_Rtik_Subtype_Array =>
                     Rti_To_Handle
                       (To_Ghdl_Rtin_Subtype_Composite_Acc (Atype).Basetype,
                        Ref.Ctxt, Res);
                     if Res.Kind /= VhpiUndefined then
                        Error := AvhpiErrorOk;
                     end if;
                  when Ghdl_Rtik_Subtype_Scalar =>
                     Rti_To_Handle
                       (To_Ghdl_Rtin_Subtype_Scalar_Acc (Atype).Basetype,
                        Ref.Ctxt, Res);
                     if Res.Kind /= VhpiUndefined then
                        Error := AvhpiErrorOk;
                     end if;
                  when Ghdl_Rtik_Type_Array =>
                     Res := Ref;
                     Error := AvhpiErrorOk;
                  when others =>
                     return;
               end case;
            end;

         when VhpiElemSubtype =>
            declare
               Base_Type : Ghdl_Rti_Access;
            begin
               case Ref.Atype.Kind is
                  when Ghdl_Rtik_Subtype_Array =>
                     Base_Type :=
                       To_Ghdl_Rtin_Subtype_Composite_Acc (Ref.Atype).Basetype;
                  when Ghdl_Rtik_Type_Array =>
                     Base_Type := Ref.Atype;
                  when others =>
                     return;
               end case;
               Rti_To_Handle (To_Ghdl_Rtin_Type_Array_Acc (Base_Type).Element,
                              Ref.Ctxt, Res);
               if Res.Kind /= VhpiUndefined then
                  Error := AvhpiErrorOk;
               end if;
            end;

         when VhpiBaseName =>
            case Ref.Kind is
               when VhpiIndexedNameK =>
                  Rti_To_Handle
                    (To_Ghdl_Rti_Access (Ref.N_Obj), Ref.Ctxt, Res);
                  if Res.Kind /= VhpiUndefined then
                     Error := AvhpiErrorOk;
                  end if;
               when others =>
                  null;
            end case;

         when others =>
            null;
      end case;
   end Vhpi_Handle;

   procedure Constraints_By_Index (Ref : VhpiHandleT;
                                   Index : Natural;
                                   Res : out VhpiHandleT;
                                   Error : out AvhpiErrorT) is
   begin
      --  Default error.
      Error := AvhpiErrorNotImplemented;

      case Ref.Kind is
         when VhpiSubtypeIndicK =>
            if Ref.Atype.Kind = Ghdl_Rtik_Subtype_Array then
               declare
                  Arr_Subtype : constant Ghdl_Rtin_Subtype_Composite_Acc
                    := To_Ghdl_Rtin_Subtype_Composite_Acc (Ref.Atype);
                  Basetype : constant Ghdl_Rtin_Type_Array_Acc :=
                    To_Ghdl_Rtin_Type_Array_Acc (Arr_Subtype.Basetype);
                  Idx : constant Ghdl_Index_Type := Ghdl_Index_Type (Index);
                  Layout : Address;
                  Bounds : Ghdl_Range_Array (0 .. Basetype.Nbr_Dim - 1);
                  Range_Basetype : Ghdl_Rti_Access;
               begin
                  if Idx not in 1 .. Basetype.Nbr_Dim then
                     Res := Null_Handle;
                     Error := AvhpiErrorBadIndex;
                     return;
                  end if;
                  --  constraint type is basetype.indexes (idx - 1)
                  Layout := Loc_To_Addr (Arr_Subtype.Common.Depth,
                                         Arr_Subtype.Layout, Ref.Ctxt);
                  Bound_To_Range
                    (Array_Layout_To_Bounds (Layout), Basetype, Bounds);
                  Res := (Kind => VhpiIntRangeK,
                          Ctxt => Ref.Ctxt,
                          Rng_Type => Basetype.Indexes (Idx - 1),
                          Rng_Addr => Bounds (Idx - 1));
                  Range_Basetype := Get_Base_Type (Res.Rng_Type);
                  case Range_Basetype.Kind is
                     when Ghdl_Rtik_Type_I32 =>
                        null;
                     when Ghdl_Rtik_Type_E8
                       | Ghdl_Rtik_Type_E32 =>
                        Res := (Kind => VhpiEnumRangeK,
                                Ctxt => Ref.Ctxt,
                                Rng_Type => Res.Rng_Type,
                                Rng_Addr => Res.Rng_Addr);
                     when others =>
                        Internal_Error ("vhpi_handle_by_index/constraint");
                  end case;
                  Error := AvhpiErrorOk;
               end;
            end if;
         when others =>
            return;
      end case;
   end Constraints_By_Index;

   procedure Indexed_Names_By_Index (Ref : VhpiHandleT;
                                     Index : Natural;
                                     Res : out VhpiHandleT;
                                     Error : out AvhpiErrorT)
   is
      Base_Type, El_Type : VhpiHandleT;
   begin
      --  Default error.
      Error := AvhpiErrorNotImplemented;

      Vhpi_Handle (VhpiBaseType, Ref, Base_Type, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;
      if Vhpi_Get_Kind (Base_Type) /= VhpiArrayTypeDeclK then
         Error := AvhpiErrorBadRel;
         return;
      end if;
      Vhpi_Handle (VhpiElemSubtype, Base_Type, El_Type, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;
      Res := (Kind => VhpiIndexedNameK,
              Ctxt => Ref.Ctxt,
              N_Addr => Avhpi_Get_Address (Ref),
              N_Type => El_Type.Atype,
              N_Idx => Ghdl_Index_Type (Index),
              N_Obj => Ref.Obj);
      if Res.N_Addr = Null_Address then
         Error := AvhpiErrorBadRel;
         return;
      end if;
      --  Note: the index is a flat index (ie an offset).
      --  TODO: check with length ?
      Res.N_Addr := Add_Index (Res.Ctxt, Res.N_Addr, Res.N_Obj, Res.N_Type,
                               Ghdl_Index_Type (Index));
   end Indexed_Names_By_Index;

   procedure Vhpi_Handle_By_Index (Rel : VhpiOneToManyT;
                                   Ref : VhpiHandleT;
                                   Index : Natural;
                                   Res : out VhpiHandleT;
                                   Error : out AvhpiErrorT) is
   begin
      case Rel is
         when VhpiConstraints =>
            Constraints_By_Index (Ref, Index, Res, Error);

         when VhpiIndexedNames =>
            Indexed_Names_By_Index (Ref, Index, Res, Error);

         when others =>
            Res := Null_Handle;
            Error := AvhpiErrorNotImplemented;
      end case;
   end Vhpi_Handle_By_Index;

   procedure Vhpi_Handle_By_Array_Index (Ref : VhpiHandleT;
                                         Index : VhpiIntT;
                                         Res : out VhpiHandleT;
                                         Error : out AvhpiErrorT)
   is
      Typ : VhpiHandleT;
      Rng : VhpiHandleT;
      Is_Up : VhpiIntT;
      Left, Right : VhpiIntT;
      Off : Natural;
   begin
      --  Get object subtype.
      Vhpi_Handle (VhpiSubtype, Ref, Typ, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;

      --  Get the range
      Vhpi_Handle_By_Index (VhpiConstraints, Typ, 1, Rng, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;

      --  Get the range bounds
      Vhpi_Get (VhpiIsUpP, Rng, Is_Up, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;
      Vhpi_Get (VhpiLeftBoundP, Rng, Left, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;
      Vhpi_Get (VhpiRightBoundP, Rng, Right, Error);
      if Error /= AvhpiErrorOk then
         return;
      end if;

      --  Compute the offset
      if Is_Up /= 0 then
         if Index < Left or Index > Right then
            Error := AvhpiErrorBadIndex;
            return;
         end if;
         Off := Natural (Index - Left);
      else
         if Index > Left or Index < Right then
            Error := AvhpiErrorBadIndex;
            return;
         end if;
         Off := Natural (Left - Index);
      end if;

      Indexed_Names_By_Index (Ref, Off, Res, Error);
   end Vhpi_Handle_By_Array_Index;

   procedure Vhpi_Get (Property : VhpiIntPropertyT;
                       Obj : VhpiHandleT;
                       Res : out VhpiIntT;
                       Error : out AvhpiErrorT)
   is
   begin
      --  Default error.
      Error := AvhpiErrorNotImplemented;

      case Property is
         when VhpiIsUpP =>
            if Obj.Kind /= VhpiIntRangeK then
               Res := 0;
               Error := AvhpiErrorBadRel;
               return;
            end if;
            Error := AvhpiErrorOk;
            case Get_Base_Type (Obj.Rng_Type).Kind is
               when Ghdl_Rtik_Type_I32 =>
                  Res := Boolean'Pos (Obj.Rng_Addr.I32.Dir = Dir_To);
               when others =>
                  null;
            end case;

         when VhpiLeftBoundP =>
            if Obj.Kind /= VhpiIntRangeK then
               Res := 0;
               Error := AvhpiErrorBadRel;
               return;
            end if;
            Error := AvhpiErrorOk;
            case Get_Base_Type (Obj.Rng_Type).Kind is
               when Ghdl_Rtik_Type_I32 =>
                  Res := Obj.Rng_Addr.I32.Left;
               when others =>
                  null;
            end case;

         when VhpiRightBoundP =>
            if Obj.Kind /= VhpiIntRangeK then
               Error := AvhpiErrorBadRel;
               return;
            end if;
            Error := AvhpiErrorOk;
            case Get_Base_Type (Obj.Rng_Type).Kind is
               when Ghdl_Rtik_Type_I32 =>
                  Res := Obj.Rng_Addr.I32.Right;
               when others =>
                  null;
            end case;

         when VhpiLineNoP =>
            declare
               Linecol : Ghdl_Index_Type;
            begin
               case Obj.Kind is
                  when VhpiSigDeclK
                    | VhpiPortDeclK
                    | VhpiGenericDeclK
                    | VhpiConstDeclK =>
                     --  Objects.
                     Linecol := Obj.Obj.Linecol;
                  when VhpiPackInstK
                    | VhpiArchBodyK
                    | VhpiEntityDeclK
                    | VhpiProcessStmtK
                    | VhpiBlockStmtK
                    | VhpiIfGenerateK =>
                     --  Blocks.
                     Linecol :=
                       To_Ghdl_Rtin_Block_Acc (Obj.Ctxt.Block).Linecol;
                  when VhpiCompInstStmtK =>
                     Linecol := Obj.Inst.Linecol;
                  when others =>
                     return;
               end case;
               Res := VhpiIntT (Linecol / 256);
               Error := AvhpiErrorOk;
            end;

         when others =>
            null;
      end case;
   end Vhpi_Get;

   procedure Vhpi_Get (Property : VhpiIntPropertyT;
                       Obj : VhpiHandleT;
                       Res : out Boolean;
                       Error : out AvhpiErrorT)
   is
   begin
      case Property is
         when VhpiIsUpP =>
            if Obj.Kind /= VhpiIntRangeK then
               Res := False;
               Error := AvhpiErrorBadRel;
               return;
            end if;
            Error := AvhpiErrorOk;
            case Get_Base_Type (Obj.Rng_Type).Kind is
               when Ghdl_Rtik_Type_I32 =>
                  Res := Obj.Rng_Addr.I32.Dir = Dir_To;
               when others =>
                  Error := AvhpiErrorNotImplemented;
            end case;
            return;
         when others =>
            Error := AvhpiErrorNotImplemented;
      end case;
   end Vhpi_Get;

   function Vhpi_Get_EntityClass (Obj : VhpiHandleT)
                                 return VhpiEntityClassT
   is
   begin
      case Obj.Kind is
         when VhpiArchBodyK =>
            return VhpiArchitectureEC;
         when others =>
            return VhpiErrorEC;
      end case;
   end Vhpi_Get_EntityClass;

   function Vhpi_Get_Kind (Obj : VhpiHandleT) return VhpiClassKindT is
   begin
      return Obj.Kind;
   end Vhpi_Get_Kind;

   function Vhpi_Get_Mode (Obj : VhpiHandleT) return VhpiModeT is
   begin
      case Obj.Kind is
         when VhpiPortDeclK =>
            case Obj.Obj.Common.Mode and Ghdl_Rti_Signal_Mode_Mask is
               when Ghdl_Rti_Signal_Mode_In =>
                  return VhpiInMode;
               when Ghdl_Rti_Signal_Mode_Out =>
                  return VhpiOutMode;
               when Ghdl_Rti_Signal_Mode_Inout =>
                  return VhpiInoutMode;
               when Ghdl_Rti_Signal_Mode_Buffer =>
                  return VhpiBufferMode;
               when Ghdl_Rti_Signal_Mode_Linkage =>
                  return VhpiLinkageMode;
               when others =>
                  return VhpiErrorMode;
            end case;
         when others =>
            return VhpiErrorMode;
      end case;
   end Vhpi_Get_Mode;

   function Avhpi_Get_Rti (Obj : VhpiHandleT) return Ghdl_Rti_Access is
   begin
      case Obj.Kind is
         when VhpiSubtypeIndicK
           | VhpiEnumTypeDeclK =>
            return Obj.Atype;
         when VhpiSigDeclK
           | VhpiPortDeclK
           | VhpiGenericDeclK
           | VhpiConstDeclK =>
            return To_Ghdl_Rti_Access (Obj.Obj);
         when others =>
            return null;
      end case;
   end Avhpi_Get_Rti;

   function Avhpi_Get_Address (Obj : VhpiHandleT) return Address is
   begin
      case Obj.Kind is
         when VhpiPortDeclK
           | VhpiSigDeclK
           | VhpiGenericDeclK
           | VhpiConstDeclK =>
            return Loc_To_Addr (Obj.Ctxt.Block.Depth,
                                Obj.Obj.Loc,
                                Obj.Ctxt);
         when VhpiIndexedNameK =>
            return Obj.N_Addr;
         when others =>
            return Null_Address;
      end case;
   end Avhpi_Get_Address;

   function Avhpi_Get_Context (Obj : VhpiHandleT) return Rti_Context is
   begin
      return Obj.Ctxt;
   end Avhpi_Get_Context;

   function Vhpi_Compare_Handles (Hdl1, Hdl2 : VhpiHandleT)
                                 return Boolean
   is
   begin
      if Hdl1.Kind /= Hdl2.Kind then
         return False;
      end if;
      case Hdl1.Kind is
         when VhpiSubtypeIndicK
           | VhpiSubtypeDeclK
           | VhpiArrayTypeDeclK
           | VhpiPhysTypeDeclK
           | VhpiIntTypeDeclK =>
            return Hdl1.Atype = Hdl2.Atype;
         when others =>
            -- FIXME: todo
            Internal_Error ("vhpi_compare_handles");
      end case;
   end Vhpi_Compare_Handles;

   function Vhpi_Put_Value (Obj : VhpiHandleT; Val : Ghdl_I64)
                           return AvhpiErrorT
   is
      Vptr : Ghdl_Value_Ptr;
      Atype : Ghdl_Rti_Access;
   begin
      case Obj.Kind is
         when VhpiIndexedNameK =>
            Vptr := To_Ghdl_Value_Ptr (Obj.N_Addr);
            Atype := Obj.N_Type;
         when VhpiGenericDeclK =>
            --  Putting values for generics is necessary to support SDF
            --  annotations.
            Vptr := To_Ghdl_Value_Ptr (Avhpi_Get_Address (Obj));
            Atype := Obj.Obj.Obj_Type;
         when VhpiConstDeclK =>
            -- Don't support changing values of constants.
            return AvhpiErrorNotImplemented;
         when others =>
            return AvhpiErrorNotImplemented;
      end case;
      case Get_Base_Type (Atype).Kind is
         when Ghdl_Rtik_Type_P64 =>
            null;
         when others =>
            return AvhpiErrorHandle;
      end case;
      Vptr.I64 := Val;
      return AvhpiErrorOk;
   end Vhpi_Put_Value;
end Grt.Avhpi;