2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Representation of types.
9 * @author Goetz Lindenmaier, Michael Beck
12 * Implementation of the datastructure to hold
15 * This module supplies a datastructure to represent all types
16 * known in the compiled program. This includes types specified
17 * in the program as well as types defined by the language. In the
18 * view of the intermediate representation there is no difference
19 * between these types.
21 * There exist several kinds of types, arranged by the structure of
22 * the type. A type is described by a set of attributes. Some of
23 * these attributes are common to all types, others depend on the
26 * Types are different from the modes defined in irmode: Types are
27 * on the level of the programming language, modes at the level of
28 * the target processor.
53 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db);
55 ir_type *get_none_type(void)
57 return irp->none_type;
60 ir_type *get_code_type(void)
62 return irp->code_type;
65 ir_type *get_unknown_type(void)
67 return irp->unknown_type;
70 void ir_init_type(ir_prog *irp)
72 /* construct none and unknown type. */
73 irp->none_type = new_type(tpop_none, mode_BAD, NULL);
74 set_type_size_bytes(irp->none_type, 0);
75 set_type_state (irp->none_type, layout_fixed);
77 irp->code_type = new_type(tpop_code, mode_ANY, NULL);
78 set_type_state(irp->code_type, layout_fixed);
80 irp->unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
81 set_type_size_bytes(irp->unknown_type, 0);
82 set_type_state (irp->unknown_type, layout_fixed);
85 void ir_finish_type(ir_prog *irp)
87 /** nothing todo. (The none, code, unknown types are in the global type list
92 ir_visited_t firm_type_visited;
94 void (set_master_type_visited)(ir_visited_t val)
96 _set_master_type_visited(val);
99 ir_visited_t (get_master_type_visited)(void)
101 return _get_master_type_visited();
104 void (inc_master_type_visited)(void)
106 _inc_master_type_visited();
110 * Creates a new type representation:
112 * @param type_op the kind of this type. May not be type_id.
113 * @param mode the mode to be used for this type, may be NULL
114 * @param db debug info
116 * @return A new type of the given type. The remaining private attributes are
117 * not initialized. The type is in state layout_undefined.
119 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db)
124 node_size = offsetof(ir_type, attr) + type_op->attr_size;
125 res = (ir_type*)xmalloc(node_size);
126 memset(res, 0, node_size);
129 res->type_op = type_op;
131 res->visibility = ir_visibility_external;
132 res->flags = tf_none;
139 res->nr = get_irp_new_node_nr();
140 #endif /* defined DEBUG_libfirm */
142 add_irp_type(res); /* Remember the new type global. */
147 void free_type_entities(ir_type *tp)
149 const tp_op *op = get_type_tpop(tp);
150 if (op->ops.free_entities != NULL)
151 op->ops.free_entities(tp);
154 static void free_type_attrs(ir_type *tp)
156 const tp_op *tpop = get_type_tpop(tp);
158 if (tpop->ops.free_attrs)
159 tpop->ops.free_attrs(tp);
162 void free_type(ir_type *tp)
164 const tp_op *op = get_type_tpop(tp);
166 free_type_entities(tp);
167 /* Remove from list of all types */
169 /* Free the attributes of the type. */
171 /* Free entities automatically allocated with the ir_type */
172 if (op->ops.free_auto_entities)
173 op->ops.free_auto_entities(tp);
174 /* And now the type itself... */
181 void *(get_type_link)(const ir_type *tp)
183 return _get_type_link(tp);
186 void (set_type_link)(ir_type *tp, void *l)
188 _set_type_link(tp, l);
191 const tp_op *(get_type_tpop)(const ir_type *tp)
193 return _get_type_tpop(tp);
196 ident *(get_type_tpop_nameid)(const ir_type *tp)
198 return _get_type_tpop_nameid(tp);
201 const char* get_type_tpop_name(const ir_type *tp)
203 assert(tp && tp->kind == k_type);
204 return get_id_str(tp->type_op->name);
207 tp_opcode (get_type_tpop_code)(const ir_type *tp)
209 return _get_type_tpop_code(tp);
212 ir_mode *(get_type_mode)(const ir_type *tp)
214 return _get_type_mode(tp);
217 void set_type_mode(ir_type *tp, ir_mode *mode)
219 const tp_op *tpop = get_type_tpop(tp);
221 if (tpop->ops.set_type_mode)
222 tpop->ops.set_type_mode(tp, mode);
224 assert(0 && "setting a mode is NOT allowed for this type");
227 long get_type_nr(const ir_type *tp)
233 return (long)PTR_TO_INT(tp);
237 unsigned (get_type_size_bytes)(const ir_type *tp)
239 return _get_type_size_bytes(tp);
242 ir_visibility get_type_visibility(const ir_type *tp)
245 return tp->visibility;
248 void set_type_visibility(ir_type *tp, ir_visibility v)
254 void set_type_size_bytes(ir_type *tp, unsigned size)
256 const tp_op *tpop = get_type_tpop(tp);
257 tpop->ops.set_type_size(tp, size);
260 unsigned get_type_alignment_bytes(ir_type *tp)
267 /* alignment NOT set calculate it "on demand" */
269 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
270 else if (is_Array_type(tp))
271 align = get_type_alignment_bytes(get_array_element_type(tp));
272 else if (is_compound_type(tp)) {
273 size_t i, n = get_compound_n_members(tp);
276 for (i = 0; i < n; ++i) {
277 ir_type *t = get_entity_type(get_compound_member(tp, i));
278 unsigned a = get_type_alignment_bytes(t);
283 } else if (is_Method_type(tp)) {
293 void set_type_alignment_bytes(ir_type *tp, unsigned align)
295 assert(tp && tp->kind == k_type);
296 /* Methods don't have an alignment. */
297 if (tp->type_op != type_method) {
302 const char *get_type_state_name(ir_type_state s)
304 #define X(a) case a: return #a
313 ir_type_state (get_type_state)(const ir_type *tp)
315 return _get_type_state(tp);
318 void set_type_state(ir_type *tp, ir_type_state state)
320 assert(tp && tp->kind == k_type);
322 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
323 (tp->type_op == type_method))
326 /* Just a correctness check: */
327 if (state == layout_fixed) {
329 switch (get_type_tpop_code(tp)) {
331 if (tp != get_glob_type()) {
332 size_t n_mem = get_class_n_members(tp);
333 for (i = 0; i < n_mem; i++) {
334 ir_entity *entity = get_class_member(tp, i);
335 if (is_Method_type(get_entity_type(entity)))
337 assert(get_entity_offset(entity) > -1);
342 for (i = 0; i < get_struct_n_members(tp); i++) {
343 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
350 case tpo_enumeration: {
352 size_t n_enums = get_enumeration_n_enums(tp);
353 assert(get_type_mode(tp) != NULL);
354 for (i = 0; i < n_enums; ++i) {
355 ir_enum_const *ec = get_enumeration_const(tp, i);
356 ir_tarval *tv = get_enumeration_value(ec);
357 assert(tv != NULL && tv != tarval_bad);
365 if (state == layout_fixed)
366 tp->flags |= tf_layout_fixed;
368 tp->flags &= ~tf_layout_fixed;
371 ir_visited_t (get_type_visited)(const ir_type *tp)
373 return _get_type_visited(tp);
376 void (set_type_visited)(ir_type *tp, ir_visited_t num)
378 _set_type_visited(tp, num);
381 void (mark_type_visited)(ir_type *tp)
383 _mark_type_visited(tp);
386 int (type_visited)(const ir_type *tp)
388 return _type_visited(tp);
391 int (type_not_visited)(const ir_type *tp)
393 return _type_not_visited(tp);
396 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
398 return _get_type_dbg_info(tp);
401 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
403 _set_type_dbg_info(tp, db);
406 int (is_type)(const void *thing)
408 return _is_type(thing);
411 int equal_type(ir_type *typ1, ir_type *typ2)
418 if (typ1 == typ2) return 1;
420 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
421 typ1->name != typ2->name ||
422 (get_type_mode(typ1) != get_type_mode(typ2)) ||
423 (get_type_state(typ1) != get_type_state(typ2)))
425 if ((get_type_state(typ1) == layout_fixed) &&
426 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
429 switch (get_type_tpop_code(typ1)) {
431 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
432 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
433 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
434 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
435 /** Compare the members **/
436 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
437 /* First sort the members of typ2 */
438 for (i = 0; i < get_class_n_members(typ1); i++) {
439 ir_entity *e1 = get_class_member(typ1, i);
440 for (j = 0; j < get_class_n_members(typ2); j++) {
441 ir_entity *e2 = get_class_member(typ2, j);
442 if (get_entity_name(e1) == get_entity_name(e2))
446 for (i = 0; i < get_class_n_members(typ1); i++) {
447 if (!m[i] || get_class_member(typ1, i) != m[i])
450 /** Compare the supertypes **/
451 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
452 /* First sort the supertypes of typ2 */
453 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
454 ir_type *t1 = get_class_supertype(typ1, i);
455 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
456 ir_type *t2 = get_class_supertype(typ2, j);
457 if (t2->name == t1->name)
461 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
462 if (!t[i] || /* Found no counterpart */
463 get_class_supertype(typ1, i) != t[i])
469 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
470 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
471 /* First sort the members of lt */
472 for (i = 0; i < get_struct_n_members(typ1); i++) {
473 ir_entity *e1 = get_struct_member(typ1, i);
474 for (j = 0; j < get_struct_n_members(typ2); j++) {
475 ir_entity *e2 = get_struct_member(typ2, j);
476 if (get_entity_name(e1) == get_entity_name(e2))
480 for (i = 0; i < get_struct_n_members(typ1); i++) {
481 if (!m[i] || get_struct_member(typ1, i) != m[i])
490 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
491 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
492 if (get_method_calling_convention(typ1) !=
493 get_method_calling_convention(typ2)) return 0;
495 n_param1 = get_method_n_params(typ1);
496 n_param2 = get_method_n_params(typ2);
498 if (n_param1 != n_param2) return 0;
500 for (i = 0; i < n_param1; i++) {
501 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
504 for (i = 0; i < get_method_n_ress(typ1); i++) {
505 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
511 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
512 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
513 /* First sort the members of lt */
514 for (i = 0; i < get_union_n_members(typ1); i++) {
515 ir_entity *e1 = get_union_member(typ1, i);
516 for (j = 0; j < get_union_n_members(typ2); j++) {
517 ir_entity *e2 = get_union_member(typ2, j);
518 if (get_entity_name(e1) == get_entity_name(e2))
522 for (i = 0; i < get_union_n_members(typ1); i++) {
523 if (!m[i] || get_union_member(typ1, i) != m[i])
529 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
531 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
533 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
534 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
535 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
537 if (get_array_order(typ1, i) != get_array_order(typ2, i))
538 assert(0 && "type compare with different dimension orders not implemented");
542 case tpo_enumeration:
543 assert(0 && "enumerations not implemented");
547 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
559 int smaller_type(ir_type *st, ir_type *lt)
566 if (st == lt) return 1;
568 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
571 switch (get_type_tpop_code(st)) {
573 return is_SubClass_of(st, lt);
576 n_st_members = get_struct_n_members(st);
577 if (n_st_members != get_struct_n_members(lt))
580 m = ALLOCANZ(ir_entity*, n_st_members);
581 /* First sort the members of lt */
582 for (i = 0; i < n_st_members; ++i) {
583 ir_entity *se = get_struct_member(st, i);
584 size_t n = get_struct_n_members(lt);
585 for (j = 0; j < n; ++j) {
586 ir_entity *le = get_struct_member(lt, j);
587 if (get_entity_name(le) == get_entity_name(se))
591 for (i = 0; i < n_st_members; i++) {
592 if (!m[i] || /* Found no counterpart */
593 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
599 size_t n_param1, n_param2;
601 /** FIXME: is this still 1? */
602 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
603 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
604 if (get_method_calling_convention(st) !=
605 get_method_calling_convention(lt)) return 0;
607 n_param1 = get_method_n_params(st);
608 n_param2 = get_method_n_params(lt);
610 if (n_param1 != n_param2) return 0;
612 for (i = 0; i < get_method_n_params(st); i++) {
613 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
616 for (i = 0; i < get_method_n_ress(st); i++) {
617 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
623 n_st_members = get_union_n_members(st);
624 if (n_st_members != get_union_n_members(lt)) return 0;
625 m = ALLOCANZ(ir_entity*, n_st_members);
626 /* First sort the members of lt */
627 for (i = 0; i < n_st_members; ++i) {
628 ir_entity *se = get_union_member(st, i);
629 size_t n = get_union_n_members(lt);
630 for (j = 0; j < n; ++j) {
631 ir_entity *le = get_union_member(lt, j);
632 if (get_entity_name(le) == get_entity_name(se))
636 for (i = 0; i < n_st_members; ++i) {
637 if (!m[i] || /* Found no counterpart */
638 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
644 ir_type *set, *let; /* small/large elt. ir_type */
645 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
647 set = get_array_element_type(st);
648 let = get_array_element_type(lt);
650 /* If the element types are different, set must be convertible
651 to let, and they must have the same size so that address
652 computations work out. To have a size the layout must
654 if ((get_type_state(set) != layout_fixed) ||
655 (get_type_state(let) != layout_fixed))
657 if (!smaller_type(set, let) ||
658 get_type_size_bytes(set) != get_type_size_bytes(let))
661 for (i = 0; i < get_array_n_dimensions(st); i++) {
662 if (get_array_lower_bound(lt, i))
663 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
665 if (get_array_upper_bound(lt, i))
666 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
671 case tpo_enumeration:
672 assert(0 && "enumerations not implemented");
676 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
681 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
691 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
695 res = new_type(type_class, NULL, db);
698 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
699 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
700 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
701 res->attr.ca.peculiarity = peculiarity_existent;
702 res->attr.ca.vtable_size = 0;
703 res->attr.ca.clss_flags = cf_none;
704 res->attr.ca.dfn = 0;
709 ir_type *new_type_class(ident *name)
711 return new_d_type_class(name, NULL);
714 void free_class_entities(ir_type *clss)
717 assert(clss && (clss->type_op == type_class));
718 /* we must iterate backward here */
719 for (i = get_class_n_members(clss); i > 0;)
720 free_entity(get_class_member(clss, --i));
723 void free_class_attrs(ir_type *clss)
725 assert(clss && (clss->type_op == type_class));
726 DEL_ARR_F(clss->attr.ca.members);
727 DEL_ARR_F(clss->attr.ca.subtypes);
728 DEL_ARR_F(clss->attr.ca.supertypes);
731 ident *get_class_ident(const ir_type *clss)
733 assert(clss->type_op == type_class);
737 const char *get_class_name(const ir_type *clss)
739 if (get_class_ident(clss) == NULL)
741 return get_id_str(get_class_ident(clss));
744 static void add_class_member(ir_type *clss, ir_entity *member)
746 assert(clss && (clss->type_op == type_class));
747 assert(clss != get_entity_type(member) && "recursive type");
748 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
751 size_t (get_class_n_members)(const ir_type *clss)
753 return _get_class_n_members(clss);
756 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
759 assert(clss && (clss->type_op == type_class));
760 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
761 if (get_class_member(clss, i) == mem)
764 return INVALID_MEMBER_INDEX;
767 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
769 return _get_class_member(clss, pos);
772 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
775 assert(clss && (clss->type_op == type_class));
776 n_mem = get_class_n_members(clss);
777 for (i = 0; i < n_mem; ++i) {
778 ir_entity *mem = get_class_member(clss, i);
779 if (get_entity_ident(mem) == name)
785 static void remove_class_member(ir_type *clss, ir_entity *member)
788 assert(clss && (clss->type_op == type_class));
789 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
790 if (clss->attr.ca.members[i] == member) {
791 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
792 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
793 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
799 void add_class_subtype(ir_type *clss, ir_type *subtype)
802 assert(clss->type_op == type_class);
803 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
804 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
805 if (get_class_supertype(subtype, i) == clss)
806 /* Class already registered */
809 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
812 size_t get_class_n_subtypes(const ir_type *clss)
814 assert(clss->type_op == type_class);
815 return ARR_LEN (clss->attr.ca.subtypes);
818 ir_type *get_class_subtype(ir_type *clss, size_t pos)
820 assert(clss->type_op == type_class);
821 assert(pos < get_class_n_subtypes(clss));
822 return clss->attr.ca.subtypes[pos];
825 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
827 size_t i, n_subtypes = get_class_n_subtypes(clss);
828 assert(is_Class_type(subclass));
829 for (i = 0; i < n_subtypes; ++i) {
830 if (get_class_subtype(clss, i) == subclass)
836 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
838 assert(clss->type_op == type_class);
839 assert(pos < get_class_n_subtypes(clss));
840 clss->attr.ca.subtypes[pos] = subtype;
843 void remove_class_subtype(ir_type *clss, ir_type *subtype)
846 assert(clss && (clss->type_op == type_class));
847 for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
848 if (clss->attr.ca.subtypes[i] == subtype) {
849 for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
850 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
851 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
857 void add_class_supertype(ir_type *clss, ir_type *supertype)
861 assert(clss && (clss->type_op == type_class));
862 assert(supertype && (supertype -> type_op == type_class));
863 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
864 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
865 if (get_class_subtype(supertype, i) == clss)
866 /* Class already registered */
869 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
872 size_t get_class_n_supertypes(const ir_type *clss)
874 assert(clss->type_op == type_class);
875 return ARR_LEN(clss->attr.ca.supertypes);
878 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
880 size_t i, n_supertypes = get_class_n_supertypes(clss);
881 assert(super_clss && (super_clss->type_op == type_class));
882 for (i = 0; i < n_supertypes; i++) {
883 if (get_class_supertype(clss, i) == super_clss)
889 ir_type *get_class_supertype(ir_type *clss, size_t pos)
891 assert(clss->type_op == type_class);
892 assert(pos < get_class_n_supertypes(clss));
893 return clss->attr.ca.supertypes[pos];
896 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
898 assert(clss->type_op == type_class);
899 assert(pos < get_class_n_supertypes(clss));
900 clss->attr.ca.supertypes[pos] = supertype;
903 void remove_class_supertype(ir_type *clss, ir_type *supertype)
906 assert(clss && (clss->type_op == type_class));
907 for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
908 if (clss->attr.ca.supertypes[i] == supertype) {
909 for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
910 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
911 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
917 ir_peculiarity get_class_peculiarity(const ir_type *clss)
919 assert(clss && (clss->type_op == type_class));
920 return clss->attr.ca.peculiarity;
923 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
925 assert(clss && (clss->type_op == type_class));
926 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
927 clss->attr.ca.peculiarity = pec;
930 unsigned (get_class_vtable_size)(const ir_type *clss)
932 return _get_class_vtable_size(clss);
935 void (set_class_vtable_size)(ir_type *clss, unsigned size)
937 _set_class_vtable_size(clss, size);
940 int (is_class_final)(const ir_type *clss)
942 return _is_class_final(clss);
945 void (set_class_final)(ir_type *clss, int flag)
947 _set_class_final(clss, flag);
950 int (is_class_interface)(const ir_type *clss)
952 return _is_class_interface(clss);
955 void (set_class_interface)(ir_type *clss, int flag)
957 _set_class_interface(clss, flag);
960 int (is_class_abstract)(const ir_type *clss)
962 return _is_class_abstract(clss);
965 void (set_class_abstract)(ir_type *clss, int final)
967 _set_class_abstract(clss, final);
970 void set_class_dfn(ir_type *clss, int dfn)
972 clss->attr.ca.dfn = dfn;
975 int get_class_dfn(const ir_type *clss)
977 return (clss->attr.ca.dfn);
980 int (is_Class_type)(const ir_type *clss)
982 return _is_class_type(clss);
985 void set_class_mode(ir_type *tp, ir_mode *mode)
987 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
988 assert(get_type_state(tp) == layout_fixed &&
989 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
993 void set_class_size(ir_type *tp, unsigned size)
999 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1001 ir_type *res = new_type(type_struct, NULL, db);
1004 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1009 ir_type *new_type_struct(ident *name)
1011 return new_d_type_struct (name, NULL);
1014 void free_struct_entities(ir_type *strct)
1017 assert(strct && (strct->type_op == type_struct));
1018 /* we must iterate backward here */
1019 for (i = get_struct_n_members(strct); i > 0;)
1020 free_entity(get_struct_member(strct, --i));
1023 void free_struct_attrs(ir_type *strct)
1025 assert(strct && (strct->type_op == type_struct));
1026 DEL_ARR_F(strct->attr.sa.members);
1029 ident *get_struct_ident(const ir_type *strct)
1031 assert(strct->type_op == type_struct);
1035 const char *get_struct_name(const ir_type *strct)
1037 if (get_struct_ident(strct) == NULL)
1039 return get_id_str(get_struct_ident(strct));
1042 size_t get_struct_n_members(const ir_type *strct)
1044 assert(strct->type_op == type_struct);
1045 return ARR_LEN(strct->attr.sa.members);
1048 static void add_struct_member(ir_type *strct, ir_entity *member)
1050 assert(strct && (strct->type_op == type_struct));
1051 assert(get_type_tpop(get_entity_type(member)) != type_method);
1052 assert(strct != get_entity_type(member) && "recursive type");
1053 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1056 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1058 assert(strct && (strct->type_op == type_struct));
1059 assert(pos < get_struct_n_members(strct));
1060 return strct->attr.sa.members[pos];
1063 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1066 assert(strct && (strct->type_op == type_struct));
1067 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1068 if (get_struct_member(strct, i) == mem)
1074 static void remove_struct_member(ir_type *strct, ir_entity *member)
1077 assert(strct && (strct->type_op == type_struct));
1078 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1079 if (strct->attr.sa.members[i] == member) {
1080 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1081 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1082 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1088 int (is_Struct_type)(const ir_type *strct)
1090 return _is_struct_type(strct);
1093 void set_struct_mode(ir_type *tp, ir_mode *mode)
1095 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1096 assert(get_type_state(tp) == layout_fixed &&
1097 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1101 void set_struct_size(ir_type *tp, unsigned size)
1106 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1110 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1111 res = new_type(type_method, mode_P_code, db);
1112 res->flags |= tf_layout_fixed;
1113 res->size = get_mode_size_bytes(mode_P_code);
1114 res->attr.ma.n_params = n_param;
1115 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1116 res->attr.ma.n_res = n_res;
1117 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1118 res->attr.ma.variadicity = variadicity_non_variadic;
1119 res->attr.ma.properties = mtp_no_property;
1124 ir_type *new_type_method(size_t n_param, size_t n_res)
1126 return new_d_type_method(n_param, n_res, NULL);
1129 ir_type *clone_type_method(ir_type *tp)
1137 assert(is_Method_type(tp));
1140 n_params = tp->attr.ma.n_params;
1141 n_res = tp->attr.ma.n_res;
1144 res = new_type(type_method, mode, db);
1146 res->flags = tp->flags;
1147 res->higher_type = tp->higher_type;
1148 res->size = tp->size;
1149 res->attr.ma.n_params = n_params;
1150 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1151 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1152 res->attr.ma.n_res = n_res;
1153 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1154 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1155 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1156 res->attr.ma.properties = tp->attr.ma.properties;
1157 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1162 void free_method_entities(ir_type *method)
1165 assert(method && (method->type_op == type_method));
1168 void free_method_attrs(ir_type *method)
1170 assert(method && (method->type_op == type_method));
1171 free(method->attr.ma.params);
1172 free(method->attr.ma.res_type);
1175 size_t (get_method_n_params)(const ir_type *method)
1177 return _get_method_n_params(method);
1180 ir_type *get_method_param_type(const ir_type *method, size_t pos)
1183 assert(method->type_op == type_method);
1184 assert(pos < get_method_n_params(method));
1185 res = method->attr.ma.params[pos].tp;
1186 assert(res != NULL && "empty method param type");
1190 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1192 assert(method->type_op == type_method);
1193 assert(pos < get_method_n_params(method));
1194 method->attr.ma.params[pos].tp = tp;
1197 size_t (get_method_n_ress)(const ir_type *method)
1199 return _get_method_n_ress(method);
1202 ir_type *get_method_res_type(const ir_type *method, size_t pos)
1205 assert(method->type_op == type_method);
1206 assert(pos < get_method_n_ress(method));
1207 res = method->attr.ma.res_type[pos].tp;
1208 assert(res != NULL && "empty method return type");
1212 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1214 assert(method->type_op == type_method);
1215 assert(pos < get_method_n_ress(method));
1216 /* set the result ir_type */
1217 method->attr.ma.res_type[pos].tp = tp;
1218 /* If information constructed set pass-by-value representation. */
1221 const char *get_variadicity_name(ir_variadicity vari)
1223 #define X(a) case a: return #a
1225 X(variadicity_non_variadic);
1226 X(variadicity_variadic);
1233 ir_variadicity get_method_variadicity(const ir_type *method)
1235 assert(method && (method->type_op == type_method));
1236 return method->attr.ma.variadicity;
1239 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1241 assert(method && (method->type_op == type_method));
1242 method->attr.ma.variadicity = vari;
1245 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1247 return _get_method_additional_properties(method);
1250 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1252 _set_method_additional_properties(method, mask);
1255 void (add_method_additional_properties)(ir_type *method,
1256 mtp_additional_properties flag)
1258 _add_method_additional_properties(method, flag);
1261 unsigned (get_method_calling_convention)(const ir_type *method)
1263 return _get_method_calling_convention(method);
1266 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1268 _set_method_calling_convention(method, cc_mask);
1271 unsigned get_method_n_regparams(ir_type *method)
1273 unsigned cc = get_method_calling_convention(method);
1274 assert(IS_FASTCALL(cc));
1276 return cc & ~cc_bits;
1279 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1281 unsigned cc = get_method_calling_convention(method);
1282 assert(IS_FASTCALL(cc));
1284 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1287 int (is_Method_type)(const ir_type *method)
1289 return _is_method_type(method);
1293 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1295 ir_type *res = new_type(type_union, NULL, db);
1298 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1303 ir_type *new_type_union(ident *name)
1305 return new_d_type_union(name, NULL);
1308 void free_union_entities(ir_type *uni)
1311 assert(uni && (uni->type_op == type_union));
1312 /* we must iterate backward here */
1313 for (i = get_union_n_members(uni); i > 0;)
1314 free_entity(get_union_member(uni, --i));
1317 void free_union_attrs(ir_type *uni)
1319 assert(uni && (uni->type_op == type_union));
1320 DEL_ARR_F(uni->attr.ua.members);
1323 ident *get_union_ident(const ir_type *uni)
1325 assert(uni->type_op == type_union);
1329 const char *get_union_name(const ir_type *uni)
1331 if (get_union_ident(uni) == NULL)
1333 return get_id_str(get_union_ident(uni));
1336 size_t get_union_n_members(const ir_type *uni)
1338 assert(uni->type_op == type_union);
1339 return ARR_LEN(uni->attr.ua.members);
1342 static void add_union_member(ir_type *uni, ir_entity *member)
1344 assert(uni->type_op == type_union);
1345 assert(uni != get_entity_type(member) && "recursive type");
1346 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1349 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1351 assert(uni->type_op == type_union);
1352 assert(pos < get_union_n_members(uni));
1353 return uni->attr.ua.members[pos];
1356 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1359 assert(uni && (uni->type_op == type_union));
1360 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1361 if (get_union_member(uni, i) == mem)
1367 static void remove_union_member(ir_type *uni, ir_entity *member)
1370 assert(uni && (uni->type_op == type_union));
1371 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1372 if (uni->attr.ua.members[i] == member) {
1373 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1374 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1375 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1381 int (is_Union_type)(const ir_type *uni)
1383 return _is_union_type(uni);
1386 void set_union_size(ir_type *tp, unsigned size)
1392 ir_type *new_type_segment(ident *const name, type_flags const flags)
1394 ir_type *const seg = new_type_class(name);
1395 seg->flags |= tf_segment | flags;
1396 set_class_final(seg, true);
1401 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1407 ir_graph *irg = get_const_code_irg();
1409 assert(!is_Method_type(element_type));
1411 res = new_type(type_array, NULL, db);
1412 res->attr.aa.n_dimensions = n_dimensions;
1413 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1414 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1415 res->attr.aa.order = XMALLOCNZ(size_t, n_dimensions);
1417 unk = new_r_Unknown(irg, mode_Iu);
1418 for (i = 0; i < n_dimensions; i++) {
1419 res->attr.aa.lower_bound[i] =
1420 res->attr.aa.upper_bound[i] = unk;
1421 res->attr.aa.order[i] = i;
1424 ident *const id = new_id_from_chars("elem_ent", 8);
1425 res->attr.aa.element_type = element_type;
1426 res->attr.aa.element_ent = new_entity(res, id, element_type);
1432 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1434 return new_d_type_array(n_dimensions, element_type, NULL);
1437 void free_array_automatic_entities(ir_type *array)
1439 assert(array->type_op == type_array);
1440 free_entity(get_array_element_entity(array));
1443 void free_array_entities(ir_type *array)
1446 assert(array->type_op == type_array);
1449 void free_array_attrs(ir_type *array)
1451 assert(array->type_op == type_array);
1452 free(array->attr.aa.lower_bound);
1453 free(array->attr.aa.upper_bound);
1454 free(array->attr.aa.order);
1457 size_t get_array_n_dimensions(const ir_type *array)
1459 assert(array->type_op == type_array);
1460 return array->attr.aa.n_dimensions;
1463 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1464 ir_node *upper_bound)
1466 assert(array->type_op == type_array);
1467 assert(lower_bound && "lower_bound node may not be NULL.");
1468 assert(upper_bound && "upper_bound node may not be NULL.");
1469 assert(dimension < array->attr.aa.n_dimensions);
1470 array->attr.aa.lower_bound[dimension] = lower_bound;
1471 array->attr.aa.upper_bound[dimension] = upper_bound;
1474 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1477 ir_graph *irg = get_const_code_irg();
1478 set_array_bounds(array, dimension,
1479 new_r_Const_long(irg, mode_Iu, lower_bound),
1480 new_r_Const_long(irg, mode_Iu, upper_bound));
1483 void set_array_lower_bound(ir_type *array, size_t dimension,
1484 ir_node *lower_bound)
1486 assert(array->type_op == type_array);
1487 assert(lower_bound && "lower_bound node may not be NULL.");
1488 array->attr.aa.lower_bound[dimension] = lower_bound;
1491 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1493 ir_graph *irg = get_const_code_irg();
1494 set_array_lower_bound(array, dimension,
1495 new_r_Const_long(irg, mode_Iu, lower_bound));
1498 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1500 assert(array->type_op == type_array);
1501 assert(upper_bound && "upper_bound node may not be NULL.");
1502 array->attr.aa.upper_bound[dimension] = upper_bound;
1505 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1507 ir_graph *irg = get_const_code_irg();
1508 set_array_upper_bound(array, dimension,
1509 new_r_Const_long(irg, mode_Iu, upper_bound));
1512 int has_array_lower_bound(const ir_type *array, size_t dimension)
1514 assert(array->type_op == type_array);
1515 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1518 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1520 assert(array->type_op == type_array);
1521 return array->attr.aa.lower_bound[dimension];
1524 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1527 assert(array->type_op == type_array);
1528 node = array->attr.aa.lower_bound[dimension];
1529 return get_tarval_long(get_Const_tarval(node));
1532 int has_array_upper_bound(const ir_type *array, size_t dimension)
1534 assert(array->type_op == type_array);
1535 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1538 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1540 assert(array->type_op == type_array);
1541 return array->attr.aa.upper_bound[dimension];
1544 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1547 assert(array->type_op == type_array);
1548 node = array->attr.aa.upper_bound[dimension];
1549 return get_tarval_long(get_Const_tarval(node));
1552 void set_array_order(ir_type *array, size_t dimension, size_t order)
1554 assert(array->type_op == type_array);
1555 array->attr.aa.order[dimension] = order;
1558 size_t get_array_order(const ir_type *array, size_t dimension)
1560 assert(array->type_op == type_array);
1561 return array->attr.aa.order[dimension];
1564 size_t find_array_dimension(const ir_type *array, size_t order)
1568 assert(array->type_op == type_array);
1570 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1571 if (array->attr.aa.order[dim] == order)
1577 void set_array_element_type(ir_type *array, ir_type *tp)
1579 assert(array->type_op == type_array);
1580 assert(!is_Method_type(tp));
1581 array->attr.aa.element_type = tp;
1584 ir_type *get_array_element_type(const ir_type *array)
1586 assert(array->type_op == type_array);
1587 return array->attr.aa.element_type;
1590 void set_array_element_entity(ir_type *array, ir_entity *ent)
1592 assert(array->type_op == type_array);
1593 assert((get_entity_type(ent)->type_op != type_method));
1594 array->attr.aa.element_ent = ent;
1595 array->attr.aa.element_type = get_entity_type(ent);
1598 ir_entity *get_array_element_entity(const ir_type *array)
1600 assert(array->type_op == type_array);
1601 return array->attr.aa.element_ent;
1604 int is_array_variable_size(const ir_type *array)
1606 assert(array->type_op == type_array);
1607 return (array->flags & tf_variable_size) != 0;
1610 void set_array_variable_size(ir_type *array, int flag)
1612 assert(array->type_op == type_array);
1613 array->flags = (array->flags & ~tf_variable_size)
1614 | (flag != 0 ? tf_variable_size : 0);
1617 int (is_Array_type)(const ir_type *array)
1619 return _is_array_type(array);
1622 void set_array_size(ir_type *tp, unsigned size)
1624 /* FIXME: Here we should make some checks with the element type size */
1629 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1633 res = new_type(type_enumeration, NULL, db);
1635 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1640 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1642 return new_d_type_enumeration(name, n_enums, NULL);
1645 void free_enumeration_entities(ir_type *enumeration)
1648 assert(enumeration->type_op == type_enumeration);
1651 void free_enumeration_attrs(ir_type *enumeration)
1653 assert(enumeration->type_op == type_enumeration);
1654 DEL_ARR_F(enumeration->attr.ea.enumer);
1657 ident *get_enumeration_ident(const ir_type *enumeration)
1659 assert(enumeration->type_op == type_enumeration);
1660 return enumeration->name;
1663 const char *get_enumeration_name(const ir_type *enumeration)
1665 if (get_enumeration_ident(enumeration) == NULL)
1667 return get_id_str(get_enumeration_ident(enumeration));
1670 size_t get_enumeration_n_enums(const ir_type *enumeration)
1672 assert(enumeration->type_op == type_enumeration);
1673 return ARR_LEN(enumeration->attr.ea.enumer);
1676 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1679 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1680 enumeration->attr.ea.enumer[pos].nameid = nameid;
1681 enumeration->attr.ea.enumer[pos].value = con;
1682 enumeration->attr.ea.enumer[pos].owner = enumeration;
1685 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1687 assert(enumeration->type_op == type_enumeration);
1688 assert(pos < get_enumeration_n_enums(enumeration));
1689 return &enumeration->attr.ea.enumer[pos];
1692 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1694 return enum_cnst->owner;
1697 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1699 enum_cnst->value = con;
1702 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1704 return enum_cnst->value;
1707 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1709 enum_cnst->nameid = id;
1712 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1714 return enum_cnst->nameid;
1717 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1719 return get_id_str(enum_cnst->nameid);
1722 int (is_Enumeration_type)(const ir_type *enumeration)
1724 return _is_enumeration_type(enumeration);
1727 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1729 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1730 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1731 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1733 tp->size = get_mode_size_bytes(mode);
1739 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1744 if (is_Method_type(points_to) || is_code_type(points_to)) {
1750 res = new_type(type_pointer, mode, db);
1751 res->attr.pa.points_to = points_to;
1752 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1753 res->size = get_mode_size_bytes(res->mode);
1754 res->flags |= tf_layout_fixed;
1759 ir_type *new_type_pointer(ir_type *points_to)
1761 return new_d_type_pointer(points_to, NULL);
1764 void free_pointer_entities(ir_type *pointer)
1767 assert(pointer && (pointer->type_op == type_pointer));
1770 void free_pointer_attrs(ir_type *pointer)
1773 assert(pointer && (pointer->type_op == type_pointer));
1776 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1778 assert(pointer && (pointer->type_op == type_pointer));
1779 pointer->attr.pa.points_to = tp;
1782 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1784 assert(pointer && (pointer->type_op == type_pointer));
1785 return pointer->attr.pa.points_to;
1788 int (is_Pointer_type)(const ir_type *pointer)
1790 return _is_pointer_type(pointer);
1793 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1795 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1796 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1797 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1799 tp->size = get_mode_size_bytes(mode);
1803 ir_type *find_pointer_type_to_type(ir_type *tp)
1805 size_t i, n = get_irp_n_types();
1806 for (i = 0; i < n; ++i) {
1807 ir_type *found = get_irp_type(i);
1808 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1811 return get_unknown_type();
1815 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1817 ir_type *res = new_type(type_primitive, mode, db);
1818 res->size = get_mode_size_bytes(mode);
1819 res->flags |= tf_layout_fixed;
1820 res->attr.ba.base_type = NULL;
1825 ir_type *new_type_primitive(ir_mode *mode)
1827 return new_d_type_primitive(mode, NULL);
1830 int (is_Primitive_type)(const ir_type *primitive)
1832 return _is_primitive_type(primitive);
1835 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1837 /* Modes of primitives must be data */
1838 assert(mode_is_data(mode));
1840 /* For primitive size depends on the mode. */
1841 tp->size = get_mode_size_bytes(mode);
1845 ir_type *get_primitive_base_type(const ir_type *tp)
1847 assert(is_Primitive_type(tp));
1848 return tp->attr.ba.base_type;
1851 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
1853 assert(is_Primitive_type(tp));
1854 tp->attr.ba.base_type = base_tp;
1859 int (is_atomic_type)(const ir_type *tp)
1861 return _is_atomic_type(tp);
1864 size_t get_compound_n_members(const ir_type *tp)
1866 const tp_op *op = get_type_tpop(tp);
1867 return op->ops.get_n_members(tp);
1870 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
1872 const tp_op *op = get_type_tpop(tp);
1873 return op->ops.get_member(tp, pos);
1876 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
1878 const tp_op *op = get_type_tpop(tp);
1879 return op->ops.get_member_index(tp, member);
1882 void set_compound_variable_size(ir_type *tp, int variable_size_flag)
1884 assert(is_compound_type(tp));
1885 tp->flags = (tp->flags & ~tf_variable_size)
1886 | (variable_size_flag != 0 ? tf_variable_size : 0);
1889 int is_compound_variable_size(const ir_type *tp)
1891 assert(is_compound_type(tp));
1892 return (tp->flags & tf_variable_size) != 0;
1895 int is_compound_type(const ir_type *tp)
1897 assert(tp->kind == k_type);
1898 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1901 ident *get_compound_ident(const ir_type *tp)
1903 assert(is_compound_type(tp));
1907 const char *get_compound_name(const ir_type *tp)
1909 if (get_compound_ident(tp) == NULL)
1911 return get_id_str(get_compound_ident(tp));
1914 void remove_compound_member(ir_type *compound, ir_entity *entity)
1916 switch (get_type_tpop_code(compound)) {
1917 case tpo_class: remove_class_member(compound, entity); break;
1918 case tpo_struct: remove_struct_member(compound, entity); break;
1919 case tpo_union: remove_union_member(compound, entity); break;
1921 panic("argument for remove_compound_member not a compound type");
1925 void add_compound_member(ir_type *compound, ir_entity *entity)
1927 switch (get_type_tpop_code(compound)) {
1928 case tpo_class: add_class_member(compound, entity); break;
1929 case tpo_struct: add_struct_member(compound, entity); break;
1930 case tpo_union: add_union_member(compound, entity); break;
1932 panic("argument for add_compound_member not a compound type");
1936 int is_code_type(const ir_type *tp)
1938 assert(tp->kind == k_type);
1939 return tp->type_op == tpop_code;
1942 int is_unknown_type(const ir_type *tp)
1944 assert(tp->kind == k_type);
1945 return tp->type_op == tpop_unknown;
1948 int is_none_type(const ir_type *tp)
1950 assert(tp->kind == k_type);
1951 return tp->type_op == tpop_none;
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 /* It is not possible to derive from the frame type. Set the final flag. */
1966 set_class_final(res, 1);
1971 ir_type *clone_frame_type(ir_type *type)
1976 assert(is_frame_type(type));
1977 /* the entity link resource should be allocated if this function is called */
1978 assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
1980 res = new_type_frame();
1981 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
1982 ir_entity *ent = get_class_member(type, i);
1983 ir_entity *nent = copy_entity_own(ent, res);
1984 set_entity_link(ent, nent);
1985 set_entity_link(nent, ent);
1990 void set_default_size(ir_type *tp, unsigned size)
1995 void default_layout_compound_type(ir_type *type)
1998 size_t n = get_compound_n_members(type);
2000 unsigned align_all = 1;
2001 bool var_size = is_compound_variable_size(type);
2003 for (i = 0; i < n; ++i) {
2004 ir_entity *entity = get_compound_member(type, i);
2005 ir_type *entity_type = get_entity_type(entity);
2008 unsigned entity_size;
2010 if (is_Method_type(entity_type))
2013 if (i+1 < n || !var_size) {
2014 assert(get_type_state(entity_type) == layout_fixed);
2015 entity_size = get_type_size_bytes(entity_type);
2020 align = get_type_alignment_bytes(entity_type);
2021 align_all = align > align_all ? align : align_all;
2022 misalign = (align ? size % align : 0);
2023 size += (misalign ? align - misalign : 0);
2025 set_entity_offset(entity, size);
2026 if (!is_Union_type(type)) {
2027 size += entity_size;
2030 if (align_all > 0 && size % align_all) {
2031 size += align_all - (size % align_all);
2033 if (align_all > get_type_alignment_bytes(type)) {
2034 set_type_alignment_bytes(type, align_all);
2036 set_type_size_bytes(type, size);
2037 set_type_state(type, layout_fixed);
2040 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2048 unsigned frame_size = get_type_size_bytes(frame_type);
2049 unsigned frame_align = get_type_alignment_bytes(frame_type);
2050 static unsigned area_cnt = 0;
2052 assert(is_frame_type(frame_type));
2053 assert(get_type_state(frame_type) == layout_fixed);
2054 assert(get_type_alignment_bytes(frame_type) > 0);
2055 set_type_state(frame_type, layout_undefined);
2057 if (irp->byte_type == NULL)
2058 irp->byte_type = new_type_primitive(mode_Bu);
2060 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2061 name = new_id_from_str(buf);
2063 tp = new_type_array(1, irp->byte_type);
2064 set_array_bounds_int(tp, 0, 0, size);
2065 set_type_alignment_bytes(tp, alignment);
2066 set_type_size_bytes(tp, size);
2070 unsigned delta = (size + frame_align - 1) & ~(frame_align - 1);
2071 /* fix all offsets so far */
2072 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2073 ir_entity *ent = get_class_member(frame_type, i);
2075 set_entity_offset(ent, get_entity_offset(ent) + delta);
2077 /* calculate offset and new type size */
2079 frame_size += delta;
2081 /* calculate offset and new type size */
2082 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2083 frame_size = offset + size;
2086 area = new_entity(frame_type, name, tp);
2087 set_entity_offset(area, offset);
2088 set_type_size_bytes(frame_type, frame_size);
2089 if (alignment > frame_align) {
2090 set_type_alignment_bytes(frame_type, alignment);
2093 /* mark this entity as compiler generated */
2094 set_entity_compiler_generated(area, 1);
2096 set_type_state(frame_type, layout_fixed);
2100 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2104 type_dbg_info *tdbgi = get_type_dbg_info(type);
2105 if (tdbgi != NULL) {
2106 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2110 /* we have to construct some name... */
2111 switch (get_type_tpop_code(type)) {
2112 case tpo_uninitialized:
2115 snprintf(buffer, buffer_size, "code");
2119 id = get_class_ident(type);
2120 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2124 id = get_struct_ident(type);
2125 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2129 id = get_union_ident(type);
2130 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2133 case tpo_enumeration:
2134 id = get_enumeration_ident(type);
2135 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2139 snprintf(buffer, buffer_size, "unknown type");
2143 p = snprintf(buffer, buffer_size, "pointer to ");
2146 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2150 p = snprintf(buffer, buffer_size, "array of ");
2153 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2157 id = get_mode_ident(get_type_mode(type));
2158 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2162 snprintf(buffer, buffer_size, "none");
2165 /* TODO: we should print argument and return types here... */
2166 snprintf(buffer, buffer_size, "method type");
2169 snprintf(buffer, buffer_size, "invalid type");