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