Fixed indentation
[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       assert(get_type_size_bits(tp) > -1);
410       if (tp != get_glob_type()) {
411         int n_mem = get_class_n_members(tp);
412         for (i = 0; i < n_mem; i++) {
413           if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
414             { DDMT(tp); DDME(get_class_member(tp, i)); }
415           assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
416               /* TR ??
417           assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
418              (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
419                      */
420         }
421       }
422       break;
423     case tpo_struct:
424       assert(get_type_size_bits(tp) > -1);
425       for (i = 0; i < get_struct_n_members(tp); i++) {
426         assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
427         assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
428       }
429       break;
430     case tpo_union:
431       /* ?? */
432       break;
433     case tpo_array:
434       /* ??
435          Check order?
436          Assure that only innermost dimension is dynamic? */
437       break;
438     case tpo_enumeration:
439       assert(get_type_mode != NULL);
440       for (i = 0; i < get_enumeration_n_enums(tp); i++)
441         assert(get_enumeration_enum(tp, i) != NULL);
442       break;
443     default: break;
444     } /* switch (tp) */
445   }
446   if (state == layout_fixed)
447     tp->flags |= tf_layout_fixed;
448   else
449     tp->flags &= ~tf_layout_fixed;
450 }
451
452 unsigned long (get_type_visited)(const ir_type *tp) {
453   return _get_type_visited(tp);
454 }
455
456 void (set_type_visited)(ir_type *tp, unsigned long num) {
457   _set_type_visited(tp, num);
458 }
459
460 /* Sets visited field in type to type_visited. */
461 void (mark_type_visited)(ir_type *tp) {
462   _mark_type_visited(tp);
463 }
464
465 int (type_visited)(const ir_type *tp) {
466   return _type_visited(tp);
467 }
468
469 int (type_not_visited)(const ir_type *tp) {
470   return _type_not_visited(tp);
471 }
472
473 int (is_type)(const void *thing) {
474   return _is_type(thing);
475 }
476
477 /* Checks whether two types are structural equal.*/
478 int equal_type(ir_type *typ1, ir_type *typ2) {
479   entity **m;
480   ir_type **t;
481   int i, j;
482
483   if (typ1 == typ2) return 1;
484
485   if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
486       (get_type_ident(typ1) != get_type_ident(typ2)) ||
487       (get_type_mode(typ1) != get_type_mode(typ2)) ||
488       (get_type_state(typ1) != get_type_state(typ2)))
489     return 0;
490   if ((get_type_state(typ1) == layout_fixed) &&
491       (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
492     return 0;
493
494   switch (get_type_tpop_code(typ1)) {
495   case tpo_class:       {
496     if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
497     if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
498     if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
499     if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
500     /** Compare the members **/
501     m = alloca(sizeof(entity *) * get_class_n_members(typ1));
502     memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
503     /* First sort the members of typ2 */
504     for (i = 0; i < get_class_n_members(typ1); i++) {
505       entity *e1 = get_class_member(typ1, i);
506       for (j = 0; j < get_class_n_members(typ2); j++) {
507         entity *e2 = get_class_member(typ2, j);
508         if (get_entity_name(e1) == get_entity_name(e2))
509           m[i] = e2;
510       }
511     }
512     for (i = 0; i < get_class_n_members(typ1); i++) {
513       if (!m[i]  ||  /* Found no counterpart */
514           !equal_entity(get_class_member(typ1, i), m[i]))
515         return 0;
516     }
517     /** Compare the supertypes **/
518     t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
519     memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
520     /* First sort the supertypes of typ2 */
521     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
522       ir_type *t1 = get_class_supertype(typ1, i);
523       for (j = 0; j < get_class_n_supertypes(typ2); j++) {
524         ir_type *t2 = get_class_supertype(typ2, j);
525         if (get_type_ident(t2) == get_type_ident(t1))
526           t[i] = t2;
527       }
528     }
529     for (i = 0; i < get_class_n_supertypes(typ1); i++) {
530       if (!t[i]  ||  /* Found no counterpart */
531           get_class_supertype(typ1, i) != t[i])
532         return 0;
533     }
534   } break;
535   case tpo_struct:      {
536     if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
537     m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
538     memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
539     /* First sort the members of lt */
540     for (i = 0; i < get_struct_n_members(typ1); i++) {
541       entity *e1 = get_struct_member(typ1, i);
542       for (j = 0; j < get_struct_n_members(typ2); j++) {
543         entity *e2 = get_struct_member(typ2, j);
544         if (get_entity_name(e1) == get_entity_name(e2))
545           m[i] = e2;
546       }
547     }
548     for (i = 0; i < get_struct_n_members(typ1); i++) {
549       if (!m[i]  ||  /* Found no counterpart */
550           !equal_entity(get_struct_member(typ1, i), m[i]))
551         return 0;
552     }
553   } break;
554   case tpo_method:      {
555     int n_param1, n_param2;
556
557     if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
558     if (get_method_n_ress(typ1)      != get_method_n_ress(typ2)) return 0;
559     if (get_method_calling_convention(typ1) !=
560         get_method_calling_convention(typ2)) return 0;
561
562     if (get_method_variadicity(typ1) == variadicity_non_variadic) {
563       n_param1 = get_method_n_params(typ1);
564       n_param2 = get_method_n_params(typ2);
565     }
566     else {
567       n_param1 = get_method_first_variadic_param_index(typ1);
568       n_param2 = get_method_first_variadic_param_index(typ2);
569     }
570
571     if (n_param1 != n_param2) return 0;
572
573     for (i = 0; i < n_param1; i++) {
574       if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
575         return 0;
576     }
577     for (i = 0; i < get_method_n_ress(typ1); i++) {
578       if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
579         return 0;
580     }
581   } break;
582   case tpo_union:       {
583     if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
584     m = alloca(sizeof(entity *) * get_union_n_members(typ1));
585     memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
586     /* First sort the members of lt */
587     for (i = 0; i < get_union_n_members(typ1); i++) {
588       entity *e1 = get_union_member(typ1, i);
589       for (j = 0; j < get_union_n_members(typ2); j++) {
590         entity *e2 = get_union_member(typ2, j);
591         if (get_entity_name(e1) == get_entity_name(e2))
592           m[i] = e2;
593       }
594     }
595     for (i = 0; i < get_union_n_members(typ1); i++) {
596       if (!m[i]  ||  /* Found no counterpart */
597           !equal_entity(get_union_member(typ1, i), m[i]))
598         return 0;
599     }
600   } break;
601   case tpo_array:       {
602     if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
603       return 0;
604     if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
605       return 0;
606     for(i = 0; i < get_array_n_dimensions(typ1); i++) {
607       if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
608           get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
609         return 0;
610       if (get_array_order(typ1, i) != get_array_order(typ2, i))
611         assert(0 && "type compare with different dimension orders not implemented");
612     }
613   } break;
614   case tpo_enumeration: {
615     assert(0 && "enumerations not implemented");
616   } break;
617   case tpo_pointer:     {
618     if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
619       return 0;
620   } break;
621   case tpo_primitive:   {
622   } break;
623   default: break;
624   }
625   return 1;
626 }
627
628 /* Checks whether two types are structural comparable. */
629 int smaller_type (ir_type *st, ir_type *lt) {
630   entity **m;
631   int i, j;
632
633   if (st == lt) return 1;
634
635   if (get_type_tpop_code(st) != get_type_tpop_code(lt))
636     return 0;
637
638   switch(get_type_tpop_code(st)) {
639   case tpo_class:       {
640     return is_SubClass_of(st, lt);
641   } break;
642   case tpo_struct:      {
643     if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
644     m = alloca(sizeof(entity *) * get_struct_n_members(st));
645     memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
646     /* First sort the members of lt */
647     for (i = 0; i < get_struct_n_members(st); i++) {
648       entity *se = get_struct_member(st, i);
649       for (j = 0; j < get_struct_n_members(lt); j++) {
650         entity *le = get_struct_member(lt, j);
651         if (get_entity_name(le) == get_entity_name(se))
652           m[i] = le;
653       }
654     }
655     for (i = 0; i < get_struct_n_members(st); i++) {
656       if (!m[i]  ||  /* Found no counterpart */
657           !smaller_type(get_entity_type(get_struct_member(st, i)),
658                 get_entity_type(m[i])))
659         return 0;
660     }
661   } break;
662   case tpo_method:      {
663     int n_param1, n_param2;
664
665     /** FIXME: is this still 1? */
666     if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
667     if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
668     if (get_method_calling_convention(st) !=
669       get_method_calling_convention(lt)) return 0;
670
671     if (get_method_variadicity(st) == variadicity_non_variadic) {
672       n_param1 = get_method_n_params(st);
673       n_param2 = get_method_n_params(lt);
674     }
675     else {
676       n_param1 = get_method_first_variadic_param_index(st);
677       n_param2 = get_method_first_variadic_param_index(lt);
678     }
679
680     if (n_param1 != n_param2) return 0;
681
682     for (i = 0; i < get_method_n_params(st); i++) {
683       if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
684         return 0;
685     }
686     for (i = 0; i < get_method_n_ress(st); i++) {
687       if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
688         return 0;
689     }
690   } break;
691   case tpo_union:       {
692     if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
693     m = alloca(sizeof(entity *) * get_union_n_members(st));
694     memset(m, 0, sizeof(entity *) * get_union_n_members(st));
695     /* First sort the members of lt */
696     for (i = 0; i < get_union_n_members(st); i++) {
697       entity *se = get_union_member(st, i);
698       for (j = 0; j < get_union_n_members(lt); j++) {
699         entity *le = get_union_member(lt, j);
700         if (get_entity_name(le) == get_entity_name(se))
701           m[i] = le;
702           }
703     }
704     for (i = 0; i < get_union_n_members(st); i++) {
705       if (!m[i]  ||  /* Found no counterpart */
706           !smaller_type(get_entity_type(get_union_member(st, i)),
707                 get_entity_type(m[i])))
708         return 0;
709     }
710   } break;
711   case tpo_array:       {
712     ir_type *set, *let;  /* small/large elt. ir_type */
713     if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
714       return 0;
715     set = get_array_element_type(st);
716     let = get_array_element_type(lt);
717     if (set != let) {
718       /* If the element types are different, set must be convertible
719          to let, and they must have the same size so that address
720          computations work out.  To have a size the layout must
721          be fixed. */
722       if ((get_type_state(set) != layout_fixed) ||
723           (get_type_state(let) != layout_fixed))
724         return 0;
725       if (!smaller_type(set, let) ||
726           get_type_size_bits(set) != get_type_size_bits(let))
727         return 0;
728     }
729     for(i = 0; i < get_array_n_dimensions(st); i++) {
730       if (get_array_lower_bound(lt, i))
731         if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
732           return 0;
733       if (get_array_upper_bound(lt, i))
734         if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
735           return 0;
736     }
737   } break;
738   case tpo_enumeration: {
739     assert(0 && "enumerations not implemented");
740   } break;
741   case tpo_pointer:     {
742     if (!smaller_type(get_pointer_points_to_type(st),
743               get_pointer_points_to_type(lt)))
744       return 0;
745   } break;
746   case tpo_primitive:   {
747     if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
748       return 0;
749   } break;
750   default: break;
751   }
752   return 1;
753 }
754
755 /*-----------------------------------------------------------------*/
756 /* TYPE_CLASS                                                      */
757 /*-----------------------------------------------------------------*/
758
759 /* create a new class ir_type */
760 ir_type *new_d_type_class (ident *name, dbg_info *db) {
761   ir_type *res;
762
763   res = new_type(type_class, NULL, name, db);
764
765   res->attr.ca.members     = NEW_ARR_F (entity *, 0);
766   res->attr.ca.subtypes    = NEW_ARR_F (ir_type *, 0);
767   res->attr.ca.supertypes  = NEW_ARR_F (ir_type *, 0);
768   res->attr.ca.peculiarity = peculiarity_existent;
769   res->attr.ca.type_info   = NULL;
770   res->attr.ca.vtable_size = 0;
771   res->attr.ca.clss_flags  = cf_none;
772   res->attr.ca.dfn         = 0;
773   hook_new_type(res);
774   return res;
775 }
776
777 ir_type *new_type_class (ident *name) {
778   return new_d_type_class (name, NULL);
779 }
780
781 /* free all entities of a class */
782 void free_class_entities(ir_type *clss) {
783   int i;
784   assert(clss && (clss->type_op == type_class));
785   for (i = get_class_n_members(clss) - 1; i >= 0; --i)
786     free_entity(get_class_member(clss, i));
787   /* do NOT free the type info here. It belongs to another class */
788 }
789
790 void free_class_attrs(ir_type *clss) {
791   assert(clss && (clss->type_op == type_class));
792   DEL_ARR_F(clss->attr.ca.members);
793   DEL_ARR_F(clss->attr.ca.subtypes);
794   DEL_ARR_F(clss->attr.ca.supertypes);
795 }
796
797 /* manipulate private fields of class type  */
798 void    add_class_member   (ir_type *clss, entity *member) {
799   assert(clss && (clss->type_op == type_class));
800   assert(clss != get_entity_type(member) && "recursive type");
801   ARR_APP1 (entity *, clss->attr.ca.members, member);
802 }
803
804 int     (get_class_n_members) (const ir_type *clss) {
805   return _get_class_n_members(clss);
806 }
807
808 int     get_class_member_index(const ir_type *clss, entity *mem) {
809   int i, n;
810   assert(clss && (clss->type_op == type_class));
811   for (i = 0, n = get_class_n_members(clss); i < n; ++i)
812     if (get_class_member(clss, i) == mem)
813       return i;
814   return -1;
815 }
816
817 entity *(get_class_member)   (const ir_type *clss, int pos) {
818   return _get_class_member(clss, pos);
819 }
820
821 entity *get_class_member_by_name(ir_type *clss, ident *name) {
822   int i, n_mem;
823   assert(clss && (clss->type_op == type_class));
824   n_mem = get_class_n_members(clss);
825   for (i = 0; i < n_mem; ++i) {
826     entity *mem = get_class_member(clss, i);
827     if (get_entity_ident(mem) == name) return mem;
828   }
829   return NULL;
830 }
831
832 void    set_class_member   (ir_type *clss, entity *member, int pos) {
833   assert(clss && (clss->type_op == type_class));
834   assert(pos >= 0 && pos < get_class_n_members(clss));
835   clss->attr.ca.members[pos] = member;
836 }
837 void    set_class_members  (ir_type *clss, entity **members, int arity) {
838   int i;
839   assert(clss && (clss->type_op == type_class));
840   DEL_ARR_F(clss->attr.ca.members);
841   clss->attr.ca.members    = NEW_ARR_F (entity *, 0);
842   for (i = 0; i < arity; i++) {
843     set_entity_owner(members[i], clss);
844     ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
845   }
846 }
847 void    remove_class_member(ir_type *clss, entity *member) {
848   int i;
849   assert(clss && (clss->type_op == type_class));
850   for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
851     if (clss->attr.ca.members[i] == member) {
852       for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
853         clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
854       ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
855       break;
856     }
857   }
858 }
859
860 void    add_class_subtype   (ir_type *clss, ir_type *subtype) {
861   int i;
862   assert(clss && (clss->type_op == type_class));
863   ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
864   for (i = 0; i < get_class_n_supertypes(subtype); i++)
865     if (get_class_supertype(subtype, i) == clss)
866       /* Class already registered */
867       return;
868   ARR_APP1 (ir_type *, subtype->attr.ca.supertypes, clss);
869 }
870 int     get_class_n_subtypes (const ir_type *clss) {
871   assert(clss && (clss->type_op == type_class));
872   return (ARR_LEN (clss->attr.ca.subtypes));
873 }
874 ir_type *get_class_subtype   (ir_type *clss, int pos) {
875   assert(clss && (clss->type_op == type_class));
876   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
877   return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
878 }
879 int     get_class_subtype_index(ir_type *clss, const ir_type *subclass) {
880   int i, n_subtypes = get_class_n_subtypes(clss);
881   assert(is_Class_type(subclass));
882   for (i = 0; i < n_subtypes; ++i) {
883     if (get_class_subtype(clss, i) == subclass) return i;
884   }
885   return -1;
886 }
887 void    set_class_subtype   (ir_type *clss, ir_type *subtype, int pos) {
888   assert(clss && (clss->type_op == type_class));
889   assert(pos >= 0 && pos < get_class_n_subtypes(clss));
890   clss->attr.ca.subtypes[pos] = subtype;
891 }
892 void    remove_class_subtype(ir_type *clss, ir_type *subtype) {
893   int i;
894   assert(clss && (clss->type_op == type_class));
895   for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
896     if (clss->attr.ca.subtypes[i] == subtype) {
897       for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
898         clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
899       ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
900       break;
901     }
902 }
903
904 void    add_class_supertype   (ir_type *clss, ir_type *supertype) {
905   int i;
906   assert(clss && (clss->type_op == type_class));
907   assert(supertype && (supertype -> type_op == type_class));
908   ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
909   for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
910     if (get_class_subtype(supertype, i) == clss)
911       /* Class already registered */
912       return;
913   ARR_APP1 (ir_type *, supertype->attr.ca.subtypes, clss);
914 }
915 int     get_class_n_supertypes (const ir_type *clss) {
916   assert(clss && (clss->type_op == type_class));
917   return (ARR_LEN (clss->attr.ca.supertypes));
918 }
919 int get_class_supertype_index(ir_type *clss, ir_type *super_clss) {
920   int i, n_supertypes = get_class_n_supertypes(clss);
921   assert(super_clss && (super_clss->type_op == type_class));
922   for (i = 0; i < n_supertypes; i++)
923     if (get_class_supertype(clss, i) == super_clss)
924       return i;
925   return -1;
926 }
927 ir_type *get_class_supertype   (ir_type *clss, int pos) {
928   assert(clss && (clss->type_op == type_class));
929   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
930   return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
931 }
932 void    set_class_supertype   (ir_type *clss, ir_type *supertype, int pos) {
933   assert(clss && (clss->type_op == type_class));
934   assert(pos >= 0 && pos < get_class_n_supertypes(clss));
935   clss->attr.ca.supertypes[pos] = supertype;
936 }
937 void    remove_class_supertype(ir_type *clss, ir_type *supertype) {
938   int i;
939   assert(clss && (clss->type_op == type_class));
940   for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
941     if (clss->attr.ca.supertypes[i] == supertype) {
942       for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
943     clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
944       ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
945       break;
946     }
947 }
948 entity *get_class_type_info(const ir_type *clss) {
949   return clss->attr.ca.type_info;
950 }
951 void set_class_type_info(ir_type *clss, entity *ent) {
952   clss->attr.ca.type_info = ent;
953 }
954
955 const char *get_peculiarity_name(peculiarity p) {
956 #define X(a)    case a: return #a
957   switch (p) {
958     X(peculiarity_description);
959     X(peculiarity_inherited);
960     X(peculiarity_existent);
961   }
962 #undef X
963   return "invalid peculiarity";
964 }
965
966 peculiarity get_class_peculiarity (const ir_type *clss) {
967   assert(clss && (clss->type_op == type_class));
968   return clss->attr.ca.peculiarity;
969 }
970
971 void        set_class_peculiarity (ir_type *clss, peculiarity pec) {
972   assert(clss && (clss->type_op == type_class));
973   assert(pec != peculiarity_inherited);  /* There is no inheritance of types in libFirm. */
974   clss->attr.ca.peculiarity = pec;
975 }
976
977 /* Returns the size of the virtual function table. */
978 unsigned (get_class_vtable_size)(const ir_type *clss) {
979   return _get_class_vtable_size(clss);
980 }
981
982 /* Sets a new size of the virtual function table. */
983 void (set_class_vtable_size)(ir_type *clss, unsigned size) {
984   _set_class_vtable_size(clss, size);
985 }
986
987 /* Returns non-zero if a class is final. */
988 int (is_class_final)(const ir_type *clss) {
989   return _is_class_final(clss);
990 }
991
992 /* Sets if a class is final. */
993 void (set_class_final)(ir_type *clss, int flag) {
994   _set_class_final(clss, flag);
995 }
996
997 /* Returns non-zero if a class is an interface. */
998 int (is_class_interface)(const ir_type *clss) {
999   return _is_class_interface(clss);
1000 }
1001
1002 /* Sets the class interface flag. */
1003 void (set_class_interface)(ir_type *clss, int flag) {
1004   _set_class_interface(clss, flag);
1005 }
1006
1007 /* Returns non-zero if a class is abstract. */
1008 int (is_class_abstract)(const ir_type *clss) {
1009   return _is_class_abstract(clss);
1010 }
1011
1012 /* Sets the class abstract flag. */
1013 void (set_class_abstract)(ir_type *clss, int final) {
1014   _set_class_abstract(clss, final);
1015 }
1016
1017 void set_class_dfn (ir_type *clss, int dfn) {
1018   clss->attr.ca.dfn = dfn;
1019 }
1020
1021 int get_class_dfn (const ir_type *clss) {
1022   return (clss->attr.ca.dfn);
1023 }
1024
1025 /* typecheck */
1026 int (is_Class_type)(const ir_type *clss) {
1027   return _is_class_type(clss);
1028 }
1029
1030 void set_class_mode(ir_type *tp, ir_mode *mode) {
1031   /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1032   assert(get_type_state(tp) == layout_fixed &&
1033     tp->size == get_mode_size_bits(mode) && "mode don't match class layout");
1034   tp->mode = mode;
1035 }
1036
1037 void set_class_size_bits(ir_type *tp, int size) {
1038   /* argh: we must allow to set negative values as "invalid size" */
1039   tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1040   assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1041 }
1042
1043 /*----------------------------------------------------------------**/
1044 /* TYPE_STRUCT                                                     */
1045 /*----------------------------------------------------------------**/
1046
1047 /* create a new type struct */
1048 ir_type *new_d_type_struct(ident *name, dbg_info *db) {
1049   ir_type *res = new_type(type_struct, NULL, name, db);
1050
1051   res->attr.sa.members = NEW_ARR_F(entity *, 0);
1052   hook_new_type(res);
1053   return res;
1054 }
1055
1056 ir_type *new_type_struct (ident *name) {
1057   return new_d_type_struct (name, NULL);
1058 }
1059
1060 void free_struct_entities (ir_type *strct) {
1061   int i;
1062   assert(strct && (strct->type_op == type_struct));
1063   for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1064     free_entity(get_struct_member(strct, i));
1065 }
1066 void free_struct_attrs (ir_type *strct) {
1067   assert(strct && (strct->type_op == type_struct));
1068   DEL_ARR_F(strct->attr.sa.members);
1069 }
1070
1071 /* manipulate private fields of struct */
1072 int     get_struct_n_members (const ir_type *strct) {
1073   assert(strct && (strct->type_op == type_struct));
1074   return (ARR_LEN (strct->attr.sa.members));
1075 }
1076
1077 void    add_struct_member   (ir_type *strct, entity *member) {
1078   assert(strct && (strct->type_op == type_struct));
1079   assert(get_type_tpop(get_entity_type(member)) != type_method);
1080     /*    @@@ lowerfirm geht nicht durch */
1081   assert(strct != get_entity_type(member) && "recursive type");
1082   ARR_APP1 (entity *, strct->attr.sa.members, member);
1083 }
1084
1085 entity *get_struct_member   (const ir_type *strct, int pos) {
1086   assert(strct && (strct->type_op == type_struct));
1087   assert(pos >= 0 && pos < get_struct_n_members(strct));
1088   return strct->attr.sa.members[pos];
1089 }
1090
1091 int     get_struct_member_index(const ir_type *strct, entity *mem) {
1092   int i, n;
1093   assert(strct && (strct->type_op == type_struct));
1094   for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1095     if (get_struct_member(strct, i) == mem)
1096       return i;
1097   return -1;
1098 }
1099
1100 void    set_struct_member   (ir_type *strct, int pos, entity *member) {
1101   assert(strct && (strct->type_op == type_struct));
1102   assert(pos >= 0 && pos < get_struct_n_members(strct));
1103   assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1104   strct->attr.sa.members[pos] = member;
1105 }
1106 void    remove_struct_member(ir_type *strct, entity *member) {
1107   int i;
1108   assert(strct && (strct->type_op == type_struct));
1109   for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1110     if (strct->attr.sa.members[i] == member) {
1111       for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1112     strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1113       ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1114       break;
1115     }
1116 }
1117
1118 /* typecheck */
1119 int (is_Struct_type)(const ir_type *strct) {
1120   return _is_struct_type(strct);
1121 }
1122
1123 void set_struct_mode(ir_type *tp, ir_mode *mode) {
1124   /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1125   assert(get_type_state(tp) == layout_fixed &&
1126     tp->size == get_mode_size_bits(mode) && "mode don't match struct layout");
1127   tp->mode = mode;
1128 }
1129
1130 void set_struct_size_bits(ir_type *tp, int size) {
1131   /* argh: we must allow to set negative values as "invalid size" */
1132   tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1133   assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1134 }
1135
1136 /*******************************************************************/
1137 /** TYPE_METHOD                                                   **/
1138 /*******************************************************************/
1139
1140 /**
1141  * Lazy construction of value argument / result representation.
1142  * Constructs a struct type and its member.  The types of the members
1143  * are passed in the argument list.
1144  *
1145  * @param name    name of the type constructed
1146  * @param len     number of fields
1147  * @param tps     array of field types with length len
1148  */
1149 static INLINE ir_type *
1150 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1151   int i;
1152   ir_type *res = new_type_struct(name);
1153   res->flags |= tf_value_param_type;
1154   /* Remove type from type list.  Must be treated differently than other types. */
1155   remove_irp_type(res);
1156   for (i = 0; i < len; i++) {
1157     /* use res as default if corresponding type is not yet set. */
1158     ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1159
1160     tps[i].ent = new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1161     set_entity_allocation(tps[i].ent, allocation_parameter);
1162   }
1163   return res;
1164 }
1165
1166 /* Create a new method type.
1167    N_param is the number of parameters, n_res the number of results.  */
1168 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1169   ir_type *res;
1170
1171   assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1172   res = new_type(type_method, mode_P_code, name, db);
1173   res->flags                       |= tf_layout_fixed;
1174   res->size                         = get_mode_size_bits(mode_P_code);
1175   res->attr.ma.n_params             = n_param;
1176   res->attr.ma.param_type           = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1177   res->attr.ma.value_params         = NULL;
1178   res->attr.ma.n_res                = n_res;
1179   res->attr.ma.res_type             = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1180   res->attr.ma.value_ress           = NULL;
1181   res->attr.ma.variadicity          = variadicity_non_variadic;
1182   res->attr.ma.first_variadic_param = -1;
1183   res->attr.ma.additional_properties = mtp_no_property;
1184   res->attr.ma.irg_calling_conv     = default_cc_mask;
1185   hook_new_type(res);
1186   return res;
1187 }
1188
1189 ir_type *new_type_method(ident *name, int n_param, int n_res) {
1190   return new_d_type_method(name, n_param, n_res, NULL);
1191 }
1192
1193 void free_method_entities(ir_type *method) {
1194   assert(method && (method->type_op == type_method));
1195 }
1196
1197 /* Attention: also frees entities in value parameter subtypes! */
1198 void free_method_attrs(ir_type *method) {
1199   assert(method && (method->type_op == type_method));
1200   free(method->attr.ma.param_type);
1201   free(method->attr.ma.res_type);
1202   if (method->attr.ma.value_params) {
1203     free_type_entities(method->attr.ma.value_params);
1204     free_type(method->attr.ma.value_params);
1205   }
1206   if (method->attr.ma.value_ress) {
1207     free_type_entities(method->attr.ma.value_ress);
1208     free_type(method->attr.ma.value_ress);
1209   }
1210 }
1211
1212 /* manipulate private fields of method. */
1213 int (get_method_n_params)(const ir_type *method) {
1214   return _get_method_n_params(method);
1215 }
1216
1217 ir_type *get_method_param_type(ir_type *method, int pos) {
1218   ir_type *res;
1219   assert(method && (method->type_op == type_method));
1220   assert(pos >= 0 && pos < get_method_n_params(method));
1221   res = method->attr.ma.param_type[pos].tp;
1222   assert(res != NULL && "empty method param type");
1223   return method->attr.ma.param_type[pos].tp = skip_tid(res);
1224 }
1225
1226 void  set_method_param_type(ir_type *method, int pos, ir_type *tp) {
1227   assert(method && (method->type_op == type_method));
1228   assert(pos >= 0 && pos < get_method_n_params(method));
1229   method->attr.ma.param_type[pos].tp = tp;
1230   /* If information constructed set pass-by-value representation. */
1231   if (method->attr.ma.value_params) {
1232     assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1233     set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1234   }
1235 }
1236
1237 /* Returns an entity that represents the copied value argument.  Only necessary
1238    for compounds passed by value. */
1239 entity *get_method_value_param_ent(ir_type *method, int pos) {
1240   assert(method && (method->type_op == type_method));
1241   assert(pos >= 0 && pos < get_method_n_params(method));
1242
1243   if (!method->attr.ma.value_params) {
1244     /* parameter value type not created yet, build */
1245     method->attr.ma.value_params
1246       = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1247              get_method_n_params(method), method->attr.ma.param_type);
1248   }
1249   /*
1250    * build_value_type() sets the method->attr.ma.value_params type as default if
1251    * no type is set!
1252    */
1253   assert((get_entity_type(method->attr.ma.param_type[pos].ent) != method->attr.ma.value_params)
1254      && "param type not yet set");
1255   return method->attr.ma.param_type[pos].ent;
1256 }
1257
1258 /*
1259  * Returns a type that represents the copied value arguments.
1260  */
1261 ir_type *get_method_value_param_type(const ir_type *method)
1262 {
1263   assert(method && (method->type_op == type_method));
1264   return method->attr.ma.value_params;
1265 }
1266
1267 int (get_method_n_ress)(const ir_type *method) {
1268   return _get_method_n_ress(method);
1269 }
1270
1271 ir_type *get_method_res_type(ir_type *method, int pos) {
1272   ir_type *res;
1273   assert(method && (method->type_op == type_method));
1274   assert(pos >= 0 && pos < get_method_n_ress(method));
1275   res = method->attr.ma.res_type[pos].tp;
1276   assert(res != NULL && "empty method return type");
1277   return method->attr.ma.res_type[pos].tp = skip_tid(res);
1278 }
1279
1280 void  set_method_res_type(ir_type *method, int pos, ir_type *tp) {
1281   assert(method && (method->type_op == type_method));
1282   assert(pos >= 0 && pos < get_method_n_ress(method));
1283   /* set the result ir_type */
1284   method->attr.ma.res_type[pos].tp = tp;
1285   /* If information constructed set pass-by-value representation. */
1286   if (method->attr.ma.value_ress) {
1287     assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1288     set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1289   }
1290 }
1291
1292 /* Returns an entity that represents the copied value result.  Only necessary
1293    for compounds passed by value. */
1294 entity *get_method_value_res_ent(ir_type *method, int pos) {
1295   assert(method && (method->type_op == type_method));
1296   assert(pos >= 0 && pos < get_method_n_ress(method));
1297
1298   if (!method->attr.ma.value_ress) {
1299     /* result value type not created yet, build */
1300     method->attr.ma.value_ress
1301       = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1302              get_method_n_ress(method), method->attr.ma.res_type);
1303   }
1304   /*
1305    * build_value_type() sets the method->attr.ma.value_ress type as default if
1306    * no type is set!
1307    */
1308   assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1309      && "result type not yet set");
1310
1311   return method->attr.ma.res_type[pos].ent;
1312 }
1313
1314 /*
1315  * Returns a type that represents the copied value results.
1316  */
1317 ir_type *get_method_value_res_type(const ir_type *method) {
1318   assert(method && (method->type_op == type_method));
1319   return method->attr.ma.value_ress;
1320 }
1321
1322 /* Returns the null-terminated name of this variadicity. */
1323 const char *get_variadicity_name(variadicity vari)
1324 {
1325 #define X(a)    case a: return #a
1326   switch (vari) {
1327     X(variadicity_non_variadic);
1328     X(variadicity_variadic);
1329     default:
1330       return "BAD VALUE";
1331   }
1332 #undef X
1333 }
1334
1335 variadicity get_method_variadicity(const ir_type *method)
1336 {
1337   assert(method && (method->type_op == type_method));
1338   return method->attr.ma.variadicity;
1339 }
1340
1341 void set_method_variadicity(ir_type *method, variadicity vari)
1342 {
1343   assert(method && (method->type_op == type_method));
1344   method->attr.ma.variadicity = vari;
1345 }
1346
1347 /*
1348  * Returns the first variadic parameter index of a type.
1349  * If this index was NOT set, the index of the last parameter
1350  * of the method type plus one is returned for variadic functions.
1351  * Non-variadic function types always return -1 here.
1352  */
1353 int get_method_first_variadic_param_index(const ir_type *method)
1354 {
1355   assert(method && (method->type_op == type_method));
1356
1357   if (method->attr.ma.variadicity == variadicity_non_variadic)
1358     return -1;
1359
1360   if (method->attr.ma.first_variadic_param == -1)
1361     return get_method_n_params(method);
1362   return method->attr.ma.first_variadic_param;
1363 }
1364
1365 /*
1366  * Sets the first variadic parameter index. This allows to specify
1367  * a complete call type (containing the type of all parameters)
1368  * but still have the knowledge, which parameter must be passed as
1369  * variadic one.
1370  */
1371 void set_method_first_variadic_param_index(ir_type *method, int index)
1372 {
1373   assert(method && (method->type_op == type_method));
1374   assert(index >= 0 && index <= get_method_n_params(method));
1375
1376   method->attr.ma.first_variadic_param = index;
1377 }
1378
1379 unsigned (get_method_additional_properties)(const ir_type *method) {
1380   return _get_method_additional_properties(method);
1381 }
1382
1383 void (set_method_additional_properties)(ir_type *method, unsigned mask) {
1384   _set_method_additional_properties(method, mask);
1385 }
1386
1387 void (set_method_additional_property)(ir_type *method, mtp_additional_property flag) {
1388   _set_method_additional_property(method, flag);
1389 }
1390
1391 /* Returns the calling convention of an entities graph. */
1392 unsigned (get_method_calling_convention)(const ir_type *method) {
1393   return _get_method_calling_convention(method);
1394 }
1395
1396 /* Sets the calling convention of an entities graph. */
1397 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask) {
1398   _set_method_calling_convention(method, cc_mask);
1399 }
1400
1401 /* Returns the number of registers parameters, 0 means default. */
1402 unsigned get_method_n_regparams(ir_type *method) {
1403   unsigned cc = get_method_calling_convention(method);
1404   assert(IS_FASTCALL(cc));
1405
1406   return cc & ~cc_bits;
1407 }
1408
1409 /* Sets the number of registers parameters, 0 means default. */
1410 void set_method_n_regparams(ir_type *method, unsigned n_regs) {
1411   unsigned cc = get_method_calling_convention(method);
1412   assert(IS_FASTCALL(cc));
1413
1414   set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1415 }
1416
1417 /* typecheck */
1418 int (is_Method_type)(const ir_type *method) {
1419   return _is_method_type(method);
1420 }
1421
1422 /*-----------------------------------------------------------------*/
1423 /* TYPE_UNION                                                      */
1424 /*-----------------------------------------------------------------*/
1425
1426 /* create a new type uni */
1427 ir_type *new_d_type_union(ident *name, dbg_info *db) {
1428   ir_type *res = new_type(type_union, NULL, name, db);
1429
1430   res->attr.ua.members = NEW_ARR_F(entity *, 0);
1431   hook_new_type(res);
1432   return res;
1433 }
1434
1435 ir_type *new_type_union(ident *name) {
1436   return new_d_type_union(name, NULL);
1437 }
1438
1439 void free_union_entities(ir_type *uni) {
1440   int i;
1441   assert(uni && (uni->type_op == type_union));
1442   for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1443     free_entity(get_union_member(uni, i));
1444 }
1445
1446 void free_union_attrs (ir_type *uni) {
1447   assert(uni && (uni->type_op == type_union));
1448   DEL_ARR_F(uni->attr.ua.members);
1449 }
1450
1451 /* manipulate private fields of union */
1452 int    get_union_n_members      (const ir_type *uni) {
1453   assert(uni && (uni->type_op == type_union));
1454   return (ARR_LEN (uni->attr.ua.members));
1455 }
1456 void    add_union_member   (ir_type *uni, entity *member) {
1457   assert(uni && (uni->type_op == type_union));
1458   assert(uni != get_entity_type(member) && "recursive type");
1459   ARR_APP1 (entity *, uni->attr.ua.members, member);
1460 }
1461 entity  *get_union_member (const ir_type *uni, int pos) {
1462   assert(uni && (uni->type_op == type_union));
1463   assert(pos >= 0 && pos < get_union_n_members(uni));
1464   return uni->attr.ua.members[pos];
1465 }
1466 int     get_union_member_index(const ir_type *uni, entity *mem) {
1467   int i, n;
1468   assert(uni && (uni->type_op == type_union));
1469   for (i = 0, n = get_union_n_members(uni); i < n; ++i)
1470     if (get_union_member(uni, i) == mem)
1471       return i;
1472   return -1;
1473 }
1474 void   set_union_member (ir_type *uni, int pos, entity *member) {
1475   assert(uni && (uni->type_op == type_union));
1476   assert(pos >= 0 && pos < get_union_n_members(uni));
1477   uni->attr.ua.members[pos] = member;
1478 }
1479 void   remove_union_member(ir_type *uni, entity *member) {
1480   int i;
1481   assert(uni && (uni->type_op == type_union));
1482   for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1483     if (uni->attr.ua.members[i] == member) {
1484       for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1485         uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1486       ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1487       break;
1488     }
1489 }
1490
1491 /* typecheck */
1492 int (is_Union_type)(const ir_type *uni) {
1493   return _is_union_type(uni);
1494 }
1495
1496 void set_union_size_bits(ir_type *tp, int size) {
1497   /* argh: we must allow to set negative values as "invalid size" */
1498   tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1499   assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1500 }
1501
1502 /*-----------------------------------------------------------------*/
1503 /* TYPE_ARRAY                                                      */
1504 /*-----------------------------------------------------------------*/
1505
1506
1507 /* create a new type array -- set dimension sizes independently */
1508 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type, dbg_info *db) {
1509   ir_type *res;
1510   int i;
1511   ir_node *unk;
1512   ir_graph *rem = current_ir_graph;
1513
1514   assert(!is_Method_type(element_type));
1515
1516   res = new_type(type_array, NULL, name, db);
1517   res->attr.aa.n_dimensions = n_dimensions;
1518   res->attr.aa.lower_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1519   res->attr.aa.upper_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1520   res->attr.aa.order        = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1521
1522   current_ir_graph = get_const_code_irg();
1523   unk = new_Unknown( mode_Iu);
1524   for (i = 0; i < n_dimensions; i++) {
1525     res->attr.aa.lower_bound[i] =
1526     res->attr.aa.upper_bound[i] = unk;
1527     res->attr.aa.order[i]       = i;
1528   }
1529   current_ir_graph = rem;
1530
1531   res->attr.aa.element_type = element_type;
1532   new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1533   hook_new_type(res);
1534   return res;
1535 }
1536
1537 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type) {
1538   return new_d_type_array(name, n_dimensions, element_type, NULL);
1539 }
1540
1541 void free_array_automatic_entities(ir_type *array) {
1542   assert(array && (array->type_op == type_array));
1543   free_entity(get_array_element_entity(array));
1544 }
1545
1546 void free_array_entities (ir_type *array) {
1547   assert(array && (array->type_op == type_array));
1548 }
1549
1550 void free_array_attrs (ir_type *array) {
1551   assert(array && (array->type_op == type_array));
1552   free(array->attr.aa.lower_bound);
1553   free(array->attr.aa.upper_bound);
1554   free(array->attr.aa.order);
1555 }
1556
1557 /* manipulate private fields of array ir_type */
1558 int   get_array_n_dimensions (const ir_type *array) {
1559   assert(array && (array->type_op == type_array));
1560   return array->attr.aa.n_dimensions;
1561 }
1562
1563 void
1564 set_array_bounds (ir_type *array, int dimension, ir_node * lower_bound,
1565           ir_node * upper_bound) {
1566   assert(array && (array->type_op == type_array));
1567   assert(lower_bound && "lower_bound node may not be NULL.");
1568   assert(upper_bound && "upper_bound node may not be NULL.");
1569   assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1570   array->attr.aa.lower_bound[dimension] = lower_bound;
1571   array->attr.aa.upper_bound[dimension] = upper_bound;
1572 }
1573 void
1574 set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
1575               int upper_bound) {
1576   ir_graph *rem = current_ir_graph;
1577   current_ir_graph = get_const_code_irg();
1578   set_array_bounds (array, dimension,
1579             new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1580             new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1581   current_ir_graph = rem;
1582 }
1583 void
1584 set_array_lower_bound  (ir_type *array, int dimension, ir_node * lower_bound) {
1585   assert(array && (array->type_op == type_array));
1586   assert(lower_bound && "lower_bound node may not be NULL.");
1587   array->attr.aa.lower_bound[dimension] = lower_bound;
1588 }
1589 void  set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound) {
1590   ir_graph *rem = current_ir_graph;
1591   current_ir_graph = get_const_code_irg();
1592   set_array_lower_bound  (array, dimension,
1593               new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1594   current_ir_graph = rem;
1595 }
1596 void
1597 set_array_upper_bound  (ir_type *array, int dimension, ir_node * upper_bound) {
1598   assert(array && (array->type_op == type_array));
1599   assert(upper_bound && "upper_bound node may not be NULL.");
1600   array->attr.aa.upper_bound[dimension] = upper_bound;
1601 }
1602 void  set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound) {
1603   ir_graph *rem = current_ir_graph;
1604   current_ir_graph = get_const_code_irg();
1605   set_array_upper_bound  (array, dimension,
1606               new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1607   current_ir_graph = rem;
1608 }
1609 int      has_array_lower_bound  (const ir_type *array, int dimension) {
1610   assert(array && (array->type_op == type_array));
1611   return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1612 }
1613 ir_node *get_array_lower_bound  (const ir_type *array, int dimension) {
1614   assert(array && (array->type_op == type_array));
1615   return array->attr.aa.lower_bound[dimension];
1616 }
1617 long     get_array_lower_bound_int  (const ir_type *array, int dimension) {
1618   ir_node *node;
1619   assert(array && (array->type_op == type_array));
1620   node = array->attr.aa.lower_bound[dimension];
1621   assert(get_irn_op(node) == op_Const);
1622   return get_tarval_long(get_Const_tarval(node));
1623 }
1624 int       has_array_upper_bound  (const ir_type *array, int dimension) {
1625   assert(array && (array->type_op == type_array));
1626   return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1627 }
1628 ir_node * get_array_upper_bound  (const ir_type *array, int dimension) {
1629   assert(array && (array->type_op == type_array));
1630   return array->attr.aa.upper_bound[dimension];
1631 }
1632 long     get_array_upper_bound_int  (const ir_type *array, int dimension) {
1633   ir_node *node;
1634   assert(array && (array->type_op == type_array));
1635   node = array->attr.aa.upper_bound[dimension];
1636   assert(get_irn_op(node) == op_Const);
1637   return get_tarval_long(get_Const_tarval(node));
1638 }
1639
1640 void set_array_order (ir_type *array, int dimension, int order) {
1641   assert(array && (array->type_op == type_array));
1642   array->attr.aa.order[dimension] = order;
1643 }
1644
1645 int  get_array_order (const ir_type *array, int dimension) {
1646   assert(array && (array->type_op == type_array));
1647   return array->attr.aa.order[dimension];
1648 }
1649
1650 int find_array_dimension(const ir_type *array, int order) {
1651   int dim;
1652
1653   assert(array && (array->type_op == type_array));
1654
1655   for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1656     if (array->attr.aa.order[dim] == order)
1657       return dim;
1658   }
1659   return -1;
1660 }
1661
1662 void  set_array_element_type (ir_type *array, ir_type *tp) {
1663   assert(array && (array->type_op == type_array));
1664   assert(!is_Method_type(tp));
1665   array->attr.aa.element_type = tp;
1666 }
1667 ir_type *get_array_element_type (ir_type *array) {
1668   assert(array && (array->type_op == type_array));
1669   return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1670 }
1671
1672 void  set_array_element_entity (ir_type *array, entity *ent) {
1673   assert(array && (array->type_op == type_array));
1674   assert((get_entity_type(ent)->type_op != type_method));
1675   array->attr.aa.element_ent = ent;
1676   array->attr.aa.element_type = get_entity_type(ent);
1677 }
1678 entity *get_array_element_entity (const ir_type *array) {
1679   assert(array && (array->type_op == type_array));
1680   return array->attr.aa.element_ent;
1681 }
1682
1683 /* typecheck */
1684 int (is_Array_type)(const ir_type *array) {
1685   return _is_array_type(array);
1686 }
1687
1688 void set_array_size_bits(ir_type *tp, int size) {
1689   /* FIXME: Here we should make some checks with the element type size */
1690   tp->size = size;
1691 }
1692 /*-----------------------------------------------------------------*/
1693 /* TYPE_ENUMERATION                                                */
1694 /*-----------------------------------------------------------------*/
1695
1696 /* create a new type enumeration -- set the enumerators independently */
1697 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1698   ir_type *res = new_type(type_enumeration, NULL, name, db);
1699
1700   res->attr.ea.n_enums     = n_enums;
1701   res->attr.ea.enumer      = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1702   res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1703   hook_new_type(res);
1704   return res;
1705 }
1706
1707 ir_type *new_type_enumeration(ident *name, int n_enums) {
1708   return new_d_type_enumeration(name, n_enums, NULL);
1709 }
1710
1711 void free_enumeration_entities(ir_type *enumeration) {
1712   assert(enumeration && (enumeration->type_op == type_enumeration));
1713 }
1714 void free_enumeration_attrs(ir_type *enumeration) {
1715   assert(enumeration && (enumeration->type_op == type_enumeration));
1716   free(enumeration->attr.ea.enumer);
1717   free(enumeration->attr.ea.enum_nameid);
1718 }
1719
1720 /* manipulate fields of enumeration type. */
1721 int     get_enumeration_n_enums (const ir_type *enumeration) {
1722   assert(enumeration && (enumeration->type_op == type_enumeration));
1723   return enumeration->attr.ea.n_enums;
1724 }
1725 void    set_enumeration_enum    (ir_type *enumeration, int pos, tarval *con) {
1726   assert(enumeration && (enumeration->type_op == type_enumeration));
1727   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1728   enumeration->attr.ea.enumer[pos] = con;
1729 }
1730 tarval *get_enumeration_enum    (const ir_type *enumeration, int pos) {
1731   assert(enumeration && (enumeration->type_op == type_enumeration));
1732   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1733   return enumeration->attr.ea.enumer[pos];
1734 }
1735 void    set_enumeration_nameid  (ir_type *enumeration, int pos, ident *id) {
1736   assert(enumeration && (enumeration->type_op == type_enumeration));
1737   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1738   enumeration->attr.ea.enum_nameid[pos] = id;
1739 }
1740 ident  *get_enumeration_nameid  (const ir_type *enumeration, int pos) {
1741   assert(enumeration && (enumeration->type_op == type_enumeration));
1742   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1743   return enumeration->attr.ea.enum_nameid[pos];
1744 }
1745 const char *get_enumeration_name(const ir_type *enumeration, int pos) {
1746   assert(enumeration && (enumeration->type_op == type_enumeration));
1747   assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1748   return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1749 }
1750
1751 /* typecheck */
1752 int (is_Enumeration_type)(const ir_type *enumeration) {
1753   return _is_enumeration_type(enumeration);
1754 }
1755
1756 void set_enumeration_mode(ir_type *tp, ir_mode *mode) {
1757   assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1758   /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1759   assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1760
1761   tp->size = get_mode_size_bits(mode);
1762   tp->mode = mode;
1763 }
1764
1765 /*-----------------------------------------------------------------*/
1766 /* TYPE_POINTER                                                    */
1767 /*-----------------------------------------------------------------*/
1768
1769 /* Create a new type pointer */
1770 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1771   ir_type *res;
1772
1773   assert(mode_is_reference(ptr_mode));
1774   res = new_type(type_pointer, ptr_mode, name, db);
1775   res->attr.pa.points_to = points_to;
1776   assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1777   res->size = get_mode_size_bits(res->mode);
1778   res->flags |= tf_layout_fixed;
1779   hook_new_type(res);
1780   return res;
1781 }
1782
1783 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode) {
1784   return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1785 }
1786
1787 void free_pointer_entities (ir_type *pointer) {
1788   assert(pointer && (pointer->type_op == type_pointer));
1789 }
1790
1791 void free_pointer_attrs (ir_type *pointer) {
1792   assert(pointer && (pointer->type_op == type_pointer));
1793 }
1794
1795 /* manipulate fields of type_pointer */
1796 void  set_pointer_points_to_type (ir_type *pointer, ir_type *tp) {
1797   assert(pointer && (pointer->type_op == type_pointer));
1798   pointer->attr.pa.points_to = tp;
1799 }
1800
1801 ir_type *get_pointer_points_to_type (ir_type *pointer) {
1802   assert(pointer && (pointer->type_op == type_pointer));
1803   return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1804 }
1805
1806 /* typecheck */
1807 int (is_Pointer_type)(const ir_type *pointer) {
1808   return _is_pointer_type(pointer);
1809 }
1810
1811 void set_pointer_mode(ir_type *tp, ir_mode *mode) {
1812   assert(mode_is_reference(mode) && "Modes of pointers must be references");
1813   /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1814   assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1815
1816   tp->size = get_mode_size_bits(mode);
1817   tp->mode = mode;
1818 }
1819
1820 /* Returns the first pointer type that has as points_to tp.
1821  *  Not efficient: O(#types).
1822  *  If not found returns firm_unknown_type. */
1823 ir_type *find_pointer_type_to_type (ir_type *tp) {
1824   int i, n = get_irp_n_types();
1825   for (i = 0; i < n; ++i) {
1826     ir_type *found = get_irp_type(i);
1827     if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1828       return (found);
1829   }
1830   return firm_unknown_type;
1831 }
1832
1833
1834 /*-----------------------------------------------------------------*/
1835 /* TYPE_PRIMITIVE                                                  */
1836 /*-----------------------------------------------------------------*/
1837
1838 /* create a new type primitive */
1839 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1840   ir_type *res;
1841   /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1842   res = new_type(type_primitive, mode, name, db);
1843   res->size  = get_mode_size_bits(mode);
1844   res->flags |= tf_layout_fixed;
1845   hook_new_type(res);
1846   return res;
1847 }
1848
1849 ir_type *new_type_primitive(ident *name, ir_mode *mode) {
1850   return new_d_type_primitive(name, mode, NULL);
1851 }
1852
1853 /* typecheck */
1854 int (is_Primitive_type)(const ir_type *primitive) {
1855   return _is_primitive_type(primitive);
1856 }
1857
1858 void set_primitive_mode(ir_type *tp, ir_mode *mode) {
1859   /* Modes of primitives must be data */
1860   assert(mode_is_data(mode));
1861
1862   /* For primitive size depends on the mode. */
1863   tp->size = get_mode_size_bits(mode);
1864   tp->mode = mode;
1865 }
1866
1867
1868 /*-----------------------------------------------------------------*/
1869 /* common functionality                                            */
1870 /*-----------------------------------------------------------------*/
1871
1872
1873 int (is_atomic_type)(const ir_type *tp) {
1874   return _is_atomic_type(tp);
1875 }
1876
1877 /*
1878  * Gets the number of elements in a firm compound type.
1879  */
1880 int get_compound_n_members(const ir_type *tp)
1881 {
1882   const tp_op *op = get_type_tpop(tp);
1883   int res = 0;
1884
1885   if (op->ops.get_n_members)
1886     res = op->ops.get_n_members(tp);
1887   else
1888     assert(0 && "no member count for this type");
1889
1890   return res;
1891 }
1892
1893 /*
1894  * Gets the member of a firm compound type at position pos.
1895  */
1896 entity *get_compound_member(const ir_type *tp, int pos)
1897 {
1898   const tp_op *op = get_type_tpop(tp);
1899   entity *res = NULL;
1900
1901   if (op->ops.get_member)
1902     res = op->ops.get_member(tp, pos);
1903   else
1904     assert(0 && "no members in this type");
1905
1906   return res;
1907 }
1908
1909 /* Returns index of member in tp, -1 if not contained. */
1910 int get_compound_member_index(const ir_type *tp, entity *member)
1911 {
1912   const tp_op *op = get_type_tpop(tp);
1913   int index = -1;
1914
1915   if (op->ops.get_member_index)
1916     index = op->ops.get_member_index(tp, member);
1917   else
1918     assert(0 && "no members in this type");
1919
1920   return index;
1921 }
1922
1923 int is_compound_type(const ir_type *tp) {
1924   assert(tp && tp->kind == k_type);
1925   return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1926 }
1927
1928 /* Checks, whether a type is a frame type */
1929 int is_frame_type(const ir_type *tp) {
1930   return tp->flags & tf_frame_type;
1931 }
1932
1933 /* Checks, whether a type is a value parameter type */
1934 int is_value_param_type(const ir_type *tp) {
1935   return tp->flags & tf_value_param_type;
1936 }
1937
1938 /* Checks, whether a type is a lowered type */
1939 int is_lowered_type(const ir_type *tp) {
1940   return tp->flags & tf_lowered_type;
1941 }
1942
1943 /* Makes a new frame type. */
1944 ir_type *new_type_frame(ident *name)
1945 {
1946   ir_type *res = new_type_class(name);
1947
1948   res->flags |= tf_frame_type;
1949
1950   /* Remove type from type list.  Must be treated differently than other types. */
1951   remove_irp_type(res);
1952
1953   return res;
1954 }
1955
1956 /* Sets a lowered type for a type. This sets both associations. */
1957 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
1958   assert(is_type(tp) && is_type(lowered_type));
1959   lowered_type->flags |= tf_lowered_type;
1960   tp->assoc_type = lowered_type;
1961   lowered_type->assoc_type = tp;
1962 }
1963
1964 /*
1965  * Gets the lowered/unlowered type of a type or NULL if this type
1966  * has no lowered/unlowered one.
1967  */
1968 ir_type *get_associated_type(const ir_type *tp) {
1969   return tp->assoc_type;
1970 }
1971
1972 /* set the type size for the unknown and none ir_type */
1973 void set_default_size_bits(ir_type *tp, int size) {
1974   tp->size = size;
1975 }
1976
1977 /*
1978  * Allocate an area of size bytes aligned at alignment
1979  * at the start or the end of a frame type.
1980  * The frame type must have already an fixed layout.
1981  */
1982 entity *frame_alloc_area(ir_type *frame_type, int size, int alignment, int at_start)
1983 {
1984   entity *area;
1985   ir_type *tp;
1986   ident *name;
1987   char buf[32];
1988   int frame_align, i, offset, frame_size;
1989   static unsigned area_cnt = 0;
1990   static ir_type *a_byte = NULL;
1991
1992   assert(is_frame_type(frame_type));
1993   assert(get_type_state(frame_type) == layout_fixed);
1994
1995   if (! a_byte)
1996     a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
1997
1998   snprintf(buf, sizeof(buf), "area%u", area_cnt++);
1999   name = new_id_from_str(buf);
2000
2001   /* align the size */
2002   frame_align = get_type_alignment_bytes(frame_type);
2003   size = (size + frame_align - 1) & -frame_align;
2004
2005   tp = new_type_array(mangle_u(get_type_ident(frame_type), name), 1, a_byte);
2006   set_array_bounds_int(tp, 0, 0, size);
2007   set_type_alignment_bytes(tp, alignment);
2008
2009   frame_size = get_type_size_bytes(frame_type);
2010   if (at_start) {
2011     /* fix all offsets so far */
2012     for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2013       entity *ent = get_class_member(frame_type, i);
2014
2015       set_entity_offset_bytes(ent, get_entity_offset_bytes(ent) + size);
2016     }
2017     /* calculate offset and new type size */
2018     offset = 0;
2019     frame_size += size;
2020   }
2021   else {
2022     /* calculate offset and new type size */
2023     offset = (frame_size + alignment - 1) & -alignment;
2024     frame_size = offset + size;
2025   }
2026
2027   area = new_entity(frame_type, name, tp);
2028   set_entity_offset_bytes(area, offset);
2029   set_type_size_bytes(frame_type, frame_size);
2030
2031   return area;
2032 }