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