2 * Copyright (C) 1995-2011 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.
66 ir_type *firm_none_type;
67 ir_type *get_none_type(void)
69 return firm_none_type;
72 ir_type *firm_code_type;
73 ir_type *get_code_type(void)
75 return firm_code_type;
78 ir_type *firm_unknown_type;
79 ir_type *get_unknown_type(void)
81 return firm_unknown_type;
84 /* Suffixes added to types used for pass-by-value representations. */
85 static ident *value_ress_suffix = NULL;
87 void ir_init_type(void)
89 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
91 /* construct none and unknown type. */
92 firm_none_type = new_type(tpop_none, mode_BAD, NULL);
93 set_type_size_bytes(firm_none_type, 0);
94 set_type_state (firm_none_type, layout_fixed);
95 remove_irp_type(firm_none_type);
97 firm_code_type = new_type(tpop_code, mode_ANY, NULL);
98 set_type_state(firm_code_type, layout_fixed);
99 remove_irp_type(firm_code_type);
101 firm_unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
102 set_type_size_bytes(firm_unknown_type, 0);
103 set_type_state (firm_unknown_type, layout_fixed);
104 remove_irp_type(firm_unknown_type);
107 void ir_finish_type(void)
109 if (firm_none_type != NULL) {
110 free_type(firm_none_type);
111 firm_none_type = NULL;
113 if (firm_code_type != NULL) {
114 free_type(firm_code_type);
115 firm_code_type = NULL;
117 if (firm_unknown_type != NULL) {
118 free_type(firm_unknown_type);
119 firm_unknown_type = NULL;
121 value_ress_suffix = NULL;
124 /** the global type visited flag */
125 ir_visited_t firm_type_visited;
127 void (set_master_type_visited)(ir_visited_t val)
129 _set_master_type_visited(val);
132 ir_visited_t (get_master_type_visited)(void)
134 return _get_master_type_visited();
137 void (inc_master_type_visited)(void)
139 _inc_master_type_visited();
142 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
147 node_size = offsetof(ir_type, attr) + type_op->attr_size;
148 res = (ir_type*)xmalloc(node_size);
149 memset(res, 0, node_size);
152 res->type_op = type_op;
154 res->visibility = ir_visibility_external;
155 res->flags = tf_none;
161 res->assoc_type = NULL;
163 res->nr = get_irp_new_node_nr();
164 #endif /* defined DEBUG_libfirm */
166 add_irp_type(res); /* Remember the new type global. */
171 void free_type(ir_type *tp)
173 const tp_op *op = get_type_tpop(tp);
175 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
176 || (get_type_tpop(tp) == tpop_code))
178 /* Remove from list of all types */
180 /* Free the attributes of the type. */
182 /* Free entities automatically allocated with the ir_type */
183 if (op->ops.free_auto_entities)
184 op->ops.free_auto_entities(tp);
185 /* And now the type itself... */
190 void free_type_entities(ir_type *tp)
192 const tp_op *tpop = get_type_tpop(tp);
194 if (tpop->ops.free_entities)
195 tpop->ops.free_entities(tp);
198 void free_type_attrs(ir_type *tp)
200 const tp_op *tpop = get_type_tpop(tp);
202 if (tpop->ops.free_attrs)
203 tpop->ops.free_attrs(tp);
206 void *(get_type_link)(const ir_type *tp)
208 return _get_type_link(tp);
211 void (set_type_link)(ir_type *tp, void *l)
213 _set_type_link(tp, l);
216 const tp_op *(get_type_tpop)(const ir_type *tp)
218 return _get_type_tpop(tp);
221 ident *(get_type_tpop_nameid)(const ir_type *tp)
223 return _get_type_tpop_nameid(tp);
226 const char* get_type_tpop_name(const ir_type *tp)
228 assert(tp && tp->kind == k_type);
229 return get_id_str(tp->type_op->name);
232 tp_opcode (get_type_tpop_code)(const ir_type *tp)
234 return _get_type_tpop_code(tp);
237 ir_mode *(get_type_mode)(const ir_type *tp)
239 return _get_type_mode(tp);
242 void set_type_mode(ir_type *tp, ir_mode *mode)
244 const tp_op *tpop = get_type_tpop(tp);
246 if (tpop->ops.set_type_mode)
247 tpop->ops.set_type_mode(tp, mode);
249 assert(0 && "setting a mode is NOT allowed for this type");
252 /* Outputs a unique number for this node */
253 long get_type_nr(const ir_type *tp)
259 return (long)PTR_TO_INT(tp);
263 unsigned (get_type_size_bytes)(const ir_type *tp)
265 return _get_type_size_bytes(tp);
268 ir_visibility get_type_visibility(const ir_type *tp)
271 return tp->visibility;
274 void set_type_visibility(ir_type *tp, ir_visibility v)
280 void set_type_size_bytes(ir_type *tp, unsigned size)
282 const tp_op *tpop = get_type_tpop(tp);
284 if (tpop->ops.set_type_size)
285 tpop->ops.set_type_size(tp, size);
287 assert(0 && "Cannot set size for this type");
290 unsigned get_type_alignment_bytes(ir_type *tp)
297 /* alignment NOT set calculate it "on demand" */
299 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
300 else if (is_Array_type(tp))
301 align = get_type_alignment_bytes(get_array_element_type(tp));
302 else if (is_compound_type(tp)) {
303 size_t i, n = get_compound_n_members(tp);
306 for (i = 0; i < n; ++i) {
307 ir_type *t = get_entity_type(get_compound_member(tp, i));
308 unsigned a = get_type_alignment_bytes(t);
313 } else if (is_Method_type(tp)) {
323 void set_type_alignment_bytes(ir_type *tp, unsigned align)
325 assert(tp && tp->kind == k_type);
326 /* Methods don't have an alignment. */
327 if (tp->type_op != type_method) {
332 const char *get_type_state_name(ir_type_state s)
334 #define X(a) case a: return #a;
343 ir_type_state (get_type_state)(const ir_type *tp)
345 return _get_type_state(tp);
348 void set_type_state(ir_type *tp, ir_type_state state)
350 assert(tp && tp->kind == k_type);
352 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
353 (tp->type_op == type_method))
356 /* Just a correctness check: */
357 if (state == layout_fixed) {
359 switch (get_type_tpop_code(tp)) {
361 if (tp != get_glob_type()) {
362 size_t n_mem = get_class_n_members(tp);
363 for (i = 0; i < n_mem; i++) {
364 ir_entity *entity = get_class_member(tp, i);
365 if (is_Method_type(get_entity_type(entity)))
367 assert(get_entity_offset(entity) > -1);
372 for (i = 0; i < get_struct_n_members(tp); i++) {
373 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
380 case tpo_enumeration: {
382 size_t n_enums = get_enumeration_n_enums(tp);
383 assert(get_type_mode(tp) != NULL);
384 for (i = 0; i < n_enums; ++i) {
385 ir_enum_const *ec = get_enumeration_const(tp, i);
386 ir_tarval *tv = get_enumeration_value(ec);
387 assert(tv != NULL && tv != tarval_bad);
395 if (state == layout_fixed)
396 tp->flags |= tf_layout_fixed;
398 tp->flags &= ~tf_layout_fixed;
401 ir_visited_t (get_type_visited)(const ir_type *tp)
403 return _get_type_visited(tp);
406 void (set_type_visited)(ir_type *tp, ir_visited_t num)
408 _set_type_visited(tp, num);
411 void (mark_type_visited)(ir_type *tp)
413 _mark_type_visited(tp);
416 int (type_visited)(const ir_type *tp)
418 return _type_visited(tp);
421 int (type_not_visited)(const ir_type *tp)
423 return _type_not_visited(tp);
426 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
428 return _get_type_dbg_info(tp);
431 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
433 _set_type_dbg_info(tp, db);
436 int (is_type)(const void *thing)
438 return _is_type(thing);
441 /* Checks whether two types are structural equal.*/
442 int equal_type(ir_type *typ1, ir_type *typ2)
449 if (typ1 == typ2) return 1;
451 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
452 typ1->name != typ2->name ||
453 (get_type_mode(typ1) != get_type_mode(typ2)) ||
454 (get_type_state(typ1) != get_type_state(typ2)))
456 if ((get_type_state(typ1) == layout_fixed) &&
457 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
460 switch (get_type_tpop_code(typ1)) {
462 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
463 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
464 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
465 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
466 /** Compare the members **/
467 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
468 /* First sort the members of typ2 */
469 for (i = 0; i < get_class_n_members(typ1); i++) {
470 ir_entity *e1 = get_class_member(typ1, i);
471 for (j = 0; j < get_class_n_members(typ2); j++) {
472 ir_entity *e2 = get_class_member(typ2, j);
473 if (get_entity_name(e1) == get_entity_name(e2))
477 for (i = 0; i < get_class_n_members(typ1); i++) {
478 if (!m[i] || get_class_member(typ1, i) != m[i])
481 /** Compare the supertypes **/
482 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
483 /* First sort the supertypes of typ2 */
484 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
485 ir_type *t1 = get_class_supertype(typ1, i);
486 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
487 ir_type *t2 = get_class_supertype(typ2, j);
488 if (t2->name == t1->name)
492 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
493 if (!t[i] || /* Found no counterpart */
494 get_class_supertype(typ1, i) != t[i])
500 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
501 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
502 /* First sort the members of lt */
503 for (i = 0; i < get_struct_n_members(typ1); i++) {
504 ir_entity *e1 = get_struct_member(typ1, i);
505 for (j = 0; j < get_struct_n_members(typ2); j++) {
506 ir_entity *e2 = get_struct_member(typ2, j);
507 if (get_entity_name(e1) == get_entity_name(e2))
511 for (i = 0; i < get_struct_n_members(typ1); i++) {
512 if (!m[i] || get_struct_member(typ1, i) != m[i])
521 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
522 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
523 if (get_method_calling_convention(typ1) !=
524 get_method_calling_convention(typ2)) return 0;
526 n_param1 = get_method_n_params(typ1);
527 n_param2 = get_method_n_params(typ2);
529 if (n_param1 != n_param2) return 0;
531 for (i = 0; i < n_param1; i++) {
532 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
535 for (i = 0; i < get_method_n_ress(typ1); i++) {
536 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
542 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
543 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
544 /* First sort the members of lt */
545 for (i = 0; i < get_union_n_members(typ1); i++) {
546 ir_entity *e1 = get_union_member(typ1, i);
547 for (j = 0; j < get_union_n_members(typ2); j++) {
548 ir_entity *e2 = get_union_member(typ2, j);
549 if (get_entity_name(e1) == get_entity_name(e2))
553 for (i = 0; i < get_union_n_members(typ1); i++) {
554 if (!m[i] || get_union_member(typ1, i) != m[i])
560 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
562 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
564 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
565 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
566 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
568 if (get_array_order(typ1, i) != get_array_order(typ2, i))
569 assert(0 && "type compare with different dimension orders not implemented");
573 case tpo_enumeration:
574 assert(0 && "enumerations not implemented");
578 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
590 int smaller_type(ir_type *st, ir_type *lt)
597 if (st == lt) return 1;
599 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
602 switch (get_type_tpop_code(st)) {
604 return is_SubClass_of(st, lt);
607 n_st_members = get_struct_n_members(st);
608 if (n_st_members != get_struct_n_members(lt))
611 m = ALLOCANZ(ir_entity*, n_st_members);
612 /* First sort the members of lt */
613 for (i = 0; i < n_st_members; ++i) {
614 ir_entity *se = get_struct_member(st, i);
615 size_t n = get_struct_n_members(lt);
616 for (j = 0; j < n; ++j) {
617 ir_entity *le = get_struct_member(lt, j);
618 if (get_entity_name(le) == get_entity_name(se))
622 for (i = 0; i < n_st_members; i++) {
623 if (!m[i] || /* Found no counterpart */
624 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
630 size_t n_param1, n_param2;
632 /** FIXME: is this still 1? */
633 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
634 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
635 if (get_method_calling_convention(st) !=
636 get_method_calling_convention(lt)) return 0;
638 n_param1 = get_method_n_params(st);
639 n_param2 = get_method_n_params(lt);
641 if (n_param1 != n_param2) return 0;
643 for (i = 0; i < get_method_n_params(st); i++) {
644 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
647 for (i = 0; i < get_method_n_ress(st); i++) {
648 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
654 n_st_members = get_union_n_members(st);
655 if (n_st_members != get_union_n_members(lt)) return 0;
656 m = ALLOCANZ(ir_entity*, n_st_members);
657 /* First sort the members of lt */
658 for (i = 0; i < n_st_members; ++i) {
659 ir_entity *se = get_union_member(st, i);
660 size_t n = get_union_n_members(lt);
661 for (j = 0; j < n; ++j) {
662 ir_entity *le = get_union_member(lt, j);
663 if (get_entity_name(le) == get_entity_name(se))
667 for (i = 0; i < n_st_members; ++i) {
668 if (!m[i] || /* Found no counterpart */
669 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
675 ir_type *set, *let; /* small/large elt. ir_type */
676 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
678 set = get_array_element_type(st);
679 let = get_array_element_type(lt);
681 /* If the element types are different, set must be convertible
682 to let, and they must have the same size so that address
683 computations work out. To have a size the layout must
685 if ((get_type_state(set) != layout_fixed) ||
686 (get_type_state(let) != layout_fixed))
688 if (!smaller_type(set, let) ||
689 get_type_size_bytes(set) != get_type_size_bytes(let))
692 for (i = 0; i < get_array_n_dimensions(st); i++) {
693 if (get_array_lower_bound(lt, i))
694 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
696 if (get_array_upper_bound(lt, i))
697 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
702 case tpo_enumeration:
703 assert(0 && "enumerations not implemented");
707 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
712 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
722 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
726 res = new_type(type_class, NULL, db);
729 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
730 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
731 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
732 res->attr.ca.peculiarity = peculiarity_existent;
733 res->attr.ca.type_info = NULL;
734 res->attr.ca.vtable_size = 0;
735 res->attr.ca.clss_flags = cf_none;
736 res->attr.ca.dfn = 0;
741 ir_type *new_type_class(ident *name)
743 return new_d_type_class(name, NULL);
746 void free_class_entities(ir_type *clss)
749 assert(clss && (clss->type_op == type_class));
750 /* we must iterate backward here */
751 for (i = get_class_n_members(clss); i > 0;)
752 free_entity(get_class_member(clss, --i));
753 /* do NOT free the type info here. It belongs to another class */
756 void free_class_attrs(ir_type *clss)
758 assert(clss && (clss->type_op == type_class));
759 DEL_ARR_F(clss->attr.ca.members);
760 DEL_ARR_F(clss->attr.ca.subtypes);
761 DEL_ARR_F(clss->attr.ca.supertypes);
764 ident *get_class_ident(const ir_type *clss)
766 assert(clss->type_op == type_class);
770 const char *get_class_name(const ir_type *clss)
772 if (get_class_ident(clss) == NULL)
774 return get_id_str(get_class_ident(clss));
777 static void add_class_member(ir_type *clss, ir_entity *member)
779 assert(clss && (clss->type_op == type_class));
780 assert(clss != get_entity_type(member) && "recursive type");
781 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
784 size_t (get_class_n_members)(const ir_type *clss)
786 return _get_class_n_members(clss);
789 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
792 assert(clss && (clss->type_op == type_class));
793 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
794 if (get_class_member(clss, i) == mem)
797 return INVALID_MEMBER_INDEX;
800 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
802 return _get_class_member(clss, pos);
805 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
808 assert(clss && (clss->type_op == type_class));
809 n_mem = get_class_n_members(clss);
810 for (i = 0; i < n_mem; ++i) {
811 ir_entity *mem = get_class_member(clss, i);
812 if (get_entity_ident(mem) == name)
818 static void remove_class_member(ir_type *clss, ir_entity *member)
821 assert(clss && (clss->type_op == type_class));
822 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
823 if (clss->attr.ca.members[i] == member) {
824 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
825 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
826 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
832 void add_class_subtype(ir_type *clss, ir_type *subtype)
835 assert(clss->type_op == type_class);
836 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
837 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
838 if (get_class_supertype(subtype, i) == clss)
839 /* Class already registered */
842 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
845 size_t get_class_n_subtypes(const ir_type *clss)
847 assert(clss->type_op == type_class);
848 return ARR_LEN (clss->attr.ca.subtypes);
851 ir_type *get_class_subtype(ir_type *clss, size_t pos)
853 assert(clss->type_op == type_class);
854 assert(pos < get_class_n_subtypes(clss));
855 return clss->attr.ca.subtypes[pos];
858 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
860 size_t i, n_subtypes = get_class_n_subtypes(clss);
861 assert(is_Class_type(subclass));
862 for (i = 0; i < n_subtypes; ++i) {
863 if (get_class_subtype(clss, i) == subclass)
869 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
871 assert(clss->type_op == type_class);
872 assert(pos < get_class_n_subtypes(clss));
873 clss->attr.ca.subtypes[pos] = subtype;
876 void remove_class_subtype(ir_type *clss, ir_type *subtype)
879 assert(clss && (clss->type_op == type_class));
880 for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
881 if (clss->attr.ca.subtypes[i] == subtype) {
882 for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
883 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
884 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
890 void add_class_supertype(ir_type *clss, ir_type *supertype)
894 assert(clss && (clss->type_op == type_class));
895 assert(supertype && (supertype -> type_op == type_class));
896 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
897 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
898 if (get_class_subtype(supertype, i) == clss)
899 /* Class already registered */
902 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
905 size_t get_class_n_supertypes(const ir_type *clss)
907 assert(clss->type_op == type_class);
908 return ARR_LEN(clss->attr.ca.supertypes);
911 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
913 size_t i, n_supertypes = get_class_n_supertypes(clss);
914 assert(super_clss && (super_clss->type_op == type_class));
915 for (i = 0; i < n_supertypes; i++) {
916 if (get_class_supertype(clss, i) == super_clss)
922 ir_type *get_class_supertype(ir_type *clss, size_t pos)
924 assert(clss->type_op == type_class);
925 assert(pos < get_class_n_supertypes(clss));
926 return clss->attr.ca.supertypes[pos];
929 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
931 assert(clss->type_op == type_class);
932 assert(pos < get_class_n_supertypes(clss));
933 clss->attr.ca.supertypes[pos] = supertype;
936 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(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
950 ir_entity *get_class_type_info(const ir_type *clss)
952 return clss->attr.ca.type_info;
955 void set_class_type_info(ir_type *clss, ir_entity *ent)
957 clss->attr.ca.type_info = ent;
959 ent->repr_class = clss;
962 ir_peculiarity get_class_peculiarity(const ir_type *clss)
964 assert(clss && (clss->type_op == type_class));
965 return clss->attr.ca.peculiarity;
968 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
970 assert(clss && (clss->type_op == type_class));
971 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
972 clss->attr.ca.peculiarity = pec;
975 unsigned (get_class_vtable_size)(const ir_type *clss)
977 return _get_class_vtable_size(clss);
980 void (set_class_vtable_size)(ir_type *clss, unsigned size)
982 _set_class_vtable_size(clss, size);
985 int (is_class_final)(const ir_type *clss)
987 return _is_class_final(clss);
990 void (set_class_final)(ir_type *clss, int flag)
992 _set_class_final(clss, flag);
995 int (is_class_interface)(const ir_type *clss)
997 return _is_class_interface(clss);
1000 void (set_class_interface)(ir_type *clss, int flag)
1002 _set_class_interface(clss, flag);
1005 int (is_class_abstract)(const ir_type *clss)
1007 return _is_class_abstract(clss);
1010 void (set_class_abstract)(ir_type *clss, int final)
1012 _set_class_abstract(clss, final);
1015 void set_class_dfn(ir_type *clss, int dfn)
1017 clss->attr.ca.dfn = dfn;
1020 int get_class_dfn(const ir_type *clss)
1022 return (clss->attr.ca.dfn);
1025 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)
1032 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1033 assert(get_type_state(tp) == layout_fixed &&
1034 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1038 void set_class_size(ir_type *tp, unsigned size)
1044 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1046 ir_type *res = new_type(type_struct, NULL, db);
1049 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1054 ir_type *new_type_struct(ident *name)
1056 return new_d_type_struct (name, NULL);
1059 void free_struct_entities(ir_type *strct)
1062 assert(strct && (strct->type_op == type_struct));
1063 /* we must iterate backward here */
1064 for (i = get_struct_n_members(strct); i > 0;)
1065 free_entity(get_struct_member(strct, --i));
1068 void free_struct_attrs(ir_type *strct)
1070 assert(strct && (strct->type_op == type_struct));
1071 DEL_ARR_F(strct->attr.sa.members);
1074 ident *get_struct_ident(const ir_type *strct)
1076 assert(strct->type_op == type_struct);
1080 const char *get_struct_name(const ir_type *strct)
1082 if (get_struct_ident(strct) == NULL)
1084 return get_id_str(get_struct_ident(strct));
1087 size_t get_struct_n_members(const ir_type *strct)
1089 assert(strct->type_op == type_struct);
1090 return ARR_LEN(strct->attr.sa.members);
1093 static void add_struct_member(ir_type *strct, ir_entity *member)
1095 assert(strct && (strct->type_op == type_struct));
1096 assert(get_type_tpop(get_entity_type(member)) != type_method);
1097 assert(strct != get_entity_type(member) && "recursive type");
1098 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1101 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1103 assert(strct && (strct->type_op == type_struct));
1104 assert(pos < get_struct_n_members(strct));
1105 return strct->attr.sa.members[pos];
1108 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1111 assert(strct && (strct->type_op == type_struct));
1112 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1113 if (get_struct_member(strct, i) == mem)
1119 static void remove_struct_member(ir_type *strct, ir_entity *member)
1122 assert(strct && (strct->type_op == type_struct));
1123 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1124 if (strct->attr.sa.members[i] == member) {
1125 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1126 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1127 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1133 int (is_Struct_type)(const ir_type *strct)
1135 return _is_struct_type(strct);
1138 void set_struct_mode(ir_type *tp, ir_mode *mode)
1140 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1141 assert(get_type_state(tp) == layout_fixed &&
1142 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1146 void set_struct_size(ir_type *tp, unsigned size)
1153 * Lazy construction of value argument / result representation.
1154 * Constructs a struct type and its member. The types of the members
1155 * are passed in the argument list.
1157 * @param name name of the type constructed
1158 * @param len number of fields
1159 * @param tps array of field types with length len
1161 static ir_type *build_value_type(char const* name, size_t len, tp_ent_pair *tps)
1164 ir_type *res = new_type_struct(new_id_from_str(name));
1165 res->flags |= tf_value_param_type;
1166 /* Remove type from type list. Must be treated differently than other types. */
1167 remove_irp_type(res);
1168 for (i = 0; i < len; ++i) {
1169 ident *id = new_id_from_str("elt");
1171 /* use res as default if corresponding type is not yet set. */
1172 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1173 tps[i].ent = new_entity(res, id, elt_type);
1174 set_entity_allocation(tps[i].ent, allocation_parameter);
1179 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1183 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1184 res = new_type(type_method, mode_P_code, db);
1185 res->flags |= tf_layout_fixed;
1186 res->size = get_mode_size_bytes(mode_P_code);
1187 res->attr.ma.n_params = n_param;
1188 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1189 res->attr.ma.value_params = NULL;
1190 res->attr.ma.n_res = n_res;
1191 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1192 res->attr.ma.value_ress = NULL;
1193 res->attr.ma.variadicity = variadicity_non_variadic;
1194 res->attr.ma.additional_properties = mtp_no_property;
1199 ir_type *new_type_method(size_t n_param, size_t n_res)
1201 return new_d_type_method(n_param, n_res, NULL);
1204 ir_type *clone_type_method(ir_type *tp)
1212 assert(is_Method_type(tp));
1215 n_params = tp->attr.ma.n_params;
1216 n_res = tp->attr.ma.n_res;
1219 res = new_type(type_method, mode, db);
1221 res->flags = tp->flags;
1222 res->assoc_type = tp->assoc_type;
1223 res->size = tp->size;
1224 res->attr.ma.n_params = n_params;
1225 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1226 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1227 res->attr.ma.value_params = tp->attr.ma.value_params;
1228 res->attr.ma.n_res = n_res;
1229 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1230 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1231 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1232 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1233 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1234 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1239 void free_method_entities(ir_type *method)
1242 assert(method && (method->type_op == type_method));
1245 void free_method_attrs(ir_type *method)
1247 assert(method && (method->type_op == type_method));
1248 free(method->attr.ma.params);
1249 free(method->attr.ma.res_type);
1250 /* cannot free it yet, type could be cloned ...
1251 if (method->attr.ma.value_params) {
1252 free_type_entities(method->attr.ma.value_params);
1253 free_type(method->attr.ma.value_params);
1256 if (method->attr.ma.value_ress) {
1257 free_type_entities(method->attr.ma.value_ress);
1258 free_type(method->attr.ma.value_ress);
1262 size_t (get_method_n_params)(const ir_type *method)
1264 return _get_method_n_params(method);
1267 ir_type *get_method_param_type(ir_type *method, size_t pos)
1270 assert(method->type_op == type_method);
1271 assert(pos < get_method_n_params(method));
1272 res = method->attr.ma.params[pos].tp;
1273 assert(res != NULL && "empty method param type");
1277 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1279 assert(method->type_op == type_method);
1280 assert(pos < get_method_n_params(method));
1281 method->attr.ma.params[pos].tp = tp;
1282 /* If information constructed set pass-by-value representation. */
1283 if (method->attr.ma.value_params) {
1284 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1285 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1289 ir_entity *get_method_value_param_ent(ir_type *method, size_t pos)
1291 assert(method && (method->type_op == type_method));
1292 assert(pos < get_method_n_params(method));
1294 if (!method->attr.ma.value_params) {
1295 /* parameter value type not created yet, build */
1296 method->attr.ma.value_params = build_value_type("<value param>",
1297 get_method_n_params(method), method->attr.ma.params);
1300 * build_value_type() sets the method->attr.ma.value_params type as default if
1303 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1304 && "param type not yet set");
1305 return method->attr.ma.params[pos].ent;
1308 void set_method_value_param_type(ir_type *method, ir_type *tp)
1313 assert(method && (method->type_op == type_method));
1314 assert(is_value_param_type(tp));
1315 assert(get_method_n_params(method) == get_struct_n_members(tp));
1317 method->attr.ma.value_params = tp;
1319 n = get_struct_n_members(tp);
1320 for (i = 0; i < n; i++) {
1321 ir_entity *ent = get_struct_member(tp, i);
1322 method->attr.ma.params[i].ent = ent;
1326 ir_type *get_method_value_param_type(const ir_type *method)
1328 assert(method && (method->type_op == type_method));
1329 return method->attr.ma.value_params;
1332 size_t (get_method_n_ress)(const ir_type *method)
1334 return _get_method_n_ress(method);
1337 ir_type *get_method_res_type(ir_type *method, size_t pos)
1340 assert(method->type_op == type_method);
1341 assert(pos < get_method_n_ress(method));
1342 res = method->attr.ma.res_type[pos].tp;
1343 assert(res != NULL && "empty method return type");
1347 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1349 assert(method->type_op == type_method);
1350 assert(pos < get_method_n_ress(method));
1351 /* set the result ir_type */
1352 method->attr.ma.res_type[pos].tp = tp;
1353 /* If information constructed set pass-by-value representation. */
1354 if (method->attr.ma.value_ress) {
1355 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1356 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1360 ir_entity *get_method_value_res_ent(ir_type *method, size_t pos)
1362 assert(method->type_op == type_method);
1363 assert(pos < get_method_n_ress(method));
1365 if (!method->attr.ma.value_ress) {
1366 /* result value type not created yet, build */
1367 method->attr.ma.value_ress = build_value_type("<value result>",
1368 get_method_n_ress(method), method->attr.ma.res_type);
1371 * build_value_type() sets the method->attr.ma.value_ress type as default if
1374 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1375 && "result type not yet set");
1377 return method->attr.ma.res_type[pos].ent;
1380 ir_type *get_method_value_res_type(const ir_type *method)
1382 assert(method->type_op == type_method);
1383 return method->attr.ma.value_ress;
1386 const char *get_variadicity_name(ir_variadicity vari)
1388 #define X(a) case a: return #a
1390 X(variadicity_non_variadic);
1391 X(variadicity_variadic);
1398 ir_variadicity get_method_variadicity(const ir_type *method)
1400 assert(method && (method->type_op == type_method));
1401 return method->attr.ma.variadicity;
1404 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1406 assert(method && (method->type_op == type_method));
1407 method->attr.ma.variadicity = vari;
1410 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1412 return _get_method_additional_properties(method);
1415 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1417 _set_method_additional_properties(method, mask);
1420 void (add_method_additional_properties)(ir_type *method,
1421 mtp_additional_properties flag)
1423 _add_method_additional_properties(method, flag);
1426 unsigned (get_method_calling_convention)(const ir_type *method)
1428 return _get_method_calling_convention(method);
1431 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1433 _set_method_calling_convention(method, cc_mask);
1436 unsigned get_method_n_regparams(ir_type *method)
1438 unsigned cc = get_method_calling_convention(method);
1439 assert(IS_FASTCALL(cc));
1441 return cc & ~cc_bits;
1444 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1446 unsigned cc = get_method_calling_convention(method);
1447 assert(IS_FASTCALL(cc));
1449 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1452 int (is_Method_type)(const ir_type *method)
1454 return _is_method_type(method);
1458 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1460 ir_type *res = new_type(type_union, NULL, db);
1463 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1468 ir_type *new_type_union(ident *name)
1470 return new_d_type_union(name, NULL);
1473 void free_union_entities(ir_type *uni)
1476 assert(uni && (uni->type_op == type_union));
1477 /* we must iterate backward here */
1478 for (i = get_union_n_members(uni); i > 0;)
1479 free_entity(get_union_member(uni, --i));
1482 void free_union_attrs(ir_type *uni)
1484 assert(uni && (uni->type_op == type_union));
1485 DEL_ARR_F(uni->attr.ua.members);
1488 ident *get_union_ident(const ir_type *uni)
1490 assert(uni->type_op == type_union);
1494 const char *get_union_name(const ir_type *uni)
1496 if (get_union_ident(uni) == NULL)
1498 return get_id_str(get_union_ident(uni));
1501 size_t get_union_n_members(const ir_type *uni)
1503 assert(uni->type_op == type_union);
1504 return ARR_LEN(uni->attr.ua.members);
1507 static void add_union_member(ir_type *uni, ir_entity *member)
1509 assert(uni->type_op == type_union);
1510 assert(uni != get_entity_type(member) && "recursive type");
1511 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1514 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1516 assert(uni->type_op == type_union);
1517 assert(pos < get_union_n_members(uni));
1518 return uni->attr.ua.members[pos];
1521 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1524 assert(uni && (uni->type_op == type_union));
1525 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1526 if (get_union_member(uni, i) == mem)
1532 static void remove_union_member(ir_type *uni, ir_entity *member)
1535 assert(uni && (uni->type_op == type_union));
1536 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1537 if (uni->attr.ua.members[i] == member) {
1538 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1539 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1540 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1546 int (is_Union_type)(const ir_type *uni)
1548 return _is_union_type(uni);
1551 void set_union_size(ir_type *tp, unsigned size)
1558 ir_type *new_d_type_array(int n_dimensions, ir_type *element_type,
1564 ir_graph *irg = get_const_code_irg();
1566 assert(!is_Method_type(element_type));
1568 res = new_type(type_array, NULL, db);
1569 res->attr.aa.n_dimensions = n_dimensions;
1570 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1571 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1572 res->attr.aa.order = XMALLOCNZ(int, n_dimensions);
1574 unk = new_r_Unknown(irg, mode_Iu);
1575 for (i = 0; i < n_dimensions; i++) {
1576 res->attr.aa.lower_bound[i] =
1577 res->attr.aa.upper_bound[i] = unk;
1578 res->attr.aa.order[i] = i;
1581 res->attr.aa.element_type = element_type;
1582 res->attr.aa.element_ent
1583 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1584 res->attr.aa.element_ent->owner = res;
1590 ir_type *new_type_array(int n_dimensions, ir_type *element_type)
1592 return new_d_type_array(n_dimensions, element_type, NULL);
1595 void free_array_automatic_entities(ir_type *array)
1597 assert(array && (array->type_op == type_array));
1598 free_entity(get_array_element_entity(array));
1601 void free_array_entities(ir_type *array)
1604 assert(array->type_op == type_array);
1607 void free_array_attrs(ir_type *array)
1609 assert(array->type_op == type_array);
1610 free(array->attr.aa.lower_bound);
1611 free(array->attr.aa.upper_bound);
1612 free(array->attr.aa.order);
1615 /* manipulate private fields of array ir_type */
1616 size_t get_array_n_dimensions(const ir_type *array)
1618 assert(array->type_op == type_array);
1619 return array->attr.aa.n_dimensions;
1622 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1623 ir_node *upper_bound)
1625 assert(array && (array->type_op == type_array));
1626 assert(lower_bound && "lower_bound node may not be NULL.");
1627 assert(upper_bound && "upper_bound node may not be NULL.");
1628 assert(dimension < array->attr.aa.n_dimensions);
1629 array->attr.aa.lower_bound[dimension] = lower_bound;
1630 array->attr.aa.upper_bound[dimension] = upper_bound;
1633 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1636 ir_graph *irg = get_const_code_irg();
1637 set_array_bounds(array, dimension,
1638 new_r_Const_long(irg, mode_Iu, lower_bound),
1639 new_r_Const_long(irg, mode_Iu, upper_bound));
1642 void set_array_lower_bound(ir_type *array, size_t dimension,
1643 ir_node *lower_bound)
1645 assert(array && (array->type_op == type_array));
1646 assert(lower_bound && "lower_bound node may not be NULL.");
1647 array->attr.aa.lower_bound[dimension] = lower_bound;
1650 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1652 ir_graph *irg = get_const_code_irg();
1653 set_array_lower_bound(array, dimension,
1654 new_r_Const_long(irg, mode_Iu, lower_bound));
1657 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1659 assert(array && (array->type_op == type_array));
1660 assert(upper_bound && "upper_bound node may not be NULL.");
1661 array->attr.aa.upper_bound[dimension] = upper_bound;
1664 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1666 ir_graph *irg = get_const_code_irg();
1667 set_array_upper_bound(array, dimension,
1668 new_r_Const_long(irg, mode_Iu, upper_bound));
1671 int has_array_lower_bound(const ir_type *array, size_t dimension)
1673 assert(array && (array->type_op == type_array));
1674 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1677 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1679 assert(array && (array->type_op == type_array));
1680 return array->attr.aa.lower_bound[dimension];
1683 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1686 assert(array && (array->type_op == type_array));
1687 node = array->attr.aa.lower_bound[dimension];
1688 assert(is_Const(node));
1689 return get_tarval_long(get_Const_tarval(node));
1692 int has_array_upper_bound(const ir_type *array, size_t dimension)
1694 assert(array && (array->type_op == type_array));
1695 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1698 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1700 assert(array && (array->type_op == type_array));
1701 return array->attr.aa.upper_bound[dimension];
1704 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1707 assert(array && (array->type_op == type_array));
1708 node = array->attr.aa.upper_bound[dimension];
1709 assert(is_Const(node));
1710 return get_tarval_long(get_Const_tarval(node));
1713 void set_array_order(ir_type *array, size_t dimension, int order)
1715 assert(array && (array->type_op == type_array));
1716 array->attr.aa.order[dimension] = order;
1719 int get_array_order(const ir_type *array, size_t dimension)
1721 assert(array && (array->type_op == type_array));
1722 return array->attr.aa.order[dimension];
1725 size_t find_array_dimension(const ir_type *array, int order)
1729 assert(array->type_op == type_array);
1731 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1732 if (array->attr.aa.order[dim] == order)
1738 void set_array_element_type(ir_type *array, ir_type *tp)
1740 assert(array && (array->type_op == type_array));
1741 assert(!is_Method_type(tp));
1742 array->attr.aa.element_type = tp;
1745 ir_type *get_array_element_type(const ir_type *array)
1747 assert(array && (array->type_op == type_array));
1748 return array->attr.aa.element_type;
1751 void set_array_element_entity(ir_type *array, ir_entity *ent)
1753 assert(array && (array->type_op == type_array));
1754 assert((get_entity_type(ent)->type_op != type_method));
1755 array->attr.aa.element_ent = ent;
1756 array->attr.aa.element_type = get_entity_type(ent);
1759 ir_entity *get_array_element_entity(const ir_type *array)
1761 assert(array && (array->type_op == type_array));
1762 return array->attr.aa.element_ent;
1765 int (is_Array_type)(const ir_type *array)
1767 return _is_array_type(array);
1770 void set_array_size(ir_type *tp, unsigned size)
1772 /* FIXME: Here we should make some checks with the element type size */
1777 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1781 res = new_type(type_enumeration, NULL, db);
1783 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1788 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1790 return new_d_type_enumeration(name, n_enums, NULL);
1793 void free_enumeration_entities(ir_type *enumeration)
1796 assert(enumeration->type_op == type_enumeration);
1799 void free_enumeration_attrs(ir_type *enumeration)
1801 assert(enumeration->type_op == type_enumeration);
1802 DEL_ARR_F(enumeration->attr.ea.enumer);
1805 ident *get_enumeration_ident(const ir_type *enumeration)
1807 assert(enumeration->type_op == type_enumeration);
1808 return enumeration->name;
1811 const char *get_enumeration_name(const ir_type *enumeration)
1813 if (get_enumeration_ident(enumeration) == NULL)
1815 return get_id_str(get_enumeration_ident(enumeration));
1818 size_t get_enumeration_n_enums(const ir_type *enumeration)
1820 assert(enumeration->type_op == type_enumeration);
1821 return ARR_LEN(enumeration->attr.ea.enumer);
1824 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1827 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1828 enumeration->attr.ea.enumer[pos].nameid = nameid;
1829 enumeration->attr.ea.enumer[pos].value = con;
1830 enumeration->attr.ea.enumer[pos].owner = enumeration;
1833 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1835 assert(enumeration->type_op == type_enumeration);
1836 assert(pos < get_enumeration_n_enums(enumeration));
1837 return &enumeration->attr.ea.enumer[pos];
1840 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1842 return enum_cnst->owner;
1845 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1847 enum_cnst->value = con;
1850 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1852 return enum_cnst->value;
1855 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1857 enum_cnst->nameid = id;
1860 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1862 return enum_cnst->nameid;
1865 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1867 return get_id_str(enum_cnst->nameid);
1870 int (is_Enumeration_type)(const ir_type *enumeration)
1872 return _is_enumeration_type(enumeration);
1875 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1877 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1878 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1879 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1881 tp->size = get_mode_size_bytes(mode);
1887 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1892 if (is_Method_type(points_to) || is_code_type(points_to)) {
1898 res = new_type(type_pointer, mode, db);
1899 res->attr.pa.points_to = points_to;
1900 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1901 res->size = get_mode_size_bytes(res->mode);
1902 res->flags |= tf_layout_fixed;
1907 ir_type *new_type_pointer(ir_type *points_to)
1909 return new_d_type_pointer(points_to, NULL);
1912 void free_pointer_entities(ir_type *pointer)
1915 assert(pointer && (pointer->type_op == type_pointer));
1918 void free_pointer_attrs(ir_type *pointer)
1921 assert(pointer && (pointer->type_op == type_pointer));
1924 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1926 assert(pointer && (pointer->type_op == type_pointer));
1927 pointer->attr.pa.points_to = tp;
1930 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1932 assert(pointer && (pointer->type_op == type_pointer));
1933 return pointer->attr.pa.points_to;
1936 int (is_Pointer_type)(const ir_type *pointer)
1938 return _is_pointer_type(pointer);
1941 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1943 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1944 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1945 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1947 tp->size = get_mode_size_bytes(mode);
1951 ir_type *find_pointer_type_to_type(ir_type *tp)
1953 size_t i, n = get_irp_n_types();
1954 for (i = 0; i < n; ++i) {
1955 ir_type *found = get_irp_type(i);
1956 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1959 return firm_unknown_type;
1964 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1966 ir_type *res = new_type(type_primitive, mode, db);
1967 res->size = get_mode_size_bytes(mode);
1968 res->flags |= tf_layout_fixed;
1969 res->attr.ba.base_type = NULL;
1974 ir_type *new_type_primitive(ir_mode *mode)
1976 return new_d_type_primitive(mode, NULL);
1979 int (is_Primitive_type)(const ir_type *primitive)
1981 return _is_primitive_type(primitive);
1984 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1986 /* Modes of primitives must be data */
1987 assert(mode_is_data(mode));
1989 /* For primitive size depends on the mode. */
1990 tp->size = get_mode_size_bytes(mode);
1994 ir_type *get_primitive_base_type(const ir_type *tp)
1996 assert(is_Primitive_type(tp));
1997 return tp->attr.ba.base_type;
2000 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
2002 assert(is_Primitive_type(tp));
2003 tp->attr.ba.base_type = base_tp;
2008 int (is_atomic_type)(const ir_type *tp)
2010 return _is_atomic_type(tp);
2013 size_t get_compound_n_members(const ir_type *tp)
2015 const tp_op *op = get_type_tpop(tp);
2016 return op->ops.get_n_members(tp);
2019 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
2021 const tp_op *op = get_type_tpop(tp);
2022 return op->ops.get_member(tp, pos);
2025 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
2027 const tp_op *op = get_type_tpop(tp);
2028 return op->ops.get_member_index(tp, member);
2031 int is_compound_type(const ir_type *tp)
2033 assert(tp->kind == k_type);
2034 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2037 ident *get_compound_ident(const ir_type *tp)
2039 assert(is_compound_type(tp));
2043 const char *get_compound_name(const ir_type *tp)
2045 if (get_compound_ident(tp) == NULL)
2047 return get_id_str(get_compound_ident(tp));
2050 void remove_compound_member(ir_type *compound, ir_entity *entity)
2052 switch (get_type_tpop_code(compound)) {
2053 case tpo_class: remove_class_member(compound, entity); break;
2054 case tpo_struct: remove_struct_member(compound, entity); break;
2055 case tpo_union: remove_union_member(compound, entity); break;
2057 panic("argument for remove_compound_member not a compound type");
2061 void add_compound_member(ir_type *compound, ir_entity *entity)
2063 switch (get_type_tpop_code(compound)) {
2064 case tpo_class: add_class_member(compound, entity); break;
2065 case tpo_struct: add_struct_member(compound, entity); break;
2066 case tpo_union: add_union_member(compound, entity); break;
2068 panic("argument for add_compound_member not a compound type");
2074 int is_code_type(const ir_type *tp)
2076 assert(tp && tp->kind == k_type);
2077 return tp->type_op == tpop_code;
2080 int is_frame_type(const ir_type *tp)
2082 return tp->flags & tf_frame_type;
2085 int is_value_param_type(const ir_type *tp)
2087 return tp->flags & tf_value_param_type;
2090 int is_lowered_type(const ir_type *tp)
2092 return tp->flags & tf_lowered_type;
2095 ir_type *new_type_value(void)
2097 ir_type *res = new_type_struct(new_id_from_str("<value_type>"));
2099 res->flags |= tf_value_param_type;
2101 /* Remove type from type list. Must be treated differently than other types. */
2102 remove_irp_type(res);
2107 ir_type *new_type_frame(void)
2109 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
2111 res->flags |= tf_frame_type;
2113 /* Remove type from type list. Must be treated differently than other types. */
2114 remove_irp_type(res);
2116 /* It is not possible to derive from the frame type. Set the final flag. */
2117 set_class_final(res, 1);
2122 ir_type *clone_frame_type(ir_type *type)
2127 assert(is_frame_type(type));
2128 /* the entity link resource should be allocated if this function is called */
2129 assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2131 res = new_type_frame();
2132 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2133 ir_entity *ent = get_class_member(type, i);
2134 ir_entity *nent = copy_entity_own(ent, res);
2135 set_entity_link(ent, nent);
2136 set_entity_link(nent, ent);
2141 void set_lowered_type(ir_type *tp, ir_type *lowered_type)
2143 assert(is_type(tp) && is_type(lowered_type));
2144 lowered_type->flags |= tf_lowered_type;
2145 tp->assoc_type = lowered_type;
2146 lowered_type->assoc_type = tp;
2149 ir_type *get_associated_type(const ir_type *tp)
2151 return tp->assoc_type;
2154 void set_default_size(ir_type *tp, unsigned size)
2159 void default_layout_compound_type(ir_type *type)
2162 size_t n = get_compound_n_members(type);
2164 unsigned align_all = 1;
2166 for (i = 0; i < n; ++i) {
2167 ir_entity *entity = get_compound_member(type, i);
2168 ir_type *entity_type = get_entity_type(entity);
2172 if (is_Method_type(entity_type))
2175 assert(get_type_state(entity_type) == layout_fixed);
2176 align = get_type_alignment_bytes(entity_type);
2177 align_all = align > align_all ? align : align_all;
2178 misalign = (align ? size % align : 0);
2179 size += (misalign ? align - misalign : 0);
2181 set_entity_offset(entity, size);
2182 if (!is_Union_type(type)) {
2183 size += get_type_size_bytes(entity_type);
2186 if (align_all > 0 && size % align_all) {
2187 size += align_all - (size % align_all);
2189 if (align_all > get_type_alignment_bytes(type)) {
2190 set_type_alignment_bytes(type, align_all);
2192 set_type_size_bytes(type, size);
2193 set_type_state(type, layout_fixed);
2196 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2203 unsigned frame_align;
2204 int offset, frame_size;
2205 static unsigned area_cnt = 0;
2206 static ir_type *a_byte = NULL;
2208 assert(is_frame_type(frame_type));
2209 assert(get_type_state(frame_type) == layout_fixed);
2210 assert(get_type_alignment_bytes(frame_type) > 0);
2211 set_type_state(frame_type, layout_undefined);
2214 a_byte = new_type_primitive(mode_Bu);
2216 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2217 name = new_id_from_str(buf);
2219 /* align the size */
2220 frame_align = get_type_alignment_bytes(frame_type);
2221 size = (size + frame_align - 1) & ~(frame_align - 1);
2223 tp = new_type_array(1, a_byte);
2224 set_array_bounds_int(tp, 0, 0, size);
2225 set_type_alignment_bytes(tp, alignment);
2227 frame_size = get_type_size_bytes(frame_type);
2230 /* fix all offsets so far */
2231 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2232 ir_entity *ent = get_class_member(frame_type, i);
2234 set_entity_offset(ent, get_entity_offset(ent) + size);
2236 /* calculate offset and new type size */
2240 /* increase size to match alignment... */
2241 if (alignment > frame_align) {
2242 frame_align = alignment;
2243 set_type_alignment_bytes(frame_type, frame_align);
2244 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2247 /* calculate offset and new type size */
2248 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2249 frame_size = offset + size;
2252 area = new_entity(frame_type, name, tp);
2253 set_entity_offset(area, offset);
2254 set_type_size_bytes(frame_type, frame_size);
2256 /* mark this entity as compiler generated */
2257 set_entity_compiler_generated(area, 1);
2259 set_type_state(frame_type, layout_fixed);
2263 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2267 type_dbg_info *tdbgi = get_type_dbg_info(type);
2268 if (tdbgi != NULL) {
2269 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2273 /* we have to construct some name... */
2274 switch (get_type_tpop_code(type)) {
2275 case tpo_uninitialized:
2278 snprintf(buffer, buffer_size, "code");
2282 id = get_class_ident(type);
2283 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2287 id = get_struct_ident(type);
2288 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2292 id = get_union_ident(type);
2293 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2296 case tpo_enumeration:
2297 id = get_enumeration_ident(type);
2298 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2302 snprintf(buffer, buffer_size, "unknown type");
2306 p = snprintf(buffer, buffer_size, "pointer to ");
2309 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2313 p = snprintf(buffer, buffer_size, "array of ");
2316 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2320 id = get_mode_ident(get_type_mode(type));
2321 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2325 snprintf(buffer, buffer_size, "none");
2328 /* TODO: we should print argument and return types here... */
2329 snprintf(buffer, buffer_size, "method type");
2332 snprintf(buffer, buffer_size, "invalid type");