1--  Iir to ortho translator.
2--  Copyright (C) 2002 - 2014 Tristan Gingold
3--
4--  This program is free software: you can redistribute it and/or modify
5--  it under the terms of the GNU General Public License as published by
6--  the Free Software Foundation, either version 2 of the License, or
7--  (at your option) any later version.
8--
9--  This program is distributed in the hope that it will be useful,
10--  but WITHOUT ANY WARRANTY; without even the implied warranty of
11--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12--  GNU General Public License for more details.
13--
14--  You should have received a copy of the GNU General Public License
15--  along with this program.  If not, see <gnu.org/licenses>.
16with Interfaces; use Interfaces;
17with Ortho_Nodes; use Ortho_Nodes;
18with Ortho_Ident; use Ortho_Ident;
19with Flags; use Flags;
20with Types; use Types;
21with Errorout; use Errorout;
22with Vhdl.Errors; use Vhdl.Errors;
23with Name_Table; -- use Name_Table;
24with Str_Table;
25with Files_Map;
26with Vhdl.Utils; use Vhdl.Utils;
27with Vhdl.Std_Package; use Vhdl.Std_Package;
28with Vhdl.Sem_Specs;
29with Libraries;
30with Std_Names;
31with Vhdl.Canon;
32with Trans;
33with Trans_Decls; use Trans_Decls;
34with Trans.Chap1;
35with Trans.Chap2;
36with Trans.Chap3;
37with Trans.Chap4;
38with Trans.Chap7;
39with Trans.Chap12;
40with Trans.Rtis;
41with Trans.Helpers2;
42
43package body Translation is
44   use Trans;
45   use Trans.Chap10;
46   use Trans.Helpers;
47   use Trans.Helpers2;
48
49   function Get_Ortho_Decl (Subprg : Iir) return O_Dnode is
50   begin
51      return Get_Info (Subprg).Subprg_Node;
52   end Get_Ortho_Decl;
53
54   function Get_Resolv_Ortho_Decl (Func : Iir) return O_Dnode
55   is
56      Info : Subprg_Resolv_Info_Acc;
57   begin
58      Info := Get_Info (Func).Subprg_Resolv;
59      if Info = null then
60         --  Maybe the resolver is not used.
61         return O_Dnode_Null;
62      else
63         return Info.Resolv_Func;
64      end if;
65   end Get_Resolv_Ortho_Decl;
66
67   function Get_String_As_String (Expr : Iir) return String is
68   begin
69      case Get_Kind (Expr) is
70         when Iir_Kind_String_Literal8 =>
71            declare
72               Len : constant Natural := Natural (Get_String_Length (Expr));
73               Id : constant String8_Id := Get_String8_Id (Expr);
74               Res : String (1 .. Len);
75            begin
76               for I in 1 .. Len loop
77                  Res (I) := Str_Table.Char_String8 (Id, Pos32 (I));
78               end loop;
79               return Res;
80            end;
81         when Iir_Kind_Simple_Aggregate =>
82            declare
83               List : constant Iir_Flist := Get_Simple_Aggregate_List (Expr);
84               Len : constant Natural := Get_Nbr_Elements (List);
85               Res : String (1 .. Len);
86               El : Iir;
87            begin
88               for I in Flist_First .. Flist_Last (List) loop
89                  El := Get_Nth_Element (List, I);
90                  pragma Assert (Get_Kind (El) = Iir_Kind_Enumeration_Literal);
91                  Res (I - Flist_First + 1) :=
92                    Character'Val (Get_Enum_Pos (El));
93               end loop;
94               return Res;
95            end;
96         when others =>
97            if Get_Expr_Staticness (Expr) /= Locally then
98               Error_Msg_Sem
99                 (+Expr, "value of FOREIGN attribute must be locally static");
100               return "";
101            else
102               raise Internal_Error;
103            end if;
104      end case;
105   end Get_String_As_String;
106
107   function Translate_Foreign_Id (Decl : Iir) return Foreign_Info_Type
108   is
109      --  Look for 'FOREIGN.
110      Attr : constant Iir_Attribute_Value :=
111        Vhdl.Sem_Specs.Find_Attribute_Value (Decl, Std_Names.Name_Foreign);
112      pragma Assert (Attr /= Null_Iir);
113      Spec : constant Iir_Attribute_Specification :=
114        Get_Attribute_Specification (Attr);
115      Name : constant String := Get_String_As_String (Get_Expression (Spec));
116      Length : constant Natural := Name'Length;
117   begin
118      if Length = 0 then
119         return Foreign_Bad;
120      end if;
121
122      pragma Assert (Name'First = 1);
123
124      --  Only 'VHPIDIRECT' is recognized.
125      if Length >= 10 and then Name (1 .. 10) = "VHPIDIRECT" then
126         declare
127            Info : Foreign_Info_Type (Foreign_Vhpidirect);
128            P : Natural;
129            Sf, Sl : Natural;
130            Lf, Ll : Natural;
131         begin
132            P := 11;
133
134            --  Skip spaces.
135            while P <= Length and then Name (P) = ' ' loop
136               P := P + 1;
137            end loop;
138            if P > Length then
139               Error_Msg_Sem
140                 (+Spec, "missing subprogram/library name after VHPIDIRECT");
141               Info.Lib_Len := 0;
142               Info.Subprg_Len := 0;
143               return Info;
144            end if;
145            --  Extract library.
146            Lf := P;
147            while P <= Length and then Name (P) /= ' ' loop
148               P := P + 1;
149            end loop;
150            Ll := P - 1;
151            --  Extract subprogram.
152            while P <= Length and then Name (P) = ' ' loop
153               P := P + 1;
154            end loop;
155            Sf := P;
156            while P <= Length and then Name (P) /= ' ' loop
157               P := P + 1;
158            end loop;
159            Sl := P - 1;
160            if P <= Length then
161               Error_Msg_Sem (+Spec, "garbage at end of VHPIDIRECT");
162            end if;
163
164            --  Accept empty library.
165            if Sf > Length then
166               Sf := Lf;
167               Sl := Ll;
168               Lf := 1;
169               Ll := 0;
170            end if;
171
172            Info.Lib_Len := Ll - Lf + 1;
173            Info.Lib_Name (1 .. Info.Lib_Len) := Name (Lf .. Ll);
174
175            Info.Subprg_Len := Sl - Sf + 1;
176            Info.Subprg_Name (1 .. Info.Subprg_Len) := Name (Sf .. Sl);
177            return Info;
178         end;
179      elsif Length = 14
180        and then Name (1 .. 14) = "GHDL intrinsic"
181      then
182         return Foreign_Info_Type'(Kind => Foreign_Intrinsic);
183      else
184         Error_Msg_Sem
185           (+Spec,
186            "value of 'FOREIGN attribute does not begin with VHPIDIRECT");
187         return Foreign_Bad;
188      end if;
189   end Translate_Foreign_Id;
190
191   procedure Gen_Filename (Design_File : Iir)
192   is
193      Info : Design_File_Info_Acc;
194   begin
195      pragma Assert (Current_Filename_Node = O_Dnode_Null);
196
197      Info := Get_Info (Design_File);
198      if Info = null then
199         Info := Add_Info (Design_File, Kind_Design_File);
200         Info.Design_Filename := Create_String
201           (Get_Design_File_Filename (Design_File),
202            Create_Uniq_Identifier, O_Storage_Private);
203      end if;
204      Current_Filename_Node := Info.Design_Filename;
205   end Gen_Filename;
206
207   --  Decorate the tree in order to be usable with the internal simulator.
208   procedure Translate (Unit : Iir_Design_Unit; Main : Boolean)
209   is
210      Design_File : constant Iir_Design_File := Get_Design_File (Unit);
211      Lib_Unit : constant Iir := Get_Library_Unit (Unit);
212      Lib : Iir_Library_Declaration;
213      Lib_Mark, Ent_Mark, Sep_Mark, Unit_Mark : Id_Mark_Type;
214      Id : Name_Id;
215   begin
216      Update_Node_Infos;
217
218      if False then
219         --  No translation for context items.
220         declare
221            El : Iir;
222         begin
223            El := Get_Context_Items (Unit);
224            while El /= Null_Iir loop
225               case Get_Kind (El) is
226                  when Iir_Kind_Use_Clause =>
227                     null;
228                  when Iir_Kind_Library_Clause =>
229                     null;
230                  when others =>
231                     Error_Kind ("translate1", El);
232               end case;
233               El := Get_Chain (El);
234            end loop;
235         end;
236      end if;
237
238      if Flags.Verbose then
239         if Main then
240            Report_Msg (Msgid_Note, Semantic, +Unit,
241                        "translating (with code generation) %n",
242                        (1 => +Lib_Unit));
243         else
244            Report_Msg (Msgid_Note, Semantic, +Unit,
245                        "translating %n", (1 => +Lib_Unit));
246         end if;
247      end if;
248
249      --  Create the prefix for identifiers.
250      Lib := Get_Library (Get_Design_File (Unit));
251      Reset_Identifier_Prefix;
252      if Lib = Libraries.Work_Library then
253         Id := Libraries.Work_Library_Name;
254      else
255         Id := Get_Identifier (Lib);
256      end if;
257      Push_Identifier_Prefix (Lib_Mark, Id);
258
259      if Get_Kind (Lib_Unit) = Iir_Kind_Architecture_Body then
260         --  Put 'ARCH' between the entity name and the architecture name, to
261         --  avoid a name clash with names from entity (eg an entity port with
262         --  the same name as an architecture).
263         Push_Identifier_Prefix (Ent_Mark,
264                                 Get_Identifier (Get_Entity (Lib_Unit)));
265         Push_Identifier_Prefix (Sep_Mark, "ARCH");
266      end if;
267      Id := Get_Identifier (Lib_Unit);
268      if Id /= Null_Identifier then
269         Push_Identifier_Prefix (Unit_Mark, Id);
270      end if;
271
272      if Main then
273         Set_Global_Storage (O_Storage_Public);
274         --  Create the variable containing the current file name.
275         Gen_Filename (Get_Design_File (Unit));
276      else
277         Set_Global_Storage (O_Storage_External);
278      end if;
279
280      declare
281         Pathname : constant String := Files_Map.Get_Pathname
282           (Get_Design_File_Directory (Design_File),
283            Get_Design_File_Filename (Design_File));
284      begin
285         New_Debug_Filename_Decl (Pathname);
286      end;
287
288      Current_Library_Unit := Lib_Unit;
289
290      case Get_Kind (Lib_Unit) is
291         when Iir_Kind_Package_Declaration =>
292            New_Debug_Comment_Decl
293              ("package declaration " & Image_Identifier (Lib_Unit));
294            Chap2.Translate_Package_Declaration (Lib_Unit);
295            if Get_Package_Origin (Lib_Unit) /= Null_Iir
296              and then Get_Package_Body (Lib_Unit) /= Null_Iir
297            then
298               --  Corresponding body for package instantiation.
299               Chap2.Translate_Package_Body (Get_Package_Body (Lib_Unit));
300            end if;
301         when Iir_Kind_Package_Body =>
302            New_Debug_Comment_Decl
303              ("package body " & Image_Identifier (Lib_Unit));
304            Chap2.Translate_Package_Body (Lib_Unit);
305         when Iir_Kind_Package_Instantiation_Declaration =>
306            New_Debug_Comment_Decl
307              ("package instantiation " & Image_Identifier (Lib_Unit));
308            Chap2.Translate_Package_Instantiation_Declaration (Lib_Unit);
309         when Iir_Kind_Entity_Declaration =>
310            New_Debug_Comment_Decl ("entity " & Image_Identifier (Lib_Unit));
311            Chap1.Translate_Entity_Declaration (Lib_Unit);
312         when Iir_Kind_Architecture_Body =>
313            New_Debug_Comment_Decl
314              ("architecture " & Image_Identifier (Lib_Unit));
315            Chap1.Translate_Architecture_Body (Lib_Unit);
316         when Iir_Kind_Configuration_Declaration =>
317            New_Debug_Comment_Decl
318              ("configuration " & Image_Identifier (Lib_Unit));
319            if Id = Null_Identifier then
320               --  Default configuration.
321               declare
322                  Mark : Id_Mark_Type;
323                  Mark_Entity : Id_Mark_Type;
324                  Mark_Arch : Id_Mark_Type;
325                  Mark_Sep : Id_Mark_Type;
326                  Arch : Iir;
327                  Entity : constant Iir := Get_Entity (Lib_Unit);
328               begin
329                  --  Note: this is done inside the architecture identifier.
330                  Push_Identifier_Prefix
331                    (Mark_Entity, Get_Identifier (Entity));
332                  Arch := Get_Block_Specification
333                    (Get_Block_Configuration (Lib_Unit));
334                  Push_Identifier_Prefix (Mark_Sep, "ARCH");
335                  Push_Identifier_Prefix (Mark_Arch, Get_Identifier (Arch));
336                  Push_Identifier_Prefix
337                    (Mark, Name_Table.Get_Identifier ("DEFAULT_CONFIG"));
338                  --  Spec is built during translation of architecture.
339                  Chap1.Translate_Configuration_Declaration_Body (Lib_Unit);
340                  Pop_Identifier_Prefix (Mark);
341                  Pop_Identifier_Prefix (Mark_Arch);
342                  Pop_Identifier_Prefix (Mark_Sep);
343                  Pop_Identifier_Prefix (Mark_Entity);
344               end;
345            else
346               Chap1.Translate_Configuration_Declaration_Decl (Lib_Unit);
347               Chap1.Translate_Configuration_Declaration_Body (Lib_Unit);
348            end if;
349         when Iir_Kind_Context_Declaration =>
350            New_Debug_Comment_Decl ("context " & Image_Identifier (Lib_Unit));
351            null;
352         when others =>
353            Error_Kind ("translate", Lib_Unit);
354      end case;
355
356      Current_Filename_Node := O_Dnode_Null;
357      Current_Library_Unit := Null_Iir;
358
359      if Id /= Null_Identifier then
360         Pop_Identifier_Prefix (Unit_Mark);
361      end if;
362      if Get_Kind (Lib_Unit) = Iir_Kind_Architecture_Body then
363         Pop_Identifier_Prefix (Sep_Mark);
364         Pop_Identifier_Prefix (Ent_Mark);
365      end if;
366      Pop_Identifier_Prefix (Lib_Mark);
367   end Translate;
368
369   procedure Initialize
370   is
371      Interfaces : O_Inter_List;
372      Param : O_Dnode;
373   begin
374      Init_Node_Infos;
375
376      --  Set flags for canon.
377      Vhdl.Canon.Canon_Flag_Add_Labels := True;
378
379      --  Force to unnest subprograms is the code generator doesn't support
380      --  nested subprograms.
381      if not Ortho_Nodes.Has_Nested_Subprograms then
382         Flag_Unnest_Subprograms := True;
383      end if;
384
385      New_Debug_Comment_Decl ("internal declarations, part 1");
386
387      -- Create well known identifiers.
388      Wki_This := Get_Identifier ("this");
389      Wki_Size := Get_Identifier ("size");
390      Wki_Res := Get_Identifier ("res");
391      Wki_Dir_To := Get_Identifier ("dir_to");
392      Wki_Dir_Downto := Get_Identifier ("dir_downto");
393      Wki_Left := Get_Identifier ("left");
394      Wki_Right := Get_Identifier ("right");
395      Wki_Dir := Get_Identifier ("dir");
396      Wki_Length := Get_Identifier ("length");
397      Wki_I := Get_Identifier ("I");
398      Wki_Instance := Get_Identifier ("INSTANCE");
399      Wki_Arch_Instance := Get_Identifier ("ARCH_INSTANCE");
400      Wki_Name := Get_Identifier ("NAME");
401      Wki_Sig := Get_Identifier ("sig");
402      Wki_Obj := Get_Identifier ("OBJ");
403      Wki_Rti := Get_Identifier ("RTI");
404      Wki_Parent := Get_Identifier ("parent");
405      Wki_Filename := Get_Identifier ("filename");
406      Wki_Line := Get_Identifier ("line");
407      Wki_Lo := Get_Identifier ("lo");
408      Wki_Hi := Get_Identifier ("hi");
409      Wki_Mid := Get_Identifier ("mid");
410      Wki_Cmp := Get_Identifier ("cmp");
411      Wki_Upframe := Get_Identifier ("UPFRAME");
412      Wki_Frame := Get_Identifier ("FRAME");
413      Wki_Val := Get_Identifier ("val");
414      Wki_L_Len := Get_Identifier ("l_len");
415      Wki_R_Len := Get_Identifier ("r_len");
416      Wki_Base := Get_Identifier ("BASE");
417      Wki_Bounds := Get_Identifier ("BOUNDS");
418      Wki_Locvars := Get_Identifier ("LOCVARS");
419
420      Sizetype := New_Unsigned_Type (32);
421      New_Type_Decl (Get_Identifier ("__ghdl_size_type"), Sizetype);
422
423      --  Create __ghdl_index_type, which is the type for *all* array index.
424      Ghdl_Index_Type := New_Unsigned_Type (32);
425      New_Type_Decl (Get_Identifier ("__ghdl_index_type"), Ghdl_Index_Type);
426
427      Ghdl_Index_0 := New_Unsigned_Literal (Ghdl_Index_Type, 0);
428      Ghdl_Index_1 := New_Unsigned_Literal (Ghdl_Index_Type, 1);
429      Ghdl_Index_2 := New_Unsigned_Literal (Ghdl_Index_Type, 2);
430      Ghdl_Index_4 := New_Unsigned_Literal (Ghdl_Index_Type, 4);
431      Ghdl_Index_8 := New_Unsigned_Literal (Ghdl_Index_Type, 8);
432
433      Ghdl_I32_Type := New_Signed_Type (32);
434      New_Type_Decl (Get_Identifier ("__ghdl_i32"), Ghdl_I32_Type);
435
436      Ghdl_Real_Type := New_Float_Type;
437      New_Type_Decl (Get_Identifier ("__ghdl_real"), Ghdl_Real_Type);
438
439      Ghdl_I64_Type := New_Signed_Type (64);
440      New_Type_Decl (Get_Identifier ("__ghdl_i64"), Ghdl_I64_Type);
441
442      --  File index for elaborated file object.
443      Ghdl_File_Index_Type := New_Unsigned_Type (32);
444      New_Type_Decl (Get_Identifier ("__ghdl_file_index"),
445                     Ghdl_File_Index_Type);
446      Ghdl_File_Index_Ptr_Type := New_Access_Type (Ghdl_File_Index_Type);
447      New_Type_Decl (Get_Identifier ("__ghdl_file_index_ptr"),
448                     Ghdl_File_Index_Ptr_Type);
449
450      --  Create char, char [] and char *.
451      Char_Type_Node := New_Unsigned_Type (8);
452      New_Type_Decl (Get_Identifier ("__ghdl_char"), Char_Type_Node);
453
454      Chararray_Type := New_Array_Type (Char_Type_Node, Ghdl_Index_Type);
455      New_Type_Decl (Get_Identifier ("__ghdl_chararray"), Chararray_Type);
456
457      Char_Ptr_Type := New_Access_Type (Chararray_Type);
458      New_Type_Decl (Get_Identifier ("__ghdl_char_ptr"), Char_Ptr_Type);
459
460      Ghdl_Index_Ptr_Align := New_Alignof (Char_Ptr_Type, Ghdl_Index_Type);
461
462      Char_Ptr_Array_Type := New_Array_Type (Char_Ptr_Type, Ghdl_Index_Type);
463      New_Type_Decl (Get_Identifier ("__ghdl_char_ptr_array"),
464                     Char_Ptr_Array_Type);
465
466      Char_Ptr_Array_Ptr_Type := New_Access_Type (Char_Ptr_Array_Type);
467      New_Type_Decl (Get_Identifier ("__ghdl_char_ptr_array_ptr"),
468                     Char_Ptr_Array_Ptr_Type);
469
470      --  Generic pointer.
471      Ghdl_Ptr_Type := New_Access_Type (Char_Type_Node);
472      New_Type_Decl (Get_Identifier ("__ghdl_ptr"), Ghdl_Ptr_Type);
473
474      --  Create record
475      --     len : natural;
476      --     str : C_String;
477      --  end record;
478      declare
479         Constr : O_Element_List;
480      begin
481         Start_Record_Type (Constr);
482         New_Record_Field (Constr, Ghdl_Str_Len_Type_Len_Field,
483                           Get_Identifier ("len"), Ghdl_Index_Type);
484         New_Record_Field
485           (Constr, Ghdl_Str_Len_Type_Str_Field,
486            Get_Identifier ("str"), Char_Ptr_Type);
487         Finish_Record_Type (Constr, Ghdl_Str_Len_Type_Node);
488         New_Type_Decl (Get_Identifier ("__ghdl_str_len"),
489                        Ghdl_Str_Len_Type_Node);
490      end;
491
492      Ghdl_Str_Len_Array_Type_Node := New_Array_Type
493        (Ghdl_Str_Len_Type_Node, Ghdl_Index_Type);
494      New_Type_Decl (Get_Identifier ("__ghdl_str_len_array"),
495                     Ghdl_Str_Len_Array_Type_Node);
496
497      -- Create type __ghdl_str_len_ptr is access all __ghdl_str_len
498      Ghdl_Str_Len_Ptr_Node := New_Access_Type (Ghdl_Str_Len_Type_Node);
499      New_Type_Decl (Get_Identifier ("__ghdl_str_len_ptr"),
500                     Ghdl_Str_Len_Ptr_Node);
501
502      -- Create type __ghdl_bool_type is (false, true)
503      New_Boolean_Type (Ghdl_Bool_Type,
504                        Get_Identifier ("false"),
505                        Ghdl_Bool_False_Node,
506                        Get_Identifier ("true"),
507                        Ghdl_Bool_True_Node);
508      New_Type_Decl (Get_Identifier ("__ghdl_bool_type"),
509                     Ghdl_Bool_Type);
510
511      --  __ghdl_bool_array is array (ghdl_index_type) of ghdl_bool_type
512      Ghdl_Bool_Array_Type :=
513        New_Array_Type (Ghdl_Bool_Type, Ghdl_Index_Type);
514      New_Type_Decl
515        (Get_Identifier ("__ghdl_bool_array_type"), Ghdl_Bool_Array_Type);
516
517      --  __ghdl_bool_array_ptr is access __ghdl_bool_array;
518      Ghdl_Bool_Array_Ptr := New_Access_Type (Ghdl_Bool_Array_Type);
519      New_Type_Decl
520        (Get_Identifier ("__ghdl_bool_array_ptr"), Ghdl_Bool_Array_Ptr);
521
522      --  Create:
523      --  type __ghdl_sizes_type is record
524      --     size_val : ghdl_index_type;
525      --     size_sig : ghdl_index_type;
526      --  end record;
527      declare
528         Constr : O_Element_List;
529      begin
530         Start_Record_Type (Constr);
531         New_Record_Field (Constr, Ghdl_Sizes_Val,
532                           Get_Identifier ("size_val"), Ghdl_Index_Type);
533         New_Record_Field (Constr, Ghdl_Sizes_Sig,
534                           Get_Identifier ("size_sig"), Ghdl_Index_Type);
535         Finish_Record_Type (Constr, Ghdl_Sizes_Type);
536         New_Type_Decl (Get_Identifier ("__ghdl_sizes_type"),
537                        Ghdl_Sizes_Type);
538      end;
539
540      --  __ghdl_sizes_ptr is access __ghdl_sizes_type;
541      Ghdl_Sizes_Ptr := New_Access_Type (Ghdl_Sizes_Type);
542      New_Type_Decl (Get_Identifier ("__ghdl_sizes_ptr"), Ghdl_Sizes_Ptr);
543
544      --  Create type ghdl_compare_type is (lt, eq, ge);
545      declare
546         Constr : O_Enum_List;
547      begin
548         Start_Enum_Type  (Constr, 8);
549         New_Enum_Literal (Constr, Get_Identifier ("lt"), Ghdl_Compare_Lt);
550         New_Enum_Literal (Constr, Get_Identifier ("eq"), Ghdl_Compare_Eq);
551         New_Enum_Literal (Constr, Get_Identifier ("gt"), Ghdl_Compare_Gt);
552         Finish_Enum_Type (Constr, Ghdl_Compare_Type);
553         New_Type_Decl (Get_Identifier ("__ghdl_compare_type"),
554                        Ghdl_Compare_Type);
555      end;
556
557      --  Create:
558      --  type __ghdl_location is record
559      --     file : char_ptr_type;
560      --     line : ghdl_i32;
561      --     col : ghdl_i32;
562      --  end record;
563      declare
564         Constr : O_Element_List;
565      begin
566         Start_Record_Type (Constr);
567         New_Record_Field
568           (Constr, Ghdl_Location_Filename_Node, Wki_Filename, Char_Ptr_Type);
569         New_Record_Field
570           (Constr, Ghdl_Location_Line_Node, Wki_Line, Ghdl_I32_Type);
571         New_Record_Field (Constr, Ghdl_Location_Col_Node,
572                           Get_Identifier ("col"),
573                           Ghdl_I32_Type);
574         Finish_Record_Type (Constr, Ghdl_Location_Type_Node);
575         New_Type_Decl (Get_Identifier ("__ghdl_location"),
576                        Ghdl_Location_Type_Node);
577      end;
578      -- Create type __ghdl_location_ptr is access __ghdl_location;
579      Ghdl_Location_Ptr_Node := New_Access_Type (Ghdl_Location_Type_Node);
580      New_Type_Decl (Get_Identifier ("__ghdl_location_ptr"),
581                     Ghdl_Location_Ptr_Node);
582
583      --  Create type ghdl_dir_type is (dir_to, dir_downto);
584      declare
585         Constr : O_Enum_List;
586      begin
587         Start_Enum_Type (Constr, 8);
588         New_Enum_Literal (Constr, Wki_Dir_To, Ghdl_Dir_To_Node);
589         New_Enum_Literal (Constr, Wki_Dir_Downto, Ghdl_Dir_Downto_Node);
590         Finish_Enum_Type (Constr, Ghdl_Dir_Type_Node);
591         New_Type_Decl (Get_Identifier ("__ghdl_dir_type"),
592                        Ghdl_Dir_Type_Node);
593      end;
594
595      --  Create __ghdl_signal_ptr (incomplete type).
596      New_Uncomplete_Record_Type (Ghdl_Signal_Type);
597      New_Type_Decl (Get_Identifier ("__ghdl_signal"), Ghdl_Signal_Type);
598
599      Ghdl_Signal_Ptr := New_Access_Type (Ghdl_Signal_Type);
600      New_Type_Decl (Get_Identifier ("__ghdl_signal_ptr"), Ghdl_Signal_Ptr);
601
602      --  Create void* __ghdl_alloc (unsigned size);
603      Start_Function_Decl (Interfaces, Get_Identifier ("__ghdl_alloc"),
604                           O_Storage_External, Ghdl_Ptr_Type);
605      New_Interface_Decl (Interfaces, Param, Wki_Size, Sizetype);
606      Finish_Subprogram_Decl (Interfaces, Ghdl_Alloc_Ptr);
607
608      --  procedure __ghdl_program_error (filename : char_ptr_type;
609      --                                  line : ghdl_i32;
610      --                                  code : ghdl_index_type);
611      Start_Procedure_Decl
612        (Interfaces, Get_Identifier ("__ghdl_program_error"),
613         O_Storage_External);
614      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
615      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
616      New_Interface_Decl
617        (Interfaces, Param, Get_Identifier ("code"), Ghdl_Index_Type);
618      Finish_Subprogram_Decl (Interfaces, Ghdl_Program_Error);
619
620      --  procedure __ghdl_bound_check_failed (filename : char_ptr_type;
621      --                                       line : ghdl_i32);
622      Start_Procedure_Decl
623        (Interfaces, Get_Identifier ("__ghdl_bound_check_failed"),
624         O_Storage_External);
625      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
626      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
627      Finish_Subprogram_Decl (Interfaces, Ghdl_Bound_Check_Failed);
628
629      --  procedure __ghdl_direction_check_failed (filename : char_ptr_type;
630      --                                           line : ghdl_i32);
631      Start_Procedure_Decl
632        (Interfaces, Get_Identifier ("__ghdl_direction_check_failed"),
633         O_Storage_External);
634      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
635      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
636      Finish_Subprogram_Decl (Interfaces, Ghdl_Direction_Check_Failed);
637
638      --  Secondary stack subprograms.
639      --  function __ghdl_stack2_allocate (size : ghdl_index_type)
640      --    return ghdl_ptr_type;
641      Start_Function_Decl
642        (Interfaces, Get_Identifier ("__ghdl_stack2_allocate"),
643         O_Storage_External, Ghdl_Ptr_Type);
644      New_Interface_Decl (Interfaces, Param, Wki_Size, Ghdl_Index_Type);
645      Finish_Subprogram_Decl (Interfaces, Ghdl_Stack2_Allocate);
646
647      --  function __ghdl_stack2_mark return ghdl_ptr_type;
648      Start_Function_Decl (Interfaces, Get_Identifier ("__ghdl_stack2_mark"),
649                           O_Storage_External, Ghdl_Ptr_Type);
650      Finish_Subprogram_Decl (Interfaces, Ghdl_Stack2_Mark);
651
652      --  procedure __ghdl_stack2_release (mark : ghdl_ptr_type);
653      Start_Procedure_Decl
654        (Interfaces, Get_Identifier ("__ghdl_stack2_release"),
655         O_Storage_External);
656      New_Interface_Decl (Interfaces, Param, Get_Identifier ("mark"),
657                          Ghdl_Ptr_Type);
658      Finish_Subprogram_Decl (Interfaces, Ghdl_Stack2_Release);
659
660      --  procedure __ghdl_memcpy (dest : ghdl_ptr_type;
661      --                           src  : ghdl_ptr_type;
662      --                           length : ghdl_index_type);
663      Start_Procedure_Decl
664        (Interfaces, Get_Identifier ("__ghdl_memcpy"), O_Storage_External);
665      New_Interface_Decl (Interfaces, Param, Get_Identifier ("dest"),
666                          Ghdl_Ptr_Type);
667      New_Interface_Decl (Interfaces, Param, Get_Identifier ("src"),
668                          Ghdl_Ptr_Type);
669      New_Interface_Decl (Interfaces, Param, Wki_Length, Ghdl_Index_Type);
670      Finish_Subprogram_Decl (Interfaces, Ghdl_Memcpy);
671
672      --  procedure __ghdl_deallocate (ptr : ghdl_ptr_type);
673      Start_Procedure_Decl
674        (Interfaces, Get_Identifier ("__ghdl_deallocate"), O_Storage_External);
675      New_Interface_Decl (Interfaces, Param, Wki_Obj, Ghdl_Ptr_Type);
676      Finish_Subprogram_Decl (Interfaces, Ghdl_Deallocate);
677
678      -- function __ghdl_malloc (length : ghdl_index_type)
679      --    return ghdl_ptr_type;
680      Start_Function_Decl
681        (Interfaces, Get_Identifier ("__ghdl_malloc"), O_Storage_External,
682         Ghdl_Ptr_Type);
683      New_Interface_Decl (Interfaces, Param, Wki_Length, Ghdl_Index_Type);
684      Finish_Subprogram_Decl (Interfaces, Ghdl_Malloc);
685
686      -- function __ghdl_malloc0 (length : ghdl_index_type)
687      --    return ghdl_ptr_type;
688      Start_Function_Decl
689        (Interfaces, Get_Identifier ("__ghdl_malloc0"), O_Storage_External,
690         Ghdl_Ptr_Type);
691      New_Interface_Decl (Interfaces, Param, Wki_Length, Ghdl_Index_Type);
692      Finish_Subprogram_Decl (Interfaces, Ghdl_Malloc0);
693
694      --  function __ghdl_text_file_elaborate return file_index_type;
695      Start_Function_Decl
696        (Interfaces, Get_Identifier ("__ghdl_text_file_elaborate"),
697         O_Storage_External, Ghdl_File_Index_Type);
698      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_File_Elaborate);
699
700      --  function __ghdl_file_elaborate (name : char_ptr_type)
701      --                                 return file_index_type;
702      Start_Function_Decl
703        (Interfaces, Get_Identifier ("__ghdl_file_elaborate"),
704         O_Storage_External, Ghdl_File_Index_Type);
705      New_Interface_Decl (Interfaces, Param, Wki_Name, Char_Ptr_Type);
706      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Elaborate);
707
708      --  procedure __ghdl_file_finalize (file : file_index_type);
709      Start_Procedure_Decl
710        (Interfaces, Get_Identifier ("__ghdl_file_finalize"),
711         O_Storage_External);
712      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
713                          Ghdl_File_Index_Type);
714      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Finalize);
715
716      --  procedure __ghdl_text_file_finalize (file : file_index_type);
717      Start_Procedure_Decl
718        (Interfaces, Get_Identifier ("__ghdl_text_file_finalize"),
719         O_Storage_External);
720      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
721                          Ghdl_File_Index_Type);
722      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_File_Finalize);
723
724      declare
725         procedure Create_Protected_Subprg
726           (Name : String; Subprg : out O_Dnode)
727         is
728         begin
729            Start_Procedure_Decl
730              (Interfaces, Get_Identifier (Name), O_Storage_External);
731            New_Interface_Decl (Interfaces, Param, Wki_Obj, Ghdl_Ptr_Type);
732            Finish_Subprogram_Decl (Interfaces, Subprg);
733         end Create_Protected_Subprg;
734      begin
735         --  procedure __ghdl_protected_enter (obj : ghdl_ptr_type);
736         Create_Protected_Subprg
737           ("__ghdl_protected_enter", Ghdl_Protected_Enter);
738
739         --  procedure __ghdl_protected_leave (obj : ghdl_ptr_type);
740         Create_Protected_Subprg
741           ("__ghdl_protected_leave", Ghdl_Protected_Leave);
742
743         Create_Protected_Subprg
744           ("__ghdl_protected_init", Ghdl_Protected_Init);
745
746         Create_Protected_Subprg
747           ("__ghdl_protected_fini", Ghdl_Protected_Fini);
748      end;
749
750      if Flag_Rti then
751         Rtis.Rti_Initialize;
752      end if;
753
754      --  procedure __ghdl_signal_name_rti
755      --       (obj : ghdl_rti_access;
756      --        ctxt : ghdl_rti_access;
757      --        addr : ghdl_ptr_type);
758      Start_Procedure_Decl
759        (Interfaces, Get_Identifier ("__ghdl_signal_name_rti"),
760         O_Storage_External);
761      New_Interface_Decl (Interfaces, Param, Wki_Obj, Rtis.Ghdl_Rti_Access);
762      New_Interface_Decl (Interfaces, Param, Get_Identifier ("ctxt"),
763                          Rtis.Ghdl_Rti_Access);
764      New_Interface_Decl (Interfaces, Param, Get_Identifier ("addr"),
765                          Ghdl_Ptr_Type);
766      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Name_Rti);
767
768      declare
769         --  procedure NAME (this : ghdl_ptr_type;
770         --                  proc : ghdl_ptr_type;
771         --                  ctxt : ghdl_rti_access;
772         --                  addr : ghdl_ptr_type);
773         procedure Create_Process_Register (Name : String; Res : out O_Dnode)
774         is
775         begin
776            Start_Procedure_Decl
777              (Interfaces, Get_Identifier (Name), O_Storage_External);
778            New_Interface_Decl
779              (Interfaces, Param, Wki_This, Ghdl_Ptr_Type);
780            New_Interface_Decl
781              (Interfaces, Param, Get_Identifier ("proc"), Ghdl_Ptr_Type);
782            New_Interface_Decl (Interfaces, Param, Get_Identifier ("ctxt"),
783                                Rtis.Ghdl_Rti_Access);
784            New_Interface_Decl (Interfaces, Param, Get_Identifier ("addr"),
785                                Ghdl_Ptr_Type);
786            Finish_Subprogram_Decl (Interfaces, Res);
787         end Create_Process_Register;
788      begin
789         Create_Process_Register ("__ghdl_process_register",
790                                  Ghdl_Process_Register);
791         Create_Process_Register ("__ghdl_sensitized_process_register",
792                                  Ghdl_Sensitized_Process_Register);
793         Create_Process_Register ("__ghdl_postponed_process_register",
794                                  Ghdl_Postponed_Process_Register);
795         Create_Process_Register
796           ("__ghdl_postponed_sensitized_process_register",
797            Ghdl_Postponed_Sensitized_Process_Register);
798      end;
799
800      Start_Procedure_Decl
801        (Interfaces, Get_Identifier ("__ghdl_finalize_register"),
802         O_Storage_External);
803      New_Interface_Decl
804        (Interfaces, Param, Wki_This, Ghdl_Ptr_Type);
805      New_Interface_Decl
806        (Interfaces, Param, Get_Identifier ("proc"), Ghdl_Ptr_Type);
807      Finish_Subprogram_Decl (Interfaces, Ghdl_Finalize_Register);
808   end Initialize;
809
810   procedure Create_Signal_Subprograms (Suffix          : String;
811                                        Val_Type        : O_Tnode;
812                                        Create_Signal   : out O_Dnode;
813                                        Init_Signal     : out O_Dnode;
814                                        Simple_Assign   : out O_Dnode;
815                                        Start_Assign    : out O_Dnode;
816                                        Next_Assign     : out O_Dnode;
817                                        Associate_Value : out O_Dnode;
818                                        Add_Port_Driver : out O_Dnode;
819                                        Driving_Value   : out O_Dnode;
820                                        Force_Drv       : out O_Dnode;
821                                        Force_Eff       : out O_Dnode)
822   is
823      Interfaces : O_Inter_List;
824      Param : O_Dnode;
825   begin
826      --  function __ghdl_create_signal_XXX (val_ptr : ghdl_ptr_type;
827      --                                     resolv_func : ghdl_ptr_type;
828      --                                     resolv_inst : ghdl_ptr_type)
829      --                                     return __ghdl_signal_ptr;
830      Start_Function_Decl
831        (Interfaces, Get_Identifier ("__ghdl_create_signal_" & Suffix),
832         O_Storage_External, Ghdl_Signal_Ptr);
833      New_Interface_Decl
834        (Interfaces, Param, Get_Identifier ("val_ptr"), Ghdl_Ptr_Type);
835      New_Interface_Decl (Interfaces, Param, Get_Identifier ("resolv_func"),
836                          Ghdl_Ptr_Type);
837      New_Interface_Decl (Interfaces, Param, Get_Identifier ("resolv_inst"),
838                          Ghdl_Ptr_Type);
839      Finish_Subprogram_Decl (Interfaces, Create_Signal);
840
841      --  procedure __ghdl_signal_init_XXX (sign : __ghdl_signal_ptr;
842      --                                    val : VAL_TYPE);
843      Start_Procedure_Decl
844        (Interfaces, Get_Identifier ("__ghdl_signal_init_" & Suffix),
845         O_Storage_External);
846      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
847      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
848      Finish_Subprogram_Decl (Interfaces, Init_Signal);
849
850      --  procedure __ghdl_signal_simple_assign_XXX (sign : __ghdl_signal_ptr;
851      --                                             val : VAL_TYPE);
852      Start_Procedure_Decl
853        (Interfaces, Get_Identifier ("__ghdl_signal_simple_assign_" & Suffix),
854         O_Storage_External);
855      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
856      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
857      Finish_Subprogram_Decl (Interfaces, Simple_Assign);
858
859      --  procedure __ghdl_signal_start_assign_XXX (sign : __ghdl_signal_ptr;
860      --                                            reject : std_time;
861      --                                            val : VAL_TYPE;
862      --                                            after : std_time);
863      Start_Procedure_Decl
864        (Interfaces, Get_Identifier ("__ghdl_signal_start_assign_" & Suffix),
865         O_Storage_External);
866      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
867      New_Interface_Decl (Interfaces, Param, Get_Identifier ("reject"),
868                          Std_Time_Otype);
869      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
870      New_Interface_Decl (Interfaces, Param, Get_Identifier ("after"),
871                          Std_Time_Otype);
872      Finish_Subprogram_Decl (Interfaces, Start_Assign);
873
874      --  procedure __ghdl_signal_next_assign_XXX (sign : __ghdl_signal_ptr;
875      --                                            val : VAL_TYPE;
876      --                                            after : std_time);
877      Start_Procedure_Decl
878        (Interfaces, Get_Identifier ("__ghdl_signal_next_assign_" & Suffix),
879         O_Storage_External);
880      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
881      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
882      New_Interface_Decl (Interfaces, Param, Get_Identifier ("after"),
883                          Std_Time_Otype);
884      Finish_Subprogram_Decl (Interfaces, Next_Assign);
885
886      --  procedure __ghdl_signal_associate_XXX (sign : __ghdl_signal_ptr;
887      --                                        val : VAL_TYPE);
888      Start_Procedure_Decl
889        (Interfaces, Get_Identifier ("__ghdl_signal_associate_" & Suffix),
890         O_Storage_External);
891      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
892      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
893      Finish_Subprogram_Decl (Interfaces, Associate_Value);
894
895      --  procedure __ghdl_signal_add_port_driver_XX (sign : __ghdl_signal_ptr;
896      --                                              val : VAL_TYPE);
897      Start_Procedure_Decl
898        (Interfaces,
899         Get_Identifier ("__ghdl_signal_add_port_driver_" & Suffix),
900         O_Storage_External);
901      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
902      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
903      Finish_Subprogram_Decl (Interfaces, Add_Port_Driver);
904
905      --  function __ghdl_signal_driving_value_XXX (sign : __ghdl_signal_ptr)
906      --     return VAL_TYPE;
907      Start_Function_Decl
908        (Interfaces, Get_Identifier ("__ghdl_signal_driving_value_" & Suffix),
909         O_Storage_External, Val_Type);
910      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
911      Finish_Subprogram_Decl (Interfaces, Driving_Value);
912
913      --  procedure __ghdl_signal_force_drv_XXX (sign : __ghdl_signal_ptr;
914      --                                         val : VAL_TYPE);
915      Start_Procedure_Decl
916        (Interfaces, Get_Identifier ("__ghdl_signal_force_drv_" & Suffix),
917         O_Storage_External);
918      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
919      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
920      Finish_Subprogram_Decl (Interfaces, Force_Drv);
921
922      --  procedure __ghdl_signal_force_eff_XXX (sign : __ghdl_signal_ptr;
923      --                                         val : VAL_TYPE);
924      Start_Procedure_Decl
925        (Interfaces, Get_Identifier ("__ghdl_signal_force_eff_" & Suffix),
926         O_Storage_External);
927      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
928      New_Interface_Decl (Interfaces, Param, Wki_Val, Val_Type);
929      Finish_Subprogram_Decl (Interfaces, Force_Eff);
930   end Create_Signal_Subprograms;
931
932   --  procedure __ghdl_image_NAME (res : std_string_ptr_node;
933   --                               val : VAL_TYPE;
934   --                               rti : ghdl_rti_access);
935   --
936   --  function __ghdl_value_NAME (val : std_string_ptr_node;
937   --                              rti : ghdl_rti_access);
938   --      return VAL_TYPE;
939   procedure Create_Image_Value_Subprograms (Name : String;
940                                             Val_Type : O_Tnode;
941                                             Has_Td : Boolean;
942                                             Image_Subprg : out O_Dnode;
943                                             Value_Subprg : out O_Dnode)
944   is
945      Interfaces : O_Inter_List;
946      Param : O_Dnode;
947   begin
948      Start_Procedure_Decl
949        (Interfaces, Get_Identifier ("__ghdl_image_" & Name),
950         O_Storage_External);
951      New_Interface_Decl
952        (Interfaces, Param, Get_Identifier ("res"), Std_String_Ptr_Node);
953      New_Interface_Decl
954        (Interfaces, Param, Wki_Val, Val_Type);
955      if Has_Td then
956         New_Interface_Decl
957           (Interfaces, Param, Wki_Rti, Rtis.Ghdl_Rti_Access);
958      end if;
959      Finish_Subprogram_Decl (Interfaces, Image_Subprg);
960
961      Start_Function_Decl
962        (Interfaces, Get_Identifier ("__ghdl_value_" & Name),
963         O_Storage_External, Val_Type);
964      New_Interface_Decl
965        (Interfaces, Param, Wki_Val, Std_String_Ptr_Node);
966      if Has_Td then
967         New_Interface_Decl
968           (Interfaces, Param, Get_Identifier ("rti"), Rtis.Ghdl_Rti_Access);
969      end if;
970      Finish_Subprogram_Decl (Interfaces, Value_Subprg);
971   end Create_Image_Value_Subprograms;
972
973   --  function __ghdl_std_ulogic_match_NAME (l : __ghdl_e8; r : __ghdl_e8)
974   --    return __ghdl_e8;
975   procedure Create_Std_Ulogic_Match_Subprogram (Name : String;
976                                                 Subprg : out O_Dnode)
977   is
978      Interfaces : O_Inter_List;
979      Param : O_Dnode;
980   begin
981      Start_Function_Decl
982        (Interfaces, Get_Identifier ("__ghdl_std_ulogic_match_" & Name),
983         O_Storage_External, Ghdl_I32_Type);
984      New_Interface_Decl (Interfaces, Param, Wki_Left, Ghdl_I32_Type);
985      New_Interface_Decl (Interfaces, Param, Wki_Right, Ghdl_I32_Type);
986      Finish_Subprogram_Decl (Interfaces, Subprg);
987   end Create_Std_Ulogic_Match_Subprogram;
988
989   --  function __ghdl_std_ulogic_array_match_NAME
990   --    (l : __ghdl_ptr; l_len : ghdl_index_type;
991   --     r : __ghdl_ptr; r_len : ghdl_index_type)
992   --    return __ghdl_i32;
993   procedure Create_Std_Ulogic_Array_Match_Subprogram (Name : String;
994                                                       Subprg : out O_Dnode)
995   is
996      Interfaces : O_Inter_List;
997      Param : O_Dnode;
998   begin
999      Start_Function_Decl
1000        (Interfaces, Get_Identifier ("__ghdl_std_ulogic_array_match_" & Name),
1001         O_Storage_External, Ghdl_I32_Type);
1002      New_Interface_Decl (Interfaces, Param, Wki_Left, Ghdl_Ptr_Type);
1003      New_Interface_Decl (Interfaces, Param, Wki_L_Len, Ghdl_Index_Type);
1004      New_Interface_Decl (Interfaces, Param, Wki_Right, Ghdl_Ptr_Type);
1005      New_Interface_Decl (Interfaces, Param, Wki_R_Len, Ghdl_Index_Type);
1006      Finish_Subprogram_Decl (Interfaces, Subprg);
1007   end Create_Std_Ulogic_Array_Match_Subprogram;
1008
1009   --  procedure NAME (res : std_string_ptr_node;
1010   --                  val : VAL_TYPE;
1011   --                  ARG2_NAME : ARG2_TYPE);
1012   procedure Create_To_String_Subprogram (Name : String;
1013                                          Subprg : out O_Dnode;
1014                                          Val_Type : O_Tnode;
1015                                          Arg2_Type : O_Tnode := O_Tnode_Null;
1016                                          Arg2_Id : O_Ident := O_Ident_Nul;
1017                                          Arg3_Type : O_Tnode := O_Tnode_Null;
1018                                          Arg3_Id : O_Ident := O_Ident_Nul)
1019   is
1020      Interfaces : O_Inter_List;
1021      Param : O_Dnode;
1022   begin
1023      Start_Procedure_Decl
1024        (Interfaces, Get_Identifier (Name), O_Storage_External);
1025      New_Interface_Decl
1026        (Interfaces, Param, Wki_Res, Std_String_Ptr_Node);
1027      New_Interface_Decl
1028        (Interfaces, Param, Wki_Val, Val_Type);
1029      if Arg2_Type /= O_Tnode_Null then
1030         New_Interface_Decl
1031           (Interfaces, Param, Arg2_Id, Arg2_Type);
1032         if Arg3_Type /= O_Tnode_Null then
1033            New_Interface_Decl
1034              (Interfaces, Param, Arg3_Id, Arg3_Type);
1035         end if;
1036      end if;
1037      Finish_Subprogram_Decl (Interfaces, Subprg);
1038   end Create_To_String_Subprogram;
1039
1040   --  Do internal declarations that need std.standard declarations.
1041   procedure Post_Initialize
1042   is
1043      Interfaces : O_Inter_List;
1044      Rec : O_Element_List;
1045      Param : O_Dnode;
1046      Info : Type_Info_Acc;
1047   begin
1048      New_Debug_Comment_Decl ("internal declarations, part 2");
1049
1050      --  Remember some pervasive types.
1051      Info := Get_Info (String_Type_Definition);
1052      Std_String_Node := Info.Ortho_Type (Mode_Value);
1053      Std_String_Ptr_Node := Info.Ortho_Ptr_Type (Mode_Value);
1054
1055      Std_Integer_Otype :=
1056        Get_Ortho_Type (Integer_Type_Definition, Mode_Value);
1057      Std_Real_Otype :=
1058        Get_Ortho_Type (Real_Type_Definition, Mode_Value);
1059      Std_Time_Otype := Get_Ortho_Type (Time_Type_Definition, Mode_Value);
1060
1061      --  __ghdl_now : time;
1062      --  ??? maybe this should be a function ?
1063      New_Var_Decl (Ghdl_Now, Get_Identifier ("__ghdl_now"),
1064                    O_Storage_External, Std_Time_Otype);
1065
1066      --  procedure __ghdl_assert_failed (str : __ghdl_array_template;
1067      --                                  severity : ghdl_int);
1068      --                                  loc : __ghdl_location_acc);
1069
1070      --  procedure __ghdl_report (str : __ghdl_array_template;
1071      --                                  severity : ghdl_int);
1072      --                                  loc : __ghdl_location_acc);
1073      declare
1074         procedure Create_Report_Subprg (Name : String; Subprg : out O_Dnode)
1075         is
1076         begin
1077            Start_Procedure_Decl
1078              (Interfaces, Get_Identifier (Name), O_Storage_External);
1079            New_Interface_Decl
1080              (Interfaces, Param, Get_Identifier ("msg"), Std_String_Ptr_Node);
1081            New_Interface_Decl
1082              (Interfaces, Param, Get_Identifier ("severity"),
1083               Get_Ortho_Type (Severity_Level_Type_Definition, Mode_Value));
1084            New_Interface_Decl (Interfaces, Param, Get_Identifier ("location"),
1085                                Ghdl_Location_Ptr_Node);
1086            Finish_Subprogram_Decl (Interfaces, Subprg);
1087         end Create_Report_Subprg;
1088
1089         procedure Create_Fail_Subprg (Name : String; Subprg : out O_Dnode) is
1090         begin
1091            Start_Procedure_Decl
1092              (Interfaces, Get_Identifier (Name), O_Storage_External);
1093            New_Interface_Decl (Interfaces, Param, Get_Identifier ("location"),
1094                                Ghdl_Location_Ptr_Node);
1095            Finish_Subprogram_Decl (Interfaces, Subprg);
1096         end Create_Fail_Subprg;
1097      begin
1098         Create_Report_Subprg
1099           ("__ghdl_assert_failed", Ghdl_Assert_Failed);
1100         Create_Report_Subprg
1101           ("__ghdl_ieee_assert_failed", Ghdl_Ieee_Assert_Failed);
1102         Create_Report_Subprg ("__ghdl_psl_assert_failed",
1103                               Ghdl_Psl_Assert_Failed);
1104         Create_Report_Subprg ("__ghdl_psl_cover", Ghdl_Psl_Cover);
1105         Create_Report_Subprg ("__ghdl_psl_cover_failed",
1106                               Ghdl_Psl_Cover_Failed);
1107         Create_Report_Subprg ("__ghdl_report", Ghdl_Report);
1108
1109         Create_Fail_Subprg ("__ghdl_psl_assume_failed",
1110                             Ghdl_Psl_Assume_Failed);
1111      end;
1112
1113      --  procedure __ghdl_check_stack_allocation (size : __ghdl_index_type)
1114      Start_Procedure_Decl
1115        (Interfaces, Get_Identifier ("__ghdl_check_stack_allocation"),
1116         O_Storage_External);
1117      New_Interface_Decl (Interfaces, Param, Wki_Val, Ghdl_Index_Type);
1118      Finish_Subprogram_Decl (Interfaces, Ghdl_Check_Stack_Allocation);
1119
1120      if Flag_Check_Stack_Allocation > 0 then
1121         Check_Stack_Allocation_Threshold :=
1122           New_Index_Lit (Unsigned_64 (Flag_Check_Stack_Allocation));
1123      else
1124         Check_Stack_Allocation_Threshold := O_Cnode_Null;
1125      end if;
1126
1127      --  procedure __ghdl_integer_indexed_check_failed
1128      --   (filename : char_ptr_type;
1129      --    line : ghdl_i32;
1130      --    val : standard_integer;
1131      --    rng : integer_range_ptr);
1132      Start_Procedure_Decl
1133        (Interfaces, Get_Identifier ("__ghdl_integer_index_check_failed"),
1134         O_Storage_External);
1135      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
1136      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
1137      New_Interface_Decl (Interfaces, Param, Wki_Val, Std_Integer_Otype);
1138      New_Interface_Decl (Interfaces, Param, Get_Identifier ("rng"),
1139                          Get_Info (Integer_Type_Definition).B.Range_Ptr_Type);
1140      Finish_Subprogram_Decl (Interfaces, Ghdl_Integer_Index_Check_Failed);
1141
1142      --  procedure __ghdl_text_write (file : __ghdl_file_index;
1143      --                               str  : std_string_ptr);
1144      Start_Procedure_Decl
1145        (Interfaces, Get_Identifier ("__ghdl_text_write"), O_Storage_External);
1146      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1147                          Ghdl_File_Index_Type);
1148      New_Interface_Decl (Interfaces, Param, Get_Identifier ("str"),
1149                          Std_String_Ptr_Node);
1150      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_Write);
1151
1152      --  function __ghdl_text_read_length (file : __ghdl_file_index;
1153      --                                    str : std_string_ptr)
1154      --     return std__standard_integer;
1155      Start_Function_Decl
1156        (Interfaces, Get_Identifier ("__ghdl_text_read_length"),
1157         O_Storage_External, Std_Integer_Otype);
1158      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1159                          Ghdl_File_Index_Type);
1160      New_Interface_Decl (Interfaces, Param, Get_Identifier ("str"),
1161                          Std_String_Ptr_Node);
1162      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_Read_Length);
1163
1164      --  procedure __ghdl_write_scalar (file : __ghdl_file_index;
1165      --                                 ptr : __ghdl_ptr_type;
1166      --                                 length : __ghdl_index_type);
1167      Start_Procedure_Decl
1168        (Interfaces, Get_Identifier ("__ghdl_write_scalar"),
1169         O_Storage_External);
1170      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1171                          Ghdl_File_Index_Type);
1172      New_Interface_Decl (Interfaces, Param, Get_Identifier ("ptr"),
1173                          Ghdl_Ptr_Type);
1174      New_Interface_Decl (Interfaces, Param, Wki_Length, Ghdl_Index_Type);
1175      Finish_Subprogram_Decl (Interfaces, Ghdl_Write_Scalar);
1176
1177      --  procedure __ghdl_read_scalar (file : __ghdl_file_index;
1178      --                                ptr : __ghdl_ptr_type;
1179      --                                length : __ghdl_index_type);
1180      Start_Procedure_Decl
1181        (Interfaces, Get_Identifier ("__ghdl_read_scalar"),
1182         O_Storage_External);
1183      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1184                          Ghdl_File_Index_Type);
1185      New_Interface_Decl (Interfaces, Param, Get_Identifier ("ptr"),
1186                          Ghdl_Ptr_Type);
1187      New_Interface_Decl (Interfaces, Param, Wki_Length, Ghdl_Index_Type);
1188      Finish_Subprogram_Decl (Interfaces, Ghdl_Read_Scalar);
1189
1190      --  function __ghdl_real_exp (left : std__standard__real;
1191      --                            right : std__standard__integer)
1192      --   return std__standard__real;
1193      Start_Function_Decl
1194        (Interfaces, Get_Identifier ("__ghdl_real_exp"), O_Storage_External,
1195         Std_Real_Otype);
1196      New_Interface_Decl (Interfaces, Param, Get_Identifier ("left"),
1197                          Std_Real_Otype);
1198      New_Interface_Decl (Interfaces, Param, Get_Identifier ("right"),
1199                          Std_Integer_Otype);
1200      Finish_Subprogram_Decl (Interfaces, Ghdl_Real_Exp);
1201
1202      --  function __ghdl_i32_exp (left : ghdl_i32;
1203      --                           right : std__standard__integer)
1204      --   return ghdl_i32;
1205      Start_Function_Decl
1206        (Interfaces, Get_Identifier ("__ghdl_i32_exp"), O_Storage_External,
1207         Ghdl_I32_Type);
1208      New_Interface_Decl (Interfaces, Param, Wki_Left, Ghdl_I32_Type);
1209      New_Interface_Decl (Interfaces, Param, Wki_Right, Std_Integer_Otype);
1210      Finish_Subprogram_Decl (Interfaces, Ghdl_I32_Exp);
1211
1212      --  function __ghdl_i64_exp (left : ghdl_i64;
1213      --                           right : std__standard__integer)
1214      --   return ghdl_i64;
1215      Start_Function_Decl
1216        (Interfaces, Get_Identifier ("__ghdl_i64_exp"), O_Storage_External,
1217         Ghdl_I64_Type);
1218      New_Interface_Decl (Interfaces, Param, Wki_Left, Ghdl_I64_Type);
1219      New_Interface_Decl (Interfaces, Param, Wki_Right, Std_Integer_Otype);
1220      Finish_Subprogram_Decl (Interfaces, Ghdl_I64_Exp);
1221
1222      --  procedure __ghdl_image_b1 (res : std_string_ptr_node;
1223      --                             val : ghdl_bool_type;
1224      --                             rti : ghdl_rti_access);
1225      Create_Image_Value_Subprograms
1226        ("b1", Ghdl_Bool_Type, True, Ghdl_Image_B1, Ghdl_Value_B1);
1227
1228      --  procedure __ghdl_image_e8 (res : std_string_ptr_node;
1229      --                             val : ghdl_i32_type;
1230      --                             rti : ghdl_rti_access);
1231      Create_Image_Value_Subprograms
1232        ("e8", Ghdl_I32_Type, True, Ghdl_Image_E8, Ghdl_Value_E8);
1233
1234      --  procedure __ghdl_image_e32 (res : std_string_ptr_node;
1235      --                             val : ghdl_i32_type;
1236      --                             rti : ghdl_rti_access);
1237      Create_Image_Value_Subprograms
1238        ("e32", Ghdl_I32_Type, True, Ghdl_Image_E32, Ghdl_Value_E32);
1239
1240      --  procedure __ghdl_image_i32 (res : std_string_ptr_node;
1241      --                              val : ghdl_i32_type);
1242      Create_Image_Value_Subprograms
1243        ("i32", Ghdl_I32_Type, False, Ghdl_Image_I32, Ghdl_Value_I32);
1244
1245      --  procedure __ghdl_image_i64 (res : std_string_ptr_node;
1246      --                              val : ghdl_i64_type);
1247      Create_Image_Value_Subprograms
1248        ("i64", Ghdl_I64_Type, False, Ghdl_Image_I64, Ghdl_Value_I64);
1249
1250      --  procedure __ghdl_image_p32 (res : std_string_ptr_node;
1251      --                              val : ghdl_i32_type;
1252      --                             rti : ghdl_rti_access);
1253      Create_Image_Value_Subprograms
1254        ("p32", Ghdl_I32_Type, True, Ghdl_Image_P32, Ghdl_Value_P32);
1255
1256      --  procedure __ghdl_image_p64 (res : std_string_ptr_node;
1257      --                              val : ghdl_i64_type;
1258      --                             rti : ghdl_rti_access);
1259      Create_Image_Value_Subprograms
1260        ("p64", Ghdl_I64_Type, True, Ghdl_Image_P64, Ghdl_Value_P64);
1261
1262      --  procedure __ghdl_image_f64 (res : std_string_ptr_node;
1263      --                              val : ghdl_real_type);
1264      Create_Image_Value_Subprograms
1265        ("f64", Ghdl_Real_Type, False, Ghdl_Image_F64, Ghdl_Value_F64);
1266
1267      -------------
1268      --  files  --
1269      -------------
1270
1271      --  procedure __ghdl_text_file_open (file : file_index_type;
1272      --                                   mode : Ghdl_I32_Type;
1273      --                                   str : std__standard__string_PTR);
1274      Start_Procedure_Decl
1275        (Interfaces, Get_Identifier ("__ghdl_text_file_open"),
1276         O_Storage_External);
1277      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1278                          Ghdl_File_Index_Type);
1279      New_Interface_Decl (Interfaces, Param, Get_Identifier ("mode"),
1280                          Ghdl_I32_Type);
1281      New_Interface_Decl (Interfaces, Param, Get_Identifier ("str"),
1282                          Std_String_Ptr_Node);
1283      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_File_Open);
1284
1285      --  procedure __ghdl_file_open (file : file_index_type;
1286      --                              mode : Ghdl_I32_Type;
1287      --                              str : std__standard__string_PTR);
1288      Start_Procedure_Decl
1289        (Interfaces, Get_Identifier ("__ghdl_file_open"),
1290         O_Storage_External);
1291      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1292                          Ghdl_File_Index_Type);
1293      New_Interface_Decl (Interfaces, Param, Get_Identifier ("mode"),
1294                          Ghdl_I32_Type);
1295      New_Interface_Decl (Interfaces, Param, Get_Identifier ("str"),
1296                          Std_String_Ptr_Node);
1297      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Open);
1298
1299      --  function __ghdl_text_file_open_status
1300      --    (file : file_index_type;
1301      --     mode : Ghdl_I32_Type;
1302      --     str : std__standard__string_PTR)
1303      --     return ghdl_i32_type;
1304      Start_Function_Decl
1305        (Interfaces, Get_Identifier ("__ghdl_text_file_open_status"),
1306         O_Storage_External, Ghdl_I32_Type);
1307      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1308                          Ghdl_File_Index_Type);
1309      New_Interface_Decl (Interfaces, Param, Get_Identifier ("mode"),
1310                          Ghdl_I32_Type);
1311      New_Interface_Decl (Interfaces, Param, Get_Identifier ("str"),
1312                          Std_String_Ptr_Node);
1313      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_File_Open_Status);
1314
1315      --  function __ghdl_file_open_status (file : file_index_type;
1316      --                                    mode : Ghdl_I32_Type;
1317      --                                    str : std__standard__string_PTR)
1318      --     return ghdl_i32_type;
1319      Start_Function_Decl
1320        (Interfaces, Get_Identifier ("__ghdl_file_open_status"),
1321         O_Storage_External, Ghdl_I32_Type);
1322      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1323                          Ghdl_File_Index_Type);
1324      New_Interface_Decl (Interfaces, Param, Get_Identifier ("mode"),
1325                          Ghdl_I32_Type);
1326      New_Interface_Decl (Interfaces, Param, Get_Identifier ("str"),
1327                          Std_String_Ptr_Node);
1328      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Open_Status);
1329
1330      --  function __ghdl_file_endfile (file : file_index_type)
1331      --    return std_boolean_type_node;
1332      Start_Function_Decl (Interfaces, Get_Identifier ("__ghdl_file_endfile"),
1333                           O_Storage_External, Std_Boolean_Type_Node);
1334      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1335                          Ghdl_File_Index_Type);
1336      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Endfile);
1337
1338      --  procedure __ghdl_text_file_close (file : file_index_type);
1339      Start_Procedure_Decl
1340        (Interfaces, Get_Identifier ("__ghdl_text_file_close"),
1341         O_Storage_External);
1342      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1343                          Ghdl_File_Index_Type);
1344      Finish_Subprogram_Decl (Interfaces, Ghdl_Text_File_Close);
1345
1346      --  procedure __ghdl_file_close (file : file_index_type);
1347      Start_Procedure_Decl (Interfaces, Get_Identifier ("__ghdl_file_close"),
1348                            O_Storage_External);
1349      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1350                          Ghdl_File_Index_Type);
1351      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Close);
1352
1353      --  procedure __ghdl_file_flush (file : file_index_type);
1354      Start_Procedure_Decl (Interfaces, Get_Identifier ("__ghdl_file_flush"),
1355                            O_Storage_External);
1356      New_Interface_Decl (Interfaces, Param, Get_Identifier ("file"),
1357                          Ghdl_File_Index_Type);
1358      Finish_Subprogram_Decl (Interfaces, Ghdl_File_Flush);
1359
1360      ---------------
1361      --  signals  --
1362      ---------------
1363
1364      --  procedure __ghdl_signal_create_resolution
1365      --    (func : ghdl_ptr_type;
1366      --     instance : ghdl_ptr_type;
1367      --     sig : ghdl_ptr_type;
1368      --     nbr_sig : ghdl_index_type);
1369      Start_Procedure_Decl
1370        (Interfaces, Get_Identifier ("__ghdl_signal_create_resolution"),
1371         O_Storage_External);
1372      New_Interface_Decl
1373        (Interfaces, Param, Get_Identifier ("func"), Ghdl_Ptr_Type);
1374      New_Interface_Decl (Interfaces, Param, Wki_Instance, Ghdl_Ptr_Type);
1375      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Ptr_Type);
1376      New_Interface_Decl
1377        (Interfaces, Param, Get_Identifier ("nbr_sig"), Ghdl_Index_Type);
1378      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Create_Resolution);
1379
1380      --  Declarations for signals.
1381      --  Max length of a scalar type.
1382      --  Note: this type is not correctly aligned.  Restricted use only.
1383      --  type __ghdl_scalar_bytes is __ghdl_chararray (0 .. 8);
1384      Ghdl_Scalar_Bytes := New_Array_Subtype
1385        (Chararray_Type,
1386         Char_Type_Node,
1387         New_Unsigned_Literal (Ghdl_Index_Type, 8));
1388      New_Type_Decl (Get_Identifier ("__ghdl_scalar_bytes"),
1389                     Ghdl_Scalar_Bytes);
1390
1391      --  Type __signal_signal is record
1392      Start_Uncomplete_Record_Type (Ghdl_Signal_Type, Rec);
1393      New_Record_Field (Rec, Ghdl_Signal_Driving_Value_Field,
1394                        Get_Identifier ("driving_value"),
1395                        Ghdl_Scalar_Bytes);
1396      New_Record_Field (Rec, Ghdl_Signal_Last_Value_Field,
1397                        Get_Identifier ("last_value"),
1398                        Ghdl_Scalar_Bytes);
1399      New_Record_Field (Rec, Ghdl_Signal_Last_Event_Field,
1400                        Get_Identifier ("last_event"),
1401                        Std_Time_Otype);
1402      New_Record_Field (Rec, Ghdl_Signal_Last_Active_Field,
1403                        Get_Identifier ("last_active"),
1404                        Std_Time_Otype);
1405      New_Record_Field (Rec, Ghdl_Signal_Value_Field,
1406                        Get_Identifier ("value"),
1407                        Ghdl_Ptr_Type);
1408      New_Record_Field (Rec, Ghdl_Signal_Event_Field,
1409                        Get_Identifier ("event"),
1410                        Std_Boolean_Type_Node);
1411      New_Record_Field (Rec, Ghdl_Signal_Active_Field,
1412                        Get_Identifier ("active"),
1413                        Std_Boolean_Type_Node);
1414      New_Record_Field (Rec, Ghdl_Signal_Has_Active_Field,
1415                        Get_Identifier ("has_active"),
1416                        Ghdl_Bool_Type);
1417      Finish_Record_Type (Rec, Ghdl_Signal_Type);
1418
1419      Ghdl_Signal_Ptr_Ptr := New_Access_Type (Ghdl_Signal_Ptr);
1420      New_Type_Decl (Get_Identifier ("__ghdl_signal_ptr_ptr"),
1421                     Ghdl_Signal_Ptr_Ptr);
1422
1423      --  procedure __ghdl_signal_merge_rti
1424      --       (sig : ghdl_signal_ptr; rti : ghdl_rti_access)
1425      Start_Procedure_Decl
1426        (Interfaces, Get_Identifier ("__ghdl_signal_merge_rti"),
1427         O_Storage_External);
1428      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1429      New_Interface_Decl (Interfaces, Param, Wki_Rti, Rtis.Ghdl_Rti_Access);
1430      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Merge_Rti);
1431
1432      --  procedure __ghdl_signal_add_source (targ : __ghdl_signal_ptr;
1433      --                                      src : __ghdl_signal_ptr);
1434      Start_Procedure_Decl
1435        (Interfaces, Get_Identifier ("__ghdl_signal_add_source"),
1436         O_Storage_External);
1437      New_Interface_Decl (Interfaces, Param, Get_Identifier ("targ"),
1438                          Ghdl_Signal_Ptr);
1439      New_Interface_Decl (Interfaces, Param, Get_Identifier ("src"),
1440                          Ghdl_Signal_Ptr);
1441      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Add_Source);
1442
1443      --  procedure __ghdl_signal_effective_value (targ : __ghdl_signal_ptr;
1444      --                                           src : __ghdl_signal_ptr);
1445      Start_Procedure_Decl
1446        (Interfaces, Get_Identifier ("__ghdl_signal_effective_value"),
1447         O_Storage_External);
1448      New_Interface_Decl (Interfaces, Param, Get_Identifier ("targ"),
1449                          Ghdl_Signal_Ptr);
1450      New_Interface_Decl (Interfaces, Param, Get_Identifier ("src"),
1451                          Ghdl_Signal_Ptr);
1452      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Effective_Value);
1453
1454      --  procedure __ghdl_signal_set_disconnect (sig : __ghdl_signal_ptr;
1455      --                                          val : std_time);
1456      Start_Procedure_Decl
1457        (Interfaces, Get_Identifier ("__ghdl_signal_set_disconnect"),
1458         O_Storage_External);
1459      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1460      New_Interface_Decl
1461        (Interfaces, Param, Get_Identifier ("time"), Std_Time_Otype);
1462      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Set_Disconnect);
1463
1464      --  procedure __ghdl_signal_disconnect (sig : __ghdl_signal_ptr);
1465      Start_Procedure_Decl
1466        (Interfaces, Get_Identifier ("__ghdl_signal_disconnect"),
1467         O_Storage_External);
1468      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1469      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Disconnect);
1470
1471      --  function __ghdl_signal_get_nbr_drivers (sig : __ghdl_signal_ptr)
1472      --                                          return ghdl_index_type;
1473      Start_Function_Decl
1474        (Interfaces, Get_Identifier ("__ghdl_signal_get_nbr_drivers"),
1475         O_Storage_External, Ghdl_Index_Type);
1476      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1477      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Get_Nbr_Drivers);
1478
1479      --  function __ghdl_signal_get_nbr_sources (sig : __ghdl_signal_ptr)
1480      --                                          return ghdl_index_type;
1481      Start_Function_Decl
1482        (Interfaces, Get_Identifier ("__ghdl_signal_get_nbr_ports"),
1483         O_Storage_External, Ghdl_Index_Type);
1484      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1485      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Get_Nbr_Ports);
1486
1487      --  function __ghdl_signal_read_driver (sig : __ghdl_signal_ptr;
1488      --                                      num : ghdl_index_type)
1489      --                                     return ghdl_ptr_type;
1490      declare
1491         procedure Create_Signal_Read (Name : String; Subprg : out O_Dnode) is
1492         begin
1493            Start_Function_Decl
1494              (Interfaces, Get_Identifier (Name),
1495               O_Storage_External, Ghdl_Ptr_Type);
1496            New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1497            New_Interface_Decl
1498              (Interfaces, Param, Get_Identifier ("num"), Ghdl_Index_Type);
1499            Finish_Subprogram_Decl (Interfaces, Subprg);
1500         end Create_Signal_Read;
1501      begin
1502         Create_Signal_Read
1503           ("__ghdl_signal_read_driver", Ghdl_Signal_Read_Driver);
1504         Create_Signal_Read
1505           ("__ghdl_signal_read_port", Ghdl_Signal_Read_Port);
1506      end;
1507
1508      --  function __ghdl_signal_driving (sig : __ghdl_signal_ptr)
1509      --                                 return std_boolean;
1510      Start_Function_Decl
1511        (Interfaces, Get_Identifier ("__ghdl_signal_driving"),
1512         O_Storage_External, Std_Boolean_Type_Node);
1513      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1514      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Driving);
1515
1516      --  procedure __ghdl_signal_simple_assign_error
1517      --              (sig : __ghdl_signal_ptr;
1518      --               filename : char_ptr_type;
1519      --               line : ghdl_i32);
1520      Start_Procedure_Decl
1521        (Interfaces, Get_Identifier ("__ghdl_signal_simple_assign_error"),
1522         O_Storage_External);
1523      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1524      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
1525      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
1526      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Simple_Assign_Error);
1527
1528      --  procedure __ghdl_signal_start_assign_error (sign : __ghdl_signal_ptr;
1529      --                                              reject : std_time;
1530      --                                              after : std_time;
1531      --                                              filename : char_ptr_type;
1532      --                                              line : ghdl_i32);
1533      Start_Procedure_Decl
1534        (Interfaces, Get_Identifier ("__ghdl_signal_start_assign_error"),
1535         O_Storage_External);
1536      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1537      New_Interface_Decl (Interfaces, Param, Get_Identifier ("reject"),
1538                          Std_Time_Otype);
1539      New_Interface_Decl (Interfaces, Param, Get_Identifier ("after"),
1540                          Std_Time_Otype);
1541      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
1542      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
1543      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Start_Assign_Error);
1544
1545      --  procedure __ghdl_signal_next_assign_error (sig : __ghdl_signal_ptr;
1546      --                                             after : std_time;
1547      --                                             filename : char_ptr_type;
1548      --                                             line : ghdl_i32);
1549      Start_Procedure_Decl
1550        (Interfaces, Get_Identifier ("__ghdl_signal_next_assign_error"),
1551         O_Storage_External);
1552      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1553      New_Interface_Decl (Interfaces, Param, Get_Identifier ("after"),
1554                          Std_Time_Otype);
1555      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
1556      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
1557      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Next_Assign_Error);
1558
1559      --  procedure __ghdl_signal_start_assign_null (sig : __ghdl_signal_ptr;
1560      --                                             reject : std_time;
1561      --                                             after : std_time);
1562      Start_Procedure_Decl
1563        (Interfaces, Get_Identifier ("__ghdl_signal_start_assign_null"),
1564         O_Storage_External);
1565      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1566      New_Interface_Decl (Interfaces, Param, Get_Identifier ("reject"),
1567                          Std_Time_Otype);
1568      New_Interface_Decl (Interfaces, Param, Get_Identifier ("after"),
1569                          Std_Time_Otype);
1570      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Start_Assign_Null);
1571
1572      --  procedure __ghdl_signal_next_assign_null (sig : __ghdl_signal_ptr;
1573      --                                            after : std_time);
1574      Start_Procedure_Decl
1575        (Interfaces, Get_Identifier ("__ghdl_signal_next_assign_null"),
1576         O_Storage_External);
1577      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1578      New_Interface_Decl (Interfaces, Param, Get_Identifier ("after"),
1579                          Std_Time_Otype);
1580      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Next_Assign_Null);
1581
1582      --  function __ghdl_create_signal_e8 (init_val : ghdl_i32_type)
1583      --                                    return __ghdl_signal_ptr;
1584      --  procedure __ghdl_signal_simple_assign_e8 (sign : __ghdl_signal_ptr;
1585      --                                            val : __ghdl_integer);
1586      Create_Signal_Subprograms ("e8", Ghdl_I32_Type,
1587                                 Ghdl_Create_Signal_E8,
1588                                 Ghdl_Signal_Init_E8,
1589                                 Ghdl_Signal_Simple_Assign_E8,
1590                                 Ghdl_Signal_Start_Assign_E8,
1591                                 Ghdl_Signal_Next_Assign_E8,
1592                                 Ghdl_Signal_Associate_E8,
1593                                 Ghdl_Signal_Add_Port_Driver_E8,
1594                                 Ghdl_Signal_Driving_Value_E8,
1595                                 Ghdl_Signal_Force_Drv_E8,
1596                                 Ghdl_Signal_Force_Eff_E8);
1597
1598      --  function __ghdl_create_signal_e32 (init_val : ghdl_i32_type)
1599      --                                     return __ghdl_signal_ptr;
1600      --  procedure __ghdl_signal_simple_assign_e32 (sign : __ghdl_signal_ptr;
1601      --                                             val : __ghdl_integer);
1602      Create_Signal_Subprograms ("e32", Ghdl_I32_Type,
1603                                 Ghdl_Create_Signal_E32,
1604                                 Ghdl_Signal_Init_E32,
1605                                 Ghdl_Signal_Simple_Assign_E32,
1606                                 Ghdl_Signal_Start_Assign_E32,
1607                                 Ghdl_Signal_Next_Assign_E32,
1608                                 Ghdl_Signal_Associate_E32,
1609                                 Ghdl_Signal_Add_Port_Driver_E32,
1610                                 Ghdl_Signal_Driving_Value_E32,
1611                                 Ghdl_Signal_Force_Drv_E32,
1612                                 Ghdl_Signal_Force_Eff_E32);
1613
1614      --  function __ghdl_create_signal_b1 (init_val : ghdl_bool_type)
1615      --                                    return __ghdl_signal_ptr;
1616      --  procedure __ghdl_signal_simple_assign_b1 (sign : __ghdl_signal_ptr;
1617      --                                            val : ghdl_bool_type);
1618      Create_Signal_Subprograms ("b1", Ghdl_Bool_Type,
1619                                 Ghdl_Create_Signal_B1,
1620                                 Ghdl_Signal_Init_B1,
1621                                 Ghdl_Signal_Simple_Assign_B1,
1622                                 Ghdl_Signal_Start_Assign_B1,
1623                                 Ghdl_Signal_Next_Assign_B1,
1624                                 Ghdl_Signal_Associate_B1,
1625                                 Ghdl_Signal_Add_Port_Driver_B1,
1626                                 Ghdl_Signal_Driving_Value_B1,
1627                                 Ghdl_Signal_Force_Drv_B1,
1628                                 Ghdl_Signal_Force_Eff_B1);
1629
1630      Create_Signal_Subprograms ("i32", Ghdl_I32_Type,
1631                                 Ghdl_Create_Signal_I32,
1632                                 Ghdl_Signal_Init_I32,
1633                                 Ghdl_Signal_Simple_Assign_I32,
1634                                 Ghdl_Signal_Start_Assign_I32,
1635                                 Ghdl_Signal_Next_Assign_I32,
1636                                 Ghdl_Signal_Associate_I32,
1637                                 Ghdl_Signal_Add_Port_Driver_I32,
1638                                 Ghdl_Signal_Driving_Value_I32,
1639                                 Ghdl_Signal_Force_Drv_I32,
1640                                 Ghdl_Signal_Force_Eff_I32);
1641
1642      Create_Signal_Subprograms ("f64", Ghdl_Real_Type,
1643                                 Ghdl_Create_Signal_F64,
1644                                 Ghdl_Signal_Init_F64,
1645                                 Ghdl_Signal_Simple_Assign_F64,
1646                                 Ghdl_Signal_Start_Assign_F64,
1647                                 Ghdl_Signal_Next_Assign_F64,
1648                                 Ghdl_Signal_Associate_F64,
1649                                 Ghdl_Signal_Add_Port_Driver_F64,
1650                                 Ghdl_Signal_Driving_Value_F64,
1651                                 Ghdl_Signal_Force_Drv_F64,
1652                                 Ghdl_Signal_Force_Eff_F64);
1653
1654      Create_Signal_Subprograms ("i64", Ghdl_I64_Type,
1655                                 Ghdl_Create_Signal_I64,
1656                                 Ghdl_Signal_Init_I64,
1657                                 Ghdl_Signal_Simple_Assign_I64,
1658                                 Ghdl_Signal_Start_Assign_I64,
1659                                 Ghdl_Signal_Next_Assign_I64,
1660                                 Ghdl_Signal_Associate_I64,
1661                                 Ghdl_Signal_Add_Port_Driver_I64,
1662                                 Ghdl_Signal_Driving_Value_I64,
1663                                 Ghdl_Signal_Force_Drv_I64,
1664                                 Ghdl_Signal_Force_Eff_I64);
1665
1666      --  procedure __ghdl_signal_release_drv (sig : __ghdl_signal_ptr);
1667      Start_Procedure_Decl
1668        (Interfaces, Get_Identifier ("__ghdl_signal_release_drv"),
1669         O_Storage_External);
1670      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1671      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Release_Drv);
1672
1673      --  procedure __ghdl_signal_release_eff (sig : __ghdl_signal_ptr);
1674      Start_Procedure_Decl
1675        (Interfaces, Get_Identifier ("__ghdl_signal_release_eff"),
1676         O_Storage_External);
1677      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1678      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Release_Eff);
1679
1680      --  procedure __ghdl_process_add_sensitivity (sig : __ghdl_signal_ptr);
1681      Start_Procedure_Decl
1682        (Interfaces, Get_Identifier ("__ghdl_process_add_sensitivity"),
1683         O_Storage_External);
1684      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1685      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Add_Sensitivity);
1686
1687      --  procedure __ghdl_process_add_driver (sig : __ghdl_signal_ptr);
1688      Start_Procedure_Decl
1689        (Interfaces, Get_Identifier ("__ghdl_process_add_driver"),
1690         O_Storage_External);
1691      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1692      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Add_Driver);
1693
1694      --  procedure __ghdl_signal_add_direct_driver (sig : __ghdl_signal_ptr;
1695      --                                             Drv : Ghdl_Ptr_type);
1696      Start_Procedure_Decl
1697        (Interfaces, Get_Identifier ("__ghdl_signal_add_direct_driver"),
1698         O_Storage_External);
1699      New_Interface_Decl
1700        (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1701      New_Interface_Decl
1702        (Interfaces, Param, Get_Identifier ("drv"), Ghdl_Ptr_Type);
1703      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Add_Direct_Driver);
1704
1705      --  procedure __ghdl_signal_direct_assign (sig : __ghdl_signal_ptr);
1706      Start_Procedure_Decl
1707        (Interfaces, Get_Identifier ("__ghdl_signal_direct_assign"),
1708         O_Storage_External);
1709      New_Interface_Decl
1710        (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1711      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Direct_Assign);
1712
1713      declare
1714         procedure Create_Signal_Conversion (Name : String; Res : out O_Dnode)
1715         is
1716         begin
1717            Start_Procedure_Decl
1718              (Interfaces, Get_Identifier (Name), O_Storage_External);
1719            New_Interface_Decl
1720              (Interfaces, Param, Get_Identifier ("func"), Ghdl_Ptr_Type);
1721            New_Interface_Decl
1722              (Interfaces, Param, Wki_Instance, Ghdl_Ptr_Type);
1723            New_Interface_Decl
1724              (Interfaces, Param, Get_Identifier ("src"), Ghdl_Signal_Ptr);
1725            New_Interface_Decl
1726              (Interfaces, Param, Get_Identifier ("src_len"), Ghdl_Index_Type);
1727            New_Interface_Decl
1728              (Interfaces, Param, Get_Identifier ("dst"), Ghdl_Signal_Ptr);
1729            New_Interface_Decl
1730              (Interfaces, Param, Get_Identifier ("dst_len"), Ghdl_Index_Type);
1731            Finish_Subprogram_Decl (Interfaces, Res);
1732         end Create_Signal_Conversion;
1733      begin
1734         --  procedure __ghdl_signal_in_conversion (func : ghdl_ptr_type;
1735         --                                         instance : ghdl_ptr_type;
1736         --                                         src : ghdl_signal_ptr;
1737         --                                         src_len : ghdl_index_type;
1738         --                                         dst : ghdl_signal_ptr;
1739         --                                         dst_len : ghdl_index_type);
1740         Create_Signal_Conversion
1741           ("__ghdl_signal_in_conversion", Ghdl_Signal_In_Conversion);
1742         Create_Signal_Conversion
1743           ("__ghdl_signal_out_conversion", Ghdl_Signal_Out_Conversion);
1744      end;
1745
1746      declare
1747         --  function __ghdl_create_XXX_signal (val_ptr : ghdl_ptr_type;
1748         --                                     val : std_time)
1749         --    return __ghdl_signal_ptr;
1750         procedure Create_Signal_Attribute (Name : String; Res : out O_Dnode)
1751         is
1752         begin
1753            Start_Function_Decl (Interfaces, Get_Identifier (Name),
1754                                 O_Storage_External, Ghdl_Signal_Ptr);
1755            New_Interface_Decl (Interfaces, Param, Get_Identifier ("val_ptr"),
1756                                Ghdl_Ptr_Type);
1757            New_Interface_Decl (Interfaces, Param, Wki_Val, Std_Time_Otype);
1758            Finish_Subprogram_Decl (Interfaces, Res);
1759         end Create_Signal_Attribute;
1760      begin
1761         --  function __ghdl_create_stable_signal (val_ptr : ghdl_ptr_type;
1762         --                                        val : std_time)
1763         --    return __ghdl_signal_ptr;
1764         Create_Signal_Attribute
1765           ("__ghdl_create_stable_signal", Ghdl_Create_Stable_Signal);
1766
1767         --  function __ghdl_create_quiet_signal (val_ptr : ghdl_ptr_type;
1768         --                                       val : std_time)
1769         --    return __ghdl_signal_ptr;
1770         Create_Signal_Attribute
1771           ("__ghdl_create_quiet_signal", Ghdl_Create_Quiet_Signal);
1772
1773         --  function __ghdl_create_transaction_signal
1774         --     (val_ptr : ghdl_ptr_type)
1775         --    return __ghdl_signal_ptr;
1776         Start_Function_Decl
1777           (Interfaces, Get_Identifier ("__ghdl_create_transaction_signal"),
1778            O_Storage_External, Ghdl_Signal_Ptr);
1779         New_Interface_Decl (Interfaces, Param, Get_Identifier ("val_ptr"),
1780                             Ghdl_Ptr_Type);
1781         Finish_Subprogram_Decl (Interfaces, Ghdl_Create_Transaction_Signal);
1782      end;
1783
1784      --  procedure __ghdl_signal_attribute_register_prefix
1785      --    (sig : __ghdl_signal_ptr);
1786      Start_Procedure_Decl
1787        (Interfaces,
1788         Get_Identifier ("__ghdl_signal_attribute_register_prefix"),
1789         O_Storage_External);
1790      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1791      Finish_Subprogram_Decl
1792        (Interfaces, Ghdl_Signal_Attribute_Register_Prefix);
1793
1794      --  function __ghdl_create_delayed_signal (sig : __ghdl_signal_ptr;
1795      --                                         val_ptr : ghdl_ptr_type;
1796      --                                         val : std_time)
1797      --    return __ghdl_signal_ptr;
1798      Start_Function_Decl
1799        (Interfaces, Get_Identifier ("__ghdl_create_delayed_signal"),
1800         O_Storage_External, Ghdl_Signal_Ptr);
1801      New_Interface_Decl (Interfaces, Param, Get_Identifier ("sig"),
1802                          Ghdl_Signal_Ptr);
1803      New_Interface_Decl (Interfaces, Param, Get_Identifier ("val_ptr"),
1804                          Ghdl_Ptr_Type);
1805      New_Interface_Decl (Interfaces, Param, Wki_Val, Std_Time_Otype);
1806      Finish_Subprogram_Decl (Interfaces, Ghdl_Create_Delayed_Signal);
1807
1808      --  function __ghdl_signal_create_guard
1809      --    (val_ptr : Ghdl_Ptr_type;
1810      --     this : ghdl_ptr_type;
1811      --     proc : ghdl_ptr_type;
1812      --     instance_name : __ghdl_instance_name_acc)
1813      --    return __ghdl_signal_ptr;
1814      Start_Function_Decl
1815        (Interfaces, Get_Identifier ("__ghdl_signal_create_guard"),
1816         O_Storage_External, Ghdl_Signal_Ptr);
1817      New_Interface_Decl (Interfaces, Param, Get_Identifier ("val_ptr"),
1818                          Ghdl_Ptr_Type);
1819      New_Interface_Decl (Interfaces, Param, Get_Identifier ("this"),
1820                          Ghdl_Ptr_Type);
1821      New_Interface_Decl (Interfaces, Param, Get_Identifier ("proc"),
1822                          Ghdl_Ptr_Type);
1823      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Create_Guard);
1824
1825      --  procedure __ghdl_signal_guard_dependence (sig : __ghdl_signal_ptr);
1826      Start_Procedure_Decl
1827        (Interfaces, Get_Identifier ("__ghdl_signal_guard_dependence"),
1828         O_Storage_External);
1829      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1830      Finish_Subprogram_Decl (Interfaces, Ghdl_Signal_Guard_Dependence);
1831
1832      --  procedure __ghdl_process_wait_exit (void);
1833      Start_Procedure_Decl
1834        (Interfaces, Get_Identifier ("__ghdl_process_wait_exit"),
1835         O_Storage_External);
1836      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Exit);
1837
1838      --  void __ghdl_process_wait_timeout (time : std_time);
1839      Start_Procedure_Decl
1840        (Interfaces, Get_Identifier ("__ghdl_process_wait_timeout"),
1841         O_Storage_External);
1842      New_Interface_Decl (Interfaces, Param, Get_Identifier ("time"),
1843                          Std_Time_Otype);
1844      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
1845      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
1846      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Timeout);
1847
1848      --  void __ghdl_process_wait_set_timeout (time : std_time);
1849      Start_Procedure_Decl
1850        (Interfaces, Get_Identifier ("__ghdl_process_wait_set_timeout"),
1851         O_Storage_External);
1852      New_Interface_Decl (Interfaces, Param, Get_Identifier ("time"),
1853                          Std_Time_Otype);
1854      New_Interface_Decl (Interfaces, Param, Wki_Filename, Char_Ptr_Type);
1855      New_Interface_Decl (Interfaces, Param, Wki_Line, Ghdl_I32_Type);
1856      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Set_Timeout);
1857
1858      --  void __ghdl_process_wait_add_sensitivity (sig : __ghdl_signal_ptr);
1859      Start_Procedure_Decl
1860        (Interfaces, Get_Identifier ("__ghdl_process_wait_add_sensitivity"),
1861         O_Storage_External);
1862      New_Interface_Decl (Interfaces, Param, Wki_Sig, Ghdl_Signal_Ptr);
1863      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Add_Sensitivity);
1864
1865      --  procedure __ghdl_process_wait_suspend (void);
1866      Start_Procedure_Decl
1867        (Interfaces, Get_Identifier ("__ghdl_process_wait_suspend"),
1868         O_Storage_External);
1869      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Suspend);
1870
1871      --  function __ghdl_process_wait_timed_out return __ghdl_bool_type;
1872      Start_Function_Decl
1873        (Interfaces, Get_Identifier ("__ghdl_process_wait_timed_out"),
1874         O_Storage_External, Ghdl_Bool_Type);
1875      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Timed_Out);
1876
1877      --  void __ghdl_process_wait_close (void);
1878      Start_Procedure_Decl
1879        (Interfaces, Get_Identifier ("__ghdl_process_wait_close"),
1880         O_Storage_External);
1881      Finish_Subprogram_Decl (Interfaces, Ghdl_Process_Wait_Close);
1882
1883      declare
1884         procedure Create_Get_Name (Name : String; Res : out O_Dnode)
1885         is
1886         begin
1887            Start_Procedure_Decl
1888              (Interfaces, Get_Identifier (Name), O_Storage_External);
1889            New_Interface_Decl
1890              (Interfaces, Param, Wki_Res, Std_String_Ptr_Node);
1891            New_Interface_Decl (Interfaces, Param, Get_Identifier ("ctxt"),
1892                                Rtis.Ghdl_Rti_Access);
1893            New_Interface_Decl (Interfaces, Param, Get_Identifier ("addr"),
1894                                Ghdl_Ptr_Type);
1895            New_Interface_Decl (Interfaces, Param, Get_Identifier ("name"),
1896                                Ghdl_Str_Len_Ptr_Node);
1897            Finish_Subprogram_Decl (Interfaces, Res);
1898         end Create_Get_Name;
1899      begin
1900         -- procedure __ghdl_get_path_name (res : std_string_ptr_node;
1901         --                                 ctxt : ghdl_rti_access;
1902         --                                 addr : ghdl_ptr_type;
1903         --                                 name : __ghdl_str_len_ptr);
1904         Create_Get_Name ("__ghdl_get_path_name", Ghdl_Get_Path_Name);
1905
1906         -- procedure __ghdl_get_instance_name (res : std_string_ptr_node;
1907         --                                     ctxt : ghdl_rti_access;
1908         --                                     addr : ghdl_ptr_type;
1909         --                                     name : __ghdl_str_len_ptr);
1910         Create_Get_Name ("__ghdl_get_instance_name", Ghdl_Get_Instance_Name);
1911      end;
1912
1913      --  procedure __ghdl_rti_add_package (rti : ghdl_rti_access)
1914      Start_Procedure_Decl
1915        (Interfaces, Get_Identifier ("__ghdl_rti_add_package"),
1916         O_Storage_External);
1917      New_Interface_Decl (Interfaces, Param, Wki_Rti, Rtis.Ghdl_Rti_Access);
1918      Finish_Subprogram_Decl (Interfaces, Ghdl_Rti_Add_Package);
1919
1920      --  procedure __ghdl_rti_add_top (max_pkgs : ghdl_index_type;
1921      --                                pkgs : ghdl_rti_arr_acc);
1922      Start_Procedure_Decl
1923        (Interfaces, Get_Identifier ("__ghdl_rti_add_top"),
1924         O_Storage_External);
1925      New_Interface_Decl (Interfaces, Param, Get_Identifier ("max_pkgs"),
1926                          Ghdl_Index_Type);
1927      New_Interface_Decl (Interfaces, Param, Get_Identifier ("pkgs"),
1928                          Rtis.Ghdl_Rti_Arr_Acc);
1929      New_Interface_Decl (Interfaces, Param, Wki_Rti, Rtis.Ghdl_Rti_Access);
1930      New_Interface_Decl
1931        (Interfaces, Param, Wki_Instance, Ghdl_Ptr_Type);
1932      Finish_Subprogram_Decl (Interfaces, Ghdl_Rti_Add_Top);
1933
1934      --  procedure __ghdl_init_top_generics();
1935      Start_Procedure_Decl
1936        (Interfaces, Get_Identifier ("__ghdl_init_top_generics"),
1937         O_Storage_External);
1938      Finish_Subprogram_Decl (Interfaces, Ghdl_Init_Top_Generics);
1939
1940      --  Create match subprograms for std_ulogic type.
1941      Create_Std_Ulogic_Match_Subprogram ("eq", Ghdl_Std_Ulogic_Match_Eq);
1942      Create_Std_Ulogic_Match_Subprogram ("ne", Ghdl_Std_Ulogic_Match_Ne);
1943      Create_Std_Ulogic_Match_Subprogram ("lt", Ghdl_Std_Ulogic_Match_Lt);
1944      Create_Std_Ulogic_Match_Subprogram ("le", Ghdl_Std_Ulogic_Match_Le);
1945
1946      Create_Std_Ulogic_Array_Match_Subprogram
1947        ("eq", Ghdl_Std_Ulogic_Array_Match_Eq);
1948      Create_Std_Ulogic_Array_Match_Subprogram
1949        ("ne", Ghdl_Std_Ulogic_Array_Match_Ne);
1950
1951      --  Create To_String subprograms.
1952      Create_To_String_Subprogram
1953        ("__ghdl_to_string_i32", Ghdl_To_String_I32, Ghdl_I32_Type);
1954      Create_To_String_Subprogram
1955        ("__ghdl_to_string_i64", Ghdl_To_String_I64, Ghdl_I64_Type);
1956      Create_To_String_Subprogram
1957        ("__ghdl_to_string_f64", Ghdl_To_String_F64, Ghdl_Real_Type);
1958      Create_To_String_Subprogram
1959        ("__ghdl_to_string_f64_digits", Ghdl_To_String_F64_Digits,
1960         Ghdl_Real_Type, Ghdl_I32_Type, Get_Identifier ("nbr_digits"));
1961      Create_To_String_Subprogram
1962        ("__ghdl_to_string_f64_format", Ghdl_To_String_F64_Format,
1963         Ghdl_Real_Type, Std_String_Ptr_Node, Get_Identifier ("format"));
1964      declare
1965         Bv_Base_Ptr : constant O_Tnode :=
1966           Get_Info (Bit_Vector_Type_Definition).B.Base_Ptr_Type (Mode_Value);
1967      begin
1968         Create_To_String_Subprogram
1969           ("__ghdl_bv_to_ostring", Ghdl_BV_To_Ostring,
1970            Bv_Base_Ptr, Ghdl_Index_Type, Wki_Length);
1971         Create_To_String_Subprogram
1972           ("__ghdl_bv_to_hstring", Ghdl_BV_To_Hstring,
1973            Bv_Base_Ptr, Ghdl_Index_Type, Wki_Length);
1974      end;
1975      Create_To_String_Subprogram
1976        ("__ghdl_to_string_b1", Ghdl_To_String_B1, Ghdl_Bool_Type,
1977         Rtis.Ghdl_Rti_Access, Wki_Rti);
1978      Create_To_String_Subprogram
1979        ("__ghdl_to_string_e8", Ghdl_To_String_E8, Ghdl_I32_Type,
1980         Rtis.Ghdl_Rti_Access, Wki_Rti);
1981      Create_To_String_Subprogram
1982        ("__ghdl_to_string_char", Ghdl_To_String_Char,
1983         Get_Ortho_Type (Character_Type_Definition, Mode_Value));
1984      Create_To_String_Subprogram
1985        ("__ghdl_to_string_e32", Ghdl_To_String_E32, Ghdl_I32_Type,
1986         Rtis.Ghdl_Rti_Access, Wki_Rti);
1987      Create_To_String_Subprogram
1988        ("__ghdl_to_string_p32", Ghdl_To_String_P32, Ghdl_I32_Type,
1989         Rtis.Ghdl_Rti_Access, Wki_Rti);
1990      Create_To_String_Subprogram
1991        ("__ghdl_to_string_p64", Ghdl_To_String_P64, Ghdl_I64_Type,
1992         Rtis.Ghdl_Rti_Access, Wki_Rti);
1993      Create_To_String_Subprogram
1994        ("__ghdl_time_to_string_unit", Ghdl_Time_To_String_Unit,
1995         Std_Time_Otype, Std_Time_Otype, Get_Identifier ("unit"),
1996         Rtis.Ghdl_Rti_Access, Wki_Rti);
1997      Create_To_String_Subprogram
1998        ("__ghdl_array_char_to_string_b1", Ghdl_Array_Char_To_String_B1,
1999         Ghdl_Ptr_Type, Ghdl_Index_Type, Wki_Length,
2000         Rtis.Ghdl_Rti_Access, Wki_Rti);
2001      Create_To_String_Subprogram
2002        ("__ghdl_array_char_to_string_e8", Ghdl_Array_Char_To_String_E8,
2003         Ghdl_Ptr_Type, Ghdl_Index_Type, Wki_Length,
2004         Rtis.Ghdl_Rti_Access, Wki_Rti);
2005      Create_To_String_Subprogram
2006        ("__ghdl_array_char_to_string_e32", Ghdl_Array_Char_To_String_E32,
2007         Ghdl_Ptr_Type, Ghdl_Index_Type, Wki_Length,
2008         Rtis.Ghdl_Rti_Access, Wki_Rti);
2009
2010   end Post_Initialize;
2011
2012   procedure Translate_Type_Implicit_Subprograms
2013     (Decl : in out Iir; Main : Boolean)
2014   is
2015      Infos : Chap7.Implicit_Subprogram_Infos;
2016      Subprg_Kind : Subprg_Translate_Kind;
2017   begin
2018      pragma Assert (Get_Kind (Decl) in Iir_Kinds_Type_Declaration);
2019
2020      if Main then
2021         Subprg_Kind := Subprg_Translate_Spec_And_Body;
2022      else
2023         Subprg_Kind := Subprg_Translate_Only_Spec;
2024      end if;
2025      Chap3.Translate_Type_Subprograms (Decl, Subprg_Kind);
2026
2027      --  Skip type declaration.
2028      Decl := Get_Chain (Decl);
2029
2030      --  Implicit subprograms are immediately follow the type declaration.
2031      Chap7.Init_Implicit_Subprogram_Infos (Infos);
2032      while Decl /= Null_Iir loop
2033         if Get_Kind (Decl) in Iir_Kinds_Subprogram_Declaration
2034           and then Is_Implicit_Subprogram (Decl)
2035         then
2036            Chap7.Translate_Implicit_Subprogram_Spec (Decl, Infos);
2037            Chap7.Translate_Implicit_Subprogram_Body (Decl);
2038            Decl := Get_Chain (Decl);
2039         else
2040            exit;
2041         end if;
2042      end loop;
2043   end Translate_Type_Implicit_Subprograms;
2044
2045   procedure Translate_Standard (Main : Boolean)
2046   is
2047      Lib_Mark, Unit_Mark : Id_Mark_Type;
2048      Info : Ortho_Info_Acc;
2049      pragma Unreferenced (Info);
2050      Decl : Iir;
2051      Time_Type_Staticness : Iir_Staticness;
2052      Time_Subtype_Staticness : Iir_Staticness;
2053   begin
2054      Update_Node_Infos;
2055
2056      New_Debug_Comment_Decl ("package std.standard");
2057      if Main then
2058         Gen_Filename (Std_Standard_File);
2059         Set_Global_Storage (O_Storage_Public);
2060      else
2061         Set_Global_Storage (O_Storage_External);
2062      end if;
2063
2064      Info := Add_Info (Standard_Package, Kind_Package);
2065
2066      Reset_Identifier_Prefix;
2067      Push_Identifier_Prefix
2068        (Lib_Mark, Get_Identifier (Libraries.Std_Library));
2069      Push_Identifier_Prefix
2070        (Unit_Mark, Get_Identifier (Standard_Package));
2071
2072      --  With VHDL93 and later, time type is globally static.  As a result,
2073      --  it will be elaborated at run-time (and not statically).
2074      --  However, there is no elaboration of std.standard.  Furthermore,
2075      --  time type can be pre-elaborated without any difficulties.
2076      --  There is a kludge here:  set type staticess of time type locally
2077      --  and then revert it just after its translation.
2078      Time_Type_Staticness := Get_Type_Staticness (Time_Type_Definition);
2079      Time_Subtype_Staticness := Get_Type_Staticness (Time_Subtype_Definition);
2080      if Flags.Flag_Time_64 then
2081         Set_Type_Staticness (Time_Type_Definition, Locally);
2082      end if;
2083      Set_Type_Staticness (Time_Subtype_Definition, Locally);
2084      if Flags.Vhdl_Std > Vhdl_87 then
2085         Set_Type_Staticness (Delay_Length_Subtype_Definition, Locally);
2086      end if;
2087
2088      Decl := Get_Declaration_Chain (Standard_Package);
2089
2090      --  The first (and one of the most important) declaration is the
2091      --  boolean type declaration.
2092      pragma Assert (Decl = Boolean_Type_Declaration);
2093      Chap4.Translate_Bool_Type_Declaration (Boolean_Type_Declaration);
2094      --  We need this type very early, for predefined functions.
2095      Std_Boolean_Type_Node :=
2096        Get_Ortho_Type (Boolean_Type_Definition, Mode_Value);
2097      Std_Boolean_True_Node := Get_Ortho_Literal (Boolean_True);
2098      Std_Boolean_False_Node := Get_Ortho_Literal (Boolean_False);
2099
2100      Std_Boolean_Array_Type :=
2101        New_Array_Type (Std_Boolean_Type_Node, Ghdl_Index_Type);
2102      New_Type_Decl (Create_Identifier ("BOOLEAN_ARRAY"),
2103                     Std_Boolean_Array_Type);
2104      Translate_Type_Implicit_Subprograms (Decl, Main);
2105
2106      --  Second declaration: bit.
2107      pragma Assert (Decl = Bit_Type_Declaration);
2108      Chap4.Translate_Bool_Type_Declaration (Bit_Type_Declaration);
2109      Translate_Type_Implicit_Subprograms (Decl, Main);
2110
2111      --  Nothing special for other declarations.
2112      while Decl /= Null_Iir loop
2113         case Get_Kind (Decl) is
2114            when Iir_Kind_Type_Declaration =>
2115               Chap4.Translate_Type_Declaration (Decl);
2116               Translate_Type_Implicit_Subprograms (Decl, Main);
2117            when Iir_Kind_Anonymous_Type_Declaration =>
2118               Chap4.Translate_Anonymous_Type_Declaration (Decl);
2119               Translate_Type_Implicit_Subprograms (Decl, Main);
2120            when Iir_Kind_Subtype_Declaration =>
2121               Chap4.Translate_Subtype_Declaration (Decl);
2122               Decl := Get_Chain (Decl);
2123            when Iir_Kind_Attribute_Declaration =>
2124               Decl := Get_Chain (Decl);
2125            when Iir_Kind_Function_Declaration =>
2126               case Get_Implicit_Definition (Decl) is
2127                  when Iir_Predefined_Now_Function =>
2128                     null;
2129                  when Iir_Predefined_Enum_To_String
2130                    | Iir_Predefined_Integer_To_String
2131                    | Iir_Predefined_Floating_To_String
2132                    | Iir_Predefined_Real_To_String_Digits
2133                    | Iir_Predefined_Real_To_String_Format
2134                    | Iir_Predefined_Physical_To_String
2135                    | Iir_Predefined_Time_To_String_Unit =>
2136                     --  These are defined after the types.
2137                     null;
2138                  when others =>
2139                     Error_Kind
2140                       ("translate_standard ("
2141                          & Iir_Predefined_Functions'Image
2142                          (Get_Implicit_Definition (Decl)) & ")",
2143                        Decl);
2144               end case;
2145               Decl := Get_Chain (Decl);
2146            when others =>
2147               Error_Kind ("translate_standard", Decl);
2148         end case;
2149         --  DECL was updated by Translate_Type_Implicit_Subprograms or
2150         --  explicitly in other branches.
2151      end loop;
2152
2153      --  These types don't appear in std.standard.
2154      Chap4.Translate_Anonymous_Type_Declaration
2155        (Convertible_Integer_Type_Declaration);
2156      Chap4.Translate_Anonymous_Type_Declaration
2157        (Convertible_Real_Type_Declaration);
2158
2159      --  Restore time type staticness.
2160
2161      if Flags.Vhdl_Std > Vhdl_87 then
2162         Set_Type_Staticness (Delay_Length_Subtype_Definition,
2163                              Time_Subtype_Staticness);
2164      end if;
2165      Set_Type_Staticness (Time_Type_Definition, Time_Type_Staticness);
2166      Set_Type_Staticness (Time_Subtype_Definition, Time_Subtype_Staticness);
2167
2168      if Flag_Rti then
2169         Rtis.Generate_Unit (Standard_Package);
2170         Std_Standard_Boolean_Rti
2171           := Get_Info (Boolean_Type_Definition).Type_Rti;
2172         Std_Standard_Bit_Rti
2173           := Get_Info (Bit_Type_Definition).Type_Rti;
2174      end if;
2175
2176      --  Std_Ulogic indexed array of STD.Boolean.
2177      --  Used by PSL to convert Std_Ulogic to boolean.
2178      Std_Ulogic_Boolean_Array_Type := New_Array_Subtype
2179        (Std_Boolean_Array_Type, Std_Boolean_Type_Node, New_Index_Lit (9));
2180      New_Type_Decl (Get_Identifier ("__ghdl_std_ulogic_boolean_array_type"),
2181                     Std_Ulogic_Boolean_Array_Type);
2182      New_Const_Decl (Ghdl_Std_Ulogic_To_Boolean_Array,
2183                      Get_Identifier ("__ghdl_std_ulogic_to_boolean_array"),
2184                      O_Storage_External, Std_Ulogic_Boolean_Array_Type);
2185
2186      Pop_Identifier_Prefix (Unit_Mark);
2187      Pop_Identifier_Prefix (Lib_Mark);
2188
2189      Post_Initialize;
2190      Current_Filename_Node := O_Dnode_Null;
2191      --Pop_Global_Factory;
2192   end Translate_Standard;
2193
2194   procedure Finalize is
2195   begin
2196      Free_Node_Infos;
2197      Free_Old_Temp;
2198   end Finalize;
2199
2200   procedure Elaborate (Config : Iir; Whole : Boolean)
2201     renames Trans.Chap12.Elaborate;
2202
2203end Translation;
2204