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