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