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>.
16
17package Trans.Chap3 is
18   --  Translate the subtype of an object, since an object can define
19   --  a subtype.
20   --  This can be done only for a declaration.
21   --  DECL must have an identifier and a type.
22   procedure Translate_Object_Subtype_Indication
23     (Decl : Iir; With_Vars : Boolean := True);
24   procedure Elab_Object_Subtype_Indication (Decl : Iir);
25
26   --  Translate the subtype of a literal.
27   --  This can be done not at declaration time, ie no variables are created
28   --  for this subtype.
29   --procedure Translate_Literal_Subtype (Def : Iir);
30
31   --  Translation of a type definition or subtype indication.
32   --  1. Create corresponding Ortho type.
33   --  2. Create bounds type
34   --  3. Create bounds declaration
35   --  4. Create bounds constructor
36   --  5. Create type descriptor declaration
37   --  6. Create type descriptor constructor
38   procedure Translate_Type_Definition (Def : Iir);
39
40   --  Translate subprograms for types.
41   procedure Translate_Type_Subprograms
42     (Decl : Iir; Kind : Subprg_Translate_Kind);
43
44   function Create_Static_Composite_Subtype_Layout (Def : Iir) return O_Cnode;
45
46   --  Same as Translate_type_definition only for std.standard.boolean and
47   --  std.standard.bit.
48   procedure Translate_Bool_Type_Definition (Def : Iir);
49
50   --  Call lock or unlock on a protected object.
51   procedure Call_Ghdl_Protected_Procedure (Type_Def : Iir; Proc : O_Dnode);
52
53   procedure Translate_Protected_Type_Body (Bod : Iir);
54   procedure Translate_Protected_Type_Body_Subprograms_Spec (Bod : Iir);
55   procedure Translate_Protected_Type_Body_Subprograms_Body (Bod : Iir);
56
57   procedure Translate_Subtype_Definition
58     (Def : Iir; With_Vars : Boolean := True);
59
60   --  Translate a proper subtype indication.
61   procedure Translate_Subtype_Indication (Def : Iir; With_Vars : Boolean);
62
63   procedure Translate_Named_Subtype_Definition (Def : Iir; Id : Name_Id);
64
65   --  When there is no name for the subtype (eg: the subtype of a string or
66   --  of an aggregate).  There is also no type mark.
67   procedure Translate_Anonymous_Subtype_Definition
68     (Def : Iir; With_Vars : Boolean);
69
70   --  It generates code to do type elaboration.
71   procedure Elab_Type_Declaration (Decl : Iir);
72   procedure Elab_Subtype_Declaration (Decl : Iir_Subtype_Declaration);
73
74   --  Builders.
75   --  A complex type is a type whose size is not locally static.
76   --
77   --  The most simple example is an unidimensionnl array whose range
78   --  depends on generics.
79   --
80   --  We call first order complex type any array whose bounds are not
81   --  locally static and whose sub-element size is locally static.
82   --
83   --  First order complex type objects are represented by a pointer to an
84   --  array of sub-element, and the storage area for the array is
85   --  allocated at run-time.
86   --
87   --  Since a sub-element type may be a complex type, a type may be
88   --  complex because one of its sub-element type is complex.
89   --  EG, a record type whose one element is a complex array.
90   --
91   --  A type may be complex either because it is a first order complex
92   --  type (ie an array whose bounds are not locally static) or because
93   --  one of its sub-element type is such a type (this is recursive).
94   --
95   --  We call second order complex type a complex type that is not of first
96   --  order.
97   --  We call third order complex type a second order complex type which is
98   --  an array whose bounds are not locally static.
99   --
100   --  In a complex type, sub-element of first order complex type are
101   --  represented by a pointer.
102   --  Any complex type object (constant, signal, variable, port, generic)
103   --  is represented by a pointer.
104   --
105   --  Creation of a second or third order complex type object consists in
106   --  allocating the memory and building the object.
107   --  Building a object consists in setting internal pointers.
108   --
109   --  A complex type has always a non-null INFO.C, and its size is computed
110   --  during elaboration.
111   --
112   --  For a second or third order complex type, INFO.C.BUILDER_NEED_FUNC
113   --  is set to TRUE.
114
115   --  Functions for fat array.
116   --  Fat array are array whose size is not known at compilation time.
117   --  This corresponds to an unconstrained array or a non locally static
118   --  constrained array.
119   --  A fat array is a structure containing 2 fields:
120   --  * base: a pointer to the data of the array.
121   --  * bounds: a pointer to a structure containing as many fields as
122   --    number of dimensions; these fields are a structure describing the
123   --    range of the dimension.
124
125   procedure Gen_Call_Type_Builder
126     (Layout : Mnode; Var_Type : Iir; Kind : Object_Kind_Type);
127
128   --  If the element subtype of ARR_TYPE is unbounded, create a fat pointer,
129   --  set the bounds of it (from ARR), and return it.
130   --  Otherwise, return a null mnode.
131   --  Used to build a var for a subelement of ARR.
132   --  This is used by foreach_non_composite to factorize fat-pointer
133   --  building.
134   function Create_Maybe_Fat_Array_Element (Arr : Mnode; Arr_Type : Iir)
135                                           return Mnode;
136
137   --  If the element subtype of the array is unbounded, set the base of VAR
138   --  from EL, and return it.
139   --  Otherwise directly return EL (VAR must be null).
140   function Assign_Maybe_Fat_Array_Element (Var : Mnode; El : Mnode)
141                                           return Mnode;
142
143   --  Index array BASE of type ATYPE with INDEX.
144   --  INDEX must be of type ghdl_index_type, thus no bounds checks are
145   --  performed.
146   function Index_Base (Base : Mnode; Atype : Iir; Index : O_Enode)
147                       return Mnode;
148
149   --  Index array ARR of type ATYPE with INDEX.
150   --  Return the base.
151   function Index_Array (Arr : Mnode; Atype : Iir; Index : O_Enode)
152                        return Mnode;
153
154   --  Same for for slicing.
155   function Slice_Base
156     (Base : Mnode; Atype : Iir; Index : O_Enode; Stride : O_Enode)
157     return Mnode;
158
159   --  Get the length of the array (the number of elements).
160   function Get_Array_Length (Arr : Mnode; Atype : Iir) return O_Enode;
161
162   --  Get the number of elements for bounds BOUNDS.  BOUNDS are
163   --  automatically stabilized if necessary.
164   function Get_Bounds_Length (Bounds : Mnode; Atype : Iir) return O_Enode;
165
166   --  Return the number of elements for statically bounded array ATYPE.
167   function Get_Static_Array_Length (Atype : Iir) return Int64;
168
169   --  Get the number of elements in array ATYPE.
170   function Get_Array_Type_Length (Atype : Iir) return O_Enode;
171
172   --  Return the allocation kind used for layout variable of type INFO.
173   function Get_Composite_Type_Layout_Alloc (Info : Type_Info_Acc)
174                                             return Allocation_Kind;
175
176   --  Get the base of array or record OBJ.  If OBJ is already constrained,
177   --  return it.
178   function Get_Composite_Base (Obj : Mnode) return Mnode;
179
180   --  The base returned by Get_Composite_Base is always the least
181   --  constrained array base.  But the subtype may be more constrained than
182   --  the base.  In that case the base must be converted to the subtype.
183   function Convert_Array_Base (Arr : Mnode) return Mnode;
184
185   --  Get the base of array or record OBJ; but if OBJ is already constrained,
186   --  convert it to the base of an unbounded object (so this unboxes the
187   --  records).
188   function Get_Composite_Unbounded_Base (Obj : Mnode) return Mnode;
189
190   --  Get the bounds of composite ARR (an array or an unbounded record).
191   function Get_Composite_Bounds (Obj : Mnode) return Mnode;
192
193   --  Get the range ot ATYPE.
194   function Type_To_Range (Atype : Iir) return Mnode;
195
196   --  Get length of range R.
197   function Range_To_Length (R : Mnode) return Mnode;
198
199   --  Get direction of range R.
200   function Range_To_Dir (R : Mnode) return Mnode;
201
202   --  Get left/right bounds for range R.
203   function Range_To_Left (R : Mnode) return Mnode;
204   function Range_To_Right (R : Mnode) return Mnode;
205
206   --  Get range for dimension DIM (1 based) of array bounds B of type
207   --  ATYPE.
208   function Bounds_To_Range (B : Mnode; Atype : Iir; Dim : Positive)
209                                return Mnode;
210
211   --  Get the range of dimension DIM (1 based) of array ARR of type ATYPE.
212   function Get_Array_Range (Arr : Mnode; Atype : Iir; Dim : Positive)
213                                return Mnode;
214
215   --  Get array/record bounds for type ATYPE.
216   function Get_Composite_Type_Bounds (Atype : Iir) return Mnode;
217
218   --  Return a pointer to the base from bounds_acc ACC.
219   function Get_Bounds_Acc_Base
220     (Acc : O_Enode; D_Type : Iir) return O_Enode;
221
222   --  Return bounds from layout B.
223   function Layout_To_Bounds (B : Mnode) return Mnode;
224
225   function Layout_To_Size (Layout : Mnode; Kind : Object_Kind_Type)
226                           return O_Lnode;
227
228   --  From a record layout B, return the layout of element EL.  EL must be
229   --  an unbounded element.
230   function Record_Layout_To_Element_Layout (B : Mnode; El : Iir) return Mnode;
231
232   --  From an unbounded record bounds B, get the bounds for (unbounded)
233   --  element EL.
234   function Record_Bounds_To_Element_Bounds (B : Mnode; El : Iir) return Mnode;
235
236   --  Return the offset for field EL in record B.
237   function Record_Layout_To_Element_Offset
238     (B : Mnode; El : Iir; Kind : Object_Kind_Type) return O_Lnode;
239
240   --  From an unbounded array bounds B, get the bounds for the (unbounded)
241   --  element.
242   function Array_Bounds_To_Element_Bounds (B : Mnode; Arr_Type : Iir)
243                                           return Mnode;
244
245   --  From unbounded array bounds B, get the layout of the unbounded element.
246   function Array_Bounds_To_Element_Layout (B : Mnode; Arr_Type : Iir)
247                                           return Mnode;
248
249   --  Deallocate OBJ.
250   procedure Gen_Deallocate (Obj : O_Enode);
251
252   --  Performs deallocation of PARAM (the parameter of a deallocate call).
253   procedure Translate_Object_Deallocation (Param : Iir);
254
255   --  Copy bounds from SRC to DEST.
256   procedure Copy_Bounds (Dest : O_Enode; Src : O_Enode; Obj_Type : Iir);
257   procedure Copy_Bounds (Dest : Mnode; Src : Mnode; Obj_Type : Iir);
258
259   --  Allocate an object of type OBJ_TYPE and set RES.
260   --  RES must be a stable access of type ortho_ptr_type.
261   --  For an unconstrained array, BOUNDS is a pointer to the boundaries of
262   --  the object, which are copied.
263   procedure Translate_Object_Allocation
264     (Res        : in out Mnode;
265      Alloc_Kind : Allocation_Kind;
266      Obj_Type   : Iir;
267      Bounds     : Mnode);
268
269   --  Low level copy of SRC to DEST.  Both have the same type, OBJ_TYPE.
270   --  There is no length check, so arrays must be of the same length.
271   procedure Translate_Object_Copy
272     (Dest : Mnode; Src : Mnode; Obj_Type : Iir);
273
274   --  Get size (in bytes with type ghdl_index_type) of subtype ATYPE.
275   --  For an unconstrained array, BOUNDS must be set, otherwise it may be a
276   --  null_mnode.
277   function Get_Subtype_Size
278     (Atype : Iir; Bounds : Mnode; Kind : Object_Kind_Type) return O_Enode;
279
280   --  Get size (in bytes with type ghdl_index_type) of object OBJ.
281   --  For an unconstrained array, OBJ must be really an object, otherwise,
282   --  it may be the result of T2M.
283   function Get_Object_Size (Obj : Mnode; Obj_Type : Iir) return O_Enode;
284
285   --  Allocate the base of an unbounded composite, whose length is
286   --  determined from the bounds (already set).
287   --  RES_PTR is a pointer to the fat pointer (must be a stable variable: it
288   --  can be referenced several times).
289   --  ARR_TYPE is the type of the composite.
290   procedure Allocate_Unbounded_Composite_Base (Alloc_Kind : Allocation_Kind;
291                                                Res        : Mnode;
292                                                Arr_Type   : Iir);
293
294   --  Allocate the bounds for type OBJ_TYPE and assign it to unbounded object
295   --  RES.  Then copy bounds from BOUNDS to the object.
296   procedure Allocate_Unbounded_Composite_Bounds
297     (Alloc_Kind : Allocation_Kind;
298      Res        : Mnode;
299      Obj_Type   : Iir);
300
301   --  Used for alias: create the vars for the subtype of the name (when the
302   --  name is a slice).  The identifier prefix must have been set.
303   --
304   --  Slices are special because they create new bounds variables.  These
305   --  variables are expected to be transcient.  But in some cases (like
306   --  aliases), they have a longer life.
307   procedure Translate_Array_Subtype (Arr_Type : Iir);
308   procedure Elab_Array_Subtype (Arr_Type : Iir);
309
310   --  Create the bounds for SUB_TYPE.
311   --  SUB_TYPE is expected to be a non-static, anonymous array or record
312   --  subtype.
313   procedure Create_Composite_Subtype (Sub_Type : Iir; Elab : Boolean := True);
314
315   --  Return TRUE if VALUE is not is the range specified by ATYPE.
316   --  VALUE must be stable.
317   function Not_In_Range (Value : O_Dnode; Atype : Iir) return O_Enode;
318
319   --  Return TRUE if base type of ATYPE is larger than its bounds, ie
320   --  if a value of type ATYPE may be out of range.
321   function Need_Range_Check (Expr : Iir; Atype : Iir) return Boolean;
322
323   --  Generate an error if VALUE (computed from EXPR which may be NULL_IIR
324   --  if not from a tree) is not in range specified by ATYPE.
325   procedure Check_Range
326     (Value : O_Dnode; Expr : Iir; Atype : Iir; Loc : Iir);
327
328   --  Insert a scalar check for VALUE of type ATYPE.  EXPR may be NULL_IIR.
329   function Insert_Scalar_Check
330     (Value : O_Enode; Expr : Iir; Atype : Iir; Loc : Iir) return O_Enode;
331
332   --  The base type of EXPR and the base type of ATYPE must be the same.
333   --  If the type is a scalar type, and if a range check is needed, this
334   --  function inserts the check.  Otherwise, it returns VALUE.
335   function Maybe_Insert_Scalar_Check
336     (Value : O_Enode; Expr : Iir; Atype : Iir) return O_Enode;
337
338   --  Return True iff all indexes of L_TYPE and R_TYPE have the same
339   --  length.  They must be constrained.
340   function Locally_Array_Match (L_Type, R_Type : Iir) return Tri_State_Type;
341
342   --  Check bounds of L match bounds of R.
343   --  If L_TYPE (resp. R_TYPE) is not a thin composite type, then L_NODE
344   --    (resp. R_NODE) are not used (and may be Mnode_Null).
345   --  If L_TYPE (resp. T_TYPE) is a fat type, then L_NODE (resp. R_NODE)
346   --    must designate the object.
347   procedure Check_Composite_Match
348     (L_Type : Iir; L_Node : Mnode; R_Type : Iir; R_Node : Mnode; Loc : Iir);
349
350   --  Create a subtype range to be stored into RES from length LENGTH, which
351   --  is of type INDEX_TYPE.
352   --  This is done according to rules 7.2.4 of LRM93, ie:
353   --  direction and left bound of the range is the same of INDEX_TYPE.
354   --  LENGTH is a variable. LOC is the location in case of error.
355   procedure Create_Range_From_Length
356     (Index_Type : Iir; Length : O_Dnode; Res : Mnode; Loc : Iir);
357
358end Trans.Chap3;
359