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
26 * Implementation of the datastructure to hold
29 * This module supplies a datastructure to represent all types
30 * known in the compiled program. This includes types specified
31 * in the program as well as types defined by the language. In the
32 * view of the intermediate representation there is no difference
33 * between these types.
35 * There exist several kinds of types, arranged by the structure of
36 * the type. A type is described by a set of attributes. Some of
37 * these attributes are common to all types, others depend on the
40 * Types are different from the modes defined in irmode: Types are
41 * on the level of the programming language, modes at the level of
42 * the target processor.
67 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db);
69 ir_type *get_none_type(void)
71 return irp->none_type;
74 ir_type *get_code_type(void)
76 return irp->code_type;
79 ir_type *get_unknown_type(void)
81 return irp->unknown_type;
84 void ir_init_type(ir_prog *irp)
86 /* construct none and unknown type. */
87 irp->none_type = new_type(tpop_none, mode_BAD, NULL);
88 set_type_size_bytes(irp->none_type, 0);
89 set_type_state (irp->none_type, layout_fixed);
91 irp->code_type = new_type(tpop_code, mode_ANY, NULL);
92 set_type_state(irp->code_type, layout_fixed);
94 irp->unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
95 set_type_size_bytes(irp->unknown_type, 0);
96 set_type_state (irp->unknown_type, layout_fixed);
99 void ir_finish_type(ir_prog *irp)
101 /** nothing todo. (The none, code, unknown types are in the global type list
106 ir_visited_t firm_type_visited;
108 void (set_master_type_visited)(ir_visited_t val)
110 _set_master_type_visited(val);
113 ir_visited_t (get_master_type_visited)(void)
115 return _get_master_type_visited();
118 void (inc_master_type_visited)(void)
120 _inc_master_type_visited();
124 * Creates a new type representation:
126 * @param type_op the kind of this type. May not be type_id.
127 * @param mode the mode to be used for this type, may be NULL
128 * @param db debug info
130 * @return A new type of the given type. The remaining private attributes are
131 * not initialized. The type is in state layout_undefined.
133 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db)
138 node_size = offsetof(ir_type, attr) + type_op->attr_size;
139 res = (ir_type*)xmalloc(node_size);
140 memset(res, 0, node_size);
143 res->type_op = type_op;
145 res->visibility = ir_visibility_external;
146 res->flags = tf_none;
153 res->nr = get_irp_new_node_nr();
154 #endif /* defined DEBUG_libfirm */
156 add_irp_type(res); /* Remember the new type global. */
161 void free_type_entities(ir_type *tp)
163 const tp_op *op = get_type_tpop(tp);
164 if (op->ops.free_entities != NULL)
165 op->ops.free_entities(tp);
168 static void free_type_attrs(ir_type *tp)
170 const tp_op *tpop = get_type_tpop(tp);
172 if (tpop->ops.free_attrs)
173 tpop->ops.free_attrs(tp);
176 void free_type(ir_type *tp)
178 const tp_op *op = get_type_tpop(tp);
180 free_type_entities(tp);
181 /* Remove from list of all types */
183 /* Free the attributes of the type. */
185 /* Free entities automatically allocated with the ir_type */
186 if (op->ops.free_auto_entities)
187 op->ops.free_auto_entities(tp);
188 /* And now the type itself... */
195 void *(get_type_link)(const ir_type *tp)
197 return _get_type_link(tp);
200 void (set_type_link)(ir_type *tp, void *l)
202 _set_type_link(tp, l);
205 const tp_op *(get_type_tpop)(const ir_type *tp)
207 return _get_type_tpop(tp);
210 ident *(get_type_tpop_nameid)(const ir_type *tp)
212 return _get_type_tpop_nameid(tp);
215 const char* get_type_tpop_name(const ir_type *tp)
217 assert(tp && tp->kind == k_type);
218 return get_id_str(tp->type_op->name);
221 tp_opcode (get_type_tpop_code)(const ir_type *tp)
223 return _get_type_tpop_code(tp);
226 ir_mode *(get_type_mode)(const ir_type *tp)
228 return _get_type_mode(tp);
231 void set_type_mode(ir_type *tp, ir_mode *mode)
233 const tp_op *tpop = get_type_tpop(tp);
235 if (tpop->ops.set_type_mode)
236 tpop->ops.set_type_mode(tp, mode);
238 assert(0 && "setting a mode is NOT allowed for this type");
241 long get_type_nr(const ir_type *tp)
247 return (long)PTR_TO_INT(tp);
251 unsigned (get_type_size_bytes)(const ir_type *tp)
253 return _get_type_size_bytes(tp);
256 ir_visibility get_type_visibility(const ir_type *tp)
259 return tp->visibility;
262 void set_type_visibility(ir_type *tp, ir_visibility v)
268 void set_type_size_bytes(ir_type *tp, unsigned size)
270 const tp_op *tpop = get_type_tpop(tp);
271 tpop->ops.set_type_size(tp, size);
274 unsigned get_type_alignment_bytes(ir_type *tp)
281 /* alignment NOT set calculate it "on demand" */
283 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
284 else if (is_Array_type(tp))
285 align = get_type_alignment_bytes(get_array_element_type(tp));
286 else if (is_compound_type(tp)) {
287 size_t i, n = get_compound_n_members(tp);
290 for (i = 0; i < n; ++i) {
291 ir_type *t = get_entity_type(get_compound_member(tp, i));
292 unsigned a = get_type_alignment_bytes(t);
297 } else if (is_Method_type(tp)) {
307 void set_type_alignment_bytes(ir_type *tp, unsigned align)
309 assert(tp && tp->kind == k_type);
310 /* Methods don't have an alignment. */
311 if (tp->type_op != type_method) {
316 const char *get_type_state_name(ir_type_state s)
318 #define X(a) case a: return #a
327 ir_type_state (get_type_state)(const ir_type *tp)
329 return _get_type_state(tp);
332 void set_type_state(ir_type *tp, ir_type_state state)
334 assert(tp && tp->kind == k_type);
336 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
337 (tp->type_op == type_method))
340 /* Just a correctness check: */
341 if (state == layout_fixed) {
343 switch (get_type_tpop_code(tp)) {
345 if (tp != get_glob_type()) {
346 size_t n_mem = get_class_n_members(tp);
347 for (i = 0; i < n_mem; i++) {
348 ir_entity *entity = get_class_member(tp, i);
349 if (is_Method_type(get_entity_type(entity)))
351 assert(get_entity_offset(entity) > -1);
356 for (i = 0; i < get_struct_n_members(tp); i++) {
357 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
364 case tpo_enumeration: {
366 size_t n_enums = get_enumeration_n_enums(tp);
367 assert(get_type_mode(tp) != NULL);
368 for (i = 0; i < n_enums; ++i) {
369 ir_enum_const *ec = get_enumeration_const(tp, i);
370 ir_tarval *tv = get_enumeration_value(ec);
371 assert(tv != NULL && tv != tarval_bad);
379 if (state == layout_fixed)
380 tp->flags |= tf_layout_fixed;
382 tp->flags &= ~tf_layout_fixed;
385 ir_visited_t (get_type_visited)(const ir_type *tp)
387 return _get_type_visited(tp);
390 void (set_type_visited)(ir_type *tp, ir_visited_t num)
392 _set_type_visited(tp, num);
395 void (mark_type_visited)(ir_type *tp)
397 _mark_type_visited(tp);
400 int (type_visited)(const ir_type *tp)
402 return _type_visited(tp);
405 int (type_not_visited)(const ir_type *tp)
407 return _type_not_visited(tp);
410 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
412 return _get_type_dbg_info(tp);
415 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
417 _set_type_dbg_info(tp, db);
420 int (is_type)(const void *thing)
422 return _is_type(thing);
425 int equal_type(ir_type *typ1, ir_type *typ2)
432 if (typ1 == typ2) return 1;
434 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
435 typ1->name != typ2->name ||
436 (get_type_mode(typ1) != get_type_mode(typ2)) ||
437 (get_type_state(typ1) != get_type_state(typ2)))
439 if ((get_type_state(typ1) == layout_fixed) &&
440 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
443 switch (get_type_tpop_code(typ1)) {
445 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
446 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
447 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
448 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
449 /** Compare the members **/
450 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
451 /* First sort the members of typ2 */
452 for (i = 0; i < get_class_n_members(typ1); i++) {
453 ir_entity *e1 = get_class_member(typ1, i);
454 for (j = 0; j < get_class_n_members(typ2); j++) {
455 ir_entity *e2 = get_class_member(typ2, j);
456 if (get_entity_name(e1) == get_entity_name(e2))
460 for (i = 0; i < get_class_n_members(typ1); i++) {
461 if (!m[i] || get_class_member(typ1, i) != m[i])
464 /** Compare the supertypes **/
465 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
466 /* First sort the supertypes of typ2 */
467 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
468 ir_type *t1 = get_class_supertype(typ1, i);
469 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
470 ir_type *t2 = get_class_supertype(typ2, j);
471 if (t2->name == t1->name)
475 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
476 if (!t[i] || /* Found no counterpart */
477 get_class_supertype(typ1, i) != t[i])
483 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
484 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
485 /* First sort the members of lt */
486 for (i = 0; i < get_struct_n_members(typ1); i++) {
487 ir_entity *e1 = get_struct_member(typ1, i);
488 for (j = 0; j < get_struct_n_members(typ2); j++) {
489 ir_entity *e2 = get_struct_member(typ2, j);
490 if (get_entity_name(e1) == get_entity_name(e2))
494 for (i = 0; i < get_struct_n_members(typ1); i++) {
495 if (!m[i] || get_struct_member(typ1, i) != m[i])
504 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
505 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
506 if (get_method_calling_convention(typ1) !=
507 get_method_calling_convention(typ2)) return 0;
509 n_param1 = get_method_n_params(typ1);
510 n_param2 = get_method_n_params(typ2);
512 if (n_param1 != n_param2) return 0;
514 for (i = 0; i < n_param1; i++) {
515 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
518 for (i = 0; i < get_method_n_ress(typ1); i++) {
519 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
525 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
526 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
527 /* First sort the members of lt */
528 for (i = 0; i < get_union_n_members(typ1); i++) {
529 ir_entity *e1 = get_union_member(typ1, i);
530 for (j = 0; j < get_union_n_members(typ2); j++) {
531 ir_entity *e2 = get_union_member(typ2, j);
532 if (get_entity_name(e1) == get_entity_name(e2))
536 for (i = 0; i < get_union_n_members(typ1); i++) {
537 if (!m[i] || get_union_member(typ1, i) != m[i])
543 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
545 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
547 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
548 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
549 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
551 if (get_array_order(typ1, i) != get_array_order(typ2, i))
552 assert(0 && "type compare with different dimension orders not implemented");
556 case tpo_enumeration:
557 assert(0 && "enumerations not implemented");
561 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
573 int smaller_type(ir_type *st, ir_type *lt)
580 if (st == lt) return 1;
582 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
585 switch (get_type_tpop_code(st)) {
587 return is_SubClass_of(st, lt);
590 n_st_members = get_struct_n_members(st);
591 if (n_st_members != get_struct_n_members(lt))
594 m = ALLOCANZ(ir_entity*, n_st_members);
595 /* First sort the members of lt */
596 for (i = 0; i < n_st_members; ++i) {
597 ir_entity *se = get_struct_member(st, i);
598 size_t n = get_struct_n_members(lt);
599 for (j = 0; j < n; ++j) {
600 ir_entity *le = get_struct_member(lt, j);
601 if (get_entity_name(le) == get_entity_name(se))
605 for (i = 0; i < n_st_members; i++) {
606 if (!m[i] || /* Found no counterpart */
607 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
613 size_t n_param1, n_param2;
615 /** FIXME: is this still 1? */
616 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
617 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
618 if (get_method_calling_convention(st) !=
619 get_method_calling_convention(lt)) return 0;
621 n_param1 = get_method_n_params(st);
622 n_param2 = get_method_n_params(lt);
624 if (n_param1 != n_param2) return 0;
626 for (i = 0; i < get_method_n_params(st); i++) {
627 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
630 for (i = 0; i < get_method_n_ress(st); i++) {
631 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
637 n_st_members = get_union_n_members(st);
638 if (n_st_members != get_union_n_members(lt)) return 0;
639 m = ALLOCANZ(ir_entity*, n_st_members);
640 /* First sort the members of lt */
641 for (i = 0; i < n_st_members; ++i) {
642 ir_entity *se = get_union_member(st, i);
643 size_t n = get_union_n_members(lt);
644 for (j = 0; j < n; ++j) {
645 ir_entity *le = get_union_member(lt, j);
646 if (get_entity_name(le) == get_entity_name(se))
650 for (i = 0; i < n_st_members; ++i) {
651 if (!m[i] || /* Found no counterpart */
652 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
658 ir_type *set, *let; /* small/large elt. ir_type */
659 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
661 set = get_array_element_type(st);
662 let = get_array_element_type(lt);
664 /* If the element types are different, set must be convertible
665 to let, and they must have the same size so that address
666 computations work out. To have a size the layout must
668 if ((get_type_state(set) != layout_fixed) ||
669 (get_type_state(let) != layout_fixed))
671 if (!smaller_type(set, let) ||
672 get_type_size_bytes(set) != get_type_size_bytes(let))
675 for (i = 0; i < get_array_n_dimensions(st); i++) {
676 if (get_array_lower_bound(lt, i))
677 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
679 if (get_array_upper_bound(lt, i))
680 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
685 case tpo_enumeration:
686 assert(0 && "enumerations not implemented");
690 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
695 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
705 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
709 res = new_type(type_class, NULL, db);
712 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
713 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
714 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
715 res->attr.ca.peculiarity = peculiarity_existent;
716 res->attr.ca.type_info = NULL;
717 res->attr.ca.vtable_size = 0;
718 res->attr.ca.clss_flags = cf_none;
719 res->attr.ca.dfn = 0;
724 ir_type *new_type_class(ident *name)
726 return new_d_type_class(name, NULL);
729 void free_class_entities(ir_type *clss)
732 assert(clss && (clss->type_op == type_class));
733 /* we must iterate backward here */
734 for (i = get_class_n_members(clss); i > 0;)
735 free_entity(get_class_member(clss, --i));
736 /* do NOT free the type info here. It belongs to another class */
739 void free_class_attrs(ir_type *clss)
741 assert(clss && (clss->type_op == type_class));
742 DEL_ARR_F(clss->attr.ca.members);
743 DEL_ARR_F(clss->attr.ca.subtypes);
744 DEL_ARR_F(clss->attr.ca.supertypes);
747 ident *get_class_ident(const ir_type *clss)
749 assert(clss->type_op == type_class);
753 const char *get_class_name(const ir_type *clss)
755 if (get_class_ident(clss) == NULL)
757 return get_id_str(get_class_ident(clss));
760 static void add_class_member(ir_type *clss, ir_entity *member)
762 assert(clss && (clss->type_op == type_class));
763 assert(clss != get_entity_type(member) && "recursive type");
764 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
767 size_t (get_class_n_members)(const ir_type *clss)
769 return _get_class_n_members(clss);
772 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
775 assert(clss && (clss->type_op == type_class));
776 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
777 if (get_class_member(clss, i) == mem)
780 return INVALID_MEMBER_INDEX;
783 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
785 return _get_class_member(clss, pos);
788 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
791 assert(clss && (clss->type_op == type_class));
792 n_mem = get_class_n_members(clss);
793 for (i = 0; i < n_mem; ++i) {
794 ir_entity *mem = get_class_member(clss, i);
795 if (get_entity_ident(mem) == name)
801 static void remove_class_member(ir_type *clss, ir_entity *member)
804 assert(clss && (clss->type_op == type_class));
805 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
806 if (clss->attr.ca.members[i] == member) {
807 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
808 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
809 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
815 void add_class_subtype(ir_type *clss, ir_type *subtype)
818 assert(clss->type_op == type_class);
819 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
820 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
821 if (get_class_supertype(subtype, i) == clss)
822 /* Class already registered */
825 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
828 size_t get_class_n_subtypes(const ir_type *clss)
830 assert(clss->type_op == type_class);
831 return ARR_LEN (clss->attr.ca.subtypes);
834 ir_type *get_class_subtype(ir_type *clss, size_t pos)
836 assert(clss->type_op == type_class);
837 assert(pos < get_class_n_subtypes(clss));
838 return clss->attr.ca.subtypes[pos];
841 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
843 size_t i, n_subtypes = get_class_n_subtypes(clss);
844 assert(is_Class_type(subclass));
845 for (i = 0; i < n_subtypes; ++i) {
846 if (get_class_subtype(clss, i) == subclass)
852 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
854 assert(clss->type_op == type_class);
855 assert(pos < get_class_n_subtypes(clss));
856 clss->attr.ca.subtypes[pos] = subtype;
859 void remove_class_subtype(ir_type *clss, ir_type *subtype)
862 assert(clss && (clss->type_op == type_class));
863 for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
864 if (clss->attr.ca.subtypes[i] == subtype) {
865 for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
866 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
867 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
873 void add_class_supertype(ir_type *clss, ir_type *supertype)
877 assert(clss && (clss->type_op == type_class));
878 assert(supertype && (supertype -> type_op == type_class));
879 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
880 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
881 if (get_class_subtype(supertype, i) == clss)
882 /* Class already registered */
885 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
888 size_t get_class_n_supertypes(const ir_type *clss)
890 assert(clss->type_op == type_class);
891 return ARR_LEN(clss->attr.ca.supertypes);
894 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
896 size_t i, n_supertypes = get_class_n_supertypes(clss);
897 assert(super_clss && (super_clss->type_op == type_class));
898 for (i = 0; i < n_supertypes; i++) {
899 if (get_class_supertype(clss, i) == super_clss)
905 ir_type *get_class_supertype(ir_type *clss, size_t pos)
907 assert(clss->type_op == type_class);
908 assert(pos < get_class_n_supertypes(clss));
909 return clss->attr.ca.supertypes[pos];
912 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
914 assert(clss->type_op == type_class);
915 assert(pos < get_class_n_supertypes(clss));
916 clss->attr.ca.supertypes[pos] = supertype;
919 void remove_class_supertype(ir_type *clss, ir_type *supertype)
922 assert(clss && (clss->type_op == type_class));
923 for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
924 if (clss->attr.ca.supertypes[i] == supertype) {
925 for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
926 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
927 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
933 ir_entity *get_class_type_info(const ir_type *clss)
935 return clss->attr.ca.type_info;
938 void set_class_type_info(ir_type *clss, ir_entity *ent)
940 clss->attr.ca.type_info = ent;
942 ent->repr_class = clss;
945 ir_peculiarity get_class_peculiarity(const ir_type *clss)
947 assert(clss && (clss->type_op == type_class));
948 return clss->attr.ca.peculiarity;
951 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
953 assert(clss && (clss->type_op == type_class));
954 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
955 clss->attr.ca.peculiarity = pec;
958 unsigned (get_class_vtable_size)(const ir_type *clss)
960 return _get_class_vtable_size(clss);
963 void (set_class_vtable_size)(ir_type *clss, unsigned size)
965 _set_class_vtable_size(clss, size);
968 int (is_class_final)(const ir_type *clss)
970 return _is_class_final(clss);
973 void (set_class_final)(ir_type *clss, int flag)
975 _set_class_final(clss, flag);
978 int (is_class_interface)(const ir_type *clss)
980 return _is_class_interface(clss);
983 void (set_class_interface)(ir_type *clss, int flag)
985 _set_class_interface(clss, flag);
988 int (is_class_abstract)(const ir_type *clss)
990 return _is_class_abstract(clss);
993 void (set_class_abstract)(ir_type *clss, int final)
995 _set_class_abstract(clss, final);
998 void set_class_dfn(ir_type *clss, int dfn)
1000 clss->attr.ca.dfn = dfn;
1003 int get_class_dfn(const ir_type *clss)
1005 return (clss->attr.ca.dfn);
1008 int (is_Class_type)(const ir_type *clss)
1010 return _is_class_type(clss);
1013 void set_class_mode(ir_type *tp, ir_mode *mode)
1015 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1016 assert(get_type_state(tp) == layout_fixed &&
1017 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1021 void set_class_size(ir_type *tp, unsigned size)
1027 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1029 ir_type *res = new_type(type_struct, NULL, db);
1032 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1037 ir_type *new_type_struct(ident *name)
1039 return new_d_type_struct (name, NULL);
1042 void free_struct_entities(ir_type *strct)
1045 assert(strct && (strct->type_op == type_struct));
1046 /* we must iterate backward here */
1047 for (i = get_struct_n_members(strct); i > 0;)
1048 free_entity(get_struct_member(strct, --i));
1051 void free_struct_attrs(ir_type *strct)
1053 assert(strct && (strct->type_op == type_struct));
1054 DEL_ARR_F(strct->attr.sa.members);
1057 ident *get_struct_ident(const ir_type *strct)
1059 assert(strct->type_op == type_struct);
1063 const char *get_struct_name(const ir_type *strct)
1065 if (get_struct_ident(strct) == NULL)
1067 return get_id_str(get_struct_ident(strct));
1070 size_t get_struct_n_members(const ir_type *strct)
1072 assert(strct->type_op == type_struct);
1073 return ARR_LEN(strct->attr.sa.members);
1076 static void add_struct_member(ir_type *strct, ir_entity *member)
1078 assert(strct && (strct->type_op == type_struct));
1079 assert(get_type_tpop(get_entity_type(member)) != type_method);
1080 assert(strct != get_entity_type(member) && "recursive type");
1081 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1084 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1086 assert(strct && (strct->type_op == type_struct));
1087 assert(pos < get_struct_n_members(strct));
1088 return strct->attr.sa.members[pos];
1091 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1094 assert(strct && (strct->type_op == type_struct));
1095 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1096 if (get_struct_member(strct, i) == mem)
1102 static void remove_struct_member(ir_type *strct, ir_entity *member)
1105 assert(strct && (strct->type_op == type_struct));
1106 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1107 if (strct->attr.sa.members[i] == member) {
1108 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1109 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1110 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1116 int (is_Struct_type)(const ir_type *strct)
1118 return _is_struct_type(strct);
1121 void set_struct_mode(ir_type *tp, ir_mode *mode)
1123 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1124 assert(get_type_state(tp) == layout_fixed &&
1125 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1129 void set_struct_size(ir_type *tp, unsigned size)
1134 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1138 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1139 res = new_type(type_method, mode_P_code, db);
1140 res->flags |= tf_layout_fixed;
1141 res->size = get_mode_size_bytes(mode_P_code);
1142 res->attr.ma.n_params = n_param;
1143 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1144 res->attr.ma.n_res = n_res;
1145 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1146 res->attr.ma.variadicity = variadicity_non_variadic;
1147 res->attr.ma.properties = mtp_no_property;
1152 ir_type *new_type_method(size_t n_param, size_t n_res)
1154 return new_d_type_method(n_param, n_res, NULL);
1157 ir_type *clone_type_method(ir_type *tp)
1165 assert(is_Method_type(tp));
1168 n_params = tp->attr.ma.n_params;
1169 n_res = tp->attr.ma.n_res;
1172 res = new_type(type_method, mode, db);
1174 res->flags = tp->flags;
1175 res->higher_type = tp->higher_type;
1176 res->size = tp->size;
1177 res->attr.ma.n_params = n_params;
1178 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1179 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1180 res->attr.ma.n_res = n_res;
1181 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1182 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1183 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1184 res->attr.ma.properties = tp->attr.ma.properties;
1185 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1190 void free_method_entities(ir_type *method)
1193 assert(method && (method->type_op == type_method));
1196 void free_method_attrs(ir_type *method)
1198 assert(method && (method->type_op == type_method));
1199 free(method->attr.ma.params);
1200 free(method->attr.ma.res_type);
1203 size_t (get_method_n_params)(const ir_type *method)
1205 return _get_method_n_params(method);
1208 ir_type *get_method_param_type(const ir_type *method, size_t pos)
1211 assert(method->type_op == type_method);
1212 assert(pos < get_method_n_params(method));
1213 res = method->attr.ma.params[pos].tp;
1214 assert(res != NULL && "empty method param type");
1218 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1220 assert(method->type_op == type_method);
1221 assert(pos < get_method_n_params(method));
1222 method->attr.ma.params[pos].tp = tp;
1225 size_t (get_method_n_ress)(const ir_type *method)
1227 return _get_method_n_ress(method);
1230 ir_type *get_method_res_type(const ir_type *method, size_t pos)
1233 assert(method->type_op == type_method);
1234 assert(pos < get_method_n_ress(method));
1235 res = method->attr.ma.res_type[pos].tp;
1236 assert(res != NULL && "empty method return type");
1240 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1242 assert(method->type_op == type_method);
1243 assert(pos < get_method_n_ress(method));
1244 /* set the result ir_type */
1245 method->attr.ma.res_type[pos].tp = tp;
1246 /* If information constructed set pass-by-value representation. */
1249 const char *get_variadicity_name(ir_variadicity vari)
1251 #define X(a) case a: return #a
1253 X(variadicity_non_variadic);
1254 X(variadicity_variadic);
1261 ir_variadicity get_method_variadicity(const ir_type *method)
1263 assert(method && (method->type_op == type_method));
1264 return method->attr.ma.variadicity;
1267 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1269 assert(method && (method->type_op == type_method));
1270 method->attr.ma.variadicity = vari;
1273 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1275 return _get_method_additional_properties(method);
1278 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1280 _set_method_additional_properties(method, mask);
1283 void (add_method_additional_properties)(ir_type *method,
1284 mtp_additional_properties flag)
1286 _add_method_additional_properties(method, flag);
1289 unsigned (get_method_calling_convention)(const ir_type *method)
1291 return _get_method_calling_convention(method);
1294 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1296 _set_method_calling_convention(method, cc_mask);
1299 unsigned get_method_n_regparams(ir_type *method)
1301 unsigned cc = get_method_calling_convention(method);
1302 assert(IS_FASTCALL(cc));
1304 return cc & ~cc_bits;
1307 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1309 unsigned cc = get_method_calling_convention(method);
1310 assert(IS_FASTCALL(cc));
1312 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1315 int (is_Method_type)(const ir_type *method)
1317 return _is_method_type(method);
1321 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1323 ir_type *res = new_type(type_union, NULL, db);
1326 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1331 ir_type *new_type_union(ident *name)
1333 return new_d_type_union(name, NULL);
1336 void free_union_entities(ir_type *uni)
1339 assert(uni && (uni->type_op == type_union));
1340 /* we must iterate backward here */
1341 for (i = get_union_n_members(uni); i > 0;)
1342 free_entity(get_union_member(uni, --i));
1345 void free_union_attrs(ir_type *uni)
1347 assert(uni && (uni->type_op == type_union));
1348 DEL_ARR_F(uni->attr.ua.members);
1351 ident *get_union_ident(const ir_type *uni)
1353 assert(uni->type_op == type_union);
1357 const char *get_union_name(const ir_type *uni)
1359 if (get_union_ident(uni) == NULL)
1361 return get_id_str(get_union_ident(uni));
1364 size_t get_union_n_members(const ir_type *uni)
1366 assert(uni->type_op == type_union);
1367 return ARR_LEN(uni->attr.ua.members);
1370 static void add_union_member(ir_type *uni, ir_entity *member)
1372 assert(uni->type_op == type_union);
1373 assert(uni != get_entity_type(member) && "recursive type");
1374 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1377 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1379 assert(uni->type_op == type_union);
1380 assert(pos < get_union_n_members(uni));
1381 return uni->attr.ua.members[pos];
1384 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1387 assert(uni && (uni->type_op == type_union));
1388 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1389 if (get_union_member(uni, i) == mem)
1395 static void remove_union_member(ir_type *uni, ir_entity *member)
1398 assert(uni && (uni->type_op == type_union));
1399 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1400 if (uni->attr.ua.members[i] == member) {
1401 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1402 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1403 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1409 int (is_Union_type)(const ir_type *uni)
1411 return _is_union_type(uni);
1414 void set_union_size(ir_type *tp, unsigned size)
1421 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1427 ir_graph *irg = get_const_code_irg();
1429 assert(!is_Method_type(element_type));
1431 res = new_type(type_array, NULL, db);
1432 res->attr.aa.n_dimensions = n_dimensions;
1433 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1434 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1435 res->attr.aa.order = XMALLOCNZ(size_t, n_dimensions);
1437 unk = new_r_Unknown(irg, mode_Iu);
1438 for (i = 0; i < n_dimensions; i++) {
1439 res->attr.aa.lower_bound[i] =
1440 res->attr.aa.upper_bound[i] = unk;
1441 res->attr.aa.order[i] = i;
1444 res->attr.aa.element_type = element_type;
1445 res->attr.aa.element_ent
1446 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1447 res->attr.aa.element_ent->owner = res;
1453 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1455 return new_d_type_array(n_dimensions, element_type, NULL);
1458 void free_array_automatic_entities(ir_type *array)
1460 assert(array->type_op == type_array);
1461 free_entity(get_array_element_entity(array));
1464 void free_array_entities(ir_type *array)
1467 assert(array->type_op == type_array);
1470 void free_array_attrs(ir_type *array)
1472 assert(array->type_op == type_array);
1473 free(array->attr.aa.lower_bound);
1474 free(array->attr.aa.upper_bound);
1475 free(array->attr.aa.order);
1478 size_t get_array_n_dimensions(const ir_type *array)
1480 assert(array->type_op == type_array);
1481 return array->attr.aa.n_dimensions;
1484 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1485 ir_node *upper_bound)
1487 assert(array->type_op == type_array);
1488 assert(lower_bound && "lower_bound node may not be NULL.");
1489 assert(upper_bound && "upper_bound node may not be NULL.");
1490 assert(dimension < array->attr.aa.n_dimensions);
1491 array->attr.aa.lower_bound[dimension] = lower_bound;
1492 array->attr.aa.upper_bound[dimension] = upper_bound;
1495 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1498 ir_graph *irg = get_const_code_irg();
1499 set_array_bounds(array, dimension,
1500 new_r_Const_long(irg, mode_Iu, lower_bound),
1501 new_r_Const_long(irg, mode_Iu, upper_bound));
1504 void set_array_lower_bound(ir_type *array, size_t dimension,
1505 ir_node *lower_bound)
1507 assert(array->type_op == type_array);
1508 assert(lower_bound && "lower_bound node may not be NULL.");
1509 array->attr.aa.lower_bound[dimension] = lower_bound;
1512 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1514 ir_graph *irg = get_const_code_irg();
1515 set_array_lower_bound(array, dimension,
1516 new_r_Const_long(irg, mode_Iu, lower_bound));
1519 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1521 assert(array->type_op == type_array);
1522 assert(upper_bound && "upper_bound node may not be NULL.");
1523 array->attr.aa.upper_bound[dimension] = upper_bound;
1526 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1528 ir_graph *irg = get_const_code_irg();
1529 set_array_upper_bound(array, dimension,
1530 new_r_Const_long(irg, mode_Iu, upper_bound));
1533 int has_array_lower_bound(const ir_type *array, size_t dimension)
1535 assert(array->type_op == type_array);
1536 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1539 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1541 assert(array->type_op == type_array);
1542 return array->attr.aa.lower_bound[dimension];
1545 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1548 assert(array->type_op == type_array);
1549 node = array->attr.aa.lower_bound[dimension];
1550 assert(is_Const(node));
1551 return get_tarval_long(get_Const_tarval(node));
1554 int has_array_upper_bound(const ir_type *array, size_t dimension)
1556 assert(array->type_op == type_array);
1557 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1560 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1562 assert(array->type_op == type_array);
1563 return array->attr.aa.upper_bound[dimension];
1566 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1569 assert(array->type_op == type_array);
1570 node = array->attr.aa.upper_bound[dimension];
1571 assert(is_Const(node));
1572 return get_tarval_long(get_Const_tarval(node));
1575 void set_array_order(ir_type *array, size_t dimension, size_t order)
1577 assert(array->type_op == type_array);
1578 array->attr.aa.order[dimension] = order;
1581 size_t get_array_order(const ir_type *array, size_t dimension)
1583 assert(array->type_op == type_array);
1584 return array->attr.aa.order[dimension];
1587 size_t find_array_dimension(const ir_type *array, size_t order)
1591 assert(array->type_op == type_array);
1593 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1594 if (array->attr.aa.order[dim] == order)
1600 void set_array_element_type(ir_type *array, ir_type *tp)
1602 assert(array->type_op == type_array);
1603 assert(!is_Method_type(tp));
1604 array->attr.aa.element_type = tp;
1607 ir_type *get_array_element_type(const ir_type *array)
1609 assert(array->type_op == type_array);
1610 return array->attr.aa.element_type;
1613 void set_array_element_entity(ir_type *array, ir_entity *ent)
1615 assert(array->type_op == type_array);
1616 assert((get_entity_type(ent)->type_op != type_method));
1617 array->attr.aa.element_ent = ent;
1618 array->attr.aa.element_type = get_entity_type(ent);
1621 ir_entity *get_array_element_entity(const ir_type *array)
1623 assert(array->type_op == type_array);
1624 return array->attr.aa.element_ent;
1627 int is_array_variable_size(const ir_type *array)
1629 assert(array->type_op == type_array);
1630 return (array->flags & tf_variable_size) != 0;
1633 void set_array_variable_size(ir_type *array, int flag)
1635 assert(array->type_op == type_array);
1636 array->flags = (array->flags & ~tf_variable_size)
1637 | (flag != 0 ? tf_variable_size : 0);
1640 int (is_Array_type)(const ir_type *array)
1642 return _is_array_type(array);
1645 void set_array_size(ir_type *tp, unsigned size)
1647 /* FIXME: Here we should make some checks with the element type size */
1652 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1656 res = new_type(type_enumeration, NULL, db);
1658 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1663 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1665 return new_d_type_enumeration(name, n_enums, NULL);
1668 void free_enumeration_entities(ir_type *enumeration)
1671 assert(enumeration->type_op == type_enumeration);
1674 void free_enumeration_attrs(ir_type *enumeration)
1676 assert(enumeration->type_op == type_enumeration);
1677 DEL_ARR_F(enumeration->attr.ea.enumer);
1680 ident *get_enumeration_ident(const ir_type *enumeration)
1682 assert(enumeration->type_op == type_enumeration);
1683 return enumeration->name;
1686 const char *get_enumeration_name(const ir_type *enumeration)
1688 if (get_enumeration_ident(enumeration) == NULL)
1690 return get_id_str(get_enumeration_ident(enumeration));
1693 size_t get_enumeration_n_enums(const ir_type *enumeration)
1695 assert(enumeration->type_op == type_enumeration);
1696 return ARR_LEN(enumeration->attr.ea.enumer);
1699 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1702 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1703 enumeration->attr.ea.enumer[pos].nameid = nameid;
1704 enumeration->attr.ea.enumer[pos].value = con;
1705 enumeration->attr.ea.enumer[pos].owner = enumeration;
1708 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1710 assert(enumeration->type_op == type_enumeration);
1711 assert(pos < get_enumeration_n_enums(enumeration));
1712 return &enumeration->attr.ea.enumer[pos];
1715 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1717 return enum_cnst->owner;
1720 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1722 enum_cnst->value = con;
1725 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1727 return enum_cnst->value;
1730 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1732 enum_cnst->nameid = id;
1735 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1737 return enum_cnst->nameid;
1740 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1742 return get_id_str(enum_cnst->nameid);
1745 int (is_Enumeration_type)(const ir_type *enumeration)
1747 return _is_enumeration_type(enumeration);
1750 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1752 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1753 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1754 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1756 tp->size = get_mode_size_bytes(mode);
1762 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1767 if (is_Method_type(points_to) || is_code_type(points_to)) {
1773 res = new_type(type_pointer, mode, db);
1774 res->attr.pa.points_to = points_to;
1775 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1776 res->size = get_mode_size_bytes(res->mode);
1777 res->flags |= tf_layout_fixed;
1782 ir_type *new_type_pointer(ir_type *points_to)
1784 return new_d_type_pointer(points_to, NULL);
1787 void free_pointer_entities(ir_type *pointer)
1790 assert(pointer && (pointer->type_op == type_pointer));
1793 void free_pointer_attrs(ir_type *pointer)
1796 assert(pointer && (pointer->type_op == type_pointer));
1799 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1801 assert(pointer && (pointer->type_op == type_pointer));
1802 pointer->attr.pa.points_to = tp;
1805 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1807 assert(pointer && (pointer->type_op == type_pointer));
1808 return pointer->attr.pa.points_to;
1811 int (is_Pointer_type)(const ir_type *pointer)
1813 return _is_pointer_type(pointer);
1816 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1818 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1819 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1820 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1822 tp->size = get_mode_size_bytes(mode);
1826 ir_type *find_pointer_type_to_type(ir_type *tp)
1828 size_t i, n = get_irp_n_types();
1829 for (i = 0; i < n; ++i) {
1830 ir_type *found = get_irp_type(i);
1831 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1834 return get_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 void set_compound_variable_size(ir_type *tp, int variable_size_flag)
1907 assert(is_compound_type(tp));
1908 tp->flags = (tp->flags & ~tf_variable_size)
1909 | (variable_size_flag != 0 ? tf_variable_size : 0);
1912 int is_compound_variable_size(const ir_type *tp)
1914 assert(is_compound_type(tp));
1915 return (tp->flags & tf_variable_size) != 0;
1918 int is_compound_type(const ir_type *tp)
1920 assert(tp->kind == k_type);
1921 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1924 ident *get_compound_ident(const ir_type *tp)
1926 assert(is_compound_type(tp));
1930 const char *get_compound_name(const ir_type *tp)
1932 if (get_compound_ident(tp) == NULL)
1934 return get_id_str(get_compound_ident(tp));
1937 void remove_compound_member(ir_type *compound, ir_entity *entity)
1939 switch (get_type_tpop_code(compound)) {
1940 case tpo_class: remove_class_member(compound, entity); break;
1941 case tpo_struct: remove_struct_member(compound, entity); break;
1942 case tpo_union: remove_union_member(compound, entity); break;
1944 panic("argument for remove_compound_member not a compound type");
1948 void add_compound_member(ir_type *compound, ir_entity *entity)
1950 switch (get_type_tpop_code(compound)) {
1951 case tpo_class: add_class_member(compound, entity); break;
1952 case tpo_struct: add_struct_member(compound, entity); break;
1953 case tpo_union: add_union_member(compound, entity); break;
1955 panic("argument for add_compound_member not a compound type");
1959 int is_code_type(const ir_type *tp)
1961 assert(tp->kind == k_type);
1962 return tp->type_op == tpop_code;
1965 int is_unknown_type(const ir_type *tp)
1967 assert(tp->kind == k_type);
1968 return tp->type_op == tpop_unknown;
1971 int is_none_type(const ir_type *tp)
1973 assert(tp->kind == k_type);
1974 return tp->type_op == tpop_none;
1977 int is_frame_type(const ir_type *tp)
1979 return tp->flags & tf_frame_type;
1982 ir_type *new_type_frame(void)
1984 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
1986 res->flags |= tf_frame_type;
1988 /* It is not possible to derive from the frame type. Set the final flag. */
1989 set_class_final(res, 1);
1994 ir_type *clone_frame_type(ir_type *type)
1999 assert(is_frame_type(type));
2000 /* the entity link resource should be allocated if this function is called */
2001 assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
2003 res = new_type_frame();
2004 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2005 ir_entity *ent = get_class_member(type, i);
2006 ir_entity *nent = copy_entity_own(ent, res);
2007 set_entity_link(ent, nent);
2008 set_entity_link(nent, ent);
2013 void set_default_size(ir_type *tp, unsigned size)
2018 void default_layout_compound_type(ir_type *type)
2021 size_t n = get_compound_n_members(type);
2023 unsigned align_all = 1;
2024 bool var_size = is_compound_variable_size(type);
2026 for (i = 0; i < n; ++i) {
2027 ir_entity *entity = get_compound_member(type, i);
2028 ir_type *entity_type = get_entity_type(entity);
2031 unsigned entity_size;
2033 if (is_Method_type(entity_type))
2036 if (i+1 < n || !var_size) {
2037 assert(get_type_state(entity_type) == layout_fixed);
2038 entity_size = get_type_size_bytes(entity_type);
2043 align = get_type_alignment_bytes(entity_type);
2044 align_all = align > align_all ? align : align_all;
2045 misalign = (align ? size % align : 0);
2046 size += (misalign ? align - misalign : 0);
2048 set_entity_offset(entity, size);
2049 if (!is_Union_type(type)) {
2050 size += entity_size;
2053 if (align_all > 0 && size % align_all) {
2054 size += align_all - (size % align_all);
2056 if (align_all > get_type_alignment_bytes(type)) {
2057 set_type_alignment_bytes(type, align_all);
2059 set_type_size_bytes(type, size);
2060 set_type_state(type, layout_fixed);
2063 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2071 unsigned frame_size = get_type_size_bytes(frame_type);
2072 unsigned frame_align = get_type_alignment_bytes(frame_type);
2073 static unsigned area_cnt = 0;
2075 assert(is_frame_type(frame_type));
2076 assert(get_type_state(frame_type) == layout_fixed);
2077 assert(get_type_alignment_bytes(frame_type) > 0);
2078 set_type_state(frame_type, layout_undefined);
2080 if (irp->byte_type == NULL)
2081 irp->byte_type = new_type_primitive(mode_Bu);
2083 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2084 name = new_id_from_str(buf);
2086 tp = new_type_array(1, irp->byte_type);
2087 set_array_bounds_int(tp, 0, 0, size);
2088 set_type_alignment_bytes(tp, alignment);
2089 set_type_size_bytes(tp, size);
2093 unsigned delta = (size + frame_align - 1) & ~(frame_align - 1);
2094 /* fix all offsets so far */
2095 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2096 ir_entity *ent = get_class_member(frame_type, i);
2098 set_entity_offset(ent, get_entity_offset(ent) + delta);
2100 /* calculate offset and new type size */
2102 frame_size += delta;
2104 /* calculate offset and new type size */
2105 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2106 frame_size = offset + size;
2109 area = new_entity(frame_type, name, tp);
2110 set_entity_offset(area, offset);
2111 set_type_size_bytes(frame_type, frame_size);
2112 if (alignment > frame_align) {
2113 set_type_alignment_bytes(frame_type, alignment);
2116 /* mark this entity as compiler generated */
2117 set_entity_compiler_generated(area, 1);
2119 set_type_state(frame_type, layout_fixed);
2123 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2127 type_dbg_info *tdbgi = get_type_dbg_info(type);
2128 if (tdbgi != NULL) {
2129 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2133 /* we have to construct some name... */
2134 switch (get_type_tpop_code(type)) {
2135 case tpo_uninitialized:
2138 snprintf(buffer, buffer_size, "code");
2142 id = get_class_ident(type);
2143 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2147 id = get_struct_ident(type);
2148 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2152 id = get_union_ident(type);
2153 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2156 case tpo_enumeration:
2157 id = get_enumeration_ident(type);
2158 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2162 snprintf(buffer, buffer_size, "unknown type");
2166 p = snprintf(buffer, buffer_size, "pointer to ");
2169 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2173 p = snprintf(buffer, buffer_size, "array of ");
2176 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2180 id = get_mode_ident(get_type_mode(type));
2181 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2185 snprintf(buffer, buffer_size, "none");
2188 /* TODO: we should print argument and return types here... */
2189 snprintf(buffer, buffer_size, "method type");
2192 snprintf(buffer, buffer_size, "invalid type");