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