fixed multi-input join (thx, Boris) --flo
[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 # ifdef HAVE_CONFIG_H
16 # include "config.h"
17 # endif
18 # include "type.h"
19 # include "tpop_t.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   type   **subtypes;   /**< direct subtypes */
35   type   **supertypes; /**< direct supertypes */
36   peculiarity peculiarity;
37   int dfn;             /**< number used for 'instanceof' operator */
38 } cls_attr;
39
40 /** struct attributs */
41 typedef struct {
42   entity **members;    /**< fields of this struct. No method entities
43               allowed. */
44 } stc_attr;
45
46 /** method attributes */
47 typedef struct {
48   int n_params;              /**< number of parameters */
49   type **param_type;         /**< code generation needs this information. */
50   type *value_params;        /**< A type whose entities represent copied value arguments. */
51   int n_res;                 /**< number of results */
52   type **res_type;           /**< array with result types */
53   type *value_ress;          /**< A type whose entities represent copied value results. */
54   variadicity variadicity;   /**< variadicity of the method. */
55   int first_variadic_param;  /**< index of the first variadic param or -1 if non-variadic .*/
56 } mtd_attr;
57
58 /** union attributs */
59 typedef struct {
60   int     n_types;
61   /* type  **unioned_type; * a list of unioned types. */
62   /* ident **delim_names;  * names of the union delimiters. */
63   entity **members;    /**< fields of this union. No method entities
64               allowed.  */
65
66 } uni_attr;
67
68 /** array attributs */
69 typedef struct {
70   int   n_dimensions;  /**< Number of array dimensions.  */
71   ir_node **lower_bound;   /**< Lower bounds of dimensions.  Usually all 0. */
72   ir_node **upper_bound;   /**< Upper bounds or dimensions. */
73   int *order;              /**< Ordering of dimensions. */
74   type *element_type;  /**< The type of the array elements. */
75   entity *element_ent; /**< Entity for the array elements, to be used for
76               element selection with Sel. */
77 } arr_attr;
78
79 /** enum attributs */
80 typedef struct {
81   int      n_enums;    /**< Number of enumerators. */
82   tarval **enumer;     /**< Contains all constants that represent a member
83                           of the enum -- enumerators. */
84   ident  **enum_nameid;/**< Contains the names of the enum fields as specified by
85                           the source program */
86 } enm_attr;
87
88 /** pointer attributs */
89 typedef struct {
90   type *points_to;     /**< The type of the enitity the pointer points to. */
91 } ptr_attr;
92
93 /*
94 typedef struct {   * No private attr yet! *
95 } pri_attr; */
96
97
98 /*
99 typedef struct {        * No private attr, must be smaller than others! *
100 } id_attr;
101 */
102
103 /** General type attributs. */
104 typedef union {
105   cls_attr ca;      /**< attributes of a class type */
106   stc_attr sa;      /**< attributes of a struct type */
107   mtd_attr ma;      /**< attributes of a method type */
108   uni_attr ua;      /**< attributes of an union type */
109   arr_attr aa;      /**< attributes of an array type */
110   enm_attr ea;      /**< attributes of an enumeration type */
111   ptr_attr pa;      /**< attributes of a pointer type */
112 } tp_attr;
113
114 /** the structure of a type */
115 struct type {
116   firm_kind kind;
117   tp_op *type_op;
118   ident *name;
119   type_state state;        /**< Represents the types state: layout undefined or
120                   fixed. */
121   int size;                /**< Size of an entity of this type. This is determined
122                   when fixing the layout of this class.  Size must be
123                   given in bits. */
124   int align;               /**< Alignment of an entity of this type. This should be
125                                 set according to the source language needs. If not set it's
126                                 calculated automatically by get_type_alignment().
127                                 Alignment must be given in bits. */
128   ir_mode *mode;           /**< The mode for atomic types */
129   unsigned long visit;     /**< visited counter for walks of the type information */
130   void *link;              /**< holds temporary data - like in irnode_t.h */
131   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
132
133   /* ------------- fields for analyses ---------------*/
134   ir_node **allocations;    /**< array of all Alloc nodes with this type
135                   @@@ Should not be in here, hash table! */
136
137 #ifdef DEBUG_libfirm
138   int nr;             /**< a unique node number for each node to make output
139                   readable. */
140   char *c_name;                 /**< since idents are opaque, provide the name in cleartext */
141 #endif
142   tp_attr attr;            /* type kind specific fields. This must be the last
143                   entry in this struct!  Varying size! */
144 };
145
146 /**
147  *   Creates a new type representation:
148  *
149  *   @param type_op - the kind of this type.  May not be type_id.
150  *   @param mode    - the mode to be used for this type, may be NULL
151  *   @param name    - an ident for the name of this type.
152  *   @return a new type of the given type.  The remaining private attributes are not
153  *   @return initalized.  The type is in state layout_undefined.
154  *
155  */
156 INLINE type *
157 new_type(tp_op *type_op,
158      ir_mode *mode,
159      ident* name);
160 void free_type_attrs       (type *tp);
161
162 INLINE void free_class_entities      (type *clss);
163 INLINE void free_struct_entities     (type *strct);
164 INLINE void free_method_entities     (type *method);
165 INLINE void free_union_entities      (type *uni);
166 INLINE void free_array_entities      (type *array);
167 INLINE void free_enumeration_entities(type *enumeration);
168 INLINE void free_pointer_entities    (type *pointer);
169 INLINE void free_primitive_entities  (type *primitive);
170
171 INLINE void free_class_attrs      (type *clss);
172 INLINE void free_struct_attrs     (type *strct);
173 INLINE void free_method_attrs     (type *method);
174 INLINE void free_union_attrs      (type *uni);
175 INLINE void free_array_attrs      (type *array);
176 INLINE void free_enumeration_attrs(type *enumeration);
177 INLINE void free_pointer_attrs    (type *pointer);
178 INLINE void free_primitive_attrs  (type *primitive);
179
180
181 /** initialize the type module */
182 void init_type (void);
183
184
185 /* ------------------- *
186  *  inline functions   *
187  * ------------------- */
188
189 extern unsigned long type_visited;
190
191 static INLINE void __set_master_type_visited(unsigned long val) { type_visited = val; }
192 static INLINE unsigned long __get_master_type_visited(void)     { return type_visited; }
193 static INLINE void __inc_master_type_visited(void)              { type_visited++; }
194
195 static INLINE void *
196 __get_type_link(const type *tp) {
197   assert(tp && tp->kind == k_type);
198   return(tp -> link);
199 }
200
201 static INLINE void
202 __set_type_link(type *tp, void *l) {
203   assert(tp && tp->kind == k_type);
204   tp -> link = l;
205 }
206
207 static INLINE tp_op*
208 __get_type_tpop(const type *tp) {
209   assert(tp && tp->kind == k_type);
210   return tp->type_op;
211 }
212
213 static INLINE ident*
214 __get_type_tpop_nameid(const type *tp) {
215   assert(tp && tp->kind == k_type);
216   return get_tpop_ident(tp->type_op);
217 }
218
219 static INLINE tp_opcode
220 __get_type_tpop_code(const type *tp) {
221   assert(tp && tp->kind == k_type);
222   return get_tpop_code(tp->type_op);
223 }
224
225 static INLINE ir_mode *
226 __get_type_mode(const type *tp) {
227   assert(tp && tp->kind == k_type);
228   return tp->mode;
229 }
230
231 static INLINE ident *
232 __get_type_ident(const type *tp) {
233   assert(tp && tp->kind == k_type);
234   return tp->name;
235 }
236
237 static INLINE void
238 __set_type_ident(type *tp, ident* id) {
239   assert(tp && tp->kind == k_type);
240   tp->name = id;
241 }
242
243 static INLINE long
244 __get_type_nr(const type *tp) {
245   assert(tp);
246 #ifdef DEBUG_libfirm
247   return tp->nr;
248 #else
249   return (long)tp;
250 #endif
251 }
252
253 static INLINE int
254 __get_type_size_bits(const type *tp) {
255   assert(tp && tp->kind == k_type);
256   return tp->size;
257 }
258
259 static INLINE int
260 __get_type_size_bytes(const type *tp) {
261   int size = __get_type_size_bits(tp);
262   if (size < 0)
263     return -1;
264   if ((size & 7) != 0) {
265     assert(0 && "cannot take byte size of this type");
266     return -1;
267   }
268   return size >> 3;
269 }
270
271 static INLINE type_state
272 __get_type_state(const type *tp) {
273   assert(tp && tp->kind == k_type);
274   return tp->state;
275 }
276
277 static INLINE unsigned long
278 __get_type_visited(const type *tp) {
279   assert(tp && tp->kind == k_type);
280   return tp->visit;
281 }
282
283 static INLINE void
284 __set_type_visited(type *tp, unsigned long num) {
285   assert(tp && tp->kind == k_type);
286   tp->visit = num;
287 }
288
289 static INLINE void
290 __mark_type_visited(type *tp) {
291   assert(tp && tp->kind == k_type);
292   assert(tp->visit < type_visited);
293   tp->visit = type_visited;
294 }
295
296 static INLINE int
297 __type_visited(const type *tp) {
298   assert(tp && tp->kind == k_type);
299   return tp->visit >= type_visited;
300 }
301
302 static INLINE int
303 __type_not_visited(const type *tp) {
304   assert(tp && tp->kind == k_type);
305   return tp->visit  < type_visited;
306 }
307
308 static INLINE int
309 __is_type(const void *thing) {
310   return (get_kind(thing) == k_type);
311 }
312
313 static INLINE int
314 __is_class_type(const type *clss) {
315   assert(clss);
316   return (clss->type_op == type_class);
317 }
318
319 static INLINE int
320 __get_class_n_members (const type *clss) {
321   assert(clss && (clss->type_op == type_class));
322   return (ARR_LEN (clss->attr.ca.members));
323 }
324
325 static INLINE entity *
326 __get_class_member   (const type *clss, int pos) {
327   assert(clss && (clss->type_op == type_class));
328   assert(pos >= 0 && pos < __get_class_n_members(clss));
329   return clss->attr.ca.members[pos];
330 }
331
332 static INLINE int
333 __is_struct_type(const type *strct) {
334   assert(strct);
335   return (strct->type_op == type_struct);
336 }
337
338 static INLINE int
339 __is_method_type(const type *method) {
340   assert(method);
341   return (method->type_op == type_method);
342 }
343
344 static INLINE int
345 __is_union_type(const type *uni) {
346   assert(uni);
347   return (uni->type_op == type_union);
348 }
349
350 static INLINE int
351 __is_array_type(const type *array) {
352   assert(array);
353   return (array->type_op == type_array);
354 }
355
356 static INLINE int
357 __is_enumeration_type(const type *enumeration) {
358   assert(enumeration);
359   return (enumeration->type_op == type_enumeration);
360 }
361
362 static INLINE int
363 __is_pointer_type(const type *pointer) {
364   assert(pointer);
365   return (pointer->type_op == type_pointer);
366 }
367
368 /** Returns true if a type is a primitive type. */
369 static INLINE int
370 __is_primitive_type(const type *primitive) {
371   assert(primitive && primitive->kind == k_type);
372   return (primitive->type_op == type_primitive);
373 }
374
375 static INLINE int
376 __is_atomic_type(const type *tp) {
377   assert(tp && tp->kind == k_type);
378   return (is_primitive_type(tp) || is_pointer_type(tp) ||
379       is_enumeration_type(tp));
380 }
381
382
383 #define set_master_type_visited(val)      __set_master_type_visited(val)
384 #define get_master_type_visited()         __get_master_type_visited()
385 #define inc_master_type_visited()         __inc_master_type_visited()
386 #define get_type_link(tp)                 __get_type_link(tp)
387 #define set_type_link(tp, l)              __set_type_link(tp, l)
388 #define get_type_tpop(tp)                 __get_type_tpop(tp)
389 #define get_type_tpop_nameid(tp)          __get_type_tpop_nameid(tp)
390 #define get_type_tpop_code(tp)            __get_type_tpop_code(tp)
391 #define get_type_mode(tp)                 __get_type_mode(tp)
392 #define get_type_ident(tp)                __get_type_ident(tp)
393 #define set_type_ident(tp, id)            __set_type_ident(tp, id)
394 #define get_type_nr(tp)                   __get_type_nr(tp)
395 #define get_type_size(tp)                 __get_type_size(tp)
396 #define get_type_state(tp)                __get_type_state(tp)
397 #define get_type_visited(tp)              __get_type_visited(tp)
398 #define set_type_visited(tp, num)         __set_type_visited(tp, num)
399 #define mark_type_visited(tp)             __mark_type_visited(tp)
400 #define type_visited(tp)                  __type_visited(tp)
401 #define type_not_visited(tp)              __type_not_visited(tp)
402 #define is_type(thing)                    __is_type(thing)
403 #define is_class_type(clss)               __is_class_type(clss)
404 #define get_class_n_members(clss)         __get_class_n_members(clss)
405 #define get_class_member(clss, pos)       __get_class_member(clss, pos)
406 #define is_struct_type(strct)             __is_struct_type(strct)
407 #define is_method_type(method)            __is_method_type(method)
408 #define is_union_type(uni)                __is_union_type(uni)
409 #define is_array_type(array)              __is_array_type(array)
410 #define is_enumeration_type(enumeration)  __is_enumeration_type(enumeration)
411 #define is_pointer_type(pointer)          __is_pointer_type(pointer)
412 #define is_primitive_type(primitive)      __is_primitive_type(primitive)
413 #define is_atomic_type(tp)                __is_atomic_type(tp)
414
415 # endif /* _TYPE_T_H_ */