5e576921dfa4022310f9f6ddcf583839274a74c9
[libfirm] / ir / tr / type_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/type_t.h
4  * Purpose:     Representation of types -- private header.
5  * Author:      Goetz Lindenmaier
6  * Modified by: Michael Beck
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2001-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifndef _FIRM_TR_TYPE_T_H_
14 #define _FIRM_TR_TYPE_T_H_
15
16 #include "firm_config.h"
17 #include "type.h"
18 #include "tpop_t.h"
19 #include "irgraph.h"
20
21 #include "array.h"
22
23 /**
24  * @file type_t.h
25  * This file contains the datatypes hidden in type.h.
26  *
27  * @author Goetz Lindenmaier
28  * @see  type.h tpop_t.h tpop.h
29  */
30
31 /** Class flags. */
32 enum class_flags {
33   cf_none            = 0,  /**< No flags. */
34   cf_final_class     = 1,  /**< Set if a class is an final class */
35   cf_interface_class = 2,  /**< Set if a class is an "interface" */
36   cf_absctract_class = 4,  /**< Set if a class is "abstract" */
37 };
38
39 /** Class type attributes. */
40 typedef struct {
41   ir_entity  **members;           /**< Array containing the fields and methods of this class. */
42   ir_type **subtypes;          /**< Array containing the direct subtypes. */
43   ir_type **supertypes;        /**< Array containing the direct supertypes */
44   ir_peculiarity peculiarity;  /**< The peculiarity of this class. */
45   ir_entity *type_info;        /**< An ir_entity representing this class, used for type info. */
46   int      dfn;                /**< A number that can be used for 'instanceof' operator. */
47   unsigned vtable_size;        /**< The size of the vtable for this class. */
48   unsigned clss_flags;         /**< Additional class flags. */
49 } cls_attr;
50
51 /** Struct type attributes. */
52 typedef struct {
53   ir_entity **members; /**< Fields of this struct. No method entities allowed. */
54 } stc_attr;
55
56 /** A (type, ir_entity) pair. */
57 typedef struct {
58   ir_type *tp;         /**< A type. */
59   ir_entity  *ent;     /**< An ir_entity. */
60   ident   *param_name; /**< For debugging purposes: the name of the parameter */
61 } tp_ent_pair;
62
63 /** Method type attributes. */
64 typedef struct {
65   int n_params;                   /**< Number of parameters. */
66   tp_ent_pair *params;            /**< Array of parameter type/value entities pairs. */
67   ir_type *value_params;          /**< A type whose entities represent copied value arguments. */
68   int n_res;                      /**< Number of results. */
69   tp_ent_pair *res_type;          /**< Array of result type/value ir_entity pairs. */
70   ir_type *value_ress;            /**< A type whose entities represent copied value results. */
71   variadicity variadicity;        /**< The variadicity of the method. */
72   int first_variadic_param;       /**< The index of the first variadic parameter or -1 if non-variadic .*/
73   unsigned additional_properties; /**< Set of additional method properties. */
74   unsigned irg_calling_conv;      /**< A set of calling convention flags. */
75 } mtd_attr;
76
77 /** Union type attributes. */
78 typedef struct {
79   ir_entity **members;    /**< Fields of this union. No method entities allowed. */
80 } uni_attr;
81
82 /** Array type attributes. */
83 typedef struct {
84   int     n_dimensions;   /**< Number of array dimensions.  */
85   ir_node **lower_bound;  /**< Lower bounds of dimensions.  Usually all 0. */
86   ir_node **upper_bound;  /**< Upper bounds or dimensions. */
87   int     *order;         /**< Ordering of dimensions. */
88   ir_type *element_type;  /**< The type of the array elements. */
89   ir_entity *element_ent; /**< entity for the array elements, to be used for
90                                element selection with a Sel node. */
91 } arr_attr;
92
93 /** An enumerator constant. */
94 struct ir_enum_const {
95   tarval  *value;     /**< The constants that represents this enumerator identifier. */
96   ident   *nameid;    /**< The name of the enumerator identifier. */
97   ir_type *owner;     /**< owner type of this enumerator constant. */
98 };
99
100 /** Enum type attributes. */
101 typedef struct {
102   ir_enum_const *enumer;   /**< Contains all enumerator constants that represent a member
103                                 of the enum -- enumerators. */
104 } enm_attr;
105
106 /** Pointer type attributes. */
107 typedef struct {
108   ir_type *points_to;  /**< The type of the ir_entity the pointer points to. */
109 } ptr_attr;
110
111 /*
112 typedef struct {   * No private attr yet! *
113 } pri_attr; */
114
115
116 /*
117 typedef struct {        * No private attr, must be smaller than others! *
118 } id_attr;
119 */
120
121 /** General type attributes. */
122 typedef union {
123   cls_attr ca;      /**< Attributes of a class type */
124   stc_attr sa;      /**< Attributes of a struct type */
125   mtd_attr ma;      /**< Attributes of a method type */
126   uni_attr ua;      /**< Attributes of an union type */
127   arr_attr aa;      /**< Attributes of an array type */
128   enm_attr ea;      /**< Attributes of an enumeration type */
129   ptr_attr pa;      /**< Attributes of a pointer type */
130 } tp_attr;
131
132 /** Additional type flags. */
133 enum type_flags {
134   tf_none             =  0, /**< No flags. */
135   tf_frame_type       =  1, /**< Set if this is a frame type. */
136   tf_value_param_type =  2, /**< Set if this is a value param type. */
137   tf_lowered_type     =  4, /**< Set if this is a lowered type. */
138   tf_layout_fixed     =  8, /**< Set if the layout of a type is fixed */
139   tf_global_type      = 16, /**< Set only for the global type */
140   tf_tls_type         = 32, /**< Set only for the tls type */
141 };
142
143 /** The structure of a type. */
144 struct ir_type {
145   firm_kind kind;          /**< the firm kind, must be k_type */
146   const tp_op *type_op;    /**< the type operation of the type */
147   ident *name;             /**< The name of the type */
148   ir_visibility visibility;/**< Visibility of entities of this type. */
149   unsigned flags;          /**< Type flags, a bitmask of enum type_flags. */
150   int size;                /**< Size of an ir_entity of this type. This is determined
151                                 when fixing the layout of this class.  Size must be
152                                 given in bits. */
153   int align;               /**< Alignment of an ir_entity of this type. This should be
154                                 set according to the source language needs. If not set it's
155                                 calculated automatically by get_type_alignment().
156                                 Alignment must be given in bits. */
157   ir_mode *mode;           /**< The mode for atomic types */
158   unsigned long visit;     /**< visited counter for walks of the type information */
159   void *link;              /**< holds temporary data - like in irnode_t.h */
160   struct dbg_info *dbi;    /**< A pointer to information for debug support. */
161   ir_type *assoc_type;     /**< The associated lowered/unlowered type */
162
163   /* ------------- fields for analyses ---------------*/
164
165 #ifdef DEBUG_libfirm
166   long nr;                 /**< An unique node number for each node to make output
167                                 readable. */
168 #endif
169   tp_attr attr;            /**< Type kind specific fields. This must be the last
170                                 entry in this struct!  Varying size! */
171 };
172
173 /**
174  *   Creates a new type representation:
175  *
176  *   @param type_op  the kind of this type.  May not be type_id.
177  *   @param mode     the mode to be used for this type, may be NULL
178  *   @param name     an ident for the name of this type.
179  *   @param db       debug info
180  *
181  *   @return A new type of the given type.  The remaining private attributes are not
182  *           initialized.  The type is in state layout_undefined.
183  */
184 ir_type *
185 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
186 void free_type_attrs       (ir_type *tp);
187
188 void free_class_entities      (ir_type *clss);
189 void free_struct_entities     (ir_type *strct);
190 void free_method_entities     (ir_type *method);
191 void free_union_entities      (ir_type *uni);
192 void free_array_entities      (ir_type *array);
193 void free_enumeration_entities(ir_type *enumeration);
194 void free_pointer_entities    (ir_type *pointer);
195
196 void free_array_automatic_entities(ir_type *array);
197
198 void free_class_attrs      (ir_type *clss);
199 void free_struct_attrs     (ir_type *strct);
200 void free_method_attrs     (ir_type *method);
201 void free_union_attrs      (ir_type *uni);
202 void free_array_attrs      (ir_type *array);
203 void free_enumeration_attrs(ir_type *enumeration);
204 void free_pointer_attrs    (ir_type *pointer);
205
206 void set_class_mode(ir_type *tp, ir_mode *mode);
207 void set_struct_mode(ir_type *tp, ir_mode *mode);
208 void set_pointer_mode(ir_type *tp, ir_mode *mode);
209 void set_primitive_mode(ir_type *tp, ir_mode *mode);
210 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
211
212 void set_class_size_bits(ir_type *tp, int bits);
213 void set_struct_size_bits(ir_type *tp, int bits);
214 void set_union_size_bits(ir_type *tp, int bits);
215 void set_array_size_bits(ir_type *tp, int size);
216 void set_default_size_bits(ir_type *tp, int size);
217
218 /**
219  * Initialize the type module.
220  *
221  * @param builtin_db       debug info for built-in objects
222  * @param default_cc_mask  default calling conventions for methods
223  */
224 void firm_init_type(dbg_info *builtin_db, unsigned default_cc_mask);
225
226
227 /* ------------------- *
228  *  inline functions   *
229  * ------------------- */
230
231 extern unsigned long firm_type_visited;
232
233 static INLINE void _set_master_type_visited(unsigned long val) { firm_type_visited = val; }
234 static INLINE unsigned long _get_master_type_visited(void)     { return firm_type_visited; }
235 static INLINE void _inc_master_type_visited(void)              { ++firm_type_visited; }
236
237 static INLINE void *
238 _get_type_link(const ir_type *tp) {
239   assert(tp && tp->kind == k_type);
240   return(tp -> link);
241 }
242
243 static INLINE void
244 _set_type_link(ir_type *tp, void *l) {
245   assert(tp && tp->kind == k_type);
246   tp -> link = l;
247 }
248
249 static INLINE const tp_op*
250 _get_type_tpop(const ir_type *tp) {
251   assert(tp && tp->kind == k_type);
252   return tp->type_op;
253 }
254
255 static INLINE ident*
256 _get_type_tpop_nameid(const ir_type *tp) {
257   assert(tp && tp->kind == k_type);
258   return get_tpop_ident(tp->type_op);
259 }
260
261 static INLINE tp_opcode
262 _get_type_tpop_code(const ir_type *tp) {
263   assert(tp && tp->kind == k_type);
264   return get_tpop_code(tp->type_op);
265 }
266
267 static INLINE ir_mode *
268 _get_type_mode(const ir_type *tp) {
269   assert(tp && tp->kind == k_type);
270   return tp->mode;
271 }
272
273 static INLINE ident *
274 _get_type_ident(const ir_type *tp) {
275   assert(tp && tp->kind == k_type);
276   return tp->name;
277 }
278
279 static INLINE void
280 _set_type_ident(ir_type *tp, ident* id) {
281   assert(tp && tp->kind == k_type);
282   tp->name = id;
283 }
284
285 static INLINE int
286 _get_type_size_bits(const ir_type *tp) {
287   assert(tp && tp->kind == k_type);
288   return tp->size;
289 }
290
291 static INLINE int
292 _get_type_size_bytes(const ir_type *tp) {
293   int size = _get_type_size_bits(tp);
294   if (size < 0)
295     return -1;
296   if ((size & 7) != 0) {
297     assert(0 && "cannot take byte size of this type");
298     return -1;
299   }
300   return size >> 3;
301 }
302
303 static INLINE type_state
304 _get_type_state(const ir_type *tp) {
305   assert(tp && tp->kind == k_type);
306   return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
307 }
308
309 static INLINE unsigned long
310 _get_type_visited(const ir_type *tp) {
311   assert(tp && tp->kind == k_type);
312   return tp->visit;
313 }
314
315 static INLINE void
316 _set_type_visited(ir_type *tp, unsigned long num) {
317   assert(tp && tp->kind == k_type);
318   tp->visit = num;
319 }
320
321 static INLINE void
322 _mark_type_visited(ir_type *tp) {
323   assert(tp && tp->kind == k_type);
324   assert(tp->visit < firm_type_visited);
325   tp->visit = firm_type_visited;
326 }
327
328 static INLINE int
329 _type_visited(const ir_type *tp) {
330   assert(tp && tp->kind == k_type);
331   return tp->visit >= firm_type_visited;
332 }
333
334 static INLINE int
335 _type_not_visited(const ir_type *tp) {
336   assert(tp && tp->kind == k_type);
337   return tp->visit  < firm_type_visited;
338 }
339
340 static INLINE int
341 _is_type(const void *thing) {
342   return (get_kind(thing) == k_type);
343 }
344
345 static INLINE int
346 _is_class_type(const ir_type *clss) {
347   assert(clss);
348   return (clss->type_op == type_class);
349 }
350
351 static INLINE int
352 _get_class_n_members (const ir_type *clss) {
353   assert(clss && (clss->type_op == type_class));
354   return (ARR_LEN (clss->attr.ca.members));
355 }
356
357 static INLINE ir_entity *
358 _get_class_member   (const ir_type *clss, int pos) {
359   assert(clss && (clss->type_op == type_class));
360   assert(pos >= 0 && pos < _get_class_n_members(clss));
361   return clss->attr.ca.members[pos];
362 }
363
364 static INLINE unsigned
365 _get_class_vtable_size(const ir_type *clss) {
366   assert(clss && (clss->type_op == type_class));
367   return clss->attr.ca.vtable_size;
368 }
369
370 static INLINE void
371 _set_class_vtable_size(ir_type *clss, unsigned vtable_size) {
372   assert(clss && (clss->type_op == type_class));
373   clss->attr.ca.vtable_size = vtable_size;
374 }
375
376 static INLINE int
377 _is_class_final(const ir_type *clss) {
378   assert(clss && (clss->type_op == type_class));
379   return clss->attr.ca.clss_flags & cf_final_class;
380 }
381
382 static INLINE void
383 _set_class_final(ir_type *clss, int final) {
384   assert(clss && (clss->type_op == type_class));
385   if (final)
386     clss->attr.ca.clss_flags |= cf_final_class;
387   else
388     clss->attr.ca.clss_flags &= ~cf_final_class;
389 }
390
391 static INLINE int
392 _is_class_interface(const ir_type *clss) {
393   assert(clss && (clss->type_op == type_class));
394   return clss->attr.ca.clss_flags & cf_interface_class;
395 }
396
397 static INLINE void
398 _set_class_interface(ir_type *clss, int final) {
399   assert(clss && (clss->type_op == type_class));
400   if (final)
401     clss->attr.ca.clss_flags |= cf_interface_class;
402   else
403     clss->attr.ca.clss_flags &= ~cf_interface_class;
404 }
405
406 static INLINE int
407 _is_class_abstract(const ir_type *clss) {
408   assert(clss && (clss->type_op == type_class));
409   return clss->attr.ca.clss_flags & cf_absctract_class;
410 }
411
412 static INLINE void
413 _set_class_abstract(ir_type *clss, int final) {
414   assert(clss && (clss->type_op == type_class));
415   if (final)
416     clss->attr.ca.clss_flags |= cf_absctract_class;
417   else
418     clss->attr.ca.clss_flags &= ~cf_absctract_class;
419 }
420
421 static INLINE int
422 _is_struct_type(const ir_type *strct) {
423   assert(strct);
424   return (strct->type_op == type_struct);
425 }
426
427 static INLINE int
428 _is_method_type(const ir_type *method) {
429   assert(method);
430   return (method->type_op == type_method);
431 }
432
433 static INLINE int
434 _is_union_type(const ir_type *uni) {
435   assert(uni);
436   return (uni->type_op == type_union);
437 }
438
439 static INLINE int
440 _is_array_type(const ir_type *array) {
441   assert(array);
442   return (array->type_op == type_array);
443 }
444
445 static INLINE int
446 _is_enumeration_type(const ir_type *enumeration) {
447   assert(enumeration);
448   return (enumeration->type_op == type_enumeration);
449 }
450
451 static INLINE int
452 _is_pointer_type(const ir_type *pointer) {
453   assert(pointer);
454   return (pointer->type_op == type_pointer);
455 }
456
457 /** Returns true if a type is a primitive type. */
458 static INLINE int
459 _is_primitive_type(const ir_type *primitive) {
460   assert(primitive && primitive->kind == k_type);
461   return (primitive->type_op == type_primitive);
462 }
463
464 static INLINE int
465 _is_atomic_type(const ir_type *tp) {
466   assert(tp && tp->kind == k_type);
467   return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
468       _is_enumeration_type(tp));
469 }
470
471 static INLINE int
472 _get_method_n_params(const ir_type *method) {
473   assert(method && (method->type_op == type_method));
474   return method->attr.ma.n_params;
475 }
476
477 static INLINE int
478 _get_method_n_ress(const ir_type *method) {
479   assert(method && (method->type_op == type_method));
480   return method->attr.ma.n_res;
481 }
482
483 static INLINE unsigned
484 _get_method_additional_properties(const ir_type *method) {
485   assert(method && (method->type_op == type_method));
486   return method->attr.ma.additional_properties;
487 }
488
489 static INLINE void
490 _set_method_additional_properties(ir_type *method, unsigned mask) {
491   assert(method && (method->type_op == type_method));
492
493   /* do not allow to set the mtp_property_inherited flag or
494    * the automatic inheritance of flags will not work */
495   method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
496 }
497
498 static INLINE void
499 _set_method_additional_property(ir_type *method, mtp_additional_property flag) {
500   assert(method && (method->type_op == type_method));
501
502   /* do not allow to set the mtp_property_inherited flag or
503    * the automatic inheritance of flags will not work */
504   method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
505 }
506
507 static INLINE unsigned
508 _get_method_calling_convention(const ir_type *method) {
509   assert(method && (method->type_op == type_method));
510   return method->attr.ma.irg_calling_conv;
511 }
512
513 static INLINE void
514 _set_method_calling_convention(ir_type *method, unsigned cc_mask) {
515   assert(method && (method->type_op == type_method));
516   method->attr.ma.irg_calling_conv = cc_mask;
517 }
518
519 #define set_master_type_visited(val)      _set_master_type_visited(val)
520 #define get_master_type_visited()         _get_master_type_visited()
521 #define inc_master_type_visited()         _inc_master_type_visited()
522 #define get_type_link(tp)                 _get_type_link(tp)
523 #define set_type_link(tp, l)              _set_type_link(tp, l)
524 #define get_type_tpop(tp)                 _get_type_tpop(tp)
525 #define get_type_tpop_nameid(tp)          _get_type_tpop_nameid(tp)
526 #define get_type_tpop_code(tp)            _get_type_tpop_code(tp)
527 #define get_type_mode(tp)                 _get_type_mode(tp)
528 #define get_type_ident(tp)                _get_type_ident(tp)
529 #define set_type_ident(tp, id)            _set_type_ident(tp, id)
530 #define get_type_size_bits(tp)            _get_type_size_bits(tp)
531 #define get_type_size_bytes(tp)           _get_type_size_bytes(tp)
532 #define get_type_state(tp)                _get_type_state(tp)
533 #define get_type_visited(tp)              _get_type_visited(tp)
534 #define set_type_visited(tp, num)         _set_type_visited(tp, num)
535 #define mark_type_visited(tp)             _mark_type_visited(tp)
536 #define type_visited(tp)                  _type_visited(tp)
537 #define type_not_visited(tp)              _type_not_visited(tp)
538 #define is_type(thing)                    _is_type(thing)
539 #define is_Class_type(clss)               _is_class_type(clss)
540 #define get_class_n_members(clss)         _get_class_n_members(clss)
541 #define get_class_member(clss, pos)       _get_class_member(clss, pos)
542 #define get_class_vtable_size(clss)       _get_class_vtable_size(clss)
543 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
544 #define is_class_final(clss)              _is_class_final(clss)
545 #define set_class_final(clss, flag)       _set_class_final(clss, flag)
546 #define is_class_interface(clss)          _is_class_interface(clss)
547 #define set_class_interface(clss, flag)   _set_class_interface(clss, flag)
548 #define is_class_abstract(clss)           _is_class_abstract(clss)
549 #define set_class_abstract(clss, flag)    _set_class_abstract(clss, flag)
550 #define is_Struct_type(strct)             _is_struct_type(strct)
551 #define is_Method_type(method)            _is_method_type(method)
552 #define is_Union_type(uni)                _is_union_type(uni)
553 #define is_Array_type(array)              _is_array_type(array)
554 #define is_Enumeration_type(enumeration)  _is_enumeration_type(enumeration)
555 #define is_Pointer_type(pointer)          _is_pointer_type(pointer)
556 #define is_Primitive_type(primitive)      _is_primitive_type(primitive)
557 #define is_atomic_type(tp)                _is_atomic_type(tp)
558 #define get_method_n_params(method)       _get_method_n_params(method)
559 #define get_method_n_ress(method)         _get_method_n_ress(method)
560 #define get_method_additional_properties(method)        _get_method_additional_properties(method)
561 #define set_method_additional_properties(method, mask)  _set_method_additional_properties(method, mask)
562 #define set_method_additional_property(method, flag)    _set_method_additional_property(method, flag)
563 #define get_method_calling_convention(method)           _get_method_calling_convention(method)
564 #define set_method_calling_convention(method, cc_mask)  _set_method_calling_convention(method, cc_mask)
565
566 #endif /* _FIRM_TR_TYPE_T_H_ */