2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of types -- private header.
23 * @author Goetz Lindenmaier, Michael Beck
25 * @see type.h tpop_t.h tpop.h
27 #ifndef FIRM_TR_TYPE_T_H
28 #define FIRM_TR_TYPE_T_H
30 #include "firm_config.h"
34 #include "firm_common.h"
40 cf_none = 0, /**< No flags. */
41 cf_final_class = 1, /**< Set if a class is an final class */
42 cf_interface_class = 2, /**< Set if a class is an "interface" */
43 cf_absctract_class = 4, /**< Set if a class is "abstract" */
46 /** Class type attributes. */
48 ir_entity **members; /**< Array containing the fields and methods of this class. */
49 ir_type **subtypes; /**< Array containing the direct subtypes. */
50 ir_type **supertypes; /**< Array containing the direct supertypes */
51 ir_peculiarity peculiarity; /**< The peculiarity of this class. */
52 ir_entity *type_info; /**< An ir_entity representing this class, used for type info. */
53 int dfn; /**< A number that can be used for 'instanceof' operator. */
54 unsigned vtable_size; /**< The size of the vtable for this class. */
55 unsigned clss_flags; /**< Additional class flags. */
58 /** Struct type attributes. */
60 ir_entity **members; /**< Fields of this struct. No method entities allowed. */
63 /** A (type, ir_entity) pair. */
65 ir_type *tp; /**< A type. */
66 ir_entity *ent; /**< An ir_entity. */
67 ident *param_name; /**< For debugging purposes: the name of the parameter */
70 /** Method type attributes. */
72 int n_params; /**< Number of parameters. */
73 tp_ent_pair *params; /**< Array of parameter type/value entities pairs. */
74 ir_type *value_params; /**< A type whose entities represent copied value arguments. */
75 int n_res; /**< Number of results. */
76 tp_ent_pair *res_type; /**< Array of result type/value ir_entity pairs. */
77 ir_type *value_ress; /**< A type whose entities represent copied value results. */
78 variadicity variadicity; /**< The variadicity of the method. */
79 int first_variadic_param; /**< The index of the first variadic parameter or -1 if non-variadic .*/
80 unsigned additional_properties; /**< Set of additional method properties. */
81 unsigned irg_calling_conv; /**< A set of calling convention flags. */
84 /** Union type attributes. */
86 ir_entity **members; /**< Fields of this union. No method entities allowed. */
89 /** Array type attributes. */
91 int n_dimensions; /**< Number of array dimensions. */
92 ir_node **lower_bound; /**< Lower bounds of dimensions. Usually all 0. */
93 ir_node **upper_bound; /**< Upper bounds or dimensions. */
94 int *order; /**< Ordering of dimensions. */
95 ir_type *element_type; /**< The type of the array elements. */
96 ir_entity *element_ent; /**< entity for the array elements, to be used for
97 element selection with a Sel node. */
100 /** An enumerator constant. */
101 struct ir_enum_const {
102 tarval *value; /**< The constants that represents this enumerator identifier. */
103 ident *nameid; /**< The name of the enumerator identifier. */
104 ir_type *owner; /**< owner type of this enumerator constant. */
107 /** Enum type attributes. */
109 ir_enum_const *enumer; /**< Contains all enumerator constants that represent a member
110 of the enum -- enumerators. */
113 /** Pointer type attributes. */
115 ir_type *points_to; /**< The type of the ir_entity the pointer points to. */
118 /** Primitive type attributes. */
120 ir_type *base_type; /**< For bitfield types: The base primitive type, NULL else. */
125 typedef struct { * No private attr, must be smaller than others! *
129 /** General type attributes. */
131 cls_attr ca; /**< Attributes of a class type */
132 stc_attr sa; /**< Attributes of a struct type */
133 mtd_attr ma; /**< Attributes of a method type */
134 uni_attr ua; /**< Attributes of an union type */
135 arr_attr aa; /**< Attributes of an array type */
136 enm_attr ea; /**< Attributes of an enumeration type */
137 ptr_attr pa; /**< Attributes of a pointer type */
138 pri_attr ba; /**< Attributes of a primitive bitfield type */
141 /** Additional type flags. */
143 tf_none = 0, /**< No flags. */
144 tf_frame_type = 1, /**< Set if this is a frame type. */
145 tf_value_param_type = 2, /**< Set if this is a value param type. */
146 tf_lowered_type = 4, /**< Set if this is a lowered type. */
147 tf_layout_fixed = 8, /**< Set if the layout of a type is fixed */
148 tf_global_type = 16, /**< Set only for the global type */
149 tf_tls_type = 32, /**< Set only for the tls type */
153 * An abstract data type to represent types.
155 * This is the abstract data type with which any type known in the
156 * compiled program can be represented. This includes types specified
157 * in the program as well as types defined by the language. In the
158 * view of the intermediate representation there is no difference
159 * between these types.
161 * There exist several kinds of types, arranged by the structure of
162 * the type. These are distinguished by a type opcode.
163 * A type is described by a set of attributes. Some of these attributes
164 * are common to all types, others depend on the kind of the type.
166 * The following describes the common attributes. They can only be
167 * accessed by the functions given below.
169 * The common fields are:
171 * - firm_kind: A firm_kind tag containing k_type. This is useful
172 * for dynamically checking whether a node is a type node.
173 * - type_op: A tp_op specifying the kind of the type.
174 * - name: An identifier specifying the name of the type. To be
175 * set by the frontend.
176 * - visibility: The visibility of this type.
177 * - size: The size of the type, i.e. an entity of this type will
178 * occupy size bits in memory. In several cases this is
179 * determined when fixing the layout of this type (class,
180 * struct, union, array, enumeration).
181 * - alignment The alignment of the type, i.e. an entity of this type will
182 * be allocated an an address in memory with this alignment.
183 * In several cases this is determined when fixing the layout
184 * of this type (class, struct, union, array)
185 * - mode: The mode to be used to represent the type on a machine.
186 * - state: The state of the type. The state represents whether the
187 * layout of the type is undefined or fixed (values: layout_undefined
188 * or layout_fixed). Compound types can have an undefined
189 * layout. The layout of the basic types primitive and pointer
190 * is always layout_fixed. If the layout of
191 * compound types is fixed all entities must have an offset
192 * and the size of the type must be set.
193 * A fixed layout for enumeration types means that each enumeration
194 * is associated with an implementation value.
195 * - assoc_type: The associated lowered/upper type.
196 * - visit: A counter for walks of the type information.
197 * - link: A void* to associate some additional information with the type.
199 * These fields can only be accessed via access functions.
201 * Depending on the value of @c type_op, i.e., depending on the kind of the
202 * type the adt contains further attributes. These are documented below.
206 * @link class_type class @endlink, @link struct_type struct @endlink,
207 * @link method_type method @endlink, @link union_type union @endlink,
208 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
209 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
212 * mode maybe not global field??
215 firm_kind kind; /**< the firm kind, must be k_type */
216 const tp_op *type_op; /**< the type operation of the type */
217 ident *name; /**< The name of the type */
218 ir_visibility visibility;/**< Visibility of entities of this type. */
219 unsigned flags; /**< Type flags, a bitmask of enum type_flags. */
220 unsigned size; /**< Size of an ir_entity of this type. This is determined
221 when fixing the layout of this class. Size must be
223 unsigned align; /**< Alignment of an ir_entity of this type. This should be
224 set according to the source language needs. If not set it's
225 calculated automatically by get_type_alignment().
226 Alignment must be given in bytes. */
227 ir_mode *mode; /**< The mode for atomic types */
228 ir_visited_t visit; /**< visited counter for walks of the type information */
229 void *link; /**< holds temporary data - like in irnode_t.h */
230 struct dbg_info *dbi; /**< A pointer to information for debug support. */
231 ir_type *assoc_type; /**< The associated lowered/unlowered type */
233 /* ------------- fields for analyses ---------------*/
236 long nr; /**< An unique node number for each node to make output
239 tp_attr attr; /**< Type kind specific fields. This must be the last
240 entry in this struct! Varying size! */
244 * Creates a new type representation:
246 * @param type_op the kind of this type. May not be type_id.
247 * @param mode the mode to be used for this type, may be NULL
248 * @param name an ident for the name of this type.
249 * @param db debug info
251 * @return A new type of the given type. The remaining private attributes are not
252 * initialized. The type is in state layout_undefined.
255 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
256 void free_type_attrs (ir_type *tp);
258 void free_class_entities (ir_type *clss);
259 void free_struct_entities (ir_type *strct);
260 void free_method_entities (ir_type *method);
261 void free_union_entities (ir_type *uni);
262 void free_array_entities (ir_type *array);
263 void free_enumeration_entities(ir_type *enumeration);
264 void free_pointer_entities (ir_type *pointer);
266 void free_array_automatic_entities(ir_type *array);
268 void free_class_attrs (ir_type *clss);
269 void free_struct_attrs (ir_type *strct);
270 void free_method_attrs (ir_type *method);
271 void free_union_attrs (ir_type *uni);
272 void free_array_attrs (ir_type *array);
273 void free_enumeration_attrs(ir_type *enumeration);
274 void free_pointer_attrs (ir_type *pointer);
276 void set_class_mode(ir_type *tp, ir_mode *mode);
277 void set_struct_mode(ir_type *tp, ir_mode *mode);
278 void set_pointer_mode(ir_type *tp, ir_mode *mode);
279 void set_primitive_mode(ir_type *tp, ir_mode *mode);
280 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
282 void set_class_size(ir_type *tp, unsigned bytes);
283 void set_struct_size(ir_type *tp, unsigned bytes);
284 void set_union_size(ir_type *tp, unsigned bytes);
285 void set_array_size(ir_type *tp, unsigned bytes);
286 void set_default_size(ir_type *tp, unsigned bytes);
289 * Initialize the type module.
291 * @param builtin_db debug info for built-in objects
292 * @param default_cc_mask default calling conventions for methods
294 void firm_init_type(dbg_info *builtin_db, unsigned default_cc_mask);
297 /* ------------------- *
299 * ------------------- */
301 extern ir_visited_t firm_type_visited;
303 static INLINE void _set_master_type_visited(ir_visited_t val) { firm_type_visited = val; }
304 static INLINE ir_visited_t _get_master_type_visited(void) { return firm_type_visited; }
305 static INLINE void _inc_master_type_visited(void) { ++firm_type_visited; }
308 _get_type_link(const ir_type *tp) {
309 assert(tp && tp->kind == k_type);
314 _set_type_link(ir_type *tp, void *l) {
315 assert(tp && tp->kind == k_type);
319 static INLINE const tp_op*
320 _get_type_tpop(const ir_type *tp) {
321 assert(tp && tp->kind == k_type);
326 _get_type_tpop_nameid(const ir_type *tp) {
327 assert(tp && tp->kind == k_type);
328 return get_tpop_ident(tp->type_op);
331 static INLINE tp_opcode
332 _get_type_tpop_code(const ir_type *tp) {
333 assert(tp && tp->kind == k_type);
334 return get_tpop_code(tp->type_op);
337 static INLINE ir_mode *
338 _get_type_mode(const ir_type *tp) {
339 assert(tp && tp->kind == k_type);
343 static INLINE ident *
344 _get_type_ident(const ir_type *tp) {
345 assert(tp && tp->kind == k_type);
350 _set_type_ident(ir_type *tp, ident* id) {
351 assert(tp && tp->kind == k_type);
355 static INLINE unsigned
356 _get_type_size_bytes(const ir_type *tp) {
357 assert(tp && tp->kind == k_type);
361 static INLINE ir_type_state
362 _get_type_state(const ir_type *tp) {
363 assert(tp && tp->kind == k_type);
364 return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
367 static INLINE ir_visited_t
368 _get_type_visited(const ir_type *tp) {
369 assert(tp && tp->kind == k_type);
374 _set_type_visited(ir_type *tp, ir_visited_t num) {
375 assert(tp && tp->kind == k_type);
380 _mark_type_visited(ir_type *tp) {
381 assert(tp && tp->kind == k_type);
382 assert(tp->visit < firm_type_visited);
383 tp->visit = firm_type_visited;
387 _type_visited(const ir_type *tp) {
388 assert(tp && tp->kind == k_type);
389 return tp->visit >= firm_type_visited;
393 _type_not_visited(const ir_type *tp) {
394 assert(tp && tp->kind == k_type);
395 return tp->visit < firm_type_visited;
398 static INLINE dbg_info *
399 _get_type_dbg_info(const ir_type *tp) {
404 _set_type_dbg_info(ir_type *tp, dbg_info *db) {
409 _is_type(const void *thing) {
410 return (get_kind(thing) == k_type);
414 _is_class_type(const ir_type *clss) {
416 return (clss->type_op == type_class);
420 _get_class_n_members (const ir_type *clss) {
421 assert(clss && (clss->type_op == type_class));
422 return (ARR_LEN (clss->attr.ca.members));
425 static INLINE ir_entity *
426 _get_class_member (const ir_type *clss, int pos) {
427 assert(clss && (clss->type_op == type_class));
428 assert(pos >= 0 && pos < _get_class_n_members(clss));
429 return clss->attr.ca.members[pos];
432 static INLINE unsigned
433 _get_class_vtable_size(const ir_type *clss) {
434 assert(clss && (clss->type_op == type_class));
435 return clss->attr.ca.vtable_size;
439 _set_class_vtable_size(ir_type *clss, unsigned vtable_size) {
440 assert(clss && (clss->type_op == type_class));
441 clss->attr.ca.vtable_size = vtable_size;
445 _is_class_final(const ir_type *clss) {
446 assert(clss && (clss->type_op == type_class));
447 return clss->attr.ca.clss_flags & cf_final_class;
451 _set_class_final(ir_type *clss, int final) {
452 assert(clss && (clss->type_op == type_class));
454 clss->attr.ca.clss_flags |= cf_final_class;
456 clss->attr.ca.clss_flags &= ~cf_final_class;
460 _is_class_interface(const ir_type *clss) {
461 assert(clss && (clss->type_op == type_class));
462 return clss->attr.ca.clss_flags & cf_interface_class;
466 _set_class_interface(ir_type *clss, int final) {
467 assert(clss && (clss->type_op == type_class));
469 clss->attr.ca.clss_flags |= cf_interface_class;
471 clss->attr.ca.clss_flags &= ~cf_interface_class;
475 _is_class_abstract(const ir_type *clss) {
476 assert(clss && (clss->type_op == type_class));
477 return clss->attr.ca.clss_flags & cf_absctract_class;
481 _set_class_abstract(ir_type *clss, int final) {
482 assert(clss && (clss->type_op == type_class));
484 clss->attr.ca.clss_flags |= cf_absctract_class;
486 clss->attr.ca.clss_flags &= ~cf_absctract_class;
490 _is_struct_type(const ir_type *strct) {
492 return (strct->type_op == type_struct);
496 _is_method_type(const ir_type *method) {
498 return (method->type_op == type_method);
502 _is_union_type(const ir_type *uni) {
504 return (uni->type_op == type_union);
508 _is_array_type(const ir_type *array) {
510 return (array->type_op == type_array);
514 _is_enumeration_type(const ir_type *enumeration) {
516 return (enumeration->type_op == type_enumeration);
520 _is_pointer_type(const ir_type *pointer) {
522 return (pointer->type_op == type_pointer);
525 /** Returns true if a type is a primitive type. */
527 _is_primitive_type(const ir_type *primitive) {
528 assert(primitive && primitive->kind == k_type);
529 return (primitive->type_op == type_primitive);
533 _is_atomic_type(const ir_type *tp) {
534 assert(tp && tp->kind == k_type);
535 return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
536 _is_enumeration_type(tp));
540 _get_method_n_params(const ir_type *method) {
541 assert(method && (method->type_op == type_method));
542 return method->attr.ma.n_params;
546 _get_method_n_ress(const ir_type *method) {
547 assert(method && (method->type_op == type_method));
548 return method->attr.ma.n_res;
551 static INLINE unsigned
552 _get_method_additional_properties(const ir_type *method) {
553 assert(method && (method->type_op == type_method));
554 return method->attr.ma.additional_properties;
558 _set_method_additional_properties(ir_type *method, unsigned mask) {
559 assert(method && (method->type_op == type_method));
561 /* do not allow to set the mtp_property_inherited flag or
562 * the automatic inheritance of flags will not work */
563 method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
567 _set_method_additional_property(ir_type *method, mtp_additional_property flag) {
568 assert(method && (method->type_op == type_method));
570 /* do not allow to set the mtp_property_inherited flag or
571 * the automatic inheritance of flags will not work */
572 method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
575 static INLINE unsigned
576 _get_method_calling_convention(const ir_type *method) {
577 assert(method && (method->type_op == type_method));
578 return method->attr.ma.irg_calling_conv;
582 _set_method_calling_convention(ir_type *method, unsigned cc_mask) {
583 assert(method && (method->type_op == type_method));
584 method->attr.ma.irg_calling_conv = cc_mask;
588 #define set_master_type_visited(val) _set_master_type_visited(val)
589 #define get_master_type_visited() _get_master_type_visited()
590 #define inc_master_type_visited() _inc_master_type_visited()
591 #define get_type_link(tp) _get_type_link(tp)
592 #define set_type_link(tp, l) _set_type_link(tp, l)
593 #define get_type_tpop(tp) _get_type_tpop(tp)
594 #define get_type_tpop_nameid(tp) _get_type_tpop_nameid(tp)
595 #define get_type_tpop_code(tp) _get_type_tpop_code(tp)
596 #define get_type_mode(tp) _get_type_mode(tp)
597 #define get_type_ident(tp) _get_type_ident(tp)
598 #define set_type_ident(tp, id) _set_type_ident(tp, id)
599 #define get_type_size_bytes(tp) _get_type_size_bytes(tp)
600 #define get_type_state(tp) _get_type_state(tp)
601 #define get_type_visited(tp) _get_type_visited(tp)
602 #define set_type_visited(tp, num) _set_type_visited(tp, num)
603 #define mark_type_visited(tp) _mark_type_visited(tp)
604 #define type_visited(tp) _type_visited(tp)
605 #define type_not_visited(tp) _type_not_visited(tp)
606 #define get_type_dbg_info(tp) _get_type_dbg_info(tp)
607 #define set_type_dbg_info(tp, db) _set_type_dbg_info(tp, db)
608 #define is_type(thing) _is_type(thing)
609 #define is_Class_type(clss) _is_class_type(clss)
610 #define get_class_n_members(clss) _get_class_n_members(clss)
611 #define get_class_member(clss, pos) _get_class_member(clss, pos)
612 #define get_class_vtable_size(clss) _get_class_vtable_size(clss)
613 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
614 #define is_class_final(clss) _is_class_final(clss)
615 #define set_class_final(clss, flag) _set_class_final(clss, flag)
616 #define is_class_interface(clss) _is_class_interface(clss)
617 #define set_class_interface(clss, flag) _set_class_interface(clss, flag)
618 #define is_class_abstract(clss) _is_class_abstract(clss)
619 #define set_class_abstract(clss, flag) _set_class_abstract(clss, flag)
620 #define is_Struct_type(strct) _is_struct_type(strct)
621 #define is_Method_type(method) _is_method_type(method)
622 #define is_Union_type(uni) _is_union_type(uni)
623 #define is_Array_type(array) _is_array_type(array)
624 #define is_Enumeration_type(enumeration) _is_enumeration_type(enumeration)
625 #define is_Pointer_type(pointer) _is_pointer_type(pointer)
626 #define is_Primitive_type(primitive) _is_primitive_type(primitive)
627 #define is_atomic_type(tp) _is_atomic_type(tp)
628 #define get_method_n_params(method) _get_method_n_params(method)
629 #define get_method_n_ress(method) _get_method_n_ress(method)
630 #define get_method_additional_properties(method) _get_method_additional_properties(method)
631 #define set_method_additional_properties(method, mask) _set_method_additional_properties(method, mask)
632 #define set_method_additional_property(method, flag) _set_method_additional_property(method, flag)
633 #define get_method_calling_convention(method) _get_method_calling_convention(method)
634 #define set_method_calling_convention(method, cc_mask) _set_method_calling_convention(method, cc_mask)
636 #endif /* FIRM_TR_TYPE_T_H */