62f604f6d99d15a9568e9f362ba948bc92053379
[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 compound entities. */
75 typedef struct compound_ent_attr {
76         ir_node **values;     /**< constant values of compound entities. */
77         compound_graph_path **val_paths;
78                              /**< paths corresponding to constant values. */
79 } compound_ent_attr;
80
81 /** The attributes for methods. */
82 typedef struct method_ent_attr {
83         ir_graph *irg;                 /**< The corresponding irg if known.
84                                             The ir_graph constructor automatically sets this field. */
85         unsigned irg_add_properties;   /**< Additional graph properties can be
86                                             stored in a entity if no irg is available. */
87
88         unsigned vtable_number;        /**< For a dynamically called method, the number assigned
89                                             in the virtual function table. */
90
91         ptr_access_kind *param_access; /**< the parameter access */
92         unsigned *param_weight;        /**< The weight of method's parameters. Parameters
93                                             with a high weight are good candidates for procedure cloning. */
94 } method_ent_attr;
95
96 /** additional attributes for code entities */
97 typedef struct code_ent_attr {
98         ir_label_t  label;       /** label of the basic block */
99 } code_ent_attr;
100
101
102 /**
103  * An abstract data type to represent program entities.
104  *
105  * @see  ir_type
106  */
107 struct ir_entity {
108         firm_kind kind;       /**< The dynamic type tag for entity. */
109         ident *name;          /**< The name of this entity. */
110         ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
111                                    name.  If the field is read before written a default
112                                    mangling is applies.  The name of the owner is prepended
113                                    to the name of the entity, separated by a underscore.
114                                    E.g.,  for a class `A' with field `a' this
115                                    is the ident for `A_a'. */
116         ir_type *type;        /**< The type of this entity, e.g., a method type, a
117                                    basic type of the language or a class itself. */
118         ir_type *owner;       /**< The compound type (e.g. class type) this entity belongs to. */
119         unsigned linkage:10;        /**< Specifies linkage type */
120         unsigned volatility:1;      /**< Specifies volatility of entities content.*/
121         unsigned aligned:1;         /**< Specifies alignment of entities content. */
122         unsigned usage:4;           /**< flag indicating usage types of this entity,
123                                          see ir_entity_usage. */
124         unsigned compiler_gen:1;    /**< If set, this entity was compiler generated.
125                                      */
126         unsigned visibility:3;      /**< @deprecated */
127         unsigned allocation:3;      /**< @deprecated */
128         unsigned peculiarity:3;     /**< @deprecated */
129         unsigned final:1;           /**< @deprecated */
130         int offset;                 /**< Offset in bytes for this entity. Fixed
131                                          when layout of owner is determined. */
132         unsigned alignment;         /**< entity alignment in bytes */
133         unsigned char offset_bit_remainder;
134                                     /**< If the entity is a bit field, this is the
135                                          offset of the start of the bit field
136                                          within the byte specified by offset. */
137         ir_visited_t visit;         /**< visited counter for walks of the type
138                                          information. */
139         struct dbg_info *dbi;       /**< A pointer to information for debug support.
140                                      */
141         void *link;                 /**< To store some intermediate information. */
142         ir_type *repr_class;        /**< If this entity represents a class info, the
143                                          associated class. */
144
145         /* ------------- fields for entities owned by a class type ---------------*/
146
147         ir_entity **overwrites;     /**< A list of entities this entity overwrites.
148                                      */
149         ir_entity **overwrittenby;  /**< A list of entities that overwrite this
150                                          entity. */
151
152         /* ------------- fields for atomic entities  --------------- */
153         ir_initializer_t *initializer; /**< entity initializer */
154         union {
155                 /* ------------- fields for compound entities -------------- */
156                 compound_ent_attr cmpd_attr;
157                 /* ------------- fields for method entities ---------------- */
158                 method_ent_attr   mtd_attr;
159                 /* fields for code entities */
160                 code_ent_attr     code_attr;
161         } attr; /**< type specific attributes */
162
163         /* ------------- fields for analyses ---------------*/
164
165 #ifdef DEBUG_libfirm
166         long nr;             /**< A unique node number for each node to make output
167                                   readable. */
168 #endif
169 };
170
171 /** Initialize the entity module. */
172 void ir_init_entity(void);
173 /** Cleanup entity module */
174 void ir_finish_entity(void);
175
176 /* ----------------------- inline functions ------------------------ */
177 static inline int _is_entity(const void *thing)
178 {
179         return get_kind(thing) == k_entity;
180 }
181
182 static inline const char *_get_entity_name(const ir_entity *ent)
183 {
184         assert(ent && ent->kind == k_entity);
185         return get_id_str(get_entity_ident(ent));
186 }
187
188 static inline ident *_get_entity_ident(const ir_entity *ent)
189 {
190         assert(ent && ent->kind == k_entity);
191         return ent->name;
192 }
193
194 static inline void _set_entity_ident(ir_entity *ent, ident *id)
195 {
196         assert(ent && ent->kind == k_entity);
197         ent->name = id;
198 }
199
200 static inline ir_type *_get_entity_owner(const ir_entity *ent)
201 {
202         assert(ent && ent->kind == k_entity);
203         return ent->owner;
204 }
205
206 static inline ident *_get_entity_ld_ident(const ir_entity *ent)
207 {
208         assert(ent && ent->kind == k_entity);
209         if (ent->ld_name == NULL)
210                 return ent->name;
211         return ent->ld_name;
212 }
213
214 static inline void _set_entity_ld_ident(ir_entity *ent, ident *ld_ident)
215 {
216         assert(ent && ent->kind == k_entity);
217         ent->ld_name = ld_ident;
218 }
219
220 static inline const char *_get_entity_ld_name(const ir_entity *ent)
221 {
222         assert(ent && ent->kind == k_entity);
223         return get_id_str(get_entity_ld_ident(ent));
224 }
225
226 static inline ir_type *_get_entity_type(const ir_entity *ent)
227 {
228         assert(ent && ent->kind == k_entity);
229         return ent->type;
230 }
231
232 static inline void _set_entity_type(ir_entity *ent, ir_type *type)
233 {
234         assert(ent && ent->kind == k_entity);
235         ent->type = type;
236 }
237
238 static inline ir_linkage _get_entity_linkage(const ir_entity *ent)
239 {
240         assert(ent && ent->kind == k_entity);
241         return ent->linkage;
242 }
243
244 static inline ir_volatility _get_entity_volatility(const ir_entity *ent)
245 {
246         assert(ent && ent->kind == k_entity);
247         return ent->volatility;
248 }
249
250 static inline void _set_entity_volatility(ir_entity *ent, ir_volatility vol)
251 {
252         assert(ent && ent->kind == k_entity);
253         ent->volatility = vol;
254 }
255
256 static inline unsigned _get_entity_alignment(const ir_entity *ent)
257 {
258         assert(ent && ent->kind == k_entity);
259         return ent->alignment;
260 }
261
262 static inline void _set_entity_alignment(ir_entity *ent, unsigned alignment)
263 {
264         assert(ent && ent->kind == k_entity);
265         ent->alignment = alignment;
266 }
267
268 static inline ir_align _get_entity_aligned(const ir_entity *ent)
269 {
270         assert(ent && ent->kind == k_entity);
271         return ent->aligned;
272 }
273
274 static inline void _set_entity_aligned(ir_entity *ent, ir_align a)
275 {
276         assert(ent && ent->kind == k_entity);
277         ent->aligned = a;
278 }
279
280 static inline int _is_entity_compiler_generated(const ir_entity *ent)
281 {
282         assert(ent && ent->kind == k_entity);
283         return ent->compiler_gen;
284 }
285
286 static inline void _set_entity_compiler_generated(ir_entity *ent, int flag)
287 {
288         assert(ent && ent->kind == k_entity);
289         ent->compiler_gen = flag ? 1 : 0;
290 }
291
292 static inline ir_entity_usage _get_entity_usage(const ir_entity *ent)
293 {
294         assert(ent && ent->kind == k_entity);
295         return ent->usage;
296 }
297
298 static inline void _set_entity_usage(ir_entity *ent, ir_entity_usage state)
299 {
300         assert(ent && ent->kind == k_entity);
301         ent->usage = state;
302 }
303
304 static inline int _get_entity_offset(const ir_entity *ent)
305 {
306         assert(ent && ent->kind == k_entity);
307         return ent->offset;
308 }
309
310 static inline void _set_entity_offset(ir_entity *ent, int offset)
311 {
312         assert(ent && ent->kind == k_entity);
313         ent->offset = offset;
314 }
315
316 static inline unsigned char _get_entity_offset_bits_remainder(const ir_entity *ent)
317 {
318         assert(ent && ent->kind == k_entity);
319         return ent->offset_bit_remainder;
320 }
321
322 static inline void _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset)
323 {
324         assert(ent && ent->kind == k_entity);
325         ent->offset_bit_remainder = offset;
326 }
327
328 static inline void *_get_entity_link(const ir_entity *ent)
329 {
330         assert(ent && ent->kind == k_entity);
331         return ent->link;
332 }
333
334 static inline void _set_entity_link(ir_entity *ent, void *l)
335 {
336         assert(ent && ent->kind == k_entity);
337         ent->link = l;
338 }
339
340 static inline ir_graph *_get_entity_irg(const ir_entity *ent)
341 {
342         ir_graph *irg;
343         assert(ent && ent->kind == k_entity);
344         if (!is_Method_type(ent->type) || ent == unknown_entity) {
345                 return NULL;
346         }
347
348         irg = ent->attr.mtd_attr.irg;
349         if (irg != NULL && !get_visit_pseudo_irgs()     && is_pseudo_ir_graph(irg))
350                 return NULL;
351         return irg;
352 }
353
354 static inline ir_visited_t _get_entity_visited(const ir_entity *ent)
355 {
356         assert(ent && ent->kind == k_entity);
357         return ent->visit;
358 }
359
360 static inline void _set_entity_visited(ir_entity *ent, ir_visited_t num)
361 {
362         assert(ent && ent->kind == k_entity);
363         ent->visit = num;
364 }
365
366 static inline void _mark_entity_visited(ir_entity *ent)
367 {
368         assert(ent && ent->kind == k_entity);
369         ent->visit = firm_type_visited;
370 }
371
372 static inline int _entity_visited(const ir_entity *ent)
373 {
374         return _get_entity_visited(ent) >= firm_type_visited;
375 }
376
377 static inline int _entity_not_visited(const ir_entity *ent)
378 {
379         return _get_entity_visited(ent) < firm_type_visited;
380 }
381
382 static inline ir_type *_get_entity_repr_class(const ir_entity *ent)
383 {
384         assert(ent && ent->kind == k_entity);
385         return ent->repr_class;
386 }
387
388 static inline dbg_info *_get_entity_dbg_info(const ir_entity *ent)
389 {
390         return ent->dbi;
391 }
392
393 static inline void _set_entity_dbg_info(ir_entity *ent, dbg_info *db)
394 {
395         ent->dbi = db;
396 }
397
398 #define is_entity(thing)                         _is_entity(thing)
399 #define get_entity_name(ent)                     _get_entity_name(ent)
400 #define get_entity_ident(ent)                    _get_entity_ident(ent)
401 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
402 #define get_entity_owner(ent)                    _get_entity_owner(ent)
403 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
404 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
405 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
406 #define get_entity_type(ent)                     _get_entity_type(ent)
407 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
408 #define get_entity_linkage(ent)                  _get_entity_linkage(ent)
409 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
410 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
411 #define set_entity_alignment(ent, alignment)     _set_entity_alignment(ent, alignment)
412 #define get_entity_alignment(ent)                _get_entity_alignment(ent)
413 #define get_entity_align(ent)                    _get_entity_align(ent)
414 #define set_entity_align(ent, a)                 _set_entity_align(ent, a)
415 #define is_entity_compiler_generated(ent)        _is_entity_compiler_generated(ent)
416 #define set_entity_compiler_generated(ent, flag) _set_entity_compiler_generated(ent, flag)
417 #define get_entity_usage(ent)                    _get_entity_usage(ent)
418 #define set_entity_usage(ent, flags)             _set_entity_usage(ent, flags)
419 #define get_entity_offset(ent)                   _get_entity_offset(ent)
420 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
421 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
422 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
423 #define get_entity_link(ent)                     _get_entity_link(ent)
424 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
425 #define get_entity_irg(ent)                      _get_entity_irg(ent)
426 #define get_entity_visited(ent)                  _get_entity_visited(ent)
427 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
428 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
429 #define entity_visited(ent)                      _entity_visited(ent)
430 #define entity_not_visited(ent)                  _entity_not_visited(ent)
431 #define get_entity_repr_class(ent)               _get_entity_repr_class(ent)
432 #define get_entity_dbg_info(ent)                 _get_entity_dbg_info(ent)
433 #define set_entity_dbg_info(ent, db)             _set_entity_dbg_info(ent, db)
434
435 #endif