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