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
33 #include "firm_common.h"
39 cf_none = 0, /**< No flags. */
40 cf_final_class = 1, /**< Set if a class is an final class */
41 cf_interface_class = 2, /**< Set if a class is an "interface" */
42 cf_absctract_class = 4, /**< Set if a class is "abstract" */
45 /** Class type attributes. */
47 ir_entity **members; /**< Array containing the fields and methods of this class. */
48 ir_type **subtypes; /**< Array containing the direct subtypes. */
49 ir_type **supertypes; /**< Array containing the direct supertypes */
50 ir_peculiarity peculiarity; /**< The peculiarity of this class. */
51 ir_entity *type_info; /**< An ir_entity representing this class, used for type info. */
52 int dfn; /**< A number that can be used for 'instanceof' operator. */
53 unsigned vtable_size; /**< The size of the vtable for this class. */
54 unsigned clss_flags; /**< Additional class flags. */
57 /** Struct type attributes. */
59 ir_entity **members; /**< Fields of this struct. No method entities allowed. */
62 /** A (type, ir_entity) pair. */
64 ir_type *tp; /**< A type. */
65 ir_entity *ent; /**< An ir_entity. */
66 ident *param_name; /**< For debugging purposes: the name of the parameter */
69 /** Method type attributes. */
71 int n_params; /**< Number of parameters. */
72 tp_ent_pair *params; /**< Array of parameter type/value entities pairs. */
73 ir_type *value_params; /**< A type whose entities represent copied value arguments. */
74 int n_res; /**< Number of results. */
75 tp_ent_pair *res_type; /**< Array of result type/value ir_entity pairs. */
76 ir_type *value_ress; /**< A type whose entities represent copied value results. */
77 ir_variadicity variadicity; /**< The variadicity of the method. */
78 int first_variadic_param; /**< The index of the first variadic parameter or -1 if non-variadic .*/
79 unsigned additional_properties; /**< Set of additional method properties. */
80 unsigned irg_calling_conv; /**< A set of calling convention flags. */
83 /** Union type attributes. */
85 ir_entity **members; /**< Fields of this union. No method entities allowed. */
88 /** Array type attributes. */
90 int n_dimensions; /**< Number of array dimensions. */
91 ir_node **lower_bound; /**< Lower bounds of dimensions. Usually all 0. */
92 ir_node **upper_bound; /**< Upper bounds or dimensions. */
93 int *order; /**< Ordering of dimensions. */
94 ir_type *element_type; /**< The type of the array elements. */
95 ir_entity *element_ent; /**< entity for the array elements, to be used for
96 element selection with a Sel node. */
99 /** An enumerator constant. */
100 struct ir_enum_const {
101 tarval *value; /**< The constants that represents this enumerator identifier. */
102 ident *nameid; /**< The name of the enumerator identifier. */
103 ir_type *owner; /**< owner type of this enumerator constant. */
106 /** Enum type attributes. */
108 ir_enum_const *enumer; /**< Contains all enumerator constants that represent a member
109 of the enum -- enumerators. */
112 /** Pointer type attributes. */
114 ir_type *points_to; /**< The type of the ir_entity the pointer points to. */
117 /** Primitive type attributes. */
119 ir_type *base_type; /**< For bitfield types: The base primitive type, NULL else. */
122 /** General type attributes. */
124 cls_attr ca; /**< Attributes of a class type */
125 stc_attr sa; /**< Attributes of a struct type */
126 mtd_attr ma; /**< Attributes of a method type */
127 uni_attr ua; /**< Attributes of an union type */
128 arr_attr aa; /**< Attributes of an array type */
129 enm_attr ea; /**< Attributes of an enumeration type */
130 ptr_attr pa; /**< Attributes of a pointer type */
131 pri_attr ba; /**< Attributes of a primitive bitfield type */
134 /** Additional type flags. */
136 tf_none = 0, /**< No flags. */
137 tf_lowered_type = 1, /**< Set if this is a lowered type. */
138 tf_layout_fixed = 2, /**< Set if the layout of a type is fixed */
140 tf_frame_type = 4, /**< Set if this is a frame type. */
141 tf_value_param_type = 8, /**< Set if this is a value param type. */
142 tf_global_type = 16, /**< Set only for the global type */
143 tf_tls_type = 32, /**< Set only for the tls type */
144 tf_constructors = 64, /**< Set only for the constructors segment type */
145 tf_destructors = 128, /**< Set only for the destructors segment type */
149 * An abstract data type to represent types.
151 * This is the abstract data type with which any type known in the
152 * compiled program can be represented. This includes types specified
153 * in the program as well as types defined by the language. In the
154 * view of the intermediate representation there is no difference
155 * between these types.
157 * There exist several kinds of types, arranged by the structure of
158 * the type. These are distinguished by a type opcode.
159 * A type is described by a set of attributes. Some of these attributes
160 * are common to all types, others depend on the kind of the type.
162 * The following describes the common attributes. They can only be
163 * accessed by the functions given below.
165 * The common fields are:
167 * - firm_kind: A firm_kind tag containing k_type. This is useful
168 * for dynamically checking whether a node is a type node.
169 * - type_op: A tp_op specifying the kind of the type.
170 * - name: An identifier specifying the name of the type. To be
171 * set by the frontend.
172 * - size: The size of the type, i.e. an entity of this type will
173 * occupy size bits in memory. In several cases this is
174 * determined when fixing the layout of this type (class,
175 * struct, union, array, enumeration).
176 * - alignment The alignment of the type, i.e. an entity of this type will
177 * be allocated an an address in memory with this alignment.
178 * In several cases this is determined when fixing the layout
179 * of this type (class, struct, union, array)
180 * - mode: The mode to be used to represent the type on a machine.
181 * - state: The state of the type. The state represents whether the
182 * layout of the type is undefined or fixed (values: layout_undefined
183 * or layout_fixed). Compound types can have an undefined
184 * layout. The layout of the basic types primitive and pointer
185 * is always layout_fixed. If the layout of
186 * compound types is fixed all entities must have an offset
187 * and the size of the type must be set.
188 * A fixed layout for enumeration types means that each enumeration
189 * is associated with an implementation value.
190 * - assoc_type: The associated lowered/upper type.
191 * - visit: A counter for walks of the type information.
192 * - link: A void* to associate some additional information with the type.
194 * These fields can only be accessed via access functions.
196 * Depending on the value of @c type_op, i.e., depending on the kind of the
197 * type the adt contains further attributes. These are documented below.
201 * @link class_type class @endlink, @link struct_type struct @endlink,
202 * @link method_type method @endlink, @link union_type union @endlink,
203 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
204 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
207 * mode maybe not global field??
210 firm_kind kind; /**< the firm kind, must be k_type */
211 const tp_op *type_op; /**< the type operation of the type */
212 ident *name; /**< The name of the type */
213 ir_visibility visibility;/**< Visibility of entities of this type. */
214 unsigned flags; /**< Type flags, a bitmask of enum type_flags. */
215 unsigned size; /**< Size of an ir_entity of this type. This is determined
216 when fixing the layout of this class. Size must be
218 unsigned align; /**< Alignment of an ir_entity of this type. This should be
219 set according to the source language needs. If not set it's
220 calculated automatically by get_type_alignment().
221 Alignment must be given in bytes. */
222 ir_mode *mode; /**< The mode for atomic types */
223 ir_visited_t visit; /**< visited counter for walks of the type information */
224 void *link; /**< holds temporary data - like in irnode_t.h */
225 type_dbg_info *dbi; /**< A pointer to information for debug support. */
226 ir_type *assoc_type; /**< The associated lowered/unlowered type */
228 /* ------------- fields for analyses ---------------*/
231 long nr; /**< An unique node number for each node to make output
234 tp_attr attr; /**< Type kind specific fields. This must be the last
235 entry in this struct! Varying size! */
239 * Creates a new type representation:
241 * @param type_op the kind of this type. May not be type_id.
242 * @param mode the mode to be used for this type, may be NULL
243 * @param db debug info
245 * @return A new type of the given type. The remaining private attributes are not
246 * initialized. The type is in state layout_undefined.
248 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db);
249 void free_type_attrs(ir_type *tp);
251 void free_class_entities (ir_type *clss);
252 void free_struct_entities (ir_type *strct);
253 void free_method_entities (ir_type *method);
254 void free_union_entities (ir_type *uni);
255 void free_array_entities (ir_type *array);
256 void free_enumeration_entities(ir_type *enumeration);
257 void free_pointer_entities (ir_type *pointer);
259 void free_array_automatic_entities(ir_type *array);
261 void free_class_attrs (ir_type *clss);
262 void free_struct_attrs (ir_type *strct);
263 void free_method_attrs (ir_type *method);
264 void free_union_attrs (ir_type *uni);
265 void free_array_attrs (ir_type *array);
266 void free_enumeration_attrs(ir_type *enumeration);
267 void free_pointer_attrs (ir_type *pointer);
269 void set_class_mode(ir_type *tp, ir_mode *mode);
270 void set_struct_mode(ir_type *tp, ir_mode *mode);
271 void set_pointer_mode(ir_type *tp, ir_mode *mode);
272 void set_primitive_mode(ir_type *tp, ir_mode *mode);
273 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
275 void set_class_size(ir_type *tp, unsigned bytes);
276 void set_struct_size(ir_type *tp, unsigned bytes);
277 void set_union_size(ir_type *tp, unsigned bytes);
278 void set_array_size(ir_type *tp, unsigned bytes);
279 void set_default_size(ir_type *tp, unsigned bytes);
282 * Initialize the type module.
284 * @param default_cc_mask default calling conventions for methods
286 void firm_init_type(unsigned default_cc_mask);
288 /** Clone an existing method type.
290 * @param tp the method type to clone.
291 * @param prefix if non-null, will be the prefix for the name of
294 * @return the cloned method type.
296 ir_type *clone_type_method(ir_type *tp);
298 /* ------------------- *
300 * ------------------- */
302 extern ir_visited_t firm_type_visited;
304 static inline void _set_master_type_visited(ir_visited_t val) { firm_type_visited = val; }
305 static inline ir_visited_t _get_master_type_visited(void) { return firm_type_visited; }
306 static inline void _inc_master_type_visited(void) { ++firm_type_visited; }
308 static inline void *_get_type_link(const ir_type *tp)
310 assert(tp && tp->kind == k_type);
314 static inline void _set_type_link(ir_type *tp, void *l)
316 assert(tp && tp->kind == k_type);
320 static inline const tp_op *_get_type_tpop(const ir_type *tp)
322 assert(tp && tp->kind == k_type);
326 static inline ident *_get_type_tpop_nameid(const ir_type *tp)
328 assert(tp && tp->kind == k_type);
329 return get_tpop_ident(tp->type_op);
332 static inline tp_opcode _get_type_tpop_code(const ir_type *tp)
334 assert(tp && tp->kind == k_type);
335 return get_tpop_code(tp->type_op);
338 static inline ir_mode *_get_type_mode(const ir_type *tp)
340 assert(tp && tp->kind == k_type);
344 static inline unsigned _get_type_size_bytes(const ir_type *tp)
346 assert(tp && tp->kind == k_type);
350 static inline ir_type_state _get_type_state(const ir_type *tp)
352 assert(tp && tp->kind == k_type);
353 return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
356 static inline ir_visited_t _get_type_visited(const ir_type *tp)
358 assert(tp && tp->kind == k_type);
362 static inline void _set_type_visited(ir_type *tp, ir_visited_t num)
364 assert(tp && tp->kind == k_type);
368 static inline void _mark_type_visited(ir_type *tp)
370 assert(tp && tp->kind == k_type);
371 assert(tp->visit < firm_type_visited);
372 tp->visit = firm_type_visited;
375 static inline int _type_visited(const ir_type *tp)
377 assert(tp && tp->kind == k_type);
378 return tp->visit >= firm_type_visited;
381 static inline int _type_not_visited(const ir_type *tp)
383 assert(tp && tp->kind == k_type);
384 return tp->visit < firm_type_visited;
387 static inline type_dbg_info *_get_type_dbg_info(const ir_type *tp)
392 static inline void _set_type_dbg_info(ir_type *tp, type_dbg_info *db)
397 static inline int _is_type(const void *thing)
399 return (get_kind(thing) == k_type);
402 static inline int _is_class_type(const ir_type *clss)
405 return (clss->type_op == type_class);
408 static inline int _get_class_n_members (const ir_type *clss)
410 assert(clss && (clss->type_op == type_class));
411 return (ARR_LEN (clss->attr.ca.members));
414 static inline ir_entity *_get_class_member(const ir_type *clss, int pos)
416 assert(clss && (clss->type_op == type_class));
417 assert(pos >= 0 && pos < _get_class_n_members(clss));
418 return clss->attr.ca.members[pos];
421 static inline unsigned _get_class_vtable_size(const ir_type *clss)
423 assert(clss && (clss->type_op == type_class));
424 return clss->attr.ca.vtable_size;
427 static inline void _set_class_vtable_size(ir_type *clss, unsigned vtable_size)
429 assert(clss && (clss->type_op == type_class));
430 clss->attr.ca.vtable_size = vtable_size;
433 static inline int _is_class_final(const ir_type *clss)
435 assert(clss && (clss->type_op == type_class));
436 return clss->attr.ca.clss_flags & cf_final_class;
439 static inline void _set_class_final(ir_type *clss, int final)
441 assert(clss && (clss->type_op == type_class));
443 clss->attr.ca.clss_flags |= cf_final_class;
445 clss->attr.ca.clss_flags &= ~cf_final_class;
448 static inline int _is_class_interface(const ir_type *clss)
450 assert(clss && (clss->type_op == type_class));
451 return clss->attr.ca.clss_flags & cf_interface_class;
454 static inline void _set_class_interface(ir_type *clss, int final)
456 assert(clss && (clss->type_op == type_class));
458 clss->attr.ca.clss_flags |= cf_interface_class;
460 clss->attr.ca.clss_flags &= ~cf_interface_class;
463 static inline int _is_class_abstract(const ir_type *clss)
465 assert(clss && (clss->type_op == type_class));
466 return clss->attr.ca.clss_flags & cf_absctract_class;
469 static inline void _set_class_abstract(ir_type *clss, int final)
471 assert(clss && (clss->type_op == type_class));
473 clss->attr.ca.clss_flags |= cf_absctract_class;
475 clss->attr.ca.clss_flags &= ~cf_absctract_class;
478 static inline int _is_struct_type(const ir_type *strct)
481 return (strct->type_op == type_struct);
484 static inline int _is_method_type(const ir_type *method)
487 return (method->type_op == type_method);
490 static inline int _is_union_type(const ir_type *uni)
493 return (uni->type_op == type_union);
496 static inline int _is_array_type(const ir_type *array)
499 return (array->type_op == type_array);
502 static inline int _is_enumeration_type(const ir_type *enumeration)
505 return (enumeration->type_op == type_enumeration);
508 static inline int _is_pointer_type(const ir_type *pointer)
511 return (pointer->type_op == type_pointer);
514 /** Returns true if a type is a primitive type. */
515 static inline int _is_primitive_type(const ir_type *primitive)
517 assert(primitive && primitive->kind == k_type);
518 return (primitive->type_op == type_primitive);
521 static inline int _is_atomic_type(const ir_type *tp)
523 assert(tp && tp->kind == k_type);
524 return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
525 _is_enumeration_type(tp));
528 static inline int _get_method_n_params(const ir_type *method)
530 assert(method && (method->type_op == type_method));
531 return method->attr.ma.n_params;
534 static inline int _get_method_n_ress(const ir_type *method)
536 assert(method && (method->type_op == type_method));
537 return method->attr.ma.n_res;
540 static inline unsigned _get_method_additional_properties(const ir_type *method)
542 assert(method && (method->type_op == type_method));
543 return method->attr.ma.additional_properties;
546 static inline void _set_method_additional_properties(ir_type *method, unsigned mask)
548 assert(method && (method->type_op == type_method));
550 /* do not allow to set the mtp_property_inherited flag or
551 * the automatic inheritance of flags will not work */
552 method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
555 static inline void _set_method_additional_property(ir_type *method, mtp_additional_property flag)
557 assert(method && (method->type_op == type_method));
559 /* do not allow to set the mtp_property_inherited flag or
560 * the automatic inheritance of flags will not work */
561 method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
564 static inline unsigned _get_method_calling_convention(const ir_type *method)
566 assert(method && (method->type_op == type_method));
567 return method->attr.ma.irg_calling_conv;
570 static inline void _set_method_calling_convention(ir_type *method, unsigned cc_mask)
572 assert(method && (method->type_op == type_method));
573 method->attr.ma.irg_calling_conv = cc_mask;
577 #define set_master_type_visited(val) _set_master_type_visited(val)
578 #define get_master_type_visited() _get_master_type_visited()
579 #define inc_master_type_visited() _inc_master_type_visited()
580 #define get_type_link(tp) _get_type_link(tp)
581 #define set_type_link(tp, l) _set_type_link(tp, l)
582 #define get_type_tpop(tp) _get_type_tpop(tp)
583 #define get_type_tpop_nameid(tp) _get_type_tpop_nameid(tp)
584 #define get_type_tpop_code(tp) _get_type_tpop_code(tp)
585 #define get_type_mode(tp) _get_type_mode(tp)
586 #define get_type_size_bytes(tp) _get_type_size_bytes(tp)
587 #define get_type_state(tp) _get_type_state(tp)
588 #define get_type_visited(tp) _get_type_visited(tp)
589 #define set_type_visited(tp, num) _set_type_visited(tp, num)
590 #define mark_type_visited(tp) _mark_type_visited(tp)
591 #define type_visited(tp) _type_visited(tp)
592 #define type_not_visited(tp) _type_not_visited(tp)
593 #define get_type_dbg_info(tp) _get_type_dbg_info(tp)
594 #define set_type_dbg_info(tp, db) _set_type_dbg_info(tp, db)
595 #define is_type(thing) _is_type(thing)
596 #define is_Class_type(clss) _is_class_type(clss)
597 #define get_class_n_members(clss) _get_class_n_members(clss)
598 #define get_class_member(clss, pos) _get_class_member(clss, pos)
599 #define get_class_vtable_size(clss) _get_class_vtable_size(clss)
600 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
601 #define is_class_final(clss) _is_class_final(clss)
602 #define set_class_final(clss, flag) _set_class_final(clss, flag)
603 #define is_class_interface(clss) _is_class_interface(clss)
604 #define set_class_interface(clss, flag) _set_class_interface(clss, flag)
605 #define is_class_abstract(clss) _is_class_abstract(clss)
606 #define set_class_abstract(clss, flag) _set_class_abstract(clss, flag)
607 #define is_Struct_type(strct) _is_struct_type(strct)
608 #define is_Method_type(method) _is_method_type(method)
609 #define is_Union_type(uni) _is_union_type(uni)
610 #define is_Array_type(array) _is_array_type(array)
611 #define is_Enumeration_type(enumeration) _is_enumeration_type(enumeration)
612 #define is_Pointer_type(pointer) _is_pointer_type(pointer)
613 #define is_Primitive_type(primitive) _is_primitive_type(primitive)
614 #define is_atomic_type(tp) _is_atomic_type(tp)
615 #define get_method_n_params(method) _get_method_n_params(method)
616 #define get_method_n_ress(method) _get_method_n_ress(method)
617 #define get_method_additional_properties(method) _get_method_additional_properties(method)
618 #define set_method_additional_properties(method, mask) _set_method_additional_properties(method, mask)
619 #define set_method_additional_property(method, flag) _set_method_additional_property(method, flag)
620 #define get_method_calling_convention(method) _get_method_calling_convention(method)
621 #define set_method_calling_convention(method, cc_mask) _set_method_calling_convention(method, cc_mask)
623 #endif /* FIRM_TR_TYPE_T_H */