2c74a68b88486d7268a9236cff78512d3430d4fd
[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 compound 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   unsigned irg_calling_conv;  /**< If (type == method_type) this is a set of calling.
115                convention flags if the irg of an entity is not known. */
116   ptr_access_kind *param_access;  /**< the parameter access */
117   float *param_weight; /**< The weight of method's parameters. Parameters
118                           with a high weight are good for procedure cloning.*/
119
120   /* ------------- fields for analyses ---------------*/
121
122
123 #ifdef DEBUG_libfirm
124   int nr;             /**< a unique node number for each node to make output
125                            readable. */
126 # endif /* DEBUG_libfirm */
127 };
128
129 /** Initialize entity module. */
130 void firm_init_entity(void);
131
132
133 /* ----------------------- inline functions ------------------------ */
134 static INLINE int
135 _is_entity(const void *thing) {
136   return get_kind(thing) == k_entity;
137 }
138
139 static INLINE const char *
140 _get_entity_name(const entity *ent) {
141   assert(ent && ent->kind == k_entity);
142   return get_id_str(get_entity_ident(ent));
143 }
144
145 static INLINE ident *
146 _get_entity_ident(const entity *ent) {
147   assert(ent && ent->kind == k_entity);
148   return ent->name;
149 }
150
151 static INLINE type *
152 _get_entity_owner(entity *ent) {
153   assert(ent && ent->kind == k_entity);
154   return ent->owner = skip_tid(ent->owner);
155 }
156
157 static INLINE ident *
158 _get_entity_ld_ident(entity *ent)
159 {
160   assert(ent && ent->kind == k_entity);
161   if (ent->ld_name == NULL)
162     ent->ld_name = mangle_entity(ent);
163   return ent->ld_name;
164 }
165
166 static INLINE void
167 _set_entity_ld_ident(entity *ent, ident *ld_ident) {
168   assert(ent && ent->kind == k_entity);
169   ent->ld_name = ld_ident;
170 }
171
172 static INLINE const char *
173 _get_entity_ld_name(entity *ent) {
174   assert(ent && ent->kind == k_entity);
175   return get_id_str(get_entity_ld_ident(ent));
176 }
177
178 static INLINE type *
179 _get_entity_type(entity *ent) {
180   assert(ent && ent->kind == k_entity);
181   return ent->type = skip_tid(ent->type);
182 }
183
184 static INLINE void
185 _set_entity_type(entity *ent, type *type) {
186   assert(ent && ent->kind == k_entity);
187   ent->type = type;
188 }
189
190 static INLINE ent_allocation
191 _get_entity_allocation(const entity *ent) {
192   assert(ent && ent->kind == k_entity);
193   return ent->allocation;
194 }
195
196 static INLINE void
197 _set_entity_allocation(entity *ent, ent_allocation al) {
198   assert(ent && ent->kind == k_entity);
199   ent->allocation = al;
200 }
201
202 static INLINE visibility
203 _get_entity_visibility(const entity *ent) {
204   assert(ent && ent->kind == k_entity);
205   return ent->visibility;
206 }
207
208 static INLINE ent_variability
209 _get_entity_variability(const entity *ent) {
210   assert(ent && ent->kind == k_entity);
211   return ent->variability;
212 }
213
214 static INLINE ent_volatility
215 _get_entity_volatility(const entity *ent) {
216   assert(ent && ent->kind == k_entity);
217   return ent->volatility;
218 }
219
220 static INLINE void
221 _set_entity_volatility(entity *ent, ent_volatility vol) {
222   assert(ent && ent->kind == k_entity);
223   ent->volatility = vol;
224 }
225
226 static INLINE peculiarity
227 _get_entity_peculiarity(const entity *ent) {
228   assert(ent && ent->kind == k_entity);
229   return ent->peculiarity;
230 }
231
232 /**
233  * @todo Why peculiarity only for methods?
234  *       Good question.  Originally, there were only description and
235  *       existent.  The thought was, what sense does it make to
236  *       describe a field?  With inherited the situation changed.  So
237  *       I removed the assertion.  GL, 28.2.05
238  */
239 static INLINE void
240 _set_entity_peculiarity(entity *ent, peculiarity pec) {
241   assert(ent && ent->kind == k_entity);
242   /* @@@ why peculiarity only for methods? */
243   //assert(is_Method_type(ent->type));
244
245   ent->peculiarity = pec;
246 }
247
248 static INLINE ent_stickyness
249 _get_entity_stickyness(const entity *ent) {
250   assert(ent && ent->kind == k_entity);
251   return ent->stickyness;
252 }
253
254 static INLINE void
255 _set_entity_stickyness(entity *ent, ent_stickyness stickyness)
256 {
257   assert(ent && ent->kind == k_entity);
258   ent->stickyness = stickyness;
259 }
260
261 static INLINE int
262 _get_entity_offset_bits(const entity *ent) {
263   assert(ent && ent->kind == k_entity);
264   return ent->offset;
265 }
266
267 static INLINE int
268 _get_entity_offset_bytes(const entity *ent) {
269   int bits = _get_entity_offset_bits(ent);
270
271   if (bits & 7) return -1;
272   return bits >> 3;
273 }
274
275 static INLINE void
276 _set_entity_offset_bits(entity *ent, int offset) {
277   assert(ent && ent->kind == k_entity);
278   ent->offset = offset;
279 }
280
281 static INLINE void
282 _set_entity_offset_bytes(entity *ent, int offset) {
283   _set_entity_offset_bits(ent, offset * 8);
284 }
285
286 static INLINE void *
287 _get_entity_link(const entity *ent) {
288   assert(ent && ent->kind == k_entity);
289   return ent->link;
290 }
291
292 static INLINE void
293 _set_entity_link(entity *ent, void *l) {
294   assert(ent && ent->kind == k_entity);
295   ent->link = l;
296 }
297
298 static INLINE ir_graph *
299 _get_entity_irg(const entity *ent) {
300   assert(ent && ent->kind == k_entity);
301   assert(ent == unknown_entity || is_Method_type(ent->type));
302   if (!get_visit_pseudo_irgs() && ent->irg && is_pseudo_ir_graph(ent->irg))
303     return NULL;
304   return ent->irg;
305 }
306
307 static INLINE unsigned long
308 _get_entity_visited(entity *ent) {
309   assert(ent && ent->kind == k_entity);
310   return ent->visit;
311 }
312
313 static INLINE void
314 _set_entity_visited(entity *ent, unsigned long num) {
315   assert(ent && ent->kind == k_entity);
316   ent->visit = num;
317 }
318
319 static INLINE void
320 _mark_entity_visited(entity *ent) {
321   assert(ent && ent->kind == k_entity);
322   ent->visit = firm_type_visited;
323 }
324
325 static INLINE int
326 _entity_visited(entity *ent) {
327   return _get_entity_visited(ent) >= firm_type_visited;
328 }
329
330 static INLINE int
331 _entity_not_visited(entity *ent) {
332   return _get_entity_visited(ent) < firm_type_visited;
333 }
334
335 static INLINE unsigned
336 _get_entity_additional_properties(const entity *ent) {
337   ir_graph *irg;
338   assert(ent && ent->kind == k_entity);
339   assert(ent == unknown_entity || is_Method_type(ent->type));
340   irg = _get_entity_irg(ent);
341   return irg ?
342     get_irg_additional_properties(irg) :
343     ent->irg_add_properties;
344 }
345
346 static INLINE void
347 _set_entity_additional_properties(entity *ent, unsigned mask) {
348   ir_graph *irg;
349   assert(ent && ent->kind == k_entity);
350   assert(ent == unknown_entity || is_Method_type(ent->type));
351   irg = _get_entity_irg(ent);
352   if (irg)
353     set_irg_additional_properties(irg, mask);
354   else
355     ent->irg_add_properties = mask;
356 }
357
358 static INLINE void
359 _set_entity_additional_property(entity *ent, irg_additional_property flag) {
360   ir_graph *irg;
361   assert(ent && ent->kind == k_entity);
362   assert(ent == unknown_entity || is_Method_type(ent->type));
363   irg = _get_entity_irg(ent);
364   if (irg)
365     set_irg_additional_property(irg, flag);
366   else
367     ent->irg_add_properties |= flag;
368 }
369
370 static INLINE unsigned
371 _get_entity_calling_convention(const entity *ent) {
372   ir_graph *irg;
373   assert(ent && ent->kind == k_entity);
374   assert(ent == unknown_entity || is_Method_type(ent->type));
375   irg = _get_entity_irg(ent);
376   if (irg)
377     return get_irg_calling_convention(irg);
378   else
379     return ent->irg_calling_conv;
380 }
381
382 static INLINE void
383 _set_entity_calling_convention(entity *ent, unsigned cc_mask) {
384   ir_graph *irg;
385   assert(ent && ent->kind == k_entity);
386   assert(ent == unknown_entity || is_Method_type(ent->type));
387   irg = _get_entity_irg(ent);
388   if (irg)
389     set_irg_calling_convention(irg, cc_mask);
390   else
391     ent->irg_calling_conv = cc_mask;
392 }
393
394 #define is_entity(thing)                         _is_entity(thing)
395 #define get_entity_name(ent)                     _get_entity_name(ent)
396 #define get_entity_ident(ent)                    _get_entity_ident(ent)
397 #define get_entity_owner(ent)                    _get_entity_owner(ent)
398 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
399 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
400 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
401 #define get_entity_type(ent)                     _get_entity_type(ent)
402 #define set_entity_type(ent, type)               _set_entity_type(ent, type)
403 #define get_entity_allocation(ent)               _get_entity_allocation(ent)
404 #define set_entity_allocation(ent, al)           _set_entity_allocation(ent, al)
405 #define get_entity_visibility(ent)               _get_entity_visibility(ent)
406 #define get_entity_variability(ent)              _get_entity_variability(ent)
407 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
408 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
409 #define get_entity_peculiarity(ent)              _get_entity_peculiarity(ent)
410 #define set_entity_peculiarity(ent, pec)         _set_entity_peculiarity(ent, pec)
411 #define get_entity_stickyness(ent)               _get_entity_stickyness(ent)
412 #define set_entity_stickyness(ent, stickyness)   _set_entity_stickyness(ent, stickyness)
413 #define get_entity_offset_bits(ent)              _get_entity_offset_bits(ent)
414 #define get_entity_offset_bytes(ent)             _get_entity_offset_bytes(ent)
415 #define set_entity_offset_bits(ent, offset)      _set_entity_offset_bits(ent, offset)
416 #define set_entity_offset_bytes(ent, offset)     _set_entity_offset_bytes(ent, offset)
417 #define get_entity_link(ent)                     _get_entity_link(ent)
418 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
419 #define get_entity_irg(ent)                      _get_entity_irg(ent)
420 #define get_entity_visited(ent)                  _get_entity_visited(ent)
421 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
422 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
423 #define entity_visited(ent)                      _entity_visited(ent)
424 #define entity_not_visited(ent)                  _entity_not_visited(ent)
425 #define get_entity_additional_properties(ent)    _get_entity_additional_properties(ent)
426 #define set_entity_additional_properties(ent, m) _set_entity_additional_properties(ent, m)
427 #define set_entity_additional_property(ent, f)   _set_entity_additional_property(ent, f)
428 #define get_entity_calling_convention(ent)       _get_entity_calling_convention(ent)
429 #define set_entity_calling_convention(ent, cc)   _set_entity_calling_convention(ent, cc)
430
431 # endif /* _ENTITY_T_H_ */