2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Representation of types.
23 * @author Goetz Lindenmaier, Michael Beck
27 * Implementation of the datastructure to hold
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.
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
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.
64 ir_type *firm_none_type;
65 ir_type *get_none_type(void)
67 return firm_none_type;
70 ir_type *firm_code_type;
71 ir_type *get_code_type(void)
73 return firm_code_type;
76 ir_type *firm_unknown_type;
77 ir_type *get_unknown_type(void) {
78 return firm_unknown_type;
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;
85 /** The default calling convention for method types. */
86 static unsigned default_cc_mask;
88 unsigned get_default_cc_mask(void)
90 return default_cc_mask;
93 void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask)
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);
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);
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);
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);
115 /** the global type visited flag */
116 ir_visited_t firm_type_visited;
118 void (set_master_type_visited)(ir_visited_t val)
120 _set_master_type_visited(val);
123 ir_visited_t (get_master_type_visited)(void)
125 return _get_master_type_visited();
128 void (inc_master_type_visited)(void)
130 _inc_master_type_visited();
133 ir_type *new_type(const tp_op *type_op, ir_mode *mode, ident *name,
139 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
141 node_size = offsetof(ir_type, attr) + type_op->attr_size;
142 res = xmalloc(node_size);
143 memset(res, 0, node_size);
146 res->type_op = type_op;
149 res->visibility = visibility_external_allocated;
150 res->flags = tf_none;
156 res->assoc_type = NULL;
158 res->nr = get_irp_new_node_nr();
159 #endif /* defined DEBUG_libfirm */
161 add_irp_type(res); /* Remember the new type global. */
166 void free_type(ir_type *tp)
168 const tp_op *op = get_type_tpop(tp);
170 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
171 || (get_type_tpop(tp) == tpop_code))
173 /* Remove from list of all types */
175 /* Free the attributes of the type. */
177 /* Free entities automatically allocated with the ir_type */
178 if (op->ops.free_auto_entities)
179 op->ops.free_auto_entities(tp);
180 /* And now the type itself... */
185 void free_type_entities(ir_type *tp)
187 const tp_op *tpop = get_type_tpop(tp);
189 if (tpop->ops.free_entities)
190 tpop->ops.free_entities(tp);
193 void free_type_attrs(ir_type *tp)
195 const tp_op *tpop = get_type_tpop(tp);
197 if (tpop->ops.free_attrs)
198 tpop->ops.free_attrs(tp);
201 void *(get_type_link)(const ir_type *tp)
203 return _get_type_link(tp);
206 void (set_type_link)(ir_type *tp, void *l)
208 _set_type_link(tp, l);
211 const tp_op *(get_type_tpop)(const ir_type *tp)
213 return _get_type_tpop(tp);
216 ident *(get_type_tpop_nameid)(const ir_type *tp)
218 return _get_type_tpop_nameid(tp);
221 const char* get_type_tpop_name(const ir_type *tp)
223 assert(tp && tp->kind == k_type);
224 return get_id_str(tp->type_op->name);
227 tp_opcode (get_type_tpop_code)(const ir_type *tp)
229 return _get_type_tpop_code(tp);
232 ir_mode *(get_type_mode)(const ir_type *tp)
234 return _get_type_mode(tp);
237 void set_type_mode(ir_type *tp, ir_mode *mode)
239 const tp_op *tpop = get_type_tpop(tp);
241 if (tpop->ops.set_type_mode)
242 tpop->ops.set_type_mode(tp, mode);
244 assert(0 && "setting a mode is NOT allowed for this type");
247 ident *(get_type_ident)(const ir_type *tp)
249 return _get_type_ident(tp);
252 void (set_type_ident)(ir_type *tp, ident* id)
254 _set_type_ident(tp, id);
257 /* Outputs a unique number for this node */
258 long get_type_nr(const ir_type *tp)
264 return (long)PTR_TO_INT(tp);
268 const char *get_type_name(const ir_type *tp)
270 assert(tp && tp->kind == k_type);
271 return (get_id_str(tp->name));
274 unsigned (get_type_size_bytes)(const ir_type *tp)
276 return _get_type_size_bytes(tp);
279 ir_visibility get_type_visibility(const ir_type *tp)
282 return tp->visibility;
285 void set_type_visibility(ir_type *tp, ir_visibility v)
291 void set_type_size_bytes(ir_type *tp, unsigned size)
293 const tp_op *tpop = get_type_tpop(tp);
295 if (tpop->ops.set_type_size)
296 tpop->ops.set_type_size(tp, size);
298 assert(0 && "Cannot set size for this type");
301 unsigned get_type_alignment_bytes(ir_type *tp)
308 /* alignment NOT set calculate it "on demand" */
310 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
311 else if (is_Array_type(tp))
312 align = get_type_alignment_bytes(get_array_element_type(tp));
313 else if (is_compound_type(tp)) {
314 int i, n = get_compound_n_members(tp);
317 for (i = 0; i < n; ++i) {
318 ir_type *t = get_entity_type(get_compound_member(tp, i));
319 unsigned a = get_type_alignment_bytes(t);
324 } else if (is_Method_type(tp)) {
334 void set_type_alignment_bytes(ir_type *tp, unsigned align)
336 assert(tp && tp->kind == k_type);
337 /* Methods don't have an alignment. */
338 if (tp->type_op != type_method) {
343 const char *get_type_state_name(ir_type_state s)
345 #define X(a) case a: return #a;
354 ir_type_state (get_type_state)(const ir_type *tp)
356 return _get_type_state(tp);
359 void set_type_state(ir_type *tp, ir_type_state state)
361 assert(tp && tp->kind == k_type);
363 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
364 (tp->type_op == type_method))
367 /* Just a correctness check: */
368 if (state == layout_fixed) {
370 switch (get_type_tpop_code(tp)) {
372 if (tp != get_glob_type()) {
373 int n_mem = get_class_n_members(tp);
374 for (i = 0; i < n_mem; i++) {
375 assert(get_entity_offset(get_class_member(tp, i)) > -1);
377 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
378 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
384 for (i = 0; i < get_struct_n_members(tp); i++) {
385 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
386 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
395 Assure that only innermost dimension is dynamic? */
397 case tpo_enumeration:
399 assert(get_type_mode != NULL);
400 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
401 ir_enum_const *ec = get_enumeration_const(tp, i);
402 tarval *tv = get_enumeration_value(ec);
403 assert(tv != NULL && tv != tarval_bad);
410 if (state == layout_fixed)
411 tp->flags |= tf_layout_fixed;
413 tp->flags &= ~tf_layout_fixed;
416 ir_visited_t (get_type_visited)(const ir_type *tp)
418 return _get_type_visited(tp);
421 void (set_type_visited)(ir_type *tp, ir_visited_t num)
423 _set_type_visited(tp, num);
426 void (mark_type_visited)(ir_type *tp)
428 _mark_type_visited(tp);
431 int (type_visited)(const ir_type *tp)
433 return _type_visited(tp);
436 int (type_not_visited)(const ir_type *tp)
438 return _type_not_visited(tp);
441 dbg_info *(get_type_dbg_info)(const ir_type *tp)
443 return _get_type_dbg_info(tp);
446 void (set_type_dbg_info)(ir_type *tp, dbg_info *db)
448 _set_type_dbg_info(tp, db);
451 int (is_type)(const void *thing)
453 return _is_type(thing);
456 /* Checks whether two types are structural equal.*/
457 int equal_type(ir_type *typ1, ir_type *typ2)
463 if (typ1 == typ2) return 1;
465 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
466 (get_type_ident(typ1) != get_type_ident(typ2)) ||
467 (get_type_mode(typ1) != get_type_mode(typ2)) ||
468 (get_type_state(typ1) != get_type_state(typ2)))
470 if ((get_type_state(typ1) == layout_fixed) &&
471 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
474 switch (get_type_tpop_code(typ1)) {
476 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
477 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
478 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
479 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
480 /** Compare the members **/
481 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
482 /* First sort the members of typ2 */
483 for (i = 0; i < get_class_n_members(typ1); i++) {
484 ir_entity *e1 = get_class_member(typ1, i);
485 for (j = 0; j < get_class_n_members(typ2); j++) {
486 ir_entity *e2 = get_class_member(typ2, j);
487 if (get_entity_name(e1) == get_entity_name(e2))
491 for (i = 0; i < get_class_n_members(typ1); i++) {
492 if (!m[i] || get_class_member(typ1, i) != m[i])
495 /** Compare the supertypes **/
496 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
497 /* First sort the supertypes of typ2 */
498 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
499 ir_type *t1 = get_class_supertype(typ1, i);
500 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
501 ir_type *t2 = get_class_supertype(typ2, j);
502 if (get_type_ident(t2) == get_type_ident(t1))
506 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
507 if (!t[i] || /* Found no counterpart */
508 get_class_supertype(typ1, i) != t[i])
514 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
515 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
516 /* First sort the members of lt */
517 for (i = 0; i < get_struct_n_members(typ1); i++) {
518 ir_entity *e1 = get_struct_member(typ1, i);
519 for (j = 0; j < get_struct_n_members(typ2); j++) {
520 ir_entity *e2 = get_struct_member(typ2, j);
521 if (get_entity_name(e1) == get_entity_name(e2))
525 for (i = 0; i < get_struct_n_members(typ1); i++) {
526 if (!m[i] || get_struct_member(typ1, i) != m[i])
532 int n_param1, n_param2;
534 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
535 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
536 if (get_method_calling_convention(typ1) !=
537 get_method_calling_convention(typ2)) return 0;
539 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
540 n_param1 = get_method_n_params(typ1);
541 n_param2 = get_method_n_params(typ2);
543 n_param1 = get_method_first_variadic_param_index(typ1);
544 n_param2 = get_method_first_variadic_param_index(typ2);
547 if (n_param1 != n_param2) return 0;
549 for (i = 0; i < n_param1; i++) {
550 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
553 for (i = 0; i < get_method_n_ress(typ1); i++) {
554 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
560 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
561 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
562 /* First sort the members of lt */
563 for (i = 0; i < get_union_n_members(typ1); i++) {
564 ir_entity *e1 = get_union_member(typ1, i);
565 for (j = 0; j < get_union_n_members(typ2); j++) {
566 ir_entity *e2 = get_union_member(typ2, j);
567 if (get_entity_name(e1) == get_entity_name(e2))
571 for (i = 0; i < get_union_n_members(typ1); i++) {
572 if (!m[i] || get_union_member(typ1, i) != m[i])
578 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
580 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
582 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
583 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
584 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
586 if (get_array_order(typ1, i) != get_array_order(typ2, i))
587 assert(0 && "type compare with different dimension orders not implemented");
591 case tpo_enumeration:
592 assert(0 && "enumerations not implemented");
596 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
608 int smaller_type(ir_type *st, ir_type *lt)
611 int i, j, n_st_members;
613 if (st == lt) return 1;
615 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
618 switch(get_type_tpop_code(st)) {
620 return is_SubClass_of(st, lt);
623 n_st_members = get_struct_n_members(st);
624 if (n_st_members != get_struct_n_members(lt))
627 m = ALLOCANZ(ir_entity*, n_st_members);
628 /* First sort the members of lt */
629 for (i = 0; i < n_st_members; ++i) {
630 ir_entity *se = get_struct_member(st, i);
631 int n = get_struct_n_members(lt);
632 for (j = 0; j < n; ++j) {
633 ir_entity *le = get_struct_member(lt, j);
634 if (get_entity_name(le) == get_entity_name(se))
638 for (i = 0; i < n_st_members; i++) {
639 if (!m[i] || /* Found no counterpart */
640 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
646 int n_param1, n_param2;
648 /** FIXME: is this still 1? */
649 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
650 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
651 if (get_method_calling_convention(st) !=
652 get_method_calling_convention(lt)) return 0;
654 if (get_method_variadicity(st) == variadicity_non_variadic) {
655 n_param1 = get_method_n_params(st);
656 n_param2 = get_method_n_params(lt);
658 n_param1 = get_method_first_variadic_param_index(st);
659 n_param2 = get_method_first_variadic_param_index(lt);
662 if (n_param1 != n_param2) return 0;
664 for (i = 0; i < get_method_n_params(st); i++) {
665 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
668 for (i = 0; i < get_method_n_ress(st); i++) {
669 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
675 n_st_members = get_union_n_members(st);
676 if (n_st_members != get_union_n_members(lt)) return 0;
677 m = ALLOCANZ(ir_entity*, n_st_members);
678 /* First sort the members of lt */
679 for (i = 0; i < n_st_members; ++i) {
680 ir_entity *se = get_union_member(st, i);
681 int n = get_union_n_members(lt);
682 for (j = 0; j < n; ++j) {
683 ir_entity *le = get_union_member(lt, j);
684 if (get_entity_name(le) == get_entity_name(se))
688 for (i = 0; i < n_st_members; ++i) {
689 if (!m[i] || /* Found no counterpart */
690 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
696 ir_type *set, *let; /* small/large elt. ir_type */
697 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
699 set = get_array_element_type(st);
700 let = get_array_element_type(lt);
702 /* If the element types are different, set must be convertible
703 to let, and they must have the same size so that address
704 computations work out. To have a size the layout must
706 if ((get_type_state(set) != layout_fixed) ||
707 (get_type_state(let) != layout_fixed))
709 if (!smaller_type(set, let) ||
710 get_type_size_bytes(set) != get_type_size_bytes(let))
713 for(i = 0; i < get_array_n_dimensions(st); i++) {
714 if (get_array_lower_bound(lt, i))
715 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
717 if (get_array_upper_bound(lt, i))
718 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
723 case tpo_enumeration:
724 assert(0 && "enumerations not implemented");
728 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
733 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
743 ir_type *new_d_type_class(ident *name, dbg_info *db)
747 res = new_type(type_class, NULL, name, db);
749 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
750 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
751 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
752 res->attr.ca.peculiarity = peculiarity_existent;
753 res->attr.ca.type_info = NULL;
754 res->attr.ca.vtable_size = 0;
755 res->attr.ca.clss_flags = cf_none;
756 res->attr.ca.dfn = 0;
761 ir_type *new_type_class(ident *name)
763 return new_d_type_class (name, NULL);
766 void free_class_entities(ir_type *clss)
769 assert(clss && (clss->type_op == type_class));
770 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
771 free_entity(get_class_member(clss, i));
772 /* do NOT free the type info here. It belongs to another class */
775 void free_class_attrs(ir_type *clss)
777 assert(clss && (clss->type_op == type_class));
778 DEL_ARR_F(clss->attr.ca.members);
779 DEL_ARR_F(clss->attr.ca.subtypes);
780 DEL_ARR_F(clss->attr.ca.supertypes);
783 void add_class_member(ir_type *clss, ir_entity *member)
785 assert(clss && (clss->type_op == type_class));
786 assert(clss != get_entity_type(member) && "recursive type");
787 assert(get_type_state(clss) != layout_fixed);
788 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
791 int (get_class_n_members)(const ir_type *clss)
793 return _get_class_n_members(clss);
796 int get_class_member_index(const ir_type *clss, ir_entity *mem)
799 assert(clss && (clss->type_op == type_class));
800 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
801 if (get_class_member(clss, i) == mem)
806 ir_entity *(get_class_member)(const ir_type *clss, int pos)
808 return _get_class_member(clss, pos);
811 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
814 assert(clss && (clss->type_op == type_class));
815 n_mem = get_class_n_members(clss);
816 for (i = 0; i < n_mem; ++i) {
817 ir_entity *mem = get_class_member(clss, i);
818 if (get_entity_ident(mem) == name) return mem;
823 void set_class_member(ir_type *clss, ir_entity *member, int pos)
825 assert(clss && (clss->type_op == type_class));
826 assert(pos >= 0 && pos < get_class_n_members(clss));
827 clss->attr.ca.members[pos] = member;
830 void set_class_members(ir_type *clss, ir_entity **members, int arity)
833 assert(clss && (clss->type_op == type_class));
834 DEL_ARR_F(clss->attr.ca.members);
835 clss->attr.ca.members = NEW_ARR_F(ir_entity *, 0);
836 for (i = 0; i < arity; ++i) {
837 set_entity_owner(members[i], clss);
838 ARR_APP1(ir_entity *, clss->attr.ca.members, members[i]);
842 void remove_class_member(ir_type *clss, ir_entity *member)
845 assert(clss && (clss->type_op == type_class));
846 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
847 if (clss->attr.ca.members[i] == member) {
848 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
849 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
850 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
856 void add_class_subtype(ir_type *clss, ir_type *subtype)
859 assert(clss && (clss->type_op == type_class));
860 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
861 for (i = 0; i < get_class_n_supertypes(subtype); i++)
862 if (get_class_supertype(subtype, i) == clss)
863 /* Class already registered */
865 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
868 int get_class_n_subtypes(const ir_type *clss)
870 assert(clss && (clss->type_op == type_class));
871 return (ARR_LEN (clss->attr.ca.subtypes));
874 ir_type *get_class_subtype(ir_type *clss, int pos)
876 assert(clss && (clss->type_op == type_class));
877 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
878 return clss->attr.ca.subtypes[pos];
881 int get_class_subtype_index(ir_type *clss, const ir_type *subclass)
883 int i, n_subtypes = get_class_n_subtypes(clss);
884 assert(is_Class_type(subclass));
885 for (i = 0; i < n_subtypes; ++i) {
886 if (get_class_subtype(clss, i) == subclass) return i;
891 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos)
893 assert(clss && (clss->type_op == type_class));
894 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
895 clss->attr.ca.subtypes[pos] = subtype;
898 void remove_class_subtype(ir_type *clss, ir_type *subtype)
901 assert(clss && (clss->type_op == type_class));
902 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
903 if (clss->attr.ca.subtypes[i] == subtype) {
904 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
905 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
906 ARR_SETLEN(ir_entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
911 void add_class_supertype(ir_type *clss, ir_type *supertype)
914 assert(clss && (clss->type_op == type_class));
915 assert(supertype && (supertype -> type_op == type_class));
916 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
917 for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
918 if (get_class_subtype(supertype, i) == clss)
919 /* Class already registered */
921 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
924 int get_class_n_supertypes(const ir_type *clss)
926 assert(clss && (clss->type_op == type_class));
927 return ARR_LEN(clss->attr.ca.supertypes);
930 int get_class_supertype_index(ir_type *clss, ir_type *super_clss)
932 int i, n_supertypes = get_class_n_supertypes(clss);
933 assert(super_clss && (super_clss->type_op == type_class));
934 for (i = 0; i < n_supertypes; i++)
935 if (get_class_supertype(clss, i) == super_clss)
940 ir_type *get_class_supertype(ir_type *clss, int pos)
942 assert(clss && (clss->type_op == type_class));
943 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
944 return clss->attr.ca.supertypes[pos];
947 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos)
949 assert(clss && (clss->type_op == type_class));
950 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
951 clss->attr.ca.supertypes[pos] = supertype;
954 void remove_class_supertype(ir_type *clss, ir_type *supertype)
957 assert(clss && (clss->type_op == type_class));
958 for (i = 0; i < (ARR_LEN(clss->attr.ca.supertypes)); i++)
959 if (clss->attr.ca.supertypes[i] == supertype) {
960 for(; i < (ARR_LEN(clss->attr.ca.supertypes))-1; i++)
961 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
962 ARR_SETLEN(ir_entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
967 ir_entity *get_class_type_info(const ir_type *clss)
969 return clss->attr.ca.type_info;
972 void set_class_type_info(ir_type *clss, ir_entity *ent)
974 clss->attr.ca.type_info = ent;
976 ent->repr_class = clss;
979 const char *get_peculiarity_name(ir_peculiarity p)
981 #define X(a) case a: return #a
983 X(peculiarity_description);
984 X(peculiarity_inherited);
985 X(peculiarity_existent);
988 return "invalid peculiarity";
991 ir_peculiarity get_class_peculiarity(const ir_type *clss)
993 assert(clss && (clss->type_op == type_class));
994 return clss->attr.ca.peculiarity;
997 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
999 assert(clss && (clss->type_op == type_class));
1000 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
1001 clss->attr.ca.peculiarity = pec;
1004 unsigned (get_class_vtable_size)(const ir_type *clss)
1006 return _get_class_vtable_size(clss);
1009 void (set_class_vtable_size)(ir_type *clss, unsigned size)
1011 _set_class_vtable_size(clss, size);
1014 int (is_class_final)(const ir_type *clss)
1016 return _is_class_final(clss);
1019 void (set_class_final)(ir_type *clss, int flag)
1021 _set_class_final(clss, flag);
1024 int (is_class_interface)(const ir_type *clss)
1026 return _is_class_interface(clss);
1029 void (set_class_interface)(ir_type *clss, int flag)
1031 _set_class_interface(clss, flag);
1034 int (is_class_abstract)(const ir_type *clss)
1036 return _is_class_abstract(clss);
1039 void (set_class_abstract)(ir_type *clss, int final)
1041 _set_class_abstract(clss, final);
1044 void set_class_dfn(ir_type *clss, int dfn)
1046 clss->attr.ca.dfn = dfn;
1049 int get_class_dfn(const ir_type *clss)
1051 return (clss->attr.ca.dfn);
1054 int (is_Class_type)(const ir_type *clss)
1056 return _is_class_type(clss);
1059 void set_class_mode(ir_type *tp, ir_mode *mode)
1061 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1062 assert(get_type_state(tp) == layout_fixed &&
1063 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1067 void set_class_size(ir_type *tp, unsigned size) {
1072 ir_type *new_d_type_struct(ident *name, dbg_info *db)
1074 ir_type *res = new_type(type_struct, NULL, name, db);
1076 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1081 ir_type *new_type_struct(ident *name)
1083 return new_d_type_struct (name, NULL);
1086 void free_struct_entities(ir_type *strct)
1089 assert(strct && (strct->type_op == type_struct));
1090 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1091 free_entity(get_struct_member(strct, i));
1094 void free_struct_attrs(ir_type *strct)
1096 assert(strct && (strct->type_op == type_struct));
1097 DEL_ARR_F(strct->attr.sa.members);
1100 int get_struct_n_members(const ir_type *strct)
1102 assert(strct && (strct->type_op == type_struct));
1103 return ARR_LEN(strct->attr.sa.members);
1106 void add_struct_member(ir_type *strct, ir_entity *member)
1108 assert(strct && (strct->type_op == type_struct));
1109 assert(get_type_tpop(get_entity_type(member)) != type_method);
1110 assert(strct != get_entity_type(member) && "recursive type");
1111 assert(get_type_state(strct) != layout_fixed);
1112 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1115 ir_entity *get_struct_member(const ir_type *strct, int pos)
1117 assert(strct && (strct->type_op == type_struct));
1118 assert(pos >= 0 && pos < get_struct_n_members(strct));
1119 return strct->attr.sa.members[pos];
1122 int get_struct_member_index(const ir_type *strct, ir_entity *mem)
1125 assert(strct && (strct->type_op == type_struct));
1126 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1127 if (get_struct_member(strct, i) == mem)
1132 void set_struct_member(ir_type *strct, int pos, ir_entity *member)
1134 assert(strct && (strct->type_op == type_struct));
1135 assert(pos >= 0 && pos < get_struct_n_members(strct));
1136 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1137 strct->attr.sa.members[pos] = member;
1140 void remove_struct_member(ir_type *strct, ir_entity *member)
1143 assert(strct && (strct->type_op == type_struct));
1144 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1145 if (strct->attr.sa.members[i] == member) {
1146 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1147 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1148 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1153 int (is_Struct_type)(const ir_type *strct)
1155 return _is_struct_type(strct);
1158 void set_struct_mode(ir_type *tp, ir_mode *mode)
1160 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1161 assert(get_type_state(tp) == layout_fixed &&
1162 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1166 void set_struct_size(ir_type *tp, unsigned size)
1173 * Lazy construction of value argument / result representation.
1174 * Constructs a struct type and its member. The types of the members
1175 * are passed in the argument list.
1177 * @param name name of the type constructed
1178 * @param len number of fields
1179 * @param tps array of field types with length len
1181 static ir_type *build_value_type(ident *name, int len, tp_ent_pair *tps)
1184 ir_type *res = new_type_struct(name);
1185 res->flags |= tf_value_param_type;
1186 /* Remove type from type list. Must be treated differently than other types. */
1187 remove_irp_type(res);
1188 for (i = 0; i < len; i++) {
1189 ident *id = tps[i].param_name;
1191 /* use res as default if corresponding type is not yet set. */
1192 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1194 /* use the parameter name if specified */
1196 id = id_mangle_u(name, get_type_ident(elt_type));
1197 tps[i].ent = new_entity(res, id, elt_type);
1198 set_entity_allocation(tps[i].ent, allocation_parameter);
1203 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db)
1207 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1208 res = new_type(type_method, mode_P_code, name, db);
1209 res->flags |= tf_layout_fixed;
1210 res->size = get_mode_size_bytes(mode_P_code);
1211 res->attr.ma.n_params = n_param;
1212 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1213 res->attr.ma.value_params = NULL;
1214 res->attr.ma.n_res = n_res;
1215 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1216 res->attr.ma.value_ress = NULL;
1217 res->attr.ma.variadicity = variadicity_non_variadic;
1218 res->attr.ma.first_variadic_param = -1;
1219 res->attr.ma.additional_properties = mtp_no_property;
1220 res->attr.ma.irg_calling_conv = default_cc_mask;
1225 ir_type *new_type_method(ident *name, int n_param, int n_res)
1227 return new_d_type_method(name, n_param, n_res, NULL);
1230 ir_type *clone_type_method(ir_type *tp, ident *prefix)
1235 int n_params, n_res;
1238 assert(is_Method_type(tp));
1242 name = id_mangle(prefix, name);
1245 n_params = tp->attr.ma.n_params;
1246 n_res = tp->attr.ma.n_res;
1249 res = new_type(type_method, mode, name, db);
1251 res->flags = tp->flags;
1252 res->assoc_type = tp->assoc_type;
1253 res->size = tp->size;
1254 res->attr.ma.n_params = n_params;
1255 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1256 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1257 res->attr.ma.value_params = tp->attr.ma.value_params;
1258 res->attr.ma.n_res = n_res;
1259 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1260 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1261 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1262 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1263 res->attr.ma.first_variadic_param = tp->attr.ma.first_variadic_param;
1264 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1265 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1270 void free_method_entities(ir_type *method)
1273 assert(method && (method->type_op == type_method));
1276 void free_method_attrs(ir_type *method)
1278 assert(method && (method->type_op == type_method));
1279 free(method->attr.ma.params);
1280 free(method->attr.ma.res_type);
1281 /* cannot free it yet, type could be cloned ...
1282 if (method->attr.ma.value_params) {
1283 free_type_entities(method->attr.ma.value_params);
1284 free_type(method->attr.ma.value_params);
1287 if (method->attr.ma.value_ress) {
1288 free_type_entities(method->attr.ma.value_ress);
1289 free_type(method->attr.ma.value_ress);
1293 int (get_method_n_params)(const ir_type *method)
1295 return _get_method_n_params(method);
1298 ir_type *get_method_param_type(ir_type *method, int pos)
1301 assert(method && (method->type_op == type_method));
1302 assert(pos >= 0 && pos < get_method_n_params(method));
1303 res = method->attr.ma.params[pos].tp;
1304 assert(res != NULL && "empty method param type");
1308 void set_method_param_type(ir_type *method, int pos, ir_type *tp)
1310 assert(method && (method->type_op == type_method));
1311 assert(pos >= 0 && pos < get_method_n_params(method));
1312 method->attr.ma.params[pos].tp = tp;
1313 /* If information constructed set pass-by-value representation. */
1314 if (method->attr.ma.value_params) {
1315 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1316 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1320 ident *get_method_param_ident(ir_type *method, int pos)
1322 assert(method && (method->type_op == type_method));
1323 assert(pos >= 0 && pos < get_method_n_params(method));
1324 return method->attr.ma.params[pos].param_name;
1327 const char *get_method_param_name(ir_type *method, int pos)
1329 ident *id = get_method_param_ident(method, pos);
1330 return id ? get_id_str(id) : NULL;
1333 void set_method_param_ident(ir_type *method, int pos, ident *id)
1335 assert(method && (method->type_op == type_method));
1336 assert(pos >= 0 && pos < get_method_n_params(method));
1337 method->attr.ma.params[pos].param_name = id;
1340 ir_entity *get_method_value_param_ent(ir_type *method, int pos)
1342 assert(method && (method->type_op == type_method));
1343 assert(pos >= 0 && pos < get_method_n_params(method));
1345 if (!method->attr.ma.value_params) {
1346 /* parameter value type not created yet, build */
1347 method->attr.ma.value_params
1348 = build_value_type(id_mangle_u(get_type_ident(method), value_params_suffix),
1349 get_method_n_params(method), method->attr.ma.params);
1352 * build_value_type() sets the method->attr.ma.value_params type as default if
1355 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1356 && "param type not yet set");
1357 return method->attr.ma.params[pos].ent;
1360 void set_method_value_param_type(ir_type *method, ir_type *tp)
1364 assert(method && (method->type_op == type_method));
1365 assert(is_value_param_type(tp));
1366 assert(get_method_n_params(method) == get_struct_n_members(tp));
1368 method->attr.ma.value_params = tp;
1370 n = get_struct_n_members(tp);
1371 for (i = 0; i < n; i++) {
1372 ir_entity *ent = get_struct_member(tp, i);
1373 method->attr.ma.params[i].ent = ent;
1377 ir_type *get_method_value_param_type(const ir_type *method)
1379 assert(method && (method->type_op == type_method));
1380 return method->attr.ma.value_params;
1383 int (get_method_n_ress)(const ir_type *method)
1385 return _get_method_n_ress(method);
1388 ir_type *get_method_res_type(ir_type *method, int pos)
1391 assert(method && (method->type_op == type_method));
1392 assert(pos >= 0 && pos < get_method_n_ress(method));
1393 res = method->attr.ma.res_type[pos].tp;
1394 assert(res != NULL && "empty method return type");
1398 void set_method_res_type(ir_type *method, int pos, ir_type *tp)
1400 assert(method && (method->type_op == type_method));
1401 assert(pos >= 0 && pos < get_method_n_ress(method));
1402 /* set the result ir_type */
1403 method->attr.ma.res_type[pos].tp = tp;
1404 /* If information constructed set pass-by-value representation. */
1405 if (method->attr.ma.value_ress) {
1406 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1407 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1411 ir_entity *get_method_value_res_ent(ir_type *method, int pos)
1413 assert(method && (method->type_op == type_method));
1414 assert(pos >= 0 && pos < get_method_n_ress(method));
1416 if (!method->attr.ma.value_ress) {
1417 /* result value type not created yet, build */
1418 method->attr.ma.value_ress
1419 = build_value_type(id_mangle_u(get_type_ident(method), value_ress_suffix),
1420 get_method_n_ress(method), method->attr.ma.res_type);
1423 * build_value_type() sets the method->attr.ma.value_ress type as default if
1426 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1427 && "result type not yet set");
1429 return method->attr.ma.res_type[pos].ent;
1432 ir_type *get_method_value_res_type(const ir_type *method)
1434 assert(method && (method->type_op == type_method));
1435 return method->attr.ma.value_ress;
1438 const char *get_variadicity_name(ir_variadicity vari)
1440 #define X(a) case a: return #a
1442 X(variadicity_non_variadic);
1443 X(variadicity_variadic);
1450 ir_variadicity get_method_variadicity(const ir_type *method)
1452 assert(method && (method->type_op == type_method));
1453 return method->attr.ma.variadicity;
1456 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1458 assert(method && (method->type_op == type_method));
1459 method->attr.ma.variadicity = vari;
1462 int get_method_first_variadic_param_index(const ir_type *method)
1464 assert(method && (method->type_op == type_method));
1466 if (method->attr.ma.variadicity == variadicity_non_variadic)
1469 if (method->attr.ma.first_variadic_param == -1)
1470 return get_method_n_params(method);
1471 return method->attr.ma.first_variadic_param;
1474 void set_method_first_variadic_param_index(ir_type *method, int index)
1476 assert(method && (method->type_op == type_method));
1477 assert(index >= 0 && index <= get_method_n_params(method));
1479 method->attr.ma.first_variadic_param = index;
1482 unsigned (get_method_additional_properties)(const ir_type *method)
1484 return _get_method_additional_properties(method);
1487 void (set_method_additional_properties)(ir_type *method, unsigned mask)
1489 _set_method_additional_properties(method, mask);
1492 void (set_method_additional_property)(ir_type *method,
1493 mtp_additional_property flag)
1495 _set_method_additional_property(method, flag);
1498 unsigned (get_method_calling_convention)(const ir_type *method)
1500 return _get_method_calling_convention(method);
1503 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1505 _set_method_calling_convention(method, cc_mask);
1508 unsigned get_method_n_regparams(ir_type *method)
1510 unsigned cc = get_method_calling_convention(method);
1511 assert(IS_FASTCALL(cc));
1513 return cc & ~cc_bits;
1516 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1518 unsigned cc = get_method_calling_convention(method);
1519 assert(IS_FASTCALL(cc));
1521 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1524 int (is_Method_type)(const ir_type *method)
1526 return _is_method_type(method);
1530 ir_type *new_d_type_union(ident *name, dbg_info *db)
1532 ir_type *res = new_type(type_union, NULL, name, db);
1534 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1539 ir_type *new_type_union(ident *name)
1541 return new_d_type_union(name, NULL);
1544 void free_union_entities(ir_type *uni)
1547 assert(uni && (uni->type_op == type_union));
1548 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1549 free_entity(get_union_member(uni, i));
1552 void free_union_attrs (ir_type *uni)
1554 assert(uni && (uni->type_op == type_union));
1555 DEL_ARR_F(uni->attr.ua.members);
1558 int get_union_n_members(const ir_type *uni)
1560 assert(uni && (uni->type_op == type_union));
1561 return ARR_LEN(uni->attr.ua.members);
1564 void add_union_member(ir_type *uni, ir_entity *member)
1566 assert(uni && (uni->type_op == type_union));
1567 assert(uni != get_entity_type(member) && "recursive type");
1568 assert(get_type_state(uni) != layout_fixed);
1569 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1572 ir_entity *get_union_member(const ir_type *uni, int pos)
1574 assert(uni && (uni->type_op == type_union));
1575 assert(pos >= 0 && pos < get_union_n_members(uni));
1576 return uni->attr.ua.members[pos];
1579 int get_union_member_index(const ir_type *uni, ir_entity *mem)
1582 assert(uni && (uni->type_op == type_union));
1583 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1584 if (get_union_member(uni, i) == mem)
1590 void set_union_member(ir_type *uni, int pos, ir_entity *member)
1592 assert(uni && (uni->type_op == type_union));
1593 assert(pos >= 0 && pos < get_union_n_members(uni));
1594 uni->attr.ua.members[pos] = member;
1597 void remove_union_member(ir_type *uni, ir_entity *member)
1600 assert(uni && (uni->type_op == type_union));
1601 for (i = 0; i < (ARR_LEN(uni->attr.ua.members)); i++) {
1602 if (uni->attr.ua.members[i] == member) {
1603 for(; i < (ARR_LEN(uni->attr.ua.members))-1; i++)
1604 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1605 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1611 int (is_Union_type)(const ir_type *uni)
1613 return _is_union_type(uni);
1616 void set_union_size(ir_type *tp, unsigned size)
1623 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type,
1629 ir_graph *rem = current_ir_graph;
1631 assert(!is_Method_type(element_type));
1633 res = new_type(type_array, NULL, name, db);
1634 res->attr.aa.n_dimensions = n_dimensions;
1635 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1636 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1637 res->attr.aa.order = XMALLOCNZ(int, n_dimensions);
1639 current_ir_graph = get_const_code_irg();
1640 unk = new_Unknown(mode_Iu);
1641 for (i = 0; i < n_dimensions; i++) {
1642 res->attr.aa.lower_bound[i] =
1643 res->attr.aa.upper_bound[i] = unk;
1644 res->attr.aa.order[i] = i;
1646 current_ir_graph = rem;
1648 res->attr.aa.element_type = element_type;
1649 new_entity(res, id_mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1654 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type)
1656 return new_d_type_array(name, n_dimensions, element_type, NULL);
1659 void free_array_automatic_entities(ir_type *array)
1661 assert(array && (array->type_op == type_array));
1662 free_entity(get_array_element_entity(array));
1665 void free_array_entities (ir_type *array)
1668 assert(array && (array->type_op == type_array));
1671 void free_array_attrs (ir_type *array)
1673 assert(array && (array->type_op == type_array));
1674 free(array->attr.aa.lower_bound);
1675 free(array->attr.aa.upper_bound);
1676 free(array->attr.aa.order);
1679 /* manipulate private fields of array ir_type */
1680 int get_array_n_dimensions (const ir_type *array)
1682 assert(array && (array->type_op == type_array));
1683 return array->attr.aa.n_dimensions;
1686 void set_array_bounds(ir_type *array, int dimension, ir_node *lower_bound,
1687 ir_node *upper_bound)
1689 assert(array && (array->type_op == type_array));
1690 assert(lower_bound && "lower_bound node may not be NULL.");
1691 assert(upper_bound && "upper_bound node may not be NULL.");
1692 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1693 array->attr.aa.lower_bound[dimension] = lower_bound;
1694 array->attr.aa.upper_bound[dimension] = upper_bound;
1697 void set_array_bounds_int(ir_type *array, int dimension, int lower_bound,
1700 ir_graph *rem = current_ir_graph;
1701 current_ir_graph = get_const_code_irg();
1702 set_array_bounds(array, dimension,
1703 new_Const_long(mode_Iu, lower_bound),
1704 new_Const_long(mode_Iu, upper_bound));
1705 current_ir_graph = rem;
1708 void set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound)
1710 assert(array && (array->type_op == type_array));
1711 assert(lower_bound && "lower_bound node may not be NULL.");
1712 array->attr.aa.lower_bound[dimension] = lower_bound;
1715 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound)
1717 ir_graph *rem = current_ir_graph;
1718 current_ir_graph = get_const_code_irg();
1719 set_array_lower_bound(array, dimension,
1720 new_Const_long(mode_Iu, lower_bound));
1721 current_ir_graph = rem;
1724 void set_array_upper_bound(ir_type *array, int dimension, ir_node *upper_bound)
1726 assert(array && (array->type_op == type_array));
1727 assert(upper_bound && "upper_bound node may not be NULL.");
1728 array->attr.aa.upper_bound[dimension] = upper_bound;
1731 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound)
1733 ir_graph *rem = current_ir_graph;
1734 current_ir_graph = get_const_code_irg();
1735 set_array_upper_bound(array, dimension,
1736 new_Const_long(mode_Iu, upper_bound));
1737 current_ir_graph = rem;
1740 int has_array_lower_bound(const ir_type *array, int dimension)
1742 assert(array && (array->type_op == type_array));
1743 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1746 ir_node *get_array_lower_bound(const ir_type *array, int dimension)
1748 assert(array && (array->type_op == type_array));
1749 return array->attr.aa.lower_bound[dimension];
1752 long get_array_lower_bound_int(const ir_type *array, int dimension)
1755 assert(array && (array->type_op == type_array));
1756 node = array->attr.aa.lower_bound[dimension];
1757 assert(is_Const(node));
1758 return get_tarval_long(get_Const_tarval(node));
1761 int has_array_upper_bound(const ir_type *array, int dimension)
1763 assert(array && (array->type_op == type_array));
1764 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1767 ir_node *get_array_upper_bound(const ir_type *array, int dimension)
1769 assert(array && (array->type_op == type_array));
1770 return array->attr.aa.upper_bound[dimension];
1773 long get_array_upper_bound_int(const ir_type *array, int dimension)
1776 assert(array && (array->type_op == type_array));
1777 node = array->attr.aa.upper_bound[dimension];
1778 assert(is_Const(node));
1779 return get_tarval_long(get_Const_tarval(node));
1782 void set_array_order(ir_type *array, int dimension, int order)
1784 assert(array && (array->type_op == type_array));
1785 array->attr.aa.order[dimension] = order;
1788 int get_array_order(const ir_type *array, int dimension)
1790 assert(array && (array->type_op == type_array));
1791 return array->attr.aa.order[dimension];
1794 int find_array_dimension(const ir_type *array, int order)
1798 assert(array && (array->type_op == type_array));
1800 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1801 if (array->attr.aa.order[dim] == order)
1807 void set_array_element_type(ir_type *array, ir_type *tp)
1809 assert(array && (array->type_op == type_array));
1810 assert(!is_Method_type(tp));
1811 array->attr.aa.element_type = tp;
1814 ir_type *get_array_element_type(ir_type *array)
1816 assert(array && (array->type_op == type_array));
1817 return array->attr.aa.element_type;
1820 void set_array_element_entity(ir_type *array, ir_entity *ent)
1822 assert(array && (array->type_op == type_array));
1823 assert((get_entity_type(ent)->type_op != type_method));
1824 array->attr.aa.element_ent = ent;
1825 array->attr.aa.element_type = get_entity_type(ent);
1828 ir_entity *get_array_element_entity(const ir_type *array)
1830 assert(array && (array->type_op == type_array));
1831 return array->attr.aa.element_ent;
1834 int (is_Array_type)(const ir_type *array)
1836 return _is_array_type(array);
1839 void set_array_size(ir_type *tp, unsigned size)
1841 /* FIXME: Here we should make some checks with the element type size */
1846 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db)
1850 assert(n_enums >= 0);
1851 res = new_type(type_enumeration, NULL, name, db);
1852 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1857 ir_type *new_type_enumeration(ident *name, int n_enums)
1859 return new_d_type_enumeration(name, n_enums, NULL);
1862 void free_enumeration_entities(ir_type *enumeration)
1865 assert(enumeration && (enumeration->type_op == type_enumeration));
1868 void free_enumeration_attrs(ir_type *enumeration)
1870 assert(enumeration && (enumeration->type_op == type_enumeration));
1871 DEL_ARR_F(enumeration->attr.ea.enumer);
1874 int get_enumeration_n_enums(const ir_type *enumeration)
1876 assert(enumeration && (enumeration->type_op == type_enumeration));
1877 return ARR_LEN(enumeration->attr.ea.enumer);
1880 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid,
1883 assert(0 <= pos && pos < ARR_LEN(enumeration->attr.ea.enumer));
1884 enumeration->attr.ea.enumer[pos].nameid = nameid;
1885 enumeration->attr.ea.enumer[pos].value = con;
1886 enumeration->attr.ea.enumer[pos].owner = enumeration;
1889 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos)
1891 assert(enumeration && (enumeration->type_op == type_enumeration));
1892 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1893 return &enumeration->attr.ea.enumer[pos];
1896 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1898 return enum_cnst->owner;
1901 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con)
1903 enum_cnst->value = con;
1906 tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1908 return enum_cnst->value;
1911 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1913 enum_cnst->nameid = id;
1916 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst)
1918 return enum_cnst->nameid;
1921 const char *get_enumeration_name(const ir_enum_const *enum_cnst)
1923 return get_id_str(enum_cnst->nameid);
1926 int (is_Enumeration_type)(const ir_type *enumeration)
1928 return _is_enumeration_type(enumeration);
1931 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1933 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1934 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1935 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1937 tp->size = get_mode_size_bytes(mode);
1943 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode,
1948 assert(mode_is_reference(ptr_mode));
1949 res = new_type(type_pointer, ptr_mode, name, db);
1950 res->attr.pa.points_to = points_to;
1951 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1952 res->size = get_mode_size_bytes(res->mode);
1953 res->flags |= tf_layout_fixed;
1958 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode)
1960 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1963 void free_pointer_entities(ir_type *pointer)
1966 assert(pointer && (pointer->type_op == type_pointer));
1969 void free_pointer_attrs(ir_type *pointer)
1972 assert(pointer && (pointer->type_op == type_pointer));
1975 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1977 assert(pointer && (pointer->type_op == type_pointer));
1978 pointer->attr.pa.points_to = tp;
1981 ir_type *get_pointer_points_to_type(ir_type *pointer)
1983 assert(pointer && (pointer->type_op == type_pointer));
1984 return pointer->attr.pa.points_to;
1987 int (is_Pointer_type)(const ir_type *pointer)
1989 return _is_pointer_type(pointer);
1992 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1994 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1995 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1996 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1998 tp->size = get_mode_size_bytes(mode);
2002 ir_type *find_pointer_type_to_type(ir_type *tp)
2004 int i, n = get_irp_n_types();
2005 for (i = 0; i < n; ++i) {
2006 ir_type *found = get_irp_type(i);
2007 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
2010 return firm_unknown_type;
2015 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db)
2017 ir_type *res = new_type(type_primitive, mode, name, db);
2018 res->size = get_mode_size_bytes(mode);
2019 res->flags |= tf_layout_fixed;
2020 res->attr.ba.base_type = NULL;
2025 ir_type *new_type_primitive(ident *name, ir_mode *mode)
2027 return new_d_type_primitive(name, mode, NULL);
2030 int (is_Primitive_type)(const ir_type *primitive)
2032 return _is_primitive_type(primitive);
2035 void set_primitive_mode(ir_type *tp, ir_mode *mode)
2037 /* Modes of primitives must be data */
2038 assert(mode_is_data(mode));
2040 /* For primitive size depends on the mode. */
2041 tp->size = get_mode_size_bytes(mode);
2045 ir_type *get_primitive_base_type(ir_type *tp)
2047 assert(is_Primitive_type(tp));
2048 return tp->attr.ba.base_type;
2051 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
2053 assert(is_Primitive_type(tp));
2054 tp->attr.ba.base_type = base_tp;
2059 int (is_atomic_type)(const ir_type *tp)
2061 return _is_atomic_type(tp);
2064 int get_compound_n_members(const ir_type *tp)
2066 const tp_op *op = get_type_tpop(tp);
2069 if (op->ops.get_n_members)
2070 res = op->ops.get_n_members(tp);
2072 assert(0 && "no member count for this type");
2077 ir_entity *get_compound_member(const ir_type *tp, int pos)
2079 const tp_op *op = get_type_tpop(tp);
2080 ir_entity *res = NULL;
2082 if (op->ops.get_member)
2083 res = op->ops.get_member(tp, pos);
2085 assert(0 && "no members in this type");
2090 int get_compound_member_index(const ir_type *tp, ir_entity *member)
2092 const tp_op *op = get_type_tpop(tp);
2095 if (op->ops.get_member_index)
2096 index = op->ops.get_member_index(tp, member);
2098 assert(0 && "no members in this type");
2103 int is_compound_type(const ir_type *tp)
2105 assert(tp && tp->kind == k_type);
2106 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2109 int is_code_type(const ir_type *tp)
2111 assert(tp && tp->kind == k_type);
2112 return tp->type_op == tpop_code;
2115 int is_frame_type(const ir_type *tp)
2117 return tp->flags & tf_frame_type;
2120 int is_value_param_type(const ir_type *tp)
2122 return tp->flags & tf_value_param_type;
2125 int is_lowered_type(const ir_type *tp)
2127 return tp->flags & tf_lowered_type;
2130 ir_type *new_type_value(ident *name)
2132 ir_type *res = new_type_struct(name);
2134 res->flags |= tf_value_param_type;
2136 /* Remove type from type list. Must be treated differently than other types. */
2137 remove_irp_type(res);
2142 ir_type *new_type_frame(ident *name)
2144 ir_type *res = new_type_class(name);
2146 res->flags |= tf_frame_type;
2148 /* Remove type from type list. Must be treated differently than other types. */
2149 remove_irp_type(res);
2151 /* It is not possible to derive from the frame type. Set the final flag. */
2152 set_class_final(res, 1);
2157 ir_type *clone_frame_type(ir_type *type)
2162 assert(is_frame_type(type));
2163 /* the entity link resource should be allocated if this function is called */
2164 assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2166 res = new_type_frame(type->name);
2167 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2168 ir_entity *ent = get_class_member(type, i);
2169 ir_entity *nent = copy_entity_own(ent, res);
2170 set_entity_link(ent, nent);
2171 set_entity_link(nent, ent);
2176 void set_lowered_type(ir_type *tp, ir_type *lowered_type)
2178 assert(is_type(tp) && is_type(lowered_type));
2179 lowered_type->flags |= tf_lowered_type;
2180 tp->assoc_type = lowered_type;
2181 lowered_type->assoc_type = tp;
2184 ir_type *get_associated_type(const ir_type *tp)
2186 return tp->assoc_type;
2189 void set_default_size(ir_type *tp, unsigned size)
2194 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2201 unsigned frame_align;
2202 int i, offset, frame_size;
2203 static unsigned area_cnt = 0;
2204 static ir_type *a_byte = NULL;
2206 assert(is_frame_type(frame_type));
2207 assert(get_type_state(frame_type) == layout_fixed);
2208 assert(get_type_alignment_bytes(frame_type) > 0);
2209 set_type_state(frame_type, layout_undefined);
2212 a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
2214 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2215 name = new_id_from_str(buf);
2217 /* align the size */
2218 frame_align = get_type_alignment_bytes(frame_type);
2219 size = (size + frame_align - 1) & ~(frame_align - 1);
2221 tp = new_type_array(id_mangle_u(get_type_ident(frame_type), name), 1, a_byte);
2222 set_array_bounds_int(tp, 0, 0, size);
2223 set_type_alignment_bytes(tp, alignment);
2225 frame_size = get_type_size_bytes(frame_type);
2227 /* fix all offsets so far */
2228 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2229 ir_entity *ent = get_class_member(frame_type, i);
2231 set_entity_offset(ent, get_entity_offset(ent) + size);
2233 /* calculate offset and new type size */
2237 /* increase size to match alignment... */
2238 if (alignment > frame_align) {
2239 frame_align = alignment;
2240 set_type_alignment_bytes(frame_type, frame_align);
2241 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2244 /* calculate offset and new type size */
2245 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2246 frame_size = offset + size;
2249 area = new_entity(frame_type, name, tp);
2250 set_entity_offset(area, offset);
2251 set_type_size_bytes(frame_type, frame_size);
2253 /* mark this entity as compiler generated */
2254 set_entity_compiler_generated(area, 1);
2256 set_type_state(frame_type, layout_fixed);