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