0f306b1529a8b5b3f1a163ae471323bd01501781
[libfirm] / ir / tr / entity_t.h
1 /*
2  * Copyright (C) 1995-2008 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 typedef struct ir_initializer_base_t {
40         ir_initializer_kind_t kind;
41 } ir_initializer_base_t;
42
43 typedef struct ir_initializer_compound_t {
44         ir_initializer_base_t  base;
45         unsigned               n_initializers;
46         ir_initializer_t      *initializers[1];
47 } ir_initializer_compound_t;
48
49 typedef struct ir_initializer_const_t {
50         ir_initializer_base_t  base;
51         ir_node               *value;
52 } ir_initializer_const_t ;
53
54 typedef struct ir_initializer_tarval_t {
55         ir_initializer_base_t  base;
56         tarval                *value;
57 } ir_initializer_tarval_t ;
58
59 union ir_initializer_t {
60         ir_initializer_kind_t      kind;
61         ir_initializer_base_t      base;
62         ir_initializer_compound_t  compound;
63         ir_initializer_const_t     consti;
64         ir_initializer_tarval_t    tarval;
65 };
66
67 /** A path in a compound graph. */
68 struct compound_graph_path {
69         firm_kind kind;       /**< The dynamic type tag for compound graph path. */
70         ir_type *tp;          /**< The type this path belongs to. */
71         int len;              /**< The length of the path. */
72         struct tuple {
73                 int       index;    /**< Array index.  To compute position of array elements */
74                 ir_entity *node;    /**< The accessed entity. */
75         } list[1];            /**< List of entity/index tuple of length len to express the
76                                    access path. */
77 };
78
79 /** The attributes for atomic entities. */
80 typedef struct atomic_ent_attr {
81         ir_node *value;            /**< value if entity is not of variability uninitialized.
82                                      Only for atomic entities. */
83 } atomic_ent_attr;
84
85 /** The attributes for compound entities. */
86 typedef struct compound_ent_attr {
87         ir_node **values;     /**< constant values of compound entities. Only available if
88                                    variability not uninitialized.  Must be set for variability constant. */
89         compound_graph_path **val_paths;
90                              /**< paths corresponding to constant values. Only available if
91                                   variability not uninitialized.  Must be set for variability constant. */
92 } compound_ent_attr;
93
94 /** A reserved value for "not yet set". */
95 #define VTABLE_NUM_NOT_SET ((unsigned)(-1))
96
97 /** The attributes for methods. */
98 typedef struct method_ent_attr {
99         ir_graph *irg;                 /**< The corresponding irg if known.
100                                             The ir_graph constructor automatically sets this field. */
101         unsigned irg_add_properties;   /**< Additional graph properties can be
102                                             stored in a entity if no irg is available. */
103
104         unsigned vtable_number;        /**< For a dynamically called method, the number assigned
105                                             in the virtual function table. */
106
107         ptr_access_kind *param_access; /**< the parameter access */
108         float *param_weight;           /**< The weight of method's parameters. Parameters
109                                             with a high weight are good for procedure cloning. */
110         ir_img_section section;        /**< The code section where this method should be placed */
111 } method_ent_attr;
112
113
114 /**
115  * An abstract data type to represent program entities.
116  *
117  * @see  ir_type
118  */
119 struct ir_entity {
120         firm_kind kind;       /**< The dynamic type tag for entity. */
121         ident *name;          /**< The name of this entity. */
122         ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
123                                    name.  If the field is read before written a default
124                                    mangling is applies.  The name of the owner is prepended
125                                    to the name of the entity, separated by a underscore.
126                                    E.g.,  for a class `A' with field `a' this
127                                    is the ident for `A_a'. */
128         ir_type *type;        /**< The type of this entity, e.g., a method type, a
129                                    basic type of the language or a class itself. */
130         ir_type *owner;       /**< The compound type (e.g. class type) this entity belongs to. */
131         unsigned allocation:3;         /**< Distinguishes static and dynamically allocated
132                                             entities and some further cases. */
133         unsigned visibility:3;         /**< Specifies visibility to external program fragments. */
134         unsigned variability:3;        /**< Specifies variability of entities content. */
135         unsigned volatility:1;         /**< Specifies volatility of entities content. */
136         unsigned align:1;              /**< Specifies alignment of entities content. */
137         unsigned stickyness:2;         /**< Specifies whether this entity is sticky.  */
138         unsigned peculiarity:3;        /**< The peculiarity of this entity. */
139         unsigned address_taken:3;      /**< A flag that can be set to mark address taken entities. */
140         unsigned final:1;              /**< If set, this entity cannot be overridden. */
141         unsigned compiler_gen:1;       /**< If set, this entity was compiler generated. */
142         unsigned backend_marked:1;     /**< If set, this entity was marked by the backend for emission. */
143         unsigned has_initializer:1;
144         int offset;                    /**< Offset in bytes for this entity.  Fixed when layout
145                                             of owner is determined. */
146         unsigned char offset_bit_remainder;
147                                        /**< If the entity is a bit field, this is the offset of
148                                             the start of the bit field within the byte specified
149                                             by offset. */
150         unsigned long visit;           /**< visited counter for walks of the type information. */
151         struct dbg_info *dbi;          /**< A pointer to information for debug support. */
152         void *link;                    /**< To store some intermediate information. */
153         ir_type *repr_class;           /**< If this entity represents a class info, the associated class. */
154
155         /* ------------- fields for entities owned by a class type ---------------*/
156
157         ir_entity **overwrites;     /**< A list of entities this entity overwrites. */
158         ir_entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
159
160         /* ------------- fields for atomic entities  --------------- */
161         ir_node *value;          /**< value if entity is not of variability uninitialized.
162                                       Only for atomic entities. */
163         union {
164                 /* ------------- fields for compound entities -------------- */
165                 compound_ent_attr cmpd_attr;
166                 /* ------------- fields for method entities ---------------- */
167                 method_ent_attr   mtd_attr;
168                 /* entity initializer */
169                 ir_initializer_t *initializer;
170         } attr; /**< type specific attributes */
171
172         /* ------------- fields for analyses ---------------*/
173
174 #ifdef DEBUG_libfirm
175         long nr;             /**< A unique node number for each node to make output readable. */
176 # endif /* DEBUG_libfirm */
177 };
178
179 /** Initialize the entity module. */
180 void firm_init_entity(void);
181
182
183 /* ----------------------- inline functions ------------------------ */
184 static INLINE int
185 _is_entity(const void *thing) {
186         return get_kind(thing) == k_entity;
187 }
188
189 static INLINE const char *
190 _get_entity_name(const ir_entity *ent) {
191         assert(ent && ent->kind == k_entity);
192         return get_id_str(get_entity_ident(ent));
193 }
194
195 static INLINE ident *
196 _get_entity_ident(const ir_entity *ent) {
197         assert(ent && ent->kind == k_entity);
198         return ent->name;
199 }
200
201 static INLINE void
202 _set_entity_ident(ir_entity *ent, ident *id) {
203         assert(ent && ent->kind == k_entity);
204         ent->name = id;
205 }
206
207 static INLINE ir_type *
208 _get_entity_owner(ir_entity *ent) {
209         assert(ent && ent->kind == k_entity);
210         return ent->owner = skip_tid(ent->owner);
211 }
212
213 static INLINE ident *
214 _get_entity_ld_ident(ir_entity *ent)
215 {
216         assert(ent && ent->kind == k_entity);
217         if (ent->ld_name == NULL)
218                 ent->ld_name = mangle_entity(ent);
219         return ent->ld_name;
220 }
221
222 static INLINE void
223 _set_entity_ld_ident(ir_entity *ent, ident *ld_ident) {
224         assert(ent && ent->kind == k_entity);
225         ent->ld_name = ld_ident;
226 }
227
228 static INLINE const char *
229 _get_entity_ld_name(ir_entity *ent) {
230         assert(ent && ent->kind == k_entity);
231         return get_id_str(get_entity_ld_ident(ent));
232 }
233
234 static INLINE ir_type *
235 _get_entity_type(ir_entity *ent) {
236         assert(ent && ent->kind == k_entity);
237         return ent->type = skip_tid(ent->type);
238 }
239
240 static INLINE void
241 _set_entity_type(ir_entity *ent, ir_type *type) {
242         assert(ent && ent->kind == k_entity);
243         ent->type = type;
244 }
245
246 static INLINE ir_allocation
247 _get_entity_allocation(const ir_entity *ent) {
248         assert(ent && ent->kind == k_entity);
249         return ent->allocation;
250 }
251
252 static INLINE void
253 _set_entity_allocation(ir_entity *ent, ir_allocation al) {
254         assert(ent && ent->kind == k_entity);
255         ent->allocation = al;
256 }
257
258 static INLINE ir_visibility
259 _get_entity_visibility(const ir_entity *ent) {
260         assert(ent && ent->kind == k_entity);
261         return ent->visibility;
262 }
263
264 static INLINE ir_variability
265 _get_entity_variability(const ir_entity *ent) {
266         assert(ent && ent->kind == k_entity);
267         return ent->variability;
268 }
269
270 static INLINE ir_volatility
271 _get_entity_volatility(const ir_entity *ent) {
272         assert(ent && ent->kind == k_entity);
273         return ent->volatility;
274 }
275
276 static INLINE void
277 _set_entity_volatility(ir_entity *ent, ir_volatility vol) {
278         assert(ent && ent->kind == k_entity);
279         ent->volatility = vol;
280 }
281
282 static INLINE ir_align
283 _get_entity_align(const ir_entity *ent) {
284         assert(ent && ent->kind == k_entity);
285         return ent->align;
286 }
287
288 static INLINE void
289 _set_entity_align(ir_entity *ent, ir_align a) {
290         assert(ent && ent->kind == k_entity);
291         ent->align = a;
292 }
293
294 static INLINE ir_peculiarity
295 _get_entity_peculiarity(const ir_entity *ent) {
296         assert(ent && ent->kind == k_entity);
297         return ent->peculiarity;
298 }
299
300 /**
301  * @todo Why peculiarity only for methods?
302  *       Good question.  Originally, there were only description and
303  *       existent.  The thought was, what sense does it make to
304  *       describe a field?  With inherited the situation changed.  So
305  *       I removed the assertion.  GL, 28.2.05
306  */
307 static INLINE void
308 _set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec) {
309         assert(ent && ent->kind == k_entity);
310         /* @@@ why peculiarity only for methods? */
311         //assert(is_Method_type(ent->type));
312
313         ent->peculiarity = pec;
314 }
315
316 static INLINE ir_stickyness
317 _get_entity_stickyness(const ir_entity *ent) {
318         assert(ent && ent->kind == k_entity);
319         return ent->stickyness;
320 }
321
322 static INLINE void
323 _set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness) {
324         assert(ent && ent->kind == k_entity);
325         ent->stickyness = stickyness;
326 }
327
328 static INLINE int
329 _is_entity_final(const ir_entity *ent) {
330         assert(ent && ent->kind == k_entity);
331         return (int)ent->final;
332 }
333
334 static INLINE void
335 _set_entity_final(ir_entity *ent, int final) {
336         assert(ent && ent->kind == k_entity);
337         ent->final = final ? 1 : 0;
338 }
339
340 static INLINE int
341 _is_entity_compiler_generated(const ir_entity *ent) {
342         assert(ent && ent->kind == k_entity);
343         return ent->compiler_gen;
344 }
345
346 static INLINE void
347 _set_entity_compiler_generated(ir_entity *ent, int flag) {
348         assert(ent && ent->kind == k_entity);
349         ent->compiler_gen = flag ? 1 : 0;
350 }
351
352 static INLINE int
353 _is_entity_backend_marked(const ir_entity *ent) {
354         assert(ent && ent->kind == k_entity);
355         return ent->backend_marked;
356 }
357
358 static INLINE void
359 _set_entity_backend_marked(ir_entity *ent, int flag) {
360         assert(ent && ent->kind == k_entity);
361         ent->backend_marked = flag ? 1 : 0;
362 }
363
364 static INLINE ir_address_taken_state
365 _get_entity_address_taken(const ir_entity *ent) {
366         assert(ent && ent->kind == k_entity);
367         return ent->address_taken;
368 }
369
370 static INLINE void
371 _set_entity_address_taken(ir_entity *ent, ir_address_taken_state state) {
372         assert(ent && ent->kind == k_entity);
373         assert(state == ir_address_not_taken ||
374                         state == ir_address_taken_unknown ||
375                         state == ir_address_taken);
376         ent->address_taken = state;
377 }
378
379 static INLINE int
380 _get_entity_offset(const ir_entity *ent) {
381         assert(ent && ent->kind == k_entity);
382         return ent->offset;
383 }
384
385 static INLINE void
386 _set_entity_offset(ir_entity *ent, int offset) {
387         assert(ent && ent->kind == k_entity);
388         ent->offset = offset;
389 }
390
391 static INLINE unsigned char
392 _get_entity_offset_bits_remainder(const ir_entity *ent) {
393         assert(ent && ent->kind == k_entity);
394         return ent->offset_bit_remainder;
395 }
396
397 static INLINE void
398 _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset) {
399         assert(ent && ent->kind == k_entity);
400         ent->offset_bit_remainder = offset;
401 }
402
403 static INLINE void *
404 _get_entity_link(const ir_entity *ent) {
405         assert(ent && ent->kind == k_entity);
406         return ent->link;
407 }
408
409 static INLINE void
410 _set_entity_link(ir_entity *ent, void *l) {
411         assert(ent && ent->kind == k_entity);
412         ent->link = l;
413 }
414
415 static INLINE ir_graph *
416 _get_entity_irg(const ir_entity *ent) {
417         ir_graph *irg;
418         assert(ent && ent->kind == k_entity);
419         assert(ent == unknown_entity || is_Method_type(ent->type));
420         irg = ent->attr.mtd_attr.irg;
421         if (irg != NULL && !get_visit_pseudo_irgs()     && is_pseudo_ir_graph(irg))
422                 return NULL;
423         return irg;
424 }
425
426 static INLINE unsigned long
427 _get_entity_visited(ir_entity *ent) {
428         assert(ent && ent->kind == k_entity);
429         return ent->visit;
430 }
431
432 static INLINE void
433 _set_entity_visited(ir_entity *ent, unsigned long num) {
434         assert(ent && ent->kind == k_entity);
435         ent->visit = num;
436 }
437
438 static INLINE void
439 _mark_entity_visited(ir_entity *ent) {
440         assert(ent && ent->kind == k_entity);
441         ent->visit = firm_type_visited;
442 }
443
444 static INLINE int
445 _entity_visited(ir_entity *ent) {
446         return _get_entity_visited(ent) >= firm_type_visited;
447 }
448
449 static INLINE int
450 _entity_not_visited(ir_entity *ent) {
451         return _get_entity_visited(ent) < firm_type_visited;
452 }
453
454 static INLINE ir_type *
455 _get_entity_repr_class(const ir_entity *ent) {
456         assert(ent && ent->kind == k_entity);
457         return ent->repr_class;
458 }
459
460 static INLINE dbg_info *
461 _get_entity_dbg_info(const ir_entity *ent) {
462         return ent->dbi;
463 }
464
465 static INLINE void
466 _set_entity_dbg_info(ir_entity *ent, dbg_info *db) {
467         ent->dbi = db;
468 }
469
470
471 #define is_entity(thing)                         _is_entity(thing)
472 #define get_entity_name(ent)                     _get_entity_name(ent)
473 #define get_entity_ident(ent)                    _get_entity_ident(ent)
474 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
475 #define get_entity_owner(ent)                    _get_entity_owner(ent)
476 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
477 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
478 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
479 #define get_entity_type(ent)                     _get_entity_type(ent)
480 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
481 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
482 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
483 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
484 #define get_entity_variability(ent)              _get_entity_variability(ent)
485 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
486 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
487 #define get_entity_align(ent)                    _get_entity_align(ent)
488 #define set_entity_align(ent, a)                 _set_entity_align(ent, a)
489 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
490 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
491 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
492 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
493 #define is_entity_final(ent)                     _is_entity_final(ent)
494 #define set_entity_final(ent, final)             _set_entity_final(ent, final)
495 #define is_entity_compiler_generated(ent)        _is_entity_compiler_generated(ent)
496 #define set_entity_compiler_generated(ent, flag) _set_entity_compiler_generated(ent, flag)
497 #define is_entity_backend_marked(ent)            _is_entity_backend_marked(ent)
498 #define set_entity_backend_marked(ent, flag)     _set_entity_backend_marked(ent, flag)
499 #define get_entity_address_taken(ent)            _get_entity_address_taken(ent)
500 #define set_entity_address_taken(ent, flag)      _set_entity_address_taken(ent, flag)
501 #define get_entity_offset(ent)                   _get_entity_offset(ent)
502 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
503 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
504 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
505 #define get_entity_link(ent)                     _get_entity_link(ent)
506 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
507 #define get_entity_irg(ent)                      _get_entity_irg(ent)
508 #define get_entity_visited(ent)                  _get_entity_visited(ent)
509 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
510 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
511 #define entity_visited(ent)                      _entity_visited(ent)
512 #define entity_not_visited(ent)                  _entity_not_visited(ent)
513 #define get_entity_repr_class(ent)               _get_entity_repr_class(ent)
514 #define get_entity_dbg_info(ent)                 _get_entity_dbg_info(ent)
515 #define set_entity_dbg_info(ent, db)             _set_entity_dbg_info(ent, db)
516
517
518 #endif /* FIRM_TR_ENTITY_T_H */