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