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