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