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