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