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.
66 ir_type *get_none_type(void)
68 return irp->none_type;
71 ir_type *get_code_type(void)
73 return irp->code_type;
76 ir_type *get_unknown_type(void)
78 return irp->unknown_type;
81 void ir_init_type(ir_prog *irp)
83 /* construct none and unknown type. */
84 irp->none_type = new_type(tpop_none, mode_BAD, NULL);
85 set_type_size_bytes(irp->none_type, 0);
86 set_type_state (irp->none_type, layout_fixed);
88 irp->code_type = new_type(tpop_code, mode_ANY, NULL);
89 set_type_state(irp->code_type, layout_fixed);
91 irp->unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
92 set_type_size_bytes(irp->unknown_type, 0);
93 set_type_state (irp->unknown_type, layout_fixed);
96 void ir_finish_type(ir_prog *irp)
98 /** nothing todo. (The none, code, unknown types are in the global type list
103 ir_visited_t firm_type_visited;
105 void (set_master_type_visited)(ir_visited_t val)
107 _set_master_type_visited(val);
110 ir_visited_t (get_master_type_visited)(void)
112 return _get_master_type_visited();
115 void (inc_master_type_visited)(void)
117 _inc_master_type_visited();
120 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
125 node_size = offsetof(ir_type, attr) + type_op->attr_size;
126 res = (ir_type*)xmalloc(node_size);
127 memset(res, 0, node_size);
130 res->type_op = type_op;
132 res->visibility = ir_visibility_external;
133 res->flags = tf_none;
140 res->nr = get_irp_new_node_nr();
141 #endif /* defined DEBUG_libfirm */
143 add_irp_type(res); /* Remember the new type global. */
148 void free_type(ir_type *tp)
150 const tp_op *op = get_type_tpop(tp);
152 /* Remove from list of all types */
154 /* Free the attributes of the type. */
156 /* Free entities automatically allocated with the ir_type */
157 if (op->ops.free_auto_entities)
158 op->ops.free_auto_entities(tp);
159 /* And now the type itself... */
166 void free_type_entities(ir_type *tp)
168 const tp_op *tpop = get_type_tpop(tp);
170 if (tpop->ops.free_entities)
171 tpop->ops.free_entities(tp);
174 void free_type_attrs(ir_type *tp)
176 const tp_op *tpop = get_type_tpop(tp);
178 if (tpop->ops.free_attrs)
179 tpop->ops.free_attrs(tp);
182 void *(get_type_link)(const ir_type *tp)
184 return _get_type_link(tp);
187 void (set_type_link)(ir_type *tp, void *l)
189 _set_type_link(tp, l);
192 const tp_op *(get_type_tpop)(const ir_type *tp)
194 return _get_type_tpop(tp);
197 ident *(get_type_tpop_nameid)(const ir_type *tp)
199 return _get_type_tpop_nameid(tp);
202 const char* get_type_tpop_name(const ir_type *tp)
204 assert(tp && tp->kind == k_type);
205 return get_id_str(tp->type_op->name);
208 tp_opcode (get_type_tpop_code)(const ir_type *tp)
210 return _get_type_tpop_code(tp);
213 ir_mode *(get_type_mode)(const ir_type *tp)
215 return _get_type_mode(tp);
218 void set_type_mode(ir_type *tp, ir_mode *mode)
220 const tp_op *tpop = get_type_tpop(tp);
222 if (tpop->ops.set_type_mode)
223 tpop->ops.set_type_mode(tp, mode);
225 assert(0 && "setting a mode is NOT allowed for this type");
228 long get_type_nr(const ir_type *tp)
234 return (long)PTR_TO_INT(tp);
238 unsigned (get_type_size_bytes)(const ir_type *tp)
240 return _get_type_size_bytes(tp);
243 ir_visibility get_type_visibility(const ir_type *tp)
246 return tp->visibility;
249 void set_type_visibility(ir_type *tp, ir_visibility v)
255 void set_type_size_bytes(ir_type *tp, unsigned size)
257 const tp_op *tpop = get_type_tpop(tp);
259 if (tpop->ops.set_type_size)
260 tpop->ops.set_type_size(tp, size);
262 assert(0 && "Cannot set size for this type");
265 unsigned get_type_alignment_bytes(ir_type *tp)
272 /* alignment NOT set calculate it "on demand" */
274 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
275 else if (is_Array_type(tp))
276 align = get_type_alignment_bytes(get_array_element_type(tp));
277 else if (is_compound_type(tp)) {
278 size_t i, n = get_compound_n_members(tp);
281 for (i = 0; i < n; ++i) {
282 ir_type *t = get_entity_type(get_compound_member(tp, i));
283 unsigned a = get_type_alignment_bytes(t);
288 } else if (is_Method_type(tp)) {
298 void set_type_alignment_bytes(ir_type *tp, unsigned align)
300 assert(tp && tp->kind == k_type);
301 /* Methods don't have an alignment. */
302 if (tp->type_op != type_method) {
307 const char *get_type_state_name(ir_type_state s)
309 #define X(a) case a: return #a
318 ir_type_state (get_type_state)(const ir_type *tp)
320 return _get_type_state(tp);
323 void set_type_state(ir_type *tp, ir_type_state state)
325 assert(tp && tp->kind == k_type);
327 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
328 (tp->type_op == type_method))
331 /* Just a correctness check: */
332 if (state == layout_fixed) {
334 switch (get_type_tpop_code(tp)) {
336 if (tp != get_glob_type()) {
337 size_t n_mem = get_class_n_members(tp);
338 for (i = 0; i < n_mem; i++) {
339 ir_entity *entity = get_class_member(tp, i);
340 if (is_Method_type(get_entity_type(entity)))
342 assert(get_entity_offset(entity) > -1);
347 for (i = 0; i < get_struct_n_members(tp); i++) {
348 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
355 case tpo_enumeration: {
357 size_t n_enums = get_enumeration_n_enums(tp);
358 assert(get_type_mode(tp) != NULL);
359 for (i = 0; i < n_enums; ++i) {
360 ir_enum_const *ec = get_enumeration_const(tp, i);
361 ir_tarval *tv = get_enumeration_value(ec);
362 assert(tv != NULL && tv != tarval_bad);
370 if (state == layout_fixed)
371 tp->flags |= tf_layout_fixed;
373 tp->flags &= ~tf_layout_fixed;
376 ir_visited_t (get_type_visited)(const ir_type *tp)
378 return _get_type_visited(tp);
381 void (set_type_visited)(ir_type *tp, ir_visited_t num)
383 _set_type_visited(tp, num);
386 void (mark_type_visited)(ir_type *tp)
388 _mark_type_visited(tp);
391 int (type_visited)(const ir_type *tp)
393 return _type_visited(tp);
396 int (type_not_visited)(const ir_type *tp)
398 return _type_not_visited(tp);
401 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
403 return _get_type_dbg_info(tp);
406 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
408 _set_type_dbg_info(tp, db);
411 int (is_type)(const void *thing)
413 return _is_type(thing);
416 int equal_type(ir_type *typ1, ir_type *typ2)
423 if (typ1 == typ2) return 1;
425 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
426 typ1->name != typ2->name ||
427 (get_type_mode(typ1) != get_type_mode(typ2)) ||
428 (get_type_state(typ1) != get_type_state(typ2)))
430 if ((get_type_state(typ1) == layout_fixed) &&
431 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
434 switch (get_type_tpop_code(typ1)) {
436 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
437 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
438 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
439 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
440 /** Compare the members **/
441 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
442 /* First sort the members of typ2 */
443 for (i = 0; i < get_class_n_members(typ1); i++) {
444 ir_entity *e1 = get_class_member(typ1, i);
445 for (j = 0; j < get_class_n_members(typ2); j++) {
446 ir_entity *e2 = get_class_member(typ2, j);
447 if (get_entity_name(e1) == get_entity_name(e2))
451 for (i = 0; i < get_class_n_members(typ1); i++) {
452 if (!m[i] || get_class_member(typ1, i) != m[i])
455 /** Compare the supertypes **/
456 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
457 /* First sort the supertypes of typ2 */
458 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
459 ir_type *t1 = get_class_supertype(typ1, i);
460 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
461 ir_type *t2 = get_class_supertype(typ2, j);
462 if (t2->name == t1->name)
466 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
467 if (!t[i] || /* Found no counterpart */
468 get_class_supertype(typ1, i) != t[i])
474 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
475 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
476 /* First sort the members of lt */
477 for (i = 0; i < get_struct_n_members(typ1); i++) {
478 ir_entity *e1 = get_struct_member(typ1, i);
479 for (j = 0; j < get_struct_n_members(typ2); j++) {
480 ir_entity *e2 = get_struct_member(typ2, j);
481 if (get_entity_name(e1) == get_entity_name(e2))
485 for (i = 0; i < get_struct_n_members(typ1); i++) {
486 if (!m[i] || get_struct_member(typ1, i) != m[i])
495 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
496 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
497 if (get_method_calling_convention(typ1) !=
498 get_method_calling_convention(typ2)) return 0;
500 n_param1 = get_method_n_params(typ1);
501 n_param2 = get_method_n_params(typ2);
503 if (n_param1 != n_param2) return 0;
505 for (i = 0; i < n_param1; i++) {
506 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
509 for (i = 0; i < get_method_n_ress(typ1); i++) {
510 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
516 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
517 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
518 /* First sort the members of lt */
519 for (i = 0; i < get_union_n_members(typ1); i++) {
520 ir_entity *e1 = get_union_member(typ1, i);
521 for (j = 0; j < get_union_n_members(typ2); j++) {
522 ir_entity *e2 = get_union_member(typ2, j);
523 if (get_entity_name(e1) == get_entity_name(e2))
527 for (i = 0; i < get_union_n_members(typ1); i++) {
528 if (!m[i] || get_union_member(typ1, i) != m[i])
534 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
536 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
538 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
539 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
540 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
542 if (get_array_order(typ1, i) != get_array_order(typ2, i))
543 assert(0 && "type compare with different dimension orders not implemented");
547 case tpo_enumeration:
548 assert(0 && "enumerations not implemented");
552 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
564 int smaller_type(ir_type *st, ir_type *lt)
571 if (st == lt) return 1;
573 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
576 switch (get_type_tpop_code(st)) {
578 return is_SubClass_of(st, lt);
581 n_st_members = get_struct_n_members(st);
582 if (n_st_members != get_struct_n_members(lt))
585 m = ALLOCANZ(ir_entity*, n_st_members);
586 /* First sort the members of lt */
587 for (i = 0; i < n_st_members; ++i) {
588 ir_entity *se = get_struct_member(st, i);
589 size_t n = get_struct_n_members(lt);
590 for (j = 0; j < n; ++j) {
591 ir_entity *le = get_struct_member(lt, j);
592 if (get_entity_name(le) == get_entity_name(se))
596 for (i = 0; i < n_st_members; i++) {
597 if (!m[i] || /* Found no counterpart */
598 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
604 size_t n_param1, n_param2;
606 /** FIXME: is this still 1? */
607 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
608 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
609 if (get_method_calling_convention(st) !=
610 get_method_calling_convention(lt)) return 0;
612 n_param1 = get_method_n_params(st);
613 n_param2 = get_method_n_params(lt);
615 if (n_param1 != n_param2) return 0;
617 for (i = 0; i < get_method_n_params(st); i++) {
618 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
621 for (i = 0; i < get_method_n_ress(st); i++) {
622 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
628 n_st_members = get_union_n_members(st);
629 if (n_st_members != get_union_n_members(lt)) return 0;
630 m = ALLOCANZ(ir_entity*, n_st_members);
631 /* First sort the members of lt */
632 for (i = 0; i < n_st_members; ++i) {
633 ir_entity *se = get_union_member(st, i);
634 size_t n = get_union_n_members(lt);
635 for (j = 0; j < n; ++j) {
636 ir_entity *le = get_union_member(lt, j);
637 if (get_entity_name(le) == get_entity_name(se))
641 for (i = 0; i < n_st_members; ++i) {
642 if (!m[i] || /* Found no counterpart */
643 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
649 ir_type *set, *let; /* small/large elt. ir_type */
650 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
652 set = get_array_element_type(st);
653 let = get_array_element_type(lt);
655 /* If the element types are different, set must be convertible
656 to let, and they must have the same size so that address
657 computations work out. To have a size the layout must
659 if ((get_type_state(set) != layout_fixed) ||
660 (get_type_state(let) != layout_fixed))
662 if (!smaller_type(set, let) ||
663 get_type_size_bytes(set) != get_type_size_bytes(let))
666 for (i = 0; i < get_array_n_dimensions(st); i++) {
667 if (get_array_lower_bound(lt, i))
668 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
670 if (get_array_upper_bound(lt, i))
671 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
676 case tpo_enumeration:
677 assert(0 && "enumerations not implemented");
681 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
686 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
696 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
700 res = new_type(type_class, NULL, db);
703 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
704 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
705 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
706 res->attr.ca.peculiarity = peculiarity_existent;
707 res->attr.ca.type_info = NULL;
708 res->attr.ca.vtable_size = 0;
709 res->attr.ca.clss_flags = cf_none;
710 res->attr.ca.dfn = 0;
715 ir_type *new_type_class(ident *name)
717 return new_d_type_class(name, NULL);
720 void free_class_entities(ir_type *clss)
723 assert(clss && (clss->type_op == type_class));
724 /* we must iterate backward here */
725 for (i = get_class_n_members(clss); i > 0;)
726 free_entity(get_class_member(clss, --i));
727 /* do NOT free the type info here. It belongs to another class */
730 void free_class_attrs(ir_type *clss)
732 assert(clss && (clss->type_op == type_class));
733 DEL_ARR_F(clss->attr.ca.members);
734 DEL_ARR_F(clss->attr.ca.subtypes);
735 DEL_ARR_F(clss->attr.ca.supertypes);
738 ident *get_class_ident(const ir_type *clss)
740 assert(clss->type_op == type_class);
744 const char *get_class_name(const ir_type *clss)
746 if (get_class_ident(clss) == NULL)
748 return get_id_str(get_class_ident(clss));
751 static void add_class_member(ir_type *clss, ir_entity *member)
753 assert(clss && (clss->type_op == type_class));
754 assert(clss != get_entity_type(member) && "recursive type");
755 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
758 size_t (get_class_n_members)(const ir_type *clss)
760 return _get_class_n_members(clss);
763 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
766 assert(clss && (clss->type_op == type_class));
767 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
768 if (get_class_member(clss, i) == mem)
771 return INVALID_MEMBER_INDEX;
774 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
776 return _get_class_member(clss, pos);
779 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
782 assert(clss && (clss->type_op == type_class));
783 n_mem = get_class_n_members(clss);
784 for (i = 0; i < n_mem; ++i) {
785 ir_entity *mem = get_class_member(clss, i);
786 if (get_entity_ident(mem) == name)
792 static void remove_class_member(ir_type *clss, ir_entity *member)
795 assert(clss && (clss->type_op == type_class));
796 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
797 if (clss->attr.ca.members[i] == member) {
798 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
799 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
800 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
806 void add_class_subtype(ir_type *clss, ir_type *subtype)
809 assert(clss->type_op == type_class);
810 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
811 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
812 if (get_class_supertype(subtype, i) == clss)
813 /* Class already registered */
816 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
819 size_t get_class_n_subtypes(const ir_type *clss)
821 assert(clss->type_op == type_class);
822 return ARR_LEN (clss->attr.ca.subtypes);
825 ir_type *get_class_subtype(ir_type *clss, size_t pos)
827 assert(clss->type_op == type_class);
828 assert(pos < get_class_n_subtypes(clss));
829 return clss->attr.ca.subtypes[pos];
832 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
834 size_t i, n_subtypes = get_class_n_subtypes(clss);
835 assert(is_Class_type(subclass));
836 for (i = 0; i < n_subtypes; ++i) {
837 if (get_class_subtype(clss, i) == subclass)
843 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
845 assert(clss->type_op == type_class);
846 assert(pos < get_class_n_subtypes(clss));
847 clss->attr.ca.subtypes[pos] = subtype;
850 void remove_class_subtype(ir_type *clss, ir_type *subtype)
853 assert(clss && (clss->type_op == type_class));
854 for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
855 if (clss->attr.ca.subtypes[i] == subtype) {
856 for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
857 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
858 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
864 void add_class_supertype(ir_type *clss, ir_type *supertype)
868 assert(clss && (clss->type_op == type_class));
869 assert(supertype && (supertype -> type_op == type_class));
870 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
871 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
872 if (get_class_subtype(supertype, i) == clss)
873 /* Class already registered */
876 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
879 size_t get_class_n_supertypes(const ir_type *clss)
881 assert(clss->type_op == type_class);
882 return ARR_LEN(clss->attr.ca.supertypes);
885 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
887 size_t i, n_supertypes = get_class_n_supertypes(clss);
888 assert(super_clss && (super_clss->type_op == type_class));
889 for (i = 0; i < n_supertypes; i++) {
890 if (get_class_supertype(clss, i) == super_clss)
896 ir_type *get_class_supertype(ir_type *clss, size_t pos)
898 assert(clss->type_op == type_class);
899 assert(pos < get_class_n_supertypes(clss));
900 return clss->attr.ca.supertypes[pos];
903 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
905 assert(clss->type_op == type_class);
906 assert(pos < get_class_n_supertypes(clss));
907 clss->attr.ca.supertypes[pos] = supertype;
910 void remove_class_supertype(ir_type *clss, ir_type *supertype)
913 assert(clss && (clss->type_op == type_class));
914 for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
915 if (clss->attr.ca.supertypes[i] == supertype) {
916 for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
917 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
918 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
924 ir_entity *get_class_type_info(const ir_type *clss)
926 return clss->attr.ca.type_info;
929 void set_class_type_info(ir_type *clss, ir_entity *ent)
931 clss->attr.ca.type_info = ent;
933 ent->repr_class = clss;
936 ir_peculiarity get_class_peculiarity(const ir_type *clss)
938 assert(clss && (clss->type_op == type_class));
939 return clss->attr.ca.peculiarity;
942 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
944 assert(clss && (clss->type_op == type_class));
945 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
946 clss->attr.ca.peculiarity = pec;
949 unsigned (get_class_vtable_size)(const ir_type *clss)
951 return _get_class_vtable_size(clss);
954 void (set_class_vtable_size)(ir_type *clss, unsigned size)
956 _set_class_vtable_size(clss, size);
959 int (is_class_final)(const ir_type *clss)
961 return _is_class_final(clss);
964 void (set_class_final)(ir_type *clss, int flag)
966 _set_class_final(clss, flag);
969 int (is_class_interface)(const ir_type *clss)
971 return _is_class_interface(clss);
974 void (set_class_interface)(ir_type *clss, int flag)
976 _set_class_interface(clss, flag);
979 int (is_class_abstract)(const ir_type *clss)
981 return _is_class_abstract(clss);
984 void (set_class_abstract)(ir_type *clss, int final)
986 _set_class_abstract(clss, final);
989 void set_class_dfn(ir_type *clss, int dfn)
991 clss->attr.ca.dfn = dfn;
994 int get_class_dfn(const ir_type *clss)
996 return (clss->attr.ca.dfn);
999 int (is_Class_type)(const ir_type *clss)
1001 return _is_class_type(clss);
1004 void set_class_mode(ir_type *tp, ir_mode *mode)
1006 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1007 assert(get_type_state(tp) == layout_fixed &&
1008 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1012 void set_class_size(ir_type *tp, unsigned size)
1018 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1020 ir_type *res = new_type(type_struct, NULL, db);
1023 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1028 ir_type *new_type_struct(ident *name)
1030 return new_d_type_struct (name, NULL);
1033 void free_struct_entities(ir_type *strct)
1036 assert(strct && (strct->type_op == type_struct));
1037 /* we must iterate backward here */
1038 for (i = get_struct_n_members(strct); i > 0;)
1039 free_entity(get_struct_member(strct, --i));
1042 void free_struct_attrs(ir_type *strct)
1044 assert(strct && (strct->type_op == type_struct));
1045 DEL_ARR_F(strct->attr.sa.members);
1048 ident *get_struct_ident(const ir_type *strct)
1050 assert(strct->type_op == type_struct);
1054 const char *get_struct_name(const ir_type *strct)
1056 if (get_struct_ident(strct) == NULL)
1058 return get_id_str(get_struct_ident(strct));
1061 size_t get_struct_n_members(const ir_type *strct)
1063 assert(strct->type_op == type_struct);
1064 return ARR_LEN(strct->attr.sa.members);
1067 static void add_struct_member(ir_type *strct, ir_entity *member)
1069 assert(strct && (strct->type_op == type_struct));
1070 assert(get_type_tpop(get_entity_type(member)) != type_method);
1071 assert(strct != get_entity_type(member) && "recursive type");
1072 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1075 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1077 assert(strct && (strct->type_op == type_struct));
1078 assert(pos < get_struct_n_members(strct));
1079 return strct->attr.sa.members[pos];
1082 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1085 assert(strct && (strct->type_op == type_struct));
1086 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1087 if (get_struct_member(strct, i) == mem)
1093 static void remove_struct_member(ir_type *strct, ir_entity *member)
1096 assert(strct && (strct->type_op == type_struct));
1097 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1098 if (strct->attr.sa.members[i] == member) {
1099 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1100 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1101 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1107 int (is_Struct_type)(const ir_type *strct)
1109 return _is_struct_type(strct);
1112 void set_struct_mode(ir_type *tp, ir_mode *mode)
1114 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1115 assert(get_type_state(tp) == layout_fixed &&
1116 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1120 void set_struct_size(ir_type *tp, unsigned size)
1125 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1129 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1130 res = new_type(type_method, mode_P_code, db);
1131 res->flags |= tf_layout_fixed;
1132 res->size = get_mode_size_bytes(mode_P_code);
1133 res->attr.ma.n_params = n_param;
1134 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1135 res->attr.ma.n_res = n_res;
1136 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1137 res->attr.ma.variadicity = variadicity_non_variadic;
1138 res->attr.ma.additional_properties = mtp_no_property;
1143 ir_type *new_type_method(size_t n_param, size_t n_res)
1145 return new_d_type_method(n_param, n_res, NULL);
1148 ir_type *clone_type_method(ir_type *tp)
1156 assert(is_Method_type(tp));
1159 n_params = tp->attr.ma.n_params;
1160 n_res = tp->attr.ma.n_res;
1163 res = new_type(type_method, mode, db);
1165 res->flags = tp->flags;
1166 res->higher_type = tp->higher_type;
1167 res->size = tp->size;
1168 res->attr.ma.n_params = n_params;
1169 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1170 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1171 res->attr.ma.n_res = n_res;
1172 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1173 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1174 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1175 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1176 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1181 void free_method_entities(ir_type *method)
1184 assert(method && (method->type_op == type_method));
1187 void free_method_attrs(ir_type *method)
1189 assert(method && (method->type_op == type_method));
1190 free(method->attr.ma.params);
1191 free(method->attr.ma.res_type);
1194 size_t (get_method_n_params)(const ir_type *method)
1196 return _get_method_n_params(method);
1199 ir_type *get_method_param_type(const ir_type *method, size_t pos)
1202 assert(method->type_op == type_method);
1203 assert(pos < get_method_n_params(method));
1204 res = method->attr.ma.params[pos].tp;
1205 assert(res != NULL && "empty method param type");
1209 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1211 assert(method->type_op == type_method);
1212 assert(pos < get_method_n_params(method));
1213 method->attr.ma.params[pos].tp = tp;
1216 size_t (get_method_n_ress)(const ir_type *method)
1218 return _get_method_n_ress(method);
1221 ir_type *get_method_res_type(const ir_type *method, size_t pos)
1224 assert(method->type_op == type_method);
1225 assert(pos < get_method_n_ress(method));
1226 res = method->attr.ma.res_type[pos].tp;
1227 assert(res != NULL && "empty method return type");
1231 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1233 assert(method->type_op == type_method);
1234 assert(pos < get_method_n_ress(method));
1235 /* set the result ir_type */
1236 method->attr.ma.res_type[pos].tp = tp;
1237 /* If information constructed set pass-by-value representation. */
1240 const char *get_variadicity_name(ir_variadicity vari)
1242 #define X(a) case a: return #a
1244 X(variadicity_non_variadic);
1245 X(variadicity_variadic);
1252 ir_variadicity get_method_variadicity(const ir_type *method)
1254 assert(method && (method->type_op == type_method));
1255 return method->attr.ma.variadicity;
1258 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1260 assert(method && (method->type_op == type_method));
1261 method->attr.ma.variadicity = vari;
1264 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1266 return _get_method_additional_properties(method);
1269 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1271 _set_method_additional_properties(method, mask);
1274 void (add_method_additional_properties)(ir_type *method,
1275 mtp_additional_properties flag)
1277 _add_method_additional_properties(method, flag);
1280 unsigned (get_method_calling_convention)(const ir_type *method)
1282 return _get_method_calling_convention(method);
1285 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1287 _set_method_calling_convention(method, cc_mask);
1290 unsigned get_method_n_regparams(ir_type *method)
1292 unsigned cc = get_method_calling_convention(method);
1293 assert(IS_FASTCALL(cc));
1295 return cc & ~cc_bits;
1298 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1300 unsigned cc = get_method_calling_convention(method);
1301 assert(IS_FASTCALL(cc));
1303 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1306 int (is_Method_type)(const ir_type *method)
1308 return _is_method_type(method);
1312 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1314 ir_type *res = new_type(type_union, NULL, db);
1317 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1322 ir_type *new_type_union(ident *name)
1324 return new_d_type_union(name, NULL);
1327 void free_union_entities(ir_type *uni)
1330 assert(uni && (uni->type_op == type_union));
1331 /* we must iterate backward here */
1332 for (i = get_union_n_members(uni); i > 0;)
1333 free_entity(get_union_member(uni, --i));
1336 void free_union_attrs(ir_type *uni)
1338 assert(uni && (uni->type_op == type_union));
1339 DEL_ARR_F(uni->attr.ua.members);
1342 ident *get_union_ident(const ir_type *uni)
1344 assert(uni->type_op == type_union);
1348 const char *get_union_name(const ir_type *uni)
1350 if (get_union_ident(uni) == NULL)
1352 return get_id_str(get_union_ident(uni));
1355 size_t get_union_n_members(const ir_type *uni)
1357 assert(uni->type_op == type_union);
1358 return ARR_LEN(uni->attr.ua.members);
1361 static void add_union_member(ir_type *uni, ir_entity *member)
1363 assert(uni->type_op == type_union);
1364 assert(uni != get_entity_type(member) && "recursive type");
1365 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1368 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1370 assert(uni->type_op == type_union);
1371 assert(pos < get_union_n_members(uni));
1372 return uni->attr.ua.members[pos];
1375 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1378 assert(uni && (uni->type_op == type_union));
1379 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1380 if (get_union_member(uni, i) == mem)
1386 static void remove_union_member(ir_type *uni, ir_entity *member)
1389 assert(uni && (uni->type_op == type_union));
1390 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1391 if (uni->attr.ua.members[i] == member) {
1392 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1393 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1394 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1400 int (is_Union_type)(const ir_type *uni)
1402 return _is_union_type(uni);
1405 void set_union_size(ir_type *tp, unsigned size)
1412 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1418 ir_graph *irg = get_const_code_irg();
1420 assert(!is_Method_type(element_type));
1422 res = new_type(type_array, NULL, db);
1423 res->attr.aa.n_dimensions = n_dimensions;
1424 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1425 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1426 res->attr.aa.order = XMALLOCNZ(size_t, n_dimensions);
1428 unk = new_r_Unknown(irg, mode_Iu);
1429 for (i = 0; i < n_dimensions; i++) {
1430 res->attr.aa.lower_bound[i] =
1431 res->attr.aa.upper_bound[i] = unk;
1432 res->attr.aa.order[i] = i;
1435 res->attr.aa.element_type = element_type;
1436 res->attr.aa.element_ent
1437 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1438 res->attr.aa.element_ent->owner = res;
1444 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1446 return new_d_type_array(n_dimensions, element_type, NULL);
1449 void free_array_automatic_entities(ir_type *array)
1451 assert(array && (array->type_op == type_array));
1452 free_entity(get_array_element_entity(array));
1455 void free_array_entities(ir_type *array)
1458 assert(array->type_op == type_array);
1461 void free_array_attrs(ir_type *array)
1463 assert(array->type_op == type_array);
1464 free(array->attr.aa.lower_bound);
1465 free(array->attr.aa.upper_bound);
1466 free(array->attr.aa.order);
1469 size_t get_array_n_dimensions(const ir_type *array)
1471 assert(array->type_op == type_array);
1472 return array->attr.aa.n_dimensions;
1475 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1476 ir_node *upper_bound)
1478 assert(array && (array->type_op == type_array));
1479 assert(lower_bound && "lower_bound node may not be NULL.");
1480 assert(upper_bound && "upper_bound node may not be NULL.");
1481 assert(dimension < array->attr.aa.n_dimensions);
1482 array->attr.aa.lower_bound[dimension] = lower_bound;
1483 array->attr.aa.upper_bound[dimension] = upper_bound;
1486 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1489 ir_graph *irg = get_const_code_irg();
1490 set_array_bounds(array, dimension,
1491 new_r_Const_long(irg, mode_Iu, lower_bound),
1492 new_r_Const_long(irg, mode_Iu, upper_bound));
1495 void set_array_lower_bound(ir_type *array, size_t dimension,
1496 ir_node *lower_bound)
1498 assert(array && (array->type_op == type_array));
1499 assert(lower_bound && "lower_bound node may not be NULL.");
1500 array->attr.aa.lower_bound[dimension] = lower_bound;
1503 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1505 ir_graph *irg = get_const_code_irg();
1506 set_array_lower_bound(array, dimension,
1507 new_r_Const_long(irg, mode_Iu, lower_bound));
1510 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1512 assert(array && (array->type_op == type_array));
1513 assert(upper_bound && "upper_bound node may not be NULL.");
1514 array->attr.aa.upper_bound[dimension] = upper_bound;
1517 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1519 ir_graph *irg = get_const_code_irg();
1520 set_array_upper_bound(array, dimension,
1521 new_r_Const_long(irg, mode_Iu, upper_bound));
1524 int has_array_lower_bound(const ir_type *array, size_t dimension)
1526 assert(array && (array->type_op == type_array));
1527 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1530 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1532 assert(array && (array->type_op == type_array));
1533 return array->attr.aa.lower_bound[dimension];
1536 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1539 assert(array && (array->type_op == type_array));
1540 node = array->attr.aa.lower_bound[dimension];
1541 assert(is_Const(node));
1542 return get_tarval_long(get_Const_tarval(node));
1545 int has_array_upper_bound(const ir_type *array, size_t dimension)
1547 assert(array && (array->type_op == type_array));
1548 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1551 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1553 assert(array && (array->type_op == type_array));
1554 return array->attr.aa.upper_bound[dimension];
1557 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1560 assert(array && (array->type_op == type_array));
1561 node = array->attr.aa.upper_bound[dimension];
1562 assert(is_Const(node));
1563 return get_tarval_long(get_Const_tarval(node));
1566 void set_array_order(ir_type *array, size_t dimension, size_t order)
1568 assert(array && (array->type_op == type_array));
1569 array->attr.aa.order[dimension] = order;
1572 size_t get_array_order(const ir_type *array, size_t dimension)
1574 assert(array && (array->type_op == type_array));
1575 return array->attr.aa.order[dimension];
1578 size_t find_array_dimension(const ir_type *array, size_t order)
1582 assert(array->type_op == type_array);
1584 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1585 if (array->attr.aa.order[dim] == order)
1591 void set_array_element_type(ir_type *array, ir_type *tp)
1593 assert(array && (array->type_op == type_array));
1594 assert(!is_Method_type(tp));
1595 array->attr.aa.element_type = tp;
1598 ir_type *get_array_element_type(const ir_type *array)
1600 assert(array && (array->type_op == type_array));
1601 return array->attr.aa.element_type;
1604 void set_array_element_entity(ir_type *array, ir_entity *ent)
1606 assert(array && (array->type_op == type_array));
1607 assert((get_entity_type(ent)->type_op != type_method));
1608 array->attr.aa.element_ent = ent;
1609 array->attr.aa.element_type = get_entity_type(ent);
1612 ir_entity *get_array_element_entity(const ir_type *array)
1614 assert(array && (array->type_op == type_array));
1615 return array->attr.aa.element_ent;
1618 int (is_Array_type)(const ir_type *array)
1620 return _is_array_type(array);
1623 void set_array_size(ir_type *tp, unsigned size)
1625 /* FIXME: Here we should make some checks with the element type size */
1630 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1634 res = new_type(type_enumeration, NULL, db);
1636 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1641 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1643 return new_d_type_enumeration(name, n_enums, NULL);
1646 void free_enumeration_entities(ir_type *enumeration)
1649 assert(enumeration->type_op == type_enumeration);
1652 void free_enumeration_attrs(ir_type *enumeration)
1654 assert(enumeration->type_op == type_enumeration);
1655 DEL_ARR_F(enumeration->attr.ea.enumer);
1658 ident *get_enumeration_ident(const ir_type *enumeration)
1660 assert(enumeration->type_op == type_enumeration);
1661 return enumeration->name;
1664 const char *get_enumeration_name(const ir_type *enumeration)
1666 if (get_enumeration_ident(enumeration) == NULL)
1668 return get_id_str(get_enumeration_ident(enumeration));
1671 size_t get_enumeration_n_enums(const ir_type *enumeration)
1673 assert(enumeration->type_op == type_enumeration);
1674 return ARR_LEN(enumeration->attr.ea.enumer);
1677 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1680 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1681 enumeration->attr.ea.enumer[pos].nameid = nameid;
1682 enumeration->attr.ea.enumer[pos].value = con;
1683 enumeration->attr.ea.enumer[pos].owner = enumeration;
1686 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1688 assert(enumeration->type_op == type_enumeration);
1689 assert(pos < get_enumeration_n_enums(enumeration));
1690 return &enumeration->attr.ea.enumer[pos];
1693 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1695 return enum_cnst->owner;
1698 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1700 enum_cnst->value = con;
1703 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1705 return enum_cnst->value;
1708 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1710 enum_cnst->nameid = id;
1713 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1715 return enum_cnst->nameid;
1718 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1720 return get_id_str(enum_cnst->nameid);
1723 int (is_Enumeration_type)(const ir_type *enumeration)
1725 return _is_enumeration_type(enumeration);
1728 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1730 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1731 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1732 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1734 tp->size = get_mode_size_bytes(mode);
1740 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1745 if (is_Method_type(points_to) || is_code_type(points_to)) {
1751 res = new_type(type_pointer, mode, db);
1752 res->attr.pa.points_to = points_to;
1753 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1754 res->size = get_mode_size_bytes(res->mode);
1755 res->flags |= tf_layout_fixed;
1760 ir_type *new_type_pointer(ir_type *points_to)
1762 return new_d_type_pointer(points_to, NULL);
1765 void free_pointer_entities(ir_type *pointer)
1768 assert(pointer && (pointer->type_op == type_pointer));
1771 void free_pointer_attrs(ir_type *pointer)
1774 assert(pointer && (pointer->type_op == type_pointer));
1777 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1779 assert(pointer && (pointer->type_op == type_pointer));
1780 pointer->attr.pa.points_to = tp;
1783 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1785 assert(pointer && (pointer->type_op == type_pointer));
1786 return pointer->attr.pa.points_to;
1789 int (is_Pointer_type)(const ir_type *pointer)
1791 return _is_pointer_type(pointer);
1794 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1796 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1797 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1798 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1800 tp->size = get_mode_size_bytes(mode);
1804 ir_type *find_pointer_type_to_type(ir_type *tp)
1806 size_t i, n = get_irp_n_types();
1807 for (i = 0; i < n; ++i) {
1808 ir_type *found = get_irp_type(i);
1809 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1812 return get_unknown_type();
1816 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1818 ir_type *res = new_type(type_primitive, mode, db);
1819 res->size = get_mode_size_bytes(mode);
1820 res->flags |= tf_layout_fixed;
1821 res->attr.ba.base_type = NULL;
1826 ir_type *new_type_primitive(ir_mode *mode)
1828 return new_d_type_primitive(mode, NULL);
1831 int (is_Primitive_type)(const ir_type *primitive)
1833 return _is_primitive_type(primitive);
1836 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1838 /* Modes of primitives must be data */
1839 assert(mode_is_data(mode));
1841 /* For primitive size depends on the mode. */
1842 tp->size = get_mode_size_bytes(mode);
1846 ir_type *get_primitive_base_type(const ir_type *tp)
1848 assert(is_Primitive_type(tp));
1849 return tp->attr.ba.base_type;
1852 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
1854 assert(is_Primitive_type(tp));
1855 tp->attr.ba.base_type = base_tp;
1860 int (is_atomic_type)(const ir_type *tp)
1862 return _is_atomic_type(tp);
1865 size_t get_compound_n_members(const ir_type *tp)
1867 const tp_op *op = get_type_tpop(tp);
1868 return op->ops.get_n_members(tp);
1871 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
1873 const tp_op *op = get_type_tpop(tp);
1874 return op->ops.get_member(tp, pos);
1877 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
1879 const tp_op *op = get_type_tpop(tp);
1880 return op->ops.get_member_index(tp, member);
1883 int is_compound_type(const ir_type *tp)
1885 assert(tp->kind == k_type);
1886 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1889 ident *get_compound_ident(const ir_type *tp)
1891 assert(is_compound_type(tp));
1895 const char *get_compound_name(const ir_type *tp)
1897 if (get_compound_ident(tp) == NULL)
1899 return get_id_str(get_compound_ident(tp));
1902 void remove_compound_member(ir_type *compound, ir_entity *entity)
1904 switch (get_type_tpop_code(compound)) {
1905 case tpo_class: remove_class_member(compound, entity); break;
1906 case tpo_struct: remove_struct_member(compound, entity); break;
1907 case tpo_union: remove_union_member(compound, entity); break;
1909 panic("argument for remove_compound_member not a compound type");
1913 void add_compound_member(ir_type *compound, ir_entity *entity)
1915 switch (get_type_tpop_code(compound)) {
1916 case tpo_class: add_class_member(compound, entity); break;
1917 case tpo_struct: add_struct_member(compound, entity); break;
1918 case tpo_union: add_union_member(compound, entity); break;
1920 panic("argument for add_compound_member not a compound type");
1924 int is_code_type(const ir_type *tp)
1926 assert(tp->kind == k_type);
1927 return tp->type_op == tpop_code;
1930 int is_unknown_type(const ir_type *tp)
1932 assert(tp->kind == k_type);
1933 return tp->type_op == tpop_unknown;
1936 int is_none_type(const ir_type *tp)
1938 assert(tp->kind == k_type);
1939 return tp->type_op == tpop_none;
1942 int is_frame_type(const ir_type *tp)
1944 return tp->flags & tf_frame_type;
1947 ir_type *new_type_frame(void)
1949 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
1951 res->flags |= tf_frame_type;
1953 /* It is not possible to derive from the frame type. Set the final flag. */
1954 set_class_final(res, 1);
1959 ir_type *clone_frame_type(ir_type *type)
1964 assert(is_frame_type(type));
1965 /* the entity link resource should be allocated if this function is called */
1966 assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
1968 res = new_type_frame();
1969 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
1970 ir_entity *ent = get_class_member(type, i);
1971 ir_entity *nent = copy_entity_own(ent, res);
1972 set_entity_link(ent, nent);
1973 set_entity_link(nent, ent);
1978 void set_default_size(ir_type *tp, unsigned size)
1983 void default_layout_compound_type(ir_type *type)
1986 size_t n = get_compound_n_members(type);
1988 unsigned align_all = 1;
1990 for (i = 0; i < n; ++i) {
1991 ir_entity *entity = get_compound_member(type, i);
1992 ir_type *entity_type = get_entity_type(entity);
1996 if (is_Method_type(entity_type))
1999 assert(get_type_state(entity_type) == layout_fixed);
2000 align = get_type_alignment_bytes(entity_type);
2001 align_all = align > align_all ? align : align_all;
2002 misalign = (align ? size % align : 0);
2003 size += (misalign ? align - misalign : 0);
2005 set_entity_offset(entity, size);
2006 if (!is_Union_type(type)) {
2007 size += get_type_size_bytes(entity_type);
2010 if (align_all > 0 && size % align_all) {
2011 size += align_all - (size % align_all);
2013 if (align_all > get_type_alignment_bytes(type)) {
2014 set_type_alignment_bytes(type, align_all);
2016 set_type_size_bytes(type, size);
2017 set_type_state(type, layout_fixed);
2020 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2027 int offset, frame_size;
2028 static unsigned area_cnt = 0;
2029 static ir_type *a_byte = NULL;
2031 assert(is_frame_type(frame_type));
2032 assert(get_type_state(frame_type) == layout_fixed);
2033 assert(get_type_alignment_bytes(frame_type) > 0);
2034 set_type_state(frame_type, layout_undefined);
2037 a_byte = new_type_primitive(mode_Bu);
2039 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2040 name = new_id_from_str(buf);
2042 tp = new_type_array(1, a_byte);
2043 set_array_bounds_int(tp, 0, 0, size);
2044 set_type_alignment_bytes(tp, alignment);
2045 set_type_size_bytes(tp, size);
2047 frame_size = get_type_size_bytes(frame_type);
2050 unsigned frame_align = get_type_alignment_bytes(frame_type);
2051 unsigned delta = (size + frame_align - 1) & ~(frame_align - 1);
2052 /* fix all offsets so far */
2053 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2054 ir_entity *ent = get_class_member(frame_type, i);
2056 set_entity_offset(ent, get_entity_offset(ent) + delta);
2058 /* calculate offset and new type size */
2060 frame_size += delta;
2062 /* increase size to match alignment... */
2063 if (alignment > frame_align) {
2064 frame_align = alignment;
2065 set_type_alignment_bytes(frame_type, frame_align);
2066 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2069 /* calculate offset and new type size */
2070 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2071 frame_size = offset + size;
2074 area = new_entity(frame_type, name, tp);
2075 set_entity_offset(area, offset);
2076 set_type_size_bytes(frame_type, frame_size);
2078 /* mark this entity as compiler generated */
2079 set_entity_compiler_generated(area, 1);
2081 set_type_state(frame_type, layout_fixed);
2085 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2089 type_dbg_info *tdbgi = get_type_dbg_info(type);
2090 if (tdbgi != NULL) {
2091 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2095 /* we have to construct some name... */
2096 switch (get_type_tpop_code(type)) {
2097 case tpo_uninitialized:
2100 snprintf(buffer, buffer_size, "code");
2104 id = get_class_ident(type);
2105 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2109 id = get_struct_ident(type);
2110 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2114 id = get_union_ident(type);
2115 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2118 case tpo_enumeration:
2119 id = get_enumeration_ident(type);
2120 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2124 snprintf(buffer, buffer_size, "unknown type");
2128 p = snprintf(buffer, buffer_size, "pointer to ");
2131 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2135 p = snprintf(buffer, buffer_size, "array of ");
2138 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2142 id = get_mode_ident(get_type_mode(type));
2143 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2147 snprintf(buffer, buffer_size, "none");
2150 /* TODO: we should print argument and return types here... */
2151 snprintf(buffer, buffer_size, "method type");
2154 snprintf(buffer, buffer_size, "invalid type");