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