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