3 * File name: ir/tr/entity_t.h
4 * Purpose: Representation of all program known entities -- private header.
5 * Author: Martin Trapp, Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * entity.h: entities represent all program known objects.
18 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier
20 * An entity is the representation of program known objects in Firm.
21 * The primary concept of entities is to represent members of complex
22 * types, i.e., fields and methods of classes. As not all programming
23 * language model all variables and methods as members of some class,
24 * the concept of entities is extended to cover also local and global
25 * variables, and arbitrary procedures.
27 * An entity always specifies the type of the object it represents and
28 * the type of the object it is a part of, the owner of the entity.
29 * Originally this is the type of the class of which the entity is a
31 * The owner of local variables is the procedure they are defined in.
32 * The owner of global variables and procedures visible in the whole
33 * program is a universally defined class type "GlobalType". The owner
34 * of procedures defined in the scope of an other procedure is the
35 * enclosing procedure.
38 #ifndef _FIRM_TR_ENTITY_T_H_
39 #define _FIRM_TR_ENTITY_T_H_
41 #include "firm_common_t.h"
42 #include "firm_config.h"
48 #include "pseudo_irg.h"
50 /** A path in a compound graph. */
51 struct compound_graph_path {
52 firm_kind kind; /**< The dynamic type tag for compound graph path. */
53 ir_type *tp; /**< The type this path belongs to. */
54 int len; /**< The length of the path. */
56 int index; /**< Array index. To compute position of array elements */
57 ir_entity *node; /**< The accessed entity. */
58 } list[1]; /**< List of entity/index tuple of length len to express the
62 /** The attributes for atomic entities. */
63 typedef struct atomic_ent_attr {
64 ir_node *value; /**< value if entity is not of variability uninitialized.
65 Only for atomic entities. */
68 /** The attributes for compound entities. */
69 typedef struct compound_ent_attr {
70 ir_node **values; /**< constant values of compound entities. Only available if
71 variability not uninitialized. Must be set for variability constant. */
72 compound_graph_path **val_paths; /**< paths corresponding to constant values. Only available if
73 variability not uninitialized. Must be set for variability constant. */
76 /** A reserved value for "not yet set". */
77 #define VTABLE_NUM_NOT_SET ((unsigned)(-1))
79 /** The attributes for methods. */
80 typedef struct method_ent_attr {
81 ir_graph *irg; /**< The corresponding irg if known.
82 The ir_graph constructor automatically sets this field. */
83 unsigned irg_add_properties; /**< Additional graph properties can be
84 stored in a entity if no irg is available. */
86 unsigned vtable_number; /**< For a dynamically called method, the number assigned
87 in the virtual function table. */
89 ptr_access_kind *param_access; /**< the parameter access */
90 float *param_weight; /**< The weight of method's parameters. Parameters
91 with a high weight are good for procedure cloning. */
92 ir_img_section section; /**< The code section where this method should be placed */
96 /** The type of an entity. */
98 firm_kind kind; /**< The dynamic type tag for entity. */
99 ident *name; /**< The name of this entity. */
100 ident *ld_name; /**< Unique name of this entity, i.e., the mangled
101 name. If the field is read before written a default
102 mangling is applies. The name of the owner is prepended
103 to the name of the entity, separated by a underscore.
104 E.g., for a class `A' with field `a' this
105 is the ident for `A_a'. */
106 ir_type *type; /**< The type of this entity, e.g., a method type, a
107 basic type of the language or a class itself. */
108 ir_type *owner; /**< The compound type (e.g. class type) this entity belongs to. */
109 ir_allocation allocation:3; /**< Distinguishes static and dynamically allocated
110 entities and some further cases. */
111 ir_visibility visibility:3; /**< Specifies visibility to external program
113 ir_variability variability:3; /**< Specifies variability of entities content. */
114 ir_volatility volatility:2; /**< Specifies volatility of entities content. */
115 ir_stickyness stickyness:2; /**< Specifies whether this entity is sticky. */
116 ir_peculiarity peculiarity:3; /**< The peculiarity of this entity. */
117 unsigned final:1; /**< If set, this entity cannot be overridden. */
118 unsigned compiler_gen:1; /**< If set, this entity was compiler generated. */
119 int offset; /**< Offset in bits for this entity. Fixed when layout
120 of owner is determined. */
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. */
126 /* ------------- fields for entities owned by a class type ---------------*/
128 ir_entity **overwrites; /**< A list of entities this entity overwrites. */
129 ir_entity **overwrittenby; /**< A list of entities that overwrite this entity. */
131 /* ------------- fields for atomic entities --------------- */
132 ir_node *value; /**< value if entity is not of variability uninitialized.
133 Only for atomic entities. */
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 */
141 /* ------------- fields for analyses ---------------*/
144 long nr; /**< A unique node number for each node to make output
146 # endif /* DEBUG_libfirm */
149 /** Initialize the entity module. */
150 void firm_init_entity(void);
153 /* ----------------------- inline functions ------------------------ */
155 _is_entity(const void *thing) {
156 return get_kind(thing) == k_entity;
159 static INLINE const char *
160 _get_entity_name(const ir_entity *ent) {
161 assert(ent && ent->kind == k_entity);
162 return get_id_str(get_entity_ident(ent));
165 static INLINE ident *
166 _get_entity_ident(const ir_entity *ent) {
167 assert(ent && ent->kind == k_entity);
172 _set_entity_ident(ir_entity *ent, ident *id) {
173 assert(ent && ent->kind == k_entity);
177 static INLINE ir_type *
178 _get_entity_owner(ir_entity *ent) {
179 assert(ent && ent->kind == k_entity);
180 return ent->owner = skip_tid(ent->owner);
183 static INLINE ident *
184 _get_entity_ld_ident(ir_entity *ent)
186 assert(ent && ent->kind == k_entity);
187 if (ent->ld_name == NULL)
188 ent->ld_name = mangle_entity(ent);
193 _set_entity_ld_ident(ir_entity *ent, ident *ld_ident) {
194 assert(ent && ent->kind == k_entity);
195 ent->ld_name = ld_ident;
198 static INLINE const char *
199 _get_entity_ld_name(ir_entity *ent) {
200 assert(ent && ent->kind == k_entity);
201 return get_id_str(get_entity_ld_ident(ent));
204 static INLINE ir_type *
205 _get_entity_type(ir_entity *ent) {
206 assert(ent && ent->kind == k_entity);
207 return ent->type = skip_tid(ent->type);
211 _set_entity_type(ir_entity *ent, ir_type *type) {
212 assert(ent && ent->kind == k_entity);
216 static INLINE ir_allocation
217 _get_entity_allocation(const ir_entity *ent) {
218 assert(ent && ent->kind == k_entity);
219 return ent->allocation;
223 _set_entity_allocation(ir_entity *ent, ir_allocation al) {
224 assert(ent && ent->kind == k_entity);
225 ent->allocation = al;
228 static INLINE ir_visibility
229 _get_entity_visibility(const ir_entity *ent) {
230 assert(ent && ent->kind == k_entity);
231 return ent->visibility;
234 static INLINE ir_variability
235 _get_entity_variability(const ir_entity *ent) {
236 assert(ent && ent->kind == k_entity);
237 return ent->variability;
240 static INLINE ir_volatility
241 _get_entity_volatility(const ir_entity *ent) {
242 assert(ent && ent->kind == k_entity);
243 return ent->volatility;
247 _set_entity_volatility(ir_entity *ent, ir_volatility vol) {
248 assert(ent && ent->kind == k_entity);
249 ent->volatility = vol;
252 static INLINE ir_peculiarity
253 _get_entity_peculiarity(const ir_entity *ent) {
254 assert(ent && ent->kind == k_entity);
255 return ent->peculiarity;
259 * @todo Why peculiarity only for methods?
260 * Good question. Originally, there were only description and
261 * existent. The thought was, what sense does it make to
262 * describe a field? With inherited the situation changed. So
263 * I removed the assertion. GL, 28.2.05
266 _set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec) {
267 assert(ent && ent->kind == k_entity);
268 /* @@@ why peculiarity only for methods? */
269 //assert(is_Method_type(ent->type));
271 ent->peculiarity = pec;
274 static INLINE ir_stickyness
275 _get_entity_stickyness(const ir_entity *ent) {
276 assert(ent && ent->kind == k_entity);
277 return ent->stickyness;
281 _set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness) {
282 assert(ent && ent->kind == k_entity);
283 ent->stickyness = stickyness;
287 _get_entity_final(const ir_entity *ent) {
288 assert(ent && ent->kind == k_entity);
289 return (int)ent->final;
293 _set_entity_final(ir_entity *ent, int final) {
294 assert(ent && ent->kind == k_entity);
295 ent->final = final ? 1 : 0;
299 _get_entity_offset_bits(const ir_entity *ent) {
300 assert(ent && ent->kind == k_entity);
305 _get_entity_offset_bytes(const ir_entity *ent) {
306 int bits = _get_entity_offset_bits(ent);
308 if (bits & 7) return -1;
313 _set_entity_offset_bits(ir_entity *ent, int offset) {
314 assert(ent && ent->kind == k_entity);
315 ent->offset = offset;
319 _set_entity_offset_bytes(ir_entity *ent, int offset) {
320 _set_entity_offset_bits(ent, offset * 8);
324 _get_entity_link(const ir_entity *ent) {
325 assert(ent && ent->kind == k_entity);
330 _set_entity_link(ir_entity *ent, void *l) {
331 assert(ent && ent->kind == k_entity);
335 static INLINE ir_graph *
336 _get_entity_irg(const ir_entity *ent) {
337 assert(ent && ent->kind == k_entity);
338 assert(ent == unknown_entity || is_Method_type(ent->type));
339 if (!get_visit_pseudo_irgs() && ent->attr.mtd_attr.irg
340 && is_pseudo_ir_graph(ent->attr.mtd_attr.irg))
342 return ent->attr.mtd_attr.irg;
345 static INLINE unsigned long
346 _get_entity_visited(ir_entity *ent) {
347 assert(ent && ent->kind == k_entity);
352 _set_entity_visited(ir_entity *ent, unsigned long num) {
353 assert(ent && ent->kind == k_entity);
358 _mark_entity_visited(ir_entity *ent) {
359 assert(ent && ent->kind == k_entity);
360 ent->visit = firm_type_visited;
364 _entity_visited(ir_entity *ent) {
365 return _get_entity_visited(ent) >= firm_type_visited;
369 _entity_not_visited(ir_entity *ent) {
370 return _get_entity_visited(ent) < firm_type_visited;
373 static INLINE ir_type *
374 _get_entity_repr_class(const ir_entity *ent) {
375 assert(ent && ent->kind == k_entity);
376 return ent->repr_class;
379 #define is_entity(thing) _is_entity(thing)
380 #define get_entity_name(ent) _get_entity_name(ent)
381 #define get_entity_ident(ent) _get_entity_ident(ent)
382 #define set_entity_ident(ent, id) _set_entity_ident(ent, id)
383 #define get_entity_owner(ent) _get_entity_owner(ent)
384 #define get_entity_ld_ident(ent) _get_entity_ld_ident(ent)
385 #define set_entity_ld_ident(ent, ld_ident) _set_entity_ld_ident(ent, ld_ident)
386 #define get_entity_ld_name(ent) _get_entity_ld_name(ent)
387 #define get_entity_type(ent) _get_entity_type(ent)
388 #define set_entity_type(ent, type) _set_entity_type(ent, type)
389 #define get_entity_allocation(ent) _get_entity_allocation(ent)
390 #define set_entity_allocation(ent, al) _set_entity_allocation(ent, al)
391 #define get_entity_visibility(ent) _get_entity_visibility(ent)
392 #define get_entity_variability(ent) _get_entity_variability(ent)
393 #define get_entity_volatility(ent) _get_entity_volatility(ent)
394 #define set_entity_volatility(ent, vol) _set_entity_volatility(ent, vol)
395 #define get_entity_peculiarity(ent) _get_entity_peculiarity(ent)
396 #define set_entity_peculiarity(ent, pec) _set_entity_peculiarity(ent, pec)
397 #define get_entity_stickyness(ent) _get_entity_stickyness(ent)
398 #define set_entity_stickyness(ent, stickyness) _set_entity_stickyness(ent, stickyness)
399 #define get_entity_final(ent) _get_entity_final(ent)
400 #define set_entity_final(ent, final) _set_entity_final(ent, final)
401 #define get_entity_offset_bits(ent) _get_entity_offset_bits(ent)
402 #define get_entity_offset_bytes(ent) _get_entity_offset_bytes(ent)
403 #define set_entity_offset_bits(ent, offset) _set_entity_offset_bits(ent, offset)
404 #define set_entity_offset_bytes(ent, offset) _set_entity_offset_bytes(ent, offset)
405 #define get_entity_link(ent) _get_entity_link(ent)
406 #define set_entity_link(ent, l) _set_entity_link(ent, l)
407 #define get_entity_irg(ent) _get_entity_irg(ent)
408 #define get_entity_visited(ent) _get_entity_visited(ent)
409 #define set_entity_visited(ent, num) _set_entity_visited(ent, num)
410 #define mark_entity_visited(ent) _mark_entity_visited(ent)
411 #define entity_visited(ent) _entity_visited(ent)
412 #define entity_not_visited(ent) _entity_not_visited(ent)
413 #define get_entity_repr_class(ent) _get_entity_repr_class(ent)
416 #endif /* _FIRM_TR_ENTITY_T_H_ */