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