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