3 * File name: ir/tr/type.h
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
6 * Modified by: Michael Beck
8 * Copyright: (c) 2001-2003 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * Datastructure to hold type information.
18 * This module supplies a datastructure to represent all types
19 * known in the compiled program. This includes types specified
20 * in the program as well as types defined by the language. In the
21 * view of the intermediate representation there is no difference
22 * between these types. Finally it specifies some auxiliary types.
24 * There exist several kinds of types, arranged by the structure of
25 * the type. A type is described by a set of attributes. Some of
26 * these attributes are common to all types, others depend on the
29 * Types are different from the modes defined in irmode: Types are
30 * on the level of the programming language, modes at the level of
31 * the target processor.
35 #ifndef _FIRM_TR_TYPE_H_
36 #define _FIRM_TR_TYPE_H_
38 #include "firm_types.h"
40 #include "firm_common.h"
44 * An abstract data type to represent types.
46 * This is the abstract data type with which any type known in the
47 * compiled program can be represented. This includes types specified
48 * in the program as well as types defined by the language. In the
49 * view of the intermediate representation there is no difference
50 * between these types.
52 * There exist several kinds of types, arranged by the structure of
53 * the type. These are distinguished by a type opcode.
54 * A type is described by a set of attributes. Some of these attributes
55 * are common to all types, others depend on the kind of the type.
57 * The following describes the common attributes. They can only be
58 * accessed by the functions given below.
60 * The common fields are:
62 * - firm_kind: A firm_kind tag containing k_type. This is useful
63 * for dynamically checking whether a node is a type node.
64 * - type_op: A tp_op specifying the kind of the type.
65 * - name: An identifier specifying the name of the type. To be
66 * set by the frontend.
67 * - visibility: The visibility of this type.
68 * - size: The size of the type, i.e. an entity of this type will
69 * occupy size bits in memory. In several cases this is
70 * determined when fixing the layout of this type (class,
71 * struct, union, array, enumeration).
72 * - alignment The alignment of the type, i.e. an entity of this type will
73 * be allocated an an address in memory with this alignment.
74 * In several cases this is determined when fixing the layout
75 * of this type (class, struct, union, array)
76 * - mode: The mode to be used to represent the type on a machine.
77 * - state: The state of the type. The state represents whether the
78 * layout of the type is undefined or fixed (values: layout_undefined
79 * or layout_fixed). Compound types can have an undefined
80 * layout. The layout of the basic types primitive and pointer
81 * is always layout_fixed. If the layout of
82 * compound types is fixed all entities must have an offset
83 * and the size of the type must be set.
84 * A fixed layout for enumeration types means that each enumeration
85 * is associated with an implementation value.
86 * - assoc_type: The associated lowered/upper type.
87 * - visit: A counter for walks of the type information.
88 * - link: A void* to associate some additional information with the type.
90 * These fields can only be accessed via access functions.
92 * Depending on the value of @c type_op, i.e., depending on the kind of the
93 * type the adt contains further attributes. These are documented below.
97 * @link class_type class @endlink, @link struct_type struct @endlink,
98 * @link method_type method @endlink, @link union_type union @endlink,
99 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
100 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
103 * mode maybe not global field??
105 #ifndef _IR_TYPE_TYPEDEF_
106 #define _IR_TYPE_TYPEDEF_
107 typedef struct ir_type ir_type;
110 # include "type_or_entity.h"
112 /** Frees all entities associated with a type.
113 * Does not free the array entity.
114 * Warning: ensure these entities are not referenced anywhere else.
116 void free_type_entities(ir_type *tp);
118 /** Frees the memory used by the type.
120 * Removes the type from the type list. Does not free the entities
121 * belonging to the type, except for the array element entity. Does
122 * not free if tp is "none" or "unknown". Frees entities in value
123 * param subtypes of method types!!! Make sure these are not
124 * referenced any more. Further make sure there is no pointer type
125 * that refers to this type. */
126 void free_type(ir_type *tp);
128 const tp_op*get_type_tpop(const ir_type *tp);
129 ident* get_type_tpop_nameid(const ir_type *tp);
130 const char* get_type_tpop_name(const ir_type *tp);
131 tp_opcode get_type_tpop_code(const ir_type *tp);
133 ident* get_type_ident(const ir_type *tp);
134 void set_type_ident(ir_type *tp, ident* id);
135 const char* get_type_name(const ir_type *tp);
137 /** This enumeration flags the visibility of entities and types.
139 * This is necessary for partial compilation.
140 * We rely on the ordering of the flags.
143 visibility_local, /**< The entity is only visible locally. This is the default for
145 The type is only visible locally. All instances are allocated
146 locally, and no pointer to entities of this type are passed
147 out of this compilation unit. */
148 visibility_external_visible, /**< The entity is visible to other external program parts, but
149 it is defined here. It may not be optimized away. The entity must
151 For types: entities of this type can be accessed externally. No
152 instances of this type are allocated externally. */
153 visibility_external_allocated /**< The entity is defined and allocated externally. This compilation
154 must not allocate memory for this entity. The entity must
155 be static_allocated. This can also be an external defined
157 For types: entities of this type are allocated and accessed from
158 external code. Default for types. */
161 /** The visibility of a type.
163 * The visibility of a type indicates, whether entities of this type
164 * are accessed or allocated in external code.
166 * An entity of a type is allocated in external code, if the external
167 * code declares a variable of this type, or dynamically allocates
168 * an entity of this type. If the external code declares a (compound)
169 * type, that contains entities of this type, the visibility also
170 * must be external_allocated.
172 * The visibility must be higher than that of all entities, if the
173 * type is a compound. Here it is questionable, what happens with
174 * static entities. If these are accessed external by direct reference,
175 * (a static call to a method, that is also in the dispatch table)
176 * it should not affect the visibility of the type.
179 * @@@ Do we need a visibility for types?
180 * I change the layout of types radically when doing type splitting.
181 * I need to know, which fields of classes are accessed in the RTS,
182 * e.g., [_length. I may not move [_length to the split part.
183 * The layout though, is a property of the type.
185 * One could also think of changing the mode of a type ...
187 * But, we could also output macros to access the fields, e.g.,
188 * ACCESS_[_length (X) X->length // conventional
189 * ACCESS_[_length (X) X->_split_ref->length // with type splitting
191 * For now I implement this function, that returns the visibility
192 * based on the visibility of the entities of a compound ...
194 * This function returns visibility_external_visible if one or more
195 * entities of a compound type have visibility_external_visible.
196 * Entities of types are never visibility_external_allocated (right?).
197 * Else returns visibility_local.
199 visibility get_type_visibility (const ir_type *tp);
200 void set_type_visibility (ir_type *tp, visibility v);
204 /** The state of the type layout. */
206 layout_undefined, /**< The layout of this type is not defined.
207 Address computation to access fields is not
208 possible, fields must be accessed by Sel
209 nodes. This is the default value except for
210 pointer, primitive and method types. */
211 layout_fixed /**< The layout is fixed, all component/member entities
212 have an offset assigned. Size of the type is known.
213 Arrays can be accessed by explicit address
214 computation. Default for pointer, primitive and method
218 /** Returns a human readable string for the enum entry. */
219 const char *get_type_state_name(type_state s);
221 /** Returns the type layout state of a type. */
222 type_state get_type_state(const ir_type *tp);
224 /** Sets the type layout state of a type.
226 * For primitives, pointer and method types the layout is always fixed.
227 * This call is legal but has no effect.
229 void set_type_state(ir_type *tp, type_state state);
231 /** Returns the mode of a type.
233 * Returns NULL for all non atomic types.
235 ir_mode* get_type_mode(const ir_type *tp);
237 /** Sets the mode of a type.
239 * Only has an effect on primitive, enumeration and pointer types.
241 void set_type_mode(ir_type *tp, ir_mode* m);
243 /** Returns the size of a type in bytes, returns -1 if the size is NOT
244 * a byte size, i.e. not dividable by 8. */
245 int get_type_size_bytes(const ir_type *tp);
247 /** Returns the size of a type in bits. */
248 int get_type_size_bits(const ir_type *tp);
250 /** Sets the size of a type in bytes.
252 * For primitive, enumeration, pointer and method types the size
253 * is always fixed. This call is legal but has no effect.
255 void set_type_size_bytes(ir_type *tp, int size);
257 /** Sets the size of a type in bits.
259 * For primitive, enumeration, pointer and method types the size
260 * is always fixed. This call is legal but has no effect.
262 void set_type_size_bits(ir_type *tp, int size);
264 /** Returns the alignment of a type in bytes.
266 * Returns -1 if the alignment is NOT
267 * a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
268 int get_type_alignment_bytes(ir_type *tp);
270 /** Returns the alignment of a type in bits.
272 * If the alignment of a type is
273 * not set, it is calculated here according to the following rules:
274 * -#.) if a type has a mode, the alignment is the mode size.
275 * -#.) compound types have the alignment of there biggest member.
276 * -#.) array types have the alignment of there element type.
277 * -#.) method types return 0 here.
278 * -#.) all other types return 8 here (i.e. aligned at byte).
280 int get_type_alignment_bits(ir_type *tp);
282 /** Sets the alignment of a type in bytes. */
283 void set_type_alignment_bytes(ir_type *tp, int size);
285 /** Sets the alignment of a type in bits.
287 * For method types the alignment is always fixed.
288 * This call is legal but has no effect.
290 void set_type_alignment_bits(ir_type *tp, int size);
292 /** Returns the visited count of a type. */
293 unsigned long get_type_visited(const ir_type *tp);
294 /** Sets the visited count of a type to num. */
295 void set_type_visited(ir_type *tp, unsigned long num);
296 /** Sets visited field in type to type_visited. */
297 void mark_type_visited(ir_type *tp);
298 /** Returns non-zero if the type is already visited */
299 int type_visited(const ir_type *tp);
300 /** Returns non-zero if the type is not yet visited */
301 int type_not_visited(const ir_type *tp);
303 /** Returns the associated link field of a type. */
304 void* get_type_link(const ir_type *tp);
305 /** Sets the associated link field of a type. */
306 void set_type_link(ir_type *tp, void *l);
309 * Visited flag to traverse the type information.
311 * Increase this flag by one before traversing the type information
312 * using inc_master_type_visited().
313 * Mark type nodes as visited by mark_type_visited(ir_type).
314 * Check whether node was already visited by type_visited(ir_type)
315 * and type_not_visited(ir_type).
316 * Or use the function to walk all types.
320 void set_master_type_visited(unsigned long val);
321 unsigned long get_master_type_visited(void);
322 void inc_master_type_visited(void);
325 * Checks whether a pointer points to a type.
327 * @param thing an arbitrary pointer
330 * true if the thing is a type, else false
332 int is_type (const void *thing);
335 * Checks whether two types are structurally equal.
337 * @param typ1 the first type
338 * @param typ2 the second type
341 * true if the types are equal, else false.
343 * Types are equal if :
344 * - they are the same type kind
345 * - they have the same name
346 * - they have the same mode (if applicable)
347 * - they have the same type_state and, ev., the same size
348 * - they are class types and have:
349 * - the same members (see same_entity in entity.h)
350 * - the same supertypes -- the C-pointers are compared --> no recursive call.
351 * - the same number of subtypes. Subtypes are not compared,
352 * as this could cause a cyclic test.
353 * - the same peculiarity
354 * - they are structure types and have the same members
355 * - they are method types and have
356 * - the same parameter types
357 * - the same result types
358 * - they are union types and have the same members
359 * - they are array types and have
360 * - the same number of dimensions
361 * - the same dimension bounds
362 * - the same dimension order
363 * - the same element type
364 * - they are enumeration types and have the same enumerator names
365 * - they are pointer types and have the identical points_to type
366 * (i.e., the same C-struct to represent the type, type_id is skipped.
367 * This is to avoid endless recursions; with pointer types cyclic
368 * type graphs are possible.)
370 int equal_type(ir_type *typ1, ir_type *typ2);
373 * Checks whether two types are structural comparable.
375 * @param st pointer type
376 * @param lt pointer type
379 * true if type st is smaller than type lt, i.e. whenever
380 * lt is expected a st can be used.
382 * - they are the same type kind
383 * - mode(st) < mode (lt) (if applicable)
384 * - they are class types and st is (transitive) subtype of lt,
385 * - they are structure types and
386 * - the members of st have exactly one counterpart in lt with the same name,
387 * - the counterpart has a bigger type.
388 * - they are method types and have
389 * - the same number of parameter and result types,
390 * - the parameter types of st are smaller than those of lt,
391 * - the result types of st are smaller than those of lt
392 * - they are union types and have the members of st have exactly one
393 * @return counterpart in lt and the type is smaller
394 * - they are array types and have
395 * - the same number of dimensions
396 * - all bounds of lt are bound of st
397 * - the same dimension order
398 * - the same element type
400 * - the element type of st is smaller than that of lt
401 * - the element types have the same size and fixed layout.
402 * - they are enumeration types and have the same enumerator names
403 * - they are pointer types and have the points_to type of st is
404 * @return smaller than the points_to type of lt.
407 int smaller_type (ir_type *st, ir_type *lt);
410 * @page class_type Representation of a class type
412 * If the type opcode is set to type_class the type represents class
413 * types. A list of fields and methods is associated with a class.
414 * Further a class can inherit from and bequest to other classes.
416 * The following attributes are private to this type kind:
417 * - member: All entities belonging to this class. This are method entities
418 * which have type_method or fields that can have any of the
419 * following type kinds: type_class, type_struct, type_union,
420 * type_array, type_enumeration, type_pointer, type_primitive.
422 * The following two are dynamic lists that can be grown with an "add_" function,
425 * - subtypes: A list of direct subclasses.
427 * - supertypes: A list of direct superclasses.
429 * - peculiarity: The peculiarity of this class. If the class is of peculiarity
430 * "description" it only is a description of requirements to a class,
431 * as, e.g., a Java interface. The class will never be allocated.
432 * Peculiarity inherited is only possible for entities. An entity
433 * is of peculiarity inherited if the compiler generated the entity
434 * to explicitly resolve inheritance. An inherited method entity has
436 * Values: description, existent, inherited. Default: existent.
438 * - type_info: An entity representing the type information of this class.
439 * This entity can be of arbitrari type, Firm did not use it yet.
440 * It allows to express the coupling of a type with an entity
441 * representing this type. This information is useful for lowering
442 * of InstOf and TypeChk nodes. Default: NULL
444 * - vtable_size: The size of this class virtual function table.
447 * - final: A final class is always a leaf in the class hierarchy. Final
448 * classes cannot be super classes of other ones. As this information
449 * can only be computed in whole world compilations, we allow to
450 * set this flag. It is used in optimizations if get_opt_closed_world()
451 * is false. Default: false
453 * - interface: The class represents an interface. This flag can be set to distinguish
454 * between interfaces, abstract classes and other classes that all may
455 * have the peculiarity peculiarity_description. Depending on this flag
456 * the lowering might do different actions. Default: false
458 * - abstract : The class represents an abstract class. This flag can be set to distinguish
459 * between interfaces, abstract classes and other classes that all may
460 * have the peculiarity peculiarity_description. Depending on this flag
461 * the lowering might do different actions. Default: false
464 /** Creates a new class type. */
465 ir_type *new_type_class (ident *name);
467 /** Creates a new class type with debug information. */
468 ir_type *new_d_type_class (ident *name, dbg_info *db);
470 /* --- manipulate private fields of class type --- */
472 /** Adds the entity as member of the class. */
473 void add_class_member (ir_type *clss, entity *member);
475 /** Returns the number of members of this class. */
476 int get_class_n_members (const ir_type *clss);
478 /** Returns the member at position pos, 0 <= pos < n_member */
479 entity *get_class_member (const ir_type *clss, int pos);
481 /** Returns index of mem in clss, -1 if not contained. */
482 int get_class_member_index(const ir_type *clss, entity *mem);
484 /** Finds the member with name 'name'. If several members with the same
485 * name returns one of them. Returns NULL if no member found. */
486 entity *get_class_member_by_name(ir_type *clss, ident *name);
488 /** Overwrites the member at position pos, 0 <= pos < n_member with
489 * the passed entity. */
490 void set_class_member (ir_type *clss, entity *member, int pos);
492 /** Replaces complete member list in class type by the list passed.
494 * Copies the list passed. This function is necessary to reduce the number of members.
495 * members is an array of entities, num the size of this array. Sets all
496 * owners of the members passed to clss. */
497 void set_class_members (ir_type *clss, entity *members[], int arity);
499 /** Finds member in the list of members and removes it.
501 * Shrinks the member list, so iterate from the end!!!
502 * Does not deallocate the entity. */
503 void remove_class_member(ir_type *clss, entity *member);
506 /** Adds subtype as subtype to clss.
508 * Checks whether clss is a supertype of subtype. If not
509 * adds also clss as supertype to subtype. */
510 void add_class_subtype (ir_type *clss, ir_type *subtype);
512 /** Returns the number of subtypes */
513 int get_class_n_subtypes (const ir_type *clss);
515 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
516 ir_type *get_class_subtype (ir_type *clss, int pos);
518 /** Returns the index to access subclass as subtype of class.
520 * If subclass is no direct subtype of class returns -1.
522 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
524 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
526 * Does not set the corresponding supertype relation for subtype: this might
527 * be a different position! */
528 void set_class_subtype (ir_type *clss, ir_type *subtype, int pos);
530 /** Finds subtype in the list of subtypes and removes it */
531 void remove_class_subtype(ir_type *clss, ir_type *subtype);
533 /* Convenience macros */
534 #define add_class_derived_type(clss, drvtype) add_class_subtype(clss, drvtype)
535 #define get_class_n_derived_types(clss) get_class_n_subtypes(clss)
536 #define get_class_derived_type(clss, pos) get_class_subtype(clss, pos)
537 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
538 #define set_class_derived_type(clss, drvtype, pos) set_class_subtype(clss, drvtype, pos)
539 #define remove_class_derived_type(clss, drvtype) remove_class_subtype(clss, drvtype)
541 /** Adds supertype as supertype to class.
543 * Checks whether clss is a subtype of supertype. If not
544 * adds also clss as subtype to supertype. */
545 void add_class_supertype (ir_type *clss, ir_type *supertype);
547 /** Returns the number of supertypes */
548 int get_class_n_supertypes (const ir_type *clss);
550 /** Returns the index to access superclass as supertype of class.
552 * If superclass is no direct supertype of class returns -1.
554 int get_class_supertype_index(ir_type *clss, ir_type *super_clss);
556 /** Gets the supertype at position pos, 0 <= pos < n_supertype. */
557 ir_type *get_class_supertype (ir_type *clss, int pos);
559 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
561 * Does not set the corresponding subtype relation for supertype: this might
562 * be at a different position! */
563 void set_class_supertype (ir_type *clss, ir_type *supertype, int pos);
565 /** Finds supertype in the list of supertypes and removes it */
566 void remove_class_supertype(ir_type *clss, ir_type *supertype);
568 /** Convenience macro */
569 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
570 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
571 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
572 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
573 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
574 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
576 /** Convenience macro */
577 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
578 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
579 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
580 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
581 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
582 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
584 /** This enumeration flags the peculiarity of entities and types. */
585 typedef enum peculiarity {
586 peculiarity_description, /**< Represents only a description. The entity/type is never
587 allocated, no code/data exists for this entity/type.
588 @@@ eventually rename to descriptive (adjective as the others!)*/
589 peculiarity_inherited, /**< Describes explicitly that other entities are
590 inherited to the owner of this entity.
591 Overwrites must refer to at least one other
592 entity. If this is a method entity there exists
593 no irg for this entity, only for one of the
596 peculiarity_existent /**< The entity/type (can) exist.
597 @@@ eventually rename to 'real' i.e., 'echt'
598 This serves better as opposition to description _and_ inherited.*/
601 /** Returns a human readable string for a peculiarity. */
602 const char *get_peculiarity_name(peculiarity p);
604 /** Returns the peculiarity of the class. */
605 peculiarity get_class_peculiarity (const ir_type *clss);
606 /** Sets the peculiarity of the class. */
607 void set_class_peculiarity (ir_type *clss, peculiarity pec);
609 /** Returns the type info entity of a class. */
610 entity *get_class_type_info(const ir_type *clss);
612 /** Set a type info entity for the class. */
613 void set_class_type_info(ir_type *clss, entity *ent);
615 /** Returns the size of the virtual function table. */
616 unsigned get_class_vtable_size(const ir_type *clss);
618 /** Sets a new size of the virtual function table. */
619 void set_class_vtable_size(ir_type *clss, unsigned size);
621 /** Returns non-zero if a class is final. */
622 int is_class_final(const ir_type *clss);
624 /** Sets the class final flag. */
625 void set_class_final(ir_type *clss, int flag);
627 /** Return non-zero if a class is an interface */
628 int is_class_interface(const ir_type *clss);
630 /** Sets the class interface flag. */
631 void set_class_interface(ir_type *clss, int flag);
633 /** Return non-zero if a class is an abstract class. */
634 int is_class_abstract(const ir_type *clss);
636 /** Sets the class abstract flag. */
637 void set_class_abstract(ir_type *clss, int flag);
639 /** Set and get a class' dfn --
640 @todo This is an undocumented field, subject to change! */
641 void set_class_dfn (ir_type *clss, int dfn);
642 int get_class_dfn (const ir_type *clss);
644 /** Returns true if a type is a class type. */
645 int is_Class_type(const ir_type *clss);
648 * @page struct_type Representation of a struct type
650 * A struct type represents aggregate types that consist of a list
653 * The following attributes are private to this type kind:
654 * - member: All entities belonging to this class. This are the fields
655 * that can have any of the following types: type_class,
656 * type_struct, type_union, type_array, type_enumeration,
657 * type_pointer, type_primitive.
658 * This is a dynamic list that can be grown with an "add_" function,
660 * This is a dynamic list that can be grown with an "add_" function,
663 /** Creates a new type struct */
664 ir_type *new_type_struct (ident *name);
665 /** Creates a new type struct with debug information. */
666 ir_type *new_d_type_struct (ident *name, dbg_info* db);
668 /* --- manipulate private fields of struct --- */
670 /** Adds the entity as member of the struct. */
671 void add_struct_member (ir_type *strct, entity *member);
673 /** Returns the number of members of this struct. */
674 int get_struct_n_members (const ir_type *strct);
676 /** Returns the member at position pos, 0 <= pos < n_member */
677 entity *get_struct_member (const ir_type *strct, int pos);
679 /** Returns index of member in strct, -1 if not contained. */
680 int get_struct_member_index(const ir_type *strct, entity *member);
682 /** Overwrites the member at position pos, 0 <= pos < n_member with
683 the passed entity. */
684 void set_struct_member (ir_type *strct, int pos, entity *member);
686 /** Finds member in the list of members and removes it. */
687 void remove_struct_member (ir_type *strct, entity *member);
689 /** Returns true if a type is a struct type. */
690 int is_Struct_type(const ir_type *strct);
693 * @page method_type Representation of a method type
695 * A method type represents a method, function or procedure type.
696 * It contains a list of the parameter and result types, as these
697 * are part of the type description. These lists should not
698 * be changed by a optimization, as a change creates a new method
699 * type. Therefore optimizations should allocated new method types.
700 * The set_ routines are only for construction by a frontend.
702 * - n_params: Number of parameters to the procedure.
703 * A procedure in FIRM has only call by value parameters.
705 * - param_type: A list with the types of parameters. This list is ordered.
706 * The nth type in this list corresponds to the nth element
707 * in the parameter tuple that is a result of the start node.
708 * (See ircons.h for more information.)
711 * A list of entities (whose owner is a struct private to the
712 * method type) that represent parameters passed by value.
714 * - n_res: The number of results of the method. In general, procedures
715 * have zero results, functions one.
717 * - res_type: A list with the types of parameters. This list is ordered.
718 * The nth type in this list corresponds to the nth input to
719 * Return nodes. (See ircons.h for more information.)
722 * A list of entities (whose owner is a struct private to the
723 * method type) that represent results passed by value.
726 /* These macros define the suffixes for the types and entities used
727 to represent value parameters / results. */
728 #define VALUE_PARAMS_SUFFIX "val_param"
729 #define VALUE_RESS_SUFFIX "val_res"
731 /** Create a new method type.
733 * @param name the name (ident) of this type
734 * @param n_param the number of parameters
735 * @param n_res the number of results
737 * The arrays for the parameter and result types are not initialized by
740 ir_type *new_type_method (ident *name, int n_param, int n_res);
742 /** Create a new method type with debug information.
744 * @param name the name (ident) of this type
745 * @param n_param the number of parameters
746 * @param n_res the number of results
747 * @param db user defined debug information
749 * The arrays for the parameter and result types are not initialized by
752 ir_type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
754 /* -- manipulate private fields of method. -- */
756 /** Returns the number of parameters of this method. */
757 int get_method_n_params (const ir_type *method);
759 /** Returns the type of the parameter at position pos of a method. */
760 ir_type *get_method_param_type(ir_type *method, int pos);
761 /** Sets the type of the parameter at position pos of a method.
762 Also changes the type in the pass-by-value representation by just
763 changing the type of the corresponding entity if the representation is constructed. */
764 void set_method_param_type(ir_type *method, int pos, ir_type *tp);
765 /** Returns an entity that represents the copied value argument. Only necessary
766 for compounds passed by value. This information is constructed only on demand. */
767 entity *get_method_value_param_ent(ir_type *method, int pos);
769 * Returns a type that represents the copied value arguments if one
770 * was allocated, else NULL.
772 ir_type *get_method_value_param_type(const ir_type *method);
774 /** Returns the number of results of a method type. */
775 int get_method_n_ress (const ir_type *method);
776 /** Returns the return type of a method type at position pos. */
777 ir_type *get_method_res_type(ir_type *method, int pos);
778 /** Sets the type of the result at position pos of a method.
779 Also changes the type in the pass-by-value representation by just
780 changing the type of the corresponding entity if the representation is constructed. */
781 void set_method_res_type(ir_type *method, int pos, ir_type *tp);
782 /** Returns an entity that represents the copied value result. Only necessary
783 for compounds passed by value. This information is constructed only on demand. */
784 entity *get_method_value_res_ent(ir_type *method, int pos);
787 * Returns a type that represents the copied value results.
789 ir_type *get_method_value_res_type(const ir_type *method);
792 * This enum flags the variadicity of methods (methods with a
793 * variable amount of arguments (e.g. C's printf). Default is
796 typedef enum variadicity {
797 variadicity_non_variadic, /**< non variadic */
798 variadicity_variadic /**< variadic */
801 /** Returns the null-terminated name of this variadicity. */
802 const char *get_variadicity_name(variadicity vari);
804 /** Returns the variadicity of a method. */
805 variadicity get_method_variadicity(const ir_type *method);
807 /** Sets the variadicity of a method. */
808 void set_method_variadicity(ir_type *method, variadicity vari);
811 * Returns the first variadic parameter index of a type.
812 * If this index was NOT set, the index of the last parameter
813 * of the method type plus one is returned for variadic functions.
814 * Non-variadic function types always return -1 here.
816 int get_method_first_variadic_param_index(const ir_type *method);
819 * Sets the first variadic parameter index. This allows to specify
820 * a complete call type (containing the type of all parameters)
821 * but still have the knowledge, which parameter must be passed as
824 void set_method_first_variadic_param_index(ir_type *method, int index);
827 * Additional method type properties:
828 * Tell about special properties of a method type. Some
829 * of these may be discovered by analyses.
832 mtp_no_property = 0x00000000, /**< no additional properties, default */
833 mtp_property_const = 0x00000001, /**< This method did not access memory and calculates
834 its return values solely from its parameters.
835 GCC: __attribute__((const)). */
836 mtp_property_pure = 0x00000002, /**< This method did NOT write to memory and calculates
837 its return values solely from its parameters and
838 the memory they points to (or global vars).
839 GCC: __attribute__((pure)). */
840 mtp_property_noreturn = 0x00000004, /**< This method did not return due to an aborting system
842 GCC: __attribute__((noreturn)). */
843 mtp_property_nothrow = 0x00000008, /**< This method cannot throw an exception.
844 GCC: __attribute__((nothrow)). */
845 mtp_property_naked = 0x00000010, /**< This method is naked.
846 GCC: __attribute__((naked)). */
847 mtp_property_malloc = 0x00000020, /**< This method returns newly allocate memory.
848 GCC: __attribute__((malloc)). */
849 mtp_property_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
850 a lowering phase will remove all calls to it. */
851 mtp_property_inherited = (1<<31) /**< Internal. Used only in irg's, means property is
852 inherited from type. */
853 } mtp_additional_property;
855 /** Returns the mask of the additional graph properties. */
856 unsigned get_method_additional_properties(const ir_type *method);
858 /** Sets the mask of the additional graph properties. */
859 void set_method_additional_properties(ir_type *method, unsigned property_mask);
861 /** Sets one additional graph property. */
862 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
865 * Calling conventions: lower 24 bits are the number of register parameters,
866 * upper 8 encode the calling conventions.
869 cc_reg_param = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
870 This flag may be set as default on some architectures. */
871 cc_last_on_top = 0x02000000, /**< The last non-register parameter is transmitted on top of
872 the stack. This is equivalent to the pascal
873 calling convention. If this flag is not set, the first
874 non-register parameter is used (stdcall or cdecl
875 calling convention) */
876 cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
877 function calls (stdcall). */
878 cc_this_call = 0x08000000, /**< The first parameter is a this pointer and is transmitted
881 cc_bits = (0xFF << 24) /**< the calling convention bits */
882 } calling_convention;
884 /* some often used cases: made as defines because firmjni cannot handle two
885 equal enum values. */
887 /** cdecl calling convention */
888 #define cc_cdecl_set (0)
889 /** stdcall calling convention */
890 #define cc_stdcall_set cc_callee_clear_stk
891 /** fastcall calling convention */
892 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
894 /** Returns the default calling convention for method types. */
895 unsigned get_default_cc_mask(void);
898 * check for the CDECL calling convention
900 #define IS_CDECL(cc_mask) (((cc_mask) & cc_bits) == cc_cdecl_set)
903 * check for the STDCALL calling convention
905 #define IS_STDCALL(cc_mask) (((cc_mask) & cc_bits) == cc_stdcall_set)
908 * check for the FASTCALL calling convention
910 #define IS_FASTCALL(cc_mask) (((cc_mask) & cc_bits) == cc_fastcall_set)
913 * Sets the CDECL convention bits.
915 #define SET_CDECL(cc_mask) (((cc_mask) & ~cc_bits) | cc_cdecl_set)
918 * Set. the STDCALL convention bits.
920 #define SET_STDCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_stdcall_set)
923 * Sets the FASTCALL convention bits.
925 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
927 /** Returns the calling convention of an entities graph. */
928 unsigned get_method_calling_convention(const ir_type *method);
930 /** Sets the calling convention of an entities graph. */
931 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
933 /** Returns the number of registers parameters, 0 means default. */
934 unsigned get_method_n_regparams(ir_type *method);
936 /** Sets the number of registers parameters, 0 means default. */
937 void set_method_n_regparams(ir_type *method, unsigned n_regs);
939 /** Returns true if a type is a method type. */
940 int is_Method_type (const ir_type *method);
943 * @page union_type Representation of a union (variant) type.
945 * The union type represents union types. Note that this representation
946 * resembles the C union type. For tagged variant types like in Pascal or Modula
947 * a combination of a struct and a union type must be used.
949 * - n_types: Number of unioned types.
950 * - members: Entities for unioned types. Fixed length array.
951 * This is a dynamic list that can be grown with an "add_" function,
954 /** Creates a new type union. */
955 ir_type *new_type_union (ident *name);
957 /** Creates a new type union with debug information. */
958 ir_type *new_d_type_union (ident *name, dbg_info* db);
960 /* --- manipulate private fields of struct --- */
962 /** Returns the number of unioned types of this union */
963 int get_union_n_members (const ir_type *uni);
965 /** Adds a new entity to a union type */
966 void add_union_member (ir_type *uni, entity *member);
968 /** Returns the entity at position pos of a union */
969 entity *get_union_member (const ir_type *uni, int pos);
971 /** Returns index of member in uni, -1 if not contained. */
972 int get_union_member_index(const ir_type *uni, entity *member);
974 /** Overwrites a entity at position pos in a union type. */
975 void set_union_member (ir_type *uni, int pos, entity *member);
977 /** Finds member in the list of members and removes it. */
978 void remove_union_member (ir_type *uni, entity *member);
980 /** Returns true if a type is a union type. */
981 int is_Union_type (const ir_type *uni);
984 * @page array_type Representation of an array type
986 * The array type represents rectangular multi dimensional arrays.
987 * The constants representing the bounds must be allocated to
988 * get_const_code_irg() by setting current_ir_graph accordingly.
990 * - n_dimensions: Number of array dimensions.
991 * - *lower_bound: Lower bounds of dimensions. Usually all 0.
992 * - *upper_bound: Upper bounds or dimensions.
993 * - *element_type: The type of the array elements.
994 * - *element_ent: An entity for the array elements to be used for
995 * element selection with Sel.
997 * Do we need several entities? One might want
998 * to select a dimension and not a single element in case of multi
999 * dimensional arrays.
1002 /** Create a new type array.
1004 * Sets n_dimension to dimension and all dimension entries to NULL.
1005 * Initializes order to the order of the dimensions.
1006 * The entity for array elements is built automatically.
1007 * Set dimension sizes after call to constructor with set_* routines.
1009 ir_type *new_type_array (ident *name, int n_dimensions,
1010 ir_type *element_type);
1012 /** Create a new type array with debug information.
1014 * Sets n_dimension to dimension and all dimension entries to NULL.
1015 * Initializes order to the order of the dimensions.
1016 * The entity for array elements is built automatically.
1017 * Set dimension sizes after call to constructor with set_* routines.
1018 * A legal array type must have at least one dimension set.
1020 ir_type *new_d_type_array (ident *name, int n_dimensions,
1021 ir_type *element_type, dbg_info* db);
1023 /* --- manipulate private fields of array type --- */
1025 /** Returns the number of array dimensions of this type. */
1026 int get_array_n_dimensions (const ir_type *array);
1029 * Allocates Const nodes of mode_Is for one array dimension.
1030 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
1032 void set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
1035 * Sets the bounds for one array dimension.
1036 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
1038 void set_array_bounds (ir_type *array, int dimension, ir_node *lower_bound,
1039 ir_node *upper_bound);
1040 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
1041 void set_array_lower_bound (ir_type *array, int dimension, ir_node *lower_bound);
1043 /** Allocates Const nodes of mode_Is for the lower bound of an array
1044 dimension, i.e. [lower,upper[ */
1045 void set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound);
1047 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
1048 void set_array_upper_bound (ir_type *array, int dimension, ir_node *upper_bound);
1050 /** Allocates Const nodes of mode_Is for the upper bound of an array
1051 dimension, i.e. [lower,upper[. */
1052 void set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound);
1054 /** Returns true if lower bound != Unknown. */
1055 int has_array_lower_bound (const ir_type *array, int dimension);
1056 /** Returns the lower bound of an array. */
1057 ir_node * get_array_lower_bound (const ir_type *array, int dimension);
1058 /** Works only if bound is Const node with tarval that can be converted to long. */
1059 long get_array_lower_bound_int (const ir_type *array, int dimension);
1060 /** returns true if lower bound != Unknown */
1061 int has_array_upper_bound (const ir_type *array, int dimension);
1062 /** Returns the upper bound of an array. */
1063 ir_node * get_array_upper_bound (const ir_type *array, int dimension);
1064 /** Works only if bound is Const node with tarval that can be converted to long. */
1065 long get_array_upper_bound_int (const ir_type *array, int dimension);
1067 /** Sets an array dimension to a specific order. */
1068 void set_array_order (ir_type *array, int dimension, int order);
1070 /** Returns the order of an array dimension. */
1071 int get_array_order (const ir_type *array, int dimension);
1073 /** Find the array dimension that is placed at order ord. */
1074 int find_array_dimension(const ir_type *array, int order);
1076 /** Sets the array element type. */
1077 void set_array_element_type (ir_type *array, ir_type* tp);
1079 /** Gets the array element type. */
1080 ir_type *get_array_element_type (ir_type *array);
1082 /** Sets the array element entity. */
1083 void set_array_element_entity (ir_type *array, entity *ent);
1085 /** Get the array element entity. */
1086 entity *get_array_element_entity (const ir_type *array);
1088 /** Returns true if a type is an array type. */
1089 int is_Array_type(const ir_type *array);
1092 * @page enumeration_type Representation of an enumeration type
1094 * Enumeration types need not necessarily be represented explicitly
1095 * by Firm types, as the frontend can lower them to integer constants as
1096 * well. For debugging purposes or similar tasks this information is useful.
1098 * - *enum: The target values representing the constants used to
1099 * represent individual enumerations.
1100 * - *enum_nameid: Idents containing the source program name of the enumeration
1103 /** Create a new type enumeration -- set the enumerators independently. */
1104 ir_type *new_type_enumeration (ident *name, int n_enums);
1106 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1107 ir_type *new_d_type_enumeration (ident *name, int n_enums, dbg_info *db);
1109 /* --- manipulate fields of enumeration type. --- */
1111 /** Returns the number of enumeration values of this enumeration */
1112 int get_enumeration_n_enums (const ir_type *enumeration);
1114 /** Sets the enumeration value at a given position. */
1115 void set_enumeration_enum (ir_type *enumeration, int pos, tarval *con);
1117 /** Returns the enumeration value at a given position. */
1118 tarval *get_enumeration_enum (const ir_type *enumeration, int pos);
1120 /** Assign an ident to an enumeration value at a given position. */
1121 void set_enumeration_nameid (ir_type *enumeration, int pos, ident *id);
1123 /** Returns the assigned ident of an enumeration value at a given position. */
1124 ident *get_enumeration_nameid (const ir_type *enumeration, int pos);
1126 /** Returns the assigned name of an enumeration value at a given position. */
1127 const char *get_enumeration_name(const ir_type *enumeration, int pos);
1129 /** Returns true if a type is a enumeration type. */
1130 int is_Enumeration_type (const ir_type *enumeration);
1133 * @page pointer_type Representation of a pointer type
1135 * The mode of the pointer type must be a reference mode.
1138 * - points_to: The type of the entity this pointer points to.
1141 /** Creates a new type pointer. */
1142 ir_type *new_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode);
1144 /** Creates a new type pointer with debug information. */
1145 ir_type *new_d_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
1147 /* --- manipulate fields of type_pointer --- */
1149 /** Sets the type to which a pointer points to. */
1150 void set_pointer_points_to_type (ir_type *pointer, ir_type *tp);
1152 /** Returns the type to which a pointer points to. */
1153 ir_type *get_pointer_points_to_type (ir_type *pointer);
1155 /** Returns true if a type is a pointer type. */
1156 int is_Pointer_type (const ir_type *pointer);
1158 /** Returns the first pointer type that has as points_to tp.
1159 * Not efficient: O(#types).
1160 * If not found returns firm_unknown_type. */
1161 ir_type *find_pointer_type_to_type (ir_type *tp);
1164 * @page primitive_type Representation of a primitive type
1166 * Primitive types are types that represent atomic data values that
1167 * map directly to modes. They don't have private attributes. The
1168 * important information they carry is held in the common mode field.
1170 /** Creates a new primitive type. */
1171 ir_type *new_type_primitive (ident *name, ir_mode *mode);
1173 /** Creates a new primitive type with debug information. */
1174 ir_type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
1176 /** Returns true if a type is a primitive type. */
1177 int is_Primitive_type (const ir_type *primitive);
1181 * @page none_type The None type
1183 * This type is an auxiliary type dedicated to support type analyses.
1185 * The none type represents that there is no type. The type can be used to
1186 * initialize fields of type* that actually can not contain a type or that
1187 * are initialized for an analysis. There exists exactly one type none.
1188 * This type is not on the type list in ir_prog. It is
1189 * allocated when initializing the type module.
1191 * The following values are set:
1193 * - name: "type_none"
1194 * - state: layout_fixed
1197 /** A variable that contains the only none type. */
1198 extern ir_type *firm_none_type;
1200 /** Returns the none type. */
1201 ir_type *get_none_type(void);
1204 * @page unknown_type
1206 * This type is an auxiliary type dedicated to support type analyses.
1208 * The unknown type represents that there could be a type, but it is not
1209 * known. This type can be used to initialize fields before an analysis (not known
1210 * yet) or to represent the top of a lattice (could not be determined). There exists
1211 * exactly one type unknown. This type is not on the type list in ir_prog. It is
1212 * allocated when initializing the type module.
1214 * The following values are set:
1216 * - name: "type_unknown"
1217 * - state: layout_fixed
1220 /** A variable that contains the only unknown type. */
1221 extern ir_type *firm_unknown_type;
1223 /** Returns the unknown type. */
1224 ir_type *get_unknown_type(void);
1228 * Checks whether a type is atomic.
1229 * @param tp any type
1230 * @return true if type is primitive, pointer or enumeration
1232 int is_atomic_type(const ir_type *tp);
1234 /* --- Support for compound types --- */
1237 * Gets the number of elements in a Firm compound type.
1239 * This is just a comfortability function, because structs and
1240 * classes can often be treated be the same code, but they have
1241 * different access functions to their members.
1243 * @param tp The type (must be struct, union or class).
1245 * @return Number of members in the compound type.
1247 int get_compound_n_members(const ir_type *tp);
1250 * Gets the member of a Firm compound type at position pos.
1252 * @param tp The type (must be struct, union or class).
1253 * @param pos The number of the member.
1255 * @return The member entity at position pos.
1257 * @see get_compound_n_members() for justification of existence.
1259 entity *get_compound_member(const ir_type *tp, int pos);
1261 /** Returns index of member in tp, -1 if not contained. */
1262 int get_compound_member_index(const ir_type *tp, entity *member);
1265 * Checks whether a type is a compound type.
1267 * @param tp - any type
1269 * @return true if the type is class, structure, union or array type.
1271 int is_compound_type(const ir_type *tp);
1274 * Checks, whether a type is a frame type.
1276 int is_frame_type(const ir_type *tp);
1279 * Checks, whether a type is a value parameter type.
1281 int is_value_param_type(const ir_type *tp);
1284 * Checks, whether a type is a lowered type.
1286 int is_lowered_type(const ir_type *tp);
1289 * Makes a new frame type. Frame types are class types,
1290 * so all class access functions work.
1291 * Frame types are not in the global list of types.
1293 ir_type *new_type_frame(ident *name);
1296 * Sets a lowered type for a type. This sets both associations
1297 * and marks lowered_type as a "lowered" one.
1299 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
1302 * Gets the lowered/unlowered type of a type or NULL if this type
1303 * has no lowered/unlowered one.
1305 ir_type *get_associated_type(const ir_type *tp);
1308 * Allocate an area of size bytes aligned at alignment
1309 * at the start or the end of a frame type.
1310 * The frame type must already have a fixed layout.
1312 * @param frame_type a frame type
1313 * @param size the size of the entity
1314 * @param alignment the alignment of the entity
1315 * @param at_start if true, put the area at the frame type's start, else at end
1317 * @return the entity representing the area
1319 entity *frame_alloc_area(ir_type *frame_type, int size, int alignment, int at_start);
1321 /*-----------------------------------------------------------------*/
1323 /*-----------------------------------------------------------------*/
1326 * Outputs a unique number for this type if libfirm is compiled for
1327 * debugging, (configure with --enable-debug) else returns the address
1328 * of the type cast to long.
1330 long get_type_nr(const ir_type *tp);
1332 #endif /* _FIRM_TR_TYPE_H_ */