bb26a1dcf91087b695970a3d3a8f456a57ac1b1d
[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-2006 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
117                                             fragments. */
118         ir_variability variability:3;  /**< Specifies variability of entities content. */
119         ir_volatility volatility:2;    /**< Specifies volatility of entities content. */
120         ir_stickyness stickyness:2;    /**< Specifies whether this entity is sticky.  */
121         ir_peculiarity peculiarity:3;  /**< The peculiarity of this entity. */
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 _get_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 _get_entity_offset(const ir_entity *ent) {
308         assert(ent && ent->kind == k_entity);
309         return ent->offset;
310 }
311
312 static INLINE void
313 _set_entity_offset(ir_entity *ent, int offset) {
314         assert(ent && ent->kind == k_entity);
315         ent->offset = offset;
316 }
317
318 static INLINE unsigned char
319 _get_entity_offset_bits_remainder(const ir_entity *ent) {
320         assert(ent && ent->kind == k_entity);
321         return ent->offset_bit_remainder;
322 }
323
324 static INLINE void
325 _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset) {
326         assert(ent && ent->kind == k_entity);
327         ent->offset_bit_remainder = offset;
328 }
329
330 static INLINE void *
331 _get_entity_link(const ir_entity *ent) {
332         assert(ent && ent->kind == k_entity);
333         return ent->link;
334 }
335
336 static INLINE void
337 _set_entity_link(ir_entity *ent, void *l) {
338         assert(ent && ent->kind == k_entity);
339         ent->link = l;
340 }
341
342 static INLINE ir_graph *
343 _get_entity_irg(const ir_entity *ent) {
344         assert(ent && ent->kind == k_entity);
345         assert(ent == unknown_entity || is_Method_type(ent->type));
346         if (!get_visit_pseudo_irgs() && ent->attr.mtd_attr.irg
347                 && is_pseudo_ir_graph(ent->attr.mtd_attr.irg))
348                 return NULL;
349         return ent->attr.mtd_attr.irg;
350 }
351
352 static INLINE unsigned long
353 _get_entity_visited(ir_entity *ent) {
354         assert(ent && ent->kind == k_entity);
355         return ent->visit;
356 }
357
358 static INLINE void
359 _set_entity_visited(ir_entity *ent, unsigned long num) {
360         assert(ent && ent->kind == k_entity);
361         ent->visit = num;
362 }
363
364 static INLINE void
365 _mark_entity_visited(ir_entity *ent) {
366         assert(ent && ent->kind == k_entity);
367         ent->visit = firm_type_visited;
368 }
369
370 static INLINE int
371 _entity_visited(ir_entity *ent) {
372         return _get_entity_visited(ent) >= firm_type_visited;
373 }
374
375 static INLINE int
376 _entity_not_visited(ir_entity *ent) {
377         return _get_entity_visited(ent) < firm_type_visited;
378 }
379
380 static INLINE ir_type *
381 _get_entity_repr_class(const ir_entity *ent) {
382         assert(ent && ent->kind == k_entity);
383         return ent->repr_class;
384 }
385
386 #define is_entity(thing)                         _is_entity(thing)
387 #define get_entity_name(ent)                     _get_entity_name(ent)
388 #define get_entity_ident(ent)                    _get_entity_ident(ent)
389 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
390 #define get_entity_owner(ent)                    _get_entity_owner(ent)
391 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
392 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
393 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
394 #define get_entity_type(ent)                     _get_entity_type(ent)
395 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
396 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
397 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
398 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
399 #define get_entity_variability(ent)              _get_entity_variability(ent)
400 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
401 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
402 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
403 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
404 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
405 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
406 #define get_entity_final(ent)                    _get_entity_final(ent)
407 #define set_entity_final(ent, final)             _set_entity_final(ent, final)
408 #define get_entity_offset(ent)                   _get_entity_offset(ent)
409 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
410 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
411 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
412 #define get_entity_link(ent)                     _get_entity_link(ent)
413 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
414 #define get_entity_irg(ent)                      _get_entity_irg(ent)
415 #define get_entity_visited(ent)                  _get_entity_visited(ent)
416 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
417 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
418 #define entity_visited(ent)                      _entity_visited(ent)
419 #define entity_not_visited(ent)                  _entity_not_visited(ent)
420 #define get_entity_repr_class(ent)               _get_entity_repr_class(ent)
421
422
423 #endif /* _FIRM_TR_ENTITY_T_H_ */