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