3 * File name: ir/tr/entity.h
4 * Purpose: Representation of all program known entities.
5 * Author: Martin Trapp, Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * Entities represent all program known objects.
18 * @author Martin Trapp, Christian Schaefer
19 * @author Goetz Lindenmaier
21 * An entity is the representation of program known objects in Firm.
22 * The primary concept of entities is to represent members of complex
23 * types, i.e., fields and methods of classes. As not all programming
24 * language model all variables and methods as members of some class,
25 * the concept of entities is extended to cover also local and global
26 * variables, and arbitrary procedures.
28 * An entity always specifies the type of the object it represents and
29 * the type of the object it is a part of, the owner of the entity.
30 * Originally this is the type of the class of which the entity is a
32 * The owner of local variables is the procedure they are defined in.
33 * The owner of global variables and procedures visible in the whole
34 * program is a universally defined class type "GlobalType". The owner
35 * of procedures defined in the scope of an other procedure is the
36 * enclosing procedure.
38 * In detail the datastructure entity has the following fields:
40 * - ident *name: Name of this entity as specified in the source code.
41 * Only unequivocal in conjuction with scope.
42 * - ident *ld_name: Unique name of this entity, i.e., the mangled
43 * name. E.g., for a class `A' with field `a' this
44 * is the ident for `A_a'.
45 * - ir_type *type: The type of this entity, e.g., a method type, a
46 * basic type of the language or a class itself.
47 * - ir_type *owner: The class this entity belongs to. In case of local
48 * variables the method they are defined in.
49 * - int offset: Offset in bits for this entity. Fixed when layout
50 * of owner is determined.
51 * - ir_graph *irg: If (type == method_type) this is the corresponding irg.
52 * The ir_graph constructor automatically sets this field.
53 * If (type != method_type) access of this field will cause
55 * - unsigned irg_add_properties:
56 * If (type == method_type) this mirrors the additional flags
57 * of the corresponding irg if set or is an own set for
58 * this entity. This construction allows to specify these
59 * flags even if no graph is available.
60 * If (type != method_type) access of this field will cause
63 #ifndef _FIRM_TR_ENTITY_H_
64 #define _FIRM_TR_ENTITY_H_
66 #include "firm_types.h"
69 #include "tr_inheritance.h"
71 /*-----------------------------------------------------------------*/
73 /*-----------------------------------------------------------------*/
77 * An abstract data type to represent program entities.
79 * @param owner A compound type this entity is a part of.
80 * @param type The type of this entity.
81 * @param name The string that represents this entity in the source program.
82 * @param allocation A flag saying whether the entity is dynamically or statically
83 * allocated (values: dynamic_allocated, static_allocated,
84 * automatic_allocated).
85 * @param visibility A flag indicating the visibility of this entity (values: local,
86 * external_visible, external_allocated)
87 * @param variability A flag indicating the variability of this entity (values:
88 * uninitialized, initialized, part_constant, constant)
89 * @param volatility @@@
90 * @param offset The offset of the entity within the compound object in bits. Only set
91 * if the owner in the state "layout_fixed".
92 * @param overwrites A list of entities overwritten by this entity. This list is only
93 * existent if the owner of this entity is a class. The members in
94 * this list must be entities of super classes.
95 * @param overwrittenby A list of entities that overwrite this entity. This list is only
96 * existent if the owner of this entity is a class. The members in
97 * this list must be entities of sub classes.
98 * @param link A void* to associate some additional information with the entity.
99 * @param irg If the entity is a method this is the ir graph that represents the
100 * code of the method.
101 * @param peculiarity The peculiarity of the entity. If the entity is a method this
102 * indicates whether the entity represents
103 * a real method or whether it only exists to describe an interface.
104 * In that case there nowhere exists code for this entity and this entity
105 * is never dynamically used in the code.
106 * Values: description, existent. Default: existent.
107 * @param visited visited flag. Master flag is type_visited.
109 * @param These fields can only be accessed via access functions.
114 /* to resolve recursion between entity.h and type.h */
115 /** the type of an entity */
116 #ifndef _ENTITY_TYPEDEF_
117 #define _ENTITY_TYPEDEF_
118 typedef struct entity entity;
122 * Creates a new entity.
124 * Automatically inserts the entity as a member of owner.
125 * Entity is automatic_allocated and uninitialized except if the type
126 * is type_method, then it is static_allocated and constant. The constant
127 * value is a pointer to the method.
128 * Visibility is local, offset -1, and it is not volatile.
130 entity *new_entity (ir_type *owner, ident *name, ir_type *tp);
133 * Creates a new entity.
135 * Automatically inserts the entity as a member of owner.
136 * The entity is automatic allocated and uninitialized except if the type
137 * is type_method, then it is static allocated and constant. The constant
138 * value is a pointer to the method.
139 * Visibility is local, offset -1, and it is not volatile.
141 entity *new_d_entity (ir_type *owner, ident *name, ir_type *tp, dbg_info *db);
144 * Copies the entity if the new_owner is different from the
145 * owner of the old entity, else returns the old entity.
147 * Automatically inserts the new entity as a member of owner.
148 * Resets the overwrites/overwritten_by fields.
149 * Keeps the old atomic value.
150 * @@@ Maybe we should change this. If peculiarity of a method
151 * is existent, we should add a new SymConst that points to
152 * itself and not to the origin. Right now we have to change
153 * the peculiarity and then set a new atomic value by hand.
155 entity *copy_entity_own (entity *old, ir_type *new_owner);
158 * Copies the entity if the new_name is different from the
159 * name of the old entity, else returns the old entity.
161 * Automatically inserts the new entity as a member of owner.
162 * The mangled name ld_name is set to NULL.
163 * Overwrites relation is copied from old.
165 entity *copy_entity_name (entity *old, ident *new_name);
170 * The owner will still contain the pointer to this
171 * entity, as well as all other references!
173 void free_entity (entity *ent);
175 /** Returns the name of an entity. */
176 const char *get_entity_name (const entity *ent);
178 /** Returns the ident of an entity. */
179 ident *get_entity_ident (const entity *ent);
181 /** Sets the ident of the entity. */
182 void set_entity_ident (entity *ent, ident *id);
184 /** Returns the mangled name of the entity.
186 * If the mangled name is set it returns the existing name.
187 * Else it generates a name with mangle_entity()
188 * and remembers this new name internally.
190 ident *get_entity_ld_ident (entity *ent);
192 /** Sets the mangled name of the entity. */
193 void set_entity_ld_ident (entity *ent, ident *ld_ident);
195 /** Returns the mangled name of the entity as a string. */
196 const char *get_entity_ld_name (entity *ent);
198 /** Returns the owner of the entity. */
199 ir_type *get_entity_owner (entity *ent);
201 /** Sets the owner field in entity to owner. Don't forget to add
203 void set_entity_owner (entity *ent, ir_type *owner);
205 /** Asserts if the type owner is either a compound type or an array */
206 void assert_legal_owner_of_ent(ir_type *owner);
208 /** Returns the type of an entity. */
209 ir_type *get_entity_type (entity *ent);
211 /** Sets the type of an entity. */
212 void set_entity_type (entity *ent, ir_type *tp);
214 /** The allocation type. */
216 allocation_automatic, /**< The entity is allocated during runtime, implicitly
217 as component of a compound type. This is the default. */
218 allocation_parameter, /**< The entity is a parameter. It is also automatic allocated.
219 We distinguish the allocation of parameters from the allocation
220 of local variables as their placement depends on the calling
222 allocation_dynamic, /**< The entity is allocated during runtime, explicitly
224 allocation_static /**< The entity is allocated statically. We can use a
225 Const as address of the entity. This is the default for methods. */
228 /** Returns the allocation type of an entity. */
229 ir_allocation get_entity_allocation(const entity *ent);
231 /** Sets the allocation type of an entity. */
232 void set_entity_allocation(entity *ent, ir_allocation al);
234 /** Return the name of the allocation type. */
235 const char *get_allocation_name(ir_allocation vis);
237 /** Returns the visibility of an entity. */
238 ir_visibility get_entity_visibility(const entity *ent);
240 /** Sets the visibility of an entity. */
241 void set_entity_visibility(entity *ent, ir_visibility vis);
243 /** Return the name of the visibility */
244 const char *get_visibility_name(ir_visibility vis);
246 /** This enumeration flags the variability of entities. */
248 variability_uninitialized, /**< The content of the entity is completely unknown. Default. */
249 variability_initialized, /**< After allocation the entity is initialized with the
250 value given somewhere in the entity. */
251 variability_part_constant, /**< For entities of compound types.
252 The members of the entity are mixed constant,
253 initialized or uninitialized. */
254 variability_constant /**< The entity is constant. */
257 /** Returns the variability of an entity. */
258 ir_variability get_entity_variability(const entity *ent);
260 /** Sets the variability of an entity. */
261 void set_entity_variability(entity *ent, ir_variability var);
263 /** Return the name of the variability. */
264 const char *get_variability_name(ir_variability var);
266 /** This enumeration flags the volatility of entities. */
268 volatility_non_volatile, /**< The entity is not volatile. Default. */
269 volatility_is_volatile /**< The entity is volatile */
272 /** Returns the volatility of an entity. */
273 ir_volatility get_entity_volatility(const entity *ent);
275 /** Sets the volatility of an entity. */
276 void set_entity_volatility(entity *ent, ir_volatility vol);
278 /** Return the name of the volatility. */
279 const char *get_volatility_name(ir_volatility var);
281 /** This enumeration flags the stickyness of an entity. */
283 stickyness_unsticky, /**< The entity can be removed from
284 the program, unless contraindicated
285 by other attributes. Default. */
286 stickyness_sticky /**< The entity must remain in the
287 program in any case. */
290 /** Get the entity's stickyness */
291 ir_stickyness get_entity_stickyness(const entity *ent);
293 /** Set the entity's stickyness */
294 void set_entity_stickyness(entity *ent, ir_stickyness stickyness);
296 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
297 int get_entity_offset_bytes(const entity *ent);
299 /** Returns the offset of an entity (in a compound) in bits. Only set if layout = fixed. */
300 int get_entity_offset_bits(const entity *ent);
302 /** Sets the offset of an entity (in a compound) in bytes. */
303 void set_entity_offset_bytes(entity *ent, int offset);
305 /** Sets the offset of an entity (in a compound) in bits. */
306 void set_entity_offset_bits(entity *ent, int offset);
308 /** Returns the stored intermediate information. */
309 void *get_entity_link(const entity *ent);
311 /** Stores new intermediate information. */
312 void set_entity_link(entity *ent, void *l);
314 /* -- Fields of method entities -- */
315 /** The entity knows the corresponding irg if the entity is a method.
316 This allows to get from a Call to the called irg.
317 Only entities of peculiarity "existent" can have a corresponding irg,
318 else the field is fixed to NULL. (Get returns NULL, set asserts.) */
319 ir_graph *get_entity_irg(const entity *ent);
320 void set_entity_irg(entity *ent, ir_graph *irg);
322 /** Gets the entity vtable number. */
323 unsigned get_entity_vtable_number(entity *ent);
325 /** Sets the entity vtable number. */
326 void set_entity_vtable_number(entity *ent, unsigned vtable_number);
328 /** Return the peculiarity of an entity. */
329 ir_peculiarity get_entity_peculiarity(const entity *ent);
331 /** Sets the peculiarity of an entity. */
332 void set_entity_peculiarity(entity *ent, ir_peculiarity pec);
334 /** Checks if an entity is compiler generated */
335 int is_entity_compiler_generated(const entity *ent);
337 /** Sets/resets the compiler generated flag */
338 void set_entity_compiler_generated(entity *ent, int flag);
340 /* -- Representation of constant values of entities -- */
341 /** Returns true if the the node is representable as code on
343 int is_irn_const_expression(ir_node *n);
344 /* Set current_ir_graph to get_const_code_irg() to generate a constant
348 * Copies a firm subgraph that complies to the restrictions for
349 * constant expressions to current_block in current_ir_graph.
351 ir_node *copy_const_value(dbg_info *dbg, ir_node *n);
353 /* Set has no effect for existent entities of type method. */
354 ir_node *get_atomic_ent_value(entity *ent);
355 void set_atomic_ent_value(entity *ent, ir_node *val);
358 * The following type describes a path to a leave in the compound graph.
359 * Node 0 in the path must be an entity of type tp given in the constructor. If
360 * the type of this element is compound, the path node 1 is an element of the type
361 * of node 0 an so forth, until an entity of atomic type is reached.
363 #ifndef _COMPOUND_GRAPH_PATH_TYPEDEF_
364 #define _COMPOUND_GRAPH_PATH_TYPEDEF_
365 typedef struct compound_graph_path compound_graph_path;
366 #endif /* _COMPOUND_GRAPH_PATH_TYPEDEF_ */
368 /** Creates a new compound graph path. */
369 compound_graph_path *new_compound_graph_path(ir_type *tp, int length);
371 /** Returns non-zero if an object is a compound graph path */
372 int is_compound_graph_path(void *thing);
374 /** Frees a graph path object */
375 void free_compound_graph_path (compound_graph_path *gr);
377 /** Returns the length of a graph path */
378 int get_compound_graph_path_length(compound_graph_path *gr);
380 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
381 void set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node);
382 int get_compound_graph_path_array_index(compound_graph_path *gr, int pos);
383 void set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
385 /** Checks whether the path up to pos is correct. If the path contains a NULL,
386 * assumes the path is not complete and returns non-zero. */
387 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
389 /* A value of a compound entity is a pair of a value and the description of the
390 corresponding access path to the member of the compound. */
391 void add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path);
392 void set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos);
393 /** Returns the number of constant values needed to initialize the entity.
395 * Asserts if the entity has variability_uninitialized.
397 int get_compound_ent_n_values(entity *ent);
398 /** Returns a constant value given the position. */
399 ir_node *get_compound_ent_value(entity *ent, int pos);
400 /** Returns the access path for value at position pos. */
401 compound_graph_path *get_compound_ent_value_path(entity *ent, int pos);
402 /** Returns the position of a value with the given path.
403 * The path must contain array indicees for all array element entities. */
404 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path);
405 /** Returns a constant value given the access path.
406 * The path must contain array indicees for all array element entities. */
407 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path);
409 /** Removes all constant entries where the path ends at value_ent. Does not
410 free the memory of the paths. (The same path might be used for several
411 constant entities. */
412 void remove_compound_ent_value(entity *ent, entity *value_ent);
414 /* Some languages support only trivial access paths, i.e., the member is a
415 direct, atomic member of the constant entities type. In this case the
416 corresponding entity can be accessed directly. The following functions
417 allow direct access. */
419 /** Generates a Path with length 1.
420 Beware: Has a bad runtime for array elements (O(|array|) and should be
421 avoided there. Use add_compound_ent_value_w_path() instead and create
422 the path manually. */
423 void add_compound_ent_value(entity *ent, ir_node *val, entity *member);
425 /** Returns the last member in the path */
426 entity *get_compound_ent_value_member(entity *ent, int pos);
428 /** Sets the path at pos 0 */
429 void set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
431 /** Initializes the entity ent which must be of a one dimensional
432 array type with the values given in the values array.
433 The array must have a lower and an upper bound. Keeps the
434 order of values. Does not test whether the number of values
435 fits into the given array size. Does not test whether the
436 values have the proper mode for the array. */
437 void set_array_entity_values(entity *ent, tarval **values, int num_vals);
439 /** Return the overall offset of value at position pos in bits.
441 * This requires that the layout of all concerned types is fixed.
443 * @param ent Any entity of compound type with at least pos initialization values.
444 * @param pos The position of the value for which the offset is requested.
446 int get_compound_ent_value_offset_bits(entity *ent, int pos);
448 /** Return the overall offset of value at position pos in bytes.
450 * This requires that the layout of all concerned types is fixed.
451 * Asserts if bit offset is not byte aligned.
453 * @param ent Any entity of compound type with at least pos initialization values.
454 * @param pos The position of the value for which the offset is requested.
456 int get_compound_ent_value_offset_bytes(entity *ent, int pos);
458 /** Compute the array indicees in compound graph paths of initialized entities.
460 * All arrays must have fixed lower and upper bounds. One array can
461 * have an open upper bound. If there are several open bounds, we do
462 * nothing. There must be initializer elements for all array
463 * elements. Uses the link field in the array element entities. The
464 * array bounds must be representable as integers.
466 * @param ent Any entity.
468 void compute_compound_ent_array_indicees(entity *ent);
470 /** Sort the values of the compound entity by their overall offset.
472 * This requires that the layout of all concerned types is fixed.
473 * If the entity has no initialization information the method just
474 * returns. This is needed to dump the entity in a backend.
476 * @param ent Any entity.
478 void sort_compound_ent_values(entity *ent);
481 /* --- Fields of entities with a class type as owner --- */
482 /* Overwrites is a field that specifies that an access to the overwritten
483 entity in the supertype must use this entity. It's a list as with
484 multiple inheritance several entities can be overwritten. This field
485 is mostly useful for method entities.
486 If a Sel node selects an entity that is overwritten by other entities it
487 must return a pointer to the entity of the dynamic type of the pointer
488 that is passed to it. Lowering of the Sel node must assure this.
489 Overwrittenby is the inverse of overwrites. Both add routines add
490 both relations, they only differ in the order of arguments. */
491 void add_entity_overwrites (entity *ent, entity *overwritten);
492 int get_entity_n_overwrites (entity *ent);
493 int get_entity_overwrites_index(entity *ent, entity *overwritten);
494 entity *get_entity_overwrites (entity *ent, int pos);
495 void set_entity_overwrites (entity *ent, int pos, entity *overwritten);
496 void remove_entity_overwrites(entity *ent, entity *overwritten);
498 void add_entity_overwrittenby (entity *ent, entity *overwrites);
499 int get_entity_n_overwrittenby (entity *ent);
500 int get_entity_overwrittenby_index(entity *ent, entity *overwrites);
501 entity *get_entity_overwrittenby (entity *ent, int pos);
502 void set_entity_overwrittenby (entity *ent, int pos, entity *overwrites);
503 void remove_entity_overwrittenby(entity *ent, entity *overwrites);
506 * Checks whether a pointer points to an entity.
508 * @param thing an arbitrary pointer
511 * true if the thing is an entity, else false
513 int is_entity (const void *thing);
515 /** Returns true if the type of the entity is a primitive, pointer
516 enumeration or method type. */
517 int is_atomic_entity(entity *ent);
518 /** Returns true if the type of the entity is a class, structure,
519 array or union type. */
520 int is_compound_entity(entity *ent);
521 /** Returns true if the type of the entity is a Method type. */
522 int is_method_entity(entity *ent);
524 /** Returns non-zero if ent1 and ent2 have are equal except for their owner.
525 Two entities are equal if
526 - they have the same type (the same C-struct)
529 int equal_entity(entity *ent1, entity *ent2);
531 /** Outputs a unique number for this entity if libfirm is compiled for
532 * debugging, (configure with --enable-debug) else returns the address
533 * of the type cast to long.
535 long get_entity_nr(entity *ent);
537 /** Returns the entities visited count. */
538 unsigned long get_entity_visited(entity *ent);
540 /** Sets the entities visited count. */
541 void set_entity_visited(entity *ent, unsigned long num);
543 /** Sets visited field in entity to entity_visited. */
544 void mark_entity_visited(entity *ent);
546 /** Returns true if this entity was visited. */
547 int entity_visited(entity *ent);
549 /** Returns true if this entity was not visited. */
550 int entity_not_visited(entity *ent);
553 * Returns the mask of the additional entity properties.
554 * The properties are automatically inherited from the irg if available
555 * or from the method type if they were not set using
556 * set_entity_additional_properties() or
557 * set_entity_additional_property().
559 unsigned get_entity_additional_properties(entity *ent);
561 /** Sets the mask of the additional graph properties. */
562 void set_entity_additional_properties(entity *ent, unsigned property_mask);
564 /** Sets one additional graph property. */
565 void set_entity_additional_property(entity *ent, mtp_additional_property flag);
568 * @page unknown_entity
570 * This entity is an auxiliary entity dedicated to support analyses.
572 * The unknown entity represents that there could be an entity, but it is not
573 * known. This entity can be used to initialize fields before an analysis (not known
574 * yet) or to represent the top of a lattice (could not be determined). There exists
575 * exactly one entity unknown. This entity has as owner and as type the unknown type. It is
576 * allocated when initializing the entity module.
578 * The entity can take the role of any entity, also methods. It returns default
579 * values in these cases.
581 * The following values are set:
582 * name = "unknown_entity"
583 * ld_name = "unknown_entity"
584 * owner = unknown_type
585 * type = unknown_type
586 * allocation = allocation_automatic
587 * visibility = visibility_external_allocated
589 * variability = variability_uninitialized
590 * value = SymConst(unknown_entity)
593 * peculiarity = peculiarity_existent
594 * volatility = volatility_non_volatile
595 * stickyness = stickyness_unsticky
598 * overwrittenby = NULL
602 /* A variable that contains the only unknown entity. */
603 extern entity *unknown_entity;
605 /** Returns the unknown entity */
606 entity *get_unknown_entity(void);
608 /** Encodes how a pointer parameter is accessed. */
609 typedef enum acc_bits {
610 ptr_access_none = 0, /**< no access */
611 ptr_access_read = 1, /**< read access */
612 ptr_access_write = 2, /**< write access */
613 ptr_access_rw = ptr_access_read|ptr_access_write, /**< read AND write access */
614 ptr_access_store = 4, /**< the pointer is stored */
615 ptr_access_all = ptr_access_rw|ptr_access_store /**< all possible access */
618 #define IS_READ(a) ((a) & ptr_access_read)
619 #define IS_WRITTEN(a) ((a) & ptr_access_write)
620 #define IS_STORED(a) ((a) & ptr_access_store)
623 * Supported image sections.
624 * Currently only methods can be placed in different sections.
627 section_text, /**< The code segment. This is the default for methods. */
628 section_constructors /**< The constructor section. */
631 /** Returns the section of a method. */
632 ir_img_section get_method_img_section(const entity *method);
634 /** Sets the section of a method. */
635 void set_method_img_section(entity *method, ir_img_section section);
637 #endif /* _FIRM_TR_ENTITY_H_ */