added additional graph properties, these can be set in the entity
[libfirm] / ir / tr / entity_t.h
1 /*
2  * Project:     libFIRM
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
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file entity_t.h
15  *
16  * entity.h:  entities represent all program known objects.
17  *
18  * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier
19  *
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.
26  *
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
30  *  member.
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.
36  */
37
38 # ifndef _ENTITY_T_H_
39 # define _ENTITY_T_H_
40
41 #include "firm_common_t.h"
42 #include "firm_config.h"
43
44 #include "type_t.h"
45 #include "entity.h"
46 #include "typegmod.h"
47 #include "mangle.h"
48 #include "pseudo_irg.h"
49
50 /** A path in a compund graph. */
51 struct compound_graph_path {
52   firm_kind kind;       /**< dynamic type tag for compound graph path. */
53   type *tp;             /**< The type this path belongs to. */
54   int len;              /**< length of the path */
55   struct tuple {
56     int    index;       /**< Array index.  To compute position of array elements */
57     entity *node;       /**< entity */
58   } list[1];            /**< List of entitiy/index tuple of length len to express the
59                              access path. */
60 };
61
62 /** the type of an entity */
63 struct entity {
64   firm_kind kind;       /**< dynamic type tag for entity. */
65   ident *name;          /**< name of this entity */
66   ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
67                            name.  If the field is read before written a default
68                            mangling is applies.  The name of the owner is prepended
69                            to the name of the entity, separated by a underscore.
70                            E.g.,  for a class `A' with field `a' this
71                            is the ident for `A_a'. */
72   type *type;           /**< The type of this entity, e.g., a method type, a
73                            basic type of the language or a class itself */
74   type *owner;          /**< The compound type (e.g. class type) this entity belongs to. */
75   ent_allocation allocation;  /**< Distinguishes static and dynamically allocated
76                  entities and some further cases. */
77   visibility visibility;  /**< Specifies visibility to external program
78                  fragments */
79   ent_variability variability;  /**< Specifies variability of entities content */
80   ent_volatility volatility;    /**< Specifies volatility of entities content */
81   ent_stickyness stickyness;    /**< Specifies whether this entity is sticky  */
82   int  offset;          /**< Offset in bits for this entity.  Fixed when layout
83                of owner is determined.  */
84   void *link;           /**< To store some intermediate information */
85   unsigned long visit;  /**< visited counter for walks of the type information */
86   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
87
88   /* ------------- fields for atomic entities  ---------------*/
89
90   ir_node *value;            /**< value if entity is not of variability uninitialized.
91                                Only for atomic entities. */
92
93   /* ------------- fields for compound entities ---------------*/
94
95   ir_node **values;     /**< constant values of compound entities. Only available if
96                variability not uninitialized.  Must be set for variability constant
97                            */
98   compound_graph_path **val_paths; /**< paths corresponding to constant values. Only available if
99                       variability not uninitialized.  Must be set for variability constant */
100
101   /* ------------- fields for entities owned by a class type ---------------*/
102
103   entity **overwrites;     /**< A list of entities this entity overwrites. */
104   entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
105
106   /* ------------- fields for methods ---------------*/
107
108   enum peculiarity peculiarity;
109   ir_graph *irg;        /**< If (type == method_type) this is the corresponding irg.
110                The ir_graph constructor automatically sets this field.
111                Yes, it must be here. */
112   unsigned irg_add_properties;  /**< If (type == method_type) this is a set of additional.
113                graph flags if the irg of an entity is not known. */
114
115   /* ------------- fields for analyses ---------------*/
116
117
118 #ifdef DEBUG_libfirm
119   int nr;             /**< a unique node number for each node to make output
120                            readable. */
121 # endif /* DEBUG_libfirm */
122 };
123
124
125
126 /* ----------------------- inline functions ------------------------ */
127 static INLINE int
128 _is_entity(const void *thing) {
129   return get_kind(thing) == k_entity;
130 }
131
132 static INLINE const char *
133 _get_entity_name(const entity *ent) {
134   assert(ent && ent->kind == k_entity);
135   return get_id_str(get_entity_ident(ent));
136 }
137
138 static INLINE ident *
139 _get_entity_ident(const entity *ent) {
140   assert(ent && ent->kind == k_entity);
141   return ent->name;
142 }
143
144 static INLINE type *
145 _get_entity_owner(entity *ent) {
146   assert(ent && ent->kind == k_entity);
147   return ent->owner = skip_tid(ent->owner);
148 }
149
150 static INLINE ident *
151 _get_entity_ld_ident(entity *ent)
152 {
153   assert(ent && ent->kind == k_entity);
154   if (ent->ld_name == NULL)
155     ent->ld_name = mangle_entity(ent);
156   return ent->ld_name;
157 }
158
159 static INLINE void
160 _set_entity_ld_ident(entity *ent, ident *ld_ident) {
161   assert(ent && ent->kind == k_entity);
162   ent->ld_name = ld_ident;
163 }
164
165 static INLINE const char *
166 _get_entity_ld_name(entity *ent) {
167   assert(ent && ent->kind == k_entity);
168   return get_id_str(get_entity_ld_ident(ent));
169 }
170
171 static INLINE type *
172 _get_entity_type(entity *ent) {
173   assert(ent && ent->kind == k_entity);
174   return ent->type = skip_tid(ent->type);
175 }
176
177 static INLINE void
178 _set_entity_type(entity *ent, type *type) {
179   assert(ent && ent->kind == k_entity);
180   ent->type = type;
181 }
182
183 static INLINE ent_allocation
184 _get_entity_allocation(const entity *ent) {
185   assert(ent && ent->kind == k_entity);
186   return ent->allocation;
187 }
188
189 static INLINE void
190 _set_entity_allocation(entity *ent, ent_allocation al) {
191   assert(ent && ent->kind == k_entity);
192   ent->allocation = al;
193 }
194
195 static INLINE visibility
196 _get_entity_visibility(const entity *ent) {
197   assert(ent && ent->kind == k_entity);
198   return ent->visibility;
199 }
200
201 static INLINE ent_variability
202 _get_entity_variability(const entity *ent) {
203   assert(ent && ent->kind == k_entity);
204   return ent->variability;
205 }
206
207 static INLINE ent_volatility
208 _get_entity_volatility(const entity *ent) {
209   assert(ent && ent->kind == k_entity);
210   return ent->volatility;
211 }
212
213 static INLINE void
214 _set_entity_volatility(entity *ent, ent_volatility vol) {
215   assert(ent && ent->kind == k_entity);
216   ent->volatility = vol;
217 }
218
219 static INLINE peculiarity
220 _get_entity_peculiarity(const entity *ent) {
221   assert(ent && ent->kind == k_entity);
222   return ent->peculiarity;
223 }
224
225 /**
226  * @todo Why peculiarity only for methods?
227  *       Good question.  Originally, there were only description and
228  *       existent.  The thought was, what sense does it make to
229  *       describe a field?  With inherited the situation changed.  So
230  *       I removed the assertion.  GL, 28.2.05
231  */
232 static INLINE void
233 _set_entity_peculiarity(entity *ent, peculiarity pec) {
234   assert(ent && ent->kind == k_entity);
235   /* @@@ why peculiarity only for methods? */
236   //assert(is_Method_type(ent->type));
237
238   ent->peculiarity = pec;
239 }
240
241 static INLINE ent_stickyness
242 _get_entity_stickyness(const entity *ent) {
243   assert(ent && ent->kind == k_entity);
244   return ent->stickyness;
245 }
246
247 static INLINE void
248 _set_entity_stickyness(entity *ent, ent_stickyness stickyness)
249 {
250   assert(ent && ent->kind == k_entity);
251   ent->stickyness = stickyness;
252 }
253
254 static INLINE int
255 _get_entity_offset_bits(const entity *ent) {
256   assert(ent && ent->kind == k_entity);
257   return ent->offset;
258 }
259
260 static INLINE int
261 _get_entity_offset_bytes(const entity *ent) {
262   int bits = _get_entity_offset_bits(ent);
263
264   if (bits & 7) return -1;
265   return bits >> 3;
266 }
267
268 static INLINE void
269 _set_entity_offset_bits(entity *ent, int offset) {
270   assert(ent && ent->kind == k_entity);
271   ent->offset = offset;
272 }
273
274 static INLINE void
275 _set_entity_offset_bytes(entity *ent, int offset) {
276   _set_entity_offset_bits(ent, offset * 8);
277 }
278
279 static INLINE void *
280 _get_entity_link(const entity *ent) {
281   assert(ent && ent->kind == k_entity);
282   return ent->link;
283 }
284
285 static INLINE void
286 _set_entity_link(entity *ent, void *l) {
287   assert(ent && ent->kind == k_entity);
288   ent->link = l;
289 }
290
291 static INLINE ir_graph *
292 _get_entity_irg(const entity *ent) {
293   assert(ent && ent->kind == k_entity);
294   assert(ent == unknown_entity || is_Method_type(ent->type));
295   if (!get_visit_pseudo_irgs() && ent->irg && is_pseudo_ir_graph(ent->irg))
296     return NULL;
297   return ent->irg;
298 }
299
300 static INLINE unsigned long
301 _get_entity_visited(entity *ent) {
302   assert(ent && ent->kind == k_entity);
303   return ent->visit;
304 }
305
306 static INLINE void
307 _set_entity_visited(entity *ent, unsigned long num) {
308   assert(ent && ent->kind == k_entity);
309   ent->visit = num;
310 }
311
312 static INLINE void
313 _mark_entity_visited(entity *ent) {
314   assert(ent && ent->kind == k_entity);
315   ent->visit = firm_type_visited;
316 }
317
318 static INLINE int
319 _entity_visited(entity *ent) {
320   return _get_entity_visited(ent) >= firm_type_visited;
321 }
322
323 static INLINE int
324 _entity_not_visited(entity *ent) {
325   return _get_entity_visited(ent) < firm_type_visited;
326 }
327
328 static INLINE unsigned
329 _get_entity_additional_properties(const entity *ent) {
330   ir_graph *irg;
331   assert(ent && ent->kind == k_entity);
332   assert(ent == unknown_entity || is_Method_type(ent->type));
333   irg = _get_entity_irg(ent);
334   return irg ?
335     get_irg_additional_properties(irg) :
336     ent->irg_add_properties;
337 }
338
339 static INLINE void
340 _set_entity_additional_properties(entity *ent, unsigned mask) {
341   ir_graph *irg;
342   assert(ent && ent->kind == k_entity);
343   assert(ent == unknown_entity || is_Method_type(ent->type));
344   irg = _get_entity_irg(ent);
345   if (irg)
346     set_irg_additional_properties(irg, mask);
347   else
348     ent->irg_add_properties = mask;
349 }
350
351 static INLINE void
352 _set_entity_additional_property(entity *ent, irg_additional_property flag) {
353   ir_graph *irg;
354   assert(ent && ent->kind == k_entity);
355   assert(ent == unknown_entity || is_Method_type(ent->type));
356   irg = _get_entity_irg(ent);
357   if (irg)
358     set_irg_additional_property(irg, flag);
359   else
360     ent->irg_add_properties |= flag;
361 }
362
363 #define is_entity(thing)                         _is_entity(thing)
364 #define get_entity_name(ent)                     _get_entity_name(ent)
365 #define get_entity_ident(ent)                    _get_entity_ident(ent)
366 #define get_entity_owner(ent)                    _get_entity_owner(ent)
367 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
368 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
369 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
370 #define get_entity_type(ent)                     _get_entity_type(ent)
371 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
372 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
373 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
374 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
375 #define get_entity_variability(ent)              _get_entity_variability(ent)
376 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
377 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
378 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
379 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
380 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
381 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
382 #define get_entity_offset_bits(ent)              _get_entity_offset_bits(ent)
383 #define get_entity_offset_bytes(ent)             _get_entity_offset_bytes(ent)
384 #define set_entity_offset_bits(ent, offset)      _set_entity_offset_bits(ent, offset)
385 #define set_entity_offset_bytes(ent, offset)     _set_entity_offset_bytes(ent, offset)
386 #define get_entity_link(ent)                     _get_entity_link(ent)
387 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
388 #define get_entity_irg(ent)                      _get_entity_irg(ent)
389 #define get_entity_visited(ent)                  _get_entity_visited(ent)
390 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
391 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
392 #define entity_visited(ent)                      _entity_visited(ent)
393 #define entity_not_visited(ent)                  _entity_not_visited(ent)
394 #define get_entity_additional_properties(ent)    _get_entity_additional_properties(ent)
395 #define set_entity_additional_properties(ent, m) _set_entity_additional_properties(ent, m)
396 #define set_entity_additional_property(ent, f)   _set_entity_additional_property(ent, f)
397
398 # endif /* _ENTITY_T_H_ */