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