1--  Ortho debug back-end declarations.
2--  Copyright (C) 2005-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>.
16
17with Interfaces; use Interfaces;
18with Ortho_Ident;
19use Ortho_Ident;
20
21--  Interface to create nodes.
22package Ortho_Debug is
23   procedure Init;
24   procedure Finish;
25
26private
27   --  This back-end supports nested subprograms.
28   Has_Nested_Subprograms : constant Boolean := True;
29
30   --  Return the type of elements of array type/subtype ATYPE.
31   function Get_Array_El_Type (Atype : O_Tnode) return O_Tnode;
32
33   --  Return the base type of T.
34   --  function Get_Base_Type (T : O_Tnode) return O_Tnode;
35
36   --  A node for a type.
37   type O_Tnode_Type (<>);
38   type O_Tnode is access O_Tnode_Type;
39
40   --  A node for a statement.
41   type O_Snode_Type (<>);
42   type O_Snode is access O_Snode_Type;
43
44   Top : O_Snode;
45
46   type Str_Acc is access String;
47
48   type Decl_Scope_Type;
49   type Decl_Scope_Acc is access Decl_Scope_Type;
50
51   type On_Decl_Kind is
52     (ON_Type_Decl, ON_Completed_Type_Decl,
53      ON_Const_Decl, ON_Var_Decl, ON_Interface_Decl,
54      ON_Function_Decl, ON_Function_Body,
55      ON_Init_Value,
56      ON_Debug_Line_Decl, ON_Debug_Comment_Decl, ON_Debug_Filename_Decl);
57
58   type O_Dnode_Type (<>);
59   type O_Dnode is access O_Dnode_Type;
60
61   O_Dnode_Null : constant O_Dnode := null;
62
63   type O_Dnode_Type (Kind : On_Decl_Kind) is record
64      Next : O_Dnode;
65      Name : O_Ident;
66      Dtype : O_Tnode;
67      Storage : O_Storage;
68      --  Declare statement in which the declaration appears.
69      Scope : O_Snode;
70      --  Line number, for regen.
71      Lineno : Natural;
72      case Kind is
73         when ON_Type_Decl =>
74            null;
75         when ON_Completed_Type_Decl =>
76            null;
77         when ON_Const_Decl
78           | ON_Var_Decl =>
79            --  Corresponding declaration for initial value (if any).
80            Value_Decl : O_Dnode;
81         when ON_Init_Value =>
82            --  Corresponding declaration of the object.
83            Init_Decl : O_Dnode;
84            Value : O_Cnode;
85         when ON_Function_Decl =>
86            Interfaces : O_Dnode;
87            Func_Body : O_Dnode;
88            Alive : Boolean;
89         when ON_Function_Body =>
90            Func_Decl : O_Dnode;
91            Func_Stmt : O_Snode;
92         when ON_Interface_Decl =>
93            Func_Scope : O_Dnode;
94         when ON_Debug_Line_Decl =>
95            Line : Natural;
96         when ON_Debug_Comment_Decl =>
97            Comment : Str_Acc;
98         when ON_Debug_Filename_Decl =>
99            Filename : Str_Acc;
100      end case;
101   end record;
102
103   --  A node for a record element.
104   type O_Fnode_Type;
105   type O_Fnode is access O_Fnode_Type;
106
107   O_Fnode_Null : constant O_Fnode := null;
108
109   type O_Fnode_Type is record
110      --  Record type.
111      Parent : O_Tnode;
112      --  Next field in the record.
113      Next : O_Fnode;
114      --  Name of the record field.
115      Ident : O_Ident;
116      --  Type of the record field.
117      Ftype : O_Tnode;
118   end record;
119
120   type O_Anode_Type;
121   type O_Anode is access O_Anode_Type;
122   type O_Anode_Type is record
123      Next : O_Anode;
124      Formal : O_Dnode;
125      Actual : O_Enode;
126   end record;
127
128   type OC_Kind is
129     (
130      OC_Boolean_Lit,
131      OC_Unsigned_Lit,
132      OC_Signed_Lit,
133      OC_Float_Lit,
134      OC_Enum_Lit,
135      OC_Null_Lit,
136      OC_Sizeof_Lit,
137      OC_Record_Sizeof_Lit,
138      OC_Alignof_Lit,
139      OC_Offsetof_Lit,
140      OC_Default_Lit,
141      OC_Array_Aggregate,
142      OC_Record_Aggregate,
143      OC_Aggr_Element,
144      OC_Union_Aggr,
145      OC_Address,
146      OC_Unchecked_Address,
147      OC_Subprogram_Address
148     );
149   type O_Cnode_Type (Kind : OC_Kind) is record
150      --  Type of the constant.
151      Ctype : O_Tnode;
152      --  True if referenced.
153      Ref : Boolean;
154      case Kind is
155         when OC_Unsigned_Lit =>
156            U_Val : Unsigned_64;
157         when OC_Signed_Lit =>
158            S_Val : Integer_64;
159         when OC_Float_Lit =>
160            F_Val : IEEE_Float_64;
161         when OC_Boolean_Lit =>
162            B_Val : Boolean;
163            B_Id : O_Ident;
164         when OC_Enum_Lit =>
165            E_Val : Integer;
166            E_Next : O_Cnode;
167            E_Name : O_Ident;
168         when OC_Null_Lit =>
169            null;
170         when OC_Default_Lit =>
171            null;
172         when OC_Sizeof_Lit
173            | OC_Record_Sizeof_Lit
174            | OC_Alignof_Lit =>
175            S_Type : O_Tnode;
176         when OC_Offsetof_Lit =>
177            Off_Field : O_Fnode;
178         when OC_Array_Aggregate =>
179            Arr_Len : Unsigned_32;
180            Arr_Els : O_Cnode;
181         when OC_Record_Aggregate =>
182            Rec_Els : O_Cnode;
183         when OC_Union_Aggr =>
184            Uaggr_Field : O_Fnode;
185            Uaggr_Value : O_Cnode;
186         when OC_Aggr_Element =>
187            Aggr_Value : O_Cnode;
188            Aggr_Next : O_Cnode;
189         when OC_Address
190           | OC_Unchecked_Address =>
191            Addr_Global : O_Gnode;
192         when OC_Subprogram_Address =>
193            Addr_Decl : O_Dnode;
194      end case;
195   end record;
196
197   type O_Cnode is access O_Cnode_Type;
198   O_Cnode_Null : constant O_Cnode := null;
199
200   type OE_Kind is
201     (
202      --  Literals.
203      OE_Lit,
204
205      --  Dyadic operations.
206      OE_Add_Ov,                --  OE_Dyadic_Op_Kind
207      OE_Sub_Ov,                --  OE_Dyadic_Op_Kind
208      OE_Mul_Ov,                --  OE_Dyadic_Op_Kind
209      OE_Div_Ov,                --  OE_Dyadic_Op_Kind
210      OE_Rem_Ov,                --  OE_Dyadic_Op_Kind
211      OE_Mod_Ov,                --  OE_Dyadic_Op_Kind
212      OE_Exp_Ov,                --  OE_Dyadic_Op_Kind
213
214      --  Binary operations.
215      OE_And,                   --  OE_Dyadic_Op_Kind
216      OE_Or,                    --  OE_Dyadic_Op_Kind
217      OE_Xor,                   --  OE_Dyadic_Op_Kind
218
219      --  Monadic operations.
220      OE_Not,                   --  OE_Monadic_Op_Kind
221      OE_Neg_Ov,                --  OE_Monadic_Op_Kind
222      OE_Abs_Ov,                --  OE_Monadic_Op_Kind
223
224      --  Comparaisons
225      OE_Eq,                    --  OE_Compare_Op_Kind
226      OE_Neq,                   --  OE_Compare_Op_Kind
227      OE_Le,                    --  OE_Compare_Op_Kind
228      OE_Lt,                    --  OE_Compare_Op_Kind
229      OE_Ge,                    --  OE_Compare_Op_Kind
230      OE_Gt,                    --  OE_Compare_Op_Kind
231
232      --  Misc.
233      OE_Convert_Ov,
234      OE_Convert,
235      OE_Address,
236      OE_Unchecked_Address,
237      OE_Alloca,
238      OE_Function_Call,
239
240      OE_Value,
241      OE_Nil
242      );
243
244   subtype OE_Dyadic_Expr_Kind is OE_Kind range OE_Add_Ov .. OE_Xor;
245   subtype OE_Monadic_Expr_Kind is OE_Kind range OE_Not .. OE_Abs_Ov;
246   subtype OE_Compare_Expr_Kind is OE_Kind range OE_Eq .. OE_Gt;
247
248   type O_Enode_Type (Kind : OE_Kind);
249   type O_Enode is access O_Enode_Type;
250   O_Enode_Null : constant O_Enode := null;
251
252   type O_Enode_Type (Kind : OE_Kind) is record
253      --  Type of the result.
254      Rtype : O_Tnode;
255      --  True if referenced.
256      Ref : Boolean;
257      case Kind is
258         when OE_Dyadic_Expr_Kind
259           | OE_Compare_Expr_Kind =>
260            Left : O_Enode;
261            Right : O_Enode;
262         when OE_Monadic_Expr_Kind =>
263            Operand : O_Enode;
264         when OE_Lit =>
265            Lit : O_Cnode;
266         when OE_Address
267           | OE_Unchecked_Address =>
268            Lvalue : O_Lnode;
269         when OE_Convert_Ov
270            | OE_Convert =>
271            Conv : O_Enode;
272         when OE_Function_Call =>
273            Func : O_Dnode;
274            Assoc : O_Anode;
275         when OE_Value =>
276            Value : O_Lnode;
277         when OE_Alloca =>
278            A_Size : O_Enode;
279         when OE_Nil =>
280            null;
281      end case;
282   end record;
283   type O_Enode_Array is array (Natural range <>) of O_Enode;
284   type O_Enode_Array_Acc is access O_Enode_Array;
285
286   type OL_Kind is
287     (
288      --  Name.
289      OL_Obj,
290      OL_Indexed_Element,
291      OL_Slice,
292      OL_Selected_Element,
293      OL_Access_Element
294      );
295
296   type O_Lnode_Type (Kind : OL_Kind);
297   type O_Lnode is access O_Lnode_Type;
298   O_Lnode_Null : constant O_Lnode := null;
299
300   type O_Lnode_Type (Kind : OL_Kind) is record
301      --  Type of the result.
302      Rtype : O_Tnode;
303      --  True if referenced.
304      Ref : Boolean;
305      case Kind is
306         when OL_Obj =>
307            Obj : O_Dnode;
308         when OL_Indexed_Element =>
309            Array_Base : O_Lnode;
310            Index : O_Enode;
311         when OL_Slice =>
312            Slice_Base : O_Lnode;
313            Slice_Index : O_Enode;
314         when OL_Selected_Element =>
315            Rec_Base : O_Lnode;
316            Rec_El : O_Fnode;
317         when OL_Access_Element =>
318            Acc_Base : O_Enode;
319      end case;
320   end record;
321
322   type OG_Kind is
323     (
324      OG_Decl,
325      OG_Selected_Element
326     );
327
328   type O_Gnode_Type (Kind : OG_Kind);
329   type O_Gnode is access O_Gnode_Type;
330   O_Gnode_Null : constant O_Gnode := null;
331
332   type O_Gnode_Type (Kind : OG_Kind) is record
333      --  Type of the result.
334      Rtype : O_Tnode;
335      --  True if referenced.
336      Ref : Boolean;
337      case Kind is
338         when OG_Decl =>
339            Decl : O_Dnode;
340         when OG_Selected_Element =>
341            Rec_Base : O_Gnode;
342            Rec_El : O_Fnode;
343      end case;
344   end record;
345
346   O_Tnode_Null : constant O_Tnode := null;
347   type ON_Type_Kind is
348     (ON_Boolean_Type, ON_Enum_Type,
349      ON_Unsigned_Type, ON_Signed_Type, ON_Float_Type,
350      ON_Array_Type, ON_Array_Subtype,
351      ON_Record_Type, ON_Record_Subtype,
352      ON_Union_Type, ON_Access_Type);
353
354   subtype ON_Array_Kinds is ON_Type_Kind
355     range ON_Array_Type .. ON_Array_Subtype;
356
357   type O_Tnode_Type (Kind : ON_Type_Kind) is record
358      Decl : O_Dnode;
359      --  True if the type was first created as an uncomplete type.
360      Uncomplete : Boolean;
361      --  True if the type is complete.
362      Complete : Boolean;
363      --  True if the type is fully constrained.
364      Constrained : Boolean;
365      case Kind is
366         when ON_Boolean_Type =>
367            True_N : O_Cnode;
368            False_N : O_Cnode;
369         when ON_Unsigned_Type
370           | ON_Signed_Type =>
371            Int_Size : Natural;
372         when ON_Float_Type =>
373            null;
374         when ON_Enum_Type =>
375            Nbr : Natural;
376            Literals: O_Cnode;
377         when ON_Access_Type =>
378            D_Type : O_Tnode;
379         when ON_Array_Type =>
380            El_Type : O_Tnode;
381            Index_Type : O_Tnode;
382         when ON_Array_Subtype =>
383            Length : O_Cnode;
384            Arr_El_Type : O_Tnode;
385            Arr_Base : O_Tnode;
386         when ON_Record_Type
387           | ON_Union_Type =>
388            Rec_Elements : O_Fnode;
389         when ON_Record_Subtype =>
390            Subrec_Elements : O_Fnode;
391            Subrec_Base : O_Tnode;
392      end case;
393   end record;
394
395   type ON_Choice_Kind is (ON_Choice_Expr, ON_Choice_Range, ON_Choice_Default);
396   type O_Choice_Type (Kind : ON_Choice_Kind);
397   type O_Choice is access O_Choice_Type;
398   type O_Choice_Type (Kind : ON_Choice_Kind) is record
399      Next : O_Choice;
400      case Kind is
401         when ON_Choice_Expr =>
402            Expr : O_Cnode;
403         when ON_Choice_Range =>
404            Low, High : O_Cnode;
405         when ON_Choice_Default =>
406            null;
407      end case;
408   end record;
409
410   O_Snode_Null : constant O_Snode := null;
411   type ON_Stmt_Kind is
412     (ON_Declare_Stmt, ON_Assign_Stmt, ON_Return_Stmt, ON_If_Stmt,
413      ON_Elsif_Stmt, ON_Loop_Stmt, ON_Exit_Stmt, ON_Next_Stmt,
414      ON_Case_Stmt, ON_When_Stmt, ON_Call_Stmt,
415      ON_Debug_Line_Stmt, ON_Debug_Comment_Stmt);
416   type O_Snode_Type (Kind : ON_Stmt_Kind) is record
417      Next : O_Snode;
418      Lineno : Natural;
419      case Kind is
420         when ON_Declare_Stmt =>
421            Decls : O_Dnode;
422            Stmts : O_Snode;
423            --  True if the statement is currently open.
424            Alive : Boolean;
425         when ON_Assign_Stmt =>
426            Target : O_Lnode;
427            Value : O_Enode;
428         when ON_Return_Stmt =>
429            Ret_Val : O_Enode;
430         when ON_If_Stmt =>
431            Elsifs : O_Snode;
432            If_Last : O_Snode;
433         when ON_Elsif_Stmt =>
434            Cond : O_Enode;
435            Next_Elsif : O_Snode;
436         when ON_Loop_Stmt =>
437            Loop_Last : O_Snode;
438            Loop_Level : Natural;
439         when ON_Exit_Stmt
440           | ON_Next_Stmt =>
441            Loop_Id : O_Snode;
442         when ON_Case_Stmt =>
443            Selector : O_Enode;
444            --  Simply linked list of branches
445            Branches : O_Snode;
446            Case_Last : O_Snode;
447         when ON_When_Stmt =>
448            --  The corresponding 'case'
449            Branch_Parent : O_Snode;
450            Choice_List : O_Choice;
451            Next_Branch : O_Snode;
452         when ON_Call_Stmt =>
453            Proc : O_Dnode;
454            Assoc : O_Anode;
455         when ON_Debug_Line_Stmt =>
456            Line : Natural;
457         when ON_Debug_Comment_Stmt =>
458            Comment : Str_Acc;
459      end case;
460   end record;
461
462   type O_Inter_List is record
463      Func : O_Dnode;
464      Last : O_Dnode;
465   end record;
466
467   type O_Element_List is record
468      --  The type definition.
469      Res : O_Tnode;
470      --  The last element added.
471      Last : O_Fnode;
472   end record;
473
474   type O_Element_Sublist is record
475      --  The type definition.
476      Res : O_Tnode;
477      --  The last element added.
478      Last : O_Fnode;
479      --  The correspond field from the base type.
480      Base_Field : O_Fnode;
481   end record;
482
483   type O_Record_Aggr_List is record
484      Res : O_Cnode;
485      Last : O_Cnode;
486      Field : O_Fnode;
487   end record;
488
489   type O_Array_Aggr_List is record
490      Res : O_Cnode;
491      Last : O_Cnode;
492      El_Type : O_Tnode;
493   end record;
494
495   type O_Assoc_List is record
496      Subprg : O_Dnode;
497      Interfaces : O_Dnode;
498      First, Last : O_Anode;
499   end record;
500
501   type O_Enum_List is record
502      --  The type built.
503      Res : O_Tnode;
504
505      --  the chain of declarations.
506      Last : O_Cnode;
507   end record;
508   type O_Case_Block is record
509      Case_Stmt : O_Snode;
510   end record;
511
512   type O_If_Block is record
513      null;
514   end record;
515end Ortho_Debug;
516