bearch: Dump the output requirement and the assigned register in the same line for...
[libfirm] / ir / tr / type_t.h
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Representation of types -- private header.
23  * @author  Goetz Lindenmaier, Michael Beck
24  * @see     type.h tpop_t.h tpop.h
25  */
26 #ifndef FIRM_TR_TYPE_T_H
27 #define FIRM_TR_TYPE_T_H
28
29 #include <stdbool.h>
30 #include "typerep.h"
31 #include "tpop_t.h"
32 #include "irgraph.h"
33 #include "firm_common.h"
34
35 #include "array.h"
36
37 #define set_master_type_visited(val)      _set_master_type_visited(val)
38 #define get_master_type_visited()         _get_master_type_visited()
39 #define inc_master_type_visited()         _inc_master_type_visited()
40 #define get_type_link(tp)                 _get_type_link(tp)
41 #define set_type_link(tp, l)              _set_type_link(tp, l)
42 #define get_type_tpop(tp)                 _get_type_tpop(tp)
43 #define get_type_tpop_nameid(tp)          _get_type_tpop_nameid(tp)
44 #define get_type_tpop_code(tp)            _get_type_tpop_code(tp)
45 #define get_type_mode(tp)                 _get_type_mode(tp)
46 #define get_type_size_bytes(tp)           _get_type_size_bytes(tp)
47 #define get_type_state(tp)                _get_type_state(tp)
48 #define get_type_visited(tp)              _get_type_visited(tp)
49 #define set_type_visited(tp, num)         _set_type_visited(tp, num)
50 #define mark_type_visited(tp)             _mark_type_visited(tp)
51 #define type_visited(tp)                  _type_visited(tp)
52 #define type_not_visited(tp)              _type_not_visited(tp)
53 #define get_type_dbg_info(tp)             _get_type_dbg_info(tp)
54 #define set_type_dbg_info(tp, db)         _set_type_dbg_info(tp, db)
55 #define is_type(thing)                    _is_type(thing)
56 #define is_Class_type(clss)               _is_class_type(clss)
57 #define get_class_n_members(clss)         _get_class_n_members(clss)
58 #define get_class_member(clss, pos)       _get_class_member(clss, pos)
59 #define get_class_vtable_size(clss)       _get_class_vtable_size(clss)
60 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
61 #define is_class_final(clss)              _is_class_final(clss)
62 #define set_class_final(clss, flag)       _set_class_final(clss, flag)
63 #define is_class_interface(clss)          _is_class_interface(clss)
64 #define set_class_interface(clss, flag)   _set_class_interface(clss, flag)
65 #define is_class_abstract(clss)           _is_class_abstract(clss)
66 #define set_class_abstract(clss, flag)    _set_class_abstract(clss, flag)
67 #define is_Struct_type(strct)             _is_struct_type(strct)
68 #define is_Method_type(method)            _is_method_type(method)
69 #define is_Union_type(uni)                _is_union_type(uni)
70 #define is_Array_type(array)              _is_array_type(array)
71 #define is_Enumeration_type(enumeration)  _is_enumeration_type(enumeration)
72 #define is_Pointer_type(pointer)          _is_pointer_type(pointer)
73 #define is_Primitive_type(primitive)      _is_primitive_type(primitive)
74 #define is_atomic_type(tp)                _is_atomic_type(tp)
75 #define get_method_n_params(method)       _get_method_n_params(method)
76 #define get_method_n_ress(method)         _get_method_n_ress(method)
77 #define get_method_additional_properties(method)        _get_method_additional_properties(method)
78 #define set_method_additional_properties(method, mask)  _set_method_additional_properties(method, mask)
79 #define add_method_additional_properties(method, flag)  _add_method_additional_properties(method, flag)
80 #define get_method_calling_convention(method)           _get_method_calling_convention(method)
81 #define set_method_calling_convention(method, cc_mask)  _set_method_calling_convention(method, cc_mask)
82
83 /** Class flags. */
84 enum class_flags {
85         cf_none            = 0,  /**< No flags. */
86         cf_final_class     = 1,  /**< Set if a class is an final class */
87         cf_interface_class = 2,  /**< Set if a class is an "interface" */
88         cf_absctract_class = 4,  /**< Set if a class is "abstract" */
89 };
90
91 /** Class type attributes. */
92 typedef struct {
93         ir_entity  **members;           /**< Array containing the fields and methods of this class. */
94         ir_type **subtypes;          /**< Array containing the direct subtypes. */
95         ir_type **supertypes;        /**< Array containing the direct supertypes */
96         ir_peculiarity peculiarity;  /**< The peculiarity of this class. */
97         ir_entity *type_info;        /**< An ir_entity representing this class, used for type info. */
98         int      dfn;                /**< A number that can be used for 'instanceof' operator. */
99         unsigned vtable_size;        /**< The size of the vtable for this class. */
100         unsigned clss_flags;         /**< Additional class flags. */
101 } cls_attr;
102
103 /** Struct type attributes. */
104 typedef struct {
105         ir_entity **members; /**< Fields of this struct. No method entities allowed. */
106 } stc_attr;
107
108 /** A (type, ir_entity) pair. */
109 typedef struct {
110         ir_type   *tp;      /**< A type. */
111         ir_entity *ent;     /**< An ir_entity. */
112 } tp_ent_pair;
113
114 /** Method type attributes. */
115 typedef struct {
116         size_t       n_params;          /**< Number of parameters. */
117         tp_ent_pair *params;            /**< Array of parameter type/value entities pairs. */
118         size_t       n_res;             /**< Number of results. */
119         tp_ent_pair *res_type;          /**< Array of result type/value ir_entity pairs. */
120         ir_variadicity variadicity;     /**< The variadicity of the method. */
121         mtp_additional_properties properties; /**< Set of additional method properties. */
122         unsigned irg_calling_conv;      /**< A set of calling convention flags. */
123 } mtd_attr;
124
125 /** Union type attributes. */
126 typedef struct {
127         ir_entity **members;    /**< Fields of this union. No method entities allowed. */
128 } uni_attr;
129
130 /** Array type attributes. */
131 typedef struct {
132         size_t  n_dimensions;   /**< Number of array dimensions.  */
133         ir_node **lower_bound;  /**< Lower bounds of dimensions.  Usually all 0. */
134         ir_node **upper_bound;  /**< Upper bounds or dimensions. */
135         size_t  *order;         /**< Ordering of dimensions. */
136         ir_type *element_type;  /**< The type of the array elements. */
137         ir_entity *element_ent; /**< entity for the array elements, to be used for
138                                      element selection with a Sel node. */
139 } arr_attr;
140
141 /** An enumerator constant. */
142 struct ir_enum_const {
143         ir_tarval  *value;  /**< The constants that represents this enumerator identifier. */
144         ident   *nameid;    /**< The name of the enumerator identifier. */
145         ir_type *owner;     /**< owner type of this enumerator constant. */
146 };
147
148 /** Enum type attributes. */
149 typedef struct {
150         ir_enum_const *enumer;   /**< Contains all enumerator constants that represent a member
151                                       of the enum -- enumerators. */
152 } enm_attr;
153
154 /** Pointer type attributes. */
155 typedef struct {
156         ir_type *points_to;  /**< The type of the ir_entity the pointer points to. */
157 } ptr_attr;
158
159 /** Primitive type attributes. */
160 typedef struct {
161         ir_type *base_type;  /**< For bitfield types: The base primitive type, NULL else. */
162 } pri_attr;
163
164 /** General type attributes. */
165 typedef union {
166         cls_attr ca;      /**< Attributes of a class type */
167         stc_attr sa;      /**< Attributes of a struct type */
168         mtd_attr ma;      /**< Attributes of a method type */
169         uni_attr ua;      /**< Attributes of an union type */
170         arr_attr aa;      /**< Attributes of an array type */
171         enm_attr ea;      /**< Attributes of an enumeration type */
172         ptr_attr pa;      /**< Attributes of a pointer type */
173         pri_attr ba;      /**< Attributes of a primitive bitfield type */
174 } tp_attr;
175
176 /** Additional type flags. */
177 enum type_flags {
178         tf_none             = 0, /**< No flags. */
179         tf_lowered_type     = 1U << 0, /**< Set if this is a lowered type. */
180         tf_layout_fixed     = 1U << 1, /**< Set if the layout of a type is fixed */
181
182         tf_frame_type       = 1U << 2, /**< Set if this is a frame type. */
183         tf_segment          = 1U << 3, /**< type represents a linker segment */
184         tf_global_type      = 1U << 4, /**< Set only for the global type */
185         tf_tls_type         = 1U << 5, /**< Set only for the tls type */
186         tf_constructors     = 1U << 6, /**< Set only for the constructors segment type */
187         tf_destructors      = 1U << 7, /**< Set only for the destructors segment type */
188         tf_variable_size    = 1U << 8, /**< compound or array type may have variable size last element */
189 };
190 ENUM_BITSET(type_flags)
191
192 /**
193  *  An abstract data type to represent types.
194  *
195  *  This is the abstract data type with which any type known in the
196  *  compiled program can be represented.  This includes types specified
197  *  in the program as well as types defined by the language.  In the
198  *  view of the intermediate representation there is no difference
199  *  between these types.
200  *
201  *  There exist several kinds of types, arranged by the structure of
202  *  the type.  These are distinguished by a type opcode.
203  *  A type is described by a set of attributes.  Some of these attributes
204  *  are common to all types, others depend on the kind of the type.
205  */
206 struct ir_type {
207         firm_kind kind;          /**< the firm kind, must be k_type */
208         const tp_op *type_op;    /**< the type operation of the type */
209         ident *name;             /**< The name of the type */
210         ir_visibility visibility;/**< Visibility of entities of this type. */
211         unsigned flags;          /**< Type flags, a bitmask of enum type_flags. */
212         unsigned size;           /**< Size of an ir_entity of this type. This is
213                                       determined when fixing the layout of this
214                                       class.  Size must be given in bytes. */
215         unsigned align;          /**< Alignment of an ir_entity of this type. This
216                                       should be set according to the source
217                                       language needs. If not set, it's calculated
218                                       automatically by get_type_alignment().
219                                       Alignment must be given in bytes. */
220         ir_mode *mode;           /**< The mode for atomic types */
221         ir_visited_t visit;      /**< visited counter for walks of the type information */
222         void *link;              /**< holds temporary data - like in irnode_t.h */
223         type_dbg_info *dbi;      /**< A pointer to information for debug support. */
224         ir_type *higher_type;    /**< link to highlevel type in case of lowered
225                                       types */
226
227 #ifdef DEBUG_libfirm
228         long nr;                 /**< An unique node number for each node to make
229                                       output readable. */
230 #endif
231         tp_attr attr;            /**< Type kind specific fields. This must be the
232                                       last entry in this struct!  Varying size! */
233 };
234
235 void free_type_entities(ir_type *tp);
236
237 void free_class_entities      (ir_type *clss);
238 void free_struct_entities     (ir_type *strct);
239 void free_method_entities     (ir_type *method);
240 void free_union_entities      (ir_type *uni);
241 void free_array_entities      (ir_type *array);
242 void free_enumeration_entities(ir_type *enumeration);
243 void free_pointer_entities    (ir_type *pointer);
244
245 void free_array_automatic_entities(ir_type *array);
246
247 void free_class_attrs      (ir_type *clss);
248 void free_struct_attrs     (ir_type *strct);
249 void free_method_attrs     (ir_type *method);
250 void free_union_attrs      (ir_type *uni);
251 void free_array_attrs      (ir_type *array);
252 void free_enumeration_attrs(ir_type *enumeration);
253 void free_pointer_attrs    (ir_type *pointer);
254
255 void set_class_mode(ir_type *tp, ir_mode *mode);
256 void set_struct_mode(ir_type *tp, ir_mode *mode);
257 void set_pointer_mode(ir_type *tp, ir_mode *mode);
258 void set_primitive_mode(ir_type *tp, ir_mode *mode);
259 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
260
261 void set_class_size(ir_type *tp, unsigned bytes);
262 void set_struct_size(ir_type *tp, unsigned bytes);
263 void set_union_size(ir_type *tp, unsigned bytes);
264 void set_array_size(ir_type *tp, unsigned bytes);
265 void set_default_size(ir_type *tp, unsigned bytes);
266
267 /** Set and get a class' dfn --
268  * This is an undocumented field, subject to change! */
269 void set_class_dfn(ir_type *clss, int dfn);
270 int  get_class_dfn(const ir_type *clss);
271
272 void add_compound_member(ir_type *compound, ir_entity *entity);
273
274 /** Initialize the type module. */
275 void ir_init_type(ir_prog *irp);
276
277 /** free internal datastructures of type module */
278 void ir_finish_type(ir_prog *irp);
279
280 /** Clone an existing method type.
281  *
282  * @param tp      the method type to clone.
283  * @param prefix  if non-null, will be the prefix for the name of
284  *                the cloned type
285  *
286  * @return the cloned method type.
287  */
288 ir_type *clone_type_method(ir_type *tp);
289
290 extern ir_visited_t firm_type_visited;
291
292 static inline void _set_master_type_visited(ir_visited_t val)
293 {
294         firm_type_visited = val;
295 }
296
297 static inline ir_visited_t _get_master_type_visited(void)
298 {
299         return firm_type_visited;
300 }
301
302 static inline void _inc_master_type_visited(void)
303 {
304         ++firm_type_visited;
305 }
306
307 static inline int is_lowered_type(const ir_type *tp)
308 {
309         return tp->flags & tf_lowered_type;
310 }
311
312 static inline ir_type *get_higher_type(const ir_type *tp)
313 {
314         return tp->higher_type;
315 }
316
317 static inline void set_higher_type(ir_type *tp, ir_type *higher_type)
318 {
319         tp->flags |= tf_lowered_type;
320         tp->higher_type = higher_type;
321 }
322
323 static inline void *_get_type_link(const ir_type *tp)
324 {
325         assert(tp->kind == k_type);
326         return(tp -> link);
327 }
328
329 static inline void _set_type_link(ir_type *tp, void *l)
330 {
331         assert(tp->kind == k_type);
332         tp -> link = l;
333 }
334
335 static inline const tp_op *_get_type_tpop(const ir_type *tp)
336 {
337         assert(tp->kind == k_type);
338         return tp->type_op;
339 }
340
341 static inline ident *_get_type_tpop_nameid(const ir_type *tp)
342 {
343         assert(tp->kind == k_type);
344         return get_tpop_ident(tp->type_op);
345 }
346
347 static inline tp_opcode _get_type_tpop_code(const ir_type *tp)
348 {
349         assert(tp->kind == k_type);
350         return get_tpop_code(tp->type_op);
351 }
352
353 static inline ir_mode *_get_type_mode(const ir_type *tp)
354 {
355         assert(tp->kind == k_type);
356         return tp->mode;
357 }
358
359 static inline unsigned _get_type_size_bytes(const ir_type *tp)
360 {
361         assert(tp->kind == k_type);
362         return tp->size;
363 }
364
365 static inline ir_type_state _get_type_state(const ir_type *tp)
366 {
367         assert(tp->kind == k_type);
368         return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
369 }
370
371 static inline ir_visited_t _get_type_visited(const ir_type *tp)
372 {
373         assert(tp->kind == k_type);
374         return tp->visit;
375 }
376
377 static inline void _set_type_visited(ir_type *tp, ir_visited_t num)
378 {
379         assert(tp->kind == k_type);
380         tp->visit = num;
381 }
382
383 static inline void _mark_type_visited(ir_type *tp)
384 {
385         assert(tp->kind == k_type);
386         assert(tp->visit < firm_type_visited);
387         tp->visit = firm_type_visited;
388 }
389
390 static inline int _type_visited(const ir_type *tp)
391 {
392         assert(tp->kind == k_type);
393         return tp->visit >= firm_type_visited;
394 }
395
396 static inline int _type_not_visited(const ir_type *tp)
397 {
398         assert(tp->kind == k_type);
399         return tp->visit  < firm_type_visited;
400 }
401
402 static inline type_dbg_info *_get_type_dbg_info(const ir_type *tp)
403 {
404         return tp->dbi;
405 }
406
407 static inline void _set_type_dbg_info(ir_type *tp, type_dbg_info *db)
408 {
409         tp->dbi = db;
410 }
411
412 static inline int _is_type(const void *thing)
413 {
414         return get_kind(thing) == k_type;
415 }
416
417 static inline int _is_class_type(const ir_type *clss)
418 {
419         return clss->type_op == type_class;
420 }
421
422 static inline size_t _get_class_n_members(const ir_type *clss)
423 {
424         assert(clss->type_op == type_class);
425         return ARR_LEN(clss->attr.ca.members);
426 }
427
428 static inline ir_entity *_get_class_member(const ir_type *clss, size_t pos)
429 {
430         assert(clss->type_op == type_class);
431         assert(pos < _get_class_n_members(clss));
432         return clss->attr.ca.members[pos];
433 }
434
435 static inline unsigned _get_class_vtable_size(const ir_type *clss)
436 {
437         assert(clss->type_op == type_class);
438         return clss->attr.ca.vtable_size;
439 }
440
441 static inline void _set_class_vtable_size(ir_type *clss, unsigned vtable_size)
442 {
443         assert(clss->type_op == type_class);
444         clss->attr.ca.vtable_size = vtable_size;
445 }
446
447 static inline int _is_class_final(const ir_type *clss)
448 {
449         assert(clss->type_op == type_class);
450         return clss->attr.ca.clss_flags & cf_final_class;
451 }
452
453 static inline void _set_class_final(ir_type *clss, int final)
454 {
455         assert(clss->type_op == type_class);
456         if (final)
457                 clss->attr.ca.clss_flags |= cf_final_class;
458         else
459                 clss->attr.ca.clss_flags &= ~cf_final_class;
460 }
461
462 static inline int _is_class_interface(const ir_type *clss)
463 {
464         assert(clss->type_op == type_class);
465         return clss->attr.ca.clss_flags & cf_interface_class;
466 }
467
468 static inline void _set_class_interface(ir_type *clss, int final)
469 {
470         assert(clss->type_op == type_class);
471         if (final)
472                 clss->attr.ca.clss_flags |= cf_interface_class;
473         else
474                 clss->attr.ca.clss_flags &= ~cf_interface_class;
475 }
476
477 static inline int _is_class_abstract(const ir_type *clss)
478 {
479         assert(clss->type_op == type_class);
480         return clss->attr.ca.clss_flags & cf_absctract_class;
481 }
482
483 static inline void _set_class_abstract(ir_type *clss, int final)
484 {
485         assert(clss->type_op == type_class);
486         if (final)
487                 clss->attr.ca.clss_flags |= cf_absctract_class;
488         else
489                 clss->attr.ca.clss_flags &= ~cf_absctract_class;
490 }
491
492 static inline int _is_struct_type(const ir_type *strct)
493 {
494         return (strct->type_op == type_struct);
495 }
496
497 static inline int _is_method_type(const ir_type *method)
498 {
499         return (method->type_op == type_method);
500 }
501
502 static inline int _is_union_type(const ir_type *uni)
503 {
504         return (uni->type_op == type_union);
505 }
506
507 static inline int _is_array_type(const ir_type *array)
508 {
509         return (array->type_op == type_array);
510 }
511
512 static inline int _is_enumeration_type(const ir_type *enumeration)
513 {
514         return (enumeration->type_op == type_enumeration);
515 }
516
517 static inline int _is_pointer_type(const ir_type *pointer)
518 {
519         return (pointer->type_op == type_pointer);
520 }
521
522 /** Returns true if a type is a primitive type. */
523 static inline int _is_primitive_type(const ir_type *primitive)
524 {
525         assert(primitive->kind == k_type);
526         return (primitive->type_op == type_primitive);
527 }
528
529 static inline int _is_atomic_type(const ir_type *tp)
530 {
531         assert(tp->kind == k_type);
532         return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
533                 _is_enumeration_type(tp));
534 }
535
536 static inline size_t _get_method_n_params(const ir_type *method)
537 {
538         assert(method->type_op == type_method);
539         return method->attr.ma.n_params;
540 }
541
542 static inline size_t _get_method_n_ress(const ir_type *method)
543 {
544         assert(method->type_op == type_method);
545         return method->attr.ma.n_res;
546 }
547
548 static inline mtp_additional_properties _get_method_additional_properties(const ir_type *method)
549 {
550         assert(method->type_op == type_method);
551         return method->attr.ma.properties;
552 }
553
554 static inline void _set_method_additional_properties(ir_type *method, mtp_additional_properties properties)
555 {
556         assert(method->type_op == type_method);
557         method->attr.ma.properties = properties;
558 }
559
560 static inline void _add_method_additional_properties(ir_type *method, mtp_additional_properties properties)
561 {
562         assert(method->type_op == type_method);
563         method->attr.ma.properties |= properties;
564 }
565
566 static inline unsigned _get_method_calling_convention(const ir_type *method)
567 {
568         assert(method->type_op == type_method);
569         return method->attr.ma.irg_calling_conv;
570 }
571
572 static inline void _set_method_calling_convention(ir_type *method, unsigned cc_mask)
573 {
574         assert(method->type_op == type_method);
575         method->attr.ma.irg_calling_conv = cc_mask;
576 }
577
578 #endif /* FIRM_TR_TYPE_T_H */