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