2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of types.
23 * @author Goetz Lindenmaier, Michael Beck
27 * Implementation of the datastructure to hold
30 * This module supplies a datastructure to represent all types
31 * known in the compiled program. This includes types specified
32 * in the program as well as types defined by the language. In the
33 * view of the intermediate representation there is no difference
34 * between these types.
36 * There exist several kinds of types, arranged by the structure of
37 * the type. A type is described by a set of attributes. Some of
38 * these attributes are common to all types, others depend on the
41 * Types are different from the modes defined in irmode: Types are
42 * on the level of the programming language, modes at the level of
43 * the target processor.
66 ir_type *firm_none_type;
67 ir_type *get_none_type(void)
69 return firm_none_type;
72 ir_type *firm_code_type;
73 ir_type *get_code_type(void)
75 return firm_code_type;
78 ir_type *firm_unknown_type;
79 ir_type *get_unknown_type(void)
81 return firm_unknown_type;
84 /* Suffixes added to types used for pass-by-value representations. */
85 static ident *value_params_suffix = NULL;
86 static ident *value_ress_suffix = NULL;
88 /** The default calling convention for method types. */
89 static unsigned default_cc_mask;
91 unsigned get_default_cc_mask(void)
93 return default_cc_mask;
96 void firm_init_type(unsigned def_cc_mask)
98 default_cc_mask = def_cc_mask;
99 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
100 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
102 /* construct none and unknown type. */
103 firm_none_type = new_type(tpop_none, mode_BAD, NULL);
104 set_type_size_bytes(firm_none_type, 0);
105 set_type_state (firm_none_type, layout_fixed);
106 remove_irp_type(firm_none_type);
108 firm_code_type = new_type(tpop_code, mode_ANY, NULL);
109 set_type_state(firm_code_type, layout_fixed);
110 remove_irp_type(firm_code_type);
112 firm_unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
113 set_type_size_bytes(firm_unknown_type, 0);
114 set_type_state (firm_unknown_type, layout_fixed);
115 remove_irp_type(firm_unknown_type);
118 /** the global type visited flag */
119 ir_visited_t firm_type_visited;
121 void (set_master_type_visited)(ir_visited_t val)
123 _set_master_type_visited(val);
126 ir_visited_t (get_master_type_visited)(void)
128 return _get_master_type_visited();
131 void (inc_master_type_visited)(void)
133 _inc_master_type_visited();
136 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
141 node_size = offsetof(ir_type, attr) + type_op->attr_size;
142 res = xmalloc(node_size);
143 memset(res, 0, node_size);
146 res->type_op = type_op;
148 res->visibility = ir_visibility_external;
149 res->flags = tf_none;
155 res->assoc_type = NULL;
157 res->nr = get_irp_new_node_nr();
158 #endif /* defined DEBUG_libfirm */
160 add_irp_type(res); /* Remember the new type global. */
165 void free_type(ir_type *tp)
167 const tp_op *op = get_type_tpop(tp);
169 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
170 || (get_type_tpop(tp) == tpop_code))
172 /* Remove from list of all types */
174 /* Free the attributes of the type. */
176 /* Free entities automatically allocated with the ir_type */
177 if (op->ops.free_auto_entities)
178 op->ops.free_auto_entities(tp);
179 /* And now the type itself... */
184 void free_type_entities(ir_type *tp)
186 const tp_op *tpop = get_type_tpop(tp);
188 if (tpop->ops.free_entities)
189 tpop->ops.free_entities(tp);
192 void free_type_attrs(ir_type *tp)
194 const tp_op *tpop = get_type_tpop(tp);
196 if (tpop->ops.free_attrs)
197 tpop->ops.free_attrs(tp);
200 void *(get_type_link)(const ir_type *tp)
202 return _get_type_link(tp);
205 void (set_type_link)(ir_type *tp, void *l)
207 _set_type_link(tp, l);
210 const tp_op *(get_type_tpop)(const ir_type *tp)
212 return _get_type_tpop(tp);
215 ident *(get_type_tpop_nameid)(const ir_type *tp)
217 return _get_type_tpop_nameid(tp);
220 const char* get_type_tpop_name(const ir_type *tp)
222 assert(tp && tp->kind == k_type);
223 return get_id_str(tp->type_op->name);
226 tp_opcode (get_type_tpop_code)(const ir_type *tp)
228 return _get_type_tpop_code(tp);
231 ir_mode *(get_type_mode)(const ir_type *tp)
233 return _get_type_mode(tp);
236 void set_type_mode(ir_type *tp, ir_mode *mode)
238 const tp_op *tpop = get_type_tpop(tp);
240 if (tpop->ops.set_type_mode)
241 tpop->ops.set_type_mode(tp, mode);
243 assert(0 && "setting a mode is NOT allowed for this type");
246 /* Outputs a unique number for this node */
247 long get_type_nr(const ir_type *tp)
253 return (long)PTR_TO_INT(tp);
257 unsigned (get_type_size_bytes)(const ir_type *tp)
259 return _get_type_size_bytes(tp);
262 ir_visibility get_type_visibility(const ir_type *tp)
265 return tp->visibility;
268 void set_type_visibility(ir_type *tp, ir_visibility v)
274 void set_type_size_bytes(ir_type *tp, unsigned size)
276 const tp_op *tpop = get_type_tpop(tp);
278 if (tpop->ops.set_type_size)
279 tpop->ops.set_type_size(tp, size);
281 assert(0 && "Cannot set size for this type");
284 unsigned get_type_alignment_bytes(ir_type *tp)
291 /* alignment NOT set calculate it "on demand" */
293 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
294 else if (is_Array_type(tp))
295 align = get_type_alignment_bytes(get_array_element_type(tp));
296 else if (is_compound_type(tp)) {
297 int i, n = get_compound_n_members(tp);
300 for (i = 0; i < n; ++i) {
301 ir_type *t = get_entity_type(get_compound_member(tp, i));
302 unsigned a = get_type_alignment_bytes(t);
307 } else if (is_Method_type(tp)) {
317 void set_type_alignment_bytes(ir_type *tp, unsigned align)
319 assert(tp && tp->kind == k_type);
320 /* Methods don't have an alignment. */
321 if (tp->type_op != type_method) {
326 const char *get_type_state_name(ir_type_state s)
328 #define X(a) case a: return #a;
337 ir_type_state (get_type_state)(const ir_type *tp)
339 return _get_type_state(tp);
342 void set_type_state(ir_type *tp, ir_type_state state)
344 assert(tp && tp->kind == k_type);
346 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
347 (tp->type_op == type_method))
350 /* Just a correctness check: */
351 if (state == layout_fixed) {
353 switch (get_type_tpop_code(tp)) {
355 if (tp != get_glob_type()) {
356 int n_mem = get_class_n_members(tp);
357 for (i = 0; i < n_mem; i++) {
358 assert(get_entity_offset(get_class_member(tp, i)) > -1);
360 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
361 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
367 for (i = 0; i < get_struct_n_members(tp); i++) {
368 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
377 Assure that only innermost dimension is dynamic? */
379 case tpo_enumeration:
381 assert(get_type_mode(tp) != NULL);
382 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
383 ir_enum_const *ec = get_enumeration_const(tp, i);
384 tarval *tv = get_enumeration_value(ec);
385 assert(tv != NULL && tv != tarval_bad);
392 if (state == layout_fixed)
393 tp->flags |= tf_layout_fixed;
395 tp->flags &= ~tf_layout_fixed;
398 ir_visited_t (get_type_visited)(const ir_type *tp)
400 return _get_type_visited(tp);
403 void (set_type_visited)(ir_type *tp, ir_visited_t num)
405 _set_type_visited(tp, num);
408 void (mark_type_visited)(ir_type *tp)
410 _mark_type_visited(tp);
413 int (type_visited)(const ir_type *tp)
415 return _type_visited(tp);
418 int (type_not_visited)(const ir_type *tp)
420 return _type_not_visited(tp);
423 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
425 return _get_type_dbg_info(tp);
428 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
430 _set_type_dbg_info(tp, db);
433 int (is_type)(const void *thing)
435 return _is_type(thing);
438 /* Checks whether two types are structural equal.*/
439 int equal_type(ir_type *typ1, ir_type *typ2)
445 if (typ1 == typ2) return 1;
447 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
448 typ1->name != typ2->name ||
449 (get_type_mode(typ1) != get_type_mode(typ2)) ||
450 (get_type_state(typ1) != get_type_state(typ2)))
452 if ((get_type_state(typ1) == layout_fixed) &&
453 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
456 switch (get_type_tpop_code(typ1)) {
458 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
459 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
460 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
461 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
462 /** Compare the members **/
463 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
464 /* First sort the members of typ2 */
465 for (i = 0; i < get_class_n_members(typ1); i++) {
466 ir_entity *e1 = get_class_member(typ1, i);
467 for (j = 0; j < get_class_n_members(typ2); j++) {
468 ir_entity *e2 = get_class_member(typ2, j);
469 if (get_entity_name(e1) == get_entity_name(e2))
473 for (i = 0; i < get_class_n_members(typ1); i++) {
474 if (!m[i] || get_class_member(typ1, i) != m[i])
477 /** Compare the supertypes **/
478 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
479 /* First sort the supertypes of typ2 */
480 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
481 ir_type *t1 = get_class_supertype(typ1, i);
482 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
483 ir_type *t2 = get_class_supertype(typ2, j);
484 if (t2->name == t1->name)
488 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
489 if (!t[i] || /* Found no counterpart */
490 get_class_supertype(typ1, i) != t[i])
496 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
497 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
498 /* First sort the members of lt */
499 for (i = 0; i < get_struct_n_members(typ1); i++) {
500 ir_entity *e1 = get_struct_member(typ1, i);
501 for (j = 0; j < get_struct_n_members(typ2); j++) {
502 ir_entity *e2 = get_struct_member(typ2, j);
503 if (get_entity_name(e1) == get_entity_name(e2))
507 for (i = 0; i < get_struct_n_members(typ1); i++) {
508 if (!m[i] || get_struct_member(typ1, i) != m[i])
514 int n_param1, n_param2;
516 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
517 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
518 if (get_method_calling_convention(typ1) !=
519 get_method_calling_convention(typ2)) return 0;
521 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
522 n_param1 = get_method_n_params(typ1);
523 n_param2 = get_method_n_params(typ2);
525 n_param1 = get_method_first_variadic_param_index(typ1);
526 n_param2 = get_method_first_variadic_param_index(typ2);
529 if (n_param1 != n_param2) return 0;
531 for (i = 0; i < n_param1; i++) {
532 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
535 for (i = 0; i < get_method_n_ress(typ1); i++) {
536 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
542 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
543 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
544 /* First sort the members of lt */
545 for (i = 0; i < get_union_n_members(typ1); i++) {
546 ir_entity *e1 = get_union_member(typ1, i);
547 for (j = 0; j < get_union_n_members(typ2); j++) {
548 ir_entity *e2 = get_union_member(typ2, j);
549 if (get_entity_name(e1) == get_entity_name(e2))
553 for (i = 0; i < get_union_n_members(typ1); i++) {
554 if (!m[i] || get_union_member(typ1, i) != m[i])
560 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
562 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
564 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
565 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
566 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
568 if (get_array_order(typ1, i) != get_array_order(typ2, i))
569 assert(0 && "type compare with different dimension orders not implemented");
573 case tpo_enumeration:
574 assert(0 && "enumerations not implemented");
578 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
590 int smaller_type(ir_type *st, ir_type *lt)
593 int i, j, n_st_members;
595 if (st == lt) return 1;
597 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
600 switch (get_type_tpop_code(st)) {
602 return is_SubClass_of(st, lt);
605 n_st_members = get_struct_n_members(st);
606 if (n_st_members != get_struct_n_members(lt))
609 m = ALLOCANZ(ir_entity*, n_st_members);
610 /* First sort the members of lt */
611 for (i = 0; i < n_st_members; ++i) {
612 ir_entity *se = get_struct_member(st, i);
613 int n = get_struct_n_members(lt);
614 for (j = 0; j < n; ++j) {
615 ir_entity *le = get_struct_member(lt, j);
616 if (get_entity_name(le) == get_entity_name(se))
620 for (i = 0; i < n_st_members; i++) {
621 if (!m[i] || /* Found no counterpart */
622 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
628 int n_param1, n_param2;
630 /** FIXME: is this still 1? */
631 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
632 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
633 if (get_method_calling_convention(st) !=
634 get_method_calling_convention(lt)) return 0;
636 if (get_method_variadicity(st) == variadicity_non_variadic) {
637 n_param1 = get_method_n_params(st);
638 n_param2 = get_method_n_params(lt);
640 n_param1 = get_method_first_variadic_param_index(st);
641 n_param2 = get_method_first_variadic_param_index(lt);
644 if (n_param1 != n_param2) return 0;
646 for (i = 0; i < get_method_n_params(st); i++) {
647 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
650 for (i = 0; i < get_method_n_ress(st); i++) {
651 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
657 n_st_members = get_union_n_members(st);
658 if (n_st_members != get_union_n_members(lt)) return 0;
659 m = ALLOCANZ(ir_entity*, n_st_members);
660 /* First sort the members of lt */
661 for (i = 0; i < n_st_members; ++i) {
662 ir_entity *se = get_union_member(st, i);
663 int n = get_union_n_members(lt);
664 for (j = 0; j < n; ++j) {
665 ir_entity *le = get_union_member(lt, j);
666 if (get_entity_name(le) == get_entity_name(se))
670 for (i = 0; i < n_st_members; ++i) {
671 if (!m[i] || /* Found no counterpart */
672 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
678 ir_type *set, *let; /* small/large elt. ir_type */
679 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
681 set = get_array_element_type(st);
682 let = get_array_element_type(lt);
684 /* If the element types are different, set must be convertible
685 to let, and they must have the same size so that address
686 computations work out. To have a size the layout must
688 if ((get_type_state(set) != layout_fixed) ||
689 (get_type_state(let) != layout_fixed))
691 if (!smaller_type(set, let) ||
692 get_type_size_bytes(set) != get_type_size_bytes(let))
695 for (i = 0; i < get_array_n_dimensions(st); i++) {
696 if (get_array_lower_bound(lt, i))
697 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
699 if (get_array_upper_bound(lt, i))
700 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
705 case tpo_enumeration:
706 assert(0 && "enumerations not implemented");
710 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
715 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
725 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
729 res = new_type(type_class, NULL, db);
732 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
733 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
734 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
735 res->attr.ca.peculiarity = peculiarity_existent;
736 res->attr.ca.type_info = NULL;
737 res->attr.ca.vtable_size = 0;
738 res->attr.ca.clss_flags = cf_none;
739 res->attr.ca.dfn = 0;
744 ir_type *new_type_class(ident *name)
746 return new_d_type_class (name, NULL);
749 void free_class_entities(ir_type *clss)
752 assert(clss && (clss->type_op == type_class));
753 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
754 free_entity(get_class_member(clss, i));
755 /* do NOT free the type info here. It belongs to another class */
758 void free_class_attrs(ir_type *clss)
760 assert(clss && (clss->type_op == type_class));
761 DEL_ARR_F(clss->attr.ca.members);
762 DEL_ARR_F(clss->attr.ca.subtypes);
763 DEL_ARR_F(clss->attr.ca.supertypes);
766 ident *get_class_ident(const ir_type *clss)
768 assert(clss->type_op == type_class);
772 const char *get_class_name(const ir_type *clss)
774 if (get_class_ident(clss) == NULL)
776 return get_id_str(get_class_ident(clss));
779 void add_class_member(ir_type *clss, ir_entity *member)
781 assert(clss && (clss->type_op == type_class));
782 assert(clss != get_entity_type(member) && "recursive type");
783 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
786 int (get_class_n_members)(const ir_type *clss)
788 return _get_class_n_members(clss);
791 int get_class_member_index(const ir_type *clss, ir_entity *mem)
794 assert(clss && (clss->type_op == type_class));
795 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
796 if (get_class_member(clss, i) == mem)
801 ir_entity *(get_class_member)(const ir_type *clss, int pos)
803 return _get_class_member(clss, pos);
806 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
809 assert(clss && (clss->type_op == type_class));
810 n_mem = get_class_n_members(clss);
811 for (i = 0; i < n_mem; ++i) {
812 ir_entity *mem = get_class_member(clss, i);
813 if (get_entity_ident(mem) == name) return mem;
818 void set_class_member(ir_type *clss, ir_entity *member, int pos)
820 assert(clss && (clss->type_op == type_class));
821 assert(pos >= 0 && pos < get_class_n_members(clss));
822 clss->attr.ca.members[pos] = member;
825 void set_class_members(ir_type *clss, ir_entity **members, int arity)
828 assert(clss && (clss->type_op == type_class));
829 DEL_ARR_F(clss->attr.ca.members);
830 clss->attr.ca.members = NEW_ARR_F(ir_entity *, 0);
831 for (i = 0; i < arity; ++i) {
832 set_entity_owner(members[i], clss);
833 ARR_APP1(ir_entity *, clss->attr.ca.members, members[i]);
837 void remove_class_member(ir_type *clss, ir_entity *member)
840 assert(clss && (clss->type_op == type_class));
841 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
842 if (clss->attr.ca.members[i] == member) {
843 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
844 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
845 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
851 void add_class_subtype(ir_type *clss, ir_type *subtype)
854 assert(clss && (clss->type_op == type_class));
855 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
856 for (i = 0; i < get_class_n_supertypes(subtype); i++)
857 if (get_class_supertype(subtype, i) == clss)
858 /* Class already registered */
860 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
863 int get_class_n_subtypes(const ir_type *clss)
865 assert(clss && (clss->type_op == type_class));
866 return (ARR_LEN (clss->attr.ca.subtypes));
869 ir_type *get_class_subtype(ir_type *clss, int pos)
871 assert(clss && (clss->type_op == type_class));
872 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
873 return clss->attr.ca.subtypes[pos];
876 int get_class_subtype_index(ir_type *clss, const ir_type *subclass)
878 int i, n_subtypes = get_class_n_subtypes(clss);
879 assert(is_Class_type(subclass));
880 for (i = 0; i < n_subtypes; ++i) {
881 if (get_class_subtype(clss, i) == subclass) return i;
886 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos)
888 assert(clss && (clss->type_op == type_class));
889 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
890 clss->attr.ca.subtypes[pos] = subtype;
893 void remove_class_subtype(ir_type *clss, ir_type *subtype)
896 assert(clss && (clss->type_op == type_class));
897 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
898 if (clss->attr.ca.subtypes[i] == subtype) {
899 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
900 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
901 ARR_SETLEN(ir_entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
906 void add_class_supertype(ir_type *clss, ir_type *supertype)
909 assert(clss && (clss->type_op == type_class));
910 assert(supertype && (supertype -> type_op == type_class));
911 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
912 for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
913 if (get_class_subtype(supertype, i) == clss)
914 /* Class already registered */
916 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
919 int get_class_n_supertypes(const ir_type *clss)
921 assert(clss && (clss->type_op == type_class));
922 return ARR_LEN(clss->attr.ca.supertypes);
925 int get_class_supertype_index(ir_type *clss, ir_type *super_clss)
927 int i, n_supertypes = get_class_n_supertypes(clss);
928 assert(super_clss && (super_clss->type_op == type_class));
929 for (i = 0; i < n_supertypes; i++)
930 if (get_class_supertype(clss, i) == super_clss)
935 ir_type *get_class_supertype(ir_type *clss, int pos)
937 assert(clss && (clss->type_op == type_class));
938 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
939 return clss->attr.ca.supertypes[pos];
942 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos)
944 assert(clss && (clss->type_op == type_class));
945 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
946 clss->attr.ca.supertypes[pos] = supertype;
949 void remove_class_supertype(ir_type *clss, ir_type *supertype)
952 assert(clss && (clss->type_op == type_class));
953 for (i = 0; i < (ARR_LEN(clss->attr.ca.supertypes)); i++)
954 if (clss->attr.ca.supertypes[i] == supertype) {
955 for (; i < (ARR_LEN(clss->attr.ca.supertypes))-1; i++)
956 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
957 ARR_SETLEN(ir_entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
962 ir_entity *get_class_type_info(const ir_type *clss)
964 return clss->attr.ca.type_info;
967 void set_class_type_info(ir_type *clss, ir_entity *ent)
969 clss->attr.ca.type_info = ent;
971 ent->repr_class = clss;
974 ir_peculiarity get_class_peculiarity(const ir_type *clss)
976 assert(clss && (clss->type_op == type_class));
977 return clss->attr.ca.peculiarity;
980 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
982 assert(clss && (clss->type_op == type_class));
983 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
984 clss->attr.ca.peculiarity = pec;
987 unsigned (get_class_vtable_size)(const ir_type *clss)
989 return _get_class_vtable_size(clss);
992 void (set_class_vtable_size)(ir_type *clss, unsigned size)
994 _set_class_vtable_size(clss, size);
997 int (is_class_final)(const ir_type *clss)
999 return _is_class_final(clss);
1002 void (set_class_final)(ir_type *clss, int flag)
1004 _set_class_final(clss, flag);
1007 int (is_class_interface)(const ir_type *clss)
1009 return _is_class_interface(clss);
1012 void (set_class_interface)(ir_type *clss, int flag)
1014 _set_class_interface(clss, flag);
1017 int (is_class_abstract)(const ir_type *clss)
1019 return _is_class_abstract(clss);
1022 void (set_class_abstract)(ir_type *clss, int final)
1024 _set_class_abstract(clss, final);
1027 void set_class_dfn(ir_type *clss, int dfn)
1029 clss->attr.ca.dfn = dfn;
1032 int get_class_dfn(const ir_type *clss)
1034 return (clss->attr.ca.dfn);
1037 int (is_Class_type)(const ir_type *clss)
1039 return _is_class_type(clss);
1042 void set_class_mode(ir_type *tp, ir_mode *mode)
1044 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1045 assert(get_type_state(tp) == layout_fixed &&
1046 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1050 void set_class_size(ir_type *tp, unsigned size)
1056 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1058 ir_type *res = new_type(type_struct, NULL, db);
1061 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1066 ir_type *new_type_struct(ident *name)
1068 return new_d_type_struct (name, NULL);
1071 void free_struct_entities(ir_type *strct)
1074 assert(strct && (strct->type_op == type_struct));
1075 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1076 free_entity(get_struct_member(strct, i));
1079 void free_struct_attrs(ir_type *strct)
1081 assert(strct && (strct->type_op == type_struct));
1082 DEL_ARR_F(strct->attr.sa.members);
1085 ident *get_struct_ident(const ir_type *strct)
1087 assert(strct->type_op == type_struct);
1091 const char *get_struct_name(const ir_type *strct)
1093 if (get_struct_ident(strct) == NULL)
1095 return get_id_str(get_struct_ident(strct));
1098 int get_struct_n_members(const ir_type *strct)
1100 assert(strct && (strct->type_op == type_struct));
1101 return ARR_LEN(strct->attr.sa.members);
1104 void add_struct_member(ir_type *strct, ir_entity *member)
1106 assert(strct && (strct->type_op == type_struct));
1107 assert(get_type_tpop(get_entity_type(member)) != type_method);
1108 assert(strct != get_entity_type(member) && "recursive type");
1109 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1112 ir_entity *get_struct_member(const ir_type *strct, int pos)
1114 assert(strct && (strct->type_op == type_struct));
1115 assert(pos >= 0 && pos < get_struct_n_members(strct));
1116 return strct->attr.sa.members[pos];
1119 int get_struct_member_index(const ir_type *strct, ir_entity *mem)
1122 assert(strct && (strct->type_op == type_struct));
1123 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1124 if (get_struct_member(strct, i) == mem)
1129 void set_struct_member(ir_type *strct, int pos, ir_entity *member)
1131 assert(strct && (strct->type_op == type_struct));
1132 assert(pos >= 0 && pos < get_struct_n_members(strct));
1133 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1134 strct->attr.sa.members[pos] = member;
1137 void remove_struct_member(ir_type *strct, ir_entity *member)
1140 assert(strct && (strct->type_op == type_struct));
1141 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1142 if (strct->attr.sa.members[i] == member) {
1143 for (; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1144 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1145 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1150 int (is_Struct_type)(const ir_type *strct)
1152 return _is_struct_type(strct);
1155 void set_struct_mode(ir_type *tp, ir_mode *mode)
1157 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1158 assert(get_type_state(tp) == layout_fixed &&
1159 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1163 void set_struct_size(ir_type *tp, unsigned size)
1170 * Lazy construction of value argument / result representation.
1171 * Constructs a struct type and its member. The types of the members
1172 * are passed in the argument list.
1174 * @param name name of the type constructed
1175 * @param len number of fields
1176 * @param tps array of field types with length len
1178 static ir_type *build_value_type(char const* name, int len, tp_ent_pair *tps)
1181 ir_type *res = new_type_struct(new_id_from_str(name));
1182 res->flags |= tf_value_param_type;
1183 /* Remove type from type list. Must be treated differently than other types. */
1184 remove_irp_type(res);
1185 for (i = 0; i < len; i++) {
1186 ident *id = tps[i].param_name;
1188 /* use res as default if corresponding type is not yet set. */
1189 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1191 /* use the parameter name if specified */
1193 id = new_id_from_str("elt");
1195 tps[i].ent = new_entity(res, id, elt_type);
1196 set_entity_allocation(tps[i].ent, allocation_parameter);
1201 ir_type *new_d_type_method(int n_param, int n_res, type_dbg_info *db)
1205 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1206 res = new_type(type_method, mode_P_code, db);
1207 res->flags |= tf_layout_fixed;
1208 res->size = get_mode_size_bytes(mode_P_code);
1209 res->attr.ma.n_params = n_param;
1210 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1211 res->attr.ma.value_params = NULL;
1212 res->attr.ma.n_res = n_res;
1213 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1214 res->attr.ma.value_ress = NULL;
1215 res->attr.ma.variadicity = variadicity_non_variadic;
1216 res->attr.ma.first_variadic_param = -1;
1217 res->attr.ma.additional_properties = mtp_no_property;
1218 res->attr.ma.irg_calling_conv = default_cc_mask;
1223 ir_type *new_type_method(int n_param, int n_res)
1225 return new_d_type_method(n_param, n_res, NULL);
1228 ir_type *clone_type_method(ir_type *tp)
1232 int n_params, n_res;
1235 assert(is_Method_type(tp));
1238 n_params = tp->attr.ma.n_params;
1239 n_res = tp->attr.ma.n_res;
1242 res = new_type(type_method, mode, db);
1244 res->flags = tp->flags;
1245 res->assoc_type = tp->assoc_type;
1246 res->size = tp->size;
1247 res->attr.ma.n_params = n_params;
1248 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1249 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1250 res->attr.ma.value_params = tp->attr.ma.value_params;
1251 res->attr.ma.n_res = n_res;
1252 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1253 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1254 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1255 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1256 res->attr.ma.first_variadic_param = tp->attr.ma.first_variadic_param;
1257 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1258 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1263 void free_method_entities(ir_type *method)
1266 assert(method && (method->type_op == type_method));
1269 void free_method_attrs(ir_type *method)
1271 assert(method && (method->type_op == type_method));
1272 free(method->attr.ma.params);
1273 free(method->attr.ma.res_type);
1274 /* cannot free it yet, type could be cloned ...
1275 if (method->attr.ma.value_params) {
1276 free_type_entities(method->attr.ma.value_params);
1277 free_type(method->attr.ma.value_params);
1280 if (method->attr.ma.value_ress) {
1281 free_type_entities(method->attr.ma.value_ress);
1282 free_type(method->attr.ma.value_ress);
1286 int (get_method_n_params)(const ir_type *method)
1288 return _get_method_n_params(method);
1291 ir_type *get_method_param_type(ir_type *method, int pos)
1294 assert(method && (method->type_op == type_method));
1295 assert(pos >= 0 && pos < get_method_n_params(method));
1296 res = method->attr.ma.params[pos].tp;
1297 assert(res != NULL && "empty method param type");
1301 void set_method_param_type(ir_type *method, int pos, ir_type *tp)
1303 assert(method && (method->type_op == type_method));
1304 assert(pos >= 0 && pos < get_method_n_params(method));
1305 method->attr.ma.params[pos].tp = tp;
1306 /* If information constructed set pass-by-value representation. */
1307 if (method->attr.ma.value_params) {
1308 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1309 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1313 ident *get_method_param_ident(ir_type *method, int pos)
1315 assert(method && (method->type_op == type_method));
1316 assert(pos >= 0 && pos < get_method_n_params(method));
1317 return method->attr.ma.params[pos].param_name;
1320 const char *get_method_param_name(ir_type *method, int pos)
1322 ident *id = get_method_param_ident(method, pos);
1323 return id ? get_id_str(id) : NULL;
1326 void set_method_param_ident(ir_type *method, int pos, ident *id)
1328 assert(method && (method->type_op == type_method));
1329 assert(pos >= 0 && pos < get_method_n_params(method));
1330 method->attr.ma.params[pos].param_name = id;
1333 ir_entity *get_method_value_param_ent(ir_type *method, int pos)
1335 assert(method && (method->type_op == type_method));
1336 assert(pos >= 0 && pos < get_method_n_params(method));
1338 if (!method->attr.ma.value_params) {
1339 /* parameter value type not created yet, build */
1340 method->attr.ma.value_params = build_value_type("<value param>",
1341 get_method_n_params(method), method->attr.ma.params);
1344 * build_value_type() sets the method->attr.ma.value_params type as default if
1347 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1348 && "param type not yet set");
1349 return method->attr.ma.params[pos].ent;
1352 void set_method_value_param_type(ir_type *method, ir_type *tp)
1356 assert(method && (method->type_op == type_method));
1357 assert(is_value_param_type(tp));
1358 assert(get_method_n_params(method) == get_struct_n_members(tp));
1360 method->attr.ma.value_params = tp;
1362 n = get_struct_n_members(tp);
1363 for (i = 0; i < n; i++) {
1364 ir_entity *ent = get_struct_member(tp, i);
1365 method->attr.ma.params[i].ent = ent;
1369 ir_type *get_method_value_param_type(const ir_type *method)
1371 assert(method && (method->type_op == type_method));
1372 return method->attr.ma.value_params;
1375 int (get_method_n_ress)(const ir_type *method)
1377 return _get_method_n_ress(method);
1380 ir_type *get_method_res_type(ir_type *method, int pos)
1383 assert(method && (method->type_op == type_method));
1384 assert(pos >= 0 && pos < get_method_n_ress(method));
1385 res = method->attr.ma.res_type[pos].tp;
1386 assert(res != NULL && "empty method return type");
1390 void set_method_res_type(ir_type *method, int pos, ir_type *tp)
1392 assert(method && (method->type_op == type_method));
1393 assert(pos >= 0 && pos < get_method_n_ress(method));
1394 /* set the result ir_type */
1395 method->attr.ma.res_type[pos].tp = tp;
1396 /* If information constructed set pass-by-value representation. */
1397 if (method->attr.ma.value_ress) {
1398 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1399 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1403 ir_entity *get_method_value_res_ent(ir_type *method, int pos)
1405 assert(method && (method->type_op == type_method));
1406 assert(pos >= 0 && pos < get_method_n_ress(method));
1408 if (!method->attr.ma.value_ress) {
1409 /* result value type not created yet, build */
1410 method->attr.ma.value_ress = build_value_type("<value result>",
1411 get_method_n_ress(method), method->attr.ma.res_type);
1414 * build_value_type() sets the method->attr.ma.value_ress type as default if
1417 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1418 && "result type not yet set");
1420 return method->attr.ma.res_type[pos].ent;
1423 ir_type *get_method_value_res_type(const ir_type *method)
1425 assert(method && (method->type_op == type_method));
1426 return method->attr.ma.value_ress;
1429 const char *get_variadicity_name(ir_variadicity vari)
1431 #define X(a) case a: return #a
1433 X(variadicity_non_variadic);
1434 X(variadicity_variadic);
1441 ir_variadicity get_method_variadicity(const ir_type *method)
1443 assert(method && (method->type_op == type_method));
1444 return method->attr.ma.variadicity;
1447 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1449 assert(method && (method->type_op == type_method));
1450 method->attr.ma.variadicity = vari;
1453 int get_method_first_variadic_param_index(const ir_type *method)
1455 assert(method && (method->type_op == type_method));
1457 if (method->attr.ma.variadicity == variadicity_non_variadic)
1460 if (method->attr.ma.first_variadic_param == -1)
1461 return get_method_n_params(method);
1462 return method->attr.ma.first_variadic_param;
1465 void set_method_first_variadic_param_index(ir_type *method, int index)
1467 assert(method && (method->type_op == type_method));
1468 assert(index >= 0 && index <= get_method_n_params(method));
1470 method->attr.ma.first_variadic_param = index;
1473 unsigned (get_method_additional_properties)(const ir_type *method)
1475 return _get_method_additional_properties(method);
1478 void (set_method_additional_properties)(ir_type *method, unsigned mask)
1480 _set_method_additional_properties(method, mask);
1483 void (set_method_additional_property)(ir_type *method,
1484 mtp_additional_property flag)
1486 _set_method_additional_property(method, flag);
1489 unsigned (get_method_calling_convention)(const ir_type *method)
1491 return _get_method_calling_convention(method);
1494 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1496 _set_method_calling_convention(method, cc_mask);
1499 unsigned get_method_n_regparams(ir_type *method)
1501 unsigned cc = get_method_calling_convention(method);
1502 assert(IS_FASTCALL(cc));
1504 return cc & ~cc_bits;
1507 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1509 unsigned cc = get_method_calling_convention(method);
1510 assert(IS_FASTCALL(cc));
1512 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1515 int (is_Method_type)(const ir_type *method)
1517 return _is_method_type(method);
1521 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1523 ir_type *res = new_type(type_union, NULL, db);
1526 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1531 ir_type *new_type_union(ident *name)
1533 return new_d_type_union(name, NULL);
1536 void free_union_entities(ir_type *uni)
1539 assert(uni && (uni->type_op == type_union));
1540 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1541 free_entity(get_union_member(uni, i));
1544 void free_union_attrs (ir_type *uni)
1546 assert(uni && (uni->type_op == type_union));
1547 DEL_ARR_F(uni->attr.ua.members);
1550 ident *get_union_ident(const ir_type *uni)
1552 assert(uni->type_op == type_union);
1556 const char *get_union_name(const ir_type *uni)
1558 if (get_union_ident(uni) == NULL)
1560 return get_id_str(get_union_ident(uni));
1563 int get_union_n_members(const ir_type *uni)
1565 assert(uni && (uni->type_op == type_union));
1566 return ARR_LEN(uni->attr.ua.members);
1569 void add_union_member(ir_type *uni, ir_entity *member)
1571 assert(uni && (uni->type_op == type_union));
1572 assert(uni != get_entity_type(member) && "recursive type");
1573 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1576 ir_entity *get_union_member(const ir_type *uni, int pos)
1578 assert(uni && (uni->type_op == type_union));
1579 assert(pos >= 0 && pos < get_union_n_members(uni));
1580 return uni->attr.ua.members[pos];
1583 int get_union_member_index(const ir_type *uni, ir_entity *mem)
1586 assert(uni && (uni->type_op == type_union));
1587 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1588 if (get_union_member(uni, i) == mem)
1594 void set_union_member(ir_type *uni, int pos, ir_entity *member)
1596 assert(uni && (uni->type_op == type_union));
1597 assert(pos >= 0 && pos < get_union_n_members(uni));
1598 uni->attr.ua.members[pos] = member;
1601 void remove_union_member(ir_type *uni, ir_entity *member)
1604 assert(uni && (uni->type_op == type_union));
1605 for (i = 0; i < (ARR_LEN(uni->attr.ua.members)); i++) {
1606 if (uni->attr.ua.members[i] == member) {
1607 for (; i < (ARR_LEN(uni->attr.ua.members))-1; i++)
1608 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1609 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1615 int (is_Union_type)(const ir_type *uni)
1617 return _is_union_type(uni);
1620 void set_union_size(ir_type *tp, unsigned size)
1627 ir_type *new_d_type_array(int n_dimensions, ir_type *element_type,
1633 ir_graph *rem = current_ir_graph;
1635 assert(!is_Method_type(element_type));
1637 res = new_type(type_array, NULL, db);
1638 res->attr.aa.n_dimensions = n_dimensions;
1639 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1640 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1641 res->attr.aa.order = XMALLOCNZ(int, n_dimensions);
1643 current_ir_graph = get_const_code_irg();
1644 unk = new_Unknown(mode_Iu);
1645 for (i = 0; i < n_dimensions; i++) {
1646 res->attr.aa.lower_bound[i] =
1647 res->attr.aa.upper_bound[i] = unk;
1648 res->attr.aa.order[i] = i;
1650 current_ir_graph = rem;
1652 res->attr.aa.element_type = element_type;
1653 new_entity(res, new_id_from_chars("elem_ent", 8), element_type);
1658 ir_type *new_type_array(int n_dimensions, ir_type *element_type)
1660 return new_d_type_array(n_dimensions, element_type, NULL);
1663 void free_array_automatic_entities(ir_type *array)
1665 assert(array && (array->type_op == type_array));
1666 free_entity(get_array_element_entity(array));
1669 void free_array_entities (ir_type *array)
1672 assert(array && (array->type_op == type_array));
1675 void free_array_attrs (ir_type *array)
1677 assert(array && (array->type_op == type_array));
1678 free(array->attr.aa.lower_bound);
1679 free(array->attr.aa.upper_bound);
1680 free(array->attr.aa.order);
1683 /* manipulate private fields of array ir_type */
1684 int get_array_n_dimensions (const ir_type *array)
1686 assert(array && (array->type_op == type_array));
1687 return array->attr.aa.n_dimensions;
1690 void set_array_bounds(ir_type *array, int dimension, ir_node *lower_bound,
1691 ir_node *upper_bound)
1693 assert(array && (array->type_op == type_array));
1694 assert(lower_bound && "lower_bound node may not be NULL.");
1695 assert(upper_bound && "upper_bound node may not be NULL.");
1696 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1697 array->attr.aa.lower_bound[dimension] = lower_bound;
1698 array->attr.aa.upper_bound[dimension] = upper_bound;
1701 void set_array_bounds_int(ir_type *array, int dimension, int lower_bound,
1704 ir_graph *rem = current_ir_graph;
1705 current_ir_graph = get_const_code_irg();
1706 set_array_bounds(array, dimension,
1707 new_Const_long(mode_Iu, lower_bound),
1708 new_Const_long(mode_Iu, upper_bound));
1709 current_ir_graph = rem;
1712 void set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound)
1714 assert(array && (array->type_op == type_array));
1715 assert(lower_bound && "lower_bound node may not be NULL.");
1716 array->attr.aa.lower_bound[dimension] = lower_bound;
1719 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound)
1721 ir_graph *rem = current_ir_graph;
1722 current_ir_graph = get_const_code_irg();
1723 set_array_lower_bound(array, dimension,
1724 new_Const_long(mode_Iu, lower_bound));
1725 current_ir_graph = rem;
1728 void set_array_upper_bound(ir_type *array, int dimension, ir_node *upper_bound)
1730 assert(array && (array->type_op == type_array));
1731 assert(upper_bound && "upper_bound node may not be NULL.");
1732 array->attr.aa.upper_bound[dimension] = upper_bound;
1735 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound)
1737 ir_graph *rem = current_ir_graph;
1738 current_ir_graph = get_const_code_irg();
1739 set_array_upper_bound(array, dimension,
1740 new_Const_long(mode_Iu, upper_bound));
1741 current_ir_graph = rem;
1744 int has_array_lower_bound(const ir_type *array, int dimension)
1746 assert(array && (array->type_op == type_array));
1747 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1750 ir_node *get_array_lower_bound(const ir_type *array, int dimension)
1752 assert(array && (array->type_op == type_array));
1753 return array->attr.aa.lower_bound[dimension];
1756 long get_array_lower_bound_int(const ir_type *array, int dimension)
1759 assert(array && (array->type_op == type_array));
1760 node = array->attr.aa.lower_bound[dimension];
1761 assert(is_Const(node));
1762 return get_tarval_long(get_Const_tarval(node));
1765 int has_array_upper_bound(const ir_type *array, int dimension)
1767 assert(array && (array->type_op == type_array));
1768 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1771 ir_node *get_array_upper_bound(const ir_type *array, int dimension)
1773 assert(array && (array->type_op == type_array));
1774 return array->attr.aa.upper_bound[dimension];
1777 long get_array_upper_bound_int(const ir_type *array, int dimension)
1780 assert(array && (array->type_op == type_array));
1781 node = array->attr.aa.upper_bound[dimension];
1782 assert(is_Const(node));
1783 return get_tarval_long(get_Const_tarval(node));
1786 void set_array_order(ir_type *array, int dimension, int order)
1788 assert(array && (array->type_op == type_array));
1789 array->attr.aa.order[dimension] = order;
1792 int get_array_order(const ir_type *array, int dimension)
1794 assert(array && (array->type_op == type_array));
1795 return array->attr.aa.order[dimension];
1798 int find_array_dimension(const ir_type *array, int order)
1802 assert(array && (array->type_op == type_array));
1804 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1805 if (array->attr.aa.order[dim] == order)
1811 void set_array_element_type(ir_type *array, ir_type *tp)
1813 assert(array && (array->type_op == type_array));
1814 assert(!is_Method_type(tp));
1815 array->attr.aa.element_type = tp;
1818 ir_type *get_array_element_type(const ir_type *array)
1820 assert(array && (array->type_op == type_array));
1821 return array->attr.aa.element_type;
1824 void set_array_element_entity(ir_type *array, ir_entity *ent)
1826 assert(array && (array->type_op == type_array));
1827 assert((get_entity_type(ent)->type_op != type_method));
1828 array->attr.aa.element_ent = ent;
1829 array->attr.aa.element_type = get_entity_type(ent);
1832 ir_entity *get_array_element_entity(const ir_type *array)
1834 assert(array && (array->type_op == type_array));
1835 return array->attr.aa.element_ent;
1838 int (is_Array_type)(const ir_type *array)
1840 return _is_array_type(array);
1843 void set_array_size(ir_type *tp, unsigned size)
1845 /* FIXME: Here we should make some checks with the element type size */
1850 ir_type *new_d_type_enumeration(ident *name, int n_enums, type_dbg_info *db)
1854 assert(n_enums >= 0);
1855 res = new_type(type_enumeration, NULL, db);
1857 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1862 ir_type *new_type_enumeration(ident *name, int n_enums)
1864 return new_d_type_enumeration(name, n_enums, NULL);
1867 void free_enumeration_entities(ir_type *enumeration)
1870 assert(enumeration && (enumeration->type_op == type_enumeration));
1873 void free_enumeration_attrs(ir_type *enumeration)
1875 assert(enumeration && (enumeration->type_op == type_enumeration));
1876 DEL_ARR_F(enumeration->attr.ea.enumer);
1879 ident *get_enumeration_ident(const ir_type *enumeration)
1881 assert(enumeration->type_op == type_enumeration);
1882 return enumeration->name;
1885 const char *get_enumeration_name(const ir_type *enumeration)
1887 if (get_enumeration_ident(enumeration) == NULL)
1889 return get_id_str(get_enumeration_ident(enumeration));
1892 int get_enumeration_n_enums(const ir_type *enumeration)
1894 assert(enumeration && (enumeration->type_op == type_enumeration));
1895 return ARR_LEN(enumeration->attr.ea.enumer);
1898 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid,
1901 assert(0 <= pos && pos < ARR_LEN(enumeration->attr.ea.enumer));
1902 enumeration->attr.ea.enumer[pos].nameid = nameid;
1903 enumeration->attr.ea.enumer[pos].value = con;
1904 enumeration->attr.ea.enumer[pos].owner = enumeration;
1907 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos)
1909 assert(enumeration && (enumeration->type_op == type_enumeration));
1910 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1911 return &enumeration->attr.ea.enumer[pos];
1914 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1916 return enum_cnst->owner;
1919 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con)
1921 enum_cnst->value = con;
1924 tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1926 return enum_cnst->value;
1929 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1931 enum_cnst->nameid = id;
1934 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1936 return enum_cnst->nameid;
1939 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1941 return get_id_str(enum_cnst->nameid);
1944 int (is_Enumeration_type)(const ir_type *enumeration)
1946 return _is_enumeration_type(enumeration);
1949 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1951 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1952 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1953 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1955 tp->size = get_mode_size_bytes(mode);
1961 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1966 if (is_Method_type(points_to) || is_code_type(points_to)) {
1972 res = new_type(type_pointer, mode, db);
1973 res->attr.pa.points_to = points_to;
1974 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1975 res->size = get_mode_size_bytes(res->mode);
1976 res->flags |= tf_layout_fixed;
1981 ir_type *new_type_pointer(ir_type *points_to)
1983 return new_d_type_pointer(points_to, NULL);
1986 void free_pointer_entities(ir_type *pointer)
1989 assert(pointer && (pointer->type_op == type_pointer));
1992 void free_pointer_attrs(ir_type *pointer)
1995 assert(pointer && (pointer->type_op == type_pointer));
1998 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
2000 assert(pointer && (pointer->type_op == type_pointer));
2001 pointer->attr.pa.points_to = tp;
2004 ir_type *get_pointer_points_to_type(const ir_type *pointer)
2006 assert(pointer && (pointer->type_op == type_pointer));
2007 return pointer->attr.pa.points_to;
2010 int (is_Pointer_type)(const ir_type *pointer)
2012 return _is_pointer_type(pointer);
2015 void set_pointer_mode(ir_type *tp, ir_mode *mode)
2017 assert(mode_is_reference(mode) && "Modes of pointers must be references");
2018 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
2019 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
2021 tp->size = get_mode_size_bytes(mode);
2025 ir_type *find_pointer_type_to_type(ir_type *tp)
2027 int i, n = get_irp_n_types();
2028 for (i = 0; i < n; ++i) {
2029 ir_type *found = get_irp_type(i);
2030 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
2033 return firm_unknown_type;
2038 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
2040 ir_type *res = new_type(type_primitive, mode, db);
2041 res->size = get_mode_size_bytes(mode);
2042 res->flags |= tf_layout_fixed;
2043 res->attr.ba.base_type = NULL;
2048 ir_type *new_type_primitive(ir_mode *mode)
2050 return new_d_type_primitive(mode, NULL);
2053 int (is_Primitive_type)(const ir_type *primitive)
2055 return _is_primitive_type(primitive);
2058 void set_primitive_mode(ir_type *tp, ir_mode *mode)
2060 /* Modes of primitives must be data */
2061 assert(mode_is_data(mode));
2063 /* For primitive size depends on the mode. */
2064 tp->size = get_mode_size_bytes(mode);
2068 ir_type *get_primitive_base_type(const ir_type *tp)
2070 assert(is_Primitive_type(tp));
2071 return tp->attr.ba.base_type;
2074 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
2076 assert(is_Primitive_type(tp));
2077 tp->attr.ba.base_type = base_tp;
2082 int (is_atomic_type)(const ir_type *tp)
2084 return _is_atomic_type(tp);
2087 int get_compound_n_members(const ir_type *tp)
2089 const tp_op *op = get_type_tpop(tp);
2092 if (op->ops.get_n_members)
2093 res = op->ops.get_n_members(tp);
2095 assert(0 && "no member count for this type");
2100 ir_entity *get_compound_member(const ir_type *tp, int pos)
2102 const tp_op *op = get_type_tpop(tp);
2103 ir_entity *res = NULL;
2105 if (op->ops.get_member)
2106 res = op->ops.get_member(tp, pos);
2108 assert(0 && "no members in this type");
2113 int get_compound_member_index(const ir_type *tp, ir_entity *member)
2115 const tp_op *op = get_type_tpop(tp);
2118 if (op->ops.get_member_index)
2119 index = op->ops.get_member_index(tp, member);
2121 assert(0 && "no members in this type");
2126 int is_compound_type(const ir_type *tp)
2128 assert(tp && tp->kind == k_type);
2129 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2132 ident *get_compound_ident(const ir_type *tp)
2134 assert(is_compound_type(tp));
2138 const char *get_compound_name(const ir_type *tp)
2140 if (get_compound_ident(tp) == NULL)
2142 return get_id_str(get_compound_ident(tp));
2145 int is_code_type(const ir_type *tp)
2147 assert(tp && tp->kind == k_type);
2148 return tp->type_op == tpop_code;
2151 int is_frame_type(const ir_type *tp)
2153 return tp->flags & tf_frame_type;
2156 int is_value_param_type(const ir_type *tp)
2158 return tp->flags & tf_value_param_type;
2161 int is_lowered_type(const ir_type *tp)
2163 return tp->flags & tf_lowered_type;
2166 ir_type *new_type_value(void)
2168 ir_type *res = new_type_struct(new_id_from_str("<value_type>"));
2170 res->flags |= tf_value_param_type;
2172 /* Remove type from type list. Must be treated differently than other types. */
2173 remove_irp_type(res);
2178 ir_type *new_type_frame(void)
2180 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
2182 res->flags |= tf_frame_type;
2184 /* Remove type from type list. Must be treated differently than other types. */
2185 remove_irp_type(res);
2187 /* It is not possible to derive from the frame type. Set the final flag. */
2188 set_class_final(res, 1);
2193 ir_type *clone_frame_type(ir_type *type)
2198 assert(is_frame_type(type));
2199 /* the entity link resource should be allocated if this function is called */
2200 assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2202 res = new_type_frame();
2203 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2204 ir_entity *ent = get_class_member(type, i);
2205 ir_entity *nent = copy_entity_own(ent, res);
2206 set_entity_link(ent, nent);
2207 set_entity_link(nent, ent);
2212 void set_lowered_type(ir_type *tp, ir_type *lowered_type)
2214 assert(is_type(tp) && is_type(lowered_type));
2215 lowered_type->flags |= tf_lowered_type;
2216 tp->assoc_type = lowered_type;
2217 lowered_type->assoc_type = tp;
2220 ir_type *get_associated_type(const ir_type *tp)
2222 return tp->assoc_type;
2225 void set_default_size(ir_type *tp, unsigned size)
2230 void default_layout_compound_type(ir_type *type)
2233 int n = get_compound_n_members(type);
2235 unsigned align_all = 1;
2237 for (i = 0; i < n; ++i) {
2238 ir_entity *entity = get_compound_member(type, i);
2239 ir_type *entity_type = get_entity_type(entity);
2243 if (is_Method_type(entity_type))
2246 assert(get_type_state(entity_type) == layout_fixed);
2247 align = get_type_alignment_bytes(entity_type);
2248 align_all = align > align_all ? align : align_all;
2249 misalign = (align ? size % align : 0);
2250 size += (misalign ? align - misalign : 0);
2252 set_entity_offset(entity, size);
2253 if (!is_Union_type(type)) {
2254 size += get_type_size_bytes(entity_type);
2257 if (align_all > 0 && size % align_all) {
2258 size += align_all - (size % align_all);
2260 if (align_all > get_type_alignment_bytes(type)) {
2261 set_type_alignment_bytes(type, align_all);
2263 set_type_size_bytes(type, size);
2264 set_type_state(type, layout_fixed);
2267 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2274 unsigned frame_align;
2275 int i, offset, frame_size;
2276 static unsigned area_cnt = 0;
2277 static ir_type *a_byte = NULL;
2279 assert(is_frame_type(frame_type));
2280 assert(get_type_state(frame_type) == layout_fixed);
2281 assert(get_type_alignment_bytes(frame_type) > 0);
2282 set_type_state(frame_type, layout_undefined);
2285 a_byte = new_type_primitive(mode_Bu);
2287 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2288 name = new_id_from_str(buf);
2290 /* align the size */
2291 frame_align = get_type_alignment_bytes(frame_type);
2292 size = (size + frame_align - 1) & ~(frame_align - 1);
2294 tp = new_type_array(1, a_byte);
2295 set_array_bounds_int(tp, 0, 0, size);
2296 set_type_alignment_bytes(tp, alignment);
2298 frame_size = get_type_size_bytes(frame_type);
2300 /* fix all offsets so far */
2301 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2302 ir_entity *ent = get_class_member(frame_type, i);
2304 set_entity_offset(ent, get_entity_offset(ent) + size);
2306 /* calculate offset and new type size */
2310 /* increase size to match alignment... */
2311 if (alignment > frame_align) {
2312 frame_align = alignment;
2313 set_type_alignment_bytes(frame_type, frame_align);
2314 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2317 /* calculate offset and new type size */
2318 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2319 frame_size = offset + size;
2322 area = new_entity(frame_type, name, tp);
2323 set_entity_offset(area, offset);
2324 set_type_size_bytes(frame_type, frame_size);
2326 /* mark this entity as compiler generated */
2327 set_entity_compiler_generated(area, 1);
2329 set_type_state(frame_type, layout_fixed);
2333 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2337 type_dbg_info *tdbgi = get_type_dbg_info(type);
2338 if (tdbgi != NULL) {
2339 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2343 /* we have to construct some name... */
2344 switch (get_type_tpop_code(type)) {
2345 case tpo_uninitialized:
2348 snprintf(buffer, buffer_size, "code");
2352 id = get_class_ident(type);
2353 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2357 id = get_struct_ident(type);
2358 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2362 id = get_union_ident(type);
2363 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2366 case tpo_enumeration:
2367 id = get_enumeration_ident(type);
2368 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2372 snprintf(buffer, buffer_size, "unknown type");
2376 p = snprintf(buffer, buffer_size, "pointer to ");
2379 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2383 p = snprintf(buffer, buffer_size, "array of ");
2386 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2390 id = get_mode_ident(get_type_mode(type));
2391 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2395 snprintf(buffer, buffer_size, "none");
2398 /* TODO: we should print argument and return types here... */
2399 snprintf(buffer, buffer_size, "method type");
2402 snprintf(buffer, buffer_size, "invalid type");