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