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