becopyheur2: Cache the admissible registers eagerly.
[libfirm] / ir / tr / entity_t.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Representation of all program known entities -- private header.
9  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10  */
11 #ifndef FIRM_TR_ENTITY_T_H
12 #define FIRM_TR_ENTITY_T_H
13
14 #include <assert.h>
15 #include <stdbool.h>
16
17 #include "typerep.h"
18 #include "type_t.h"
19 #include "ident.h"
20
21 #define is_entity(thing)                         _is_entity(thing)
22 #define get_entity_name(ent)                     _get_entity_name(ent)
23 #define get_entity_ident(ent)                    _get_entity_ident(ent)
24 #define set_entity_ident(ent, id)                _set_entity_ident(ent, id)
25 #define get_entity_owner(ent)                    _get_entity_owner(ent)
26 #define get_entity_ld_ident(ent)                 _get_entity_ld_ident(ent)
27 #define set_entity_ld_ident(ent, ld_ident)       _set_entity_ld_ident(ent, ld_ident)
28 #define get_entity_ld_name(ent)                  _get_entity_ld_name(ent)
29 #define get_entity_type(ent)                     _get_entity_type(ent)
30 #define get_entity_linkage(ent)                  _get_entity_linkage(ent)
31 #define get_entity_volatility(ent)               _get_entity_volatility(ent)
32 #define set_entity_volatility(ent, vol)          _set_entity_volatility(ent, vol)
33 #define set_entity_alignment(ent, alignment)     _set_entity_alignment(ent, alignment)
34 #define get_entity_alignment(ent)                _get_entity_alignment(ent)
35 #define get_entity_align(ent)                    _get_entity_align(ent)
36 #define set_entity_align(ent, a)                 _set_entity_align(ent, a)
37 #define is_entity_compiler_generated(ent)        _is_entity_compiler_generated(ent)
38 #define set_entity_compiler_generated(ent, flag) _set_entity_compiler_generated(ent, flag)
39 #define get_entity_usage(ent)                    _get_entity_usage(ent)
40 #define set_entity_usage(ent, flags)             _set_entity_usage(ent, flags)
41 #define get_entity_offset(ent)                   _get_entity_offset(ent)
42 #define set_entity_offset(ent, offset)           _set_entity_offset(ent, offset)
43 #define get_entity_offset_bits_remainder(ent)    _get_entity_offset_bits_remainder(ent)
44 #define set_entity_offset_bits_remainder(ent, o) _set_entity_offset_bits_remainder(ent, o)
45 #define get_entity_link(ent)                     _get_entity_link(ent)
46 #define set_entity_link(ent, l)                  _set_entity_link(ent, l)
47 #define get_entity_irg(ent)                      _get_entity_irg(ent)
48 #define is_parameter_entity(ent)                 _is_parameter_entity(ent)
49 #define get_entity_parameter_number(ent)         _get_entity_parameter_number(ent)
50 #define get_entity_visited(ent)                  _get_entity_visited(ent)
51 #define set_entity_visited(ent, num)             _set_entity_visited(ent, num)
52 #define mark_entity_visited(ent)                 _mark_entity_visited(ent)
53 #define entity_visited(ent)                      _entity_visited(ent)
54 #define entity_not_visited(ent)                  _entity_not_visited(ent)
55 #define get_entity_dbg_info(ent)                 _get_entity_dbg_info(ent)
56 #define set_entity_dbg_info(ent, db)             _set_entity_dbg_info(ent, db)
57
58 typedef struct ir_initializer_base_t {
59         ir_initializer_kind_t kind;
60 } ir_initializer_base_t;
61
62 /**
63  * An compound initializer.
64  */
65 typedef struct ir_initializer_compound_t {
66         ir_initializer_base_t  base;
67         size_t                 n_initializers;
68         ir_initializer_t      *initializers[1];
69 } ir_initializer_compound_t;
70
71 /**
72  * An initializer containing an ir_node,
73  */
74 typedef struct ir_initializer_const_t {
75         ir_initializer_base_t  base;
76         ir_node               *value;
77 } ir_initializer_const_t ;
78
79 /**
80  * An initializer containing a tarval.
81  */
82 typedef struct ir_initializer_tarval_t {
83         ir_initializer_base_t  base;
84         ir_tarval             *value;
85 } ir_initializer_tarval_t ;
86
87 union ir_initializer_t {
88         ir_initializer_kind_t      kind;
89         ir_initializer_base_t      base;
90         ir_initializer_compound_t  compound;
91         ir_initializer_const_t     consti;
92         ir_initializer_tarval_t    tarval;
93 };
94
95 /** The attributes for methods. */
96 typedef struct method_ent_attr {
97         ir_graph *irg;                 /**< The corresponding irg if known.
98                                             The ir_graph constructor automatically sets this field. */
99         mtp_additional_properties properties;   /**< Additional graph properties can be
100                                             stored in a entity if no irg is available. */
101
102         unsigned vtable_number;        /**< For a dynamically called method, the number assigned
103                                             in the virtual function table. */
104
105         ptr_access_kind *param_access; /**< the parameter access */
106         unsigned *param_weight;        /**< The weight of method's parameters. Parameters
107                                             with a high weight are good candidates for procedure cloning. */
108 } method_ent_attr;
109
110 /** additional attributes for code entities */
111 typedef struct code_ent_attr {
112         ir_label_t  label;       /** label of the basic block */
113 } code_ent_attr;
114
115 typedef struct parameter_ent_attr {
116         size_t   number; /**< corresponding parameter number */
117         ir_mode *doubleword_low_mode;/**< entity is a lowered doubleword parameter,
118                                                                 so additional stores because of calling
119                                                                 convention are correctly performed.
120                                     Matze: This is a hack. In an ideal
121                                     wor^H^H^Hlibfirm we would first establish
122                                     calling conventions and then perform doubleword
123                                     lowering...) */
124 } parameter_ent_attr;
125
126 typedef enum ir_entity_kind {
127         IR_ENTITY_NORMAL,
128         IR_ENTITY_METHOD,
129         IR_ENTITY_COMPOUND_MEMBER,
130         IR_ENTITY_PARAMETER,
131         IR_ENTITY_LABEL,
132         IR_ENTITY_UNKNOWN,
133 } ir_entity_kind;
134
135 /**
136  * An abstract data type to represent program entities.
137  */
138 struct ir_entity {
139         firm_kind kind;          /**< The dynamic type tag for entity. */
140         ident *name;             /**< The name of this entity. */
141         ident *ld_name;          /**< Unique name of this entity, i.e., the mangled
142                                       name. May be NULL to indicate that a default
143                                       mangling based on the name should happen */
144         ir_type *type;           /**< The type of this entity */
145         ir_type *owner;          /**< The compound type (e.g. class type) this
146                                                               entity belongs to. */
147         unsigned entity_kind:3;  /**< entity kind */
148         unsigned linkage:10;     /**< Specifies linkage type */
149         unsigned volatility:1;   /**< Specifies volatility of entities content.*/
150         unsigned aligned:1;      /**< Specifies alignment of entities content. */
151         unsigned usage:4;        /**< flag indicating usage types of this entity,
152                                       see ir_entity_usage. */
153         unsigned compiler_gen:1; /**< If set, this entity was compiler generated.
154                                   */
155         unsigned visibility:3;   /**< @deprecated */
156         unsigned allocation:3;   /**< @deprecated */
157         unsigned peculiarity:3;  /**< @deprecated */
158         unsigned final:1;        /**< @deprecated */
159         unsigned offset_bit_remainder:8;
160                                  /**< If the entity is a bit field, this is the
161                                       offset of the start of the bit field
162                                       within the byte specified by offset. */
163         int offset;              /**< Offset in bytes for this entity. Fixed
164                                       when layout of owner is determined. */
165         unsigned alignment;      /**< entity alignment in bytes */
166         ir_visited_t visit;      /**< visited counter for walks of the type
167                                       information. */
168         struct dbg_info *dbi;    /**< A pointer to information for debug support. */
169         void *link;              /**< To store some intermediate information. */
170
171         ir_entity **overwrites;  /**< A list of entities this entity overwrites. */
172         ir_entity **overwrittenby; /**< A list of entities that overwrite this
173                                         entity. */
174
175         ir_initializer_t *initializer; /**< entity initializer */
176 #ifdef DEBUG_libfirm
177         long nr;             /**< A unique node number for each node to make output
178                                   readable. */
179 #endif
180
181         union {
182                 /* ------------- fields for method entities ---------------- */
183                 method_ent_attr    mtd_attr;
184                 /* fields for code entities */
185                 code_ent_attr      code_attr;
186                 /** parameter number for parameter entities */
187                 parameter_ent_attr parameter;
188         } attr; /**< type specific attributes */
189 };
190
191 /** Initialize the entity module. */
192 void ir_init_entity(ir_prog *irp);
193 /** Cleanup entity module */
194 void ir_finish_entity(ir_prog *irp);
195
196 /**
197  * Creates an entity corresponding to the start address of a basic block
198  * (the basic block is marked with a label id).
199  */
200 ir_entity *new_label_entity(ir_label_t label);
201
202 /**
203  * Like new_label_entity() but with debug information.
204  */
205 ir_entity *new_d_label_entity(ir_label_t label, dbg_info *dbgi);
206
207 void set_entity_irg(ir_entity *ent, ir_graph *irg);
208
209 /* ----------------------- inline functions ------------------------ */
210 static inline int _is_entity(const void *thing)
211 {
212         return get_kind(thing) == k_entity;
213 }
214
215 static inline ident *_get_entity_ident(const ir_entity *ent)
216 {
217         assert(ent->kind == k_entity);
218         return ent->name;
219 }
220
221 static inline const char *_get_entity_name(const ir_entity *ent)
222 {
223         assert(ent->kind == k_entity);
224         return get_id_str(get_entity_ident(ent));
225 }
226
227 static inline void _set_entity_ident(ir_entity *ent, ident *id)
228 {
229         assert(ent->kind == k_entity);
230         ent->name = id;
231 }
232
233 static inline ir_type *_get_entity_owner(const ir_entity *ent)
234 {
235         assert(ent->kind == k_entity);
236         return ent->owner;
237 }
238
239 static inline ident *_get_entity_ld_ident(const ir_entity *ent)
240 {
241         assert(ent->kind == k_entity);
242         if (ent->ld_name == NULL)
243                 return ent->name;
244         return ent->ld_name;
245 }
246
247 static inline void _set_entity_ld_ident(ir_entity *ent, ident *ld_ident)
248 {
249         assert(ent->kind == k_entity);
250         ent->ld_name = ld_ident;
251 }
252
253 static inline const char *_get_entity_ld_name(const ir_entity *ent)
254 {
255         assert(ent->kind == k_entity);
256         return get_id_str(get_entity_ld_ident(ent));
257 }
258
259 static inline ir_type *_get_entity_type(const ir_entity *ent)
260 {
261         assert(ent->kind == k_entity);
262         return ent->type;
263 }
264
265 static inline ir_linkage _get_entity_linkage(const ir_entity *ent)
266 {
267         assert(ent->kind == k_entity);
268         return (ir_linkage) ent->linkage;
269 }
270
271 static inline ir_volatility _get_entity_volatility(const ir_entity *ent)
272 {
273         assert(ent->kind == k_entity);
274         return (ir_volatility) ent->volatility;
275 }
276
277 static inline void _set_entity_volatility(ir_entity *ent, ir_volatility vol)
278 {
279         assert(ent->kind == k_entity);
280         ent->volatility = vol;
281 }
282
283 static inline unsigned _get_entity_alignment(const ir_entity *ent)
284 {
285         assert(ent->kind == k_entity);
286         return ent->alignment;
287 }
288
289 static inline void _set_entity_alignment(ir_entity *ent, unsigned alignment)
290 {
291         assert(ent->kind == k_entity);
292         ent->alignment = alignment;
293 }
294
295 static inline ir_align _get_entity_aligned(const ir_entity *ent)
296 {
297         assert(ent->kind == k_entity);
298         return (ir_align) ent->aligned;
299 }
300
301 static inline void _set_entity_aligned(ir_entity *ent, ir_align a)
302 {
303         assert(ent->kind == k_entity);
304         ent->aligned = a;
305 }
306
307 static inline int _is_entity_compiler_generated(const ir_entity *ent)
308 {
309         assert(ent->kind == k_entity);
310         return ent->compiler_gen;
311 }
312
313 static inline void _set_entity_compiler_generated(ir_entity *ent, int flag)
314 {
315         assert(ent->kind == k_entity);
316         ent->compiler_gen = flag ? 1 : 0;
317 }
318
319 static inline ir_entity_usage _get_entity_usage(const ir_entity *ent)
320 {
321         assert(ent->kind == k_entity);
322         return (ir_entity_usage) ent->usage;
323 }
324
325 static inline void _set_entity_usage(ir_entity *ent, ir_entity_usage state)
326 {
327         assert(ent->kind == k_entity);
328         ent->usage = state;
329 }
330
331 static inline int _get_entity_offset(const ir_entity *ent)
332 {
333         assert(ent->kind == k_entity);
334         return ent->offset;
335 }
336
337 static inline void _set_entity_offset(ir_entity *ent, int offset)
338 {
339         assert(ent->kind == k_entity);
340         ent->offset = offset;
341 }
342
343 static inline unsigned char _get_entity_offset_bits_remainder(const ir_entity *ent)
344 {
345         assert(ent->kind == k_entity);
346         return ent->offset_bit_remainder;
347 }
348
349 static inline void _set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset)
350 {
351         assert(ent->kind == k_entity);
352         ent->offset_bit_remainder = offset;
353 }
354
355 static inline void *_get_entity_link(const ir_entity *ent)
356 {
357         assert(ent->kind == k_entity);
358         return ent->link;
359 }
360
361 static inline void _set_entity_link(ir_entity *ent, void *l)
362 {
363         assert(ent->kind == k_entity);
364         ent->link = l;
365 }
366
367 static inline ir_graph *_get_entity_irg(const ir_entity *ent)
368 {
369         assert(ent->kind == k_entity);
370         if (!is_Method_type(ent->type) || is_unknown_entity(ent)) {
371                 return NULL;
372         }
373
374         return ent->attr.mtd_attr.irg;
375 }
376
377 static inline ir_visited_t _get_entity_visited(const ir_entity *ent)
378 {
379         assert(ent->kind == k_entity);
380         return ent->visit;
381 }
382
383 static inline void _set_entity_visited(ir_entity *ent, ir_visited_t num)
384 {
385         assert(ent->kind == k_entity);
386         ent->visit = num;
387 }
388
389 static inline void _mark_entity_visited(ir_entity *ent)
390 {
391         assert(ent->kind == k_entity);
392         ent->visit = firm_type_visited;
393 }
394
395 static inline int _entity_visited(const ir_entity *ent)
396 {
397         return _get_entity_visited(ent) >= firm_type_visited;
398 }
399
400 static inline int _entity_not_visited(const ir_entity *ent)
401 {
402         return _get_entity_visited(ent) < firm_type_visited;
403 }
404
405 static inline int _is_parameter_entity(const ir_entity *entity)
406 {
407         return entity->entity_kind == IR_ENTITY_PARAMETER;
408 }
409
410 static inline size_t _get_entity_parameter_number(const ir_entity *entity)
411 {
412         assert(entity->entity_kind == IR_ENTITY_PARAMETER);
413         return entity->attr.parameter.number;
414 }
415
416 static inline dbg_info *_get_entity_dbg_info(const ir_entity *ent)
417 {
418         return ent->dbi;
419 }
420
421 static inline void _set_entity_dbg_info(ir_entity *ent, dbg_info *db)
422 {
423         ent->dbi = db;
424 }
425
426 #endif