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 void ir_init_type(void)
86 /* construct none and unknown type. */
87 firm_none_type = new_type(tpop_none, mode_BAD, NULL);
88 set_type_size_bytes(firm_none_type, 0);
89 set_type_state (firm_none_type, layout_fixed);
90 remove_irp_type(firm_none_type);
92 firm_code_type = new_type(tpop_code, mode_ANY, NULL);
93 set_type_state(firm_code_type, layout_fixed);
94 remove_irp_type(firm_code_type);
96 firm_unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
97 set_type_size_bytes(firm_unknown_type, 0);
98 set_type_state (firm_unknown_type, layout_fixed);
99 remove_irp_type(firm_unknown_type);
102 void ir_finish_type(void)
104 if (firm_none_type != NULL) {
105 free_type(firm_none_type);
106 firm_none_type = NULL;
108 if (firm_code_type != NULL) {
109 free_type(firm_code_type);
110 firm_code_type = NULL;
112 if (firm_unknown_type != NULL) {
113 free_type(firm_unknown_type);
114 firm_unknown_type = NULL;
118 /** the global type visited flag */
119 ir_visited_t firm_type_visited;
121 void (set_master_type_visited)(ir_visited_t val)
123 _set_master_type_visited(val);
126 ir_visited_t (get_master_type_visited)(void)
128 return _get_master_type_visited();
131 void (inc_master_type_visited)(void)
133 _inc_master_type_visited();
136 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
141 node_size = offsetof(ir_type, attr) + type_op->attr_size;
142 res = (ir_type*)xmalloc(node_size);
143 memset(res, 0, node_size);
146 res->type_op = type_op;
148 res->visibility = ir_visibility_external;
149 res->flags = tf_none;
155 res->assoc_type = NULL;
157 res->nr = get_irp_new_node_nr();
158 #endif /* defined DEBUG_libfirm */
160 add_irp_type(res); /* Remember the new type global. */
165 void free_type(ir_type *tp)
167 const tp_op *op = get_type_tpop(tp);
169 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
170 || (get_type_tpop(tp) == tpop_code))
172 /* Remove from list of all types */
174 /* Free the attributes of the type. */
176 /* Free entities automatically allocated with the ir_type */
177 if (op->ops.free_auto_entities)
178 op->ops.free_auto_entities(tp);
179 /* And now the type itself... */
184 void free_type_entities(ir_type *tp)
186 const tp_op *tpop = get_type_tpop(tp);
188 if (tpop->ops.free_entities)
189 tpop->ops.free_entities(tp);
192 void free_type_attrs(ir_type *tp)
194 const tp_op *tpop = get_type_tpop(tp);
196 if (tpop->ops.free_attrs)
197 tpop->ops.free_attrs(tp);
200 void *(get_type_link)(const ir_type *tp)
202 return _get_type_link(tp);
205 void (set_type_link)(ir_type *tp, void *l)
207 _set_type_link(tp, l);
210 const tp_op *(get_type_tpop)(const ir_type *tp)
212 return _get_type_tpop(tp);
215 ident *(get_type_tpop_nameid)(const ir_type *tp)
217 return _get_type_tpop_nameid(tp);
220 const char* get_type_tpop_name(const ir_type *tp)
222 assert(tp && tp->kind == k_type);
223 return get_id_str(tp->type_op->name);
226 tp_opcode (get_type_tpop_code)(const ir_type *tp)
228 return _get_type_tpop_code(tp);
231 ir_mode *(get_type_mode)(const ir_type *tp)
233 return _get_type_mode(tp);
236 void set_type_mode(ir_type *tp, ir_mode *mode)
238 const tp_op *tpop = get_type_tpop(tp);
240 if (tpop->ops.set_type_mode)
241 tpop->ops.set_type_mode(tp, mode);
243 assert(0 && "setting a mode is NOT allowed for this type");
246 /* Outputs a unique number for this node */
247 long get_type_nr(const ir_type *tp)
253 return (long)PTR_TO_INT(tp);
257 unsigned (get_type_size_bytes)(const ir_type *tp)
259 return _get_type_size_bytes(tp);
262 ir_visibility get_type_visibility(const ir_type *tp)
265 return tp->visibility;
268 void set_type_visibility(ir_type *tp, ir_visibility v)
274 void set_type_size_bytes(ir_type *tp, unsigned size)
276 const tp_op *tpop = get_type_tpop(tp);
278 if (tpop->ops.set_type_size)
279 tpop->ops.set_type_size(tp, size);
281 assert(0 && "Cannot set size for this type");
284 unsigned get_type_alignment_bytes(ir_type *tp)
291 /* alignment NOT set calculate it "on demand" */
293 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
294 else if (is_Array_type(tp))
295 align = get_type_alignment_bytes(get_array_element_type(tp));
296 else if (is_compound_type(tp)) {
297 size_t i, n = get_compound_n_members(tp);
300 for (i = 0; i < n; ++i) {
301 ir_type *t = get_entity_type(get_compound_member(tp, i));
302 unsigned a = get_type_alignment_bytes(t);
307 } else if (is_Method_type(tp)) {
317 void set_type_alignment_bytes(ir_type *tp, unsigned align)
319 assert(tp && tp->kind == k_type);
320 /* Methods don't have an alignment. */
321 if (tp->type_op != type_method) {
326 const char *get_type_state_name(ir_type_state s)
328 #define X(a) case a: return #a
337 ir_type_state (get_type_state)(const ir_type *tp)
339 return _get_type_state(tp);
342 void set_type_state(ir_type *tp, ir_type_state state)
344 assert(tp && tp->kind == k_type);
346 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
347 (tp->type_op == type_method))
350 /* Just a correctness check: */
351 if (state == layout_fixed) {
353 switch (get_type_tpop_code(tp)) {
355 if (tp != get_glob_type()) {
356 size_t n_mem = get_class_n_members(tp);
357 for (i = 0; i < n_mem; i++) {
358 ir_entity *entity = get_class_member(tp, i);
359 if (is_Method_type(get_entity_type(entity)))
361 assert(get_entity_offset(entity) > -1);
366 for (i = 0; i < get_struct_n_members(tp); i++) {
367 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
374 case tpo_enumeration: {
376 size_t n_enums = get_enumeration_n_enums(tp);
377 assert(get_type_mode(tp) != NULL);
378 for (i = 0; i < n_enums; ++i) {
379 ir_enum_const *ec = get_enumeration_const(tp, i);
380 ir_tarval *tv = get_enumeration_value(ec);
381 assert(tv != NULL && tv != tarval_bad);
389 if (state == layout_fixed)
390 tp->flags |= tf_layout_fixed;
392 tp->flags &= ~tf_layout_fixed;
395 ir_visited_t (get_type_visited)(const ir_type *tp)
397 return _get_type_visited(tp);
400 void (set_type_visited)(ir_type *tp, ir_visited_t num)
402 _set_type_visited(tp, num);
405 void (mark_type_visited)(ir_type *tp)
407 _mark_type_visited(tp);
410 int (type_visited)(const ir_type *tp)
412 return _type_visited(tp);
415 int (type_not_visited)(const ir_type *tp)
417 return _type_not_visited(tp);
420 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
422 return _get_type_dbg_info(tp);
425 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
427 _set_type_dbg_info(tp, db);
430 int (is_type)(const void *thing)
432 return _is_type(thing);
435 /* Checks whether two types are structural equal.*/
436 int equal_type(ir_type *typ1, ir_type *typ2)
443 if (typ1 == typ2) return 1;
445 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
446 typ1->name != typ2->name ||
447 (get_type_mode(typ1) != get_type_mode(typ2)) ||
448 (get_type_state(typ1) != get_type_state(typ2)))
450 if ((get_type_state(typ1) == layout_fixed) &&
451 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
454 switch (get_type_tpop_code(typ1)) {
456 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
457 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
458 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
459 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
460 /** Compare the members **/
461 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
462 /* First sort the members of typ2 */
463 for (i = 0; i < get_class_n_members(typ1); i++) {
464 ir_entity *e1 = get_class_member(typ1, i);
465 for (j = 0; j < get_class_n_members(typ2); j++) {
466 ir_entity *e2 = get_class_member(typ2, j);
467 if (get_entity_name(e1) == get_entity_name(e2))
471 for (i = 0; i < get_class_n_members(typ1); i++) {
472 if (!m[i] || get_class_member(typ1, i) != m[i])
475 /** Compare the supertypes **/
476 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
477 /* First sort the supertypes of typ2 */
478 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
479 ir_type *t1 = get_class_supertype(typ1, i);
480 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
481 ir_type *t2 = get_class_supertype(typ2, j);
482 if (t2->name == t1->name)
486 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
487 if (!t[i] || /* Found no counterpart */
488 get_class_supertype(typ1, i) != t[i])
494 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
495 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
496 /* First sort the members of lt */
497 for (i = 0; i < get_struct_n_members(typ1); i++) {
498 ir_entity *e1 = get_struct_member(typ1, i);
499 for (j = 0; j < get_struct_n_members(typ2); j++) {
500 ir_entity *e2 = get_struct_member(typ2, j);
501 if (get_entity_name(e1) == get_entity_name(e2))
505 for (i = 0; i < get_struct_n_members(typ1); i++) {
506 if (!m[i] || get_struct_member(typ1, i) != m[i])
515 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
516 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
517 if (get_method_calling_convention(typ1) !=
518 get_method_calling_convention(typ2)) return 0;
520 n_param1 = get_method_n_params(typ1);
521 n_param2 = get_method_n_params(typ2);
523 if (n_param1 != n_param2) return 0;
525 for (i = 0; i < n_param1; i++) {
526 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
529 for (i = 0; i < get_method_n_ress(typ1); i++) {
530 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
536 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
537 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
538 /* First sort the members of lt */
539 for (i = 0; i < get_union_n_members(typ1); i++) {
540 ir_entity *e1 = get_union_member(typ1, i);
541 for (j = 0; j < get_union_n_members(typ2); j++) {
542 ir_entity *e2 = get_union_member(typ2, j);
543 if (get_entity_name(e1) == get_entity_name(e2))
547 for (i = 0; i < get_union_n_members(typ1); i++) {
548 if (!m[i] || get_union_member(typ1, i) != m[i])
554 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
556 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
558 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
559 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
560 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
562 if (get_array_order(typ1, i) != get_array_order(typ2, i))
563 assert(0 && "type compare with different dimension orders not implemented");
567 case tpo_enumeration:
568 assert(0 && "enumerations not implemented");
572 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
584 int smaller_type(ir_type *st, ir_type *lt)
591 if (st == lt) return 1;
593 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
596 switch (get_type_tpop_code(st)) {
598 return is_SubClass_of(st, lt);
601 n_st_members = get_struct_n_members(st);
602 if (n_st_members != get_struct_n_members(lt))
605 m = ALLOCANZ(ir_entity*, n_st_members);
606 /* First sort the members of lt */
607 for (i = 0; i < n_st_members; ++i) {
608 ir_entity *se = get_struct_member(st, i);
609 size_t n = get_struct_n_members(lt);
610 for (j = 0; j < n; ++j) {
611 ir_entity *le = get_struct_member(lt, j);
612 if (get_entity_name(le) == get_entity_name(se))
616 for (i = 0; i < n_st_members; i++) {
617 if (!m[i] || /* Found no counterpart */
618 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
624 size_t n_param1, n_param2;
626 /** FIXME: is this still 1? */
627 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
628 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
629 if (get_method_calling_convention(st) !=
630 get_method_calling_convention(lt)) return 0;
632 n_param1 = get_method_n_params(st);
633 n_param2 = get_method_n_params(lt);
635 if (n_param1 != n_param2) return 0;
637 for (i = 0; i < get_method_n_params(st); i++) {
638 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
641 for (i = 0; i < get_method_n_ress(st); i++) {
642 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
648 n_st_members = get_union_n_members(st);
649 if (n_st_members != get_union_n_members(lt)) return 0;
650 m = ALLOCANZ(ir_entity*, n_st_members);
651 /* First sort the members of lt */
652 for (i = 0; i < n_st_members; ++i) {
653 ir_entity *se = get_union_member(st, i);
654 size_t n = get_union_n_members(lt);
655 for (j = 0; j < n; ++j) {
656 ir_entity *le = get_union_member(lt, j);
657 if (get_entity_name(le) == get_entity_name(se))
661 for (i = 0; i < n_st_members; ++i) {
662 if (!m[i] || /* Found no counterpart */
663 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
669 ir_type *set, *let; /* small/large elt. ir_type */
670 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
672 set = get_array_element_type(st);
673 let = get_array_element_type(lt);
675 /* If the element types are different, set must be convertible
676 to let, and they must have the same size so that address
677 computations work out. To have a size the layout must
679 if ((get_type_state(set) != layout_fixed) ||
680 (get_type_state(let) != layout_fixed))
682 if (!smaller_type(set, let) ||
683 get_type_size_bytes(set) != get_type_size_bytes(let))
686 for (i = 0; i < get_array_n_dimensions(st); i++) {
687 if (get_array_lower_bound(lt, i))
688 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
690 if (get_array_upper_bound(lt, i))
691 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
696 case tpo_enumeration:
697 assert(0 && "enumerations not implemented");
701 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
706 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
716 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
720 res = new_type(type_class, NULL, db);
723 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
724 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
725 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
726 res->attr.ca.peculiarity = peculiarity_existent;
727 res->attr.ca.type_info = NULL;
728 res->attr.ca.vtable_size = 0;
729 res->attr.ca.clss_flags = cf_none;
730 res->attr.ca.dfn = 0;
735 ir_type *new_type_class(ident *name)
737 return new_d_type_class(name, NULL);
740 void free_class_entities(ir_type *clss)
743 assert(clss && (clss->type_op == type_class));
744 /* we must iterate backward here */
745 for (i = get_class_n_members(clss); i > 0;)
746 free_entity(get_class_member(clss, --i));
747 /* do NOT free the type info here. It belongs to another class */
750 void free_class_attrs(ir_type *clss)
752 assert(clss && (clss->type_op == type_class));
753 DEL_ARR_F(clss->attr.ca.members);
754 DEL_ARR_F(clss->attr.ca.subtypes);
755 DEL_ARR_F(clss->attr.ca.supertypes);
758 ident *get_class_ident(const ir_type *clss)
760 assert(clss->type_op == type_class);
764 const char *get_class_name(const ir_type *clss)
766 if (get_class_ident(clss) == NULL)
768 return get_id_str(get_class_ident(clss));
771 static void add_class_member(ir_type *clss, ir_entity *member)
773 assert(clss && (clss->type_op == type_class));
774 assert(clss != get_entity_type(member) && "recursive type");
775 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
778 size_t (get_class_n_members)(const ir_type *clss)
780 return _get_class_n_members(clss);
783 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
786 assert(clss && (clss->type_op == type_class));
787 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
788 if (get_class_member(clss, i) == mem)
791 return INVALID_MEMBER_INDEX;
794 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
796 return _get_class_member(clss, pos);
799 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
802 assert(clss && (clss->type_op == type_class));
803 n_mem = get_class_n_members(clss);
804 for (i = 0; i < n_mem; ++i) {
805 ir_entity *mem = get_class_member(clss, i);
806 if (get_entity_ident(mem) == name)
812 static void remove_class_member(ir_type *clss, ir_entity *member)
815 assert(clss && (clss->type_op == type_class));
816 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
817 if (clss->attr.ca.members[i] == member) {
818 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
819 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
820 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
826 void add_class_subtype(ir_type *clss, ir_type *subtype)
829 assert(clss->type_op == type_class);
830 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
831 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
832 if (get_class_supertype(subtype, i) == clss)
833 /* Class already registered */
836 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
839 size_t get_class_n_subtypes(const ir_type *clss)
841 assert(clss->type_op == type_class);
842 return ARR_LEN (clss->attr.ca.subtypes);
845 ir_type *get_class_subtype(ir_type *clss, size_t pos)
847 assert(clss->type_op == type_class);
848 assert(pos < get_class_n_subtypes(clss));
849 return clss->attr.ca.subtypes[pos];
852 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
854 size_t i, n_subtypes = get_class_n_subtypes(clss);
855 assert(is_Class_type(subclass));
856 for (i = 0; i < n_subtypes; ++i) {
857 if (get_class_subtype(clss, i) == subclass)
863 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
865 assert(clss->type_op == type_class);
866 assert(pos < get_class_n_subtypes(clss));
867 clss->attr.ca.subtypes[pos] = subtype;
870 void remove_class_subtype(ir_type *clss, ir_type *subtype)
873 assert(clss && (clss->type_op == type_class));
874 for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
875 if (clss->attr.ca.subtypes[i] == subtype) {
876 for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
877 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
878 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
884 void add_class_supertype(ir_type *clss, ir_type *supertype)
888 assert(clss && (clss->type_op == type_class));
889 assert(supertype && (supertype -> type_op == type_class));
890 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
891 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
892 if (get_class_subtype(supertype, i) == clss)
893 /* Class already registered */
896 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
899 size_t get_class_n_supertypes(const ir_type *clss)
901 assert(clss->type_op == type_class);
902 return ARR_LEN(clss->attr.ca.supertypes);
905 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
907 size_t i, n_supertypes = get_class_n_supertypes(clss);
908 assert(super_clss && (super_clss->type_op == type_class));
909 for (i = 0; i < n_supertypes; i++) {
910 if (get_class_supertype(clss, i) == super_clss)
916 ir_type *get_class_supertype(ir_type *clss, size_t pos)
918 assert(clss->type_op == type_class);
919 assert(pos < get_class_n_supertypes(clss));
920 return clss->attr.ca.supertypes[pos];
923 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
925 assert(clss->type_op == type_class);
926 assert(pos < get_class_n_supertypes(clss));
927 clss->attr.ca.supertypes[pos] = supertype;
930 void remove_class_supertype(ir_type *clss, ir_type *supertype)
933 assert(clss && (clss->type_op == type_class));
934 for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
935 if (clss->attr.ca.supertypes[i] == supertype) {
936 for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
937 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
938 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
944 ir_entity *get_class_type_info(const ir_type *clss)
946 return clss->attr.ca.type_info;
949 void set_class_type_info(ir_type *clss, ir_entity *ent)
951 clss->attr.ca.type_info = ent;
953 ent->repr_class = clss;
956 ir_peculiarity get_class_peculiarity(const ir_type *clss)
958 assert(clss && (clss->type_op == type_class));
959 return clss->attr.ca.peculiarity;
962 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
964 assert(clss && (clss->type_op == type_class));
965 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
966 clss->attr.ca.peculiarity = pec;
969 unsigned (get_class_vtable_size)(const ir_type *clss)
971 return _get_class_vtable_size(clss);
974 void (set_class_vtable_size)(ir_type *clss, unsigned size)
976 _set_class_vtable_size(clss, size);
979 int (is_class_final)(const ir_type *clss)
981 return _is_class_final(clss);
984 void (set_class_final)(ir_type *clss, int flag)
986 _set_class_final(clss, flag);
989 int (is_class_interface)(const ir_type *clss)
991 return _is_class_interface(clss);
994 void (set_class_interface)(ir_type *clss, int flag)
996 _set_class_interface(clss, flag);
999 int (is_class_abstract)(const ir_type *clss)
1001 return _is_class_abstract(clss);
1004 void (set_class_abstract)(ir_type *clss, int final)
1006 _set_class_abstract(clss, final);
1009 void set_class_dfn(ir_type *clss, int dfn)
1011 clss->attr.ca.dfn = dfn;
1014 int get_class_dfn(const ir_type *clss)
1016 return (clss->attr.ca.dfn);
1019 int (is_Class_type)(const ir_type *clss)
1021 return _is_class_type(clss);
1024 void set_class_mode(ir_type *tp, ir_mode *mode)
1026 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1027 assert(get_type_state(tp) == layout_fixed &&
1028 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1032 void set_class_size(ir_type *tp, unsigned size)
1038 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1040 ir_type *res = new_type(type_struct, NULL, db);
1043 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1048 ir_type *new_type_struct(ident *name)
1050 return new_d_type_struct (name, NULL);
1053 void free_struct_entities(ir_type *strct)
1056 assert(strct && (strct->type_op == type_struct));
1057 /* we must iterate backward here */
1058 for (i = get_struct_n_members(strct); i > 0;)
1059 free_entity(get_struct_member(strct, --i));
1062 void free_struct_attrs(ir_type *strct)
1064 assert(strct && (strct->type_op == type_struct));
1065 DEL_ARR_F(strct->attr.sa.members);
1068 ident *get_struct_ident(const ir_type *strct)
1070 assert(strct->type_op == type_struct);
1074 const char *get_struct_name(const ir_type *strct)
1076 if (get_struct_ident(strct) == NULL)
1078 return get_id_str(get_struct_ident(strct));
1081 size_t get_struct_n_members(const ir_type *strct)
1083 assert(strct->type_op == type_struct);
1084 return ARR_LEN(strct->attr.sa.members);
1087 static void add_struct_member(ir_type *strct, ir_entity *member)
1089 assert(strct && (strct->type_op == type_struct));
1090 assert(get_type_tpop(get_entity_type(member)) != type_method);
1091 assert(strct != get_entity_type(member) && "recursive type");
1092 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1095 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1097 assert(strct && (strct->type_op == type_struct));
1098 assert(pos < get_struct_n_members(strct));
1099 return strct->attr.sa.members[pos];
1102 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1105 assert(strct && (strct->type_op == type_struct));
1106 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1107 if (get_struct_member(strct, i) == mem)
1113 static void remove_struct_member(ir_type *strct, ir_entity *member)
1116 assert(strct && (strct->type_op == type_struct));
1117 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1118 if (strct->attr.sa.members[i] == member) {
1119 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1120 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1121 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1127 int (is_Struct_type)(const ir_type *strct)
1129 return _is_struct_type(strct);
1132 void set_struct_mode(ir_type *tp, ir_mode *mode)
1134 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1135 assert(get_type_state(tp) == layout_fixed &&
1136 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1140 void set_struct_size(ir_type *tp, unsigned size)
1145 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1149 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1150 res = new_type(type_method, mode_P_code, db);
1151 res->flags |= tf_layout_fixed;
1152 res->size = get_mode_size_bytes(mode_P_code);
1153 res->attr.ma.n_params = n_param;
1154 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1155 res->attr.ma.n_res = n_res;
1156 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1157 res->attr.ma.variadicity = variadicity_non_variadic;
1158 res->attr.ma.additional_properties = mtp_no_property;
1163 ir_type *new_type_method(size_t n_param, size_t n_res)
1165 return new_d_type_method(n_param, n_res, NULL);
1168 ir_type *clone_type_method(ir_type *tp)
1176 assert(is_Method_type(tp));
1179 n_params = tp->attr.ma.n_params;
1180 n_res = tp->attr.ma.n_res;
1183 res = new_type(type_method, mode, db);
1185 res->flags = tp->flags;
1186 res->assoc_type = tp->assoc_type;
1187 res->size = tp->size;
1188 res->attr.ma.n_params = n_params;
1189 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1190 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1191 res->attr.ma.n_res = n_res;
1192 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1193 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1194 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1195 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1196 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1201 void free_method_entities(ir_type *method)
1204 assert(method && (method->type_op == type_method));
1207 void free_method_attrs(ir_type *method)
1209 assert(method && (method->type_op == type_method));
1210 free(method->attr.ma.params);
1211 free(method->attr.ma.res_type);
1214 size_t (get_method_n_params)(const ir_type *method)
1216 return _get_method_n_params(method);
1219 ir_type *get_method_param_type(const ir_type *method, size_t pos)
1222 assert(method->type_op == type_method);
1223 assert(pos < get_method_n_params(method));
1224 res = method->attr.ma.params[pos].tp;
1225 assert(res != NULL && "empty method param type");
1229 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1231 assert(method->type_op == type_method);
1232 assert(pos < get_method_n_params(method));
1233 method->attr.ma.params[pos].tp = tp;
1236 size_t (get_method_n_ress)(const ir_type *method)
1238 return _get_method_n_ress(method);
1241 ir_type *get_method_res_type(const ir_type *method, size_t pos)
1244 assert(method->type_op == type_method);
1245 assert(pos < get_method_n_ress(method));
1246 res = method->attr.ma.res_type[pos].tp;
1247 assert(res != NULL && "empty method return type");
1251 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1253 assert(method->type_op == type_method);
1254 assert(pos < get_method_n_ress(method));
1255 /* set the result ir_type */
1256 method->attr.ma.res_type[pos].tp = tp;
1257 /* If information constructed set pass-by-value representation. */
1260 const char *get_variadicity_name(ir_variadicity vari)
1262 #define X(a) case a: return #a
1264 X(variadicity_non_variadic);
1265 X(variadicity_variadic);
1272 ir_variadicity get_method_variadicity(const ir_type *method)
1274 assert(method && (method->type_op == type_method));
1275 return method->attr.ma.variadicity;
1278 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1280 assert(method && (method->type_op == type_method));
1281 method->attr.ma.variadicity = vari;
1284 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1286 return _get_method_additional_properties(method);
1289 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1291 _set_method_additional_properties(method, mask);
1294 void (add_method_additional_properties)(ir_type *method,
1295 mtp_additional_properties flag)
1297 _add_method_additional_properties(method, flag);
1300 unsigned (get_method_calling_convention)(const ir_type *method)
1302 return _get_method_calling_convention(method);
1305 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1307 _set_method_calling_convention(method, cc_mask);
1310 unsigned get_method_n_regparams(ir_type *method)
1312 unsigned cc = get_method_calling_convention(method);
1313 assert(IS_FASTCALL(cc));
1315 return cc & ~cc_bits;
1318 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1320 unsigned cc = get_method_calling_convention(method);
1321 assert(IS_FASTCALL(cc));
1323 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1326 int (is_Method_type)(const ir_type *method)
1328 return _is_method_type(method);
1332 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1334 ir_type *res = new_type(type_union, NULL, db);
1337 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1342 ir_type *new_type_union(ident *name)
1344 return new_d_type_union(name, NULL);
1347 void free_union_entities(ir_type *uni)
1350 assert(uni && (uni->type_op == type_union));
1351 /* we must iterate backward here */
1352 for (i = get_union_n_members(uni); i > 0;)
1353 free_entity(get_union_member(uni, --i));
1356 void free_union_attrs(ir_type *uni)
1358 assert(uni && (uni->type_op == type_union));
1359 DEL_ARR_F(uni->attr.ua.members);
1362 ident *get_union_ident(const ir_type *uni)
1364 assert(uni->type_op == type_union);
1368 const char *get_union_name(const ir_type *uni)
1370 if (get_union_ident(uni) == NULL)
1372 return get_id_str(get_union_ident(uni));
1375 size_t get_union_n_members(const ir_type *uni)
1377 assert(uni->type_op == type_union);
1378 return ARR_LEN(uni->attr.ua.members);
1381 static void add_union_member(ir_type *uni, ir_entity *member)
1383 assert(uni->type_op == type_union);
1384 assert(uni != get_entity_type(member) && "recursive type");
1385 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1388 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1390 assert(uni->type_op == type_union);
1391 assert(pos < get_union_n_members(uni));
1392 return uni->attr.ua.members[pos];
1395 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1398 assert(uni && (uni->type_op == type_union));
1399 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1400 if (get_union_member(uni, i) == mem)
1406 static void remove_union_member(ir_type *uni, ir_entity *member)
1409 assert(uni && (uni->type_op == type_union));
1410 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1411 if (uni->attr.ua.members[i] == member) {
1412 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1413 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1414 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1420 int (is_Union_type)(const ir_type *uni)
1422 return _is_union_type(uni);
1425 void set_union_size(ir_type *tp, unsigned size)
1432 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1438 ir_graph *irg = get_const_code_irg();
1440 assert(!is_Method_type(element_type));
1442 res = new_type(type_array, NULL, db);
1443 res->attr.aa.n_dimensions = n_dimensions;
1444 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1445 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1446 res->attr.aa.order = XMALLOCNZ(size_t, n_dimensions);
1448 unk = new_r_Unknown(irg, mode_Iu);
1449 for (i = 0; i < n_dimensions; i++) {
1450 res->attr.aa.lower_bound[i] =
1451 res->attr.aa.upper_bound[i] = unk;
1452 res->attr.aa.order[i] = i;
1455 res->attr.aa.element_type = element_type;
1456 res->attr.aa.element_ent
1457 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1458 res->attr.aa.element_ent->owner = res;
1464 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1466 return new_d_type_array(n_dimensions, element_type, NULL);
1469 void free_array_automatic_entities(ir_type *array)
1471 assert(array && (array->type_op == type_array));
1472 free_entity(get_array_element_entity(array));
1475 void free_array_entities(ir_type *array)
1478 assert(array->type_op == type_array);
1481 void free_array_attrs(ir_type *array)
1483 assert(array->type_op == type_array);
1484 free(array->attr.aa.lower_bound);
1485 free(array->attr.aa.upper_bound);
1486 free(array->attr.aa.order);
1489 /* manipulate private fields of array ir_type */
1490 size_t get_array_n_dimensions(const ir_type *array)
1492 assert(array->type_op == type_array);
1493 return array->attr.aa.n_dimensions;
1496 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1497 ir_node *upper_bound)
1499 assert(array && (array->type_op == type_array));
1500 assert(lower_bound && "lower_bound node may not be NULL.");
1501 assert(upper_bound && "upper_bound node may not be NULL.");
1502 assert(dimension < array->attr.aa.n_dimensions);
1503 array->attr.aa.lower_bound[dimension] = lower_bound;
1504 array->attr.aa.upper_bound[dimension] = upper_bound;
1507 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1510 ir_graph *irg = get_const_code_irg();
1511 set_array_bounds(array, dimension,
1512 new_r_Const_long(irg, mode_Iu, lower_bound),
1513 new_r_Const_long(irg, mode_Iu, upper_bound));
1516 void set_array_lower_bound(ir_type *array, size_t dimension,
1517 ir_node *lower_bound)
1519 assert(array && (array->type_op == type_array));
1520 assert(lower_bound && "lower_bound node may not be NULL.");
1521 array->attr.aa.lower_bound[dimension] = lower_bound;
1524 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1526 ir_graph *irg = get_const_code_irg();
1527 set_array_lower_bound(array, dimension,
1528 new_r_Const_long(irg, mode_Iu, lower_bound));
1531 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1533 assert(array && (array->type_op == type_array));
1534 assert(upper_bound && "upper_bound node may not be NULL.");
1535 array->attr.aa.upper_bound[dimension] = upper_bound;
1538 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1540 ir_graph *irg = get_const_code_irg();
1541 set_array_upper_bound(array, dimension,
1542 new_r_Const_long(irg, mode_Iu, upper_bound));
1545 int has_array_lower_bound(const ir_type *array, size_t dimension)
1547 assert(array && (array->type_op == type_array));
1548 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1551 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1553 assert(array && (array->type_op == type_array));
1554 return array->attr.aa.lower_bound[dimension];
1557 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1560 assert(array && (array->type_op == type_array));
1561 node = array->attr.aa.lower_bound[dimension];
1562 assert(is_Const(node));
1563 return get_tarval_long(get_Const_tarval(node));
1566 int has_array_upper_bound(const ir_type *array, size_t dimension)
1568 assert(array && (array->type_op == type_array));
1569 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1572 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1574 assert(array && (array->type_op == type_array));
1575 return array->attr.aa.upper_bound[dimension];
1578 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1581 assert(array && (array->type_op == type_array));
1582 node = array->attr.aa.upper_bound[dimension];
1583 assert(is_Const(node));
1584 return get_tarval_long(get_Const_tarval(node));
1587 void set_array_order(ir_type *array, size_t dimension, size_t order)
1589 assert(array && (array->type_op == type_array));
1590 array->attr.aa.order[dimension] = order;
1593 size_t get_array_order(const ir_type *array, size_t dimension)
1595 assert(array && (array->type_op == type_array));
1596 return array->attr.aa.order[dimension];
1599 size_t find_array_dimension(const ir_type *array, size_t order)
1603 assert(array->type_op == type_array);
1605 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1606 if (array->attr.aa.order[dim] == order)
1612 void set_array_element_type(ir_type *array, ir_type *tp)
1614 assert(array && (array->type_op == type_array));
1615 assert(!is_Method_type(tp));
1616 array->attr.aa.element_type = tp;
1619 ir_type *get_array_element_type(const ir_type *array)
1621 assert(array && (array->type_op == type_array));
1622 return array->attr.aa.element_type;
1625 void set_array_element_entity(ir_type *array, ir_entity *ent)
1627 assert(array && (array->type_op == type_array));
1628 assert((get_entity_type(ent)->type_op != type_method));
1629 array->attr.aa.element_ent = ent;
1630 array->attr.aa.element_type = get_entity_type(ent);
1633 ir_entity *get_array_element_entity(const ir_type *array)
1635 assert(array && (array->type_op == type_array));
1636 return array->attr.aa.element_ent;
1639 int (is_Array_type)(const ir_type *array)
1641 return _is_array_type(array);
1644 void set_array_size(ir_type *tp, unsigned size)
1646 /* FIXME: Here we should make some checks with the element type size */
1651 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1655 res = new_type(type_enumeration, NULL, db);
1657 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1662 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1664 return new_d_type_enumeration(name, n_enums, NULL);
1667 void free_enumeration_entities(ir_type *enumeration)
1670 assert(enumeration->type_op == type_enumeration);
1673 void free_enumeration_attrs(ir_type *enumeration)
1675 assert(enumeration->type_op == type_enumeration);
1676 DEL_ARR_F(enumeration->attr.ea.enumer);
1679 ident *get_enumeration_ident(const ir_type *enumeration)
1681 assert(enumeration->type_op == type_enumeration);
1682 return enumeration->name;
1685 const char *get_enumeration_name(const ir_type *enumeration)
1687 if (get_enumeration_ident(enumeration) == NULL)
1689 return get_id_str(get_enumeration_ident(enumeration));
1692 size_t get_enumeration_n_enums(const ir_type *enumeration)
1694 assert(enumeration->type_op == type_enumeration);
1695 return ARR_LEN(enumeration->attr.ea.enumer);
1698 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1701 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1702 enumeration->attr.ea.enumer[pos].nameid = nameid;
1703 enumeration->attr.ea.enumer[pos].value = con;
1704 enumeration->attr.ea.enumer[pos].owner = enumeration;
1707 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1709 assert(enumeration->type_op == type_enumeration);
1710 assert(pos < get_enumeration_n_enums(enumeration));
1711 return &enumeration->attr.ea.enumer[pos];
1714 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1716 return enum_cnst->owner;
1719 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1721 enum_cnst->value = con;
1724 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1726 return enum_cnst->value;
1729 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1731 enum_cnst->nameid = id;
1734 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1736 return enum_cnst->nameid;
1739 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1741 return get_id_str(enum_cnst->nameid);
1744 int (is_Enumeration_type)(const ir_type *enumeration)
1746 return _is_enumeration_type(enumeration);
1749 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1751 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1752 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1753 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1755 tp->size = get_mode_size_bytes(mode);
1761 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1766 if (is_Method_type(points_to) || is_code_type(points_to)) {
1772 res = new_type(type_pointer, mode, db);
1773 res->attr.pa.points_to = points_to;
1774 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1775 res->size = get_mode_size_bytes(res->mode);
1776 res->flags |= tf_layout_fixed;
1781 ir_type *new_type_pointer(ir_type *points_to)
1783 return new_d_type_pointer(points_to, NULL);
1786 void free_pointer_entities(ir_type *pointer)
1789 assert(pointer && (pointer->type_op == type_pointer));
1792 void free_pointer_attrs(ir_type *pointer)
1795 assert(pointer && (pointer->type_op == type_pointer));
1798 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1800 assert(pointer && (pointer->type_op == type_pointer));
1801 pointer->attr.pa.points_to = tp;
1804 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1806 assert(pointer && (pointer->type_op == type_pointer));
1807 return pointer->attr.pa.points_to;
1810 int (is_Pointer_type)(const ir_type *pointer)
1812 return _is_pointer_type(pointer);
1815 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1817 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1818 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1819 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1821 tp->size = get_mode_size_bytes(mode);
1825 ir_type *find_pointer_type_to_type(ir_type *tp)
1827 size_t i, n = get_irp_n_types();
1828 for (i = 0; i < n; ++i) {
1829 ir_type *found = get_irp_type(i);
1830 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1833 return firm_unknown_type;
1838 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1840 ir_type *res = new_type(type_primitive, mode, db);
1841 res->size = get_mode_size_bytes(mode);
1842 res->flags |= tf_layout_fixed;
1843 res->attr.ba.base_type = NULL;
1848 ir_type *new_type_primitive(ir_mode *mode)
1850 return new_d_type_primitive(mode, NULL);
1853 int (is_Primitive_type)(const ir_type *primitive)
1855 return _is_primitive_type(primitive);
1858 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1860 /* Modes of primitives must be data */
1861 assert(mode_is_data(mode));
1863 /* For primitive size depends on the mode. */
1864 tp->size = get_mode_size_bytes(mode);
1868 ir_type *get_primitive_base_type(const ir_type *tp)
1870 assert(is_Primitive_type(tp));
1871 return tp->attr.ba.base_type;
1874 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
1876 assert(is_Primitive_type(tp));
1877 tp->attr.ba.base_type = base_tp;
1882 int (is_atomic_type)(const ir_type *tp)
1884 return _is_atomic_type(tp);
1887 size_t get_compound_n_members(const ir_type *tp)
1889 const tp_op *op = get_type_tpop(tp);
1890 return op->ops.get_n_members(tp);
1893 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
1895 const tp_op *op = get_type_tpop(tp);
1896 return op->ops.get_member(tp, pos);
1899 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
1901 const tp_op *op = get_type_tpop(tp);
1902 return op->ops.get_member_index(tp, member);
1905 int is_compound_type(const ir_type *tp)
1907 assert(tp->kind == k_type);
1908 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1911 ident *get_compound_ident(const ir_type *tp)
1913 assert(is_compound_type(tp));
1917 const char *get_compound_name(const ir_type *tp)
1919 if (get_compound_ident(tp) == NULL)
1921 return get_id_str(get_compound_ident(tp));
1924 void remove_compound_member(ir_type *compound, ir_entity *entity)
1926 switch (get_type_tpop_code(compound)) {
1927 case tpo_class: remove_class_member(compound, entity); break;
1928 case tpo_struct: remove_struct_member(compound, entity); break;
1929 case tpo_union: remove_union_member(compound, entity); break;
1931 panic("argument for remove_compound_member not a compound type");
1935 void add_compound_member(ir_type *compound, ir_entity *entity)
1937 switch (get_type_tpop_code(compound)) {
1938 case tpo_class: add_class_member(compound, entity); break;
1939 case tpo_struct: add_struct_member(compound, entity); break;
1940 case tpo_union: add_union_member(compound, entity); break;
1942 panic("argument for add_compound_member not a compound type");
1948 int is_code_type(const ir_type *tp)
1950 assert(tp && tp->kind == k_type);
1951 return tp->type_op == tpop_code;
1954 int is_frame_type(const ir_type *tp)
1956 return tp->flags & tf_frame_type;
1959 ir_type *new_type_frame(void)
1961 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
1963 res->flags |= tf_frame_type;
1965 /* Remove type from type list. Must be treated differently than other types. */
1966 remove_irp_type(res);
1968 /* It is not possible to derive from the frame type. Set the final flag. */
1969 set_class_final(res, 1);
1974 ir_type *clone_frame_type(ir_type *type)
1979 assert(is_frame_type(type));
1980 /* the entity link resource should be allocated if this function is called */
1981 assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
1983 res = new_type_frame();
1984 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
1985 ir_entity *ent = get_class_member(type, i);
1986 ir_entity *nent = copy_entity_own(ent, res);
1987 set_entity_link(ent, nent);
1988 set_entity_link(nent, ent);
1993 void set_default_size(ir_type *tp, unsigned size)
1998 void default_layout_compound_type(ir_type *type)
2001 size_t n = get_compound_n_members(type);
2003 unsigned align_all = 1;
2005 for (i = 0; i < n; ++i) {
2006 ir_entity *entity = get_compound_member(type, i);
2007 ir_type *entity_type = get_entity_type(entity);
2011 if (is_Method_type(entity_type))
2014 assert(get_type_state(entity_type) == layout_fixed);
2015 align = get_type_alignment_bytes(entity_type);
2016 align_all = align > align_all ? align : align_all;
2017 misalign = (align ? size % align : 0);
2018 size += (misalign ? align - misalign : 0);
2020 set_entity_offset(entity, size);
2021 if (!is_Union_type(type)) {
2022 size += get_type_size_bytes(entity_type);
2025 if (align_all > 0 && size % align_all) {
2026 size += align_all - (size % align_all);
2028 if (align_all > get_type_alignment_bytes(type)) {
2029 set_type_alignment_bytes(type, align_all);
2031 set_type_size_bytes(type, size);
2032 set_type_state(type, layout_fixed);
2035 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2042 unsigned frame_align;
2043 int offset, frame_size;
2044 static unsigned area_cnt = 0;
2045 static ir_type *a_byte = NULL;
2047 assert(is_frame_type(frame_type));
2048 assert(get_type_state(frame_type) == layout_fixed);
2049 assert(get_type_alignment_bytes(frame_type) > 0);
2050 set_type_state(frame_type, layout_undefined);
2053 a_byte = new_type_primitive(mode_Bu);
2055 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2056 name = new_id_from_str(buf);
2058 /* align the size */
2059 frame_align = get_type_alignment_bytes(frame_type);
2060 size = (size + frame_align - 1) & ~(frame_align - 1);
2062 tp = new_type_array(1, a_byte);
2063 set_array_bounds_int(tp, 0, 0, size);
2064 set_type_alignment_bytes(tp, alignment);
2066 frame_size = get_type_size_bytes(frame_type);
2069 /* fix all offsets so far */
2070 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2071 ir_entity *ent = get_class_member(frame_type, i);
2073 set_entity_offset(ent, get_entity_offset(ent) + size);
2075 /* calculate offset and new type size */
2079 /* increase size to match alignment... */
2080 if (alignment > frame_align) {
2081 frame_align = alignment;
2082 set_type_alignment_bytes(frame_type, frame_align);
2083 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2086 /* calculate offset and new type size */
2087 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2088 frame_size = offset + size;
2091 area = new_entity(frame_type, name, tp);
2092 set_entity_offset(area, offset);
2093 set_type_size_bytes(frame_type, frame_size);
2095 /* mark this entity as compiler generated */
2096 set_entity_compiler_generated(area, 1);
2098 set_type_state(frame_type, layout_fixed);
2102 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2106 type_dbg_info *tdbgi = get_type_dbg_info(type);
2107 if (tdbgi != NULL) {
2108 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2112 /* we have to construct some name... */
2113 switch (get_type_tpop_code(type)) {
2114 case tpo_uninitialized:
2117 snprintf(buffer, buffer_size, "code");
2121 id = get_class_ident(type);
2122 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2126 id = get_struct_ident(type);
2127 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2131 id = get_union_ident(type);
2132 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2135 case tpo_enumeration:
2136 id = get_enumeration_ident(type);
2137 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2141 snprintf(buffer, buffer_size, "unknown type");
2145 p = snprintf(buffer, buffer_size, "pointer to ");
2148 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2152 p = snprintf(buffer, buffer_size, "array of ");
2155 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2159 id = get_mode_ident(get_type_mode(type));
2160 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2164 snprintf(buffer, buffer_size, "none");
2167 /* TODO: we should print argument and return types here... */
2168 snprintf(buffer, buffer_size, "method type");
2171 snprintf(buffer, buffer_size, "invalid type");