2 * Copyright (C) 1995-2008 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
19 #ifndef FIRM_TYPEREP_H
20 #define FIRM_TYPEREP_H
22 #include "firm_types.h"
25 * @page entity Entity representation
27 * An entity is the representation of program known objects in Firm.
28 * The primary concept of entities is to represent members of complex
29 * types, i.e., fields and methods of classes. As not all programming
30 * language model all variables and methods as members of some class,
31 * the concept of entities is extended to cover also local and global
32 * variables, and arbitrary procedures.
34 * An entity always specifies the type of the object it represents and
35 * the type of the object it is a part of, the owner of the entity.
36 * Originally this is the type of the class of which the entity is a
38 * The owner of local variables is the procedure they are defined in.
39 * The owner of global variables and procedures visible in the whole
40 * program is a universally defined class type "GlobalType". The owner
41 * of procedures defined in the scope of an other procedure is the
42 * enclosing procedure.
44 * The type ir_entity is an abstract data type to represent program entities.
45 * If contains the following attributes:
47 * - owner: A compound type this entity is a part of.
48 * - type: The type of this entity.
49 * - name: The string that represents this entity in the source program. * - allocation: A flag saying whether the entity is dynamically or statically * allocated (values: dynamic_allocated, static_allocated,
50 * automatic_allocated).
51 * - visibility: A flag indicating the visibility of this entity (values: local,
52 * external_visible, external_allocated)
53 * - variability: A flag indicating the variability of this entity (values:
54 * uninitialized, initialized, part_constant, constant)
56 * - offset: The offset of the entity within the compound object in bytes. Only set
57 * if the owner in the state "layout_fixed".
58 * - offset_bits_remainder: The offset bit remainder of a bitfield entity (in a compound)
59 * in bits. Only set if the owner in the state "layout_fixed".
60 * - overwrites: A list of entities overwritten by this entity. This list is only
61 * existent if the owner of this entity is a class. The members in
62 * this list must be entities of super classes.
63 * - overwrittenby: A list of entities that overwrite this entity. This list is only
64 * existent if the owner of this entity is a class. The members in
65 * this list must be entities of sub classes.
66 * - link: A void* to associate some additional information with the entity.
67 * - irg: If the entity is a method this is the ir graph that represents the
69 * - peculiarity: The peculiarity of the entity. If the entity is a method this
70 * indicates whether the entity represents
71 * a real method or whether it only exists to describe an interface.
72 * In that case there nowhere exists code for this entity and this entity
73 * is never dynamically used in the code.
74 * Values: description, existent. Default: existent.
75 * - visited: visited flag. Master flag is type_visited.
77 * These fields can only be accessed via access functions.
79 * @see ir_type, ir_entity
82 /** This enumeration flags the visibility of entities and types.
84 * This is necessary for partial compilation.
85 * We rely on the ordering of the flags.
88 visibility_local, /**< The entity is only visible locally. This is the default for
90 The type is only visible locally. All instances are allocated
91 locally, and no pointer to entities of this type are passed
92 out of this compilation unit. */
93 visibility_external_visible, /**< The entity is visible to other external program parts, but
94 it is defined here. It may not be optimized away. The entity must
96 For types: entities of this type can be accessed externally. No
97 instances of this type are allocated externally. */
98 visibility_external_allocated /**< The entity is defined and allocated externally. This compilation
99 must not allocate memory for this entity. The entity must
100 be static_allocated. This can also be an external defined
102 For types: entities of this type are allocated and accessed from
103 external code. Default for types. */
106 /** This enumeration flags the peculiarity of entities and types. */
108 peculiarity_description, /**< Represents only a description. The entity/type is never
109 allocated, no code/data exists for this entity/type.
110 @@@ eventually rename to descriptive (adjective as the others!)*/
111 peculiarity_inherited, /**< Describes explicitly that other entities are
112 inherited to the owner of this entity.
113 Overwrites must refer to at least one other
114 entity. If this is a method entity there exists
115 no irg for this entity, only for one of the
118 peculiarity_existent /**< The entity/type (can) exist.
119 @@@ eventually rename to 'real' i.e., 'echt'
120 This serves better as opposition to description _and_ inherited.*/
124 * Additional method type properties:
125 * Tell about special properties of a method type. Some
126 * of these may be discovered by analyses.
129 mtp_no_property = 0x00000000, /**< no additional properties, default */
130 mtp_property_const = 0x00000001, /**< This method did not access memory and calculates
131 its return values solely from its parameters.
132 GCC: __attribute__((const)). */
133 mtp_property_pure = 0x00000002, /**< This method did NOT write to memory and calculates
134 its return values solely from its parameters and
135 the memory they points to (or global vars).
136 GCC: __attribute__((pure)). */
137 mtp_property_noreturn = 0x00000004, /**< This method did not return due to an aborting system
139 GCC: __attribute__((noreturn)). */
140 mtp_property_nothrow = 0x00000008, /**< This method cannot throw an exception.
141 GCC: __attribute__((nothrow)). */
142 mtp_property_naked = 0x00000010, /**< This method is naked.
143 GCC: __attribute__((naked)). */
144 mtp_property_malloc = 0x00000020, /**< This method returns newly allocate memory.
145 GCC: __attribute__((malloc)). */
146 mtp_property_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
147 a lowering phase will remove all calls to it. */
148 mtp_property_runtime = 0x00000080, /**< This method represents a runtime routine. */
149 mtp_property_private = 0x00000100, /**< All method invocations are known, the backend is free to
150 optimize the call in any possible way. */
151 mtp_property_inherited = (1<<31) /**< Internal. Used only in irg's, means property is
152 inherited from type. */
153 } mtp_additional_property;
156 * Creates a new entity.
158 * Automatically inserts the entity as a member of owner.
159 * Entity is automatic_allocated and uninitialized except if the type
160 * is type_method, then it is static_allocated and constant. The constant
161 * value is a pointer to the method.
162 * Visibility is local, offset -1, and it is not volatile.
164 ir_entity *new_entity(ir_type *owner, ident *name, ir_type *tp);
167 * Creates a new entity.
169 * Automatically inserts the entity as a member of owner.
170 * The entity is automatic allocated and uninitialized except if the type
171 * is type_method, then it is static allocated and constant. The constant
172 * value is a pointer to the method.
173 * Visibility is local, offset -1, and it is not volatile.
175 ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *tp, dbg_info *db);
178 * Copies the entity if the new_owner is different from the
179 * owner of the old entity, else returns the old entity.
181 * Automatically inserts the new entity as a member of owner.
182 * Resets the overwrites/overwritten_by fields.
183 * Keeps the old atomic value.
184 * @@@ Maybe we should change this. If peculiarity of a method
185 * is existent, we should add a new SymConst that points to
186 * itself and not to the origin. Right now we have to change
187 * the peculiarity and then set a new atomic value by hand.
189 ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner);
192 * Copies the entity if the new_name is different from the
193 * name of the old entity, else returns the old entity.
195 * Automatically inserts the new entity as a member of owner.
196 * The mangled name ld_name is set to NULL.
197 * Overwrites relation is copied from old.
199 ir_entity *copy_entity_name(ir_entity *old, ident *new_name);
204 * The owner will still contain the pointer to this
205 * entity, as well as all other references!
207 void free_entity(ir_entity *ent);
209 /** Returns the name of an entity. */
210 const char *get_entity_name(const ir_entity *ent);
212 /** Returns the ident of an entity. */
213 ident *get_entity_ident(const ir_entity *ent);
215 /** Sets the ident of the entity. */
216 void set_entity_ident(ir_entity *ent, ident *id);
218 /** Returns the mangled name of the entity.
220 * If the mangled name is set it returns the existing name.
221 * Else it generates a name with mangle_entity()
222 * and remembers this new name internally.
224 ident *get_entity_ld_ident(ir_entity *ent);
226 /** Sets the mangled name of the entity. */
227 void set_entity_ld_ident(ir_entity *ent, ident *ld_ident);
229 /** Returns the mangled name of the entity as a string. */
230 const char *get_entity_ld_name(ir_entity *ent);
232 /** Returns the owner of the entity. */
233 ir_type *get_entity_owner(ir_entity *ent);
235 /** Sets the owner field in entity to owner. Don't forget to add
237 void set_entity_owner(ir_entity *ent, ir_type *owner);
239 /** Returns the type of an entity. */
240 ir_type *get_entity_type(ir_entity *ent);
242 /** Sets the type of an entity. */
243 void set_entity_type(ir_entity *ent, ir_type *tp);
245 /** The allocation type. */
247 allocation_automatic, /**< The entity is allocated during runtime, implicitly
248 as component of a compound type. This is the default. */
249 allocation_parameter, /**< The entity is a parameter. It is also automatic allocated.
250 We distinguish the allocation of parameters from the allocation
251 of local variables as their placement depends on the calling
253 allocation_dynamic, /**< The entity is allocated during runtime, explicitly
255 allocation_static /**< The entity is allocated statically. We can use a
256 Const as address of the entity. This is the default for methods. */
259 /** Returns the allocation type of an entity. */
260 ir_allocation get_entity_allocation(const ir_entity *ent);
262 /** Sets the allocation type of an entity. */
263 void set_entity_allocation(ir_entity *ent, ir_allocation al);
265 /** Return the name of the allocation type. */
266 const char *get_allocation_name(ir_allocation vis);
268 /** Returns the visibility of an entity. */
269 ir_visibility get_entity_visibility(const ir_entity *ent);
271 /** Sets the visibility of an entity. */
272 void set_entity_visibility(ir_entity *ent, ir_visibility vis);
274 /** Return the name of the visibility */
275 const char *get_visibility_name(ir_visibility vis);
277 /** This enumeration flags the variability of entities. */
279 variability_uninitialized, /**< The content of the entity is completely unknown. Default. */
280 variability_initialized, /**< After allocation the entity is initialized with the
281 value given somewhere in the entity. */
282 variability_part_constant, /**< For entities of compound types.
283 The members of the entity are mixed constant,
284 initialized or uninitialized. */
285 variability_constant /**< The entity is constant. */
288 /** Returns the variability of an entity. */
289 ir_variability get_entity_variability(const ir_entity *ent);
291 /** Sets the variability of an entity. */
292 void set_entity_variability(ir_entity *ent, ir_variability var);
294 /** Return the name of the variability. */
295 const char *get_variability_name(ir_variability var);
297 /** This enumeration flags the volatility of entities and Loads/Stores. */
299 volatility_non_volatile, /**< The entity is not volatile. Default. */
300 volatility_is_volatile /**< The entity is volatile. */
303 /** Returns the volatility of an entity. */
304 ir_volatility get_entity_volatility(const ir_entity *ent);
306 /** Sets the volatility of an entity. */
307 void set_entity_volatility(ir_entity *ent, ir_volatility vol);
309 /** Return the name of the volatility. */
310 const char *get_volatility_name(ir_volatility var);
312 /** This enumeration flags the align of Loads/Stores. */
314 align_non_aligned, /**< The entity is not aligned. */
315 align_is_aligned /**< The entity is aligned. Default */
318 /** Returns the alignment of an entity. */
319 ir_align get_entity_align(const ir_entity *ent);
321 /** Sets the alignment of an entity. */
322 void set_entity_align(ir_entity *ent, ir_align a);
324 /** Return the name of the alignment. */
325 const char *get_align_name(ir_align a);
327 /** This enumeration flags the stickyness of an entity. */
329 stickyness_unsticky, /**< The entity can be removed from
330 the program, unless contraindicated
331 by other attributes. Default. */
332 stickyness_sticky /**< The entity must remain in the
333 program in any case. There might be external
337 /** Get the entity's stickyness. */
338 ir_stickyness get_entity_stickyness(const ir_entity *ent);
340 /** Set the entity's stickyness. */
341 void set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness);
343 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
344 int get_entity_offset(const ir_entity *ent);
346 /** Sets the offset of an entity (in a compound) in bytes. */
347 void set_entity_offset(ir_entity *ent, int offset);
349 /** Returns the offset bit remainder of a bitfield entity (in a compound) in bits. Only set if layout = fixed. */
350 unsigned char get_entity_offset_bits_remainder(const ir_entity *ent);
352 /** Sets the offset bit remainder of a bitfield entity (in a compound) in bits. */
353 void set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset);
355 /** Returns the stored intermediate information. */
356 void *get_entity_link(const ir_entity *ent);
358 /** Stores new intermediate information. */
359 void set_entity_link(ir_entity *ent, void *l);
361 /* -- Fields of method entities -- */
362 /** The entity knows the corresponding irg if the entity is a method.
363 This allows to get from a Call to the called irg.
364 Only entities of peculiarity "existent" can have a corresponding irg,
365 else the field is fixed to NULL. (Get returns NULL, set asserts.) */
366 ir_graph *get_entity_irg(const ir_entity *ent);
367 void set_entity_irg(ir_entity *ent, ir_graph *irg);
369 /** Gets the entity vtable number. */
370 unsigned get_entity_vtable_number(const ir_entity *ent);
372 /** Sets the entity vtable number. */
373 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number);
375 /** Return the peculiarity of an entity. */
376 ir_peculiarity get_entity_peculiarity(const ir_entity *ent);
378 /** Sets the peculiarity of an entity. */
379 void set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec);
381 /** Checks if an entity cannot be overridden anymore. */
382 int is_entity_final(const ir_entity *ent);
384 /** Sets/resets the final flag of an entity. */
385 void set_entity_final(ir_entity *ent, int final);
387 /** Checks if an entity is compiler generated. */
388 int is_entity_compiler_generated(const ir_entity *ent);
390 /** Sets/resets the compiler generated flag. */
391 void set_entity_compiler_generated(ir_entity *ent, int flag);
393 /** Checks if an entity is marked by the backend. */
394 int is_entity_backend_marked(const ir_entity *ent);
396 /** Sets/resets the backend marker flag. */
397 void set_entity_backend_marked(ir_entity *ent, int flag);
400 * Bitfield type indicating the way an entity is used.
403 ir_usage_address_taken = 1 << 0,
404 ir_usage_write = 1 << 1,
405 ir_usage_read = 1 << 2,
406 ir_usage_reinterpret_cast = 1 << 3,
408 = ir_usage_address_taken | ir_usage_write | ir_usage_read
409 | ir_usage_reinterpret_cast
412 /** Return the entity usage */
413 ir_entity_usage get_entity_usage(const ir_entity *ent);
415 /** Sets/resets the state of the address taken flag of an entity. */
416 void set_entity_usage(ir_entity *ent, ir_entity_usage flag);
419 * Returns the debug information of an entity.
421 * @param ent The entity.
423 dbg_info *get_entity_dbg_info(const ir_entity *ent);
426 * Sets the debug information of an entity.
428 * @param ent The entity.
429 * @param db The debug info.
431 void set_entity_dbg_info(ir_entity *ent, dbg_info *db);
433 /* -- Representation of constant values of entities -- */
435 * Returns true if the the node is representable as code on
438 * @deprecated This function is not used by libFirm and stays here
439 * only as a helper for the old Jack frontend.
441 int is_irn_const_expression(ir_node *n);
444 * Copies a Firm subgraph that complies to the restrictions for
445 * constant expressions to current_block in current_ir_graph.
447 * @param dbg debug info for all newly created nodes
450 * Set current_ir_graph to get_const_code_irg() to generate a constant
453 ir_node *copy_const_value(dbg_info *dbg, ir_node *n);
455 /* Set has no effect for existent entities of type method. */
456 ir_node *get_atomic_ent_value(ir_entity *ent);
457 void set_atomic_ent_value(ir_entity *ent, ir_node *val);
459 /** the kind (type) of an initializer */
460 typedef enum ir_initializer_kind_t {
461 /** initializer containing an ir_node from the const-code irg */
462 IR_INITIALIZER_CONST,
463 /** initializer containing a tarval */
464 IR_INITIALIZER_TARVAL,
465 /** initializes type with default values (usually 0) */
467 /** list of initializers used to initializer a compound or array type */
468 IR_INITIALIZER_COMPOUND
469 } ir_initializer_kind_t;
471 /** returns kind of an initializer */
472 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer);
475 * returns the null initializer (there's only one instance of it in a program )
477 ir_initializer_t *get_initializer_null(void);
480 * creates an initializer containing a reference to a node on the const-code
483 ir_initializer_t *create_initializer_const(ir_node *value);
485 /** creates an initializer containing a single tarval value */
486 ir_initializer_t *create_initializer_tarval(tarval *tv);
488 /** return value contained in a const initializer */
489 ir_node *get_initializer_const_value(const ir_initializer_t *initializer);
491 /** return value contained in a tarval initializer */
492 tarval *get_initializer_tarval_value(const ir_initializer_t *initialzier);
494 /** creates a compound initializer which holds @p n_entries entries */
495 ir_initializer_t *create_initializer_compound(unsigned n_entries);
497 /** returns the number of entries in a compound initializer */
498 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer);
500 /** sets entry with index @p index to the initializer @p value */
501 void set_initializer_compound_value(ir_initializer_t *initializer,
502 unsigned index, ir_initializer_t *value);
504 /** returns the value with index @p index of a compound initializer */
505 ir_initializer_t *get_initializer_compound_value(
506 const ir_initializer_t *initializer, unsigned index);
508 /** Creates a new compound graph path of given length. */
509 compound_graph_path *new_compound_graph_path(ir_type *tp, int length);
511 /** Returns non-zero if an object is a compound graph path */
512 int is_compound_graph_path(const void *thing);
514 /** Frees a graph path object */
515 void free_compound_graph_path(compound_graph_path *gr);
517 /** Returns the length of a graph path */
518 int get_compound_graph_path_length(const compound_graph_path *gr);
520 /** Get the entity node of an compound graph path at position pos. */
521 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr, int pos);
522 /** Set the entity node of an compound graph path at position pos. */
523 void set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node);
524 /** Get the index of an compound graph path at position pos. */
525 int get_compound_graph_path_array_index(const compound_graph_path *gr, int pos);
526 /** Set the index of an compound graph path at position pos. */
527 void set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
528 /** Get the type of an compound graph path. */
529 ir_type *get_compound_graph_path_type(const compound_graph_path *gr);
531 /** Checks whether the path up to pos is correct. If the path contains a NULL,
532 * assumes the path is not complete and returns non-zero. */
533 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
535 /* A value of a compound entity is a pair of a value and the description of the
536 corresponding access path to the member of the compound. */
537 void add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path);
538 void set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos);
540 /** Returns the number of constant values needed to initialize the entity.
542 * Asserts if the entity has variability_uninitialized.
544 int get_compound_ent_n_values(ir_entity *ent);
545 /** Returns a constant value given the position. */
546 ir_node *get_compound_ent_value(ir_entity *ent, int pos);
547 /** Returns the access path for value at position pos. */
548 compound_graph_path *get_compound_ent_value_path(ir_entity *ent, int pos);
549 /** Returns a constant value given the access path.
550 * The path must contain array indices for all array element entities. */
551 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path);
553 /** Removes all constant entries where the path ends at value_ent. Does not
554 free the memory of the paths. (The same path might be used for several
555 constant entities. */
556 void remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent);
558 /* Some languages support only trivial access paths, i.e., the member is a
559 direct, atomic member of the constant entities type. In this case the
560 corresponding entity can be accessed directly. The following functions
561 allow direct access. */
563 /** Generates a Path with length 1.
564 Beware: Has a bad runtime for array elements (O(|array|) and should be
565 avoided there. Use add_compound_ent_value_w_path() instead and create
566 the path manually. */
567 void add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member);
569 /** Returns the last member in the path */
570 ir_entity *get_compound_ent_value_member(ir_entity *ent, int pos);
572 /** Sets the path at pos 0 */
573 void set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos);
575 /** Sets the new style initializers of an entity. */
576 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer);
578 /** Return the new style initializers of an entity. */
579 ir_initializer_t *get_entity_initializer(const ir_entity *entity);
581 /** Initializes the entity ent which must be of a one dimensional
582 array type with the values given in the values array.
583 The array must have a lower and an upper bound. Keeps the
584 order of values. Does not test whether the number of values
585 fits into the given array size. Does not test whether the
586 values have the proper mode for the array. */
587 void set_array_entity_values(ir_entity *ent, tarval **values, int num_vals);
590 * Return the offset in bits from the last byte address.
592 * This requires that the layout of all concerned types is fixed.
594 * @param ent Any entity of compound type with at least pos initialization values.
595 * @param pos The position of the value for which the offset is requested.
597 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos);
599 /** Return the overall offset of value at position pos in bytes.
601 * This requires that the layout of all concerned types is fixed.
602 * Asserts if bit offset is not byte aligned.
604 * @param ent Any entity of compound type with at least pos initialization values.
605 * @param pos The position of the value for which the offset is requested.
607 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos);
609 /* --- Fields of entities with a class type as owner --- */
610 /* Overwrites is a field that specifies that an access to the overwritten
611 entity in the supertype must use this entity. It's a list as with
612 multiple inheritance several entities can be overwritten. This field
613 is mostly useful for method entities.
614 If a Sel node selects an entity that is overwritten by other entities it
615 must return a pointer to the entity of the dynamic type of the pointer
616 that is passed to it. Lowering of the Sel node must assure this.
617 Overwrittenby is the inverse of overwrites. Both add routines add
618 both relations, they only differ in the order of arguments. */
619 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten);
620 int get_entity_n_overwrites(ir_entity *ent);
621 int get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten);
622 ir_entity *get_entity_overwrites(ir_entity *ent, int pos);
623 void set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten);
624 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten);
626 void add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites);
627 int get_entity_n_overwrittenby(ir_entity *ent);
628 int get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites);
629 ir_entity *get_entity_overwrittenby(ir_entity *ent, int pos);
630 void set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites);
631 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites);
634 * Checks whether a pointer points to an entity.
636 * @param thing an arbitrary pointer
639 * true if the thing is an entity, else false
641 int is_entity(const void *thing);
643 /** Returns true if the type of the entity is a primitive, pointer
644 * enumeration or method type.
646 * @Note This is a different classification than from is_primitive_type().
648 int is_atomic_entity(ir_entity *ent);
649 /** Returns true if the type of the entity is a class, structure,
650 array or union type. */
651 int is_compound_entity(ir_entity *ent);
652 /** Returns true if the type of the entity is a Method type. */
653 int is_method_entity(ir_entity *ent);
655 /** Returns non-zero if ent1 and ent2 have are equal except for their owner.
656 Two entities are equal if
657 - they have the same type (the same C-struct)
660 int equal_entity(ir_entity *ent1, ir_entity *ent2);
662 /** Outputs a unique number for this entity if libfirm is compiled for
663 * debugging, (configure with --enable-debug) else returns the address
664 * of the type cast to long.
666 long get_entity_nr(const ir_entity *ent);
668 /** Returns the entities visited count. */
669 ir_visited_t get_entity_visited(ir_entity *ent);
671 /** Sets the entities visited count. */
672 void set_entity_visited(ir_entity *ent, ir_visited_t num);
674 /** Sets visited field in entity to entity_visited. */
675 void mark_entity_visited(ir_entity *ent);
677 /** Returns true if this entity was visited. */
678 int entity_visited(ir_entity *ent);
680 /** Returns true if this entity was not visited. */
681 int entity_not_visited(ir_entity *ent);
684 * Returns the mask of the additional entity properties.
685 * The properties are automatically inherited from the irg if available
686 * or from the method type if they were not set using
687 * set_entity_additional_properties() or
688 * set_entity_additional_property().
690 unsigned get_entity_additional_properties(ir_entity *ent);
692 /** Sets the mask of the additional graph properties. */
693 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask);
695 /** Sets one additional graph property. */
696 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag);
698 /** Returns the class type that this type info entity represents or NULL
699 if ent is no type info entity. */
700 ir_type *get_entity_repr_class(const ir_entity *ent);
703 * @page unknown_entity The Unknown entity
705 * This entity is an auxiliary entity dedicated to support analyses.
707 * The unknown entity represents that there could be an entity, but it is not
708 * known. This entity can be used to initialize fields before an analysis (not known
709 * yet) or to represent the top of a lattice (could not be determined). There exists
710 * exactly one entity unknown. This entity has as owner and as type the unknown type. It is
711 * allocated when initializing the entity module.
713 * The entity can take the role of any entity, also methods. It returns default
714 * values in these cases.
716 * The following values are set:
718 * - name = "unknown_entity"
719 * - ld_name = "unknown_entity"
720 * - owner = unknown_type
721 * - type = unknown_type
722 * - allocation = allocation_automatic
723 * - visibility = visibility_external_allocated
725 * - variability = variability_uninitialized
726 * - value = SymConst(unknown_entity)
729 * - peculiarity = peculiarity_existent
730 * - volatility = volatility_non_volatile
731 * - stickyness = stickyness_unsticky
733 * - overwrites = NULL
734 * - overwrittenby = NULL
738 /* A variable that contains the only unknown entity. */
739 extern ir_entity *unknown_entity;
741 /** Returns the @link unknown_entity unknown entity @endlink. */
742 ir_entity *get_unknown_entity(void);
744 /** Encodes how a pointer parameter is accessed. */
745 typedef enum acc_bits {
746 ptr_access_none = 0, /**< no access */
747 ptr_access_read = 1, /**< read access */
748 ptr_access_write = 2, /**< write access */
749 ptr_access_rw = ptr_access_read|ptr_access_write, /**< read AND write access */
750 ptr_access_store = 4, /**< the pointer is stored */
751 ptr_access_all = ptr_access_rw|ptr_access_store /**< all possible access */
754 #define IS_READ(a) ((a) & ptr_access_read)
755 #define IS_WRITTEN(a) ((a) & ptr_access_write)
756 #define IS_STORED(a) ((a) & ptr_access_store)
759 * @page tyop type operations
760 * This module specifies the kinds of types available in firm.
762 * They are called type opcodes. These include classes, structs, methods, unions,
763 * arrays, enumerations, pointers and primitive types.
764 * Special types with own opcodes are the id type, a type representing an unknown
765 * type and a type used to specify that something has no type.
771 * An enum for the type kinds.
772 * For each type kind exists a typecode to identify it.
775 tpo_uninitialized = 0, /* not a type opcode */
776 tpo_class, /**< A class type. */
777 tpo_struct, /**< A struct type. */
778 tpo_method, /**< A method type. */
779 tpo_union, /**< An union type. */
780 tpo_array, /**< An array type. */
781 tpo_enumeration, /**< An enumeration type. */
782 tpo_pointer, /**< A pointer type. */
783 tpo_primitive, /**< A primitive type. */
784 tpo_id, /**< Special Id tag used for type replacement. */
785 tpo_none, /**< Special type for the None type. */
786 tpo_unknown, /**< Special code for the Unknown type. */
787 tpo_max /* not a type opcode */
791 * A structure containing information about a kind of type.
792 * A structure containing information about a kind of type. So far
793 * this is only the kind name, an enum for case-switching and some
796 * @see get_tpop_name(), get_tpop_code(), get_tpop_ident()
798 typedef struct tp_op tp_op;
802 * Returns the string for the type opcode.
804 * @param op The type opcode to get the string from.
805 * @return a string. (@todo Null terminated?)
807 const char *get_tpop_name(const tp_op *op);
810 * Returns an enum for the type opcode.
812 * @param op The type opcode to get the enum from.
815 tp_opcode get_tpop_code(const tp_op *op);
818 * Returns the ident for the type opcode.
820 * @param op The type opcode to get the ident from.
823 ident *get_tpop_ident(const tp_op *op);
826 * This type opcode marks that the corresponding type is a class type.
828 * Consequently the type refers to supertypes, subtypes and entities.
829 * Entities can be any fields, but also methods.
830 * @@@ value class or not???
831 * This struct is dynamically allocated but constant for the lifetime
834 extern tp_op *type_class;
835 tp_op *get_tpop_class(void);
838 * This type opcode marks that the corresponding type is a compound type
841 * Consequently the type refers to a list of entities
842 * which may not be methods (but pointers to methods).
843 * This struct is dynamically allocated but constant for the lifetime
846 extern tp_op *type_struct;
847 tp_op *get_tpop_struct(void);
850 * This type opcode marks that the corresponding type is a method type.
852 * Consequently it refers to a list of arguments and results.
853 * This struct is dynamically allocated but constant for the lifetime
856 extern tp_op *type_method;
857 tp_op *get_tpop_method(void);
860 * This type opcode marks that the corresponding type is a union type.
862 * Consequently it refers to a list of unioned types.
863 * This struct is dynamically allocated but constant for the lifetime
866 extern tp_op *type_union;
867 tp_op *get_tpop_union(void);
870 * This type opcode marks that the corresponding type is an array type.
872 * Consequently it contains a list of dimensions (lower and upper bounds)
873 * and an element type.
874 * This struct is dynamically allocated but constant for the lifetime
877 extern tp_op *type_array;
878 tp_op *get_tpop_array(void);
881 * This type opcode marks that the corresponding type is an enumeration type.
883 * Consequently it contains a list of idents for the enumeration identifiers
884 * and a list of target values that are the constants used to implement
886 * This struct is dynamically allocated but constant for the lifetime
889 extern tp_op *type_enumeration;
890 tp_op *get_tpop_enumeration(void);
893 * This type opcode marks that the corresponding type is a pointer type.
895 * It contains a reference to the type the pointer points to.
896 * This struct is dynamically allocated but constant for the lifetime
899 extern tp_op *type_pointer;
900 tp_op *get_tpop_pointer(void);
903 * This type opcode marks that the corresponding type is a primitive type.
905 * Primitive types are types that are directly mapped to target machine
907 * This struct is dynamically allocated but constant for the lifetime
910 extern tp_op *type_primitive;
911 tp_op *get_tpop_primitive(void);
914 * This type opcode is an auxiliary opcode dedicated to support transformations
915 * of the type structure.
917 * If a type is changed to another type with another
918 * opcode the new type will be allocated with new memory. All nodes refering
919 * to the old type need to be changed to refer to the new one. This is simplified
920 * by turning the old type into an id type that merely forwards to the new type
921 * that now replaces the old one.
922 * type_ids should never be visible out of the type module. All access routines
923 * should automatically check for type_id and eventually follow the forward in
924 * type_id. Two types are exchanged by a call to exchange_types.
925 * If a type_id is visible externally report this as bug. If it is assured that
926 * this never happens this extern variable can be moved to tpop_t.h.
927 * This struct is dynamically allocated but constant for the lifetime
930 extern tp_op *type_id;
931 tp_op *get_tpop_id(void);
934 * This type opcode is an auxiliary opcode dedicated to support type analyses.
936 * Types with this opcode represents that there is no type.
937 * The type can be used to initialize fields of the type* that actually can not
938 * contain a type or that are initialized for an analysis. There exists exactly
939 * one type with this opcode.
941 extern tp_op *tpop_none;
942 tp_op *get_tpop_none(void);
945 * This type opcode is an auxiliary opcode dedicated to support type analyses.
947 * Types with this opcode represents that there could be a type, but it is not
948 * known. This type can be used to initialize fields before an analysis (not known
949 * yet) or to represent the top of a lattice (could not be determined). There exists
950 * exactly one type with this opcode.
952 extern tp_op *tpop_unknown;
953 tp_op *get_tpop_unknown(void);
955 /* ----------------------------------------------------------------------- */
956 /* Classify pairs of types/entities in the inheritance relations. */
957 /* ----------------------------------------------------------------------- */
959 /** Returns true if low is subclass of high.
961 * Low is a subclass of high if low == high or if low is a subclass of
962 * a subclass of high. I.e, we search in all subtypes of high for low.
963 * @@@ this can be implemented more efficient if we know the set of all
964 * subclasses of high. */
965 int is_SubClass_of(ir_type *low, ir_type *high);
967 /** Subclass check for pointers to classes.
969 * Dereferences at both types the same amount of pointer types (as
970 * many as possible). If the remaining types are both class types
971 * and subclasses, returns true, else false. Can also be called with
972 * two class types. */
973 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
975 /** Returns true if high is superclass of low.
977 * Low is a subclass of high if low == high or if low is a subclass of
978 * a subclass of high. I.e, we search in all subtypes of high for low.
979 * @@@ this can be implemented more efficient if we know the set of all
980 * subclasses of high. */
981 #define is_SuperClass_of(high, low) is_SubClass_of(low, high)
983 /** Superclass check for pointers to classes.
985 * Dereferences at both types the same amount of pointer types (as
986 * many as possible). If the remaining types are both class types
987 * and superclasses, returns true, else false. Can also be called with
988 * two class types. */
989 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
991 /** Returns true if high is (transitive) overwritten by low.
993 * Returns false if high == low. */
994 int is_overwritten_by(ir_entity *high, ir_entity *low);
996 /** Resolve polymorphism in the inheritance relation.
998 * Returns the dynamically referenced entity if the static entity and the
999 * dynamic type are given.
1000 * Searches downwards in overwritten tree. */
1001 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity* static_ent);
1003 /* ----------------------------------------------------------------------- */
1004 /* Resolve implicit inheritance. */
1005 /* ----------------------------------------------------------------------- */
1007 /** Default name mangling for inherited entities.
1009 * Returns an ident that consists of the name of type followed by an
1010 * underscore and the name (not ld_name) of the entity. */
1011 ident *default_mangle_inherited_name(ir_entity *ent, ir_type *clss);
1013 /** Type of argument functions for inheritance resolver.
1015 * @param ent The entity in the super type that will be overwritten
1016 * by the newly generated entity, for which this name is
1018 * @param clss The class type in which the new entity will be placed.
1020 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
1022 /** Resolve implicit inheritance.
1024 * Resolves the implicit inheritance supplied by firm. Firm defines,
1025 * that each entity that is not overwritten in a subclass is
1026 * inherited to this subclass without change implicitly. This
1027 * function generates entities that explicitly represent this
1028 * inheritance. It generates for each entity overwriting entities in
1029 * all subclasses of the owner of the entity, if the entity is not
1030 * overwritten in that subclass.
1032 * The name of the new entity is generated with the function passed.
1033 * If the function is NULL, the default_mangle_inherited_name() is
1036 * This function was moved here from firmlower 3/2005.
1038 void resolve_inheritance(mangle_inherited_name_func *mfunc);
1041 /* ----------------------------------------------------------------------- */
1042 /* The transitive closure of the subclass/superclass and */
1043 /* overwrites/overwrittenby relation. */
1045 /* A walk over the ir (O(#types+#entities)) computes the transitive */
1046 /* closure. Adding a new type/entity or changing the basic relations in */
1047 /* some other way invalidates the transitive closure, i.e., it is not */
1048 /* updated by the basic functions. */
1050 /* The transitive edges are held in a set, not in an array as the */
1051 /* underlying relation. */
1053 /* Do the sets contain the node itself? I assume NOT! */
1054 /* ----------------------------------------------------------------------- */
1056 /** The state of the transitive closure.
1058 * @todo: we could manage the state for each relation separately. Invalidating
1059 * the entity relations does not mean invalidating the class relation. */
1061 inh_transitive_closure_none, /**< Closure is not computed, can not be accessed. */
1062 inh_transitive_closure_valid, /**< Closure computed and valid. */
1063 inh_transitive_closure_invalid, /**< Closure invalid, but can be accessed. */
1064 inh_transitive_closure_max /**< Invalid value. */
1065 } inh_transitive_closure_state;
1067 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
1068 void invalidate_irp_inh_transitive_closure_state(void);
1069 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
1072 /** Compute transitive closure of the subclass/superclass and
1073 * overwrites/overwrittenby relation.
1075 * This function walks over the ir (O(#types+#entities)) to compute the
1076 * transitive closure. */
1077 void compute_inh_transitive_closure(void);
1079 /** Free memory occupied by the transitive closure information. */
1080 void free_inh_transitive_closure(void);
1083 /* - subtype ------------------------------------------------------------- */
1085 /** Iterate over all transitive subtypes. */
1086 ir_type *get_class_trans_subtype_first(const ir_type *tp);
1087 ir_type *get_class_trans_subtype_next(const ir_type *tp);
1088 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp);
1090 /* - supertype ----------------------------------------------------------- */
1092 /** Iterate over all transitive supertypes. */
1093 ir_type *get_class_trans_supertype_first(const ir_type *tp);
1094 ir_type *get_class_trans_supertype_next(const ir_type *tp);
1096 /* - overwrittenby ------------------------------------------------------- */
1098 /** Iterate over all entities that transitive overwrite this entities. */
1099 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent);
1100 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent);
1102 /* - overwrites ---------------------------------------------------------- */
1104 /** Iterate over all transitive overwritten entities. */
1105 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent);
1106 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent);
1109 /* ----------------------------------------------------------------------- */
1110 /** The state of Cast operations that cast class types or pointers to class
1113 * The state expresses, how far Cast operations conform with the class
1117 * class B1 extends A {}
1118 * class B2 extends A {}
1119 * class C extends B1 {}
1120 * normalized: Cast operations conform with the inheritance relation.
1121 * I.e., the type of the operand of a Cast is either a super= or a sub-
1122 * type of the type casted to. Example: (A)((B2) (new C())).
1123 * transitive: Cast operations conform with the transitive inheritance
1124 * relation. Example: (A)(new C()).
1125 * any: Cast operations do not conform with the transitive inheritance
1126 * relation. Example: (B2)(new B1())
1130 /* ----------------------------------------------------------------------- */
1132 /** Flags for class cast state.
1134 * The state in irp is always smaller or equal to the state of any
1137 * We rely on the ordering of the enum. */
1139 ir_class_casts_any = 0, /**< There are class casts that do not cast in conformance with
1140 the class hierarchy. @@@ So far this does not happen in Firm. */
1141 ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
1142 ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
1143 ir_class_casts_state_max
1144 } ir_class_cast_state;
1145 char *get_class_cast_state_string(ir_class_cast_state s);
1147 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
1148 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
1149 void set_irp_class_cast_state(ir_class_cast_state s);
1150 ir_class_cast_state get_irp_class_cast_state(void);
1152 /** Verify the class cast state of an irg.
1154 * Asserts if state is to high, outputs warning if state is to low
1155 * and firm verbosity is set.
1157 void verify_irg_class_cast_state(ir_graph *irg);
1160 * possible trvrfy() error codes
1162 enum trvrfy_error_codes {
1163 no_error = 0, /**< no error */
1164 error_ent_not_cont, /**< overwritten entity not in superclass */
1165 error_null_mem, /**< compound contains NULL member */
1166 error_const_on_wrong_irg, /**< constant placed on wrong IRG */
1167 error_existent_entity_without_irg, /**< Method entities with pecularity_exist must have an irg */
1168 error_wrong_ent_overwrites, /**< number of entity overwrites exceeds number of class overwrites */
1169 error_inherited_ent_without_const, /**< inherited method entity not pointing to existent entity */
1170 error_glob_ent_allocation, /**< wrong allocation of a global entity */
1171 error_ent_const_mode, /**< Mode of constant in entity did not match entities type. */
1172 error_ent_wrong_owner /**< Mode of constant in entity did not match entities type. */
1179 * 0 if no error encountered
1181 int check_type(ir_type *tp);
1184 * Check an entity. Currently, we check only if initialized constants
1185 * are build on the const irg graph.
1188 * 0 if no error encountered
1189 * != 0 a trvrfy_error_codes code
1191 int check_entity(ir_entity *ent);
1194 * Walks the type information and performs a set of sanity checks.
1196 * Currently, the following checks are executed:
1197 * - values of initialized entities must be allocated on the constant IRG
1198 * - class types: doesn't have NULL members
1199 * - class types: all overwrites are existent in the super type
1202 * 0 if graph is correct
1208 * If NDEBUG is defined performs nothing, else calls the tr_vrfy() function.
1213 #define TR_VRFY() tr_vrfy()
1219 * This module supplies routines that support changing the type graph.
1222 /** Replaces one type by the other.
1224 * Old type is replaced by new_type. All references to old_type
1225 * now point to new_type. The memory for the old type is destroyed,
1226 * but still used. Therefore it is not freed.
1227 * All referenced to this memory will be lost after a certain while.
1228 * An exception is the list of types in irp (irprog.h).
1229 * In the future there might be a routine to recover the memory, but
1230 * this will be at considerable runtime cost.
1232 * @param old_type - The old type that shall be replaced by the new type.
1233 * @param new_type - The new type that will replace old_type.
1236 void exchange_types(ir_type *old_type, ir_type *new_type);
1238 /** Skip id types until a useful type is reached.
1240 * @param tp - A type of arbitrary kind.
1243 * tp if it is not an id type.
1244 * If tp is an id type returns the real type it stands for.
1246 ir_type *skip_tid(ir_type *tp);
1249 * @page type representation of types
1251 * Datastructure to hold type information.
1253 * This module supplies a datastructure to represent all types
1254 * known in the compiled program. This includes types specified
1255 * in the program as well as types defined by the language. In the
1256 * view of the intermediate representation there is no difference
1257 * between these types. Finally it specifies some auxiliary types.
1259 * There exist several kinds of types, arranged by the structure of
1260 * the type. A type is described by a set of attributes. Some of
1261 * these attributes are common to all types, others depend on the
1264 * Types are different from the modes defined in irmode: Types are
1265 * on the level of the programming language, modes at the level of
1266 * the target processor.
1271 #include "typerep.h"
1273 /** Frees all entities associated with a type.
1274 * Does not free the array entity.
1275 * Warning: ensure these entities are not referenced anywhere else.
1277 void free_type_entities(ir_type *tp);
1279 /** Frees the memory used by the type.
1281 * Removes the type from the type list. Does not free the entities
1282 * belonging to the type, except for the array element entity. Does
1283 * not free if tp is "none" or "unknown". Frees entities in value
1284 * param subtypes of method types!!! Make sure these are not
1285 * referenced any more. Further make sure there is no pointer type
1286 * that refers to this type. */
1287 void free_type(ir_type *tp);
1289 const tp_op *get_type_tpop(const ir_type *tp);
1290 ident *get_type_tpop_nameid(const ir_type *tp);
1291 const char *get_type_tpop_name(const ir_type *tp);
1292 tp_opcode get_type_tpop_code(const ir_type *tp);
1294 ident *get_type_ident(const ir_type *tp);
1295 void set_type_ident(ir_type *tp, ident* id);
1296 const char *get_type_name(const ir_type *tp);
1298 /** The visibility of a type.
1300 * The visibility of a type indicates, whether entities of this type
1301 * are accessed or allocated in external code.
1303 * An entity of a type is allocated in external code, if the external
1304 * code declares a variable of this type, or dynamically allocates
1305 * an entity of this type. If the external code declares a (compound)
1306 * type, that contains entities of this type, the visibility also
1307 * must be external_allocated.
1309 * The visibility must be higher than that of all entities, if the
1310 * type is a compound. Here it is questionable, what happens with
1311 * static entities. If these are accessed external by direct reference,
1312 * (a static call to a method, that is also in the dispatch table)
1313 * it should not affect the visibility of the type.
1316 * @@@ Do we need a visibility for types?
1317 * I change the layout of types radically when doing type splitting.
1318 * I need to know, which fields of classes are accessed in the RTS,
1319 * e.g., [_length. I may not move [_length to the split part.
1320 * The layout though, is a property of the type.
1322 * One could also think of changing the mode of a type ...
1324 * But, we could also output macros to access the fields, e.g.,
1325 * ACCESS_[_length (X) X->length // conventional
1326 * ACCESS_[_length (X) X->_split_ref->length // with type splitting
1328 * For now I implement this function, that returns the visibility
1329 * based on the visibility of the entities of a compound ...
1331 * This function returns visibility_external_visible if one or more
1332 * entities of a compound type have visibility_external_visible.
1333 * Entities of types are never visibility_external_allocated (right?).
1334 * Else returns visibility_local.
1336 ir_visibility get_type_visibility(const ir_type *tp);
1337 void set_type_visibility(ir_type *tp, ir_visibility v);
1341 /** The state of the type layout. */
1343 layout_undefined, /**< The layout of this type is not defined.
1344 Address computation to access fields is not
1345 possible, fields must be accessed by Sel
1346 nodes. Enumeration constants might be undefined.
1347 This is the default value except for
1348 pointer, primitive and method types. */
1349 layout_fixed /**< The layout is fixed, all component/member entities
1350 have an offset assigned. Size of the type is known.
1351 Arrays can be accessed by explicit address
1352 computation. Enumeration constants must be defined.
1353 Default for pointer, primitive and method types. */
1356 /** Returns a human readable string for the enum entry. */
1357 const char *get_type_state_name(ir_type_state s);
1359 /** Returns the type layout state of a type. */
1360 ir_type_state get_type_state(const ir_type *tp);
1362 /** Sets the type layout state of a type.
1364 * For primitives, pointer and method types the layout is always fixed.
1365 * This call is legal but has no effect.
1367 void set_type_state(ir_type *tp, ir_type_state state);
1369 /** Returns the mode of a type.
1371 * Returns NULL for all non atomic types.
1373 ir_mode *get_type_mode(const ir_type *tp);
1375 /** Sets the mode of a type.
1377 * Only has an effect on primitive, enumeration and pointer types.
1379 void set_type_mode(ir_type *tp, ir_mode* m);
1381 /** Returns the size of a type in bytes. */
1382 unsigned get_type_size_bytes(const ir_type *tp);
1384 /** Sets the size of a type in bytes.
1386 * For primitive, enumeration, pointer and method types the size
1387 * is always fixed. This call is legal but has no effect.
1389 void set_type_size_bytes(ir_type *tp, unsigned size);
1391 /** Returns the alignment of a type in bytes. */
1392 unsigned get_type_alignment_bytes(ir_type *tp);
1394 /** Returns the alignment of a type in bits.
1396 * If the alignment of a type is
1397 * not set, it is calculated here according to the following rules:
1398 * -#.) if a type has a mode, the alignment is the mode size.
1399 * -#.) compound types have the alignment of there biggest member.
1400 * -#.) array types have the alignment of there element type.
1401 * -#.) method types return 0 here.
1402 * -#.) all other types return 1 here (i.e. aligned at byte).
1404 void set_type_alignment_bytes(ir_type *tp, unsigned align);
1406 /** Returns the visited count of a type. */
1407 ir_visited_t get_type_visited(const ir_type *tp);
1408 /** Sets the visited count of a type to num. */
1409 void set_type_visited(ir_type *tp, ir_visited_t num);
1410 /** Sets visited field in type to type_visited. */
1411 void mark_type_visited(ir_type *tp);
1412 /** Returns non-zero if the type is already visited */
1413 int type_visited(const ir_type *tp);
1414 /** Returns non-zero if the type is not yet visited */
1415 int type_not_visited(const ir_type *tp);
1417 /** Returns the associated link field of a type. */
1418 void *get_type_link(const ir_type *tp);
1419 /** Sets the associated link field of a type. */
1420 void set_type_link(ir_type *tp, void *l);
1423 * Visited flag to traverse the type information.
1425 * Increase this flag by one before traversing the type information
1426 * using inc_master_type_visited().
1427 * Mark type nodes as visited by mark_type_visited(ir_type).
1428 * Check whether node was already visited by type_visited(ir_type)
1429 * and type_not_visited(ir_type).
1430 * Or use the function to walk all types.
1434 void set_master_type_visited(ir_visited_t val);
1435 ir_visited_t get_master_type_visited(void);
1436 void inc_master_type_visited(void);
1439 * Sets the debug information of a type.
1441 * @param tp The type.
1442 * @param db The debug info.
1444 void set_type_dbg_info(ir_type *tp, dbg_info *db);
1447 * Returns the debug information of a type.
1449 * @param tp The type.
1451 dbg_info *get_type_dbg_info(const ir_type *tp);
1454 * Checks whether a pointer points to a type.
1456 * @param thing an arbitrary pointer
1459 * true if the thing is a type, else false
1461 int is_type(const void *thing);
1464 * Checks whether two types are structurally equal.
1466 * @param typ1 the first type
1467 * @param typ2 the second type
1470 * true if the types are equal, else false.
1472 * Types are equal if :
1473 * - they are the same type kind
1474 * - they have the same name
1475 * - they have the same mode (if applicable)
1476 * - they have the same type_state and, ev., the same size
1477 * - they are class types and have:
1478 * - the same members (see same_entity in entity.h)
1479 * - the same supertypes -- the C-pointers are compared --> no recursive call.
1480 * - the same number of subtypes. Subtypes are not compared,
1481 * as this could cause a cyclic test.
1482 * - the same peculiarity
1483 * - they are structure types and have the same members
1484 * - they are method types and have
1485 * - the same parameter types
1486 * - the same result types
1487 * - they are union types and have the same members
1488 * - they are array types and have
1489 * - the same number of dimensions
1490 * - the same dimension bounds
1491 * - the same dimension order
1492 * - the same element type
1493 * - they are enumeration types and have the same enumerator names
1494 * - they are pointer types and have the identical points_to type
1495 * (i.e., the same C-struct to represent the type, type_id is skipped.
1496 * This is to avoid endless recursions; with pointer types cyclic
1497 * type graphs are possible.)
1499 int equal_type(ir_type *typ1, ir_type *typ2);
1502 * Checks whether two types are structural comparable.
1504 * @param st pointer type
1505 * @param lt pointer type
1508 * true if type st is smaller than type lt, i.e. whenever
1509 * lt is expected a st can be used.
1511 * - they are the same type kind
1512 * - mode(st) < mode (lt) (if applicable)
1513 * - they are class types and st is (transitive) subtype of lt,
1514 * - they are structure types and
1515 * - the members of st have exactly one counterpart in lt with the same name,
1516 * - the counterpart has a bigger type.
1517 * - they are method types and have
1518 * - the same number of parameter and result types,
1519 * - the parameter types of st are smaller than those of lt,
1520 * - the result types of st are smaller than those of lt
1521 * - they are union types and have the members of st have exactly one
1522 * @return counterpart in lt and the type is smaller
1523 * - they are array types and have
1524 * - the same number of dimensions
1525 * - all bounds of lt are bound of st
1526 * - the same dimension order
1527 * - the same element type
1529 * - the element type of st is smaller than that of lt
1530 * - the element types have the same size and fixed layout.
1531 * - they are enumeration types and have the same enumerator names
1532 * - they are pointer types and have the points_to type of st is
1533 * @return smaller than the points_to type of lt.
1536 int smaller_type(ir_type *st, ir_type *lt);
1539 * @page class_type Representation of a class type
1541 * If the type opcode is set to type_class the type represents class
1542 * types. A list of fields and methods is associated with a class.
1543 * Further a class can inherit from and bequest to other classes.
1545 * The following attributes are private to this type kind:
1546 * - member: All entities belonging to this class. This are method entities
1547 * which have type_method or fields that can have any of the
1548 * following type kinds: type_class, type_struct, type_union,
1549 * type_array, type_enumeration, type_pointer, type_primitive.
1551 * The following two are dynamic lists that can be grown with an "add_" function,
1554 * - subtypes: A list of direct subclasses.
1556 * - supertypes: A list of direct superclasses.
1558 * - peculiarity: The peculiarity of this class. If the class is of peculiarity
1559 * "description" it only is a description of requirements to a class,
1560 * as, e.g., a Java interface. The class will never be allocated.
1561 * Peculiarity inherited is only possible for entities. An entity
1562 * is of peculiarity inherited if the compiler generated the entity
1563 * to explicitly resolve inheritance. An inherited method entity has
1565 * Values: description, existent, inherited. Default: existent.
1567 * - type_info: An entity representing the type information of this class.
1568 * This entity can be of arbitrari type, Firm did not use it yet.
1569 * It allows to express the coupling of a type with an entity
1570 * representing this type. This information is useful for lowering
1571 * of InstOf and TypeChk nodes. Default: NULL
1573 * - vtable_size: The size of this class virtual function table.
1576 * - final: A final class is always a leaf in the class hierarchy. Final
1577 * classes cannot be super classes of other ones. As this information
1578 * can only be computed in whole world compilations, we allow to
1579 * set this flag. It is used in optimizations if get_opt_closed_world()
1580 * is false. Default: false
1582 * - interface: The class represents an interface. This flag can be set to distinguish
1583 * between interfaces, abstract classes and other classes that all may
1584 * have the peculiarity peculiarity_description. Depending on this flag
1585 * the lowering might do different actions. Default: false
1587 * - abstract : The class represents an abstract class. This flag can be set to distinguish
1588 * between interfaces, abstract classes and other classes that all may
1589 * have the peculiarity peculiarity_description. Depending on this flag
1590 * the lowering might do different actions. Default: false
1593 /** Creates a new class type. */
1594 ir_type *new_type_class(ident *name);
1596 /** Creates a new class type with debug information. */
1597 ir_type *new_d_type_class(ident *name, dbg_info *db);
1599 /* --- manipulate private fields of class type --- */
1601 /** Adds the entity as member of the class. */
1602 void add_class_member(ir_type *clss, ir_entity *member);
1604 /** Returns the number of members of this class. */
1605 int get_class_n_members(const ir_type *clss);
1607 /** Returns the member at position pos, 0 <= pos < n_member */
1608 ir_entity *get_class_member(const ir_type *clss, int pos);
1610 /** Returns index of mem in clss, -1 if not contained. */
1611 int get_class_member_index(const ir_type *clss, ir_entity *mem);
1613 /** Finds the member with name 'name'. If several members with the same
1614 * name returns one of them. Returns NULL if no member found. */
1615 ir_entity *get_class_member_by_name(ir_type *clss, ident *name);
1617 /** Overwrites the member at position pos, 0 <= pos < n_member with
1618 * the passed entity. */
1619 void set_class_member(ir_type *clss, ir_entity *member, int pos);
1621 /** Replaces complete member list in class type by the list passed.
1623 * Copies the list passed. This function is necessary to reduce the number of members.
1624 * members is an array of entities, num the size of this array. Sets all
1625 * owners of the members passed to clss. */
1626 void set_class_members(ir_type *clss, ir_entity *members[], int arity);
1628 /** Finds member in the list of members and removes it.
1630 * Shrinks the member list, so iterate from the end!!!
1631 * Does not deallocate the entity. */
1632 void remove_class_member(ir_type *clss, ir_entity *member);
1635 /** Adds subtype as subtype to clss.
1637 * Checks whether clss is a supertype of subtype. If not
1638 * adds also clss as supertype to subtype. */
1639 void add_class_subtype(ir_type *clss, ir_type *subtype);
1641 /** Returns the number of subtypes */
1642 int get_class_n_subtypes(const ir_type *clss);
1644 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
1645 ir_type *get_class_subtype(ir_type *clss, int pos);
1647 /** Returns the index to access subclass as subtype of class.
1649 * If subclass is no direct subtype of class returns -1.
1651 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
1653 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
1655 * Does not set the corresponding supertype relation for subtype: this might
1656 * be a different position! */
1657 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos);
1659 /** Finds subtype in the list of subtypes and removes it */
1660 void remove_class_subtype(ir_type *clss, ir_type *subtype);
1662 /* Convenience macros */
1663 #define add_class_derived_type(clss, drvtype) add_class_subtype(clss, drvtype)
1664 #define get_class_n_derived_types(clss) get_class_n_subtypes(clss)
1665 #define get_class_derived_type(clss, pos) get_class_subtype(clss, pos)
1666 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
1667 #define set_class_derived_type(clss, drvtype, pos) set_class_subtype(clss, drvtype, pos)
1668 #define remove_class_derived_type(clss, drvtype) remove_class_subtype(clss, drvtype)
1670 /** Adds supertype as supertype to class.
1672 * Checks whether clss is a subtype of supertype. If not
1673 * adds also clss as subtype to supertype. */
1674 void add_class_supertype(ir_type *clss, ir_type *supertype);
1676 /** Returns the number of supertypes */
1677 int get_class_n_supertypes(const ir_type *clss);
1679 /** Returns the index to access superclass as supertype of class.
1681 * If superclass is no direct supertype of class returns -1.
1683 int get_class_supertype_index(ir_type *clss, ir_type *super_clss);
1685 /** Gets the supertype at position pos, 0 <= pos < n_supertype. */
1686 ir_type *get_class_supertype(ir_type *clss, int pos);
1688 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
1690 * Does not set the corresponding subtype relation for supertype: this might
1691 * be at a different position! */
1692 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos);
1694 /** Finds supertype in the list of supertypes and removes it */
1695 void remove_class_supertype(ir_type *clss, ir_type *supertype);
1697 /** Convenience macro */
1698 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
1699 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
1700 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
1701 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
1702 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
1703 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
1705 /** Convenience macro */
1706 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
1707 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
1708 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
1709 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
1710 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
1711 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
1713 /** Returns a human readable string for a peculiarity. */
1714 const char *get_peculiarity_name(ir_peculiarity p);
1716 /** Returns the peculiarity of the class. */
1717 ir_peculiarity get_class_peculiarity(const ir_type *clss);
1718 /** Sets the peculiarity of the class. */
1719 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec);
1721 /** Returns the type info entity of a class. */
1722 ir_entity *get_class_type_info(const ir_type *clss);
1724 /** Set a type info entity for the class. */
1725 void set_class_type_info(ir_type *clss, ir_entity *ent);
1727 /** Returns the size of the virtual function table. */
1728 unsigned get_class_vtable_size(const ir_type *clss);
1730 /** Sets a new size of the virtual function table. */
1731 void set_class_vtable_size(ir_type *clss, unsigned size);
1733 /** Returns non-zero if a class is final. */
1734 int is_class_final(const ir_type *clss);
1736 /** Sets the class final flag. */
1737 void set_class_final(ir_type *clss, int flag);
1739 /** Return non-zero if a class is an interface */
1740 int is_class_interface(const ir_type *clss);
1742 /** Sets the class interface flag. */
1743 void set_class_interface(ir_type *clss, int flag);
1745 /** Return non-zero if a class is an abstract class. */
1746 int is_class_abstract(const ir_type *clss);
1748 /** Sets the class abstract flag. */
1749 void set_class_abstract(ir_type *clss, int flag);
1751 /** Set and get a class' dfn --
1752 @todo This is an undocumented field, subject to change! */
1753 void set_class_dfn(ir_type *clss, int dfn);
1754 int get_class_dfn(const ir_type *clss);
1756 /** Returns true if a type is a class type. */
1757 int is_Class_type(const ir_type *clss);
1760 * @page struct_type Representation of a struct type
1762 * A struct type represents aggregate types that consist of a list
1765 * The following attributes are private to this type kind:
1766 * - member: All entities belonging to this class. This are the fields
1767 * that can have any of the following types: type_class,
1768 * type_struct, type_union, type_array, type_enumeration,
1769 * type_pointer, type_primitive.
1770 * This is a dynamic list that can be grown with an "add_" function,
1772 * This is a dynamic list that can be grown with an "add_" function,
1775 /** Creates a new type struct */
1776 ir_type *new_type_struct(ident *name);
1777 /** Creates a new type struct with debug information. */
1778 ir_type *new_d_type_struct(ident *name, dbg_info* db);
1780 /* --- manipulate private fields of struct --- */
1782 /** Adds the entity as member of the struct. */
1783 void add_struct_member(ir_type *strct, ir_entity *member);
1785 /** Returns the number of members of this struct. */
1786 int get_struct_n_members(const ir_type *strct);
1788 /** Returns the member at position pos, 0 <= pos < n_member */
1789 ir_entity *get_struct_member(const ir_type *strct, int pos);
1791 /** Returns index of member in strct, -1 if not contained. */
1792 int get_struct_member_index(const ir_type *strct, ir_entity *member);
1794 /** Overwrites the member at position pos, 0 <= pos < n_member with
1795 the passed entity. */
1796 void set_struct_member(ir_type *strct, int pos, ir_entity *member);
1798 /** Finds member in the list of members and removes it. */
1799 void remove_struct_member(ir_type *strct, ir_entity *member);
1801 /** Returns true if a type is a struct type. */
1802 int is_Struct_type(const ir_type *strct);
1805 * @page method_type Representation of a method type
1807 * A method type represents a method, function or procedure type.
1808 * It contains a list of the parameter and result types, as these
1809 * are part of the type description. These lists should not
1810 * be changed by a optimization, as a change creates a new method
1811 * type. Therefore optimizations should allocated new method types.
1812 * The set_ routines are only for construction by a frontend.
1814 * - n_params: Number of parameters to the procedure.
1815 * A procedure in FIRM has only call by value parameters.
1817 * - param_type: A list with the types of parameters. This list is ordered.
1818 * The nth type in this list corresponds to the nth element
1819 * in the parameter tuple that is a result of the start node.
1820 * (See ircons.h for more information.)
1822 * - value_param_ents
1823 * A list of entities (whose owner is a struct private to the
1824 * method type) that represent parameters passed by value.
1826 * - n_res: The number of results of the method. In general, procedures
1827 * have zero results, functions one.
1829 * - res_type: A list with the types of parameters. This list is ordered.
1830 * The nth type in this list corresponds to the nth input to
1831 * Return nodes. (See ircons.h for more information.)
1834 * A list of entities (whose owner is a struct private to the
1835 * method type) that represent results passed by value.
1838 /* These macros define the suffixes for the types and entities used
1839 to represent value parameters / results. */
1840 #define VALUE_PARAMS_SUFFIX "val_param"
1841 #define VALUE_RESS_SUFFIX "val_res"
1843 /** Create a new method type.
1845 * @param name the name (ident) of this type
1846 * @param n_param the number of parameters
1847 * @param n_res the number of results
1849 * The arrays for the parameter and result types are not initialized by
1852 ir_type *new_type_method(ident *name, int n_param, int n_res);
1854 /** Create a new method type with debug information.
1856 * @param name the name (ident) of this type
1857 * @param n_param the number of parameters
1858 * @param n_res the number of results
1859 * @param db user defined debug information
1861 * The arrays for the parameter and result types are not initialized by
1864 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db);
1866 /** Clone an existing method type.
1868 * @param tp the method type to clone.
1869 * @param prefix if non-null, will be the prefix for the name of
1872 * @return the cloned method type.
1874 ir_type *clone_type_method(ir_type *tp, ident *prefix);
1876 /* -- manipulate private fields of method. -- */
1878 /** Returns the number of parameters of this method. */
1879 int get_method_n_params(const ir_type *method);
1881 /** Returns the type of the parameter at position pos of a method. */
1882 ir_type *get_method_param_type(ir_type *method, int pos);
1883 /** Sets the type of the parameter at position pos of a method.
1884 Also changes the type in the pass-by-value representation by just
1885 changing the type of the corresponding entity if the representation is constructed. */
1886 void set_method_param_type(ir_type *method, int pos, ir_type *tp);
1887 /** Returns an entity that represents the copied value argument. Only necessary
1888 for compounds passed by value. This information is constructed only on demand. */
1889 ir_entity *get_method_value_param_ent(ir_type *method, int pos);
1891 * Returns a type that represents the copied value arguments if one
1892 * was allocated, else NULL.
1894 ir_type *get_method_value_param_type(const ir_type *method);
1895 /** Returns an ident representing the parameters name. Returns NULL if not set.
1896 For debug support only. */
1897 ident *get_method_param_ident(ir_type *method, int pos);
1898 /** Returns a string representing the parameters name. Returns NULL if not set.
1899 For debug support only. */
1900 const char *get_method_param_name(ir_type *method, int pos);
1901 /** Sets an ident representing the parameters name. For debug support only. */
1902 void set_method_param_ident(ir_type *method, int pos, ident *id);
1904 /** Returns the number of results of a method type. */
1905 int get_method_n_ress(const ir_type *method);
1906 /** Returns the return type of a method type at position pos. */
1907 ir_type *get_method_res_type(ir_type *method, int pos);
1908 /** Sets the type of the result at position pos of a method.
1909 Also changes the type in the pass-by-value representation by just
1910 changing the type of the corresponding entity if the representation is constructed. */
1911 void set_method_res_type(ir_type *method, int pos, ir_type *tp);
1912 /** Returns an entity that represents the copied value result. Only necessary
1913 for compounds passed by value. This information is constructed only on demand. */
1914 ir_entity *get_method_value_res_ent(ir_type *method, int pos);
1917 * Returns a type that represents the copied value results.
1919 ir_type *get_method_value_res_type(const ir_type *method);
1922 * This enum flags the variadicity of methods (methods with a
1923 * variable amount of arguments (e.g. C's printf). Default is
1926 typedef enum variadicity {
1927 variadicity_non_variadic, /**< non variadic */
1928 variadicity_variadic /**< variadic */
1931 /** Returns the null-terminated name of this variadicity. */
1932 const char *get_variadicity_name(variadicity vari);
1934 /** Returns the variadicity of a method. */
1935 variadicity get_method_variadicity(const ir_type *method);
1937 /** Sets the variadicity of a method. */
1938 void set_method_variadicity(ir_type *method, variadicity vari);
1941 * Returns the first variadic parameter index of a type.
1942 * If this index was NOT set, the index of the last parameter
1943 * of the method type plus one is returned for variadic functions.
1944 * Non-variadic function types always return -1 here.
1946 int get_method_first_variadic_param_index(const ir_type *method);
1949 * Sets the first variadic parameter index. This allows to specify
1950 * a complete call type (containing the type of all parameters)
1951 * but still have the knowledge, which parameter must be passed as
1954 void set_method_first_variadic_param_index(ir_type *method, int index);
1956 /** Returns the mask of the additional graph properties. */
1957 unsigned get_method_additional_properties(const ir_type *method);
1959 /** Sets the mask of the additional graph properties. */
1960 void set_method_additional_properties(ir_type *method, unsigned property_mask);
1962 /** Sets one additional graph property. */
1963 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
1966 * Calling conventions: lower 24 bits are the number of register parameters,
1967 * upper 8 encode the calling conventions.
1970 cc_reg_param = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
1971 This flag may be set as default on some architectures. */
1972 cc_last_on_top = 0x02000000, /**< The last non-register parameter is transmitted on top of
1973 the stack. This is equivalent to the pascal
1974 calling convention. If this flag is not set, the first
1975 non-register parameter is used (stdcall or cdecl
1976 calling convention) */
1977 cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
1978 function calls (stdcall). */
1979 cc_this_call = 0x08000000, /**< The first parameter is a this pointer and is transmitted
1980 in a special way. */
1981 cc_compound_ret = 0x10000000, /**< The method returns a compound type. */
1982 cc_frame_on_caller_stk = 0x20000000, /**< The method did not allocate an own stack frame, instead the
1983 caller must reserve size on its own stack. */
1984 cc_fpreg_param = 0x40000000, /**< Transmit floating point parameters in registers, else the stack is used. */
1985 cc_bits = (0xFF << 24)/**< The calling convention bits. */
1986 } calling_convention;
1988 /* some often used cases: made as defines because firmjni cannot handle two
1989 equal enum values. */
1991 /** cdecl calling convention */
1992 #define cc_cdecl_set (0)
1993 /** stdcall calling convention */
1994 #define cc_stdcall_set cc_callee_clear_stk
1995 /** fastcall calling convention */
1996 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
1998 /** Returns the default calling convention for method types. */
1999 unsigned get_default_cc_mask(void);
2002 * check for the CDECL calling convention
2004 #define IS_CDECL(cc_mask) (((cc_mask) & cc_bits) == cc_cdecl_set)
2007 * check for the STDCALL calling convention
2009 #define IS_STDCALL(cc_mask) (((cc_mask) & cc_bits) == cc_stdcall_set)
2012 * check for the FASTCALL calling convention
2014 #define IS_FASTCALL(cc_mask) (((cc_mask) & cc_bits) == cc_fastcall_set)
2017 * Sets the CDECL convention bits.
2019 #define SET_CDECL(cc_mask) (((cc_mask) & ~cc_bits) | cc_cdecl_set)
2022 * Set. the STDCALL convention bits.
2024 #define SET_STDCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_stdcall_set)
2027 * Sets the FASTCALL convention bits.
2029 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
2031 /** Returns the calling convention of an entities graph. */
2032 unsigned get_method_calling_convention(const ir_type *method);
2034 /** Sets the calling convention of an entities graph. */
2035 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
2037 /** Returns the number of registers parameters, 0 means default. */
2038 unsigned get_method_n_regparams(ir_type *method);
2040 /** Sets the number of registers parameters, 0 means default. */
2041 void set_method_n_regparams(ir_type *method, unsigned n_regs);
2043 /** Returns true if a type is a method type. */
2044 int is_Method_type(const ir_type *method);
2047 * @page union_type Representation of a union (variant) type.
2049 * The union type represents union types. Note that this representation
2050 * resembles the C union type. For tagged variant types like in Pascal or Modula
2051 * a combination of a struct and a union type must be used.
2053 * - n_types: Number of unioned types.
2054 * - members: Entities for unioned types. Fixed length array.
2055 * This is a dynamic list that can be grown with an "add_" function,
2058 /** Creates a new type union. */
2059 ir_type *new_type_union(ident *name);
2061 /** Creates a new type union with debug information. */
2062 ir_type *new_d_type_union(ident *name, dbg_info* db);
2064 /* --- manipulate private fields of struct --- */
2066 /** Returns the number of unioned types of this union */
2067 int get_union_n_members(const ir_type *uni);
2069 /** Adds a new entity to a union type */
2070 void add_union_member(ir_type *uni, ir_entity *member);
2072 /** Returns the entity at position pos of a union */
2073 ir_entity *get_union_member(const ir_type *uni, int pos);
2075 /** Returns index of member in uni, -1 if not contained. */
2076 int get_union_member_index(const ir_type *uni, ir_entity *member);
2078 /** Overwrites a entity at position pos in a union type. */
2079 void set_union_member(ir_type *uni, int pos, ir_entity *member);
2081 /** Finds member in the list of members and removes it. */
2082 void remove_union_member(ir_type *uni, ir_entity *member);
2084 /** Returns true if a type is a union type. */
2085 int is_Union_type(const ir_type *uni);
2088 * @page array_type Representation of an array type
2090 * The array type represents rectangular multi dimensional arrays.
2091 * The constants representing the bounds must be allocated to
2092 * get_const_code_irg() by setting current_ir_graph accordingly.
2094 * - n_dimensions: Number of array dimensions.
2095 * - *lower_bound: Lower bounds of dimensions. Usually all 0.
2096 * - *upper_bound: Upper bounds or dimensions.
2097 * - *element_type: The type of the array elements.
2098 * - *element_ent: An entity for the array elements to be used for
2099 * element selection with Sel.
2101 * Do we need several entities? One might want
2102 * to select a dimension and not a single element in case of multi
2103 * dimensional arrays.
2106 /** Create a new type array.
2108 * Sets n_dimension to dimension and all dimension entries to NULL.
2109 * Initializes order to the order of the dimensions.
2110 * The entity for array elements is built automatically.
2111 * Set dimension sizes after call to constructor with set_* routines.
2113 ir_type *new_type_array(ident *name, int n_dims, ir_type *element_type);
2115 /** Create a new type array with debug information.
2117 * Sets n_dimension to dimension and all dimension entries to NULL.
2118 * Initializes order to the order of the dimensions.
2119 * The entity for array elements is built automatically.
2120 * Set dimension sizes after call to constructor with set_* routines.
2121 * A legal array type must have at least one dimension set.
2123 ir_type *new_d_type_array(ident *name, int n_dims, ir_type *element_type, dbg_info* db);
2125 /* --- manipulate private fields of array type --- */
2127 /** Returns the number of array dimensions of this type. */
2128 int get_array_n_dimensions(const ir_type *array);
2131 * Allocates Const nodes of mode_Is for one array dimension.
2132 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
2134 void set_array_bounds_int(ir_type *array, int dimension, int lower_bound,
2137 * Sets the bounds for one array dimension.
2138 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
2140 void set_array_bounds(ir_type *array, int dimension, ir_node *lower_bound,
2141 ir_node *upper_bound);
2142 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
2143 void set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound);
2145 /** Allocates Const nodes of mode_Is for the lower bound of an array
2146 dimension, i.e. [lower,upper[ */
2147 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound);
2149 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
2150 void set_array_upper_bound(ir_type *array, int dimension, ir_node *upper_bound);
2152 /** Allocates Const nodes of mode_Is for the upper bound of an array
2153 dimension, i.e. [lower,upper[. */
2154 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound);
2156 /** Returns true if lower bound != Unknown. */
2157 int has_array_lower_bound(const ir_type *array, int dimension);
2158 /** Returns the lower bound of an array. */
2159 ir_node *get_array_lower_bound(const ir_type *array, int dimension);
2160 /** Works only if bound is Const node with tarval that can be converted to long. */
2161 long get_array_lower_bound_int(const ir_type *array, int dimension);
2162 /** returns true if lower bound != Unknown */
2163 int has_array_upper_bound(const ir_type *array, int dimension);
2164 /** Returns the upper bound of an array. */
2165 ir_node *get_array_upper_bound(const ir_type *array, int dimension);
2166 /** Works only if bound is Const node with tarval that can be converted to long. */
2167 long get_array_upper_bound_int(const ir_type *array, int dimension);
2169 /** Sets an array dimension to a specific order. */
2170 void set_array_order(ir_type *array, int dimension, int order);
2172 /** Returns the order of an array dimension. */
2173 int get_array_order(const ir_type *array, int dimension);
2175 /** Find the array dimension that is placed at order order. */
2176 int find_array_dimension(const ir_type *array, int order);
2178 /** Sets the array element type. */
2179 void set_array_element_type(ir_type *array, ir_type* tp);
2181 /** Gets the array element type. */
2182 ir_type *get_array_element_type(ir_type *array);
2184 /** Sets the array element entity. */
2185 void set_array_element_entity(ir_type *array, ir_entity *ent);
2187 /** Get the array element entity. */
2188 ir_entity *get_array_element_entity(const ir_type *array);
2190 /** Returns true if a type is an array type. */
2191 int is_Array_type(const ir_type *array);
2194 * @page enumeration_type Representation of an enumeration type
2196 * Enumeration types need not necessarily be represented explicitly
2197 * by Firm types, as the frontend can lower them to integer constants as
2198 * well. For debugging purposes or similar tasks this information is useful.
2199 * The type state layout_fixed is set, if all enumeration constant have
2200 * there tarvals assigned. Until then
2202 * - *const: The target values representing the constants used to
2203 * represent individual enumerations.
2206 /** Create a new type enumeration -- set the enumerators independently. */
2207 ir_type *new_type_enumeration(ident *name, int n_enums);
2209 /** Create a new type enumeration with debug information -- set the enumerators independently. */
2210 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db);
2212 /* --- manipulate fields of enumeration type. --- */
2214 /** Set an enumeration constant to a enumeration type at a given position. */
2215 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con);
2217 /** Returns the number of enumeration values of this enumeration */
2218 int get_enumeration_n_enums(const ir_type *enumeration);
2220 /** Returns the enumeration constant at a given position. */
2221 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos);
2223 /** Returns the enumeration type owner of an enumeration constant. */
2224 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst);
2226 /** Sets the enumeration constant value. */
2227 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con);
2229 /** Returns the enumeration constant value. */
2230 tarval *get_enumeration_value(const ir_enum_const *enum_cnst);
2232 /** Assign an ident to an enumeration constant. */
2233 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id);
2235 /** Returns the assigned ident of an enumeration constant. */
2236 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst);
2238 /** Returns the assigned name of an enumeration constant. */
2239 const char *get_enumeration_name(const ir_enum_const *enum_cnst);
2241 /** Returns true if a type is a enumeration type. */
2242 int is_Enumeration_type(const ir_type *enumeration);
2245 * @page pointer_type Representation of a pointer type
2247 * The mode of the pointer type must be a reference mode.
2250 * - points_to: The type of the entity this pointer points to.
2253 /** Creates a new type pointer. */
2254 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode);
2256 /** Creates a new type pointer with debug information. */
2257 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
2259 /* --- manipulate fields of type_pointer --- */
2261 /** Sets the type to which a pointer points to. */
2262 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp);
2264 /** Returns the type to which a pointer points to. */
2265 ir_type *get_pointer_points_to_type(ir_type *pointer);
2267 /** Returns true if a type is a pointer type. */
2268 int is_Pointer_type(const ir_type *pointer);
2270 /** Returns the first pointer type that has as points_to tp.
2271 * Not efficient: O(#types).
2272 * If not found returns firm_unknown_type. */
2273 ir_type *find_pointer_type_to_type(ir_type *tp);
2276 * @page primitive_type Representation of a primitive type
2278 * Primitive types are types that represent atomic data values that
2279 * map directly to modes. They don't have private attributes. The
2280 * important information they carry is held in the common mode field.
2282 /** Creates a new primitive type. */
2283 ir_type *new_type_primitive(ident *name, ir_mode *mode);
2285 /** Creates a new primitive type with debug information. */
2286 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info* db);
2288 /** Returns true if a type is a primitive type. */
2289 int is_Primitive_type(const ir_type *primitive);
2291 /** Return the base type of a primitive (bitfield) type or NULL if none. */
2292 ir_type *get_primitive_base_type(ir_type *tp);
2294 /** Sets the base type of a primitive (bitfield) type. */
2295 void set_primitive_base_type(ir_type *tp, ir_type *base_tp);
2298 * @page none_type The None type
2300 * This type is an auxiliary type dedicated to support type analyses.
2302 * The none type represents that there is no type. The type can be used to
2303 * initialize fields of type* that actually can not contain a type or that
2304 * are initialized for an analysis. There exists exactly one type none.
2305 * This type is not on the type list in ir_prog. It is
2306 * allocated when initializing the type module.
2308 * The following values are set:
2310 * - name: "type_none"
2311 * - state: layout_fixed
2314 /** A variable that contains the only none type. */
2315 extern ir_type *firm_none_type;
2317 /** Returns the none type. */
2318 ir_type *get_none_type(void);
2321 * @page unknown_type The Unknown type
2323 * This type is an auxiliary type dedicated to support type analyses.
2325 * The unknown type represents that there could be a type, but it is not
2326 * known. This type can be used to initialize fields before an analysis (not known
2327 * yet) or to represent the top of a lattice (could not be determined). There exists
2328 * exactly one type unknown. This type is not on the type list in ir_prog. It is
2329 * allocated when initializing the type module.
2331 * The following values are set:
2333 * - name: "type_unknown"
2334 * - state: layout_fixed
2337 /** A variable that contains the only unknown type. */
2338 extern ir_type *firm_unknown_type;
2340 /** Returns the unknown type. */
2341 ir_type *get_unknown_type(void);
2345 * Checks whether a type is atomic.
2346 * @param tp any type
2347 * @return true if type is primitive, pointer or enumeration
2349 int is_atomic_type(const ir_type *tp);
2351 /* --- Support for compound types --- */
2354 * Gets the number of elements in a Firm compound type.
2356 * This is just a comfortability function, because structs and
2357 * classes can often be treated be the same code, but they have
2358 * different access functions to their members.
2360 * @param tp The type (must be struct, union or class).
2362 * @return Number of members in the compound type.
2364 int get_compound_n_members(const ir_type *tp);
2367 * Gets the member of a Firm compound type at position pos.
2369 * @param tp The type (must be struct, union or class).
2370 * @param pos The number of the member.
2372 * @return The member entity at position pos.
2374 * @see get_compound_n_members() for justification of existence.
2376 ir_entity *get_compound_member(const ir_type *tp, int pos);
2378 /** Returns index of member in tp, -1 if not contained. */
2379 int get_compound_member_index(const ir_type *tp, ir_entity *member);
2382 * Checks whether a type is a compound type.
2384 * @param tp - any type
2386 * @return true if the type is class, structure, union or array type.
2388 int is_compound_type(const ir_type *tp);
2391 * Checks, whether a type is a frame type.
2393 int is_frame_type(const ir_type *tp);
2396 * Checks, whether a type is a value parameter type.
2398 int is_value_param_type(const ir_type *tp);
2401 * Checks, whether a type is a lowered type.
2403 int is_lowered_type(const ir_type *tp);
2406 * Makes a new frame type. Frame types are class types,
2407 * so all class access functions work.
2408 * Frame types are not in the global list of types.
2410 ir_type *new_type_frame(ident *name);
2413 * Makes a clone of a frame type.
2414 * Sets entity links from old frame entities to new onces and
2417 ir_type *clone_frame_type(ir_type *type);
2420 * Sets a lowered type for a type. This sets both associations
2421 * and marks lowered_type as a "lowered" one.
2423 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
2426 * Gets the lowered/unlowered type of a type or NULL if this type
2427 * has no lowered/unlowered one.
2429 ir_type *get_associated_type(const ir_type *tp);
2432 * Allocate an area of size bytes aligned at alignment
2433 * at the start or the end of a frame type.
2434 * The frame type must already have a fixed layout.
2436 * @param frame_type a frame type
2437 * @param size the size of the entity
2438 * @param alignment the alignment of the entity
2439 * @param at_start if true, put the area at the frame type's start, else at end
2441 * @return the entity representing the area
2443 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, int at_start);
2445 /*-----------------------------------------------------------------*/
2447 /*-----------------------------------------------------------------*/
2450 * Outputs a unique number for this type if libfirm is compiled for
2451 * debugging, (configure with --enable-debug) else returns the address
2452 * of the type cast to long.
2454 long get_type_nr(const ir_type *tp);
2456 /* ------------------------------------------------------------------------ */
2458 /** Type for a function that compares two types.
2460 * @param tp1 The first type to compare.
2461 * @param tp2 The second type to compare.
2463 typedef int (compare_types_func_t)(const void *tp1, const void *tp2);
2465 /** Compares two types by their name.
2467 * Compares the opcode and the name of the types. If these are
2468 * equal returns 0, else non-zero.
2470 int compare_names(const void *tp1, const void *tp2);
2472 /** Compares two types strict.
2474 * returns 0 if tp1 == tp2, else non-zero
2476 int compare_strict(const void *tp1, const void *tp2);
2478 /* ------------------------------------------------------------------------ */
2480 /** Type for a function that computes a hash value for a type.
2482 * @param tp The type to compute a hash for.
2484 typedef int (hash_types_func_t)(ir_type *tp);
2486 /** Computes a hash value by the type name.
2488 * Uses the name of the type and the type opcode to compute the hash.
2490 int firm_hash_name(ir_type *tp);
2492 /* ------------------------------------------------------------------------ */
2494 /** Finalize type construction.
2496 * Indicate that a type is so far completed that it can be
2497 * distinguished from other types. Mature_type hashes the type into a
2498 * table. It uses the function in compare_types_func to compare the
2501 * If it finds a type identical to tp it returns this type. It turns
2502 * tp into the Id type. All places formerly pointing to tp will now
2503 * point to the found type. All entities of tp now refer to the found
2504 * type as their owner, but they are not a member of this type. This
2505 * is invalid firm -- the entities must be replaced by entities of the
2506 * found type. The Id type will be removed from the representation
2507 * automatically, but within an unknown time span. It occupies memory
2510 * @param tp The type to mature.
2512 ir_type *mature_type(ir_type *tp);
2514 /** Finalize type construction.
2516 * Indicate that a type is so far completed that it can be
2517 * distinguished from other types. mature_type() hashes the type into a
2518 * table. It uses the function in compare_types_func to compare the
2521 * If it finds a type identical to tp it returns this type. It frees
2522 * type tp and all its entities.
2524 * @param tp The type to mature.
2526 ir_type *mature_type_free(ir_type *tp);
2528 /** Finalize type construction.
2530 * Indicate that a type is so far completed that it can be
2531 * distinguished from other types. Mature_type hashes the type into a
2532 * table. It uses the function in compare_types_func to compare the
2535 * If it find a type identical to tp it returns this type. It frees
2536 * the entities and turns the type into an Id type. All places
2537 * formerly pointing to tp will now point to the found type. The Id
2538 * type will be removed from the representation automatically, but
2539 * within an unknown time span. It occupies memory for this time.
2541 * @param tp The type to mature.
2543 ir_type *mature_type_free_entities(ir_type *tp);
2546 * The interface type for the type identify module;
2548 typedef struct _type_identify_if_t {
2549 compare_types_func_t *cmp; /**< The function that should be used to compare two types.
2550 If NULL, compare_strict() will be used. */
2551 hash_types_func_t *hash; /**< The function that should be used to calculate a hash
2552 value of a type. If NULL, hash_name() will be used. */
2553 } type_identify_if_t;
2556 * Initialise the type identifier module.
2558 * @param ti_if The interface functions for this module.
2560 * If the parameter ti_if is NULL, the default functions compare_strict() and
2561 * firm_hash_name() will be used.
2563 void init_type_identify(type_identify_if_t *ti_if);
2565 /** A data type to treat types and entities as the same. */
2567 ir_type *typ; /**< points to a type */
2568 ir_entity *ent; /**< points to an entity */
2571 /** Type of argument functions for type walkers.
2573 * @param tore points to the visited type or entity
2574 * @param env free environment pointer
2576 typedef void type_walk_func(type_or_ent tore, void *env);
2578 /** The class walk function
2580 * @param clss points to the visited class
2581 * @param env free environment pointer
2583 typedef void class_walk_func(ir_type *clss, void *env);
2585 /** Touches every type and entity in unspecified order. If new
2586 * types/entities are created during the traversal these will
2588 * Does not touch frame types or types for value params ... */
2589 void type_walk(type_walk_func *pre, type_walk_func *post, void *env);
2591 /** Walks over all type information reachable from an ir graph.
2593 * Walks over all type information reachable from irg, i.e., starts a
2594 * type walk at the irgs entity, the irgs frame type and all types and
2595 * entities that are attributes to firm nodes. */
2596 void type_walk_irg(ir_graph *irg, type_walk_func *pre, type_walk_func *post,
2600 Touches every class in specified order:
2601 - first the super class
2602 - second the class itself
2603 - third the sub classes. If new classes are created
2604 during the traversal these will be visited, too.
2606 @todo should be named class-walk
2608 @deprecated will be removed?
2610 void type_walk_super2sub(type_walk_func *pre, type_walk_func *post, void *env);
2612 /** Walker for class types in inheritance order.
2614 * Touches every class in specified order:
2615 * - first the super class
2616 * - second the class itself
2617 * If new classes are created during the traversal these
2618 * will be visited, too.
2619 * Starts the walk at arbitrary classes.
2620 * Executes pre when first visiting a class. Executes post after
2621 * visiting all superclasses.
2623 * The arguments pre, post, env may be NULL. */
2624 void type_walk_super(type_walk_func *pre, type_walk_func *post, void *env);
2626 /** Same as type_walk_super2sub, but visits only class types.
2627 Executes pre for a class if all superclasses have been visited.
2628 Then iterates to subclasses. Executes post after return from
2630 Does not visit global type, frame types.
2632 @bug ?? something is wrong with this.
2634 void class_walk_super2sub(class_walk_func *pre, class_walk_func *post,
2638 * the entity walk function. A function type for entity walkers.
2640 * @param ent points to the visited entity
2641 * @param env free environment pointer
2643 typedef void entity_walk_func(ir_entity *ent, void *env);
2646 * Walks over all entities in the type.
2648 * @param tp the type
2649 * @param doit the entity walker function
2650 * @param env environment, will be passed to the walker function
2652 void walk_types_entities(ir_type *tp, entity_walk_func *doit, void *env);
2655 * If we have the closed world assumption, we can calculate the
2656 * finalization of classes and entities by inspecting the class hierarchy.
2657 * After this is done, all classes and entities that are not overridden
2658 * anymore have the final property set.
2660 void types_calc_finalization(void);