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