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