345277ee3c0dce4d71f97ebae0892ab62381e194
[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 _FIRM_TR_ENTITY_T_H_
39 #define _FIRM_TR_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 compound graph. */
51 struct compound_graph_path {
52   firm_kind kind;       /**< dynamic type tag for compound graph path. */
53   ir_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 entity/index tuple of length len to express the
59                              access path. */
60 };
61
62 /** 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. */
66 } atomic_ent_attr;
67
68 /** 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. */
74 } compound_ent_attr;
75
76 /** a reserved value for "not yet set" */
77 #define VTABLE_NUM_NOT_SET ((unsigned)(-1))
78
79 /** 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. */
85
86   unsigned vtable_number;        /**< For a dynamically called method, the number assigned
87                                       in the virtual function table. */
88
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 */
93 } method_ent_attr;
94
95
96 /** the type of an entity */
97 struct 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:2;    /**< Distinguishes static and dynamically allocated
110                                     entities and some further cases. */
111   ir_visibility visibility:2;    /**< Specifies visibility to external program
112                                       fragments. */
113   ir_variability variability:2;  /**< Specifies variability of entities content. */
114   ir_volatility volatility:1;    /**< Specifies volatility of entities content. */
115   ir_stickyness stickyness:1;    /**< Specifies whether this entity is sticky.  */
116   ir_peculiarity peculiarity:2;  /**< The peculiarity of this entity. */
117   int  offset;                   /**< Offset in bits for this entity.  Fixed when layout
118                                       of owner is determined. */
119   unsigned long visit;           /**< visited counter for walks of the type information. */
120   struct dbg_info *dbi;          /**< A pointer to information for debug support. */
121   void *link;                    /**< To store some intermediate information. */
122
123   /* ------------- fields for entities owned by a class type ---------------*/
124
125   entity **overwrites;     /**< A list of entities this entity overwrites. */
126   entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
127
128   /* ------------- fields for atomic entities  --------------- */
129   ir_node *value;          /**< value if entity is not of variability uninitialized.
130                                 Only for atomic entities. */
131   union {
132     /* ------------- fields for compound entities -------------- */
133     compound_ent_attr cmpd_attr;
134     /* ------------- fields for method entities ---------------- */
135     method_ent_attr   mtd_attr;
136   } attr; /**< type specific attributes */
137
138   /* ------------- fields for analyses ---------------*/
139
140 #ifdef DEBUG_libfirm
141   long nr;             /**< A unique node number for each node to make output
142                             readable. */
143 # endif /* DEBUG_libfirm */
144 };
145
146 /** Initialize entity module. */
147 void firm_init_entity(void);
148
149
150 /* ----------------------- inline functions ------------------------ */
151 static INLINE int
152 _is_entity(const void *thing) {
153   return get_kind(thing) == k_entity;
154 }
155
156 static INLINE const char *
157 _get_entity_name(const entity *ent) {
158   assert(ent && ent->kind == k_entity);
159   return get_id_str(get_entity_ident(ent));
160 }
161
162 static INLINE ident *
163 _get_entity_ident(const entity *ent) {
164   assert(ent && ent->kind == k_entity);
165   return ent->name;
166 }
167
168 static INLINE void
169 _set_entity_ident(entity *ent, ident *id) {
170   assert(ent && ent->kind == k_entity);
171   ent->name = id;
172 }
173
174 static INLINE ir_type *
175 _get_entity_owner(entity *ent) {
176   assert(ent && ent->kind == k_entity);
177   return ent->owner = skip_tid(ent->owner);
178 }
179
180 static INLINE ident *
181 _get_entity_ld_ident(entity *ent)
182 {
183   assert(ent && ent->kind == k_entity);
184   if (ent->ld_name == NULL)
185     ent->ld_name = mangle_entity(ent);
186   return ent->ld_name;
187 }
188
189 static INLINE void
190 _set_entity_ld_ident(entity *ent, ident *ld_ident) {
191   assert(ent && ent->kind == k_entity);
192   ent->ld_name = ld_ident;
193 }
194
195 static INLINE const char *
196 _get_entity_ld_name(entity *ent) {
197   assert(ent && ent->kind == k_entity);
198   return get_id_str(get_entity_ld_ident(ent));
199 }
200
201 static INLINE ir_type *
202 _get_entity_type(entity *ent) {
203   assert(ent && ent->kind == k_entity);
204   return ent->type = skip_tid(ent->type);
205 }
206
207 static INLINE void
208 _set_entity_type(entity *ent, ir_type *type) {
209   assert(ent && ent->kind == k_entity);
210   ent->type = type;
211 }
212
213 static INLINE ir_allocation
214 _get_entity_allocation(const entity *ent) {
215   assert(ent && ent->kind == k_entity);
216   return ent->allocation;
217 }
218
219 static INLINE void
220 _set_entity_allocation(entity *ent, ir_allocation al) {
221   assert(ent && ent->kind == k_entity);
222   ent->allocation = al;
223 }
224
225 static INLINE ir_visibility
226 _get_entity_visibility(const entity *ent) {
227   assert(ent && ent->kind == k_entity);
228   return ent->visibility;
229 }
230
231 static INLINE ir_variability
232 _get_entity_variability(const entity *ent) {
233   assert(ent && ent->kind == k_entity);
234   return ent->variability;
235 }
236
237 static INLINE ir_volatility
238 _get_entity_volatility(const entity *ent) {
239   assert(ent && ent->kind == k_entity);
240   return ent->volatility;
241 }
242
243 static INLINE void
244 _set_entity_volatility(entity *ent, ir_volatility vol) {
245   assert(ent && ent->kind == k_entity);
246   ent->volatility = vol;
247 }
248
249 static INLINE ir_peculiarity
250 _get_entity_peculiarity(const entity *ent) {
251   assert(ent && ent->kind == k_entity);
252   return ent->peculiarity;
253 }
254
255 /**
256  * @todo Why peculiarity only for methods?
257  *       Good question.  Originally, there were only description and
258  *       existent.  The thought was, what sense does it make to
259  *       describe a field?  With inherited the situation changed.  So
260  *       I removed the assertion.  GL, 28.2.05
261  */
262 static INLINE void
263 _set_entity_peculiarity(entity *ent, ir_peculiarity pec) {
264   assert(ent && ent->kind == k_entity);
265   /* @@@ why peculiarity only for methods? */
266   //assert(is_Method_type(ent->type));
267
268   ent->peculiarity = pec;
269 }
270
271 static INLINE ir_stickyness
272 _get_entity_stickyness(const entity *ent) {
273   assert(ent && ent->kind == k_entity);
274   return ent->stickyness;
275 }
276
277 static INLINE void
278 _set_entity_stickyness(entity *ent, ir_stickyness stickyness)
279 {
280   assert(ent && ent->kind == k_entity);
281   ent->stickyness = stickyness;
282 }
283
284 static INLINE int
285 _get_entity_offset_bits(const entity *ent) {
286   assert(ent && ent->kind == k_entity);
287   return ent->offset;
288 }
289
290 static INLINE int
291 _get_entity_offset_bytes(const entity *ent) {
292   int bits = _get_entity_offset_bits(ent);
293
294   if (bits & 7) return -1;
295   return bits >> 3;
296 }
297
298 static INLINE void
299 _set_entity_offset_bits(entity *ent, int offset) {
300   assert(ent && ent->kind == k_entity);
301   ent->offset = offset;
302 }
303
304 static INLINE void
305 _set_entity_offset_bytes(entity *ent, int offset) {
306   _set_entity_offset_bits(ent, offset * 8);
307 }
308
309 static INLINE void *
310 _get_entity_link(const entity *ent) {
311   assert(ent && ent->kind == k_entity);
312   return ent->link;
313 }
314
315 static INLINE void
316 _set_entity_link(entity *ent, void *l) {
317   assert(ent && ent->kind == k_entity);
318   ent->link = l;
319 }
320
321 static INLINE ir_graph *
322 _get_entity_irg(const entity *ent) {
323   assert(ent && ent->kind == k_entity);
324   assert(ent == unknown_entity || is_Method_type(ent->type));
325   if (!get_visit_pseudo_irgs() && ent->attr.mtd_attr.irg
326       && is_pseudo_ir_graph(ent->attr.mtd_attr.irg))
327     return NULL;
328   return ent->attr.mtd_attr.irg;
329 }
330
331 static INLINE unsigned long
332 _get_entity_visited(entity *ent) {
333   assert(ent && ent->kind == k_entity);
334   return ent->visit;
335 }
336
337 static INLINE void
338 _set_entity_visited(entity *ent, unsigned long num) {
339   assert(ent && ent->kind == k_entity);
340   ent->visit = num;
341 }
342
343 static INLINE void
344 _mark_entity_visited(entity *ent) {
345   assert(ent && ent->kind == k_entity);
346   ent->visit = firm_type_visited;
347 }
348
349 static INLINE int
350 _entity_visited(entity *ent) {
351   return _get_entity_visited(ent) >= firm_type_visited;
352 }
353
354 static INLINE int
355 _entity_not_visited(entity *ent) {
356   return _get_entity_visited(ent) < firm_type_visited;
357 }
358
359 #define is_entity(thing)                         _is_entity(thing)
360 #define get_entity_name(ent)                     _get_entity_name(ent)
361 #define get_entity_ident(ent)                    _get_entity_ident(ent)
362 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
363 #define get_entity_owner(ent)                    _get_entity_owner(ent)
364 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
365 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
366 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
367 #define get_entity_type(ent)                     _get_entity_type(ent)
368 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
369 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
370 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
371 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
372 #define get_entity_variability(ent)              _get_entity_variability(ent)
373 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
374 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
375 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
376 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
377 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
378 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
379 #define get_entity_offset_bits(ent)              _get_entity_offset_bits(ent)
380 #define get_entity_offset_bytes(ent)             _get_entity_offset_bytes(ent)
381 #define set_entity_offset_bits(ent, offset)      _set_entity_offset_bits(ent, offset)
382 #define set_entity_offset_bytes(ent, offset)     _set_entity_offset_bytes(ent, offset)
383 #define get_entity_link(ent)                     _get_entity_link(ent)
384 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
385 #define get_entity_irg(ent)                      _get_entity_irg(ent)
386 #define get_entity_visited(ent)                  _get_entity_visited(ent)
387 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
388 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
389 #define entity_visited(ent)                      _entity_visited(ent)
390 #define entity_not_visited(ent)                  _entity_not_visited(ent)
391
392 #endif /* _FIRM_TR_ENTITY_T_H_ */