4 * Project: libFIRM <br>
5 * File name: ir/tr/type.h <br>
6 * Purpose: Representation of types. <br>
7 * Author: Goetz Lindenmaier <br>
10 * Copyright: (c) 2001-2003 Universität Karlsruhe <br>
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. <br>
15 * Datastructure to hold type information.
17 * This module supplies a datastructure to represent all types
18 * known in the compiled program. This includes types specified
19 * in the program as well as types defined by the language. In the
20 * view of the intermediate representation there is no difference
21 * between these types. Finally it specifies some auxiliary types.
23 * There exist several kinds of types, arranged by the structure of
24 * the type. A type is described by a set of attributes. Some of
25 * these attributes are common to all types, others depend on the
28 * Types are different from the modes defined in irmode: Types are
29 * on the level of the programming language, modes at the level of
30 * the target processor.
38 # include "firm_types.h"
40 # include "firm_common.h"
44 # include "tr_inheritance.h"
47 * An abstract data type to represent types.
49 * This is the abstract data type with which any type known in the
50 * compiled program can be represented. This includes types specified
51 * in the program as well as types defined by the language. In the
52 * view of the intermediate representation there is no difference
53 * between these types.
55 * There exist several kinds of types, arranged by the structure of
56 * the type. These are distinguished by a type opcode.
57 * A type is described by a set of attributes. Some of these attributes
58 * are common to all types, others depend on the kind of the type.
60 * The following describes the common attributes. They can only be
61 * accessed by the functions given below.
63 * The common fields are:
65 * - firm_kind: A firm_kind tag containing k_type. This is useful
66 * for dynamically checking whether a node is a type node.
67 * - type_op: A tp_op specifying the kind of the type.
68 * - mode: The mode to be used to represent the type on a machine.
69 * - name: An identifier specifying the name of the type. To be
70 * set by the frontend.
71 * - size: The size of the type, i.e. an entity of this type will
72 * occupy size bits in memory. In several cases this is
73 * determined when fixing the layout of this type (class,
74 * struct, union, array, enumeration).
75 * - alignment The alignment of the type, i.e. an entity of this type will
76 * be allocated an an address in memory with this alignment.
77 * In several cases this is determined when fixing the layout
78 * of this type (class, struct, union, array)
79 * - state: The state of the type. The state represents whether the
80 * layout of the type is undefined or fixed (values: layout_undefined
81 * or layout_fixed). Compound types can have an undefined
82 * layout. The layout of the basic types primitive and pointer
83 * is always layout_fixed. If the layout of
84 * compound types is fixed all entities must have an offset
85 * and the size of the type must be set.
86 * A fixed layout for enumeration types means that each enumeration
87 * is associated with an implementation value.
88 * - visit: A counter for walks of the type information.
89 * - link: A void* to associate some additional information with the type.
91 * These fields can only be accessed via access functions.
93 * Depending on the value of @c type_op, i.e., depending on the kind of the
94 * type the adt contains further attributes. These are documented below.
98 * @link class_type class @endlink, @link struct_type struct @endlink,
99 * @link method_type method @endlink, @link union_type union @endlink,
100 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
101 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
104 * mode maybe not global field??
106 #ifndef _IR_TYPE_TYPEDEF_
107 #define _IR_TYPE_TYPEDEF_
108 typedef struct ir_type ir_type;
111 # include "type_or_entity.h"
113 /** frees all entities associated with a type.
114 Does not free array entity.
115 Warning: make sure these entities are not referenced anywhere else.
117 void free_type_entities(ir_type *tp);
119 /** Frees the memory used by the type.
121 * Removes the type from the type list. Does not free the entities
122 * belonging to the type, except for the array element entity. Does
123 * not free if tp is "none" or "unknown". Frees entities in value
124 * param subtypes of method types!!! Make sure these are not
125 * referenced any more. Further make sure there is no pointer type
126 * that refers to this type. */
127 void free_type(ir_type *tp);
129 const tp_op*get_type_tpop(const ir_type *tp);
130 ident* get_type_tpop_nameid(const ir_type *tp);
131 const char* get_type_tpop_name(const ir_type *tp);
132 tp_opcode get_type_tpop_code(const ir_type *tp);
134 ident* get_type_ident(const ir_type *tp);
135 void set_type_ident(ir_type *tp, ident* id);
136 const char* get_type_name(const ir_type *tp);
138 /** This enumeration flags the visibility of entities and types.
140 * This is necessary for partial compilation.
141 * We rely on the ordering of the flags.
144 visibility_local, /**< The entity is only visible locally. This is the default for
146 The type is only visible locally. All instances are allocated
147 locally, and no pointer to entities of this type are passed
148 out of this compilation unit. */
149 visibility_external_visible, /**< The entity is visible to other external program parts, but
150 it is defined here. It may not be optimized away. The entity must
152 For types: entities of this type can be accessed externally. No
153 instances of this type are allocated externally. */
154 visibility_external_allocated /**< The entity is defined and allocated externally. This compilation
155 must not allocate memory for this entity. The entity must
156 be static_allocated. This can also be an external defined
158 For types: entities of this type are allocated and accessed from
159 external code. Default for types. */
162 /** The visibility of a type.
164 * The visibility of a type indicates, whether entities of this type
165 * are accessed or allocated in external code.
167 * An entity of a type is allocated in external code, if the external
168 * code declares a variable of this type, or dynamically allocates
169 * an entity of this type. If the external code declares a (compound)
170 * type, that contains entities of this type, the visibility also
171 * must be external_allocated.
173 * The visibility must be higher than that of all entities, if the
174 * type is a compound. Here it is questionable, what happens with
175 * static entities. If these are accessed external by direct reference,
176 * (a static call to a method, that is also in the dispatch table)
177 * it should not affect the visibility of the type.
180 * @@@ Do we need a visibility for types?
181 * I change the layout of types radically when doing type splitting.
182 * I need to know, which fields of classes are accessed in the RTS,
183 * e.g., [_length. I may not move [_length to the split part.
184 * The layout though, is a property of the type.
186 * One could also think of changing the mode of a type ...
188 * But, we could also output macros to access the fields, e.g.,
189 * ACCESS_[_length (X) X->length // conventional
190 * ACCESS_[_length (X) X->_split_ref->length // with type splitting
192 * For now I implement this function, that returns the visibility
193 * based on the visibility of the entities of a compound ...
195 * This function returns visibility_external_visible if one or more
196 * entities of a compound type have visibility_external_visible.
197 * Entities of types are never visibility_external_allocated (right?).
198 * Else returns visibility_local.
200 visibility get_type_visibility (const ir_type *tp);
201 void set_type_visibility (ir_type *tp, visibility v);
205 /** The state of the type layout. */
207 layout_undefined, /**< The layout of this type is not defined.
208 Address computation to access fields is not
209 possible, fields must be accessed by Sel
210 nodes. This is the default value except for
211 pointer, primitive and method types. */
212 layout_fixed /**< The layout is fixed, all component/member entities
213 have an offset assigned. Size of the type is known.
214 Arrays can be accessed by explicit address
215 computation. Default for pointer, primitive and method
219 /** Returns a human readable string for the enum entry. */
220 const char *get_type_state_name(type_state s);
222 /** Returns the type layout state of a type. */
223 type_state get_type_state(const ir_type *tp);
225 /** Sets the type layout state of a type.
227 * For primitives, pointer and method types the layout is always fixed.
228 * This call is legal but has no effect.
230 void set_type_state(ir_type *tp, type_state state);
232 /** Returns the mode of a type.
234 * Returns NULL for all non atomic types.
236 ir_mode* get_type_mode(const ir_type *tp);
238 /** Sets the mode of a type.
240 * Only has an effect on primitive, enumeration and pointer types.
242 void set_type_mode(ir_type *tp, ir_mode* m);
244 /** Returns the size of a type in bytes, returns -1 if the size is NOT
245 * a byte size, i.e. not dividable by 8. */
246 int get_type_size_bytes(const ir_type *tp);
248 /** Returns the size of a type in bits. */
249 int get_type_size_bits(const ir_type *tp);
251 /** Sets the size of a type in bytes.
253 * For primitive, enumeration, pointer and method types the size
254 * is always fixed. This call is legal but has no effect.
256 void set_type_size_bytes(ir_type *tp, int size);
258 /** Sets the size of a type in bits.
260 * For primitive, enumeration, pointer and method types the size
261 * is always fixed. This call is legal but has no effect.
263 void set_type_size_bits(ir_type *tp, int size);
265 /** Returns the alignment of a type in bytes.
267 * Returns -1 if the alignment is NOT
268 * a byte size, i.e. not dividable by 8. Calls get_type_alignment_bits(). */
269 int get_type_alignment_bytes(ir_type *tp);
271 /** Returns the alignment of a type in bits.
273 * If the alignment of a type is
274 * not set, it is calculated here according to the following rules:
275 * -#.) if a type has a mode, the alignment is the mode size.
276 * -#.) compound types have the alignment of there biggest member.
277 * -#.) array types have the alignment of there element type.
278 * -#.) method types return 0 here.
279 * -#.) all other types return 8 here (i.e. aligned at byte).
281 int get_type_alignment_bits(ir_type *tp);
283 /** Sets the alignment of a type in bytes. */
284 void set_type_alignment_bytes(ir_type *tp, int size);
286 /** Sets the alignment of a type in bits.
288 * For method types the alignment is always fixed.
289 * This call is legal but has no effect.
291 void set_type_alignment_bits(ir_type *tp, int size);
293 unsigned long get_type_visited(const ir_type *tp);
294 void set_type_visited(ir_type *tp, unsigned long num);
295 /* Sets visited field in type to type_visited. */
296 void mark_type_visited(ir_type *tp);
297 int type_visited(const ir_type *tp);
298 int type_not_visited(const ir_type *tp);
300 /** Returns the associated link field of a type. */
301 void* get_type_link(const ir_type *tp);
302 /** Sets the associated link field of a type. */
303 void set_type_link(ir_type *tp, void *l);
306 * Visited flag to traverse the type information.
308 * Increase this flag by one before traversing the type information
309 * using inc_master_type_visited().
310 * Mark type nodes as visited by mark_type_visited(ir_type).
311 * Check whether node was already visited by type_visited(ir_type)
312 * and type_not_visited(ir_type).
313 * Or use the function to walk all types.
317 void set_master_type_visited(unsigned long val);
318 unsigned long get_master_type_visited(void);
319 void inc_master_type_visited(void);
322 * Checks whether a pointer points to a type.
324 * @param thing an arbitrary pointer
327 * true if the thing is a type, else false
329 int is_type (const void *thing);
332 * Checks whether two types are structurally equal.
334 * @param typ1 the first type
335 * @param typ2 the second type
338 * true if the types are equal, else false.
340 * Types are equal if :
341 * - they are the same type kind
342 * - they have the same name
343 * - they have the same mode (if applicable)
344 * - they have the same type_state and, ev., the same size
345 * - they are class types and have:
346 * - the same members (see same_entity in entity.h)
347 * - the same supertypes -- the C-pointers are compared --> no recursive call.
348 * - the same number of subtypes. Subtypes are not compared,
349 * as this could cause a cyclic test.
350 * - the same peculiarity
351 * - they are structure types and have the same members
352 * - they are method types and have
353 * - the same parameter types
354 * - the same result types
355 * - they are union types and have the same members
356 * - they are array types and have
357 * - the same number of dimensions
358 * - the same dimension bounds
359 * - the same dimension order
360 * - the same element type
361 * - they are enumeration types and have the same enumerator names
362 * - they are pointer types and have the identical points_to type
363 * (i.e., the same C-struct to represent the type, type_id is skipped.
364 * This is to avoid endless recursions; with pointer types cyclic
365 * type graphs are possible.)
367 int equal_type(ir_type *typ1, ir_type *typ2);
370 * Checks whether two types are structural comparable.
372 * @param st pointer type
373 * @param lt pointer type
376 * true if type st is smaller than type lt, i.e. whenever
377 * lt is expected a st can be used.
379 * - they are the same type kind
380 * - mode(st) < mode (lt) (if applicable)
381 * - they are class types and st is (transitive) subtype of lt,
382 * - they are structure types and
383 * - the members of st have exactly one counterpart in lt with the same name,
384 * - the counterpart has a bigger type.
385 * - they are method types and have
386 * - the same number of parameter and result types,
387 * - the parameter types of st are smaller than those of lt,
388 * - the result types of st are smaller than those of lt
389 * - they are union types and have the members of st have exactly one
390 * @return counterpart in lt and the type is smaller
391 * - they are array types and have
392 * - the same number of dimensions
393 * - all bounds of lt are bound of st
394 * - the same dimension order
395 * - the same element type
397 * - the element type of st is smaller than that of lt
398 * - the element types have the same size and fixed layout.
399 * - they are enumeration types and have the same enumerator names
400 * - they are pointer types and have the points_to type of st is
401 * @return smaller than the points_to type of lt.
404 int smaller_type (ir_type *st, ir_type *lt);
407 * @page class_type Representation of a class type
409 * If the type opcode is set to type_class the type represents class
410 * types. A list of fields and methods is associated with a class.
411 * Further a class can inherit from and bequest to other classes.
413 * The following attributes are private to this type kind:
414 * - member: All entities belonging to this class. This are method entities
415 * which have type_method or fields that can have any of the
416 * following type kinds: type_class, type_struct, type_union,
417 * type_array, type_enumeration, type_pointer, type_primitive.
419 * The following two are dynamic lists that can be grown with an "add_" function,
422 * - subtypes: A list of direct subclasses.
424 * - supertypes: A list of direct superclasses.
426 * - peculiarity: The peculiarity of this class. If the class is of peculiarity
427 * "description" it only is a description of requirements to a class,
428 * as, e.g., a Java interface. The class will never be allocated.
429 * Peculiarity inherited is only possible for entities. An entity
430 * is of peculiarity inherited if the compiler generated the entity
431 * to explicitly resolve inheritance. An inherited method entity has
433 * Values: description, existent, inherited. Default: existent.
435 * - type_info: An entity representing the type information of this class.
436 * This entity can be of arbitrari type, Firm did not use it yet.
437 * It allows to express the coupling of a type with an entity
438 * representing this type. This information is useful for lowering
439 * of InstOf and TypeChk nodes. Default: NULL
441 * - final: A final class is always a leaf in the class hierarchy. Final
442 * classes cannot be super classes of other ones. As this information
443 * can only be computed in whole world compilations, we allow to
444 * set this flag. It is used in optimizations if get_opt_closed_world()
445 * is false. Default: false
448 /** Creates a new class type. */
449 ir_type *new_type_class (ident *name);
451 /** Creates a new class type with debug information. */
452 ir_type *new_d_type_class (ident *name, dbg_info *db);
454 /* --- manipulate private fields of class type --- */
456 /** Adds the entity as member of the class. */
457 void add_class_member (ir_type *clss, entity *member);
459 /** Returns the number of members of this class. */
460 int get_class_n_members (const ir_type *clss);
462 /** Returns the member at position pos, 0 <= pos < n_member */
463 entity *get_class_member (const ir_type *clss, int pos);
465 /** Returns index of mem in clss, -1 if not contained. */
466 int get_class_member_index(const ir_type *clss, entity *mem);
468 /** Finds the member with name 'name'. If several members with the same
469 * name returns one of them. Returns NULL if no member found. */
470 entity *get_class_member_by_name(ir_type *clss, ident *name);
472 /** Overwrites the member at position pos, 0 <= pos < n_member with
473 * the passed entity. */
474 void set_class_member (ir_type *clss, entity *member, int pos);
476 /** Replaces complete member list in class type by the list passed.
478 * Copies the list passed. This function is necessary to reduce the number of members.
479 * members is an array of entities, num the size of this array. Sets all
480 * owners of the members passed to clss. */
481 void set_class_members (ir_type *clss, entity *members[], int arity);
483 /** Finds member in the list of members and removes it.
485 * Shrinks the member list, so iterate from the end!!!
486 * Does not deallocate the entity. */
487 void remove_class_member(ir_type *clss, entity *member);
490 /** Adds subtype as subtype to clss.
492 * Checks whether clss is a supertype of subtype. If not
493 * adds also clss as supertype to subtype. */
494 void add_class_subtype (ir_type *clss, ir_type *subtype);
496 /** Returns the number of subtypes */
497 int get_class_n_subtypes (const ir_type *clss);
499 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
500 ir_type *get_class_subtype (ir_type *clss, int pos);
502 /** Returns the index to access subclass as subtype of class.
504 * If subclass is no direct subtype of class returns -1.
506 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
508 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
510 * Does not set the corresponding supertype relation for subtype: this might
511 * be a different position! */
512 void set_class_subtype (ir_type *clss, ir_type *subtype, int pos);
514 /** Finds subtype in the list of subtypes and removes it */
515 void remove_class_subtype(ir_type *clss, ir_type *subtype);
517 /* Convenience macros */
518 #define add_class_derived_type(clss, drvtype) add_class_subtype(clss, drvtype)
519 #define get_class_n_derived_types(clss) get_class_n_subtypes(clss)
520 #define get_class_derived_type(clss, pos) get_class_subtype(clss, pos)
521 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
522 #define set_class_derived_type(clss, drvtype, pos) set_class_subtype(clss, drvtype, pos)
523 #define remove_class_derived_type(clss, drvtype) remove_class_subtype(clss, drvtype)
525 /** Adds supertype as supertype to class.
527 * Checks whether clss is a subtype of supertype. If not
528 * adds also clss as subtype to supertype. */
529 void add_class_supertype (ir_type *clss, ir_type *supertype);
531 /** Returns the number of supertypes */
532 int get_class_n_supertypes (const ir_type *clss);
534 /** Returns the index to access superclass as supertype of class.
536 * If superclass is no direct supertype of class returns -1.
538 int get_class_supertype_index(ir_type *clss, ir_type *super_clss);
540 /** Gets the supertype at position pos, 0 <= pos < n_supertype. */
541 ir_type *get_class_supertype (ir_type *clss, int pos);
543 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
545 * Does not set the corresponding subtype relation for supertype: this might
546 * be at a different position! */
547 void set_class_supertype (ir_type *clss, ir_type *supertype, int pos);
549 /** Finds supertype in the list of supertypes and removes it */
550 void remove_class_supertype(ir_type *clss, ir_type *supertype);
552 /** Convenience macro */
553 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
554 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
555 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
556 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
557 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
558 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
560 /** Convenience macro */
561 #define add_class_base_type(clss, basetype) add_class_supertype(clss, basetype)
562 #define get_class_n_base_types(clss) get_class_n_supertypes(clss)
563 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
564 #define get_class_base_type(clss, pos) get_class_supertype(clss, pos)
565 #define set_class_base_type(clss, basetype, pos) set_class_supertype(clss, basetype, pos)
566 #define remove_class_base_type(clss, basetype) remove_class_supertype(clss, basetype)
568 /** This enumeration flags the peculiarity of entities and types. */
569 typedef enum peculiarity {
570 peculiarity_description, /**< Represents only a description. The entity/type is never
571 allocated, no code/data exists for this entity/type.
572 @@@ eventually rename to descriptive (adjective as the others!)*/
573 peculiarity_inherited, /**< Describes explicitly that other entities are
574 inherited to the owner of this entity.
575 Overwrites must refer to at least one other
576 entity. If this is a method entity there exists
577 no irg for this entity, only for one of the
580 peculiarity_existent /**< The entity/type (can) exist.
581 @@@ eventually rename to 'real' i.e., 'echt'
582 This serves better as opposition to description _and_ inherited.*/
584 const char *get_peculiarity_string(peculiarity p);
586 /** Returns the peculiarity of the class. */
587 peculiarity get_class_peculiarity (const ir_type *clss);
588 /** Sets the peculiarity of the class. */
589 void set_class_peculiarity (ir_type *clss, peculiarity pec);
591 /** Returns the type info entity of a class. */
592 entity *get_class_type_info(const ir_type *clss);
594 /** Set a type info entity for the class. */
595 void set_class_type_info(ir_type *clss, entity *ent);
597 /** Returns non-zero if a class is final. */
598 int is_class_final(const ir_type *clss);
600 /** Sets if a class is final. */
601 void set_class_final(ir_type *clss, int flag);
603 /* Set and get a class' dfn --
604 @todo This is an undocumented field, subject to change! */
605 void set_class_dfn (ir_type *clss, int dfn);
606 int get_class_dfn (const ir_type *clss);
608 /** Returns true if a type is a class type. */
609 int is_Class_type(const ir_type *clss);
612 * @page struct_type Representation of a struct type
614 * Type_strct represents aggregate types that consist of a list
616 * The following attributes are private to this type kind:
617 * - member: All entities belonging to this class. This are the fields
618 * that can have any of the following types: type_class,
619 * type_struct, type_union, type_array, type_enumeration,
620 * type_pointer, type_primitive.
621 * This is a dynamic list that can be grown with an "add_" function,
623 * This is a dynamic list that can be grown with an "add_" function,
626 /** Creates a new type struct */
627 ir_type *new_type_struct (ident *name);
628 /** Creates a new type struct with debug information. */
629 ir_type *new_d_type_struct (ident *name, dbg_info* db);
631 /* --- manipulate private fields of struct --- */
633 /** Adds the entity as member of the struct. */
634 void add_struct_member (ir_type *strct, entity *member);
636 /** Returns the number of members of this struct. */
637 int get_struct_n_members (const ir_type *strct);
639 /** Returns the member at position pos, 0 <= pos < n_member */
640 entity *get_struct_member (const ir_type *strct, int pos);
642 /** Returns index of member in strct, -1 if not contained. */
643 int get_struct_member_index(const ir_type *strct, entity *member);
645 /** Overwrites the member at position pos, 0 <= pos < n_member with
646 the passed entity. */
647 void set_struct_member (ir_type *strct, int pos, entity *member);
649 /** Finds member in the list of members and removes it. */
650 void remove_struct_member (ir_type *strct, entity *member);
652 /** Returns true if a type is a struct type. */
653 int is_Struct_type(const ir_type *strct);
656 * @page method_type Representation of a method type
658 * A method type represents a method, function or procedure type.
659 * It contains a list of the parameter and result types, as these
660 * are part of the type description. These lists should not
661 * be changed by a optimization, as a change creates a new method
662 * type. Therefore optimizations should allocated new method types.
663 * The set_ routines are only for construction by a frontend.
665 * - n_params: Number of parameters to the procedure.
666 * A procedure in FIRM has only call by value parameters.
668 * - param_type: A list with the types of parameters. This list is ordered.
669 * The nth type in this list corresponds to the nth element
670 * in the parameter tuple that is a result of the start node.
671 * (See ircons.h for more information.)
674 * A list of entities (whose owner is a struct private to the
675 * method type) that represent parameters passed by value.
677 * - n_res: The number of results of the method. In general, procedures
678 * have zero results, functions one.
680 * - res_type: A list with the types of parameters. This list is ordered.
681 * The nth type in this list corresponds to the nth input to
682 * Return nodes. (See ircons.h for more information.)
685 * A list of entities (whose owner is a struct private to the
686 * method type) that represent results passed by value.
689 /* These macros define the suffixes for the types and entities used
690 to represent value parameters / results. */
691 #define VALUE_PARAMS_SUFFIX "val_param"
692 #define VALUE_RESS_SUFFIX "val_res"
694 /** Create a new method type.
696 * @param name the name (ident) of this type
697 * @param n_param the number of parameters
698 * @param n_res the number of results
700 * The arrays for the parameter and result types are not initialized by
703 ir_type *new_type_method (ident *name, int n_param, int n_res);
705 /** Create a new method type with debug information.
707 * @param name the name (ident) of this type
708 * @param n_param the number of parameters
709 * @param n_res the number of results
710 * @param db user defined debug information
712 * The arrays for the parameter and result types are not initialized by
715 ir_type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
717 /* -- manipulate private fields of method. -- */
719 /** Returns the number of parameters of this method. */
720 int get_method_n_params (const ir_type *method);
722 /** Returns the type of the parameter at position pos of a method. */
723 ir_type *get_method_param_type(ir_type *method, int pos);
724 /** Sets the type of the parameter at position pos of a method.
725 Also changes the type in the pass-by-value representation by just
726 changing the type of the corresponding entity if the representation is constructed. */
727 void set_method_param_type(ir_type *method, int pos, ir_type *tp);
728 /** Returns an entity that represents the copied value argument. Only necessary
729 for compounds passed by value. This information is constructed only on demand. */
730 entity *get_method_value_param_ent(ir_type *method, int pos);
732 * Returns a type that represents the copied value arguments.
734 ir_type *get_method_value_param_type(const ir_type *method);
736 /** Returns the number of results of a method type. */
737 int get_method_n_ress (const ir_type *method);
738 /** Returns the return type of a method type at position pos. */
739 ir_type *get_method_res_type(ir_type *method, int pos);
740 /** Sets the type of the result at position pos of a method.
741 Also changes the type in the pass-by-value representation by just
742 changing the type of the corresponding entity if the representation is constructed. */
743 void set_method_res_type(ir_type *method, int pos, ir_type *tp);
744 /** Returns an entity that represents the copied value result. Only necessary
745 for compounds passed by value. This information is constructed only on demand. */
746 entity *get_method_value_res_ent(ir_type *method, int pos);
749 * Returns a type that represents the copied value results.
751 ir_type *get_method_value_res_type(const ir_type *method);
754 * This enum flags the variadicity of methods (methods with a
755 * variable amount of arguments (e.g. C's printf). Default is
758 typedef enum variadicity {
759 variadicity_non_variadic, /**< non variadic */
760 variadicity_variadic /**< variadic */
763 /** Returns the null-terminated name of this variadicity. */
764 const char *get_variadicity_name(variadicity vari);
766 /** Returns the variadicity of a method. */
767 variadicity get_method_variadicity(const ir_type *method);
769 /** Sets the variadicity of a method. */
770 void set_method_variadicity(ir_type *method, variadicity vari);
773 * Returns the first variadic parameter index of a type.
774 * If this index was NOT set, the index of the last parameter
775 * of the method type plus one is returned for variadic functions.
776 * Non-variadic function types always return -1 here.
778 int get_method_first_variadic_param_index(const ir_type *method);
781 * Sets the first variadic parameter index. This allows to specify
782 * a complete call type (containing the type of all parameters)
783 * but still have the knowledge, which parameter must be passed as
786 void set_method_first_variadic_param_index(ir_type *method, int index);
789 * additional method type properties:
790 * Tell about special properties of a method type. Some
791 * of these may be discovered by analyses.
794 mtp_no_property = 0x00000000, /**< no additional properties, default */
795 mtp_property_const = 0x00000001, /**< This method did not access memory and calculates
796 its return values solely from its parameters.
797 GCC: __attribute__((const)). */
798 mtp_property_pure = 0x00000002, /**< This method did NOT write to memory and calculates
799 its return values solely form its parameters and
800 the memory they points to (or global vars).
801 GCC: __attribute__((pure)). */
802 mtp_property_noreturn = 0x00000004, /**< This method did not return due to an aborting system
804 GCC: __attribute__((noreturn)). */
805 mtp_property_nothrow = 0x00000008, /**< This method cannot throw an exception.
806 GCC: __attribute__((nothrow)). */
807 mtp_property_naked = 0x00000010, /**< This method is naked.
808 GCC: __attribute__((naked)). */
809 mtp_property_malloc = 0x00000020, /**< This method returns newly allocate memory.
810 GCC: __attribute__((malloc)). */
811 mtp_property_intrinsic = 0x00000040, /**< This method is intrinsic. It is expected that
812 a lowering phase will remove all calls to it. */
813 mtp_property_inherited = 0x40000000 /**< used only in irg's, means property is inherited
815 } mtp_additional_property;
817 /** Returns the mask of the additional graph properties. */
818 unsigned get_method_additional_properties(const ir_type *method);
820 /** Sets the mask of the additional graph properties. */
821 void set_method_additional_properties(ir_type *method, unsigned property_mask);
823 /** Sets one additional graph property. */
824 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
827 * calling conventions: lower 24 bits are the number of register parameters,
828 * upper 8 encode the calling conventions
831 cc_reg_param = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
832 This flag may be set as default on some architectures. */
833 cc_last_on_top = 0x02000000, /**< The last non-register parameter is transmitted on top of
834 the stack. This is equivalent to the stdcall or pascal
835 calling convention. If this flag is not set, the first
836 non-register parameter is used (cdecl calling convention) */
837 cc_callee_clear_stk = 0x04000000, /**< The callee clears the stack. This forbids variadic
838 function calls (stdcall). */
839 cc_this_call = 0x08000000, /**< The first parameter is a this pointer and is transmitted
843 cc_bits = (0xFF << 24) /**< the calling convention bits */
844 } calling_convention;
846 /* some often used cases: made as defines for firmjni */
847 /** cdecl calling convention */
848 #define cc_cdecl_set (0)
849 /** stdcall calling convention */
850 #define cc_stdcall_set cc_callee_clear_stk
851 /** fastcall calling convention */
852 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
854 /** return the default calling convention for method types */
855 unsigned get_default_cc_mask(void);
858 * check for the CDECL calling convention
860 #define IS_CDECL(cc_mask) (((cc_mask) & cc_bits) == cc_cdecl_set)
863 * check for the STDCALL calling convention
865 #define IS_STDCALL(cc_mask) (((cc_mask) & cc_bits) == cc_stdcall_set)
868 * check for the FASTCALL calling convention
870 #define IS_FASTCALL(cc_mask) (((cc_mask) & cc_bits) == cc_fastcall_set)
873 * set the CDECL convention bits
875 #define SET_CDECL(cc_mask) (((cc_mask) & ~cc_bits) | cc_cdecl_set)
878 * set the STDCALL convention bits
880 #define SET_STDCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_stdcall_set)
883 * set the FASTCALL convention bits
885 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
887 /** Returns the calling convention of an entities graph. */
888 unsigned get_method_calling_convention(const ir_type *method);
890 /** Sets the calling convention of an entities graph. */
891 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
893 /** Returns the number of registers parameters, 0 means default. */
894 unsigned get_method_n_regparams(ir_type *method);
896 /** Sets the number of registers parameters, 0 means default. */
897 void set_method_n_regparams(ir_type *method, unsigned n_regs);
899 /** Returns true if a type is a method type. */
900 int is_Method_type (const ir_type *method);
903 * @page union_type Representation of a union (variant) type.
905 * The union type represents union types.
906 * - n_types: Number of unioned types.
907 * - members: Entities for unioned types. Fixed length array.
908 * This is a dynamic list that can be grown with an "add_" function,
911 /** Creates a new type union. */
912 ir_type *new_type_union (ident *name);
914 /** Creates a new type union with debug information. */
915 ir_type *new_d_type_union (ident *name, dbg_info* db);
917 /* --- manipulate private fields of struct --- */
919 /** Returns the number of unioned types of this union */
920 int get_union_n_members (const ir_type *uni);
922 /** Adds a new entity to a union type */
923 void add_union_member (ir_type *uni, entity *member);
925 /** Returns the entity at position pos of a union */
926 entity *get_union_member (const ir_type *uni, int pos);
928 /** Returns index of member in uni, -1 if not contained. */
929 int get_union_member_index(const ir_type *uni, entity *member);
931 /** Overwrites a entity at position pos in a union type. */
932 void set_union_member (ir_type *uni, int pos, entity *member);
934 /** Finds member in the list of members and removes it. */
935 void remove_union_member (ir_type *uni, entity *member);
937 /** Returns true if a type is a union type. */
938 int is_Union_type (const ir_type *uni);
941 * @page array_type Representation of an array type
943 * The array type represents rectangular multi dimensional arrays.
944 * The constants representing the bounds must be allocated to
945 * get_const_code_irg() by setting current_ir_graph accordingly.
947 * - n_dimensions: Number of array dimensions.
948 * - *lower_bound: Lower bounds of dimensions. Usually all 0.
949 * - *upper_bound: Upper bounds or dimensions.
950 * - *element_type: The type of the array elements.
951 * - *element_ent: An entity for the array elements to be used for
952 * element selection with Sel.
954 * Do we need several entities? One might want
955 * to select a dimension and not a single element in case of multi
956 * dimensional arrays.
959 /** Create a new type array.
961 * Sets n_dimension to dimension and all dimension entries to NULL.
962 * Initializes order to the order of the dimensions.
963 * The entity for array elements is built automatically.
964 * Set dimension sizes after call to constructor with set_* routines.
966 ir_type *new_type_array (ident *name, int n_dimensions,
967 ir_type *element_type);
969 /** Create a new type array with debug information.
971 * Sets n_dimension to dimension and all dimension entries to NULL.
972 * Initializes order to the order of the dimensions.
973 * The entity for array elements is built automatically.
974 * Set dimension sizes after call to constructor with set_* routines.
975 * A legal array type must have at least one dimension set.
977 ir_type *new_d_type_array (ident *name, int n_dimensions,
978 ir_type *element_type, dbg_info* db);
980 /* --- manipulate private fields of array type --- */
982 /** Returns the number of array dimensions of this type. */
983 int get_array_n_dimensions (const ir_type *array);
986 * Allocates Const nodes of mode_I for one array dimension.
987 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
989 void set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
992 * Sets the bounds for one array dimension.
993 * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
995 void set_array_bounds (ir_type *array, int dimension, ir_node *lower_bound,
996 ir_node *upper_bound);
997 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
998 void set_array_lower_bound (ir_type *array, int dimension, ir_node *lower_bound);
1000 /** Allocates Const nodes of mode_I for the lower bound of an array
1001 dimension, i.e. [lower,upper[ */
1002 void set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound);
1004 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
1005 void set_array_upper_bound (ir_type *array, int dimension, ir_node *upper_bound);
1007 /** Allocates Const nodes of mode_I for the upper bound of an array
1008 dimension, i.e. [lower,upper[. */
1009 void set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound);
1011 /** Returns true if lower bound != Unknown. */
1012 int has_array_lower_bound (const ir_type *array, int dimension);
1013 /** Returns the lower bound of an array. */
1014 ir_node * get_array_lower_bound (const ir_type *array, int dimension);
1015 /** Works only if bound is Const node with tarval that can be converted to long. */
1016 long get_array_lower_bound_int (const ir_type *array, int dimension);
1017 /** returns true if lower bound != Unknown */
1018 int has_array_upper_bound (const ir_type *array, int dimension);
1019 /** Returns the upper bound of an array. */
1020 ir_node * get_array_upper_bound (const ir_type *array, int dimension);
1021 /** Works only if bound is Const node with tarval that can be converted to long. */
1022 long get_array_upper_bound_int (const ir_type *array, int dimension);
1024 /** Sets an array dimension to a specific order. */
1025 void set_array_order (ir_type *array, int dimension, int order);
1027 /** Returns the order of an array dimension. */
1028 int get_array_order (const ir_type *array, int dimension);
1030 /** Find the array dimension that is placed at order ord. */
1031 int find_array_dimension(const ir_type *array, int order);
1033 /** Sets the array element type. */
1034 void set_array_element_type (ir_type *array, ir_type* tp);
1036 /** Gets the array element type. */
1037 ir_type *get_array_element_type (ir_type *array);
1039 /** Sets the array element entity. */
1040 void set_array_element_entity (ir_type *array, entity *ent);
1042 /** Get the array element entity. */
1043 entity *get_array_element_entity (const ir_type *array);
1045 /** Returns true if a type is an array type. */
1046 int is_Array_type(const ir_type *array);
1049 * @page enumeration_type Representation of an enumeration type
1051 * Enumeration types need not necessarily be represented explicitly
1052 * by Firm types, as the frontend can lower them to integer constants as
1053 * well. For debugging purposes or similar tasks this information is useful.
1055 * - *enum: The target values representing the constants used to
1056 * represent individual enumerations.
1057 * - *enum_nameid: Idents containing the source program name of the enumeration
1060 /** Create a new type enumeration -- set the enumerators independently. */
1061 ir_type *new_type_enumeration (ident *name, int n_enums);
1063 /** Create a new type enumeration with debug information -- set the enumerators independently. */
1064 ir_type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
1066 /* --- manipulate fields of enumeration type. --- */
1068 /** Returns the number of enumeration values of this enumeration */
1069 int get_enumeration_n_enums (const ir_type *enumeration);
1071 /** Sets the enumeration value at a given position. */
1072 void set_enumeration_enum (ir_type *enumeration, int pos, tarval *con);
1074 /** Returns the enumeration value at a given position. */
1075 tarval *get_enumeration_enum (const ir_type *enumeration, int pos);
1077 /** Assign an ident to an enumeration value at a given position. */
1078 void set_enumeration_nameid (ir_type *enumeration, int pos, ident *id);
1080 /** Returns the assigned ident of an enumeration value at a given position. */
1081 ident *get_enumeration_nameid (const ir_type *enumeration, int pos);
1083 /** Returns the assigned name of an enumeration value at a given position. */
1084 const char *get_enumeration_name(const ir_type *enumeration, int pos);
1086 /** Returns true if a type is a enumeration type. */
1087 int is_Enumeration_type (const ir_type *enumeration);
1090 * @page pointer_type Representation of a pointer type
1092 * The mode of the pointer type must be a reference mode.
1095 * - points_to: The type of the entity this pointer points to.
1098 /** Creates a new type pointer. */
1099 ir_type *new_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode);
1101 /** Creates a new type pointer with debug information. */
1102 ir_type *new_d_type_pointer (ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
1104 /* --- manipulate fields of type_pointer --- */
1106 /** Sets the type to which a pointer points to. */
1107 void set_pointer_points_to_type (ir_type *pointer, ir_type *tp);
1109 /** Returns the type to which a pointer points to. */
1110 ir_type *get_pointer_points_to_type (ir_type *pointer);
1112 /** Returns true if a type is a pointer type. */
1113 int is_Pointer_type (const ir_type *pointer);
1115 /** Returns the first pointer type that has as points_to tp.
1116 * Not efficient: O(#types).
1117 * If not found returns firm_unknown_type. */
1118 ir_type *find_pointer_type_to_type (ir_type *tp);
1121 * @page primitive_type Representation of a primitive type
1123 * Primitive types are types that represent indivisible data values that
1124 * map directly to modes. They don't have a private attribute. The
1125 * important information they carry is held in the common mode field.
1127 /** Creates a new primitive type. */
1128 ir_type *new_type_primitive (ident *name, ir_mode *mode);
1130 /** Creates a new primitive type with debug information. */
1131 ir_type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
1133 /** Returns true if a type is a primitive type. */
1134 int is_Primitive_type (const ir_type *primitive);
1138 * @page none_type The None type
1140 * This type is an auxiliary type dedicated to support type analyses.
1142 * The none type represents that there is no type. The type can be used to
1143 * initialize fields of type* that actually can not contain a type or that
1144 * are initialized for an analysis. There exists exactly one type none.
1145 * This type is not on the type list in ir_prog. It is
1146 * allocated when initializing the type module.
1148 * The following values are set:
1150 * - name: "type_none"
1151 * - state: layout_fixed
1154 /** A variable that contains the only none type. */
1155 extern ir_type *firm_none_type;
1156 /** Returns the none type */
1157 ir_type *get_none_type(void);
1160 * @page unknown_type
1162 * This type is an auxiliary type dedicated to support type analyses.
1164 * The unknown type represents that there could be a type, but it is not
1165 * known. This type can be used to initialize fields before an analysis (not known
1166 * yet) or to represent the top of a lattice (could not be determined). There exists
1167 * exactly one type unknown. This type is not on the type list in ir_prog. It is
1168 * allocated when initializing the type module.
1170 * The following values are set:
1172 * - name: "type_unknown"
1173 * - state: layout_fixed
1176 /** A variable that contains the only unknown type. */
1177 extern ir_type *firm_unknown_type;
1178 /** Returns the unknown type */
1179 ir_type *get_unknown_type(void);
1183 * Checks whether a type is atomic.
1184 * @param tp any type
1185 * @return true if type is primitive, pointer or enumeration
1187 int is_atomic_type(const ir_type *tp);
1189 /* --- Support for compound types --- */
1192 * Gets the number of elements in a firm compound type.
1194 * This is just a comfortability function, because structs and
1195 * classes can often be treated be the same code, but they have
1196 * different access functions to their members.
1198 * @param tp The type (must be struct, union or class).
1200 * @return Number of members in the compound type.
1202 int get_compound_n_members(const ir_type *tp);
1205 * Gets the member of a firm compound type at position pos.
1207 * @param tp The type (must be struct, union or class).
1208 * @param pos The number of the member.
1210 * @return The member entity at position pos.
1212 * @see get_compound_n_members() for justification of existence.
1214 entity *get_compound_member(const ir_type *tp, int pos);
1216 /** Returns index of member in tp, -1 if not contained. */
1217 int get_compound_member_index(const ir_type *tp, entity *member);
1220 * Checks whether a type is compound.
1222 * @param tp - any type
1224 * @return true if the type is class, structure, union or array type.
1226 int is_compound_type(const ir_type *tp);
1229 * Checks, whether a type is a frame type.
1231 int is_frame_type(const ir_type *tp);
1234 * Checks, whether a type is a lowered type.
1236 int is_lowered_type(const ir_type *tp);
1239 * Makes a new frame type. Frame types are class types,
1240 * so all class access functions work.
1241 * Frame types are not in the global list of types.
1243 ir_type *new_type_frame(ident *name);
1246 * Sets a lowered type for a type. This sets both associations
1247 * and marks lowered type as a "lowered" one.
1249 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
1252 * Gets the lowered/unlowered type of a type or NULL if this type
1253 * has no lowered/unlowered one.
1255 ir_type *get_associated_type(const ir_type *tp);
1258 * Allocate an area of size bytes aligned at alignment
1259 * at the start or the end of a frame type.
1260 * The frame type must have already an fixed layout.
1262 * @param frame_type a frame type
1263 * @param size the size of the entity
1264 * @param alignment the alignment of the entity
1265 * @param at_start if true, put the area at the frame type's start, else at end
1267 * @return the entity representing the area
1269 entity *frame_alloc_area(type *frame_type, int size, int alignment, int at_start);
1271 /*-----------------------------------------------------------------*/
1273 /*-----------------------------------------------------------------*/
1276 * Outputs a unique number for this type if libfirm is compiled for
1277 * debugging, (configure with --enable-debug) else returns the address
1278 * of the type cast to long.
1280 long get_type_nr(const ir_type *tp);
1283 # endif /* _TYPE_H_ */