2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @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. */
119 typedef struct { * No private attr yet! *
124 typedef struct { * No private attr, must be smaller than others! *
128 /** General type attributes. */
130 cls_attr ca; /**< Attributes of a class type */
131 stc_attr sa; /**< Attributes of a struct type */
132 mtd_attr ma; /**< Attributes of a method type */
133 uni_attr ua; /**< Attributes of an union type */
134 arr_attr aa; /**< Attributes of an array type */
135 enm_attr ea; /**< Attributes of an enumeration type */
136 ptr_attr pa; /**< Attributes of a pointer type */
139 /** Additional type flags. */
141 tf_none = 0, /**< No flags. */
142 tf_frame_type = 1, /**< Set if this is a frame type. */
143 tf_value_param_type = 2, /**< Set if this is a value param type. */
144 tf_lowered_type = 4, /**< Set if this is a lowered type. */
145 tf_layout_fixed = 8, /**< Set if the layout of a type is fixed */
146 tf_global_type = 16, /**< Set only for the global type */
147 tf_tls_type = 32, /**< Set only for the tls type */
151 * An abstract data type to represent types.
153 * This is the abstract data type with which any type known in the
154 * compiled program can be represented. This includes types specified
155 * in the program as well as types defined by the language. In the
156 * view of the intermediate representation there is no difference
157 * between these types.
159 * There exist several kinds of types, arranged by the structure of
160 * the type. These are distinguished by a type opcode.
161 * A type is described by a set of attributes. Some of these attributes
162 * are common to all types, others depend on the kind of the type.
164 * The following describes the common attributes. They can only be
165 * accessed by the functions given below.
167 * The common fields are:
169 * - firm_kind: A firm_kind tag containing k_type. This is useful
170 * for dynamically checking whether a node is a type node.
171 * - type_op: A tp_op specifying the kind of the type.
172 * - name: An identifier specifying the name of the type. To be
173 * set by the frontend.
174 * - visibility: The visibility of this type.
175 * - size: The size of the type, i.e. an entity of this type will
176 * occupy size bits in memory. In several cases this is
177 * determined when fixing the layout of this type (class,
178 * struct, union, array, enumeration).
179 * - alignment The alignment of the type, i.e. an entity of this type will
180 * be allocated an an address in memory with this alignment.
181 * In several cases this is determined when fixing the layout
182 * of this type (class, struct, union, array)
183 * - mode: The mode to be used to represent the type on a machine.
184 * - state: The state of the type. The state represents whether the
185 * layout of the type is undefined or fixed (values: layout_undefined
186 * or layout_fixed). Compound types can have an undefined
187 * layout. The layout of the basic types primitive and pointer
188 * is always layout_fixed. If the layout of
189 * compound types is fixed all entities must have an offset
190 * and the size of the type must be set.
191 * A fixed layout for enumeration types means that each enumeration
192 * is associated with an implementation value.
193 * - assoc_type: The associated lowered/upper type.
194 * - visit: A counter for walks of the type information.
195 * - link: A void* to associate some additional information with the type.
197 * These fields can only be accessed via access functions.
199 * Depending on the value of @c type_op, i.e., depending on the kind of the
200 * type the adt contains further attributes. These are documented below.
204 * @link class_type class @endlink, @link struct_type struct @endlink,
205 * @link method_type method @endlink, @link union_type union @endlink,
206 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
207 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
210 * mode maybe not global field??
213 firm_kind kind; /**< the firm kind, must be k_type */
214 const tp_op *type_op; /**< the type operation of the type */
215 ident *name; /**< The name of the type */
216 ir_visibility visibility;/**< Visibility of entities of this type. */
217 unsigned flags; /**< Type flags, a bitmask of enum type_flags. */
218 int size; /**< Size of an ir_entity of this type. This is determined
219 when fixing the layout of this class. Size must be
221 int align; /**< Alignment of an ir_entity of this type. This should be
222 set according to the source language needs. If not set it's
223 calculated automatically by get_type_alignment().
224 Alignment must be given in bits. */
225 ir_mode *mode; /**< The mode for atomic types */
226 unsigned long visit; /**< visited counter for walks of the type information */
227 void *link; /**< holds temporary data - like in irnode_t.h */
228 struct dbg_info *dbi; /**< A pointer to information for debug support. */
229 ir_type *assoc_type; /**< The associated lowered/unlowered type */
231 /* ------------- fields for analyses ---------------*/
234 long nr; /**< An unique node number for each node to make output
237 tp_attr attr; /**< Type kind specific fields. This must be the last
238 entry in this struct! Varying size! */
242 * Creates a new type representation:
244 * @param type_op the kind of this type. May not be type_id.
245 * @param mode the mode to be used for this type, may be NULL
246 * @param name an ident for the name of this type.
247 * @param db debug info
249 * @return A new type of the given type. The remaining private attributes are not
250 * initialized. The type is in state layout_undefined.
253 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
254 void free_type_attrs (ir_type *tp);
256 void free_class_entities (ir_type *clss);
257 void free_struct_entities (ir_type *strct);
258 void free_method_entities (ir_type *method);
259 void free_union_entities (ir_type *uni);
260 void free_array_entities (ir_type *array);
261 void free_enumeration_entities(ir_type *enumeration);
262 void free_pointer_entities (ir_type *pointer);
264 void free_array_automatic_entities(ir_type *array);
266 void free_class_attrs (ir_type *clss);
267 void free_struct_attrs (ir_type *strct);
268 void free_method_attrs (ir_type *method);
269 void free_union_attrs (ir_type *uni);
270 void free_array_attrs (ir_type *array);
271 void free_enumeration_attrs(ir_type *enumeration);
272 void free_pointer_attrs (ir_type *pointer);
274 void set_class_mode(ir_type *tp, ir_mode *mode);
275 void set_struct_mode(ir_type *tp, ir_mode *mode);
276 void set_pointer_mode(ir_type *tp, ir_mode *mode);
277 void set_primitive_mode(ir_type *tp, ir_mode *mode);
278 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
280 void set_class_size_bits(ir_type *tp, int bits);
281 void set_struct_size_bits(ir_type *tp, int bits);
282 void set_union_size_bits(ir_type *tp, int bits);
283 void set_array_size_bits(ir_type *tp, int size);
284 void set_default_size_bits(ir_type *tp, int size);
287 * Initialize the type module.
289 * @param builtin_db debug info for built-in objects
290 * @param default_cc_mask default calling conventions for methods
292 void firm_init_type(dbg_info *builtin_db, unsigned default_cc_mask);
295 /* ------------------- *
297 * ------------------- */
299 extern unsigned long firm_type_visited;
301 static INLINE void _set_master_type_visited(unsigned long val) { firm_type_visited = val; }
302 static INLINE unsigned long _get_master_type_visited(void) { return firm_type_visited; }
303 static INLINE void _inc_master_type_visited(void) { ++firm_type_visited; }
306 _get_type_link(const ir_type *tp) {
307 assert(tp && tp->kind == k_type);
312 _set_type_link(ir_type *tp, void *l) {
313 assert(tp && tp->kind == k_type);
317 static INLINE const tp_op*
318 _get_type_tpop(const ir_type *tp) {
319 assert(tp && tp->kind == k_type);
324 _get_type_tpop_nameid(const ir_type *tp) {
325 assert(tp && tp->kind == k_type);
326 return get_tpop_ident(tp->type_op);
329 static INLINE tp_opcode
330 _get_type_tpop_code(const ir_type *tp) {
331 assert(tp && tp->kind == k_type);
332 return get_tpop_code(tp->type_op);
335 static INLINE ir_mode *
336 _get_type_mode(const ir_type *tp) {
337 assert(tp && tp->kind == k_type);
341 static INLINE ident *
342 _get_type_ident(const ir_type *tp) {
343 assert(tp && tp->kind == k_type);
348 _set_type_ident(ir_type *tp, ident* id) {
349 assert(tp && tp->kind == k_type);
354 _get_type_size_bits(const ir_type *tp) {
355 assert(tp && tp->kind == k_type);
360 _get_type_size_bytes(const ir_type *tp) {
361 int size = _get_type_size_bits(tp);
364 if ((size & 7) != 0) {
365 assert(0 && "cannot take byte size of this type");
371 static INLINE type_state
372 _get_type_state(const ir_type *tp) {
373 assert(tp && tp->kind == k_type);
374 return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
377 static INLINE unsigned long
378 _get_type_visited(const ir_type *tp) {
379 assert(tp && tp->kind == k_type);
384 _set_type_visited(ir_type *tp, unsigned long num) {
385 assert(tp && tp->kind == k_type);
390 _mark_type_visited(ir_type *tp) {
391 assert(tp && tp->kind == k_type);
392 assert(tp->visit < firm_type_visited);
393 tp->visit = firm_type_visited;
397 _type_visited(const ir_type *tp) {
398 assert(tp && tp->kind == k_type);
399 return tp->visit >= firm_type_visited;
403 _type_not_visited(const ir_type *tp) {
404 assert(tp && tp->kind == k_type);
405 return tp->visit < firm_type_visited;
408 static INLINE dbg_info *
409 _get_type_dbg_info(const ir_type *tp) {
414 _set_type_dbg_info(ir_type *tp, dbg_info *db) {
419 _is_type(const void *thing) {
420 return (get_kind(thing) == k_type);
424 _is_class_type(const ir_type *clss) {
426 return (clss->type_op == type_class);
430 _get_class_n_members (const ir_type *clss) {
431 assert(clss && (clss->type_op == type_class));
432 return (ARR_LEN (clss->attr.ca.members));
435 static INLINE ir_entity *
436 _get_class_member (const ir_type *clss, int pos) {
437 assert(clss && (clss->type_op == type_class));
438 assert(pos >= 0 && pos < _get_class_n_members(clss));
439 return clss->attr.ca.members[pos];
442 static INLINE unsigned
443 _get_class_vtable_size(const ir_type *clss) {
444 assert(clss && (clss->type_op == type_class));
445 return clss->attr.ca.vtable_size;
449 _set_class_vtable_size(ir_type *clss, unsigned vtable_size) {
450 assert(clss && (clss->type_op == type_class));
451 clss->attr.ca.vtable_size = vtable_size;
455 _is_class_final(const ir_type *clss) {
456 assert(clss && (clss->type_op == type_class));
457 return clss->attr.ca.clss_flags & cf_final_class;
461 _set_class_final(ir_type *clss, int final) {
462 assert(clss && (clss->type_op == type_class));
464 clss->attr.ca.clss_flags |= cf_final_class;
466 clss->attr.ca.clss_flags &= ~cf_final_class;
470 _is_class_interface(const ir_type *clss) {
471 assert(clss && (clss->type_op == type_class));
472 return clss->attr.ca.clss_flags & cf_interface_class;
476 _set_class_interface(ir_type *clss, int final) {
477 assert(clss && (clss->type_op == type_class));
479 clss->attr.ca.clss_flags |= cf_interface_class;
481 clss->attr.ca.clss_flags &= ~cf_interface_class;
485 _is_class_abstract(const ir_type *clss) {
486 assert(clss && (clss->type_op == type_class));
487 return clss->attr.ca.clss_flags & cf_absctract_class;
491 _set_class_abstract(ir_type *clss, int final) {
492 assert(clss && (clss->type_op == type_class));
494 clss->attr.ca.clss_flags |= cf_absctract_class;
496 clss->attr.ca.clss_flags &= ~cf_absctract_class;
500 _is_struct_type(const ir_type *strct) {
502 return (strct->type_op == type_struct);
506 _is_method_type(const ir_type *method) {
508 return (method->type_op == type_method);
512 _is_union_type(const ir_type *uni) {
514 return (uni->type_op == type_union);
518 _is_array_type(const ir_type *array) {
520 return (array->type_op == type_array);
524 _is_enumeration_type(const ir_type *enumeration) {
526 return (enumeration->type_op == type_enumeration);
530 _is_pointer_type(const ir_type *pointer) {
532 return (pointer->type_op == type_pointer);
535 /** Returns true if a type is a primitive type. */
537 _is_primitive_type(const ir_type *primitive) {
538 assert(primitive && primitive->kind == k_type);
539 return (primitive->type_op == type_primitive);
543 _is_atomic_type(const ir_type *tp) {
544 assert(tp && tp->kind == k_type);
545 return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
546 _is_enumeration_type(tp));
550 _get_method_n_params(const ir_type *method) {
551 assert(method && (method->type_op == type_method));
552 return method->attr.ma.n_params;
556 _get_method_n_ress(const ir_type *method) {
557 assert(method && (method->type_op == type_method));
558 return method->attr.ma.n_res;
561 static INLINE unsigned
562 _get_method_additional_properties(const ir_type *method) {
563 assert(method && (method->type_op == type_method));
564 return method->attr.ma.additional_properties;
568 _set_method_additional_properties(ir_type *method, unsigned mask) {
569 assert(method && (method->type_op == type_method));
571 /* do not allow to set the mtp_property_inherited flag or
572 * the automatic inheritance of flags will not work */
573 method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
577 _set_method_additional_property(ir_type *method, mtp_additional_property flag) {
578 assert(method && (method->type_op == type_method));
580 /* do not allow to set the mtp_property_inherited flag or
581 * the automatic inheritance of flags will not work */
582 method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
585 static INLINE unsigned
586 _get_method_calling_convention(const ir_type *method) {
587 assert(method && (method->type_op == type_method));
588 return method->attr.ma.irg_calling_conv;
592 _set_method_calling_convention(ir_type *method, unsigned cc_mask) {
593 assert(method && (method->type_op == type_method));
594 method->attr.ma.irg_calling_conv = cc_mask;
598 #define set_master_type_visited(val) _set_master_type_visited(val)
599 #define get_master_type_visited() _get_master_type_visited()
600 #define inc_master_type_visited() _inc_master_type_visited()
601 #define get_type_link(tp) _get_type_link(tp)
602 #define set_type_link(tp, l) _set_type_link(tp, l)
603 #define get_type_tpop(tp) _get_type_tpop(tp)
604 #define get_type_tpop_nameid(tp) _get_type_tpop_nameid(tp)
605 #define get_type_tpop_code(tp) _get_type_tpop_code(tp)
606 #define get_type_mode(tp) _get_type_mode(tp)
607 #define get_type_ident(tp) _get_type_ident(tp)
608 #define set_type_ident(tp, id) _set_type_ident(tp, id)
609 #define get_type_size_bits(tp) _get_type_size_bits(tp)
610 #define get_type_size_bytes(tp) _get_type_size_bytes(tp)
611 #define get_type_state(tp) _get_type_state(tp)
612 #define get_type_visited(tp) _get_type_visited(tp)
613 #define set_type_visited(tp, num) _set_type_visited(tp, num)
614 #define mark_type_visited(tp) _mark_type_visited(tp)
615 #define type_visited(tp) _type_visited(tp)
616 #define type_not_visited(tp) _type_not_visited(tp)
617 #define get_type_dbg_info(tp) _get_type_dbg_info(tp)
618 #define set_type_dbg_info(tp, db) _set_type_dbg_info(tp, db)
619 #define is_type(thing) _is_type(thing)
620 #define is_Class_type(clss) _is_class_type(clss)
621 #define get_class_n_members(clss) _get_class_n_members(clss)
622 #define get_class_member(clss, pos) _get_class_member(clss, pos)
623 #define get_class_vtable_size(clss) _get_class_vtable_size(clss)
624 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
625 #define is_class_final(clss) _is_class_final(clss)
626 #define set_class_final(clss, flag) _set_class_final(clss, flag)
627 #define is_class_interface(clss) _is_class_interface(clss)
628 #define set_class_interface(clss, flag) _set_class_interface(clss, flag)
629 #define is_class_abstract(clss) _is_class_abstract(clss)
630 #define set_class_abstract(clss, flag) _set_class_abstract(clss, flag)
631 #define is_Struct_type(strct) _is_struct_type(strct)
632 #define is_Method_type(method) _is_method_type(method)
633 #define is_Union_type(uni) _is_union_type(uni)
634 #define is_Array_type(array) _is_array_type(array)
635 #define is_Enumeration_type(enumeration) _is_enumeration_type(enumeration)
636 #define is_Pointer_type(pointer) _is_pointer_type(pointer)
637 #define is_Primitive_type(primitive) _is_primitive_type(primitive)
638 #define is_atomic_type(tp) _is_atomic_type(tp)
639 #define get_method_n_params(method) _get_method_n_params(method)
640 #define get_method_n_ress(method) _get_method_n_ress(method)
641 #define get_method_additional_properties(method) _get_method_additional_properties(method)
642 #define set_method_additional_properties(method, mask) _set_method_additional_properties(method, mask)
643 #define set_method_additional_property(method, flag) _set_method_additional_property(method, flag)
644 #define get_method_calling_convention(method) _get_method_calling_convention(method)
645 #define set_method_calling_convention(method, cc_mask) _set_method_calling_convention(method, cc_mask)
647 #endif /* FIRM_TR_TYPE_T_H */