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