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