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