5343c4a28b454dd8c9615855e50724476b5c61a1
[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
43 # include "entity.h"
44 # include "typegmod.h"
45 # include "mangle.h"
46
47
48 /** A path in a compund graph. */
49 struct compound_graph_path {
50   firm_kind kind;       /**< dynamic type tag for compound graph path. */
51   type *tp;             /**< The type this path belongs to. */
52   int len;              /**< length of the path */
53   int *arr_indicees;    /**< List of array indicees.  To compute position of
54                  array elements */
55   entity *nodes[1];     /**< List of entities of length len to express the
56                  access path. */
57 };
58
59 /** the type of an entity */
60 struct entity {
61   firm_kind kind;       /**< dynamic type tag for entity. */
62   ident *name;          /**< name of this entity */
63   ident *ld_name;       /**< Unique name of this entity, i.e., the mangled
64                            name.  If the field is read before written a default
65                            magling is applies.  The name of the owner is prepended
66                            to the name of the entity, separated by a underscore.
67                            E.g.,  for a class `A' with field `a' this
68                            is the ident for `A_a'. */
69   type *type;           /**< The type of this entity, e.g., a method type, a
70                            basic type of the language or a class itself */
71   type *owner;          /**< The compound type (e.g. class type) this entity belongs to. */
72   ent_allocation allocation;  /**< Distinguishes static and dynamically allocated
73                  entities and some further cases. */
74   ent_visibility visibility;  /**< Specifies visibility to external program
75                  fragments */
76   ent_variability variability;  /**< Specifies variability of entities content */
77   ent_volatility volatility;    /**< Specifies volatility of entities content */
78   ent_stickyness stickyness;    /**< Specifies whether this entity is sticky  */
79   int  offset;          /**< Offset in bits for this entity.  Fixed when layout
80                of owner is determined.  */
81   void *link;           /**< To store some intermediate information */
82   unsigned long visit;  /**< visited counter for walks of the type information */
83   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
84
85   /* ------------- fields for atomic entities  ---------------*/
86
87   ir_node *value;            /**< value if entity is not of variability uninitialized.
88                                Only for atomic entities. */
89
90   /* ------------- fields for compound entities ---------------*/
91
92   ir_node **values;     /**< constant values of compound entities. Only available if
93                variablility not uninitialized.  Must be set for variability constant
94                            */
95   compound_graph_path **val_paths; /**< paths corresponding to constant values. Only available if
96                       variablility not uninitialized.  Must be set for variability constant */
97
98   /* ------------- fields for entities owned by a class type ---------------*/
99
100   entity **overwrites;     /**< A list of entities this entity overwrites. */
101   entity **overwrittenby;  /**< A list of entities that overwrite this entity.  */
102
103   /* ------------- fields for methods ---------------*/
104
105   enum peculiarity peculiarity;
106   ir_graph *irg;        /**< If (type == method_type) this is the corresponding irg.
107                The ir_graph constructor automatically sets this field.
108                Yes, it must be here. */
109
110   /* ------------- fields for analyses ---------------*/
111
112   ir_node **accesses;                      /**< accessing nodes: loads, stores. */
113
114 #ifdef DEBUG_libfirm
115   int nr;             /**< a unique node number for each node to make output
116              readable. */
117   char *c_name;                 /**< Since idents are ipaque, provide the name in cleartext */
118 # endif /* DEBUG_libfirm */
119 };
120
121 /* ----------------------- inline functions ------------------------ */
122 static INLINE int
123 __is_entity(const void *thing) {
124   return get_kind(thing) == k_entity;
125 }
126
127 static INLINE const char *
128 __get_entity_name(const entity *ent) {
129   assert(ent && ent->kind == k_entity);
130   return get_id_str(get_entity_ident(ent));
131 }
132
133 static INLINE ident *
134 __get_entity_ident(const entity *ent) {
135   assert(ent && ent->kind == k_entity);
136   return ent->name;
137 }
138
139 static INLINE type *
140 __get_entity_owner(entity *ent) {
141   assert(ent && ent->kind == k_entity);
142   return ent->owner = skip_tid(ent->owner);
143 }
144
145 static INLINE ident *
146 __get_entity_ld_ident(entity *ent)
147 {
148   assert(ent && ent->kind == k_entity);
149   if (ent->ld_name == NULL)
150     ent->ld_name = mangle_entity(ent);
151   return ent->ld_name;
152 }
153
154 static INLINE void
155 __set_entity_ld_ident(entity *ent, ident *ld_ident) {
156   assert(ent && ent->kind == k_entity);
157   ent->ld_name = ld_ident;
158 }
159
160 static INLINE const char *
161 __get_entity_ld_name(entity *ent) {
162   assert(ent && ent->kind == k_entity);
163   return get_id_str(get_entity_ld_ident(ent));
164 }
165
166 static INLINE type *
167 __get_entity_type(entity *ent) {
168   assert(ent && ent->kind == k_entity);
169   return ent->type = skip_tid(ent->type);
170 }
171
172 static INLINE void
173 __set_entity_type(entity *ent, type *type) {
174   assert(ent && ent->kind == k_entity);
175   ent->type = type;
176 }
177
178 static INLINE ent_allocation
179 __get_entity_allocation(const entity *ent) {
180   assert(ent && ent->kind == k_entity);
181   return ent->allocation;
182 }
183
184 static INLINE void
185 __set_entity_allocation(entity *ent, ent_allocation al) {
186   assert(ent && ent->kind == k_entity);
187   ent->allocation = al;
188 }
189
190 static INLINE ent_visibility
191 __get_entity_visibility(const entity *ent) {
192   assert(ent && ent->kind == k_entity);
193   return ent->visibility;
194 }
195
196 static INLINE ent_variability
197 __get_entity_variability(const entity *ent) {
198   assert(ent && ent->kind == k_entity);
199   return ent->variability;
200 }
201
202 static INLINE ent_volatility
203 __get_entity_volatility(const entity *ent) {
204   assert(ent && ent->kind == k_entity);
205   return ent->volatility;
206 }
207
208 static INLINE void
209 __set_entity_volatility(entity *ent, ent_volatility vol) {
210   assert(ent && ent->kind == k_entity);
211   ent->volatility = vol;
212 }
213
214 static INLINE peculiarity
215 __get_entity_peculiarity(const entity *ent) {
216   assert(ent && ent->kind == k_entity);
217   return ent->peculiarity;
218 }
219
220 /**
221  * @todo why peculiarity only for methods
222  */
223 static INLINE void
224 __set_entity_peculiarity(entity *ent, peculiarity pec) {
225   assert(ent && ent->kind == k_entity);
226   /* @@@ why peculiarity only for methods? */
227   assert(is_method_type(ent->type));
228   ent->peculiarity = pec;
229 }
230
231 static INLINE ent_stickyness
232 __get_entity_stickyness(const entity *ent) {
233   assert(ent && ent->kind == k_entity);
234   return ent->stickyness;
235 }
236
237 static INLINE void
238 __set_entity_stickyness(entity *ent, ent_stickyness stickyness)
239 {
240   assert(ent && ent->kind == k_entity);
241   ent->stickyness = stickyness;
242 }
243
244 static INLINE int
245 __get_entity_offset_bits(const entity *ent) {
246   assert(ent && ent->kind == k_entity);
247   return ent->offset;
248 }
249
250 static INLINE int
251 __get_entity_offset_bytes(const entity *ent) {
252   int bits = __get_entity_offset_bits(ent);
253
254   if (bits & 7) return -1;
255   return bits >> 3;
256 }
257
258 static INLINE void
259 __set_entity_offset_bits(entity *ent, int offset) {
260   assert(ent && ent->kind == k_entity);
261   ent->offset = offset;
262 }
263
264 static INLINE void
265 __set_entity_offset_bytes(entity *ent, int offset) {
266   __set_entity_offset_bits(ent, offset * 8);
267 }
268
269 static INLINE void *
270 __get_entity_link(const entity *ent) {
271   assert(ent && ent->kind == k_entity);
272   return ent->link;
273 }
274
275 static INLINE void
276 __set_entity_link(entity *ent, void *l) {
277   assert(ent && ent->kind == k_entity);
278   ent->link = l;
279 }
280
281 static INLINE ir_graph *
282 __get_entity_irg(const entity *ent) {
283   assert(ent && ent->kind == k_entity);
284   assert(is_method_type(ent->type));
285   return ent->irg;
286 }
287
288 #define is_entity(thing)                        __is_entity(thing)
289 #define get_entity_name(ent)                    __get_entity_name(ent)
290 #define get_entity_ident(ent)                   __get_entity_ident(ent)
291 #define get_entity_owner(ent)                   __get_entity_owner(ent)
292 #define get_entity_ld_ident(ent)                __get_entity_ld_ident(ent)
293 #define set_entity_ld_ident(ent, ld_ident)      __set_entity_ld_ident(ent, ld_ident)
294 #define get_entity_ld_name(ent)                 __get_entity_ld_name(ent)
295 #define get_entity_type(ent)                    __get_entity_type(ent)
296 #define set_entity_type(ent, type)              __set_entity_type(ent, type)
297 #define get_entity_allocation(ent)              __get_entity_allocation(ent)
298 #define set_entity_allocation(ent, al)          __set_entity_allocation(ent, al)
299 #define get_entity_visibility(ent)              __get_entity_visibility(ent)
300 #define get_entity_variability(ent)             __get_entity_variability(ent)
301 #define get_entity_volatility(ent)              __get_entity_volatility(ent)
302 #define set_entity_volatility(ent, vol)         __set_entity_volatility(ent, vol)
303 #define get_entity_peculiarity(ent)             __get_entity_peculiarity(ent)
304 #define set_entity_peculiarity(ent, pec)        __set_entity_peculiarity(ent, pec)
305 #define get_entity_stickyness(ent)              __get_entity_stickyness(ent)
306 #define set_entity_stickyness(ent, stickyness)  __set_entity_stickyness(ent, stickyness)
307 #define get_entity_offset_bits(ent)             __get_entity_offset_bits(ent)
308 #define get_entity_offset_bytes(ent)            __get_entity_offset_bytes(ent)
309 #define set_entity_offset_bits(ent, offset)     __set_entity_offset_bits(ent, offset)
310 #define set_entity_offset_bytes(ent, offset)    __set_entity_offset_bytes(ent, offset)
311 #define get_entity_link(ent)                    __get_entity_link(ent)
312 #define set_entity_link(ent, l)                 __set_entity_link(ent, l)
313 #define get_entity_irg(ent)                     __get_entity_irg(ent)
314
315 # endif /* _ENTITY_T_H_ */