2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * File name: ir/tr/entity.h
23 * Purpose: Representation of all program known entities.
24 * Author: Martin Trapp, Christian Schaefer
25 * Modified by: Goetz Lindenmaier, Michael Beck
28 * Copyright: (c) 1998-2006 Universität Karlsruhe
34 * Entities represent all program known objects.
36 * @author Martin Trapp, Christian Schaefer
37 * @author Goetz Lindenmaier
39 * An entity is the representation of program known objects in Firm.
40 * The primary concept of entities is to represent members of complex
41 * types, i.e., fields and methods of classes. As not all programming
42 * language model all variables and methods as members of some class,
43 * the concept of entities is extended to cover also local and global
44 * variables, and arbitrary procedures.
46 * An entity always specifies the type of the object it represents and
47 * the type of the object it is a part of, the owner of the entity.
48 * Originally this is the type of the class of which the entity is a
50 * The owner of local variables is the procedure they are defined in.
51 * The owner of global variables and procedures visible in the whole
52 * program is a universally defined class type "GlobalType". The owner
53 * of procedures defined in the scope of an other procedure is the
54 * enclosing procedure.
58 #ifndef _FIRM_TR_ENTITY_H_
59 #define _FIRM_TR_ENTITY_H_
61 #include "firm_types.h"
64 #include "tr_inheritance.h"
66 /*-----------------------------------------------------------------*/
68 /*-----------------------------------------------------------------*/
71 * @page ir_entity Representation of an program entity.
73 * The type ir_entity is an abstract data type to represent program entities.
74 * If contains the following attributes:
76 * - owner: A compound type this entity is a part of.
77 * - type: The type of this entity.
78 * - name: The string that represents this entity in the source program.
79 * - allocation: A flag saying whether the entity is dynamically or statically
80 * allocated (values: dynamic_allocated, static_allocated,
81 * automatic_allocated).
82 * - visibility: A flag indicating the visibility of this entity (values: local,
83 * external_visible, external_allocated)
84 * - variability: A flag indicating the variability of this entity (values:
85 * uninitialized, initialized, part_constant, constant)
87 * - offset: The offset of the entity within the compound object in bytes. Only set
88 * if the owner in the state "layout_fixed".
89 * - offset_bits_remainder: The offset bit remainder of a bitfield entity (in a compound)
90 * in bits. Only set if the owner in the state "layout_fixed".
91 * - overwrites: A list of entities overwritten by this entity. This list is only
92 * existent if the owner of this entity is a class. The members in
93 * this list must be entities of super classes.
94 * - overwrittenby: A list of entities that overwrite this entity. This list is only
95 * existent if the owner of this entity is a class. The members in
96 * this list must be entities of sub classes.
97 * - link: A void* to associate some additional information with the entity.
98 * - irg: If the entity is a method this is the ir graph that represents the
100 * - peculiarity: The peculiarity of the entity. If the entity is a method this
101 * indicates whether the entity represents
102 * a real method or whether it only exists to describe an interface.
103 * In that case there nowhere exists code for this entity and this entity
104 * is never dynamically used in the code.
105 * Values: description, existent. Default: existent.
106 * - visited: visited flag. Master flag is type_visited.
108 * These fields can only be accessed via access functions.
113 /* to resolve recursion between entity.h and type.h */
114 /** the type of an entity */
115 #ifndef _ENTITY_TYPEDEF_
116 #define _ENTITY_TYPEDEF_
117 typedef struct ir_entity ir_entity, entity;
121 * Creates a new entity.
123 * Automatically inserts the entity as a member of owner.
124 * Entity is automatic_allocated and uninitialized except if the type
125 * is type_method, then it is static_allocated and constant. The constant
126 * value is a pointer to the method.
127 * Visibility is local, offset -1, and it is not volatile.
129 ir_entity *new_entity(ir_type *owner, ident *name, ir_type *tp);
132 * Creates a new entity.
134 * Automatically inserts the entity as a member of owner.
135 * The entity is automatic allocated and uninitialized except if the type
136 * is type_method, then it is static allocated and constant. The constant
137 * value is a pointer to the method.
138 * Visibility is local, offset -1, and it is not volatile.
140 ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *tp, dbg_info *db);
143 * Copies the entity if the new_owner is different from the
144 * owner of the old entity, else returns the old entity.
146 * Automatically inserts the new entity as a member of owner.
147 * Resets the overwrites/overwritten_by fields.
148 * Keeps the old atomic value.
149 * @@@ Maybe we should change this. If peculiarity of a method
150 * is existent, we should add a new SymConst that points to
151 * itself and not to the origin. Right now we have to change
152 * the peculiarity and then set a new atomic value by hand.
154 ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner);
157 * Copies the entity if the new_name is different from the
158 * name of the old entity, else returns the old entity.
160 * Automatically inserts the new entity as a member of owner.
161 * The mangled name ld_name is set to NULL.
162 * Overwrites relation is copied from old.
164 ir_entity *copy_entity_name(ir_entity *old, ident *new_name);
169 * The owner will still contain the pointer to this
170 * entity, as well as all other references!
172 void free_entity(ir_entity *ent);
174 /** Returns the name of an entity. */
175 const char *get_entity_name(const ir_entity *ent);
177 /** Returns the ident of an entity. */
178 ident *get_entity_ident(const ir_entity *ent);
180 /** Sets the ident of the entity. */
181 void set_entity_ident(ir_entity *ent, ident *id);
183 /** Returns the mangled name of the entity.
185 * If the mangled name is set it returns the existing name.
186 * Else it generates a name with mangle_entity()
187 * and remembers this new name internally.
189 ident *get_entity_ld_ident(ir_entity *ent);
191 /** Sets the mangled name of the entity. */
192 void set_entity_ld_ident(ir_entity *ent, ident *ld_ident);
194 /** Returns the mangled name of the entity as a string. */
195 const char *get_entity_ld_name(ir_entity *ent);
197 /** Returns the owner of the entity. */
198 ir_type *get_entity_owner(ir_entity *ent);
200 /** Sets the owner field in entity to owner. Don't forget to add
202 void set_entity_owner(ir_entity *ent, ir_type *owner);
204 /** Returns the type of an entity. */
205 ir_type *get_entity_type(ir_entity *ent);
207 /** Sets the type of an entity. */
208 void set_entity_type(ir_entity *ent, ir_type *tp);
210 /** The allocation type. */
212 allocation_automatic, /**< The entity is allocated during runtime, implicitly
213 as component of a compound type. This is the default. */
214 allocation_parameter, /**< The entity is a parameter. It is also automatic allocated.
215 We distinguish the allocation of parameters from the allocation
216 of local variables as their placement depends on the calling
218 allocation_dynamic, /**< The entity is allocated during runtime, explicitly
220 allocation_static /**< The entity is allocated statically. We can use a
221 Const as address of the entity. This is the default for methods. */
224 /** Returns the allocation type of an entity. */
225 ir_allocation get_entity_allocation(const ir_entity *ent);
227 /** Sets the allocation type of an entity. */
228 void set_entity_allocation(ir_entity *ent, ir_allocation al);
230 /** Return the name of the allocation type. */
231 const char *get_allocation_name(ir_allocation vis);
233 /** Returns the visibility of an entity. */
234 ir_visibility get_entity_visibility(const ir_entity *ent);
236 /** Sets the visibility of an entity. */
237 void set_entity_visibility(ir_entity *ent, ir_visibility vis);
239 /** Return the name of the visibility */
240 const char *get_visibility_name(ir_visibility vis);
242 /** This enumeration flags the variability of entities. */
244 variability_uninitialized, /**< The content of the entity is completely unknown. Default. */
245 variability_initialized, /**< After allocation the entity is initialized with the
246 value given somewhere in the entity. */
247 variability_part_constant, /**< For entities of compound types.
248 The members of the entity are mixed constant,
249 initialized or uninitialized. */
250 variability_constant /**< The entity is constant. */
253 /** Returns the variability of an entity. */
254 ir_variability get_entity_variability(const ir_entity *ent);
256 /** Sets the variability of an entity. */
257 void set_entity_variability(ir_entity *ent, ir_variability var);
259 /** Return the name of the variability. */
260 const char *get_variability_name(ir_variability var);
262 /** This enumeration flags the volatility of entities. */
264 volatility_non_volatile, /**< The entity is not volatile. Default. */
265 volatility_is_volatile /**< The entity is volatile */
268 /** Returns the volatility of an entity. */
269 ir_volatility get_entity_volatility(const ir_entity *ent);
271 /** Sets the volatility of an entity. */
272 void set_entity_volatility(ir_entity *ent, ir_volatility vol);
274 /** Return the name of the volatility. */
275 const char *get_volatility_name(ir_volatility var);
277 /** This enumeration flags the stickyness of an entity. */
279 stickyness_unsticky, /**< The entity can be removed from
280 the program, unless contraindicated
281 by other attributes. Default. */
282 stickyness_sticky /**< The entity must remain in the
283 program in any case. */
286 /** Get the entity's stickyness. */
287 ir_stickyness get_entity_stickyness(const ir_entity *ent);
289 /** Set the entity's stickyness. */
290 void set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness);
292 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
293 int get_entity_offset(const ir_entity *ent);
295 /** Sets the offset of an entity (in a compound) in bytes. */
296 void set_entity_offset(ir_entity *ent, int offset);
298 /** Returns the offset bit remainder of a bitfield entity (in a compound) in bits. Only set if layout = fixed. */
299 unsigned char get_entity_offset_bits_remainder(const ir_entity *ent);
301 /** Sets the offset bit remainder of a bitfield entity (in a compound) in bits. */
302 void set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset);
304 /** Returns the stored intermediate information. */
305 void *get_entity_link(const ir_entity *ent);
307 /** Stores new intermediate information. */
308 void set_entity_link(ir_entity *ent, void *l);
310 /* -- Fields of method entities -- */
311 /** The entity knows the corresponding irg if the entity is a method.
312 This allows to get from a Call to the called irg.
313 Only entities of peculiarity "existent" can have a corresponding irg,
314 else the field is fixed to NULL. (Get returns NULL, set asserts.) */
315 ir_graph *get_entity_irg(const ir_entity *ent);
316 void set_entity_irg(ir_entity *ent, ir_graph *irg);
318 /** Gets the entity vtable number. */
319 unsigned get_entity_vtable_number(const ir_entity *ent);
321 /** Sets the entity vtable number. */
322 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number);
324 /** Return the peculiarity of an entity. */
325 ir_peculiarity get_entity_peculiarity(const ir_entity *ent);
327 /** Sets the peculiarity of an entity. */
328 void set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec);
330 /** Checks if an entity cannot be overridden anymore. */
331 int is_entity_final(const ir_entity *ent);
333 /** Sets/resets the final flag of an entity. */
334 void set_entity_final(ir_entity *ent, int final);
336 /** Checks if an entity is compiler generated. */
337 int is_entity_compiler_generated(const ir_entity *ent);
339 /** Sets/resets the compiler generated flag. */
340 void set_entity_compiler_generated(ir_entity *ent, int flag);
343 * The state of the address_taken flag.
346 ir_address_not_taken = 0, /**< The address is NOT taken. */
347 ir_address_taken_unknown = 1, /**< The state of the address taken flag is unknown. */
348 ir_address_taken = 2 /**< The address IS taken. */
349 } ir_address_taken_state;
351 /** Return the state of the address taken flag of an entity. */
352 ir_address_taken_state get_entity_address_taken(const ir_entity *ent);
354 /** Sets/resets the state of the address taken flag of an entity. */
355 void set_entity_address_taken(ir_entity *ent, ir_address_taken_state flag);
357 /** Return the name of the address_taken state. */
358 const char *get_address_taken_state_name(ir_address_taken_state state);
360 /* -- Representation of constant values of entities -- */
362 * Returns true if the the node is representable as code on
365 * @deprecated This function is not used by libFirm and stays here
366 * only as a helper for the old Jack frontend.
368 int is_irn_const_expression(ir_node *n);
371 * Copies a Firm subgraph that complies to the restrictions for
372 * constant expressions to current_block in current_ir_graph.
374 * @param dbg debug info for all newly created nodes
377 * Set current_ir_graph to get_const_code_irg() to generate a constant
380 ir_node *copy_const_value(dbg_info *dbg, ir_node *n);
382 /* Set has no effect for existent entities of type method. */
383 ir_node *get_atomic_ent_value(ir_entity *ent);
384 void set_atomic_ent_value(ir_entity *ent, ir_node *val);
387 * The following type describes a path to a leave in the compound graph.
388 * Node 0 in the path must be an entity of type tp given in the constructor. If
389 * the type of this element is compound, the path node 1 is an element of the type
390 * of node 0 an so forth, until an entity of atomic type is reached.
392 #ifndef _COMPOUND_GRAPH_PATH_TYPEDEF_
393 #define _COMPOUND_GRAPH_PATH_TYPEDEF_
394 typedef struct compound_graph_path compound_graph_path;
395 #endif /* _COMPOUND_GRAPH_PATH_TYPEDEF_ */
397 /** Creates a new compound graph path. */
398 compound_graph_path *new_compound_graph_path(ir_type *tp, int length);
400 /** Returns non-zero if an object is a compound graph path */
401 int is_compound_graph_path(const void *thing);
403 /** Frees a graph path object */
404 void free_compound_graph_path (compound_graph_path *gr);
406 /** Returns the length of a graph path */
407 int get_compound_graph_path_length(const compound_graph_path *gr);
409 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr, int pos);
410 void set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node);
411 int get_compound_graph_path_array_index(const compound_graph_path *gr, int pos);
412 void set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
414 /** Checks whether the path up to pos is correct. If the path contains a NULL,
415 * assumes the path is not complete and returns non-zero. */
416 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
418 /* A value of a compound entity is a pair of a value and the description of the
419 corresponding access path to the member of the compound. */
420 void add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path);
421 void set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos);
422 /** Returns the number of constant values needed to initialize the entity.
424 * Asserts if the entity has variability_uninitialized.
426 int get_compound_ent_n_values(ir_entity *ent);
427 /** Returns a constant value given the position. */
428 ir_node *get_compound_ent_value(ir_entity *ent, int pos);
429 /** Returns the access path for value at position pos. */
430 compound_graph_path *get_compound_ent_value_path(ir_entity *ent, int pos);
431 /** Returns a constant value given the access path.
432 * The path must contain array indices for all array element entities. */
433 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path);
435 /** Removes all constant entries where the path ends at value_ent. Does not
436 free the memory of the paths. (The same path might be used for several
437 constant entities. */
438 void remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent);
440 /* Some languages support only trivial access paths, i.e., the member is a
441 direct, atomic member of the constant entities type. In this case the
442 corresponding entity can be accessed directly. The following functions
443 allow direct access. */
445 /** Generates a Path with length 1.
446 Beware: Has a bad runtime for array elements (O(|array|) and should be
447 avoided there. Use add_compound_ent_value_w_path() instead and create
448 the path manually. */
449 void add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member);
451 /** Returns the last member in the path */
452 ir_entity *get_compound_ent_value_member(ir_entity *ent, int pos);
454 /** Sets the path at pos 0 */
455 void set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos);
457 /** Initializes the entity ent which must be of a one dimensional
458 array type with the values given in the values array.
459 The array must have a lower and an upper bound. Keeps the
460 order of values. Does not test whether the number of values
461 fits into the given array size. Does not test whether the
462 values have the proper mode for the array. */
463 void set_array_entity_values(ir_entity *ent, tarval **values, int num_vals);
466 * Return the offset in bits from the last byte address.
468 * This requires that the layout of all concerned types is fixed.
470 * @param ent Any entity of compound type with at least pos initialization values.
471 * @param pos The position of the value for which the offset is requested.
473 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos);
475 /** Return the overall offset of value at position pos in bytes.
477 * This requires that the layout of all concerned types is fixed.
478 * Asserts if bit offset is not byte aligned.
480 * @param ent Any entity of compound type with at least pos initialization values.
481 * @param pos The position of the value for which the offset is requested.
483 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos);
485 /* --- Fields of entities with a class type as owner --- */
486 /* Overwrites is a field that specifies that an access to the overwritten
487 entity in the supertype must use this entity. It's a list as with
488 multiple inheritance several entities can be overwritten. This field
489 is mostly useful for method entities.
490 If a Sel node selects an entity that is overwritten by other entities it
491 must return a pointer to the entity of the dynamic type of the pointer
492 that is passed to it. Lowering of the Sel node must assure this.
493 Overwrittenby is the inverse of overwrites. Both add routines add
494 both relations, they only differ in the order of arguments. */
495 void add_entity_overwrites (ir_entity *ent, ir_entity *overwritten);
496 int get_entity_n_overwrites (ir_entity *ent);
497 int get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten);
498 ir_entity *get_entity_overwrites (ir_entity *ent, int pos);
499 void set_entity_overwrites (ir_entity *ent, int pos, ir_entity *overwritten);
500 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten);
502 void add_entity_overwrittenby (ir_entity *ent, ir_entity *overwrites);
503 int get_entity_n_overwrittenby (ir_entity *ent);
504 int get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites);
505 ir_entity *get_entity_overwrittenby (ir_entity *ent, int pos);
506 void set_entity_overwrittenby (ir_entity *ent, int pos, ir_entity *overwrites);
507 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites);
510 * Checks whether a pointer points to an entity.
512 * @param thing an arbitrary pointer
515 * true if the thing is an entity, else false
517 int is_entity (const void *thing);
519 /** Returns true if the type of the entity is a primitive, pointer
520 * enumeration or method type.
522 * @Note This is a different classification than from is_primitive_type().
524 int is_atomic_entity(ir_entity *ent);
525 /** Returns true if the type of the entity is a class, structure,
526 array or union type. */
527 int is_compound_entity(ir_entity *ent);
528 /** Returns true if the type of the entity is a Method type. */
529 int is_method_entity(ir_entity *ent);
531 /** Returns non-zero if ent1 and ent2 have are equal except for their owner.
532 Two entities are equal if
533 - they have the same type (the same C-struct)
536 int equal_entity(ir_entity *ent1, ir_entity *ent2);
538 /** Outputs a unique number for this entity if libfirm is compiled for
539 * debugging, (configure with --enable-debug) else returns the address
540 * of the type cast to long.
542 long get_entity_nr(const ir_entity *ent);
544 /** Returns the entities visited count. */
545 unsigned long get_entity_visited(ir_entity *ent);
547 /** Sets the entities visited count. */
548 void set_entity_visited(ir_entity *ent, unsigned long num);
550 /** Sets visited field in entity to entity_visited. */
551 void mark_entity_visited(ir_entity *ent);
553 /** Returns true if this entity was visited. */
554 int entity_visited(ir_entity *ent);
556 /** Returns true if this entity was not visited. */
557 int entity_not_visited(ir_entity *ent);
560 * Returns the mask of the additional entity properties.
561 * The properties are automatically inherited from the irg if available
562 * or from the method type if they were not set using
563 * set_entity_additional_properties() or
564 * set_entity_additional_property().
566 unsigned get_entity_additional_properties(ir_entity *ent);
568 /** Sets the mask of the additional graph properties. */
569 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask);
571 /** Sets one additional graph property. */
572 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag);
574 /** Returns the class type that this type info entity represents or NULL
575 if ent is no type info entity. */
576 ir_type *get_entity_repr_class(const ir_entity *ent);
579 * @page unknown_entity The Unknown entity
581 * This entity is an auxiliary entity dedicated to support analyses.
583 * The unknown entity represents that there could be an entity, but it is not
584 * known. This entity can be used to initialize fields before an analysis (not known
585 * yet) or to represent the top of a lattice (could not be determined). There exists
586 * exactly one entity unknown. This entity has as owner and as type the unknown type. It is
587 * allocated when initializing the entity module.
589 * The entity can take the role of any entity, also methods. It returns default
590 * values in these cases.
592 * The following values are set:
594 * - name = "unknown_entity"
595 * - ld_name = "unknown_entity"
596 * - owner = unknown_type
597 * - type = unknown_type
598 * - allocation = allocation_automatic
599 * - visibility = visibility_external_allocated
601 * - variability = variability_uninitialized
602 * - value = SymConst(unknown_entity)
605 * - peculiarity = peculiarity_existent
606 * - volatility = volatility_non_volatile
607 * - stickyness = stickyness_unsticky
609 * - overwrites = NULL
610 * - overwrittenby = NULL
614 /* A variable that contains the only unknown entity. */
615 extern ir_entity *unknown_entity;
617 /** Returns the @link unknown_entity unknown entity @endlink. */
618 ir_entity *get_unknown_entity(void);
620 /** Encodes how a pointer parameter is accessed. */
621 typedef enum acc_bits {
622 ptr_access_none = 0, /**< no access */
623 ptr_access_read = 1, /**< read access */
624 ptr_access_write = 2, /**< write access */
625 ptr_access_rw = ptr_access_read|ptr_access_write, /**< read AND write access */
626 ptr_access_store = 4, /**< the pointer is stored */
627 ptr_access_all = ptr_access_rw|ptr_access_store /**< all possible access */
630 #define IS_READ(a) ((a) & ptr_access_read)
631 #define IS_WRITTEN(a) ((a) & ptr_access_write)
632 #define IS_STORED(a) ((a) & ptr_access_store)
635 * Supported image sections.
636 * Currently only methods can be placed in different sections.
639 section_text, /**< The code segment. This is the default for methods. */
640 section_constructors /**< The constructor section. */
643 /** Returns the section of a method. */
644 ir_img_section get_method_img_section(const ir_entity *method);
646 /** Sets the section of a method. */
647 void set_method_img_section(ir_entity *method, ir_img_section section);
649 #endif /* _FIRM_TR_ENTITY_H_ */