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