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