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