cleanup and improve generate_opcode script, you can now have nodes with variable...
[libfirm] / ir / tr / entity_t.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /*
21  * @file     entity_t.h
22  * @brief   Representation of all program known entities -- private header.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #ifndef FIRM_TR_ENTITY_T_H
27 #define FIRM_TR_ENTITY_T_H
28
29 #include <assert.h>
30
31 #include "firm_common_t.h"
32 #include "firm_config.h"
33
34 #include "typerep.h"
35 #include "type_t.h"
36 #include "ident.h"
37 #include "pseudo_irg.h"
38
39 /** A path in a compound graph. */
40 struct compound_graph_path {
41         firm_kind kind;       /**< The dynamic type tag for compound graph path. */
42         ir_type *tp;          /**< The type this path belongs to. */
43         int len;              /**< The length of the path. */
44         struct tuple {
45                 int       index;    /**< Array index.  To compute position of array elements */
46                 ir_entity *node;    /**< The accessed entity. */
47         } list[1];            /**< List of entity/index tuple of length len to express the
48                                    access path. */
49 };
50
51 /** The attributes for atomic entities. */
52 typedef struct atomic_ent_attr {
53         ir_node *value;            /**< value if entity is not of variability uninitialized.
54                                      Only for atomic entities. */
55 } atomic_ent_attr;
56
57 /** The attributes for compound entities. */
58 typedef struct compound_ent_attr {
59         ir_node **values;     /**< constant values of compound entities. Only available if
60                                    variability not uninitialized.  Must be set for variability constant. */
61         compound_graph_path **val_paths;
62                              /**< paths corresponding to constant values. Only available if
63                                   variability not uninitialized.  Must be set for variability constant. */
64 } compound_ent_attr;
65
66 /** A reserved value for "not yet set". */
67 #define VTABLE_NUM_NOT_SET ((unsigned)(-1))
68
69 /** The attributes for methods. */
70 typedef struct method_ent_attr {
71         ir_graph *irg;                 /**< The corresponding irg if known.
72                                             The ir_graph constructor automatically sets this field. */
73         unsigned irg_add_properties;   /**< Additional graph properties can be
74                                             stored in a entity if no irg is available. */
75
76         unsigned vtable_number;        /**< For a dynamically called method, the number assigned
77                                             in the virtual function table. */
78
79         ptr_access_kind *param_access; /**< the parameter access */
80         float *param_weight;           /**< The weight of method's parameters. Parameters
81                                             with a high weight are good for procedure cloning. */
82         ir_img_section section;        /**< The code section where this method should be placed */
83 } method_ent_attr;
84
85
86 /**
87  * An abstract data type to represent program entities.
88  *
89  * @see  ir_type
90  */
91 struct ir_entity {
92         firm_kind kind;       /**< The dynamic type tag for entity. */
93         ident *name;          /**< The name of this entity. */
94         ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
95                                    name.  If the field is read before written a default
96                                    mangling is applies.  The name of the owner is prepended
97                                    to the name of the entity, separated by a underscore.
98                                    E.g.,  for a class `A' with field `a' this
99                                    is the ident for `A_a'. */
100         ir_type *type;        /**< The type of this entity, e.g., a method type, a
101                                    basic type of the language or a class itself. */
102         ir_type *owner;       /**< The compound type (e.g. class type) this entity belongs to. */
103         ir_allocation allocation:3;             /**< Distinguishes static and dynamically allocated
104                                                      entities and some further cases. */
105         ir_visibility visibility:3;             /**< Specifies visibility to external program fragments. */
106         ir_variability variability:3;           /**< Specifies variability of entities content. */
107         ir_volatility volatility:2;             /**< Specifies volatility of entities content. */
108         ir_stickyness stickyness:2;             /**< Specifies whether this entity is sticky.  */
109         ir_peculiarity peculiarity:3;           /**< The peculiarity of this entity. */
110         ir_address_taken_state address_taken:3; /**< A flag that can be set to mark address taken entities. */
111         unsigned final:1;              /**< If set, this entity cannot be overridden. */
112         unsigned compiler_gen:1;       /**< If set, this entity was compiler generated. */
113         int offset;                    /**< Offset in bytes for this entity.  Fixed when layout
114                                             of owner is determined. */
115         unsigned char offset_bit_remainder;
116                                        /**< If the entity is a bit field, this is the offset of
117                                             the start of the bit field within the byte specified
118                                             by offset. */
119         unsigned long visit;           /**< visited counter for walks of the type information. */
120         struct dbg_info *dbi;          /**< A pointer to information for debug support. */
121         void *link;                    /**< To store some intermediate information. */
122         ir_type *repr_class;           /**< If this entity represents a class info, the associated class. */
123
124         /* ------------- fields for entities owned by a class type ---------------*/
125
126         ir_entity **overwrites;     /**< A list of entities this entity overwrites. */
127         ir_entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
128
129         /* ------------- fields for atomic entities  --------------- */
130         ir_node *value;          /**< value if entity is not of variability uninitialized.
131                                       Only for atomic entities. */
132         union {
133                 /* ------------- fields for compound entities -------------- */
134                 compound_ent_attr cmpd_attr;
135                 /* ------------- fields for method entities ---------------- */
136                 method_ent_attr   mtd_attr;
137         } attr; /**< type specific attributes */
138
139         /* ------------- fields for analyses ---------------*/
140
141 #ifdef DEBUG_libfirm
142         long nr;             /**< A unique node number for each node to make output readable. */
143 # endif /* DEBUG_libfirm */
144 };
145
146 /** Initialize the entity module. */
147 void firm_init_entity(void);
148
149
150 /* ----------------------- inline functions ------------------------ */
151 static INLINE int
152 _is_entity(const void *thing) {
153         return get_kind(thing) == k_entity;
154 }
155
156 static INLINE const char *
157 _get_entity_name(const ir_entity *ent) {
158         assert(ent && ent->kind == k_entity);
159         return get_id_str(get_entity_ident(ent));
160 }
161
162 static INLINE ident *
163 _get_entity_ident(const ir_entity *ent) {
164         assert(ent && ent->kind == k_entity);
165         return ent->name;
166 }
167
168 static INLINE void
169 _set_entity_ident(ir_entity *ent, ident *id) {
170         assert(ent && ent->kind == k_entity);
171         ent->name = id;
172 }
173
174 static INLINE ir_type *
175 _get_entity_owner(ir_entity *ent) {
176         assert(ent && ent->kind == k_entity);
177         return ent->owner = skip_tid(ent->owner);
178 }
179
180 static INLINE ident *
181 _get_entity_ld_ident(ir_entity *ent)
182 {
183         assert(ent && ent->kind == k_entity);
184         if (ent->ld_name == NULL)
185                 ent->ld_name = mangle_entity(ent);
186         return ent->ld_name;
187 }
188
189 static INLINE void
190 _set_entity_ld_ident(ir_entity *ent, ident *ld_ident) {
191         assert(ent && ent->kind == k_entity);
192         ent->ld_name = ld_ident;
193 }
194
195 static INLINE const char *
196 _get_entity_ld_name(ir_entity *ent) {
197         assert(ent && ent->kind == k_entity);
198         return get_id_str(get_entity_ld_ident(ent));
199 }
200
201 static INLINE ir_type *
202 _get_entity_type(ir_entity *ent) {
203         assert(ent && ent->kind == k_entity);
204         return ent->type = skip_tid(ent->type);
205 }
206
207 static INLINE void
208 _set_entity_type(ir_entity *ent, ir_type *type) {
209         assert(ent && ent->kind == k_entity);
210         ent->type = type;
211 }
212
213 static INLINE ir_allocation
214 _get_entity_allocation(const ir_entity *ent) {
215         assert(ent && ent->kind == k_entity);
216         return ent->allocation;
217 }
218
219 static INLINE void
220 _set_entity_allocation(ir_entity *ent, ir_allocation al) {
221         assert(ent && ent->kind == k_entity);
222         ent->allocation = al;
223 }
224
225 static INLINE ir_visibility
226 _get_entity_visibility(const ir_entity *ent) {
227         assert(ent && ent->kind == k_entity);
228         return ent->visibility;
229 }
230
231 static INLINE ir_variability
232 _get_entity_variability(const ir_entity *ent) {
233         assert(ent && ent->kind == k_entity);
234         return ent->variability;
235 }
236
237 static INLINE ir_volatility
238 _get_entity_volatility(const ir_entity *ent) {
239         assert(ent && ent->kind == k_entity);
240         return ent->volatility;
241 }
242
243 static INLINE void
244 _set_entity_volatility(ir_entity *ent, ir_volatility vol) {
245         assert(ent && ent->kind == k_entity);
246         ent->volatility = vol;
247 }
248
249 static INLINE ir_peculiarity
250 _get_entity_peculiarity(const ir_entity *ent) {
251         assert(ent && ent->kind == k_entity);
252         return ent->peculiarity;
253 }
254
255 /**
256  * @todo Why peculiarity only for methods?
257  *       Good question.  Originally, there were only description and
258  *       existent.  The thought was, what sense does it make to
259  *       describe a field?  With inherited the situation changed.  So
260  *       I removed the assertion.  GL, 28.2.05
261  */
262 static INLINE void
263 _set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec) {
264         assert(ent && ent->kind == k_entity);
265         /* @@@ why peculiarity only for methods? */
266         //assert(is_Method_type(ent->type));
267
268         ent->peculiarity = pec;
269 }
270
271 static INLINE ir_stickyness
272 _get_entity_stickyness(const ir_entity *ent) {
273         assert(ent && ent->kind == k_entity);
274         return ent->stickyness;
275 }
276
277 static INLINE void
278 _set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness) {
279         assert(ent && ent->kind == k_entity);
280         ent->stickyness = stickyness;
281 }
282
283 static INLINE int
284 _is_entity_final(const ir_entity *ent) {
285         assert(ent && ent->kind == k_entity);
286         return (int)ent->final;
287 }
288
289 static INLINE void
290 _set_entity_final(ir_entity *ent, int final) {
291         assert(ent && ent->kind == k_entity);
292         ent->final = final ? 1 : 0;
293 }
294
295 static INLINE int
296 _is_entity_compiler_generated(const ir_entity *ent) {
297         assert(ent && ent->kind == k_entity);
298         return ent->compiler_gen;
299 }
300
301 static INLINE void
302 _set_entity_compiler_generated(ir_entity *ent, int flag) {
303         assert(ent && ent->kind == k_entity);
304         ent->compiler_gen = flag ? 1 : 0;
305 }
306
307 static INLINE ir_address_taken_state
308 _get_entity_address_taken(const ir_entity *ent) {
309         assert(ent && ent->kind == k_entity);
310         return ent->address_taken;
311 }
312
313 static INLINE void
314 _set_entity_address_taken(ir_entity *ent, ir_address_taken_state state) {
315         assert(ent && ent->kind == k_entity);
316         assert(ir_address_not_taken <= state && state <= ir_address_taken);
317         ent->address_taken = state;
318 }
319
320 static INLINE int
321 _get_entity_offset(const ir_entity *ent) {
322         assert(ent && ent->kind == k_entity);
323         return ent->offset;
324 }
325
326 static INLINE void
327 _set_entity_offset(ir_entity *ent, int offset) {
328         assert(ent && ent->kind == k_entity);
329         ent->offset = offset;
330 }
331
332 static INLINE unsigned char
333 _get_entity_offset_bits_remainder(const ir_entity *ent) {
334         assert(ent && ent->kind == k_entity);
335         return ent->offset_bit_remainder;
336 }
337
338 static INLINE void
339 _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset) {
340         assert(ent && ent->kind == k_entity);
341         ent->offset_bit_remainder = offset;
342 }
343
344 static INLINE void *
345 _get_entity_link(const ir_entity *ent) {
346         assert(ent && ent->kind == k_entity);
347         return ent->link;
348 }
349
350 static INLINE void
351 _set_entity_link(ir_entity *ent, void *l) {
352         assert(ent && ent->kind == k_entity);
353         ent->link = l;
354 }
355
356 static INLINE ir_graph *
357 _get_entity_irg(const ir_entity *ent) {
358         assert(ent && ent->kind == k_entity);
359         assert(ent == unknown_entity || is_Method_type(ent->type));
360         if (!get_visit_pseudo_irgs() && ent->attr.mtd_attr.irg
361                 && is_pseudo_ir_graph(ent->attr.mtd_attr.irg))
362                 return NULL;
363         return ent->attr.mtd_attr.irg;
364 }
365
366 static INLINE unsigned long
367 _get_entity_visited(ir_entity *ent) {
368         assert(ent && ent->kind == k_entity);
369         return ent->visit;
370 }
371
372 static INLINE void
373 _set_entity_visited(ir_entity *ent, unsigned long num) {
374         assert(ent && ent->kind == k_entity);
375         ent->visit = num;
376 }
377
378 static INLINE void
379 _mark_entity_visited(ir_entity *ent) {
380         assert(ent && ent->kind == k_entity);
381         ent->visit = firm_type_visited;
382 }
383
384 static INLINE int
385 _entity_visited(ir_entity *ent) {
386         return _get_entity_visited(ent) >= firm_type_visited;
387 }
388
389 static INLINE int
390 _entity_not_visited(ir_entity *ent) {
391         return _get_entity_visited(ent) < firm_type_visited;
392 }
393
394 static INLINE ir_type *
395 _get_entity_repr_class(const ir_entity *ent) {
396         assert(ent && ent->kind == k_entity);
397         return ent->repr_class;
398 }
399
400 #define is_entity(thing)                         _is_entity(thing)
401 #define get_entity_name(ent)                     _get_entity_name(ent)
402 #define get_entity_ident(ent)                    _get_entity_ident(ent)
403 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
404 #define get_entity_owner(ent)                    _get_entity_owner(ent)
405 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
406 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
407 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
408 #define get_entity_type(ent)                     _get_entity_type(ent)
409 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
410 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
411 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
412 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
413 #define get_entity_variability(ent)              _get_entity_variability(ent)
414 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
415 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
416 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
417 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
418 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
419 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
420 #define is_entity_final(ent)                     _is_entity_final(ent)
421 #define set_entity_final(ent, final)             _set_entity_final(ent, final)
422 #define is_entity_compiler_generated(ent)        _is_entity_compiler_generated(ent)
423 #define set_entity_compiler_generated(ent, flag) _set_entity_compiler_generated(ent, flag)
424 #define get_entity_address_taken(ent)            _get_entity_address_taken(ent)
425 #define set_entity_address_taken(ent, flag)      _set_entity_address_taken(ent, flag)
426 #define get_entity_offset(ent)                   _get_entity_offset(ent)
427 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
428 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
429 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
430 #define get_entity_link(ent)                     _get_entity_link(ent)
431 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
432 #define get_entity_irg(ent)                      _get_entity_irg(ent)
433 #define get_entity_visited(ent)                  _get_entity_visited(ent)
434 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
435 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
436 #define entity_visited(ent)                      _entity_visited(ent)
437 #define entity_not_visited(ent)                  _entity_not_visited(ent)
438 #define get_entity_repr_class(ent)               _get_entity_repr_class(ent)
439
440
441 #endif /* FIRM_TR_ENTITY_T_H */