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