OSDN Git Service

Nathanael Nerode <neroden@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / repinfo.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              R E P I N F O                               --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1999-2001 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT was originally developed  by the GNAT team at  New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This package contains the routines to handle back annotation of the
36 --  tree to fill in representation information, and also the routine used
37 --  by -gnatR to print this information. This unit is used both in the
38 --  compiler and in ASIS (it is used in ASIS as part of the implementation
39 --  of the data decomposition annex.
40
41 with Types; use Types;
42 with Uintp; use Uintp;
43
44 package Repinfo is
45
46    --------------------------------
47    -- Representation Information --
48    --------------------------------
49
50    --  The representation information of interest here is size and
51    --  component information for arrays and records. For primitive
52    --  types, the front end computes the Esize and RM_Size fields of
53    --  the corresponding entities as constant non-negative integers,
54    --  and the Uint values are stored directly in these fields.
55
56    --  For composite types, there are three cases:
57
58    --    1. In some cases the front end knows the values statically,
59    --       for example in the ase where representation clauses or
60    --       pragmas specify the values.
61
62    --    2. If Backend_Layout is True, then the backend is responsible
63    --       for layout of all types and objects not laid out by the
64    --       front end. This includes all dynamic values, and also
65    --       static values (e.g. record sizes) when not set by the
66    --       front end.
67
68    --    3. If Backend_Layout is False, then the front end lays out
69    --       all data, according to target dependent size and alignment
70    --       information, creating dynamic inlinable functions where
71    --       needed in the case of sizes not known till runtime.
72
73    -----------------------------
74    -- Back-Annotation by Gigi --
75    -----------------------------
76
77    --  The following interface is used by gigi if Backend_Layout is True.
78
79    --  As part of the processing in gigi, the types are laid out and
80    --  appropriate values computed for the sizes and component positions
81    --  and sizes of records and arrays.
82
83    --  The back-annotation circuit in gigi is responsible for updating the
84    --  relevant fields in the tree to reflect these computations, as follows:
85
86    --    For E_Array_Type entities, the Component_Size field
87
88    --    For all record and array types and subtypes, the Esize field,
89    --    which contains the Size (more accurately the Object_SIze) value
90    --    for the type or subtype.
91
92    --    For E_Component and E_Distriminant entities, the Esize (size
93    --    of component) and Component_Bit_Offset fields. Note that gigi
94    --    does not (yet ???) back annotate Normalized_Position/First_Bit.
95
96    --  There are three cases to consider:
97
98    --    1. The value is constant. In this case, the back annotation works
99    --       by simply storing the non-negative universal integer value in
100    --       the appropriate field corresponding to this constant size.
101
102    --    2. The value depends on variables other than discriminants of the
103    --       current record. In this case, the value is not known, even if
104    --       the complete data of the record is available, and gigi marks
105    --       this situation by storing the special value No_Uint.
106
107    --    3. The value depends on the discriminant values for the current
108    --       record. In this case, gigi back annotates the field with a
109    --       representation of the expression for computing the value in
110    --       terms of the discriminants. A negative Uint value is used to
111    --       represent the value of such an expression, as explained in
112    --       the following section.
113
114    --  GCC expressions are represented with a Uint value that is negative.
115    --  See the body of this package for details on the representation used.
116
117    --  One other case in which gigi back annotates GCC expressions is in
118    --  the Present_Expr field of an N_Variant node. This expression which
119    --  will always depend on discriminants, and hence always be represented
120    --  as a negative Uint value, provides an expression which, when evaluated
121    --  with a given set of discriminant values, indicates whether the variant
122    --  is present for that set of values (result is True, i.e. non-zero) or
123    --  not present (result is False, i.e. zero).
124
125    subtype Node_Ref is Uint;
126    --  Subtype used for negative Uint values used to represent nodes
127
128    subtype Node_Ref_Or_Val is Uint;
129    --  Subtype used for values that can either be a Node_Ref (negative)
130    --  or a value (non-negative)
131
132    type TCode is range 0 .. 27;
133    --  Type used on Ada side to represent DEFTREECODE values defined in
134    --  tree.def. Only a subset of these tree codes can actually appear.
135    --  The names are the names from tree.def in Ada casing.
136
137    --  name                             code   description           operands
138
139    Cond_Expr        : constant TCode :=  1; -- conditional              3
140    Plus_Expr        : constant TCode :=  2; -- addition                 2
141    Minus_Expr       : constant TCode :=  3; -- subtraction              2
142    Mult_Expr        : constant TCode :=  4; -- multiplication           2
143    Trunc_Div_Expr   : constant TCode :=  5; -- truncating division      2
144    Ceil_Div_Expr    : constant TCode :=  6; -- division rounding up     2
145    Floor_Div_Expr   : constant TCode :=  7; -- division rounding down   2
146    Trunc_Mod_Expr   : constant TCode :=  8; -- mod for trunc_div        2
147    Ceil_Mod_Expr    : constant TCode :=  9; -- mod for ceil_div         2
148    Floor_Mod_Expr   : constant TCode := 10; -- mod for floor_div        2
149    Exact_Div_Expr   : constant TCode := 11; -- exact div                2
150    Negate_Expr      : constant TCode := 12; -- negation                 1
151    Min_Expr         : constant TCode := 13; -- minimum                  2
152    Max_Expr         : constant TCode := 14; -- maximum                  2
153    Abs_Expr         : constant TCode := 15; -- absolute value           1
154    Truth_Andif_Expr : constant TCode := 16; -- Boolean and then         2
155    Truth_Orif_Expr  : constant TCode := 17; -- Boolean or else          2
156    Truth_And_Expr   : constant TCode := 18; -- Boolean and              2
157    Truth_Or_Expr    : constant TCode := 19; -- Boolean or               2
158    Truth_Xor_Expr   : constant TCode := 20; -- Boolean xor              2
159    Truth_Not_Expr   : constant TCode := 21; -- Boolean not              1
160    Lt_Expr          : constant TCode := 22; -- comparision <            2
161    Le_Expr          : constant TCode := 23; -- comparision <=           2
162    Gt_Expr          : constant TCode := 24; -- comparision >            2
163    Ge_Expr          : constant TCode := 25; -- comparision >=           2
164    Eq_Expr          : constant TCode := 26; -- comparision =            2
165    Ne_Expr          : constant TCode := 27; -- comparision /=           2
166
167    --  The following entry is used to represent a discriminant value in
168    --  the tree. It has a special tree code that does not correspond
169    --  directly to a gcc node. The single operand is the number of the
170    --  discriminant in the record (1 = first discriminant).
171
172    Discrim_Val : constant TCode := 0;  -- discriminant value       1
173
174    ------------------------
175    -- The gigi Interface --
176    ------------------------
177
178    --  The following declarations are for use by gigi for back annotation
179
180    function Create_Node
181      (Expr  : TCode;
182       Op1   : Node_Ref_Or_Val;
183       Op2   : Node_Ref_Or_Val := No_Uint;
184       Op3   : Node_Ref_Or_Val := No_Uint)
185       return  Node_Ref;
186    --  Creates a node with using the tree code defined by Expr and from
187    --  1-3 operands as required (unused operands set as shown to No_Uint)
188    --  Note that this call can be used to create a discriminant reference
189    --  by using (Expr => Discrim_Val, Op1 => discriminant_number).
190
191    function Create_Discrim_Ref
192      (Discr : Entity_Id)
193       return  Node_Ref;
194    --  Creates a refrerence to the discriminant whose entity is Discr.
195
196    --------------------------------------------------------
197    -- Front-End Interface for Dynamic Size/Offset Values --
198    --------------------------------------------------------
199
200    --  If Backend_Layout is False, then the front-end deals with all
201    --  dynamic size and offset fields. There are two cases:
202
203    --    1. The value can be computed at the time of type freezing, and
204    --       is stored in a run-time constant. In this case, the field
205    --       contains a reference to this entity. In the case of sizes
206    --       the value stored is the size in storage units, since dynamic
207    --       sizes are always a multiple of storage units.
208
209    --    2. The size/offset depends on the value of discriminants at
210    --       run-time. In this case, the front end builds a function to
211    --       compute the value. This function has a single parameter
212    --       which is the discriminated record object in question. Any
213    --       references to discriminant values are simply references to
214    --       the appropriate discriminant in this single argument, and
215    --       to compute the required size/offset value at run time, the
216    --       code generator simply constructs a call to the function
217    --       with the appropriate argument. The size/offset field in
218    --       this case contains a reference to the function entity.
219    --       Note that as for case 1, if such a function is used to
220    --       return a size, then the size in storage units is returned,
221    --       not the size in bits.
222
223    --  The interface here allows these created entities to be referenced
224    --  using negative Unit values, so that they can be stored in the
225    --  appropriate size and offset fields in the tree.
226
227    --  In the case of components, if the location of the component is static,
228    --  then all four fields (Component_Bit_Offset, Normalized_Position, Esize,
229    --  and Normalized_First_Bit) are set to appropraite values. In the case of
230    --  a non-static component location, Component_Bit_Offset is not used and
231    --  is left set to Unknown. Normalized_Position and Normalized_First_Bit
232    --  are set appropriately.
233
234    subtype SO_Ref is Uint;
235    --  Type used to represent a Uint value that represents a static or
236    --  dynamic size/offset value (non-negative if static, negative if
237    --  the size value is dynamic).
238
239    subtype Dynamic_SO_Ref is Uint;
240    --  Type used to represent a negative Uint value used to store
241    --  a dynamic size/offset value.
242
243    function Is_Dynamic_SO_Ref (U : SO_Ref) return Boolean;
244    pragma Inline (Is_Dynamic_SO_Ref);
245    --  Given a SO_Ref (Uint) value, returns True iff the SO_Ref value
246    --  represents a dynamic Size/Offset value (i.e. it is negative).
247
248    function Is_Static_SO_Ref (U : SO_Ref) return Boolean;
249    pragma Inline (Is_Static_SO_Ref);
250    --  Given a SO_Ref (Uint) value, returns True iff the SO_Ref value
251    --  represents a static Size/Offset value (i.e. it is non-negative).
252
253    function Create_Dynamic_SO_Ref
254      (E    : Entity_Id)
255       return Dynamic_SO_Ref;
256    --  Given the Entity_Id for a constant (case 1), the Node_Id for an
257    --  expression (case 2), or the Entity_Id for a function (case 3),
258    --  this function returns a (negative) Uint value that can be used
259    --  to retrieve the entity or expression for later use.
260
261    function Get_Dynamic_SO_Entity
262      (U    : Dynamic_SO_Ref)
263       return Entity_Id;
264    --  Retrieve the Node_Id or Entity_Id stored by a previous call to
265    --  Create_Dynamic_SO_Ref. The approach is that the front end makes
266    --  the necessary Create_Dynamic_SO_Ref calls to associate the node
267    --  and entity id values and the back end makes Get_Dynamic_SO_Ref
268    --  calls to retrive them.
269
270    --------------------
271    -- ASIS_Interface --
272    --------------------
273
274    type Discrim_List is array (Pos range <>) of Uint;
275    --  Type used to represent list of discriminant values
276
277    function Rep_Value
278      (Val  : Node_Ref_Or_Val;
279       D    : Discrim_List)
280       return Uint;
281    --  Given the contents of a First_Bit_Position or Esize field containing
282    --  a node reference (i.e. a negative Uint value) and D, the list of
283    --  discriminant values, returns the interpreted value of this field.
284    --  For convenience, Rep_Value will take a non-negative Uint value
285    --  as an argument value, and return it unmodified. A No_Uint value is
286    --  also returned unmodified.
287
288    procedure Tree_Read;
289    --  Read in the value of the Rep_Table
290
291    ------------------------
292    -- Compiler Interface --
293    ------------------------
294
295    procedure List_Rep_Info;
296    --  Procedure to list representation information
297
298    procedure Tree_Write;
299    --  Write out the value of the Rep_Table
300
301    --------------------------
302    -- Debugging Procedures --
303    --------------------------
304
305    procedure List_GCC_Expression (U : Node_Ref_Or_Val);
306    --  Prints out given expression in symbolic form. Constants are listed
307    --  in decimal numeric form, Discriminants are listed with a # followed
308    --  by the discriminant number, and operators are output in appropriate
309    --  symbolic form No_Uint displays as two question marks. The output is
310    --  on a single line but has no line return after it. This procedure is
311    --  useful only if operating in backend layout mode.
312
313    procedure lgx (U : Node_Ref_Or_Val);
314    --  In backend layout mode, this is like List_GCC_Expression, but
315    --  includes a line return at the end. If operating in front end
316    --  layout mode, then the name of the entity for the size (either
317    --  a function of a variable) is listed followed by a line return.
318
319 end Repinfo;