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