initialization,
[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   ir_mode *mode;           /**< The mode for atomic types */
125   unsigned long visit;     /**< visited counter for walks of the type information */
126   void *link;              /**< holds temporary data - like in irnode_t.h */
127   struct dbg_info* dbi;    /**< A pointer to information for debug support. */
128
129   /* ------------- fields for analyses ---------------*/
130   ir_node **allocations;    /**< array of all Alloc nodes with this type
131                               @@@ Should not be in here, hash table! */
132
133 #ifdef DEBUG_libfirm
134   int nr;             /**< a unique node number for each node to make output
135                               readable. */
136 #endif
137   tp_attr attr;            /* type kind specific fields. This must be the last
138                               entry in this struct!  Varying size! */
139 };
140
141 /**
142  *   Creates a new type representation:
143  *
144  *   @param type_op - the kind of this type.  May not be type_id.
145  *   @param mode    - the mode to be used for this type, may be NULL
146  *   @param name    - an ident for the name of this type.
147  *   @return a new type of the given type.  The remaining private attributes are not
148  *   @return initalized.  The type is in state layout_undefined.
149  *
150  */
151 INLINE type *
152 new_type(tp_op *type_op,
153          ir_mode *mode,
154          ident* name);
155 void free_type_attrs       (type *tp);
156
157 INLINE void free_class_entities      (type *clss);
158 INLINE void free_struct_entities     (type *strct);
159 INLINE void free_method_entities     (type *method);
160 INLINE void free_union_entities      (type *uni);
161 INLINE void free_array_entities      (type *array);
162 INLINE void free_enumeration_entities(type *enumeration);
163 INLINE void free_pointer_entities    (type *pointer);
164 INLINE void free_primitive_entities  (type *primitive);
165
166 INLINE void free_class_attrs      (type *clss);
167 INLINE void free_struct_attrs     (type *strct);
168 INLINE void free_method_attrs     (type *method);
169 INLINE void free_union_attrs      (type *uni);
170 INLINE void free_array_attrs      (type *array);
171 INLINE void free_enumeration_attrs(type *enumeration);
172 INLINE void free_pointer_attrs    (type *pointer);
173 INLINE void free_primitive_attrs  (type *primitive);
174
175
176 /** initialize the type module */
177 void init_type (void);
178
179
180 /* ------------------- *
181  *  inline functions   *
182  * ------------------- */
183
184 extern unsigned long type_visited;
185
186 static INLINE void __set_master_type_visited(unsigned long val) { type_visited = val; }
187 static INLINE unsigned long __get_master_type_visited(void)     { return type_visited; }
188 static INLINE void __inc_master_type_visited(void)              { type_visited++; }
189
190 static INLINE void *
191 __get_type_link(type *tp) {
192   assert(tp && tp->kind == k_type);
193   return(tp -> link);
194 }
195
196 static INLINE void
197 __set_type_link(type *tp, void *l) {
198   assert(tp && tp->kind == k_type);
199   tp -> link = l;
200 }
201
202 static INLINE tp_op*
203 __get_type_tpop(type *tp) {
204   assert(tp && tp->kind == k_type);
205   return tp->type_op;
206 }
207
208 static INLINE ident*
209 __get_type_tpop_nameid(type *tp) {
210   assert(tp && tp->kind == k_type);
211   return get_tpop_ident(tp->type_op);
212 }
213
214 static INLINE tp_opcode
215 __get_type_tpop_code(type *tp) {
216   assert(tp && tp->kind == k_type);
217   return get_tpop_code(tp->type_op);
218 }
219
220 static INLINE ir_mode *
221 __get_type_mode(type *tp) {
222   assert(tp && tp->kind == k_type);
223   return tp->mode;
224 }
225
226 static INLINE ident *
227 __get_type_ident(type *tp) {
228   assert(tp && tp->kind == k_type);
229   return tp->name;
230 }
231
232 static INLINE void
233 __set_type_ident(type *tp, ident* id) {
234   assert(tp && tp->kind == k_type);
235   tp->name = id;
236 }
237
238 static INLINE long
239 __get_type_nr(type *tp) {
240   assert(tp);
241 #ifdef DEBUG_libfirm
242   return tp->nr;
243 #else
244   return (long)tp;
245 #endif
246 }
247
248 static INLINE int
249 __get_type_size_bits(type *tp) {
250   assert(tp && tp->kind == k_type);
251   return tp->size;
252 }
253
254 static INLINE int
255 __get_type_size_bytes(type *tp) {
256   int size = __get_type_size_bits(tp);
257   if (size < 0)
258     return -1;
259   if ((size & 7) != 0) {
260     assert(0 && "cannot take byte size of this type");
261     return -1;
262   }
263   return size >> 3;
264 }
265
266 static INLINE type_state
267 __get_type_state(type *tp) {
268   assert(tp && tp->kind == k_type);
269   return tp->state;
270 }
271
272 static INLINE unsigned long
273 __get_type_visited(type *tp) {
274   assert(tp && tp->kind == k_type);
275   return tp->visit;
276 }
277
278 static INLINE void
279 __set_type_visited(type *tp, unsigned long num) {
280   assert(tp && tp->kind == k_type);
281   tp->visit = num;
282 }
283
284 static INLINE void
285 __mark_type_visited(type *tp) {
286   assert(tp && tp->kind == k_type);
287   assert(tp->visit < type_visited);
288   tp->visit = type_visited;
289 }
290
291 static INLINE int
292 __type_visited(type *tp) {
293   assert(tp && tp->kind == k_type);
294   return tp->visit >= type_visited;
295 }
296
297 static INLINE int
298 __type_not_visited(type *tp) {
299   assert(tp && tp->kind == k_type);
300   return tp->visit  < type_visited;
301 }
302
303 static INLINE int
304 __is_type(void *thing) {
305   return (get_kind(thing) == k_type);
306 }
307
308 static INLINE int
309 __is_class_type(type *clss) {
310   assert(clss);
311   return (clss->type_op == type_class);
312 }
313
314 static INLINE int
315 __get_class_n_members (type *clss) {
316   assert(clss && (clss->type_op == type_class));
317   return (ARR_LEN (clss->attr.ca.members));
318 }
319
320 static INLINE entity *
321 __get_class_member   (type *clss, int pos) {
322   assert(clss && (clss->type_op == type_class));
323   assert(pos >= 0 && pos < get_class_n_members(clss));
324   return clss->attr.ca.members[pos];
325 }
326
327 static INLINE int
328 __is_struct_type(type *strct) {
329   assert(strct);
330   return (strct->type_op == type_struct);
331 }
332
333 static INLINE int
334 __is_method_type(type *method) {
335   assert(method);
336   return (method->type_op == type_method);
337 }
338
339 static INLINE int
340 __is_union_type(type *uni) {
341   assert(uni);
342   return (uni->type_op == type_union);
343 }
344
345 static INLINE int
346 __is_array_type(type *array) {
347   assert(array);
348   return (array->type_op == type_array);
349 }
350
351 static INLINE int
352 __is_enumeration_type(type *enumeration) {
353   assert(enumeration);
354   return (enumeration->type_op == type_enumeration);
355 }
356
357 static INLINE int
358 __is_pointer_type(type *pointer) {
359   assert(pointer);
360   return (pointer->type_op == type_pointer);
361 }
362
363 /** Returns true if a type is a primitive type. */
364 static INLINE int
365 __is_primitive_type(type *primitive) {
366   assert(primitive && primitive->kind == k_type);
367   return (primitive->type_op == type_primitive);
368 }
369
370 static INLINE int
371 __is_atomic_type(type *tp) {
372   assert(tp && tp->kind == k_type);
373   return (is_primitive_type(tp) || is_pointer_type(tp) ||
374           is_enumeration_type(tp));
375 }
376
377
378 #define set_master_type_visited(val)      __set_master_type_visited(val)
379 #define get_master_type_visited()         __get_master_type_visited()
380 #define inc_master_type_visited()         __inc_master_type_visited()
381 #define get_type_link(tp)                 __get_type_link(tp)
382 #define set_type_link(tp, l)              __set_type_link(tp, l)
383 #define get_type_tpop(tp)                 __get_type_tpop(tp)
384 #define get_type_tpop_nameid(tp)          __get_type_tpop_nameid(tp)
385 #define get_type_tpop_code(tp)            __get_type_tpop_code(tp)
386 #define get_type_mode(tp)                 __get_type_mode(tp)
387 #define get_type_ident(tp)                __get_type_ident(tp)
388 #define set_type_ident(tp, id)            __set_type_ident(tp, id)
389 #define get_type_nr(tp)                   __get_type_nr(tp)
390 #define get_type_size(tp)                 __get_type_size(tp)
391 #define get_type_state(tp)                __get_type_state(tp)
392 #define get_type_visited(tp)              __get_type_visited(tp)
393 #define set_type_visited(tp, num)         __set_type_visited(tp, num)
394 #define mark_type_visited(tp)             __mark_type_visited(tp)
395 #define type_visited(tp)                  __type_visited(tp)
396 #define type_not_visited(tp)              __type_not_visited(tp)
397 #define is_type(thing)                    __is_type(thing)
398 #define is_class_type(clss)               __is_class_type(clss)
399 #define get_class_n_members(clss)         __get_class_n_members(clss)
400 #define get_class_member(clss, pos)       __get_class_member(clss, pos)
401 #define is_struct_type(strct)             __is_struct_type(strct)
402 #define is_method_type(method)            __is_method_type(method)
403 #define is_union_type(uni)                __is_union_type(uni)
404 #define is_array_type(array)              __is_array_type(array)
405 #define is_enumeration_type(enumeration)  __is_enumeration_type(enumeration)
406 #define is_pointer_type(pointer)          __is_pointer_type(pointer)
407 #define is_primitive_type(primitive)      __is_primitive_type(primitive)
408 #define is_atomic_type(tp)                __is_atomic_type(tp)
409
410 # endif /* _TYPE_T_H_ */