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