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