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