Fixed some warning about unused variables.
[libfirm] / ir / tr / type.c
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    type.c
22  * @brief   Representation of types.
23  * @author  Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  * @brief
26  *
27  *  Implementation of the datastructure to hold
28  *  type information.
29  *
30  *  This module supplies a datastructure to represent all types
31  *  known in the compiled program.  This includes types specified
32  *  in the program as well as types defined by the language.  In the
33  *  view of the intermediate representation there is no difference
34  *  between these types.
35  *
36  *  There exist several kinds of types, arranged by the structure of
37  *  the type.  A type is described by a set of attributes.  Some of
38  *  these attributes are common to all types, others depend on the
39  *  kind of the type.
40  *
41  *  Types are different from the modes defined in irmode:  Types are
42  *  on the level of the programming language, modes at the level of
43  *  the target processor.
44  */
45 #include "config.h"
46
47 #include <string.h>
48 #include <stdlib.h>
49 #include <stddef.h>
50
51 #include "type_t.h"
52
53 #include "xmalloc.h"
54 #include "irprog_t.h"
55 #include "ircons.h"
56 #include "tpop_t.h"
57 #include "tv_t.h"
58 #include "irhooks.h"
59 #include "irtools.h"
60 #include "entity_t.h"
61 #include "error.h"
62 #include "dbginfo.h"
63
64 #include "array.h"
65
66 ir_type *firm_none_type;
67 ir_type *get_none_type(void)
68 {
69         return firm_none_type;
70 }
71
72 ir_type *firm_code_type;
73 ir_type *get_code_type(void)
74 {
75         return firm_code_type;
76 }
77
78 ir_type *firm_unknown_type;
79 ir_type *get_unknown_type(void)
80 {
81         return firm_unknown_type;
82 }
83
84 void ir_init_type(void)
85 {
86         /* construct none and unknown type. */
87         firm_none_type = new_type(tpop_none, mode_BAD, NULL);
88         set_type_size_bytes(firm_none_type, 0);
89         set_type_state (firm_none_type, layout_fixed);
90         remove_irp_type(firm_none_type);
91
92         firm_code_type = new_type(tpop_code, mode_ANY, NULL);
93         set_type_state(firm_code_type, layout_fixed);
94         remove_irp_type(firm_code_type);
95
96         firm_unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
97         set_type_size_bytes(firm_unknown_type, 0);
98         set_type_state (firm_unknown_type, layout_fixed);
99         remove_irp_type(firm_unknown_type);
100 }
101
102 void ir_finish_type(void)
103 {
104         if (firm_none_type != NULL) {
105                 free_type(firm_none_type);
106                 firm_none_type = NULL;
107         }
108         if (firm_code_type != NULL) {
109                 free_type(firm_code_type);
110                 firm_code_type = NULL;
111         }
112         if (firm_unknown_type != NULL) {
113                 free_type(firm_unknown_type);
114                 firm_unknown_type = NULL;
115         }
116 }
117
118 /** the global type visited flag */
119 ir_visited_t firm_type_visited;
120
121 void (set_master_type_visited)(ir_visited_t val)
122 {
123         _set_master_type_visited(val);
124 }
125
126 ir_visited_t (get_master_type_visited)(void)
127 {
128         return _get_master_type_visited();
129 }
130
131 void (inc_master_type_visited)(void)
132 {
133         _inc_master_type_visited();
134 }
135
136 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
137 {
138         ir_type *res;
139         size_t node_size;
140
141         node_size = offsetof(ir_type, attr) +  type_op->attr_size;
142         res = (ir_type*)xmalloc(node_size);
143         memset(res, 0, node_size);
144
145         res->kind       = k_type;
146         res->type_op    = type_op;
147         res->mode       = mode;
148         res->visibility = ir_visibility_external;
149         res->flags      = tf_none;
150         res->size       = 0;
151         res->align      = 0;
152         res->visit      = 0;
153         res->link       = NULL;
154         res->dbi        = db;
155         res->assoc_type = NULL;
156 #ifdef DEBUG_libfirm
157         res->nr         = get_irp_new_node_nr();
158 #endif /* defined DEBUG_libfirm */
159
160         add_irp_type(res);   /* Remember the new type global. */
161
162         return res;
163 }
164
165 void free_type(ir_type *tp)
166 {
167         const tp_op *op = get_type_tpop(tp);
168
169         if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
170                         || (get_type_tpop(tp) == tpop_code))
171                 return;
172         /* Remove from list of all types */
173         remove_irp_type(tp);
174         /* Free the attributes of the type. */
175         free_type_attrs(tp);
176         /* Free entities automatically allocated with the ir_type */
177         if (op->ops.free_auto_entities)
178                 op->ops.free_auto_entities(tp);
179         /* And now the type itself... */
180         tp->kind = k_BAD;
181         free(tp);
182 }
183
184 void free_type_entities(ir_type *tp)
185 {
186         const tp_op *tpop = get_type_tpop(tp);
187
188         if (tpop->ops.free_entities)
189                 tpop->ops.free_entities(tp);
190 }
191
192 void free_type_attrs(ir_type *tp)
193 {
194         const tp_op *tpop = get_type_tpop(tp);
195
196         if (tpop->ops.free_attrs)
197                 tpop->ops.free_attrs(tp);
198 }
199
200 void *(get_type_link)(const ir_type *tp)
201 {
202         return _get_type_link(tp);
203 }
204
205 void (set_type_link)(ir_type *tp, void *l)
206 {
207         _set_type_link(tp, l);
208 }
209
210 const tp_op *(get_type_tpop)(const ir_type *tp)
211 {
212         return _get_type_tpop(tp);
213 }
214
215 ident *(get_type_tpop_nameid)(const ir_type *tp)
216 {
217         return _get_type_tpop_nameid(tp);
218 }
219
220 const char* get_type_tpop_name(const ir_type *tp)
221 {
222         assert(tp && tp->kind == k_type);
223         return get_id_str(tp->type_op->name);
224 }
225
226 tp_opcode (get_type_tpop_code)(const ir_type *tp)
227 {
228         return _get_type_tpop_code(tp);
229 }
230
231 ir_mode *(get_type_mode)(const ir_type *tp)
232 {
233         return _get_type_mode(tp);
234 }
235
236 void set_type_mode(ir_type *tp, ir_mode *mode)
237 {
238         const tp_op *tpop = get_type_tpop(tp);
239
240         if (tpop->ops.set_type_mode)
241                 tpop->ops.set_type_mode(tp, mode);
242         else
243                 assert(0 && "setting a mode is NOT allowed for this type");
244 }
245
246 /* Outputs a unique number for this node */
247 long get_type_nr(const ir_type *tp)
248 {
249         assert(tp);
250 #ifdef DEBUG_libfirm
251         return tp->nr;
252 #else
253         return (long)PTR_TO_INT(tp);
254 #endif
255 }
256
257 unsigned (get_type_size_bytes)(const ir_type *tp)
258 {
259         return _get_type_size_bytes(tp);
260 }
261
262 ir_visibility get_type_visibility(const ir_type *tp)
263 {
264         assert(is_type(tp));
265         return tp->visibility;
266 }
267
268 void set_type_visibility(ir_type *tp, ir_visibility v)
269 {
270         assert(is_type(tp));
271         tp->visibility = v;
272 }
273
274 void set_type_size_bytes(ir_type *tp, unsigned size)
275 {
276         const tp_op *tpop = get_type_tpop(tp);
277
278         if (tpop->ops.set_type_size)
279                 tpop->ops.set_type_size(tp, size);
280         else
281                 assert(0 && "Cannot set size for this type");
282 }
283
284 unsigned get_type_alignment_bytes(ir_type *tp)
285 {
286         unsigned align = 1;
287
288         if (tp->align > 0)
289                 return tp->align;
290
291         /* alignment NOT set calculate it "on demand" */
292         if (tp->mode)
293                 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
294         else if (is_Array_type(tp))
295                 align = get_type_alignment_bytes(get_array_element_type(tp));
296         else if (is_compound_type(tp)) {
297                 size_t i, n = get_compound_n_members(tp);
298
299                 align = 0;
300                 for (i = 0; i < n; ++i) {
301                         ir_type  *t = get_entity_type(get_compound_member(tp, i));
302                         unsigned a  = get_type_alignment_bytes(t);
303
304                         if (a > align)
305                                 align = a;
306                 }
307         } else if (is_Method_type(tp)) {
308                 align = 0;
309         }
310
311         /* write back */
312         tp->align = align;
313
314         return align;
315 }
316
317 void set_type_alignment_bytes(ir_type *tp, unsigned align)
318 {
319         assert(tp && tp->kind == k_type);
320         /* Methods don't have an alignment. */
321         if (tp->type_op != type_method) {
322                 tp->align = align;
323         }
324 }
325
326 const char *get_type_state_name(ir_type_state s)
327 {
328 #define X(a)    case a: return #a;
329         switch (s) {
330                 X(layout_undefined);
331                 X(layout_fixed);
332         }
333         return "<unknown>";
334 #undef X
335 }
336
337 ir_type_state (get_type_state)(const ir_type *tp)
338 {
339         return _get_type_state(tp);
340 }
341
342 void set_type_state(ir_type *tp, ir_type_state state)
343 {
344         assert(tp && tp->kind == k_type);
345
346         if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
347                 (tp->type_op == type_method))
348                 return;
349
350         /* Just a correctness check: */
351         if (state == layout_fixed) {
352                 size_t i;
353                 switch (get_type_tpop_code(tp)) {
354                 case tpo_class:
355                         if (tp != get_glob_type()) {
356                                 size_t n_mem = get_class_n_members(tp);
357                                 for (i = 0; i < n_mem; i++) {
358                                         ir_entity *entity = get_class_member(tp, i);
359                                         if (is_Method_type(get_entity_type(entity)))
360                                                 continue;
361                                         assert(get_entity_offset(entity) > -1);
362                                 }
363                         }
364                         break;
365                 case tpo_struct:
366                         for (i = 0; i < get_struct_n_members(tp); i++) {
367                                 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
368                         }
369                         break;
370                 case tpo_union:
371                         break;
372                 case tpo_array:
373                         break;
374                 case tpo_enumeration: {
375 #ifndef NDEBUG
376                         size_t n_enums = get_enumeration_n_enums(tp);
377                         assert(get_type_mode(tp) != NULL);
378                         for (i = 0; i < n_enums; ++i) {
379                                 ir_enum_const *ec = get_enumeration_const(tp, i);
380                                 ir_tarval     *tv = get_enumeration_value(ec);
381                                 assert(tv != NULL && tv != tarval_bad);
382                         }
383 #endif
384                         break;
385                 }
386                 default: break;
387                 }
388         }
389         if (state == layout_fixed)
390                 tp->flags |= tf_layout_fixed;
391         else
392                 tp->flags &= ~tf_layout_fixed;
393 }
394
395 ir_visited_t (get_type_visited)(const ir_type *tp)
396 {
397         return _get_type_visited(tp);
398 }
399
400 void (set_type_visited)(ir_type *tp, ir_visited_t num)
401 {
402         _set_type_visited(tp, num);
403 }
404
405 void (mark_type_visited)(ir_type *tp)
406 {
407         _mark_type_visited(tp);
408 }
409
410 int (type_visited)(const ir_type *tp)
411 {
412         return _type_visited(tp);
413 }
414
415 int (type_not_visited)(const ir_type *tp)
416 {
417         return _type_not_visited(tp);
418 }
419
420 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
421 {
422         return _get_type_dbg_info(tp);
423 }
424
425 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
426 {
427         _set_type_dbg_info(tp, db);
428 }
429
430 int (is_type)(const void *thing)
431 {
432         return _is_type(thing);
433 }
434
435 /* Checks whether two types are structural equal.*/
436 int equal_type(ir_type *typ1, ir_type *typ2)
437 {
438         ir_entity **m;
439         ir_type **t;
440         size_t i;
441         size_t j;
442
443         if (typ1 == typ2) return 1;
444
445         if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
446             typ1->name != typ2->name ||
447             (get_type_mode(typ1) != get_type_mode(typ2)) ||
448             (get_type_state(typ1) != get_type_state(typ2)))
449                 return 0;
450         if ((get_type_state(typ1) == layout_fixed) &&
451                 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
452                 return 0;
453
454         switch (get_type_tpop_code(typ1)) {
455         case tpo_class:
456                 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
457                 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
458                 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
459                 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
460                 /** Compare the members **/
461                 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
462                 /* First sort the members of typ2 */
463                 for (i = 0; i < get_class_n_members(typ1); i++) {
464                         ir_entity *e1 = get_class_member(typ1, i);
465                         for (j = 0; j < get_class_n_members(typ2); j++) {
466                                 ir_entity *e2 = get_class_member(typ2, j);
467                                 if (get_entity_name(e1) == get_entity_name(e2))
468                                         m[i] = e2;
469                         }
470                 }
471                 for (i = 0; i < get_class_n_members(typ1); i++) {
472                         if (!m[i] || get_class_member(typ1, i) != m[i])
473                                 return 0;
474                 }
475                 /** Compare the supertypes **/
476                 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
477                 /* First sort the supertypes of typ2 */
478                 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
479                         ir_type *t1 = get_class_supertype(typ1, i);
480                         for (j = 0; j < get_class_n_supertypes(typ2); j++) {
481                                 ir_type *t2 = get_class_supertype(typ2, j);
482                                 if (t2->name == t1->name)
483                                         t[i] = t2;
484                         }
485                 }
486                 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
487                         if (!t[i]  ||  /* Found no counterpart */
488                                 get_class_supertype(typ1, i) != t[i])
489                                 return 0;
490                 }
491                 break;
492
493         case tpo_struct:
494                 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
495                 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
496                 /* First sort the members of lt */
497                 for (i = 0; i < get_struct_n_members(typ1); i++) {
498                         ir_entity *e1 = get_struct_member(typ1, i);
499                         for (j = 0; j < get_struct_n_members(typ2); j++) {
500                                 ir_entity *e2 = get_struct_member(typ2, j);
501                                 if (get_entity_name(e1) == get_entity_name(e2))
502                                         m[i] = e2;
503                         }
504                 }
505                 for (i = 0; i < get_struct_n_members(typ1); i++) {
506                         if (!m[i] || get_struct_member(typ1, i) != m[i])
507                                 return 0;
508                 }
509                 break;
510
511         case tpo_method: {
512                 size_t n_param1;
513                 size_t n_param2;
514
515                 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
516                 if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return 0;
517                 if (get_method_calling_convention(typ1) !=
518                     get_method_calling_convention(typ2)) return 0;
519
520                 n_param1 = get_method_n_params(typ1);
521                 n_param2 = get_method_n_params(typ2);
522
523                 if (n_param1 != n_param2) return 0;
524
525                 for (i = 0; i < n_param1; i++) {
526                         if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
527                                 return 0;
528                 }
529                 for (i = 0; i < get_method_n_ress(typ1); i++) {
530                         if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
531                                 return 0;
532                 }
533         } break;
534
535         case tpo_union:
536                 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
537                 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
538                 /* First sort the members of lt */
539                 for (i = 0; i < get_union_n_members(typ1); i++) {
540                         ir_entity *e1 = get_union_member(typ1, i);
541                         for (j = 0; j < get_union_n_members(typ2); j++) {
542                                 ir_entity *e2 = get_union_member(typ2, j);
543                                 if (get_entity_name(e1) == get_entity_name(e2))
544                                         m[i] = e2;
545                         }
546                 }
547                 for (i = 0; i < get_union_n_members(typ1); i++) {
548                         if (!m[i] || get_union_member(typ1, i) != m[i])
549                                 return 0;
550                 }
551                 break;
552
553         case tpo_array:
554                 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
555                         return 0;
556                 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
557                         return 0;
558                 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
559                         if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
560                                 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
561                                 return 0;
562                         if (get_array_order(typ1, i) != get_array_order(typ2, i))
563                                 assert(0 && "type compare with different dimension orders not implemented");
564                 }
565                 break;
566
567         case tpo_enumeration:
568                 assert(0 && "enumerations not implemented");
569                 break;
570
571         case tpo_pointer:
572                 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
573                         return 0;
574                 break;
575
576         case tpo_primitive:
577                 break;
578
579         default: break;
580         }
581         return 1;
582 }
583
584 int smaller_type(ir_type *st, ir_type *lt)
585 {
586         ir_entity **m;
587         size_t i;
588         size_t j;
589         size_t n_st_members;
590
591         if (st == lt) return 1;
592
593         if (get_type_tpop_code(st) != get_type_tpop_code(lt))
594                 return 0;
595
596         switch (get_type_tpop_code(st)) {
597         case tpo_class:
598                 return is_SubClass_of(st, lt);
599
600         case tpo_struct:
601                 n_st_members = get_struct_n_members(st);
602                 if (n_st_members != get_struct_n_members(lt))
603                         return 0;
604
605                 m = ALLOCANZ(ir_entity*, n_st_members);
606                 /* First sort the members of lt */
607                 for (i = 0; i < n_st_members; ++i) {
608                         ir_entity *se = get_struct_member(st, i);
609                         size_t n = get_struct_n_members(lt);
610                         for (j = 0; j < n; ++j) {
611                                 ir_entity *le = get_struct_member(lt, j);
612                                 if (get_entity_name(le) == get_entity_name(se))
613                                         m[i] = le;
614                         }
615                 }
616                 for (i = 0; i < n_st_members; i++) {
617                         if (!m[i]  ||  /* Found no counterpart */
618                             !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
619                                 return 0;
620                 }
621                 break;
622
623         case tpo_method: {
624                 size_t n_param1, n_param2;
625
626                 /** FIXME: is this still 1? */
627                 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
628                 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
629                 if (get_method_calling_convention(st) !=
630                     get_method_calling_convention(lt)) return 0;
631
632                 n_param1 = get_method_n_params(st);
633                 n_param2 = get_method_n_params(lt);
634
635                 if (n_param1 != n_param2) return 0;
636
637                 for (i = 0; i < get_method_n_params(st); i++) {
638                         if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
639                                 return 0;
640                 }
641                 for (i = 0; i < get_method_n_ress(st); i++) {
642                         if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
643                                 return 0;
644                 }
645         } break;
646
647         case tpo_union:
648                 n_st_members = get_union_n_members(st);
649                 if (n_st_members != get_union_n_members(lt)) return 0;
650                 m = ALLOCANZ(ir_entity*, n_st_members);
651                 /* First sort the members of lt */
652                 for (i = 0; i < n_st_members; ++i) {
653                         ir_entity *se = get_union_member(st, i);
654                         size_t n = get_union_n_members(lt);
655                         for (j = 0; j < n; ++j) {
656                                 ir_entity *le = get_union_member(lt, j);
657                                 if (get_entity_name(le) == get_entity_name(se))
658                                         m[i] = le;
659                         }
660                 }
661                 for (i = 0; i < n_st_members; ++i) {
662                         if (!m[i]  ||  /* Found no counterpart */
663                                 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
664                                 return 0;
665                 }
666                 break;
667
668         case tpo_array: {
669                 ir_type *set, *let;  /* small/large elt. ir_type */
670                 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
671                         return 0;
672                 set = get_array_element_type(st);
673                 let = get_array_element_type(lt);
674                 if (set != let) {
675                         /* If the element types are different, set must be convertible
676                            to let, and they must have the same size so that address
677                            computations work out.  To have a size the layout must
678                            be fixed. */
679                         if ((get_type_state(set) != layout_fixed) ||
680                             (get_type_state(let) != layout_fixed))
681                                 return 0;
682                         if (!smaller_type(set, let) ||
683                             get_type_size_bytes(set) != get_type_size_bytes(let))
684                                 return 0;
685                 }
686                 for (i = 0; i < get_array_n_dimensions(st); i++) {
687                         if (get_array_lower_bound(lt, i))
688                                 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
689                                         return 0;
690                                 if (get_array_upper_bound(lt, i))
691                                         if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
692                                                 return 0;
693                 }
694         } break;
695
696         case tpo_enumeration:
697                 assert(0 && "enumerations not implemented");
698                 break;
699
700         case tpo_pointer:
701                 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
702                         return 0;
703                 break;
704
705         case tpo_primitive:
706                 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
707                         return 0;
708                 break;
709
710         default: break;
711         }
712         return 1;
713 }
714
715
716 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
717 {
718         ir_type *res;
719
720         res = new_type(type_class, NULL, db);
721         res->name = name;
722
723         res->attr.ca.members     = NEW_ARR_F (ir_entity *, 0);
724         res->attr.ca.subtypes    = NEW_ARR_F (ir_type *, 0);
725         res->attr.ca.supertypes  = NEW_ARR_F (ir_type *, 0);
726         res->attr.ca.peculiarity = peculiarity_existent;
727         res->attr.ca.type_info   = NULL;
728         res->attr.ca.vtable_size = 0;
729         res->attr.ca.clss_flags  = cf_none;
730         res->attr.ca.dfn         = 0;
731         hook_new_type(res);
732         return res;
733 }
734
735 ir_type *new_type_class(ident *name)
736 {
737         return new_d_type_class(name, NULL);
738 }
739
740 void free_class_entities(ir_type *clss)
741 {
742         size_t i;
743         assert(clss && (clss->type_op == type_class));
744         /* we must iterate backward here */
745         for (i = get_class_n_members(clss); i > 0;)
746                 free_entity(get_class_member(clss, --i));
747         /* do NOT free the type info here. It belongs to another class */
748 }
749
750 void free_class_attrs(ir_type *clss)
751 {
752         assert(clss && (clss->type_op == type_class));
753         DEL_ARR_F(clss->attr.ca.members);
754         DEL_ARR_F(clss->attr.ca.subtypes);
755         DEL_ARR_F(clss->attr.ca.supertypes);
756 }
757
758 ident *get_class_ident(const ir_type *clss)
759 {
760         assert(clss->type_op == type_class);
761         return clss->name;
762 }
763
764 const char *get_class_name(const ir_type *clss)
765 {
766         if (get_class_ident(clss) == NULL)
767                 return NULL;
768         return get_id_str(get_class_ident(clss));
769 }
770
771 static void add_class_member(ir_type *clss, ir_entity *member)
772 {
773         assert(clss && (clss->type_op == type_class));
774         assert(clss != get_entity_type(member) && "recursive type");
775         ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
776 }
777
778 size_t (get_class_n_members)(const ir_type *clss)
779 {
780         return _get_class_n_members(clss);
781 }
782
783 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
784 {
785         size_t i, n;
786         assert(clss && (clss->type_op == type_class));
787         for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
788                 if (get_class_member(clss, i) == mem)
789                         return i;
790         }
791         return INVALID_MEMBER_INDEX;
792 }
793
794 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
795 {
796         return _get_class_member(clss, pos);
797 }
798
799 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
800 {
801         size_t i, n_mem;
802         assert(clss && (clss->type_op == type_class));
803         n_mem = get_class_n_members(clss);
804         for (i = 0; i < n_mem; ++i) {
805                 ir_entity *mem = get_class_member(clss, i);
806                 if (get_entity_ident(mem) == name)
807                         return mem;
808         }
809         return NULL;
810 }
811
812 static void remove_class_member(ir_type *clss, ir_entity *member)
813 {
814         size_t i;
815         assert(clss && (clss->type_op == type_class));
816         for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
817                 if (clss->attr.ca.members[i] == member) {
818                         for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
819                                 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
820                         ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
821                         break;
822                 }
823         }
824 }
825
826 void add_class_subtype(ir_type *clss, ir_type *subtype)
827 {
828         size_t i;
829         assert(clss->type_op == type_class);
830         ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
831         for (i = 0; i < get_class_n_supertypes(subtype); i++) {
832                 if (get_class_supertype(subtype, i) == clss)
833                         /* Class already registered */
834                         return;
835         }
836         ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
837 }
838
839 size_t get_class_n_subtypes(const ir_type *clss)
840 {
841         assert(clss->type_op == type_class);
842         return ARR_LEN (clss->attr.ca.subtypes);
843 }
844
845 ir_type *get_class_subtype(ir_type *clss, size_t pos)
846 {
847         assert(clss->type_op == type_class);
848         assert(pos < get_class_n_subtypes(clss));
849         return clss->attr.ca.subtypes[pos];
850 }
851
852 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
853 {
854         size_t i, n_subtypes = get_class_n_subtypes(clss);
855         assert(is_Class_type(subclass));
856         for (i = 0; i < n_subtypes; ++i) {
857                 if (get_class_subtype(clss, i) == subclass)
858                         return i;
859         }
860         return (size_t)-1;
861 }
862
863 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
864 {
865         assert(clss->type_op == type_class);
866         assert(pos < get_class_n_subtypes(clss));
867         clss->attr.ca.subtypes[pos] = subtype;
868 }
869
870 void remove_class_subtype(ir_type *clss, ir_type *subtype)
871 {
872         size_t i;
873         assert(clss && (clss->type_op == type_class));
874         for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
875                 if (clss->attr.ca.subtypes[i] == subtype) {
876                         for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
877                                 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
878                         ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
879                         break;
880                 }
881         }
882 }
883
884 void add_class_supertype(ir_type *clss, ir_type *supertype)
885 {
886         size_t i;
887         size_t n;
888         assert(clss && (clss->type_op == type_class));
889         assert(supertype && (supertype -> type_op == type_class));
890         ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
891         for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
892                 if (get_class_subtype(supertype, i) == clss)
893                         /* Class already registered */
894                         return;
895         }
896         ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
897 }
898
899 size_t get_class_n_supertypes(const ir_type *clss)
900 {
901         assert(clss->type_op == type_class);
902         return ARR_LEN(clss->attr.ca.supertypes);
903 }
904
905 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
906 {
907         size_t i, n_supertypes = get_class_n_supertypes(clss);
908         assert(super_clss && (super_clss->type_op == type_class));
909         for (i = 0; i < n_supertypes; i++) {
910                 if (get_class_supertype(clss, i) == super_clss)
911                         return i;
912         }
913         return (size_t)-1;
914 }
915
916 ir_type *get_class_supertype(ir_type *clss, size_t pos)
917 {
918         assert(clss->type_op == type_class);
919         assert(pos < get_class_n_supertypes(clss));
920         return clss->attr.ca.supertypes[pos];
921 }
922
923 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
924 {
925         assert(clss->type_op == type_class);
926         assert(pos < get_class_n_supertypes(clss));
927         clss->attr.ca.supertypes[pos] = supertype;
928 }
929
930 void remove_class_supertype(ir_type *clss, ir_type *supertype)
931 {
932         size_t i;
933         assert(clss && (clss->type_op == type_class));
934         for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
935                 if (clss->attr.ca.supertypes[i] == supertype) {
936                         for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
937                                 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
938                         ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
939                         break;
940                 }
941         }
942 }
943
944 ir_entity *get_class_type_info(const ir_type *clss)
945 {
946         return clss->attr.ca.type_info;
947 }
948
949 void set_class_type_info(ir_type *clss, ir_entity *ent)
950 {
951         clss->attr.ca.type_info = ent;
952         if (ent)
953                 ent->repr_class = clss;
954 }
955
956 ir_peculiarity get_class_peculiarity(const ir_type *clss)
957 {
958         assert(clss && (clss->type_op == type_class));
959         return clss->attr.ca.peculiarity;
960 }
961
962 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
963 {
964         assert(clss && (clss->type_op == type_class));
965         assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
966         clss->attr.ca.peculiarity = pec;
967 }
968
969 unsigned (get_class_vtable_size)(const ir_type *clss)
970 {
971         return _get_class_vtable_size(clss);
972 }
973
974 void (set_class_vtable_size)(ir_type *clss, unsigned size)
975 {
976         _set_class_vtable_size(clss, size);
977 }
978
979 int (is_class_final)(const ir_type *clss)
980 {
981         return _is_class_final(clss);
982 }
983
984 void (set_class_final)(ir_type *clss, int flag)
985 {
986         _set_class_final(clss, flag);
987 }
988
989 int (is_class_interface)(const ir_type *clss)
990 {
991         return _is_class_interface(clss);
992 }
993
994 void (set_class_interface)(ir_type *clss, int flag)
995 {
996         _set_class_interface(clss, flag);
997 }
998
999 int (is_class_abstract)(const ir_type *clss)
1000 {
1001          return _is_class_abstract(clss);
1002 }
1003
1004 void (set_class_abstract)(ir_type *clss, int final)
1005 {
1006         _set_class_abstract(clss, final);
1007 }
1008
1009 void set_class_dfn(ir_type *clss, int dfn)
1010 {
1011         clss->attr.ca.dfn = dfn;
1012 }
1013
1014 int get_class_dfn(const ir_type *clss)
1015 {
1016         return (clss->attr.ca.dfn);
1017 }
1018
1019 int (is_Class_type)(const ir_type *clss)
1020 {
1021         return _is_class_type(clss);
1022 }
1023
1024 void set_class_mode(ir_type *tp, ir_mode *mode)
1025 {
1026         /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1027         assert(get_type_state(tp) == layout_fixed &&
1028                tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1029         tp->mode = mode;
1030 }
1031
1032 void set_class_size(ir_type *tp, unsigned size)
1033 {
1034         tp->size = size;
1035 }
1036
1037
1038 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1039 {
1040         ir_type *res = new_type(type_struct, NULL, db);
1041         res->name = name;
1042
1043         res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1044         hook_new_type(res);
1045         return res;
1046 }
1047
1048 ir_type *new_type_struct(ident *name)
1049 {
1050         return new_d_type_struct (name, NULL);
1051 }
1052
1053 void free_struct_entities(ir_type *strct)
1054 {
1055         size_t i;
1056         assert(strct && (strct->type_op == type_struct));
1057         /* we must iterate backward here */
1058         for (i = get_struct_n_members(strct); i > 0;)
1059                 free_entity(get_struct_member(strct, --i));
1060 }
1061
1062 void free_struct_attrs(ir_type *strct)
1063 {
1064         assert(strct && (strct->type_op == type_struct));
1065         DEL_ARR_F(strct->attr.sa.members);
1066 }
1067
1068 ident *get_struct_ident(const ir_type *strct)
1069 {
1070         assert(strct->type_op == type_struct);
1071         return strct->name;
1072 }
1073
1074 const char *get_struct_name(const ir_type *strct)
1075 {
1076         if (get_struct_ident(strct) == NULL)
1077                 return NULL;
1078         return get_id_str(get_struct_ident(strct));
1079 }
1080
1081 size_t get_struct_n_members(const ir_type *strct)
1082 {
1083         assert(strct->type_op == type_struct);
1084         return ARR_LEN(strct->attr.sa.members);
1085 }
1086
1087 static void add_struct_member(ir_type *strct, ir_entity *member)
1088 {
1089         assert(strct && (strct->type_op == type_struct));
1090         assert(get_type_tpop(get_entity_type(member)) != type_method);
1091         assert(strct != get_entity_type(member) && "recursive type");
1092         ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1093 }
1094
1095 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1096 {
1097         assert(strct && (strct->type_op == type_struct));
1098         assert(pos < get_struct_n_members(strct));
1099         return strct->attr.sa.members[pos];
1100 }
1101
1102 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1103 {
1104         size_t i, n;
1105         assert(strct && (strct->type_op == type_struct));
1106         for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1107                 if (get_struct_member(strct, i) == mem)
1108                         return i;
1109         }
1110         return (size_t)-1;
1111 }
1112
1113 static void remove_struct_member(ir_type *strct, ir_entity *member)
1114 {
1115         size_t i;
1116         assert(strct && (strct->type_op == type_struct));
1117         for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1118                 if (strct->attr.sa.members[i] == member) {
1119                         for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1120                                 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1121                         ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1122                         break;
1123                 }
1124         }
1125 }
1126
1127 int (is_Struct_type)(const ir_type *strct)
1128 {
1129         return _is_struct_type(strct);
1130 }
1131
1132 void set_struct_mode(ir_type *tp, ir_mode *mode)
1133 {
1134         /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1135         assert(get_type_state(tp) == layout_fixed &&
1136                tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1137         tp->mode = mode;
1138 }
1139
1140 void set_struct_size(ir_type *tp, unsigned size)
1141 {
1142         tp->size = size;
1143 }
1144
1145
1146 /**
1147  * Lazy construction of value argument / result representation.
1148  * Constructs a struct type and its member.  The types of the members
1149  * are passed in the argument list.
1150  *
1151  * @param name    name of the type constructed
1152  * @param len     number of fields
1153  * @param tps     array of field types with length len
1154  */
1155 static ir_type *build_value_type(char const* name, size_t len, tp_ent_pair *tps)
1156 {
1157         size_t i;
1158         ir_type *res = new_type_struct(new_id_from_str(name));
1159         res->flags |= tf_value_param_type;
1160         /* Remove type from type list.  Must be treated differently than other types. */
1161         remove_irp_type(res);
1162         for (i = 0; i < len; ++i) {
1163                 ident *id = new_id_from_str("elt");
1164
1165                 /* use res as default if corresponding type is not yet set. */
1166                 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1167                 tps[i].ent = new_entity(res, id, elt_type);
1168                 set_entity_allocation(tps[i].ent, allocation_parameter);
1169         }
1170         return res;
1171 }
1172
1173 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1174 {
1175         ir_type *res;
1176
1177         assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1178         res = new_type(type_method, mode_P_code, db);
1179         res->flags                       |= tf_layout_fixed;
1180         res->size                         = get_mode_size_bytes(mode_P_code);
1181         res->attr.ma.n_params             = n_param;
1182         res->attr.ma.params               = XMALLOCNZ(tp_ent_pair, n_param);
1183         res->attr.ma.value_params         = NULL;
1184         res->attr.ma.n_res                = n_res;
1185         res->attr.ma.res_type             = XMALLOCNZ(tp_ent_pair, n_res);
1186         res->attr.ma.variadicity          = variadicity_non_variadic;
1187         res->attr.ma.additional_properties = mtp_no_property;
1188         hook_new_type(res);
1189         return res;
1190 }
1191
1192 ir_type *new_type_method(size_t n_param, size_t n_res)
1193 {
1194         return new_d_type_method(n_param, n_res, NULL);
1195 }
1196
1197 ir_type *clone_type_method(ir_type *tp)
1198 {
1199         ir_type  *res;
1200         ir_mode  *mode;
1201         size_t    n_params;
1202         size_t    n_res;
1203         type_dbg_info *db;
1204
1205         assert(is_Method_type(tp));
1206
1207         mode     = tp->mode;
1208         n_params = tp->attr.ma.n_params;
1209         n_res    = tp->attr.ma.n_res;
1210         db       = tp->dbi;
1211
1212         res = new_type(type_method, mode, db);
1213
1214         res->flags                         = tp->flags;
1215         res->assoc_type                    = tp->assoc_type;
1216         res->size                          = tp->size;
1217         res->attr.ma.n_params              = n_params;
1218         res->attr.ma.params                = XMALLOCN(tp_ent_pair, n_params);
1219         memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1220         res->attr.ma.value_params          = tp->attr.ma.value_params;
1221         res->attr.ma.n_res                 = n_res;
1222         res->attr.ma.res_type              = XMALLOCN(tp_ent_pair, n_res);
1223         memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1224         res->attr.ma.variadicity           = tp->attr.ma.variadicity;
1225         res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1226         res->attr.ma.irg_calling_conv      = tp->attr.ma.irg_calling_conv;
1227         hook_new_type(res);
1228         return res;
1229 }
1230
1231 void free_method_entities(ir_type *method)
1232 {
1233         (void) method;
1234         assert(method && (method->type_op == type_method));
1235 }
1236
1237 void free_method_attrs(ir_type *method)
1238 {
1239         assert(method && (method->type_op == type_method));
1240         free(method->attr.ma.params);
1241         free(method->attr.ma.res_type);
1242         /* cannot free it yet, type could be cloned ...
1243         if (method->attr.ma.value_params) {
1244                 free_type_entities(method->attr.ma.value_params);
1245                 free_type(method->attr.ma.value_params);
1246         }
1247         */
1248 }
1249
1250 size_t (get_method_n_params)(const ir_type *method)
1251 {
1252         return _get_method_n_params(method);
1253 }
1254
1255 ir_type *get_method_param_type(ir_type *method, size_t pos)
1256 {
1257         ir_type *res;
1258         assert(method->type_op == type_method);
1259         assert(pos < get_method_n_params(method));
1260         res = method->attr.ma.params[pos].tp;
1261         assert(res != NULL && "empty method param type");
1262         return res;
1263 }
1264
1265 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1266 {
1267         assert(method->type_op == type_method);
1268         assert(pos < get_method_n_params(method));
1269         method->attr.ma.params[pos].tp = tp;
1270         /* If information constructed set pass-by-value representation. */
1271         if (method->attr.ma.value_params) {
1272                 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1273                 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1274         }
1275 }
1276
1277 ir_entity *get_method_value_param_ent(ir_type *method, size_t pos)
1278 {
1279         assert(method && (method->type_op == type_method));
1280         assert(pos < get_method_n_params(method));
1281
1282         if (!method->attr.ma.value_params) {
1283                 /* parameter value type not created yet, build */
1284                 method->attr.ma.value_params = build_value_type("<value param>",
1285                         get_method_n_params(method), method->attr.ma.params);
1286         }
1287         /*
1288          * build_value_type() sets the method->attr.ma.value_params type as default if
1289          * no type is set!
1290          */
1291         assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1292                && "param type not yet set");
1293         return method->attr.ma.params[pos].ent;
1294 }
1295
1296 void set_method_value_param_type(ir_type *method, ir_type *tp)
1297 {
1298         size_t i;
1299         size_t n;
1300
1301         assert(method && (method->type_op == type_method));
1302         assert(is_value_param_type(tp));
1303         assert(get_method_n_params(method) == get_struct_n_members(tp));
1304
1305         method->attr.ma.value_params = tp;
1306
1307         n = get_struct_n_members(tp);
1308         for (i = 0; i < n; i++) {
1309                 ir_entity *ent = get_struct_member(tp, i);
1310                 method->attr.ma.params[i].ent = ent;
1311         }
1312 }
1313
1314 ir_type *get_method_value_param_type(const ir_type *method)
1315 {
1316         assert(method && (method->type_op == type_method));
1317         return method->attr.ma.value_params;
1318 }
1319
1320 size_t (get_method_n_ress)(const ir_type *method)
1321 {
1322         return _get_method_n_ress(method);
1323 }
1324
1325 ir_type *get_method_res_type(ir_type *method, size_t pos)
1326 {
1327         ir_type *res;
1328         assert(method->type_op == type_method);
1329         assert(pos < get_method_n_ress(method));
1330         res = method->attr.ma.res_type[pos].tp;
1331         assert(res != NULL && "empty method return type");
1332         return res;
1333 }
1334
1335 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1336 {
1337         assert(method->type_op == type_method);
1338         assert(pos < get_method_n_ress(method));
1339         /* set the result ir_type */
1340         method->attr.ma.res_type[pos].tp = tp;
1341         /* If information constructed set pass-by-value representation. */
1342 }
1343
1344 const char *get_variadicity_name(ir_variadicity vari)
1345 {
1346 #define X(a)    case a: return #a
1347         switch (vari) {
1348         X(variadicity_non_variadic);
1349         X(variadicity_variadic);
1350         default:
1351                 return "BAD VALUE";
1352         }
1353 #undef X
1354 }
1355
1356 ir_variadicity get_method_variadicity(const ir_type *method)
1357 {
1358         assert(method && (method->type_op == type_method));
1359         return method->attr.ma.variadicity;
1360 }
1361
1362 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1363 {
1364         assert(method && (method->type_op == type_method));
1365         method->attr.ma.variadicity = vari;
1366 }
1367
1368 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1369 {
1370         return _get_method_additional_properties(method);
1371 }
1372
1373 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1374 {
1375         _set_method_additional_properties(method, mask);
1376 }
1377
1378 void (add_method_additional_properties)(ir_type *method,
1379                                         mtp_additional_properties flag)
1380 {
1381         _add_method_additional_properties(method, flag);
1382 }
1383
1384 unsigned (get_method_calling_convention)(const ir_type *method)
1385 {
1386         return _get_method_calling_convention(method);
1387 }
1388
1389 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1390 {
1391         _set_method_calling_convention(method, cc_mask);
1392 }
1393
1394 unsigned get_method_n_regparams(ir_type *method)
1395 {
1396         unsigned cc = get_method_calling_convention(method);
1397         assert(IS_FASTCALL(cc));
1398
1399         return cc & ~cc_bits;
1400 }
1401
1402 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1403 {
1404         unsigned cc = get_method_calling_convention(method);
1405         assert(IS_FASTCALL(cc));
1406
1407         set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1408 }
1409
1410 int (is_Method_type)(const ir_type *method)
1411 {
1412         return _is_method_type(method);
1413 }
1414
1415
1416 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1417 {
1418         ir_type *res = new_type(type_union, NULL, db);
1419         res->name = name;
1420
1421         res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1422         hook_new_type(res);
1423         return res;
1424 }
1425
1426 ir_type *new_type_union(ident *name)
1427 {
1428         return new_d_type_union(name, NULL);
1429 }
1430
1431 void free_union_entities(ir_type *uni)
1432 {
1433         size_t i;
1434         assert(uni && (uni->type_op == type_union));
1435         /* we must iterate backward here */
1436         for (i = get_union_n_members(uni); i > 0;)
1437                 free_entity(get_union_member(uni, --i));
1438 }
1439
1440 void free_union_attrs(ir_type *uni)
1441 {
1442         assert(uni && (uni->type_op == type_union));
1443         DEL_ARR_F(uni->attr.ua.members);
1444 }
1445
1446 ident *get_union_ident(const ir_type *uni)
1447 {
1448         assert(uni->type_op == type_union);
1449         return uni->name;
1450 }
1451
1452 const char *get_union_name(const ir_type *uni)
1453 {
1454         if (get_union_ident(uni) == NULL)
1455                 return NULL;
1456         return get_id_str(get_union_ident(uni));
1457 }
1458
1459 size_t get_union_n_members(const ir_type *uni)
1460 {
1461         assert(uni->type_op == type_union);
1462         return ARR_LEN(uni->attr.ua.members);
1463 }
1464
1465 static void add_union_member(ir_type *uni, ir_entity *member)
1466 {
1467         assert(uni->type_op == type_union);
1468         assert(uni != get_entity_type(member) && "recursive type");
1469         ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1470 }
1471
1472 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1473 {
1474         assert(uni->type_op == type_union);
1475         assert(pos < get_union_n_members(uni));
1476         return uni->attr.ua.members[pos];
1477 }
1478
1479 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1480 {
1481         size_t i, n;
1482         assert(uni && (uni->type_op == type_union));
1483         for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1484                 if (get_union_member(uni, i) == mem)
1485                         return i;
1486         }
1487         return (size_t)-1;
1488 }
1489
1490 static void remove_union_member(ir_type *uni, ir_entity *member)
1491 {
1492         size_t i;
1493         assert(uni && (uni->type_op == type_union));
1494         for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1495                 if (uni->attr.ua.members[i] == member) {
1496                         for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1497                                 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1498                         ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1499                         break;
1500                 }
1501         }
1502 }
1503
1504 int (is_Union_type)(const ir_type *uni)
1505 {
1506         return _is_union_type(uni);
1507 }
1508
1509 void set_union_size(ir_type *tp, unsigned size)
1510 {
1511         tp->size = size;
1512 }
1513
1514
1515
1516 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1517                           type_dbg_info *db)
1518 {
1519         ir_type *res;
1520         size_t i;
1521         ir_node *unk;
1522         ir_graph *irg = get_const_code_irg();
1523
1524         assert(!is_Method_type(element_type));
1525
1526         res = new_type(type_array, NULL, db);
1527         res->attr.aa.n_dimensions = n_dimensions;
1528         res->attr.aa.lower_bound  = XMALLOCNZ(ir_node*, n_dimensions);
1529         res->attr.aa.upper_bound  = XMALLOCNZ(ir_node*, n_dimensions);
1530         res->attr.aa.order        = XMALLOCNZ(size_t,   n_dimensions);
1531
1532         unk = new_r_Unknown(irg, mode_Iu);
1533         for (i = 0; i < n_dimensions; i++) {
1534                 res->attr.aa.lower_bound[i] =
1535                 res->attr.aa.upper_bound[i] = unk;
1536                 res->attr.aa.order[i]       = i;
1537         }
1538
1539         res->attr.aa.element_type = element_type;
1540         res->attr.aa.element_ent
1541                 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1542         res->attr.aa.element_ent->owner = res;
1543
1544         hook_new_type(res);
1545         return res;
1546 }
1547
1548 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1549 {
1550         return new_d_type_array(n_dimensions, element_type, NULL);
1551 }
1552
1553 void free_array_automatic_entities(ir_type *array)
1554 {
1555         assert(array && (array->type_op == type_array));
1556         free_entity(get_array_element_entity(array));
1557 }
1558
1559 void free_array_entities(ir_type *array)
1560 {
1561         (void) array;
1562         assert(array->type_op == type_array);
1563 }
1564
1565 void free_array_attrs(ir_type *array)
1566 {
1567         assert(array->type_op == type_array);
1568         free(array->attr.aa.lower_bound);
1569         free(array->attr.aa.upper_bound);
1570         free(array->attr.aa.order);
1571 }
1572
1573 /* manipulate private fields of array ir_type */
1574 size_t get_array_n_dimensions(const ir_type *array)
1575 {
1576         assert(array->type_op == type_array);
1577         return array->attr.aa.n_dimensions;
1578 }
1579
1580 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1581                       ir_node *upper_bound)
1582 {
1583         assert(array && (array->type_op == type_array));
1584         assert(lower_bound && "lower_bound node may not be NULL.");
1585         assert(upper_bound && "upper_bound node may not be NULL.");
1586         assert(dimension < array->attr.aa.n_dimensions);
1587         array->attr.aa.lower_bound[dimension] = lower_bound;
1588         array->attr.aa.upper_bound[dimension] = upper_bound;
1589 }
1590
1591 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1592                           int upper_bound)
1593 {
1594         ir_graph *irg = get_const_code_irg();
1595         set_array_bounds(array, dimension,
1596                   new_r_Const_long(irg, mode_Iu, lower_bound),
1597                   new_r_Const_long(irg, mode_Iu, upper_bound));
1598 }
1599
1600 void set_array_lower_bound(ir_type *array, size_t dimension,
1601                            ir_node *lower_bound)
1602 {
1603         assert(array && (array->type_op == type_array));
1604         assert(lower_bound && "lower_bound node may not be NULL.");
1605         array->attr.aa.lower_bound[dimension] = lower_bound;
1606 }
1607
1608 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1609 {
1610         ir_graph *irg = get_const_code_irg();
1611         set_array_lower_bound(array, dimension,
1612              new_r_Const_long(irg, mode_Iu, lower_bound));
1613 }
1614
1615 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1616 {
1617   assert(array && (array->type_op == type_array));
1618   assert(upper_bound && "upper_bound node may not be NULL.");
1619   array->attr.aa.upper_bound[dimension] = upper_bound;
1620 }
1621
1622 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1623 {
1624         ir_graph *irg = get_const_code_irg();
1625         set_array_upper_bound(array, dimension,
1626                               new_r_Const_long(irg, mode_Iu, upper_bound));
1627 }
1628
1629 int has_array_lower_bound(const ir_type *array, size_t dimension)
1630 {
1631         assert(array && (array->type_op == type_array));
1632         return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1633 }
1634
1635 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1636 {
1637         assert(array && (array->type_op == type_array));
1638         return array->attr.aa.lower_bound[dimension];
1639 }
1640
1641 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1642 {
1643         ir_node *node;
1644         assert(array && (array->type_op == type_array));
1645         node = array->attr.aa.lower_bound[dimension];
1646         assert(is_Const(node));
1647         return get_tarval_long(get_Const_tarval(node));
1648 }
1649
1650 int has_array_upper_bound(const ir_type *array, size_t dimension)
1651 {
1652         assert(array && (array->type_op == type_array));
1653         return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1654 }
1655
1656 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1657 {
1658         assert(array && (array->type_op == type_array));
1659         return array->attr.aa.upper_bound[dimension];
1660 }
1661
1662 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1663 {
1664         ir_node *node;
1665         assert(array && (array->type_op == type_array));
1666         node = array->attr.aa.upper_bound[dimension];
1667         assert(is_Const(node));
1668         return get_tarval_long(get_Const_tarval(node));
1669 }
1670
1671 void set_array_order(ir_type *array, size_t dimension, size_t order)
1672 {
1673         assert(array && (array->type_op == type_array));
1674         array->attr.aa.order[dimension] = order;
1675 }
1676
1677 size_t get_array_order(const ir_type *array, size_t dimension)
1678 {
1679         assert(array && (array->type_op == type_array));
1680         return array->attr.aa.order[dimension];
1681 }
1682
1683 size_t find_array_dimension(const ir_type *array, size_t order)
1684 {
1685         size_t dim;
1686
1687         assert(array->type_op == type_array);
1688
1689         for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1690                 if (array->attr.aa.order[dim] == order)
1691                         return dim;
1692         }
1693         return (size_t)-1;
1694 }
1695
1696 void set_array_element_type(ir_type *array, ir_type *tp)
1697 {
1698         assert(array && (array->type_op == type_array));
1699         assert(!is_Method_type(tp));
1700         array->attr.aa.element_type = tp;
1701 }
1702
1703 ir_type *get_array_element_type(const ir_type *array)
1704 {
1705         assert(array && (array->type_op == type_array));
1706         return array->attr.aa.element_type;
1707 }
1708
1709 void set_array_element_entity(ir_type *array, ir_entity *ent)
1710 {
1711         assert(array && (array->type_op == type_array));
1712         assert((get_entity_type(ent)->type_op != type_method));
1713         array->attr.aa.element_ent = ent;
1714         array->attr.aa.element_type = get_entity_type(ent);
1715 }
1716
1717 ir_entity *get_array_element_entity(const ir_type *array)
1718 {
1719         assert(array && (array->type_op == type_array));
1720         return array->attr.aa.element_ent;
1721 }
1722
1723 int (is_Array_type)(const ir_type *array)
1724 {
1725         return _is_array_type(array);
1726 }
1727
1728 void set_array_size(ir_type *tp, unsigned size)
1729 {
1730         /* FIXME: Here we should make some checks with the element type size */
1731         tp->size = size;
1732 }
1733
1734
1735 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1736 {
1737         ir_type *res;
1738
1739         res = new_type(type_enumeration, NULL, db);
1740         res->name = name;
1741         res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1742         hook_new_type(res);
1743         return res;
1744 }
1745
1746 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1747 {
1748         return new_d_type_enumeration(name, n_enums, NULL);
1749 }
1750
1751 void free_enumeration_entities(ir_type *enumeration)
1752 {
1753         (void) enumeration;
1754         assert(enumeration->type_op == type_enumeration);
1755 }
1756
1757 void free_enumeration_attrs(ir_type *enumeration)
1758 {
1759         assert(enumeration->type_op == type_enumeration);
1760         DEL_ARR_F(enumeration->attr.ea.enumer);
1761 }
1762
1763 ident *get_enumeration_ident(const ir_type *enumeration)
1764 {
1765         assert(enumeration->type_op == type_enumeration);
1766         return enumeration->name;
1767 }
1768
1769 const char *get_enumeration_name(const ir_type *enumeration)
1770 {
1771         if (get_enumeration_ident(enumeration) == NULL)
1772                 return NULL;
1773         return get_id_str(get_enumeration_ident(enumeration));
1774 }
1775
1776 size_t get_enumeration_n_enums(const ir_type *enumeration)
1777 {
1778         assert(enumeration->type_op == type_enumeration);
1779         return ARR_LEN(enumeration->attr.ea.enumer);
1780 }
1781
1782 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1783                            ir_tarval *con)
1784 {
1785         assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1786         enumeration->attr.ea.enumer[pos].nameid = nameid;
1787         enumeration->attr.ea.enumer[pos].value  = con;
1788         enumeration->attr.ea.enumer[pos].owner  = enumeration;
1789 }
1790
1791 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1792 {
1793         assert(enumeration->type_op == type_enumeration);
1794         assert(pos < get_enumeration_n_enums(enumeration));
1795         return &enumeration->attr.ea.enumer[pos];
1796 }
1797
1798 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1799 {
1800         return enum_cnst->owner;
1801 }
1802
1803 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1804 {
1805         enum_cnst->value = con;
1806 }
1807
1808 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1809 {
1810         return enum_cnst->value;
1811 }
1812
1813 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1814 {
1815         enum_cnst->nameid = id;
1816 }
1817
1818 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1819 {
1820         return enum_cnst->nameid;
1821 }
1822
1823 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1824 {
1825         return get_id_str(enum_cnst->nameid);
1826 }
1827
1828 int (is_Enumeration_type)(const ir_type *enumeration)
1829 {
1830         return _is_enumeration_type(enumeration);
1831 }
1832
1833 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1834 {
1835         assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1836         /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1837         assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1838
1839         tp->size = get_mode_size_bytes(mode);
1840         tp->mode = mode;
1841 }
1842
1843
1844
1845 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1846 {
1847         ir_type *res;
1848         ir_mode *mode;
1849
1850         if (is_Method_type(points_to) || is_code_type(points_to)) {
1851                 mode = mode_P_code;
1852         } else {
1853                 mode = mode_P_data;
1854         }
1855
1856         res = new_type(type_pointer, mode, db);
1857         res->attr.pa.points_to = points_to;
1858         assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1859         res->size = get_mode_size_bytes(res->mode);
1860         res->flags |= tf_layout_fixed;
1861         hook_new_type(res);
1862         return res;
1863 }
1864
1865 ir_type *new_type_pointer(ir_type *points_to)
1866 {
1867         return new_d_type_pointer(points_to, NULL);
1868 }
1869
1870 void free_pointer_entities(ir_type *pointer)
1871 {
1872         (void) pointer;
1873         assert(pointer && (pointer->type_op == type_pointer));
1874 }
1875
1876 void free_pointer_attrs(ir_type *pointer)
1877 {
1878         (void) pointer;
1879         assert(pointer && (pointer->type_op == type_pointer));
1880 }
1881
1882 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1883 {
1884         assert(pointer && (pointer->type_op == type_pointer));
1885         pointer->attr.pa.points_to = tp;
1886 }
1887
1888 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1889 {
1890         assert(pointer && (pointer->type_op == type_pointer));
1891         return pointer->attr.pa.points_to;
1892 }
1893
1894 int (is_Pointer_type)(const ir_type *pointer)
1895 {
1896         return _is_pointer_type(pointer);
1897 }
1898
1899 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1900 {
1901         assert(mode_is_reference(mode) && "Modes of pointers must be references");
1902         /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1903         assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1904
1905         tp->size = get_mode_size_bytes(mode);
1906         tp->mode = mode;
1907 }
1908
1909 ir_type *find_pointer_type_to_type(ir_type *tp)
1910 {
1911         size_t i, n = get_irp_n_types();
1912         for (i = 0; i < n; ++i) {
1913                 ir_type *found = get_irp_type(i);
1914                 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1915                         return (found);
1916         }
1917         return firm_unknown_type;
1918 }
1919
1920
1921
1922 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1923 {
1924         ir_type *res = new_type(type_primitive, mode, db);
1925         res->size  = get_mode_size_bytes(mode);
1926         res->flags |= tf_layout_fixed;
1927         res->attr.ba.base_type = NULL;
1928         hook_new_type(res);
1929         return res;
1930 }
1931
1932 ir_type *new_type_primitive(ir_mode *mode)
1933 {
1934         return new_d_type_primitive(mode, NULL);
1935 }
1936
1937 int (is_Primitive_type)(const ir_type *primitive)
1938 {
1939         return _is_primitive_type(primitive);
1940 }
1941
1942 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1943 {
1944         /* Modes of primitives must be data */
1945         assert(mode_is_data(mode));
1946
1947         /* For primitive size depends on the mode. */
1948         tp->size = get_mode_size_bytes(mode);
1949         tp->mode = mode;
1950 }
1951
1952 ir_type *get_primitive_base_type(const ir_type *tp)
1953 {
1954         assert(is_Primitive_type(tp));
1955         return tp->attr.ba.base_type;
1956 }
1957
1958 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
1959 {
1960         assert(is_Primitive_type(tp));
1961         tp->attr.ba.base_type = base_tp;
1962 }
1963
1964
1965
1966 int (is_atomic_type)(const ir_type *tp)
1967 {
1968         return _is_atomic_type(tp);
1969 }
1970
1971 size_t get_compound_n_members(const ir_type *tp)
1972 {
1973         const tp_op *op  = get_type_tpop(tp);
1974         return op->ops.get_n_members(tp);
1975 }
1976
1977 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
1978 {
1979         const tp_op *op = get_type_tpop(tp);
1980         return op->ops.get_member(tp, pos);
1981 }
1982
1983 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
1984 {
1985         const tp_op *op = get_type_tpop(tp);
1986         return op->ops.get_member_index(tp, member);
1987 }
1988
1989 int is_compound_type(const ir_type *tp)
1990 {
1991         assert(tp->kind == k_type);
1992         return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1993 }
1994
1995 ident *get_compound_ident(const ir_type *tp)
1996 {
1997         assert(is_compound_type(tp));
1998         return tp->name;
1999 }
2000
2001 const char *get_compound_name(const ir_type *tp)
2002 {
2003         if (get_compound_ident(tp) == NULL)
2004                 return NULL;
2005         return get_id_str(get_compound_ident(tp));
2006 }
2007
2008 void remove_compound_member(ir_type *compound, ir_entity *entity)
2009 {
2010         switch (get_type_tpop_code(compound)) {
2011         case tpo_class:  remove_class_member(compound, entity);  break;
2012         case tpo_struct: remove_struct_member(compound, entity); break;
2013         case tpo_union:  remove_union_member(compound, entity);  break;
2014         default:
2015                 panic("argument for remove_compound_member not a compound type");
2016         }
2017 }
2018
2019 void add_compound_member(ir_type *compound, ir_entity *entity)
2020 {
2021         switch (get_type_tpop_code(compound)) {
2022         case tpo_class:  add_class_member(compound, entity);  break;
2023         case tpo_struct: add_struct_member(compound, entity); break;
2024         case tpo_union:  add_union_member(compound, entity);  break;
2025         default:
2026                 panic("argument for add_compound_member not a compound type");
2027         }
2028 }
2029
2030
2031
2032 int is_code_type(const ir_type *tp)
2033 {
2034         assert(tp && tp->kind == k_type);
2035         return tp->type_op == tpop_code;
2036 }
2037
2038 int is_frame_type(const ir_type *tp)
2039 {
2040         return tp->flags & tf_frame_type;
2041 }
2042
2043 int is_value_param_type(const ir_type *tp)
2044 {
2045         return tp->flags & tf_value_param_type;
2046 }
2047
2048 ir_type *new_type_value(void)
2049 {
2050         ir_type *res = new_type_struct(new_id_from_str("<value_type>"));
2051
2052         res->flags |= tf_value_param_type;
2053
2054         /* Remove type from type list.  Must be treated differently than other types. */
2055         remove_irp_type(res);
2056
2057         return res;
2058 }
2059
2060 ir_type *new_type_frame(void)
2061 {
2062         ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
2063
2064         res->flags |= tf_frame_type;
2065
2066         /* Remove type from type list.  Must be treated differently than other types. */
2067         remove_irp_type(res);
2068
2069         /* It is not possible to derive from the frame type. Set the final flag. */
2070         set_class_final(res, 1);
2071
2072         return res;
2073 }
2074
2075 ir_type *clone_frame_type(ir_type *type)
2076 {
2077         ir_type *res;
2078         size_t  i, n;
2079
2080         assert(is_frame_type(type));
2081         /* the entity link resource should be allocated if this function is called */
2082         assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2083
2084         res = new_type_frame();
2085         for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2086                 ir_entity *ent  = get_class_member(type, i);
2087                 ir_entity *nent = copy_entity_own(ent, res);
2088                 set_entity_link(ent, nent);
2089                 set_entity_link(nent, ent);
2090         }
2091         return res;
2092 }
2093
2094 void set_default_size(ir_type *tp, unsigned size)
2095 {
2096         tp->size = size;
2097 }
2098
2099 void default_layout_compound_type(ir_type *type)
2100 {
2101         size_t i;
2102         size_t n = get_compound_n_members(type);
2103         int size = 0;
2104         unsigned align_all = 1;
2105
2106         for (i = 0; i < n; ++i) {
2107                 ir_entity *entity      = get_compound_member(type, i);
2108                 ir_type   *entity_type = get_entity_type(entity);
2109                 unsigned   align;
2110                 unsigned   misalign;
2111
2112                 if (is_Method_type(entity_type))
2113                         continue;
2114
2115                 assert(get_type_state(entity_type) == layout_fixed);
2116                 align     = get_type_alignment_bytes(entity_type);
2117                 align_all = align > align_all ? align : align_all;
2118                 misalign  = (align ? size % align : 0);
2119                 size     += (misalign ? align - misalign : 0);
2120
2121                 set_entity_offset(entity, size);
2122                 if (!is_Union_type(type)) {
2123                         size += get_type_size_bytes(entity_type);
2124                 }
2125         }
2126         if (align_all > 0 && size % align_all) {
2127                 size += align_all - (size % align_all);
2128         }
2129         if (align_all > get_type_alignment_bytes(type)) {
2130                 set_type_alignment_bytes(type, align_all);
2131         }
2132         set_type_size_bytes(type, size);
2133         set_type_state(type, layout_fixed);
2134 }
2135
2136 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2137                             int at_start)
2138 {
2139         ir_entity *area;
2140         ir_type *tp;
2141         ident *name;
2142         char buf[32];
2143         unsigned frame_align;
2144         int offset, frame_size;
2145         static unsigned area_cnt = 0;
2146         static ir_type *a_byte = NULL;
2147
2148         assert(is_frame_type(frame_type));
2149         assert(get_type_state(frame_type) == layout_fixed);
2150         assert(get_type_alignment_bytes(frame_type) > 0);
2151         set_type_state(frame_type, layout_undefined);
2152
2153         if (! a_byte)
2154                 a_byte = new_type_primitive(mode_Bu);
2155
2156         snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2157         name = new_id_from_str(buf);
2158
2159         /* align the size */
2160         frame_align = get_type_alignment_bytes(frame_type);
2161         size = (size + frame_align - 1) & ~(frame_align - 1);
2162
2163         tp = new_type_array(1, a_byte);
2164         set_array_bounds_int(tp, 0, 0, size);
2165         set_type_alignment_bytes(tp, alignment);
2166
2167         frame_size = get_type_size_bytes(frame_type);
2168         if (at_start) {
2169                 size_t i, n;
2170                 /* fix all offsets so far */
2171                 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2172                         ir_entity *ent = get_class_member(frame_type, i);
2173
2174                         set_entity_offset(ent, get_entity_offset(ent) + size);
2175                 }
2176                 /* calculate offset and new type size */
2177                 offset = 0;
2178                 frame_size += size;
2179
2180                 /* increase size to match alignment... */
2181                 if (alignment > frame_align) {
2182                         frame_align = alignment;
2183                         set_type_alignment_bytes(frame_type, frame_align);
2184                         frame_size  = (frame_size + frame_align - 1) & ~(frame_align - 1);
2185                 }
2186         } else {
2187                 /* calculate offset and new type size */
2188                 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2189                 frame_size = offset + size;
2190         }
2191
2192         area = new_entity(frame_type, name, tp);
2193         set_entity_offset(area, offset);
2194         set_type_size_bytes(frame_type, frame_size);
2195
2196         /* mark this entity as compiler generated */
2197         set_entity_compiler_generated(area, 1);
2198
2199         set_type_state(frame_type, layout_fixed);
2200         return area;
2201 }
2202
2203 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2204 {
2205         ident *id;
2206         int p;
2207         type_dbg_info *tdbgi = get_type_dbg_info(type);
2208         if (tdbgi != NULL) {
2209                 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2210                 return;
2211         }
2212
2213         /* we have to construct some name... */
2214         switch (get_type_tpop_code(type)) {
2215         case tpo_uninitialized:
2216                 break;
2217         case tpo_code:
2218                 snprintf(buffer, buffer_size, "code");
2219                 return;
2220
2221         case tpo_class:
2222                 id = get_class_ident(type);
2223                 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2224                 return;
2225
2226         case tpo_struct:
2227                 id = get_struct_ident(type);
2228                 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2229                 return;
2230
2231         case tpo_union:
2232                 id = get_union_ident(type);
2233                 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2234                 return;
2235
2236         case tpo_enumeration:
2237                 id = get_enumeration_ident(type);
2238                 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2239                 return;
2240
2241         case tpo_unknown:
2242                 snprintf(buffer, buffer_size, "unknown type");
2243                 return;
2244
2245         case tpo_pointer:
2246                 p = snprintf(buffer, buffer_size, "pointer to ");
2247                 buffer      += p;
2248                 buffer_size -= p;
2249                 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2250                 return;
2251
2252         case tpo_array:
2253                 p = snprintf(buffer, buffer_size, "array of ");
2254                 buffer      += p;
2255                 buffer_size -= p;
2256                 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2257                 return;
2258
2259         case tpo_primitive:
2260                 id = get_mode_ident(get_type_mode(type));
2261                 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2262                 return;
2263
2264         case tpo_none:
2265                 snprintf(buffer, buffer_size, "none");
2266                 return;
2267         case tpo_method:
2268                 /* TODO: we should print argument and return types here... */
2269                 snprintf(buffer, buffer_size, "method type");
2270                 return;
2271         }
2272         snprintf(buffer, buffer_size, "invalid type");
2273 }