- add an extra filed for the bit offset\n- renamed access functions\n- renamed entity...
[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 /** The type of an entity. */
98 struct ir_entity {
99         firm_kind kind;       /**< The dynamic type tag for entity. */
100         ident *name;          /**< The name of this entity. */
101         ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
102                                    name.  If the field is read before written a default
103                                    mangling is applies.  The name of the owner is prepended
104                                    to the name of the entity, separated by a underscore.
105                                    E.g.,  for a class `A' with field `a' this
106                                    is the ident for `A_a'. */
107         ir_type *type;        /**< The type of this entity, e.g., a method type, a
108                                    basic type of the language or a class itself. */
109         ir_type *owner;       /**< The compound type (e.g. class type) this entity belongs to. */
110         ir_allocation allocation:3;    /**< Distinguishes static and dynamically allocated
111                                             entities and some further cases. */
112         ir_visibility visibility:3;    /**< Specifies visibility to external program
113                                             fragments. */
114         ir_variability variability:3;  /**< Specifies variability of entities content. */
115         ir_volatility volatility:2;    /**< Specifies volatility of entities content. */
116         ir_stickyness stickyness:2;    /**< Specifies whether this entity is sticky.  */
117         ir_peculiarity peculiarity:3;  /**< The peculiarity of this entity. */
118         unsigned final:1;              /**< If set, this entity cannot be overridden. */
119         unsigned compiler_gen:1;       /**< If set, this entity was compiler generated. */
120         int offset;                    /**< Offset in bytes for this entity.  Fixed when layout
121                                             of owner is determined. */
122         unsigned char offset_bit_remainder;
123                                        /**< If the entity is a bit field, this is the offset of
124                                             the start of the bit field within the byte specified
125                                             by offset. */
126         unsigned long visit;           /**< visited counter for walks of the type information. */
127         struct dbg_info *dbi;          /**< A pointer to information for debug support. */
128         void *link;                    /**< To store some intermediate information. */
129         ir_type *repr_class;           /**< If this entity represents a class info, the associated class. */
130
131         /* ------------- fields for entities owned by a class type ---------------*/
132
133         ir_entity **overwrites;     /**< A list of entities this entity overwrites. */
134         ir_entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
135
136         /* ------------- fields for atomic entities  --------------- */
137         ir_node *value;          /**< value if entity is not of variability uninitialized.
138                                       Only for atomic entities. */
139         union {
140                 /* ------------- fields for compound entities -------------- */
141                 compound_ent_attr cmpd_attr;
142                 /* ------------- fields for method entities ---------------- */
143                 method_ent_attr   mtd_attr;
144         } attr; /**< type specific attributes */
145
146         /* ------------- fields for analyses ---------------*/
147
148 #ifdef DEBUG_libfirm
149         long nr;             /**< A unique node number for each node to make output readable. */
150 # endif /* DEBUG_libfirm */
151 };
152
153 /** Initialize the entity module. */
154 void firm_init_entity(void);
155
156
157 /* ----------------------- inline functions ------------------------ */
158 static INLINE int
159 _is_entity(const void *thing) {
160         return get_kind(thing) == k_entity;
161 }
162
163 static INLINE const char *
164 _get_entity_name(const ir_entity *ent) {
165         assert(ent && ent->kind == k_entity);
166         return get_id_str(get_entity_ident(ent));
167 }
168
169 static INLINE ident *
170 _get_entity_ident(const ir_entity *ent) {
171         assert(ent && ent->kind == k_entity);
172         return ent->name;
173 }
174
175 static INLINE void
176 _set_entity_ident(ir_entity *ent, ident *id) {
177         assert(ent && ent->kind == k_entity);
178         ent->name = id;
179 }
180
181 static INLINE ir_type *
182 _get_entity_owner(ir_entity *ent) {
183         assert(ent && ent->kind == k_entity);
184         return ent->owner = skip_tid(ent->owner);
185 }
186
187 static INLINE ident *
188 _get_entity_ld_ident(ir_entity *ent)
189 {
190         assert(ent && ent->kind == k_entity);
191         if (ent->ld_name == NULL)
192                 ent->ld_name = mangle_entity(ent);
193         return ent->ld_name;
194 }
195
196 static INLINE void
197 _set_entity_ld_ident(ir_entity *ent, ident *ld_ident) {
198         assert(ent && ent->kind == k_entity);
199         ent->ld_name = ld_ident;
200 }
201
202 static INLINE const char *
203 _get_entity_ld_name(ir_entity *ent) {
204         assert(ent && ent->kind == k_entity);
205         return get_id_str(get_entity_ld_ident(ent));
206 }
207
208 static INLINE ir_type *
209 _get_entity_type(ir_entity *ent) {
210         assert(ent && ent->kind == k_entity);
211         return ent->type = skip_tid(ent->type);
212 }
213
214 static INLINE void
215 _set_entity_type(ir_entity *ent, ir_type *type) {
216         assert(ent && ent->kind == k_entity);
217         ent->type = type;
218 }
219
220 static INLINE ir_allocation
221 _get_entity_allocation(const ir_entity *ent) {
222         assert(ent && ent->kind == k_entity);
223         return ent->allocation;
224 }
225
226 static INLINE void
227 _set_entity_allocation(ir_entity *ent, ir_allocation al) {
228         assert(ent && ent->kind == k_entity);
229         ent->allocation = al;
230 }
231
232 static INLINE ir_visibility
233 _get_entity_visibility(const ir_entity *ent) {
234         assert(ent && ent->kind == k_entity);
235         return ent->visibility;
236 }
237
238 static INLINE ir_variability
239 _get_entity_variability(const ir_entity *ent) {
240         assert(ent && ent->kind == k_entity);
241         return ent->variability;
242 }
243
244 static INLINE ir_volatility
245 _get_entity_volatility(const ir_entity *ent) {
246         assert(ent && ent->kind == k_entity);
247         return ent->volatility;
248 }
249
250 static INLINE void
251 _set_entity_volatility(ir_entity *ent, ir_volatility vol) {
252         assert(ent && ent->kind == k_entity);
253         ent->volatility = vol;
254 }
255
256 static INLINE ir_peculiarity
257 _get_entity_peculiarity(const ir_entity *ent) {
258         assert(ent && ent->kind == k_entity);
259         return ent->peculiarity;
260 }
261
262 /**
263  * @todo Why peculiarity only for methods?
264  *       Good question.  Originally, there were only description and
265  *       existent.  The thought was, what sense does it make to
266  *       describe a field?  With inherited the situation changed.  So
267  *       I removed the assertion.  GL, 28.2.05
268  */
269 static INLINE void
270 _set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec) {
271         assert(ent && ent->kind == k_entity);
272         /* @@@ why peculiarity only for methods? */
273         //assert(is_Method_type(ent->type));
274
275         ent->peculiarity = pec;
276 }
277
278 static INLINE ir_stickyness
279 _get_entity_stickyness(const ir_entity *ent) {
280         assert(ent && ent->kind == k_entity);
281         return ent->stickyness;
282 }
283
284 static INLINE void
285 _set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness) {
286         assert(ent && ent->kind == k_entity);
287         ent->stickyness = stickyness;
288 }
289
290 static INLINE int
291 _get_entity_final(const ir_entity *ent) {
292         assert(ent && ent->kind == k_entity);
293         return (int)ent->final;
294 }
295
296 static INLINE void
297 _set_entity_final(ir_entity *ent, int final) {
298         assert(ent && ent->kind == k_entity);
299         ent->final = final ? 1 : 0;
300 }
301
302 static INLINE int
303 _get_entity_offset(const ir_entity *ent) {
304         assert(ent && ent->kind == k_entity);
305         return ent->offset;
306 }
307
308 static INLINE void
309 _set_entity_offset(ir_entity *ent, int offset) {
310         assert(ent && ent->kind == k_entity);
311         ent->offset = offset;
312 }
313
314 static INLINE unsigned char
315 _get_entity_offset_bits_remainder(const ir_entity *ent) {
316         assert(ent && ent->kind == k_entity);
317         return ent->offset_bit_remainder;
318 }
319
320 static INLINE void
321 _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset) {
322         assert(ent && ent->kind == k_entity);
323         ent->offset_bit_remainder = offset;
324 }
325
326 static INLINE void *
327 _get_entity_link(const ir_entity *ent) {
328         assert(ent && ent->kind == k_entity);
329         return ent->link;
330 }
331
332 static INLINE void
333 _set_entity_link(ir_entity *ent, void *l) {
334         assert(ent && ent->kind == k_entity);
335         ent->link = l;
336 }
337
338 static INLINE ir_graph *
339 _get_entity_irg(const ir_entity *ent) {
340         assert(ent && ent->kind == k_entity);
341         assert(ent == unknown_entity || is_Method_type(ent->type));
342         if (!get_visit_pseudo_irgs() && ent->attr.mtd_attr.irg
343                 && is_pseudo_ir_graph(ent->attr.mtd_attr.irg))
344                 return NULL;
345         return ent->attr.mtd_attr.irg;
346 }
347
348 static INLINE unsigned long
349 _get_entity_visited(ir_entity *ent) {
350         assert(ent && ent->kind == k_entity);
351         return ent->visit;
352 }
353
354 static INLINE void
355 _set_entity_visited(ir_entity *ent, unsigned long num) {
356         assert(ent && ent->kind == k_entity);
357         ent->visit = num;
358 }
359
360 static INLINE void
361 _mark_entity_visited(ir_entity *ent) {
362         assert(ent && ent->kind == k_entity);
363         ent->visit = firm_type_visited;
364 }
365
366 static INLINE int
367 _entity_visited(ir_entity *ent) {
368         return _get_entity_visited(ent) >= firm_type_visited;
369 }
370
371 static INLINE int
372 _entity_not_visited(ir_entity *ent) {
373         return _get_entity_visited(ent) < firm_type_visited;
374 }
375
376 static INLINE ir_type *
377 _get_entity_repr_class(const ir_entity *ent) {
378         assert(ent && ent->kind == k_entity);
379         return ent->repr_class;
380 }
381
382 #define is_entity(thing)                         _is_entity(thing)
383 #define get_entity_name(ent)                     _get_entity_name(ent)
384 #define get_entity_ident(ent)                    _get_entity_ident(ent)
385 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
386 #define get_entity_owner(ent)                    _get_entity_owner(ent)
387 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
388 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
389 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
390 #define get_entity_type(ent)                     _get_entity_type(ent)
391 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
392 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
393 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
394 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
395 #define get_entity_variability(ent)              _get_entity_variability(ent)
396 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
397 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
398 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
399 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
400 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
401 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
402 #define get_entity_final(ent)                    _get_entity_final(ent)
403 #define set_entity_final(ent, final)             _set_entity_final(ent, final)
404 #define get_entity_offset(ent)                   _get_entity_offset(ent)
405 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
406 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
407 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
408 #define get_entity_link(ent)                     _get_entity_link(ent)
409 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
410 #define get_entity_irg(ent)                      _get_entity_irg(ent)
411 #define get_entity_visited(ent)                  _get_entity_visited(ent)
412 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
413 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
414 #define entity_visited(ent)                      _entity_visited(ent)
415 #define entity_not_visited(ent)                  _entity_not_visited(ent)
416 #define get_entity_repr_class(ent)               _get_entity_repr_class(ent)
417
418
419 #endif /* _FIRM_TR_ENTITY_T_H_ */