3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
6 * Modified by: Michael Beck
9 * Copyright: (c) 2001-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 * Implementation of the datastructure to hold
20 * (C) 2001-2006 by Universitaet Karlsruhe
21 * Goetz Lindenmaier, Michael Beck
23 * This module supplies a datastructure to represent all types
24 * known in the compiled program. This includes types specified
25 * in the program as well as types defined by the language. In the
26 * view of the intermediate representation there is no difference
27 * between these types.
29 * There exist several kinds of types, arranged by the structure of
30 * the type. A type is described by a set of attributes. Some of
31 * these attributes are common to all types, others depend on the
34 * Types are different from the modes defined in irmode: Types are
35 * on the level of the programming language, modes at the level of
36 * the target processor.
38 * @see type_t.h type tpop
63 # include "irprog_t.h"
66 # include "typegmod.h"
74 /*-----------------------------------------------------------------*/
76 /*-----------------------------------------------------------------*/
78 ir_type *firm_none_type; ir_type *get_none_type(void) { return firm_none_type; }
79 ir_type *firm_unknown_type; ir_type *get_unknown_type(void) { return firm_unknown_type; }
82 /* Suffixes added to types used for pass-by-value representations. */
83 static ident *value_params_suffix = NULL;
84 static ident *value_ress_suffix = NULL;
86 /** The default calling convention for method types. */
87 static unsigned default_cc_mask;
89 /* return the default calling convention for method types */
90 unsigned get_default_cc_mask(void) {
91 return default_cc_mask;
94 /* Initialize the type module. */
95 void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask)
97 default_cc_mask = def_cc_mask;
98 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
99 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
101 /* construct none and unknown type. */
102 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"), builtin_db);
103 set_type_size_bits(firm_none_type, 0);
104 set_type_state (firm_none_type, layout_fixed);
105 remove_irp_type(firm_none_type);
107 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
108 set_type_size_bits(firm_unknown_type, 0);
109 set_type_state (firm_unknown_type, layout_fixed);
110 remove_irp_type(firm_unknown_type);
113 /** the global type visited flag */
114 unsigned long firm_type_visited;
116 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
117 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
118 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
121 * Creates a new type representation.
124 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
128 assert(type_op != type_id);
129 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
131 node_size = offsetof(ir_type, attr) + type_op->attr_size;
132 res = xmalloc(node_size);
133 memset(res, 0, node_size);
136 res->type_op = type_op;
139 res->visibility = visibility_external_allocated;
140 res->flags = tf_none;
146 res->assoc_type = NULL;
148 res->nr = get_irp_new_node_nr();
149 #endif /* defined DEBUG_libfirm */
151 add_irp_type(res); /* Remember the new type global. */
156 void free_type(ir_type *tp) {
157 const tp_op *op = get_type_tpop(tp);
159 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
161 /* Remove from list of all types */
163 /* Free the attributes of the type. */
165 /* Free entities automatically allocated with the ir_type */
166 if (op->ops.free_auto_entities)
167 op->ops.free_auto_entities(tp);
168 /* And now the type itself... */
173 void free_type_entities(ir_type *tp) {
174 const tp_op *tpop = get_type_tpop(tp);
176 if (tpop->ops.free_entities)
177 tpop->ops.free_entities(tp);
180 void free_type_attrs(ir_type *tp) {
181 const tp_op *tpop = get_type_tpop(tp);
183 if (tpop->ops.free_attrs)
184 tpop->ops.free_attrs(tp);
187 /* set/get the link field */
188 void *(get_type_link)(const ir_type *tp) {
189 return _get_type_link(tp);
192 void (set_type_link)(ir_type *tp, void *l) {
193 _set_type_link(tp, l);
196 const tp_op *(get_type_tpop)(const ir_type *tp) {
197 return _get_type_tpop(tp);
200 ident *(get_type_tpop_nameid)(const ir_type *tp) {
201 return _get_type_tpop_nameid(tp);
204 const char* get_type_tpop_name(const ir_type *tp) {
205 assert(tp && tp->kind == k_type);
206 return get_id_str(tp->type_op->name);
209 tp_opcode (get_type_tpop_code)(const ir_type *tp) {
210 return _get_type_tpop_code(tp);
213 ir_mode *(get_type_mode)(const ir_type *tp) {
214 return _get_type_mode(tp);
217 void set_type_mode(ir_type *tp, ir_mode *mode) {
218 const tp_op *tpop = get_type_tpop(tp);
220 if (tpop->ops.set_type_mode)
221 tpop->ops.set_type_mode(tp, mode);
223 assert(0 && "setting a mode is NOT allowed for this type");
226 ident *(get_type_ident)(const ir_type *tp) {
227 return _get_type_ident(tp);
230 void (set_type_ident)(ir_type *tp, ident* id) {
231 _set_type_ident(tp, id);
234 /* Outputs a unique number for this node */
235 long get_type_nr(const ir_type *tp) {
240 return (long)PTR_TO_INT(tp);
244 const char* get_type_name(const ir_type *tp) {
245 assert(tp && tp->kind == k_type);
246 return (get_id_str(tp->name));
249 int (get_type_size_bytes)(const ir_type *tp) {
250 return _get_type_size_bytes(tp);
253 int (get_type_size_bits)(const ir_type *tp) {
254 return _get_type_size_bits(tp);
258 visibility get_type_visibility (const ir_type *tp) {
260 visibility res = visibility_local;
261 if (is_compound_type(tp)) {
263 if (is_Array_type(tp)) {
264 entity *mem = get_array_element_entity(tp);
265 if (get_entity_visibility(mem) != visibility_local)
266 res = visibility_external_visible;
268 int i, n_mems = get_compound_n_members(tp);
269 for (i = 0; i < n_mems; ++i) {
270 entity *mem = get_compound_member(tp, i);
271 if (get_entity_visibility(mem) != visibility_local)
272 res = visibility_external_visible;
279 return tp->visibility;
282 void set_type_visibility (ir_type *tp, visibility v) {
285 /* check for correctness */
286 if (v != visibility_external_allocated) {
287 visibility res = visibility_local;
288 if (is_compound_type(tp)) {
289 if (is_Array_type(tp)) {
290 entity *mem = get_array_element_entity(tp);
291 if (get_entity_visibility(mem) > res)
292 res = get_entity_visibility(mem);
294 int i, n_mems = get_compound_n_members(tp);
295 for (i = 0; i < n_mems; ++i) {
296 entity *mem = get_compound_member(tp, i);
297 if (get_entity_visibility(mem) > res)
298 res = get_entity_visibility(mem);
309 set_type_size_bits(ir_type *tp, int size) {
310 const tp_op *tpop = get_type_tpop(tp);
312 if (tpop->ops.set_type_size)
313 tpop->ops.set_type_size(tp, size);
315 assert(0 && "Cannot set size for this type");
319 set_type_size_bytes(ir_type *tp, int size) {
320 set_type_size_bits(tp, 8*size);
323 int get_type_alignment_bytes(ir_type *tp) {
324 int align = get_type_alignment_bits(tp);
326 return align < 0 ? align : (align + 7) >> 3;
329 int get_type_alignment_bits(ir_type *tp) {
335 /* alignment NOT set calculate it "on demand" */
337 align = get_mode_size_bits(tp->mode);
338 else if (is_Array_type(tp))
339 align = get_type_alignment_bits(get_array_element_type(tp));
340 else if (is_compound_type(tp)) {
341 int i, n = get_compound_n_members(tp);
344 for (i = 0; i < n; ++i) {
345 ir_type *t = get_entity_type(get_compound_member(tp, i));
346 int a = get_type_alignment_bits(t);
352 else if (is_Method_type(tp))
362 set_type_alignment_bits(ir_type *tp, int align) {
363 assert(tp && tp->kind == k_type);
364 assert((align == -1 || (align & (align - 1)) == 0) && "type alignment not power of two");
365 /* Methods don't have an alignment. */
366 if (tp->type_op != type_method) {
372 set_type_alignment_bytes(ir_type *tp, int align) {
374 set_type_alignment_bits(tp, -1);
376 set_type_alignment_bits(tp, 8*align);
380 /* Returns a human readable string for the enum entry. */
381 const char *get_type_state_name(type_state s) {
382 #define X(a) case a: return #a;
392 type_state (get_type_state)(const ir_type *tp) {
393 return _get_type_state(tp);
397 set_type_state(ir_type *tp, type_state state) {
398 assert(tp && tp->kind == k_type);
400 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
401 (tp->type_op == type_method))
404 /* Just a correctness check: */
405 if (state == layout_fixed) {
407 switch (get_type_tpop_code(tp)) {
409 assert(get_type_size_bits(tp) > -1);
410 if (tp != get_glob_type()) {
411 int n_mem = get_class_n_members(tp);
412 for (i = 0; i < n_mem; i++) {
413 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
414 { DDMT(tp); DDME(get_class_member(tp, i)); }
415 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
417 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
418 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
424 assert(get_type_size_bits(tp) > -1);
425 for (i = 0; i < get_struct_n_members(tp); i++) {
426 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
427 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
436 Assure that only innermost dimension is dynamic? */
438 case tpo_enumeration:
439 assert(get_type_mode != NULL);
440 for (i = 0; i < get_enumeration_n_enums(tp); i++)
441 assert(get_enumeration_enum(tp, i) != NULL);
446 if (state == layout_fixed)
447 tp->flags |= tf_layout_fixed;
449 tp->flags &= ~tf_layout_fixed;
452 unsigned long (get_type_visited)(const ir_type *tp) {
453 return _get_type_visited(tp);
456 void (set_type_visited)(ir_type *tp, unsigned long num) {
457 _set_type_visited(tp, num);
460 /* Sets visited field in type to type_visited. */
461 void (mark_type_visited)(ir_type *tp) {
462 _mark_type_visited(tp);
465 int (type_visited)(const ir_type *tp) {
466 return _type_visited(tp);
469 int (type_not_visited)(const ir_type *tp) {
470 return _type_not_visited(tp);
473 int (is_type)(const void *thing) {
474 return _is_type(thing);
477 /* Checks whether two types are structural equal.*/
478 int equal_type(ir_type *typ1, ir_type *typ2) {
483 if (typ1 == typ2) return 1;
485 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
486 (get_type_ident(typ1) != get_type_ident(typ2)) ||
487 (get_type_mode(typ1) != get_type_mode(typ2)) ||
488 (get_type_state(typ1) != get_type_state(typ2)))
490 if ((get_type_state(typ1) == layout_fixed) &&
491 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
494 switch (get_type_tpop_code(typ1)) {
496 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
497 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
498 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
499 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
500 /** Compare the members **/
501 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
502 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
503 /* First sort the members of typ2 */
504 for (i = 0; i < get_class_n_members(typ1); i++) {
505 entity *e1 = get_class_member(typ1, i);
506 for (j = 0; j < get_class_n_members(typ2); j++) {
507 entity *e2 = get_class_member(typ2, j);
508 if (get_entity_name(e1) == get_entity_name(e2))
512 for (i = 0; i < get_class_n_members(typ1); i++) {
513 if (!m[i] || /* Found no counterpart */
514 !equal_entity(get_class_member(typ1, i), m[i]))
517 /** Compare the supertypes **/
518 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
519 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
520 /* First sort the supertypes of typ2 */
521 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
522 ir_type *t1 = get_class_supertype(typ1, i);
523 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
524 ir_type *t2 = get_class_supertype(typ2, j);
525 if (get_type_ident(t2) == get_type_ident(t1))
529 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
530 if (!t[i] || /* Found no counterpart */
531 get_class_supertype(typ1, i) != t[i])
536 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
537 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
538 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
539 /* First sort the members of lt */
540 for (i = 0; i < get_struct_n_members(typ1); i++) {
541 entity *e1 = get_struct_member(typ1, i);
542 for (j = 0; j < get_struct_n_members(typ2); j++) {
543 entity *e2 = get_struct_member(typ2, j);
544 if (get_entity_name(e1) == get_entity_name(e2))
548 for (i = 0; i < get_struct_n_members(typ1); i++) {
549 if (!m[i] || /* Found no counterpart */
550 !equal_entity(get_struct_member(typ1, i), m[i]))
555 int n_param1, n_param2;
557 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
558 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
559 if (get_method_calling_convention(typ1) !=
560 get_method_calling_convention(typ2)) return 0;
562 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
563 n_param1 = get_method_n_params(typ1);
564 n_param2 = get_method_n_params(typ2);
567 n_param1 = get_method_first_variadic_param_index(typ1);
568 n_param2 = get_method_first_variadic_param_index(typ2);
571 if (n_param1 != n_param2) return 0;
573 for (i = 0; i < n_param1; i++) {
574 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
577 for (i = 0; i < get_method_n_ress(typ1); i++) {
578 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
583 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
584 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
585 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
586 /* First sort the members of lt */
587 for (i = 0; i < get_union_n_members(typ1); i++) {
588 entity *e1 = get_union_member(typ1, i);
589 for (j = 0; j < get_union_n_members(typ2); j++) {
590 entity *e2 = get_union_member(typ2, j);
591 if (get_entity_name(e1) == get_entity_name(e2))
595 for (i = 0; i < get_union_n_members(typ1); i++) {
596 if (!m[i] || /* Found no counterpart */
597 !equal_entity(get_union_member(typ1, i), m[i]))
602 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
604 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
606 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
607 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
608 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
610 if (get_array_order(typ1, i) != get_array_order(typ2, i))
611 assert(0 && "type compare with different dimension orders not implemented");
614 case tpo_enumeration: {
615 assert(0 && "enumerations not implemented");
618 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
621 case tpo_primitive: {
628 /* Checks whether two types are structural comparable. */
629 int smaller_type (ir_type *st, ir_type *lt) {
633 if (st == lt) return 1;
635 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
638 switch(get_type_tpop_code(st)) {
640 return is_SubClass_of(st, lt);
643 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
644 m = alloca(sizeof(entity *) * get_struct_n_members(st));
645 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
646 /* First sort the members of lt */
647 for (i = 0; i < get_struct_n_members(st); i++) {
648 entity *se = get_struct_member(st, i);
649 for (j = 0; j < get_struct_n_members(lt); j++) {
650 entity *le = get_struct_member(lt, j);
651 if (get_entity_name(le) == get_entity_name(se))
655 for (i = 0; i < get_struct_n_members(st); i++) {
656 if (!m[i] || /* Found no counterpart */
657 !smaller_type(get_entity_type(get_struct_member(st, i)),
658 get_entity_type(m[i])))
663 int n_param1, n_param2;
665 /** FIXME: is this still 1? */
666 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
667 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
668 if (get_method_calling_convention(st) !=
669 get_method_calling_convention(lt)) return 0;
671 if (get_method_variadicity(st) == variadicity_non_variadic) {
672 n_param1 = get_method_n_params(st);
673 n_param2 = get_method_n_params(lt);
676 n_param1 = get_method_first_variadic_param_index(st);
677 n_param2 = get_method_first_variadic_param_index(lt);
680 if (n_param1 != n_param2) return 0;
682 for (i = 0; i < get_method_n_params(st); i++) {
683 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
686 for (i = 0; i < get_method_n_ress(st); i++) {
687 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
692 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
693 m = alloca(sizeof(entity *) * get_union_n_members(st));
694 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
695 /* First sort the members of lt */
696 for (i = 0; i < get_union_n_members(st); i++) {
697 entity *se = get_union_member(st, i);
698 for (j = 0; j < get_union_n_members(lt); j++) {
699 entity *le = get_union_member(lt, j);
700 if (get_entity_name(le) == get_entity_name(se))
704 for (i = 0; i < get_union_n_members(st); i++) {
705 if (!m[i] || /* Found no counterpart */
706 !smaller_type(get_entity_type(get_union_member(st, i)),
707 get_entity_type(m[i])))
712 ir_type *set, *let; /* small/large elt. ir_type */
713 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
715 set = get_array_element_type(st);
716 let = get_array_element_type(lt);
718 /* If the element types are different, set must be convertible
719 to let, and they must have the same size so that address
720 computations work out. To have a size the layout must
722 if ((get_type_state(set) != layout_fixed) ||
723 (get_type_state(let) != layout_fixed))
725 if (!smaller_type(set, let) ||
726 get_type_size_bits(set) != get_type_size_bits(let))
729 for(i = 0; i < get_array_n_dimensions(st); i++) {
730 if (get_array_lower_bound(lt, i))
731 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
733 if (get_array_upper_bound(lt, i))
734 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
738 case tpo_enumeration: {
739 assert(0 && "enumerations not implemented");
742 if (!smaller_type(get_pointer_points_to_type(st),
743 get_pointer_points_to_type(lt)))
746 case tpo_primitive: {
747 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
755 /*-----------------------------------------------------------------*/
757 /*-----------------------------------------------------------------*/
759 /* create a new class ir_type */
760 ir_type *new_d_type_class (ident *name, dbg_info *db) {
763 res = new_type(type_class, NULL, name, db);
765 res->attr.ca.members = NEW_ARR_F (entity *, 0);
766 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
767 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
768 res->attr.ca.peculiarity = peculiarity_existent;
769 res->attr.ca.type_info = NULL;
770 res->attr.ca.vtable_size = 0;
771 res->attr.ca.clss_flags = cf_none;
772 res->attr.ca.dfn = 0;
777 ir_type *new_type_class (ident *name) {
778 return new_d_type_class (name, NULL);
781 /* free all entities of a class */
782 void free_class_entities(ir_type *clss) {
784 assert(clss && (clss->type_op == type_class));
785 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
786 free_entity(get_class_member(clss, i));
787 /* do NOT free the type info here. It belongs to another class */
790 void free_class_attrs(ir_type *clss) {
791 assert(clss && (clss->type_op == type_class));
792 DEL_ARR_F(clss->attr.ca.members);
793 DEL_ARR_F(clss->attr.ca.subtypes);
794 DEL_ARR_F(clss->attr.ca.supertypes);
797 /* manipulate private fields of class type */
798 void add_class_member (ir_type *clss, entity *member) {
799 assert(clss && (clss->type_op == type_class));
800 assert(clss != get_entity_type(member) && "recursive type");
801 ARR_APP1 (entity *, clss->attr.ca.members, member);
804 int (get_class_n_members) (const ir_type *clss) {
805 return _get_class_n_members(clss);
808 int get_class_member_index(const ir_type *clss, entity *mem) {
810 assert(clss && (clss->type_op == type_class));
811 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
812 if (get_class_member(clss, i) == mem)
817 entity *(get_class_member) (const ir_type *clss, int pos) {
818 return _get_class_member(clss, pos);
821 entity *get_class_member_by_name(ir_type *clss, ident *name) {
823 assert(clss && (clss->type_op == type_class));
824 n_mem = get_class_n_members(clss);
825 for (i = 0; i < n_mem; ++i) {
826 entity *mem = get_class_member(clss, i);
827 if (get_entity_ident(mem) == name) return mem;
832 void set_class_member (ir_type *clss, entity *member, int pos) {
833 assert(clss && (clss->type_op == type_class));
834 assert(pos >= 0 && pos < get_class_n_members(clss));
835 clss->attr.ca.members[pos] = member;
837 void set_class_members (ir_type *clss, entity **members, int arity) {
839 assert(clss && (clss->type_op == type_class));
840 DEL_ARR_F(clss->attr.ca.members);
841 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
842 for (i = 0; i < arity; i++) {
843 set_entity_owner(members[i], clss);
844 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
847 void remove_class_member(ir_type *clss, entity *member) {
849 assert(clss && (clss->type_op == type_class));
850 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
851 if (clss->attr.ca.members[i] == member) {
852 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
853 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
854 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
860 void add_class_subtype (ir_type *clss, ir_type *subtype) {
862 assert(clss && (clss->type_op == type_class));
863 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
864 for (i = 0; i < get_class_n_supertypes(subtype); i++)
865 if (get_class_supertype(subtype, i) == clss)
866 /* Class already registered */
868 ARR_APP1 (ir_type *, subtype->attr.ca.supertypes, clss);
870 int get_class_n_subtypes (const ir_type *clss) {
871 assert(clss && (clss->type_op == type_class));
872 return (ARR_LEN (clss->attr.ca.subtypes));
874 ir_type *get_class_subtype (ir_type *clss, int pos) {
875 assert(clss && (clss->type_op == type_class));
876 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
877 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
879 int get_class_subtype_index(ir_type *clss, const ir_type *subclass) {
880 int i, n_subtypes = get_class_n_subtypes(clss);
881 assert(is_Class_type(subclass));
882 for (i = 0; i < n_subtypes; ++i) {
883 if (get_class_subtype(clss, i) == subclass) return i;
887 void set_class_subtype (ir_type *clss, ir_type *subtype, int pos) {
888 assert(clss && (clss->type_op == type_class));
889 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
890 clss->attr.ca.subtypes[pos] = subtype;
892 void remove_class_subtype(ir_type *clss, ir_type *subtype) {
894 assert(clss && (clss->type_op == type_class));
895 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
896 if (clss->attr.ca.subtypes[i] == subtype) {
897 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
898 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
899 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
904 void add_class_supertype (ir_type *clss, ir_type *supertype) {
906 assert(clss && (clss->type_op == type_class));
907 assert(supertype && (supertype -> type_op == type_class));
908 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
909 for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
910 if (get_class_subtype(supertype, i) == clss)
911 /* Class already registered */
913 ARR_APP1 (ir_type *, supertype->attr.ca.subtypes, clss);
915 int get_class_n_supertypes (const ir_type *clss) {
916 assert(clss && (clss->type_op == type_class));
917 return (ARR_LEN (clss->attr.ca.supertypes));
919 int get_class_supertype_index(ir_type *clss, ir_type *super_clss) {
920 int i, n_supertypes = get_class_n_supertypes(clss);
921 assert(super_clss && (super_clss->type_op == type_class));
922 for (i = 0; i < n_supertypes; i++)
923 if (get_class_supertype(clss, i) == super_clss)
927 ir_type *get_class_supertype (ir_type *clss, int pos) {
928 assert(clss && (clss->type_op == type_class));
929 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
930 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
932 void set_class_supertype (ir_type *clss, ir_type *supertype, int pos) {
933 assert(clss && (clss->type_op == type_class));
934 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
935 clss->attr.ca.supertypes[pos] = supertype;
937 void remove_class_supertype(ir_type *clss, ir_type *supertype) {
939 assert(clss && (clss->type_op == type_class));
940 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
941 if (clss->attr.ca.supertypes[i] == supertype) {
942 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
943 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
944 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
948 entity *get_class_type_info(const ir_type *clss) {
949 return clss->attr.ca.type_info;
951 void set_class_type_info(ir_type *clss, entity *ent) {
952 clss->attr.ca.type_info = ent;
955 const char *get_peculiarity_name(peculiarity p) {
956 #define X(a) case a: return #a
958 X(peculiarity_description);
959 X(peculiarity_inherited);
960 X(peculiarity_existent);
963 return "invalid peculiarity";
966 peculiarity get_class_peculiarity (const ir_type *clss) {
967 assert(clss && (clss->type_op == type_class));
968 return clss->attr.ca.peculiarity;
971 void set_class_peculiarity (ir_type *clss, peculiarity pec) {
972 assert(clss && (clss->type_op == type_class));
973 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
974 clss->attr.ca.peculiarity = pec;
977 /* Returns the size of the virtual function table. */
978 unsigned (get_class_vtable_size)(const ir_type *clss) {
979 return _get_class_vtable_size(clss);
982 /* Sets a new size of the virtual function table. */
983 void (set_class_vtable_size)(ir_type *clss, unsigned size) {
984 _set_class_vtable_size(clss, size);
987 /* Returns non-zero if a class is final. */
988 int (is_class_final)(const ir_type *clss) {
989 return _is_class_final(clss);
992 /* Sets if a class is final. */
993 void (set_class_final)(ir_type *clss, int flag) {
994 _set_class_final(clss, flag);
997 /* Returns non-zero if a class is an interface. */
998 int (is_class_interface)(const ir_type *clss) {
999 return _is_class_interface(clss);
1002 /* Sets the class interface flag. */
1003 void (set_class_interface)(ir_type *clss, int flag) {
1004 _set_class_interface(clss, flag);
1007 /* Returns non-zero if a class is abstract. */
1008 int (is_class_abstract)(const ir_type *clss) {
1009 return _is_class_abstract(clss);
1012 /* Sets the class abstract flag. */
1013 void (set_class_abstract)(ir_type *clss, int final) {
1014 _set_class_abstract(clss, final);
1017 void set_class_dfn (ir_type *clss, int dfn) {
1018 clss->attr.ca.dfn = dfn;
1021 int get_class_dfn (const ir_type *clss) {
1022 return (clss->attr.ca.dfn);
1026 int (is_Class_type)(const ir_type *clss) {
1027 return _is_class_type(clss);
1030 void set_class_mode(ir_type *tp, ir_mode *mode) {
1031 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1032 assert(get_type_state(tp) == layout_fixed &&
1033 tp->size == get_mode_size_bits(mode) && "mode don't match class layout");
1037 void set_class_size_bits(ir_type *tp, int size) {
1038 /* argh: we must allow to set negative values as "invalid size" */
1039 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1040 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1043 /*----------------------------------------------------------------**/
1045 /*----------------------------------------------------------------**/
1047 /* create a new type struct */
1048 ir_type *new_d_type_struct(ident *name, dbg_info *db) {
1049 ir_type *res = new_type(type_struct, NULL, name, db);
1051 res->attr.sa.members = NEW_ARR_F(entity *, 0);
1056 ir_type *new_type_struct (ident *name) {
1057 return new_d_type_struct (name, NULL);
1060 void free_struct_entities (ir_type *strct) {
1062 assert(strct && (strct->type_op == type_struct));
1063 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1064 free_entity(get_struct_member(strct, i));
1066 void free_struct_attrs (ir_type *strct) {
1067 assert(strct && (strct->type_op == type_struct));
1068 DEL_ARR_F(strct->attr.sa.members);
1071 /* manipulate private fields of struct */
1072 int get_struct_n_members (const ir_type *strct) {
1073 assert(strct && (strct->type_op == type_struct));
1074 return (ARR_LEN (strct->attr.sa.members));
1077 void add_struct_member (ir_type *strct, entity *member) {
1078 assert(strct && (strct->type_op == type_struct));
1079 assert(get_type_tpop(get_entity_type(member)) != type_method);
1080 /* @@@ lowerfirm geht nicht durch */
1081 assert(strct != get_entity_type(member) && "recursive type");
1082 ARR_APP1 (entity *, strct->attr.sa.members, member);
1085 entity *get_struct_member (const ir_type *strct, int pos) {
1086 assert(strct && (strct->type_op == type_struct));
1087 assert(pos >= 0 && pos < get_struct_n_members(strct));
1088 return strct->attr.sa.members[pos];
1091 int get_struct_member_index(const ir_type *strct, entity *mem) {
1093 assert(strct && (strct->type_op == type_struct));
1094 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1095 if (get_struct_member(strct, i) == mem)
1100 void set_struct_member (ir_type *strct, int pos, entity *member) {
1101 assert(strct && (strct->type_op == type_struct));
1102 assert(pos >= 0 && pos < get_struct_n_members(strct));
1103 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1104 strct->attr.sa.members[pos] = member;
1106 void remove_struct_member(ir_type *strct, entity *member) {
1108 assert(strct && (strct->type_op == type_struct));
1109 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1110 if (strct->attr.sa.members[i] == member) {
1111 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1112 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1113 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1119 int (is_Struct_type)(const ir_type *strct) {
1120 return _is_struct_type(strct);
1123 void set_struct_mode(ir_type *tp, ir_mode *mode) {
1124 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1125 assert(get_type_state(tp) == layout_fixed &&
1126 tp->size == get_mode_size_bits(mode) && "mode don't match struct layout");
1130 void set_struct_size_bits(ir_type *tp, int size) {
1131 /* argh: we must allow to set negative values as "invalid size" */
1132 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1133 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1136 /*******************************************************************/
1138 /*******************************************************************/
1141 * Lazy construction of value argument / result representation.
1142 * Constructs a struct type and its member. The types of the members
1143 * are passed in the argument list.
1145 * @param name name of the type constructed
1146 * @param len number of fields
1147 * @param tps array of field types with length len
1149 static INLINE ir_type *
1150 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1152 ir_type *res = new_type_struct(name);
1153 res->flags |= tf_value_param_type;
1154 /* Remove type from type list. Must be treated differently than other types. */
1155 remove_irp_type(res);
1156 for (i = 0; i < len; i++) {
1157 /* use res as default if corresponding type is not yet set. */
1158 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1160 tps[i].ent = new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1161 set_entity_allocation(tps[i].ent, allocation_parameter);
1166 /* Create a new method type.
1167 N_param is the number of parameters, n_res the number of results. */
1168 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1171 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1172 res = new_type(type_method, mode_P_code, name, db);
1173 res->flags |= tf_layout_fixed;
1174 res->size = get_mode_size_bits(mode_P_code);
1175 res->attr.ma.n_params = n_param;
1176 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1177 res->attr.ma.value_params = NULL;
1178 res->attr.ma.n_res = n_res;
1179 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1180 res->attr.ma.value_ress = NULL;
1181 res->attr.ma.variadicity = variadicity_non_variadic;
1182 res->attr.ma.first_variadic_param = -1;
1183 res->attr.ma.additional_properties = mtp_no_property;
1184 res->attr.ma.irg_calling_conv = default_cc_mask;
1189 ir_type *new_type_method(ident *name, int n_param, int n_res) {
1190 return new_d_type_method(name, n_param, n_res, NULL);
1193 void free_method_entities(ir_type *method) {
1194 assert(method && (method->type_op == type_method));
1197 /* Attention: also frees entities in value parameter subtypes! */
1198 void free_method_attrs(ir_type *method) {
1199 assert(method && (method->type_op == type_method));
1200 free(method->attr.ma.param_type);
1201 free(method->attr.ma.res_type);
1202 if (method->attr.ma.value_params) {
1203 free_type_entities(method->attr.ma.value_params);
1204 free_type(method->attr.ma.value_params);
1206 if (method->attr.ma.value_ress) {
1207 free_type_entities(method->attr.ma.value_ress);
1208 free_type(method->attr.ma.value_ress);
1212 /* manipulate private fields of method. */
1213 int (get_method_n_params)(const ir_type *method) {
1214 return _get_method_n_params(method);
1217 ir_type *get_method_param_type(ir_type *method, int pos) {
1219 assert(method && (method->type_op == type_method));
1220 assert(pos >= 0 && pos < get_method_n_params(method));
1221 res = method->attr.ma.param_type[pos].tp;
1222 assert(res != NULL && "empty method param type");
1223 return method->attr.ma.param_type[pos].tp = skip_tid(res);
1226 void set_method_param_type(ir_type *method, int pos, ir_type *tp) {
1227 assert(method && (method->type_op == type_method));
1228 assert(pos >= 0 && pos < get_method_n_params(method));
1229 method->attr.ma.param_type[pos].tp = tp;
1230 /* If information constructed set pass-by-value representation. */
1231 if (method->attr.ma.value_params) {
1232 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1233 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1237 /* Returns an entity that represents the copied value argument. Only necessary
1238 for compounds passed by value. */
1239 entity *get_method_value_param_ent(ir_type *method, int pos) {
1240 assert(method && (method->type_op == type_method));
1241 assert(pos >= 0 && pos < get_method_n_params(method));
1243 if (!method->attr.ma.value_params) {
1244 /* parameter value type not created yet, build */
1245 method->attr.ma.value_params
1246 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1247 get_method_n_params(method), method->attr.ma.param_type);
1250 * build_value_type() sets the method->attr.ma.value_params type as default if
1253 assert((get_entity_type(method->attr.ma.param_type[pos].ent) != method->attr.ma.value_params)
1254 && "param type not yet set");
1255 return method->attr.ma.param_type[pos].ent;
1259 * Returns a type that represents the copied value arguments.
1261 ir_type *get_method_value_param_type(const ir_type *method)
1263 assert(method && (method->type_op == type_method));
1264 return method->attr.ma.value_params;
1267 int (get_method_n_ress)(const ir_type *method) {
1268 return _get_method_n_ress(method);
1271 ir_type *get_method_res_type(ir_type *method, int pos) {
1273 assert(method && (method->type_op == type_method));
1274 assert(pos >= 0 && pos < get_method_n_ress(method));
1275 res = method->attr.ma.res_type[pos].tp;
1276 assert(res != NULL && "empty method return type");
1277 return method->attr.ma.res_type[pos].tp = skip_tid(res);
1280 void set_method_res_type(ir_type *method, int pos, ir_type *tp) {
1281 assert(method && (method->type_op == type_method));
1282 assert(pos >= 0 && pos < get_method_n_ress(method));
1283 /* set the result ir_type */
1284 method->attr.ma.res_type[pos].tp = tp;
1285 /* If information constructed set pass-by-value representation. */
1286 if (method->attr.ma.value_ress) {
1287 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1288 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1292 /* Returns an entity that represents the copied value result. Only necessary
1293 for compounds passed by value. */
1294 entity *get_method_value_res_ent(ir_type *method, int pos) {
1295 assert(method && (method->type_op == type_method));
1296 assert(pos >= 0 && pos < get_method_n_ress(method));
1298 if (!method->attr.ma.value_ress) {
1299 /* result value type not created yet, build */
1300 method->attr.ma.value_ress
1301 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1302 get_method_n_ress(method), method->attr.ma.res_type);
1305 * build_value_type() sets the method->attr.ma.value_ress type as default if
1308 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1309 && "result type not yet set");
1311 return method->attr.ma.res_type[pos].ent;
1315 * Returns a type that represents the copied value results.
1317 ir_type *get_method_value_res_type(const ir_type *method) {
1318 assert(method && (method->type_op == type_method));
1319 return method->attr.ma.value_ress;
1322 /* Returns the null-terminated name of this variadicity. */
1323 const char *get_variadicity_name(variadicity vari)
1325 #define X(a) case a: return #a
1327 X(variadicity_non_variadic);
1328 X(variadicity_variadic);
1335 variadicity get_method_variadicity(const ir_type *method)
1337 assert(method && (method->type_op == type_method));
1338 return method->attr.ma.variadicity;
1341 void set_method_variadicity(ir_type *method, variadicity vari)
1343 assert(method && (method->type_op == type_method));
1344 method->attr.ma.variadicity = vari;
1348 * Returns the first variadic parameter index of a type.
1349 * If this index was NOT set, the index of the last parameter
1350 * of the method type plus one is returned for variadic functions.
1351 * Non-variadic function types always return -1 here.
1353 int get_method_first_variadic_param_index(const ir_type *method)
1355 assert(method && (method->type_op == type_method));
1357 if (method->attr.ma.variadicity == variadicity_non_variadic)
1360 if (method->attr.ma.first_variadic_param == -1)
1361 return get_method_n_params(method);
1362 return method->attr.ma.first_variadic_param;
1366 * Sets the first variadic parameter index. This allows to specify
1367 * a complete call type (containing the type of all parameters)
1368 * but still have the knowledge, which parameter must be passed as
1371 void set_method_first_variadic_param_index(ir_type *method, int index)
1373 assert(method && (method->type_op == type_method));
1374 assert(index >= 0 && index <= get_method_n_params(method));
1376 method->attr.ma.first_variadic_param = index;
1379 unsigned (get_method_additional_properties)(const ir_type *method) {
1380 return _get_method_additional_properties(method);
1383 void (set_method_additional_properties)(ir_type *method, unsigned mask) {
1384 _set_method_additional_properties(method, mask);
1387 void (set_method_additional_property)(ir_type *method, mtp_additional_property flag) {
1388 _set_method_additional_property(method, flag);
1391 /* Returns the calling convention of an entities graph. */
1392 unsigned (get_method_calling_convention)(const ir_type *method) {
1393 return _get_method_calling_convention(method);
1396 /* Sets the calling convention of an entities graph. */
1397 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask) {
1398 _set_method_calling_convention(method, cc_mask);
1401 /* Returns the number of registers parameters, 0 means default. */
1402 unsigned get_method_n_regparams(ir_type *method) {
1403 unsigned cc = get_method_calling_convention(method);
1404 assert(IS_FASTCALL(cc));
1406 return cc & ~cc_bits;
1409 /* Sets the number of registers parameters, 0 means default. */
1410 void set_method_n_regparams(ir_type *method, unsigned n_regs) {
1411 unsigned cc = get_method_calling_convention(method);
1412 assert(IS_FASTCALL(cc));
1414 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1418 int (is_Method_type)(const ir_type *method) {
1419 return _is_method_type(method);
1422 /*-----------------------------------------------------------------*/
1424 /*-----------------------------------------------------------------*/
1426 /* create a new type uni */
1427 ir_type *new_d_type_union(ident *name, dbg_info *db) {
1428 ir_type *res = new_type(type_union, NULL, name, db);
1430 res->attr.ua.members = NEW_ARR_F(entity *, 0);
1435 ir_type *new_type_union(ident *name) {
1436 return new_d_type_union(name, NULL);
1439 void free_union_entities(ir_type *uni) {
1441 assert(uni && (uni->type_op == type_union));
1442 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1443 free_entity(get_union_member(uni, i));
1446 void free_union_attrs (ir_type *uni) {
1447 assert(uni && (uni->type_op == type_union));
1448 DEL_ARR_F(uni->attr.ua.members);
1451 /* manipulate private fields of union */
1452 int get_union_n_members (const ir_type *uni) {
1453 assert(uni && (uni->type_op == type_union));
1454 return (ARR_LEN (uni->attr.ua.members));
1456 void add_union_member (ir_type *uni, entity *member) {
1457 assert(uni && (uni->type_op == type_union));
1458 assert(uni != get_entity_type(member) && "recursive type");
1459 ARR_APP1 (entity *, uni->attr.ua.members, member);
1461 entity *get_union_member (const ir_type *uni, int pos) {
1462 assert(uni && (uni->type_op == type_union));
1463 assert(pos >= 0 && pos < get_union_n_members(uni));
1464 return uni->attr.ua.members[pos];
1466 int get_union_member_index(const ir_type *uni, entity *mem) {
1468 assert(uni && (uni->type_op == type_union));
1469 for (i = 0, n = get_union_n_members(uni); i < n; ++i)
1470 if (get_union_member(uni, i) == mem)
1474 void set_union_member (ir_type *uni, int pos, entity *member) {
1475 assert(uni && (uni->type_op == type_union));
1476 assert(pos >= 0 && pos < get_union_n_members(uni));
1477 uni->attr.ua.members[pos] = member;
1479 void remove_union_member(ir_type *uni, entity *member) {
1481 assert(uni && (uni->type_op == type_union));
1482 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1483 if (uni->attr.ua.members[i] == member) {
1484 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1485 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1486 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1492 int (is_Union_type)(const ir_type *uni) {
1493 return _is_union_type(uni);
1496 void set_union_size_bits(ir_type *tp, int size) {
1497 /* argh: we must allow to set negative values as "invalid size" */
1498 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1499 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1502 /*-----------------------------------------------------------------*/
1504 /*-----------------------------------------------------------------*/
1507 /* create a new type array -- set dimension sizes independently */
1508 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type, dbg_info *db) {
1512 ir_graph *rem = current_ir_graph;
1514 assert(!is_Method_type(element_type));
1516 res = new_type(type_array, NULL, name, db);
1517 res->attr.aa.n_dimensions = n_dimensions;
1518 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1519 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1520 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1522 current_ir_graph = get_const_code_irg();
1523 unk = new_Unknown( mode_Iu);
1524 for (i = 0; i < n_dimensions; i++) {
1525 res->attr.aa.lower_bound[i] =
1526 res->attr.aa.upper_bound[i] = unk;
1527 res->attr.aa.order[i] = i;
1529 current_ir_graph = rem;
1531 res->attr.aa.element_type = element_type;
1532 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1537 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type) {
1538 return new_d_type_array(name, n_dimensions, element_type, NULL);
1541 void free_array_automatic_entities(ir_type *array) {
1542 assert(array && (array->type_op == type_array));
1543 free_entity(get_array_element_entity(array));
1546 void free_array_entities (ir_type *array) {
1547 assert(array && (array->type_op == type_array));
1550 void free_array_attrs (ir_type *array) {
1551 assert(array && (array->type_op == type_array));
1552 free(array->attr.aa.lower_bound);
1553 free(array->attr.aa.upper_bound);
1554 free(array->attr.aa.order);
1557 /* manipulate private fields of array ir_type */
1558 int get_array_n_dimensions (const ir_type *array) {
1559 assert(array && (array->type_op == type_array));
1560 return array->attr.aa.n_dimensions;
1564 set_array_bounds (ir_type *array, int dimension, ir_node * lower_bound,
1565 ir_node * upper_bound) {
1566 assert(array && (array->type_op == type_array));
1567 assert(lower_bound && "lower_bound node may not be NULL.");
1568 assert(upper_bound && "upper_bound node may not be NULL.");
1569 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1570 array->attr.aa.lower_bound[dimension] = lower_bound;
1571 array->attr.aa.upper_bound[dimension] = upper_bound;
1574 set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
1576 ir_graph *rem = current_ir_graph;
1577 current_ir_graph = get_const_code_irg();
1578 set_array_bounds (array, dimension,
1579 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1580 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1581 current_ir_graph = rem;
1584 set_array_lower_bound (ir_type *array, int dimension, ir_node * lower_bound) {
1585 assert(array && (array->type_op == type_array));
1586 assert(lower_bound && "lower_bound node may not be NULL.");
1587 array->attr.aa.lower_bound[dimension] = lower_bound;
1589 void set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound) {
1590 ir_graph *rem = current_ir_graph;
1591 current_ir_graph = get_const_code_irg();
1592 set_array_lower_bound (array, dimension,
1593 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1594 current_ir_graph = rem;
1597 set_array_upper_bound (ir_type *array, int dimension, ir_node * upper_bound) {
1598 assert(array && (array->type_op == type_array));
1599 assert(upper_bound && "upper_bound node may not be NULL.");
1600 array->attr.aa.upper_bound[dimension] = upper_bound;
1602 void set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound) {
1603 ir_graph *rem = current_ir_graph;
1604 current_ir_graph = get_const_code_irg();
1605 set_array_upper_bound (array, dimension,
1606 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1607 current_ir_graph = rem;
1609 int has_array_lower_bound (const ir_type *array, int dimension) {
1610 assert(array && (array->type_op == type_array));
1611 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1613 ir_node *get_array_lower_bound (const ir_type *array, int dimension) {
1614 assert(array && (array->type_op == type_array));
1615 return array->attr.aa.lower_bound[dimension];
1617 long get_array_lower_bound_int (const ir_type *array, int dimension) {
1619 assert(array && (array->type_op == type_array));
1620 node = array->attr.aa.lower_bound[dimension];
1621 assert(get_irn_op(node) == op_Const);
1622 return get_tarval_long(get_Const_tarval(node));
1624 int has_array_upper_bound (const ir_type *array, int dimension) {
1625 assert(array && (array->type_op == type_array));
1626 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1628 ir_node * get_array_upper_bound (const ir_type *array, int dimension) {
1629 assert(array && (array->type_op == type_array));
1630 return array->attr.aa.upper_bound[dimension];
1632 long get_array_upper_bound_int (const ir_type *array, int dimension) {
1634 assert(array && (array->type_op == type_array));
1635 node = array->attr.aa.upper_bound[dimension];
1636 assert(get_irn_op(node) == op_Const);
1637 return get_tarval_long(get_Const_tarval(node));
1640 void set_array_order (ir_type *array, int dimension, int order) {
1641 assert(array && (array->type_op == type_array));
1642 array->attr.aa.order[dimension] = order;
1645 int get_array_order (const ir_type *array, int dimension) {
1646 assert(array && (array->type_op == type_array));
1647 return array->attr.aa.order[dimension];
1650 int find_array_dimension(const ir_type *array, int order) {
1653 assert(array && (array->type_op == type_array));
1655 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1656 if (array->attr.aa.order[dim] == order)
1662 void set_array_element_type (ir_type *array, ir_type *tp) {
1663 assert(array && (array->type_op == type_array));
1664 assert(!is_Method_type(tp));
1665 array->attr.aa.element_type = tp;
1667 ir_type *get_array_element_type (ir_type *array) {
1668 assert(array && (array->type_op == type_array));
1669 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1672 void set_array_element_entity (ir_type *array, entity *ent) {
1673 assert(array && (array->type_op == type_array));
1674 assert((get_entity_type(ent)->type_op != type_method));
1675 array->attr.aa.element_ent = ent;
1676 array->attr.aa.element_type = get_entity_type(ent);
1678 entity *get_array_element_entity (const ir_type *array) {
1679 assert(array && (array->type_op == type_array));
1680 return array->attr.aa.element_ent;
1684 int (is_Array_type)(const ir_type *array) {
1685 return _is_array_type(array);
1688 void set_array_size_bits(ir_type *tp, int size) {
1689 /* FIXME: Here we should make some checks with the element type size */
1692 /*-----------------------------------------------------------------*/
1693 /* TYPE_ENUMERATION */
1694 /*-----------------------------------------------------------------*/
1696 /* create a new type enumeration -- set the enumerators independently */
1697 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1698 ir_type *res = new_type(type_enumeration, NULL, name, db);
1700 res->attr.ea.n_enums = n_enums;
1701 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1702 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1707 ir_type *new_type_enumeration(ident *name, int n_enums) {
1708 return new_d_type_enumeration(name, n_enums, NULL);
1711 void free_enumeration_entities(ir_type *enumeration) {
1712 assert(enumeration && (enumeration->type_op == type_enumeration));
1714 void free_enumeration_attrs(ir_type *enumeration) {
1715 assert(enumeration && (enumeration->type_op == type_enumeration));
1716 free(enumeration->attr.ea.enumer);
1717 free(enumeration->attr.ea.enum_nameid);
1720 /* manipulate fields of enumeration type. */
1721 int get_enumeration_n_enums (const ir_type *enumeration) {
1722 assert(enumeration && (enumeration->type_op == type_enumeration));
1723 return enumeration->attr.ea.n_enums;
1725 void set_enumeration_enum (ir_type *enumeration, int pos, tarval *con) {
1726 assert(enumeration && (enumeration->type_op == type_enumeration));
1727 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1728 enumeration->attr.ea.enumer[pos] = con;
1730 tarval *get_enumeration_enum (const ir_type *enumeration, int pos) {
1731 assert(enumeration && (enumeration->type_op == type_enumeration));
1732 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1733 return enumeration->attr.ea.enumer[pos];
1735 void set_enumeration_nameid (ir_type *enumeration, int pos, ident *id) {
1736 assert(enumeration && (enumeration->type_op == type_enumeration));
1737 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1738 enumeration->attr.ea.enum_nameid[pos] = id;
1740 ident *get_enumeration_nameid (const ir_type *enumeration, int pos) {
1741 assert(enumeration && (enumeration->type_op == type_enumeration));
1742 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1743 return enumeration->attr.ea.enum_nameid[pos];
1745 const char *get_enumeration_name(const ir_type *enumeration, int pos) {
1746 assert(enumeration && (enumeration->type_op == type_enumeration));
1747 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1748 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1752 int (is_Enumeration_type)(const ir_type *enumeration) {
1753 return _is_enumeration_type(enumeration);
1756 void set_enumeration_mode(ir_type *tp, ir_mode *mode) {
1757 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1758 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1759 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1761 tp->size = get_mode_size_bits(mode);
1765 /*-----------------------------------------------------------------*/
1767 /*-----------------------------------------------------------------*/
1769 /* Create a new type pointer */
1770 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1773 assert(mode_is_reference(ptr_mode));
1774 res = new_type(type_pointer, ptr_mode, name, db);
1775 res->attr.pa.points_to = points_to;
1776 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1777 res->size = get_mode_size_bits(res->mode);
1778 res->flags |= tf_layout_fixed;
1783 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode) {
1784 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1787 void free_pointer_entities (ir_type *pointer) {
1788 assert(pointer && (pointer->type_op == type_pointer));
1791 void free_pointer_attrs (ir_type *pointer) {
1792 assert(pointer && (pointer->type_op == type_pointer));
1795 /* manipulate fields of type_pointer */
1796 void set_pointer_points_to_type (ir_type *pointer, ir_type *tp) {
1797 assert(pointer && (pointer->type_op == type_pointer));
1798 pointer->attr.pa.points_to = tp;
1801 ir_type *get_pointer_points_to_type (ir_type *pointer) {
1802 assert(pointer && (pointer->type_op == type_pointer));
1803 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1807 int (is_Pointer_type)(const ir_type *pointer) {
1808 return _is_pointer_type(pointer);
1811 void set_pointer_mode(ir_type *tp, ir_mode *mode) {
1812 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1813 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1814 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1816 tp->size = get_mode_size_bits(mode);
1820 /* Returns the first pointer type that has as points_to tp.
1821 * Not efficient: O(#types).
1822 * If not found returns firm_unknown_type. */
1823 ir_type *find_pointer_type_to_type (ir_type *tp) {
1824 int i, n = get_irp_n_types();
1825 for (i = 0; i < n; ++i) {
1826 ir_type *found = get_irp_type(i);
1827 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1830 return firm_unknown_type;
1834 /*-----------------------------------------------------------------*/
1835 /* TYPE_PRIMITIVE */
1836 /*-----------------------------------------------------------------*/
1838 /* create a new type primitive */
1839 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1841 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1842 res = new_type(type_primitive, mode, name, db);
1843 res->size = get_mode_size_bits(mode);
1844 res->flags |= tf_layout_fixed;
1849 ir_type *new_type_primitive(ident *name, ir_mode *mode) {
1850 return new_d_type_primitive(name, mode, NULL);
1854 int (is_Primitive_type)(const ir_type *primitive) {
1855 return _is_primitive_type(primitive);
1858 void set_primitive_mode(ir_type *tp, ir_mode *mode) {
1859 /* Modes of primitives must be data */
1860 assert(mode_is_data(mode));
1862 /* For primitive size depends on the mode. */
1863 tp->size = get_mode_size_bits(mode);
1868 /*-----------------------------------------------------------------*/
1869 /* common functionality */
1870 /*-----------------------------------------------------------------*/
1873 int (is_atomic_type)(const ir_type *tp) {
1874 return _is_atomic_type(tp);
1878 * Gets the number of elements in a firm compound type.
1880 int get_compound_n_members(const ir_type *tp)
1882 const tp_op *op = get_type_tpop(tp);
1885 if (op->ops.get_n_members)
1886 res = op->ops.get_n_members(tp);
1888 assert(0 && "no member count for this type");
1894 * Gets the member of a firm compound type at position pos.
1896 entity *get_compound_member(const ir_type *tp, int pos)
1898 const tp_op *op = get_type_tpop(tp);
1901 if (op->ops.get_member)
1902 res = op->ops.get_member(tp, pos);
1904 assert(0 && "no members in this type");
1909 /* Returns index of member in tp, -1 if not contained. */
1910 int get_compound_member_index(const ir_type *tp, entity *member)
1912 const tp_op *op = get_type_tpop(tp);
1915 if (op->ops.get_member_index)
1916 index = op->ops.get_member_index(tp, member);
1918 assert(0 && "no members in this type");
1923 int is_compound_type(const ir_type *tp) {
1924 assert(tp && tp->kind == k_type);
1925 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1928 /* Checks, whether a type is a frame type */
1929 int is_frame_type(const ir_type *tp) {
1930 return tp->flags & tf_frame_type;
1933 /* Checks, whether a type is a value parameter type */
1934 int is_value_param_type(const ir_type *tp) {
1935 return tp->flags & tf_value_param_type;
1938 /* Checks, whether a type is a lowered type */
1939 int is_lowered_type(const ir_type *tp) {
1940 return tp->flags & tf_lowered_type;
1943 /* Makes a new frame type. */
1944 ir_type *new_type_frame(ident *name)
1946 ir_type *res = new_type_class(name);
1948 res->flags |= tf_frame_type;
1950 /* Remove type from type list. Must be treated differently than other types. */
1951 remove_irp_type(res);
1956 /* Sets a lowered type for a type. This sets both associations. */
1957 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
1958 assert(is_type(tp) && is_type(lowered_type));
1959 lowered_type->flags |= tf_lowered_type;
1960 tp->assoc_type = lowered_type;
1961 lowered_type->assoc_type = tp;
1965 * Gets the lowered/unlowered type of a type or NULL if this type
1966 * has no lowered/unlowered one.
1968 ir_type *get_associated_type(const ir_type *tp) {
1969 return tp->assoc_type;
1972 /* set the type size for the unknown and none ir_type */
1973 void set_default_size_bits(ir_type *tp, int size) {
1978 * Allocate an area of size bytes aligned at alignment
1979 * at the start or the end of a frame type.
1980 * The frame type must have already an fixed layout.
1982 entity *frame_alloc_area(ir_type *frame_type, int size, int alignment, int at_start)
1988 int frame_align, i, offset, frame_size;
1989 static unsigned area_cnt = 0;
1990 static ir_type *a_byte = NULL;
1992 assert(is_frame_type(frame_type));
1993 assert(get_type_state(frame_type) == layout_fixed);
1996 a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
1998 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
1999 name = new_id_from_str(buf);
2001 /* align the size */
2002 frame_align = get_type_alignment_bytes(frame_type);
2003 size = (size + frame_align - 1) & -frame_align;
2005 tp = new_type_array(mangle_u(get_type_ident(frame_type), name), 1, a_byte);
2006 set_array_bounds_int(tp, 0, 0, size);
2007 set_type_alignment_bytes(tp, alignment);
2009 frame_size = get_type_size_bytes(frame_type);
2011 /* fix all offsets so far */
2012 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2013 entity *ent = get_class_member(frame_type, i);
2015 set_entity_offset_bytes(ent, get_entity_offset_bytes(ent) + size);
2017 /* calculate offset and new type size */
2022 /* calculate offset and new type size */
2023 offset = (frame_size + alignment - 1) & -alignment;
2024 frame_size = offset + size;
2027 area = new_entity(frame_type, name, tp);
2028 set_entity_offset_bytes(area, offset);
2029 set_type_size_bytes(frame_type, frame_size);