2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of types.
23 * @author Goetz Lindenmaier, Michael Beck
27 * Implementation of the datastructure to hold
30 * This module supplies a datastructure to represent all types
31 * known in the compiled program. This includes types specified
32 * in the program as well as types defined by the language. In the
33 * view of the intermediate representation there is no difference
34 * between these types.
36 * There exist several kinds of types, arranged by the structure of
37 * the type. A type is described by a set of attributes. Some of
38 * these attributes are common to all types, others depend on the
41 * Types are different from the modes defined in irmode: Types are
42 * on the level of the programming language, modes at the level of
43 * the target processor.
66 ir_type *firm_none_type;
67 ir_type *get_none_type(void)
69 return firm_none_type;
72 ir_type *firm_code_type;
73 ir_type *get_code_type(void)
75 return firm_code_type;
78 ir_type *firm_unknown_type;
79 ir_type *get_unknown_type(void)
81 return firm_unknown_type;
84 /* 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 void ir_init_type(void)
90 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
91 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
93 /* construct none and unknown type. */
94 firm_none_type = new_type(tpop_none, mode_BAD, NULL);
95 set_type_size_bytes(firm_none_type, 0);
96 set_type_state (firm_none_type, layout_fixed);
97 remove_irp_type(firm_none_type);
99 firm_code_type = new_type(tpop_code, mode_ANY, NULL);
100 set_type_state(firm_code_type, layout_fixed);
101 remove_irp_type(firm_code_type);
103 firm_unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
104 set_type_size_bytes(firm_unknown_type, 0);
105 set_type_state (firm_unknown_type, layout_fixed);
106 remove_irp_type(firm_unknown_type);
109 void ir_finish_type(void)
111 if (firm_none_type != NULL) {
112 free_type(firm_none_type);
113 firm_none_type = NULL;
115 if (firm_code_type != NULL) {
116 free_type(firm_code_type);
117 firm_code_type = NULL;
119 if (firm_unknown_type != NULL) {
120 free_type(firm_unknown_type);
121 firm_unknown_type = NULL;
123 value_params_suffix = NULL;
124 value_ress_suffix = NULL;
127 /** the global type visited flag */
128 ir_visited_t firm_type_visited;
130 void (set_master_type_visited)(ir_visited_t val)
132 _set_master_type_visited(val);
135 ir_visited_t (get_master_type_visited)(void)
137 return _get_master_type_visited();
140 void (inc_master_type_visited)(void)
142 _inc_master_type_visited();
145 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db)
150 node_size = offsetof(ir_type, attr) + type_op->attr_size;
151 res = (ir_type*)xmalloc(node_size);
152 memset(res, 0, node_size);
155 res->type_op = type_op;
157 res->visibility = ir_visibility_external;
158 res->flags = tf_none;
164 res->assoc_type = NULL;
166 res->nr = get_irp_new_node_nr();
167 #endif /* defined DEBUG_libfirm */
169 add_irp_type(res); /* Remember the new type global. */
174 void free_type(ir_type *tp)
176 const tp_op *op = get_type_tpop(tp);
178 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
179 || (get_type_tpop(tp) == tpop_code))
181 /* Remove from list of all types */
183 /* Free the attributes of the type. */
185 /* Free entities automatically allocated with the ir_type */
186 if (op->ops.free_auto_entities)
187 op->ops.free_auto_entities(tp);
188 /* And now the type itself... */
193 void free_type_entities(ir_type *tp)
195 const tp_op *tpop = get_type_tpop(tp);
197 if (tpop->ops.free_entities)
198 tpop->ops.free_entities(tp);
201 void free_type_attrs(ir_type *tp)
203 const tp_op *tpop = get_type_tpop(tp);
205 if (tpop->ops.free_attrs)
206 tpop->ops.free_attrs(tp);
209 void *(get_type_link)(const ir_type *tp)
211 return _get_type_link(tp);
214 void (set_type_link)(ir_type *tp, void *l)
216 _set_type_link(tp, l);
219 const tp_op *(get_type_tpop)(const ir_type *tp)
221 return _get_type_tpop(tp);
224 ident *(get_type_tpop_nameid)(const ir_type *tp)
226 return _get_type_tpop_nameid(tp);
229 const char* get_type_tpop_name(const ir_type *tp)
231 assert(tp && tp->kind == k_type);
232 return get_id_str(tp->type_op->name);
235 tp_opcode (get_type_tpop_code)(const ir_type *tp)
237 return _get_type_tpop_code(tp);
240 ir_mode *(get_type_mode)(const ir_type *tp)
242 return _get_type_mode(tp);
245 void set_type_mode(ir_type *tp, ir_mode *mode)
247 const tp_op *tpop = get_type_tpop(tp);
249 if (tpop->ops.set_type_mode)
250 tpop->ops.set_type_mode(tp, mode);
252 assert(0 && "setting a mode is NOT allowed for this type");
255 /* Outputs a unique number for this node */
256 long get_type_nr(const ir_type *tp)
262 return (long)PTR_TO_INT(tp);
266 unsigned (get_type_size_bytes)(const ir_type *tp)
268 return _get_type_size_bytes(tp);
271 ir_visibility get_type_visibility(const ir_type *tp)
274 return tp->visibility;
277 void set_type_visibility(ir_type *tp, ir_visibility v)
283 void set_type_size_bytes(ir_type *tp, unsigned size)
285 const tp_op *tpop = get_type_tpop(tp);
287 if (tpop->ops.set_type_size)
288 tpop->ops.set_type_size(tp, size);
290 assert(0 && "Cannot set size for this type");
293 unsigned get_type_alignment_bytes(ir_type *tp)
300 /* alignment NOT set calculate it "on demand" */
302 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
303 else if (is_Array_type(tp))
304 align = get_type_alignment_bytes(get_array_element_type(tp));
305 else if (is_compound_type(tp)) {
306 size_t i, n = get_compound_n_members(tp);
309 for (i = 0; i < n; ++i) {
310 ir_type *t = get_entity_type(get_compound_member(tp, i));
311 unsigned a = get_type_alignment_bytes(t);
316 } else if (is_Method_type(tp)) {
326 void set_type_alignment_bytes(ir_type *tp, unsigned align)
328 assert(tp && tp->kind == k_type);
329 /* Methods don't have an alignment. */
330 if (tp->type_op != type_method) {
335 const char *get_type_state_name(ir_type_state s)
337 #define X(a) case a: return #a;
346 ir_type_state (get_type_state)(const ir_type *tp)
348 return _get_type_state(tp);
351 void set_type_state(ir_type *tp, ir_type_state state)
353 assert(tp && tp->kind == k_type);
355 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
356 (tp->type_op == type_method))
359 /* Just a correctness check: */
360 if (state == layout_fixed) {
362 switch (get_type_tpop_code(tp)) {
364 if (tp != get_glob_type()) {
365 size_t n_mem = get_class_n_members(tp);
366 for (i = 0; i < n_mem; i++) {
367 assert(get_entity_offset(get_class_member(tp, i)) > -1);
369 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
370 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
376 for (i = 0; i < get_struct_n_members(tp); i++) {
377 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
386 Assure that only innermost dimension is dynamic? */
388 case tpo_enumeration: {
390 size_t n_enums = get_enumeration_n_enums(tp);
391 assert(get_type_mode(tp) != NULL);
392 for (i = 0; i < n_enums; ++i) {
393 ir_enum_const *ec = get_enumeration_const(tp, i);
394 ir_tarval *tv = get_enumeration_value(ec);
395 assert(tv != NULL && tv != tarval_bad);
403 if (state == layout_fixed)
404 tp->flags |= tf_layout_fixed;
406 tp->flags &= ~tf_layout_fixed;
409 ir_visited_t (get_type_visited)(const ir_type *tp)
411 return _get_type_visited(tp);
414 void (set_type_visited)(ir_type *tp, ir_visited_t num)
416 _set_type_visited(tp, num);
419 void (mark_type_visited)(ir_type *tp)
421 _mark_type_visited(tp);
424 int (type_visited)(const ir_type *tp)
426 return _type_visited(tp);
429 int (type_not_visited)(const ir_type *tp)
431 return _type_not_visited(tp);
434 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
436 return _get_type_dbg_info(tp);
439 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
441 _set_type_dbg_info(tp, db);
444 int (is_type)(const void *thing)
446 return _is_type(thing);
449 /* Checks whether two types are structural equal.*/
450 int equal_type(ir_type *typ1, ir_type *typ2)
457 if (typ1 == typ2) return 1;
459 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
460 typ1->name != typ2->name ||
461 (get_type_mode(typ1) != get_type_mode(typ2)) ||
462 (get_type_state(typ1) != get_type_state(typ2)))
464 if ((get_type_state(typ1) == layout_fixed) &&
465 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
468 switch (get_type_tpop_code(typ1)) {
470 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
471 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
472 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
473 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
474 /** Compare the members **/
475 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
476 /* First sort the members of typ2 */
477 for (i = 0; i < get_class_n_members(typ1); i++) {
478 ir_entity *e1 = get_class_member(typ1, i);
479 for (j = 0; j < get_class_n_members(typ2); j++) {
480 ir_entity *e2 = get_class_member(typ2, j);
481 if (get_entity_name(e1) == get_entity_name(e2))
485 for (i = 0; i < get_class_n_members(typ1); i++) {
486 if (!m[i] || get_class_member(typ1, i) != m[i])
489 /** Compare the supertypes **/
490 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
491 /* First sort the supertypes of typ2 */
492 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
493 ir_type *t1 = get_class_supertype(typ1, i);
494 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
495 ir_type *t2 = get_class_supertype(typ2, j);
496 if (t2->name == t1->name)
500 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
501 if (!t[i] || /* Found no counterpart */
502 get_class_supertype(typ1, i) != t[i])
508 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
509 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
510 /* First sort the members of lt */
511 for (i = 0; i < get_struct_n_members(typ1); i++) {
512 ir_entity *e1 = get_struct_member(typ1, i);
513 for (j = 0; j < get_struct_n_members(typ2); j++) {
514 ir_entity *e2 = get_struct_member(typ2, j);
515 if (get_entity_name(e1) == get_entity_name(e2))
519 for (i = 0; i < get_struct_n_members(typ1); i++) {
520 if (!m[i] || get_struct_member(typ1, i) != m[i])
529 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
530 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
531 if (get_method_calling_convention(typ1) !=
532 get_method_calling_convention(typ2)) return 0;
534 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
535 n_param1 = get_method_n_params(typ1);
536 n_param2 = get_method_n_params(typ2);
538 n_param1 = get_method_first_variadic_param_index(typ1);
539 n_param2 = get_method_first_variadic_param_index(typ2);
542 if (n_param1 != n_param2) return 0;
544 for (i = 0; i < n_param1; i++) {
545 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
548 for (i = 0; i < get_method_n_ress(typ1); i++) {
549 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
555 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
556 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
557 /* First sort the members of lt */
558 for (i = 0; i < get_union_n_members(typ1); i++) {
559 ir_entity *e1 = get_union_member(typ1, i);
560 for (j = 0; j < get_union_n_members(typ2); j++) {
561 ir_entity *e2 = get_union_member(typ2, j);
562 if (get_entity_name(e1) == get_entity_name(e2))
566 for (i = 0; i < get_union_n_members(typ1); i++) {
567 if (!m[i] || get_union_member(typ1, i) != m[i])
573 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
575 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
577 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
578 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
579 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
581 if (get_array_order(typ1, i) != get_array_order(typ2, i))
582 assert(0 && "type compare with different dimension orders not implemented");
586 case tpo_enumeration:
587 assert(0 && "enumerations not implemented");
591 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
603 int smaller_type(ir_type *st, ir_type *lt)
610 if (st == lt) return 1;
612 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
615 switch (get_type_tpop_code(st)) {
617 return is_SubClass_of(st, lt);
620 n_st_members = get_struct_n_members(st);
621 if (n_st_members != get_struct_n_members(lt))
624 m = ALLOCANZ(ir_entity*, n_st_members);
625 /* First sort the members of lt */
626 for (i = 0; i < n_st_members; ++i) {
627 ir_entity *se = get_struct_member(st, i);
628 size_t n = get_struct_n_members(lt);
629 for (j = 0; j < n; ++j) {
630 ir_entity *le = get_struct_member(lt, j);
631 if (get_entity_name(le) == get_entity_name(se))
635 for (i = 0; i < n_st_members; i++) {
636 if (!m[i] || /* Found no counterpart */
637 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
643 size_t n_param1, n_param2;
645 /** FIXME: is this still 1? */
646 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
647 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
648 if (get_method_calling_convention(st) !=
649 get_method_calling_convention(lt)) return 0;
651 if (get_method_variadicity(st) == variadicity_non_variadic) {
652 n_param1 = get_method_n_params(st);
653 n_param2 = get_method_n_params(lt);
655 n_param1 = get_method_first_variadic_param_index(st);
656 n_param2 = get_method_first_variadic_param_index(lt);
659 if (n_param1 != n_param2) return 0;
661 for (i = 0; i < get_method_n_params(st); i++) {
662 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
665 for (i = 0; i < get_method_n_ress(st); i++) {
666 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
672 n_st_members = get_union_n_members(st);
673 if (n_st_members != get_union_n_members(lt)) return 0;
674 m = ALLOCANZ(ir_entity*, n_st_members);
675 /* First sort the members of lt */
676 for (i = 0; i < n_st_members; ++i) {
677 ir_entity *se = get_union_member(st, i);
678 size_t n = get_union_n_members(lt);
679 for (j = 0; j < n; ++j) {
680 ir_entity *le = get_union_member(lt, j);
681 if (get_entity_name(le) == get_entity_name(se))
685 for (i = 0; i < n_st_members; ++i) {
686 if (!m[i] || /* Found no counterpart */
687 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
693 ir_type *set, *let; /* small/large elt. ir_type */
694 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
696 set = get_array_element_type(st);
697 let = get_array_element_type(lt);
699 /* If the element types are different, set must be convertible
700 to let, and they must have the same size so that address
701 computations work out. To have a size the layout must
703 if ((get_type_state(set) != layout_fixed) ||
704 (get_type_state(let) != layout_fixed))
706 if (!smaller_type(set, let) ||
707 get_type_size_bytes(set) != get_type_size_bytes(let))
710 for (i = 0; i < get_array_n_dimensions(st); i++) {
711 if (get_array_lower_bound(lt, i))
712 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
714 if (get_array_upper_bound(lt, i))
715 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
720 case tpo_enumeration:
721 assert(0 && "enumerations not implemented");
725 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
730 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
740 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
744 res = new_type(type_class, NULL, db);
747 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
748 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
749 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
750 res->attr.ca.peculiarity = peculiarity_existent;
751 res->attr.ca.type_info = NULL;
752 res->attr.ca.vtable_size = 0;
753 res->attr.ca.clss_flags = cf_none;
754 res->attr.ca.dfn = 0;
759 ir_type *new_type_class(ident *name)
761 return new_d_type_class (name, NULL);
764 void free_class_entities(ir_type *clss)
767 assert(clss && (clss->type_op == type_class));
768 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
769 free_entity(get_class_member(clss, i));
770 /* do NOT free the type info here. It belongs to another class */
773 void free_class_attrs(ir_type *clss)
775 assert(clss && (clss->type_op == type_class));
776 DEL_ARR_F(clss->attr.ca.members);
777 DEL_ARR_F(clss->attr.ca.subtypes);
778 DEL_ARR_F(clss->attr.ca.supertypes);
781 ident *get_class_ident(const ir_type *clss)
783 assert(clss->type_op == type_class);
787 const char *get_class_name(const ir_type *clss)
789 if (get_class_ident(clss) == NULL)
791 return get_id_str(get_class_ident(clss));
794 static void add_class_member(ir_type *clss, ir_entity *member)
796 assert(clss && (clss->type_op == type_class));
797 assert(clss != get_entity_type(member) && "recursive type");
798 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
801 size_t (get_class_n_members)(const ir_type *clss)
803 return _get_class_n_members(clss);
806 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
809 assert(clss && (clss->type_op == type_class));
810 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
811 if (get_class_member(clss, i) == mem)
814 return INVALID_MEMBER_INDEX;
817 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
819 return _get_class_member(clss, pos);
822 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
825 assert(clss && (clss->type_op == type_class));
826 n_mem = get_class_n_members(clss);
827 for (i = 0; i < n_mem; ++i) {
828 ir_entity *mem = get_class_member(clss, i);
829 if (get_entity_ident(mem) == name)
835 static void remove_class_member(ir_type *clss, ir_entity *member)
838 assert(clss && (clss->type_op == type_class));
839 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
840 if (clss->attr.ca.members[i] == member) {
841 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
842 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
843 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
849 void add_class_subtype(ir_type *clss, ir_type *subtype)
852 assert(clss->type_op == type_class);
853 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
854 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
855 if (get_class_supertype(subtype, i) == clss)
856 /* Class already registered */
859 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
862 size_t get_class_n_subtypes(const ir_type *clss)
864 assert(clss->type_op == type_class);
865 return ARR_LEN (clss->attr.ca.subtypes);
868 ir_type *get_class_subtype(ir_type *clss, size_t pos)
870 assert(clss->type_op == type_class);
871 assert(pos < get_class_n_subtypes(clss));
872 return clss->attr.ca.subtypes[pos];
875 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
877 size_t i, n_subtypes = get_class_n_subtypes(clss);
878 assert(is_Class_type(subclass));
879 for (i = 0; i < n_subtypes; ++i) {
880 if (get_class_subtype(clss, i) == subclass)
886 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
888 assert(clss->type_op == type_class);
889 assert(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_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
907 void add_class_supertype(ir_type *clss, ir_type *supertype)
911 assert(clss && (clss->type_op == type_class));
912 assert(supertype && (supertype -> type_op == type_class));
913 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
914 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
915 if (get_class_subtype(supertype, i) == clss)
916 /* Class already registered */
919 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
922 size_t get_class_n_supertypes(const ir_type *clss)
924 assert(clss->type_op == type_class);
925 return ARR_LEN(clss->attr.ca.supertypes);
928 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
930 size_t i, n_supertypes = get_class_n_supertypes(clss);
931 assert(super_clss && (super_clss->type_op == type_class));
932 for (i = 0; i < n_supertypes; i++) {
933 if (get_class_supertype(clss, i) == super_clss)
939 ir_type *get_class_supertype(ir_type *clss, size_t pos)
941 assert(clss->type_op == type_class);
942 assert(pos < get_class_n_supertypes(clss));
943 return clss->attr.ca.supertypes[pos];
946 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
948 assert(clss->type_op == type_class);
949 assert(pos < get_class_n_supertypes(clss));
950 clss->attr.ca.supertypes[pos] = supertype;
953 void remove_class_supertype(ir_type *clss, ir_type *supertype)
956 assert(clss && (clss->type_op == type_class));
957 for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
958 if (clss->attr.ca.supertypes[i] == supertype) {
959 for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
960 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
961 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
967 ir_entity *get_class_type_info(const ir_type *clss)
969 return clss->attr.ca.type_info;
972 void set_class_type_info(ir_type *clss, ir_entity *ent)
974 clss->attr.ca.type_info = ent;
976 ent->repr_class = clss;
979 ir_peculiarity get_class_peculiarity(const ir_type *clss)
981 assert(clss && (clss->type_op == type_class));
982 return clss->attr.ca.peculiarity;
985 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
987 assert(clss && (clss->type_op == type_class));
988 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
989 clss->attr.ca.peculiarity = pec;
992 unsigned (get_class_vtable_size)(const ir_type *clss)
994 return _get_class_vtable_size(clss);
997 void (set_class_vtable_size)(ir_type *clss, unsigned size)
999 _set_class_vtable_size(clss, size);
1002 int (is_class_final)(const ir_type *clss)
1004 return _is_class_final(clss);
1007 void (set_class_final)(ir_type *clss, int flag)
1009 _set_class_final(clss, flag);
1012 int (is_class_interface)(const ir_type *clss)
1014 return _is_class_interface(clss);
1017 void (set_class_interface)(ir_type *clss, int flag)
1019 _set_class_interface(clss, flag);
1022 int (is_class_abstract)(const ir_type *clss)
1024 return _is_class_abstract(clss);
1027 void (set_class_abstract)(ir_type *clss, int final)
1029 _set_class_abstract(clss, final);
1032 void set_class_dfn(ir_type *clss, int dfn)
1034 clss->attr.ca.dfn = dfn;
1037 int get_class_dfn(const ir_type *clss)
1039 return (clss->attr.ca.dfn);
1042 int (is_Class_type)(const ir_type *clss)
1044 return _is_class_type(clss);
1047 void set_class_mode(ir_type *tp, ir_mode *mode)
1049 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1050 assert(get_type_state(tp) == layout_fixed &&
1051 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1055 void set_class_size(ir_type *tp, unsigned size)
1061 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1063 ir_type *res = new_type(type_struct, NULL, db);
1066 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1071 ir_type *new_type_struct(ident *name)
1073 return new_d_type_struct (name, NULL);
1076 void free_struct_entities(ir_type *strct)
1079 assert(strct && (strct->type_op == type_struct));
1080 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1081 free_entity(get_struct_member(strct, i));
1084 void free_struct_attrs(ir_type *strct)
1086 assert(strct && (strct->type_op == type_struct));
1087 DEL_ARR_F(strct->attr.sa.members);
1090 ident *get_struct_ident(const ir_type *strct)
1092 assert(strct->type_op == type_struct);
1096 const char *get_struct_name(const ir_type *strct)
1098 if (get_struct_ident(strct) == NULL)
1100 return get_id_str(get_struct_ident(strct));
1103 size_t get_struct_n_members(const ir_type *strct)
1105 assert(strct->type_op == type_struct);
1106 return ARR_LEN(strct->attr.sa.members);
1109 static void add_struct_member(ir_type *strct, ir_entity *member)
1111 assert(strct && (strct->type_op == type_struct));
1112 assert(get_type_tpop(get_entity_type(member)) != type_method);
1113 assert(strct != get_entity_type(member) && "recursive type");
1114 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1117 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1119 assert(strct && (strct->type_op == type_struct));
1120 assert(pos < get_struct_n_members(strct));
1121 return strct->attr.sa.members[pos];
1124 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1127 assert(strct && (strct->type_op == type_struct));
1128 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1129 if (get_struct_member(strct, i) == mem)
1135 static void remove_struct_member(ir_type *strct, ir_entity *member)
1138 assert(strct && (strct->type_op == type_struct));
1139 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1140 if (strct->attr.sa.members[i] == member) {
1141 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1142 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1143 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1149 int (is_Struct_type)(const ir_type *strct)
1151 return _is_struct_type(strct);
1154 void set_struct_mode(ir_type *tp, ir_mode *mode)
1156 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1157 assert(get_type_state(tp) == layout_fixed &&
1158 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1162 void set_struct_size(ir_type *tp, unsigned size)
1169 * Lazy construction of value argument / result representation.
1170 * Constructs a struct type and its member. The types of the members
1171 * are passed in the argument list.
1173 * @param name name of the type constructed
1174 * @param len number of fields
1175 * @param tps array of field types with length len
1177 static ir_type *build_value_type(char const* name, size_t len, tp_ent_pair *tps)
1180 ir_type *res = new_type_struct(new_id_from_str(name));
1181 res->flags |= tf_value_param_type;
1182 /* Remove type from type list. Must be treated differently than other types. */
1183 remove_irp_type(res);
1184 for (i = 0; i < len; ++i) {
1185 ident *id = tps[i].param_name;
1187 /* use res as default if corresponding type is not yet set. */
1188 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1190 /* use the parameter name if specified */
1192 id = new_id_from_str("elt");
1194 tps[i].ent = new_entity(res, id, elt_type);
1195 set_entity_allocation(tps[i].ent, allocation_parameter);
1200 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1204 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1205 res = new_type(type_method, mode_P_code, db);
1206 res->flags |= tf_layout_fixed;
1207 res->size = get_mode_size_bytes(mode_P_code);
1208 res->attr.ma.n_params = n_param;
1209 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1210 res->attr.ma.value_params = NULL;
1211 res->attr.ma.n_res = n_res;
1212 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1213 res->attr.ma.value_ress = NULL;
1214 res->attr.ma.variadicity = variadicity_non_variadic;
1215 res->attr.ma.first_variadic_param = -1;
1216 res->attr.ma.additional_properties = mtp_no_property;
1221 ir_type *new_type_method(size_t n_param, size_t n_res)
1223 return new_d_type_method(n_param, n_res, NULL);
1226 ir_type *clone_type_method(ir_type *tp)
1234 assert(is_Method_type(tp));
1237 n_params = tp->attr.ma.n_params;
1238 n_res = tp->attr.ma.n_res;
1241 res = new_type(type_method, mode, db);
1243 res->flags = tp->flags;
1244 res->assoc_type = tp->assoc_type;
1245 res->size = tp->size;
1246 res->attr.ma.n_params = n_params;
1247 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1248 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1249 res->attr.ma.value_params = tp->attr.ma.value_params;
1250 res->attr.ma.n_res = n_res;
1251 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1252 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1253 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1254 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1255 res->attr.ma.first_variadic_param = tp->attr.ma.first_variadic_param;
1256 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1257 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1262 void free_method_entities(ir_type *method)
1265 assert(method && (method->type_op == type_method));
1268 void free_method_attrs(ir_type *method)
1270 assert(method && (method->type_op == type_method));
1271 free(method->attr.ma.params);
1272 free(method->attr.ma.res_type);
1273 /* cannot free it yet, type could be cloned ...
1274 if (method->attr.ma.value_params) {
1275 free_type_entities(method->attr.ma.value_params);
1276 free_type(method->attr.ma.value_params);
1279 if (method->attr.ma.value_ress) {
1280 free_type_entities(method->attr.ma.value_ress);
1281 free_type(method->attr.ma.value_ress);
1285 size_t (get_method_n_params)(const ir_type *method)
1287 return _get_method_n_params(method);
1290 ir_type *get_method_param_type(ir_type *method, size_t pos)
1293 assert(method->type_op == type_method);
1294 assert(pos < get_method_n_params(method));
1295 res = method->attr.ma.params[pos].tp;
1296 assert(res != NULL && "empty method param type");
1300 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1302 assert(method->type_op == type_method);
1303 assert(pos < get_method_n_params(method));
1304 method->attr.ma.params[pos].tp = tp;
1305 /* If information constructed set pass-by-value representation. */
1306 if (method->attr.ma.value_params) {
1307 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1308 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1312 ident *get_method_param_ident(ir_type *method, size_t pos)
1314 assert(method->type_op == type_method);
1315 assert(pos < get_method_n_params(method));
1316 return method->attr.ma.params[pos].param_name;
1319 const char *get_method_param_name(ir_type *method, size_t pos)
1321 ident *id = get_method_param_ident(method, pos);
1322 return id ? get_id_str(id) : NULL;
1325 void set_method_param_ident(ir_type *method, size_t pos, ident *id)
1327 assert(method->type_op == type_method);
1328 assert(pos < get_method_n_params(method));
1329 method->attr.ma.params[pos].param_name = id;
1332 ir_entity *get_method_value_param_ent(ir_type *method, size_t pos)
1334 assert(method && (method->type_op == type_method));
1335 assert(pos < get_method_n_params(method));
1337 if (!method->attr.ma.value_params) {
1338 /* parameter value type not created yet, build */
1339 method->attr.ma.value_params = build_value_type("<value param>",
1340 get_method_n_params(method), method->attr.ma.params);
1343 * build_value_type() sets the method->attr.ma.value_params type as default if
1346 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1347 && "param type not yet set");
1348 return method->attr.ma.params[pos].ent;
1351 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 size_t (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, size_t pos)
1383 assert(method->type_op == type_method);
1384 assert(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, size_t pos, ir_type *tp)
1392 assert(method->type_op == type_method);
1393 assert(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, size_t pos)
1405 assert(method->type_op == type_method);
1406 assert(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->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 size_t get_method_first_variadic_param_index(const ir_type *method)
1455 assert(method->type_op == type_method);
1457 if (method->attr.ma.variadicity == variadicity_non_variadic)
1460 if (method->attr.ma.first_variadic_param == (size_t)-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, size_t index)
1467 assert(method->type_op == type_method);
1468 assert(index <= get_method_n_params(method));
1470 method->attr.ma.first_variadic_param = index;
1473 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1475 return _get_method_additional_properties(method);
1478 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1480 _set_method_additional_properties(method, mask);
1483 void (add_method_additional_properties)(ir_type *method,
1484 mtp_additional_properties flag)
1486 _add_method_additional_properties(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 = 0, n = get_union_n_members(uni); i < n; ++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 size_t get_union_n_members(const ir_type *uni)
1565 assert(uni->type_op == type_union);
1566 return ARR_LEN(uni->attr.ua.members);
1569 static void add_union_member(ir_type *uni, ir_entity *member)
1571 assert(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, size_t pos)
1578 assert(uni->type_op == type_union);
1579 assert(pos < get_union_n_members(uni));
1580 return uni->attr.ua.members[pos];
1583 size_t 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 static void remove_union_member(ir_type *uni, ir_entity *member)
1597 assert(uni && (uni->type_op == type_union));
1598 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1599 if (uni->attr.ua.members[i] == member) {
1600 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1601 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1602 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1608 int (is_Union_type)(const ir_type *uni)
1610 return _is_union_type(uni);
1613 void set_union_size(ir_type *tp, unsigned size)
1620 ir_type *new_d_type_array(int n_dimensions, ir_type *element_type,
1626 ir_graph *irg = get_const_code_irg();
1628 assert(!is_Method_type(element_type));
1630 res = new_type(type_array, NULL, db);
1631 res->attr.aa.n_dimensions = n_dimensions;
1632 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1633 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1634 res->attr.aa.order = XMALLOCNZ(int, n_dimensions);
1636 unk = new_r_Unknown(irg, mode_Iu);
1637 for (i = 0; i < n_dimensions; i++) {
1638 res->attr.aa.lower_bound[i] =
1639 res->attr.aa.upper_bound[i] = unk;
1640 res->attr.aa.order[i] = i;
1643 res->attr.aa.element_type = element_type;
1644 res->attr.aa.element_ent
1645 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1646 res->attr.aa.element_ent->owner = res;
1652 ir_type *new_type_array(int n_dimensions, ir_type *element_type)
1654 return new_d_type_array(n_dimensions, element_type, NULL);
1657 void free_array_automatic_entities(ir_type *array)
1659 assert(array && (array->type_op == type_array));
1660 free_entity(get_array_element_entity(array));
1663 void free_array_entities(ir_type *array)
1666 assert(array->type_op == type_array);
1669 void free_array_attrs(ir_type *array)
1671 assert(array->type_op == type_array);
1672 free(array->attr.aa.lower_bound);
1673 free(array->attr.aa.upper_bound);
1674 free(array->attr.aa.order);
1677 /* manipulate private fields of array ir_type */
1678 size_t get_array_n_dimensions(const ir_type *array)
1680 assert(array->type_op == type_array);
1681 return array->attr.aa.n_dimensions;
1684 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1685 ir_node *upper_bound)
1687 assert(array && (array->type_op == type_array));
1688 assert(lower_bound && "lower_bound node may not be NULL.");
1689 assert(upper_bound && "upper_bound node may not be NULL.");
1690 assert(dimension < array->attr.aa.n_dimensions);
1691 array->attr.aa.lower_bound[dimension] = lower_bound;
1692 array->attr.aa.upper_bound[dimension] = upper_bound;
1695 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1698 ir_graph *irg = get_const_code_irg();
1699 set_array_bounds(array, dimension,
1700 new_r_Const_long(irg, mode_Iu, lower_bound),
1701 new_r_Const_long(irg, mode_Iu, upper_bound));
1704 void set_array_lower_bound(ir_type *array, size_t dimension,
1705 ir_node *lower_bound)
1707 assert(array && (array->type_op == type_array));
1708 assert(lower_bound && "lower_bound node may not be NULL.");
1709 array->attr.aa.lower_bound[dimension] = lower_bound;
1712 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1714 ir_graph *irg = get_const_code_irg();
1715 set_array_lower_bound(array, dimension,
1716 new_r_Const_long(irg, mode_Iu, lower_bound));
1719 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1721 assert(array && (array->type_op == type_array));
1722 assert(upper_bound && "upper_bound node may not be NULL.");
1723 array->attr.aa.upper_bound[dimension] = upper_bound;
1726 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1728 ir_graph *irg = get_const_code_irg();
1729 set_array_upper_bound(array, dimension,
1730 new_r_Const_long(irg, mode_Iu, upper_bound));
1733 int has_array_lower_bound(const ir_type *array, size_t dimension)
1735 assert(array && (array->type_op == type_array));
1736 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1739 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1741 assert(array && (array->type_op == type_array));
1742 return array->attr.aa.lower_bound[dimension];
1745 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1748 assert(array && (array->type_op == type_array));
1749 node = array->attr.aa.lower_bound[dimension];
1750 assert(is_Const(node));
1751 return get_tarval_long(get_Const_tarval(node));
1754 int has_array_upper_bound(const ir_type *array, size_t dimension)
1756 assert(array && (array->type_op == type_array));
1757 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1760 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1762 assert(array && (array->type_op == type_array));
1763 return array->attr.aa.upper_bound[dimension];
1766 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1769 assert(array && (array->type_op == type_array));
1770 node = array->attr.aa.upper_bound[dimension];
1771 assert(is_Const(node));
1772 return get_tarval_long(get_Const_tarval(node));
1775 void set_array_order(ir_type *array, size_t dimension, int order)
1777 assert(array && (array->type_op == type_array));
1778 array->attr.aa.order[dimension] = order;
1781 int get_array_order(const ir_type *array, size_t dimension)
1783 assert(array && (array->type_op == type_array));
1784 return array->attr.aa.order[dimension];
1787 size_t find_array_dimension(const ir_type *array, int order)
1791 assert(array->type_op == type_array);
1793 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1794 if (array->attr.aa.order[dim] == order)
1800 void set_array_element_type(ir_type *array, ir_type *tp)
1802 assert(array && (array->type_op == type_array));
1803 assert(!is_Method_type(tp));
1804 array->attr.aa.element_type = tp;
1807 ir_type *get_array_element_type(const ir_type *array)
1809 assert(array && (array->type_op == type_array));
1810 return array->attr.aa.element_type;
1813 void set_array_element_entity(ir_type *array, ir_entity *ent)
1815 assert(array && (array->type_op == type_array));
1816 assert((get_entity_type(ent)->type_op != type_method));
1817 array->attr.aa.element_ent = ent;
1818 array->attr.aa.element_type = get_entity_type(ent);
1821 ir_entity *get_array_element_entity(const ir_type *array)
1823 assert(array && (array->type_op == type_array));
1824 return array->attr.aa.element_ent;
1827 int (is_Array_type)(const ir_type *array)
1829 return _is_array_type(array);
1832 void set_array_size(ir_type *tp, unsigned size)
1834 /* FIXME: Here we should make some checks with the element type size */
1839 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1843 res = new_type(type_enumeration, NULL, db);
1845 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1850 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1852 return new_d_type_enumeration(name, n_enums, NULL);
1855 void free_enumeration_entities(ir_type *enumeration)
1858 assert(enumeration->type_op == type_enumeration);
1861 void free_enumeration_attrs(ir_type *enumeration)
1863 assert(enumeration->type_op == type_enumeration);
1864 DEL_ARR_F(enumeration->attr.ea.enumer);
1867 ident *get_enumeration_ident(const ir_type *enumeration)
1869 assert(enumeration->type_op == type_enumeration);
1870 return enumeration->name;
1873 const char *get_enumeration_name(const ir_type *enumeration)
1875 if (get_enumeration_ident(enumeration) == NULL)
1877 return get_id_str(get_enumeration_ident(enumeration));
1880 size_t get_enumeration_n_enums(const ir_type *enumeration)
1882 assert(enumeration->type_op == type_enumeration);
1883 return ARR_LEN(enumeration->attr.ea.enumer);
1886 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1889 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1890 enumeration->attr.ea.enumer[pos].nameid = nameid;
1891 enumeration->attr.ea.enumer[pos].value = con;
1892 enumeration->attr.ea.enumer[pos].owner = enumeration;
1895 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1897 assert(enumeration->type_op == type_enumeration);
1898 assert(pos < get_enumeration_n_enums(enumeration));
1899 return &enumeration->attr.ea.enumer[pos];
1902 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1904 return enum_cnst->owner;
1907 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1909 enum_cnst->value = con;
1912 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1914 return enum_cnst->value;
1917 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1919 enum_cnst->nameid = id;
1922 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1924 return enum_cnst->nameid;
1927 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1929 return get_id_str(enum_cnst->nameid);
1932 int (is_Enumeration_type)(const ir_type *enumeration)
1934 return _is_enumeration_type(enumeration);
1937 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1939 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1940 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1941 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1943 tp->size = get_mode_size_bytes(mode);
1949 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1954 if (is_Method_type(points_to) || is_code_type(points_to)) {
1960 res = new_type(type_pointer, mode, db);
1961 res->attr.pa.points_to = points_to;
1962 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1963 res->size = get_mode_size_bytes(res->mode);
1964 res->flags |= tf_layout_fixed;
1969 ir_type *new_type_pointer(ir_type *points_to)
1971 return new_d_type_pointer(points_to, NULL);
1974 void free_pointer_entities(ir_type *pointer)
1977 assert(pointer && (pointer->type_op == type_pointer));
1980 void free_pointer_attrs(ir_type *pointer)
1983 assert(pointer && (pointer->type_op == type_pointer));
1986 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1988 assert(pointer && (pointer->type_op == type_pointer));
1989 pointer->attr.pa.points_to = tp;
1992 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1994 assert(pointer && (pointer->type_op == type_pointer));
1995 return pointer->attr.pa.points_to;
1998 int (is_Pointer_type)(const ir_type *pointer)
2000 return _is_pointer_type(pointer);
2003 void set_pointer_mode(ir_type *tp, ir_mode *mode)
2005 assert(mode_is_reference(mode) && "Modes of pointers must be references");
2006 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
2007 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
2009 tp->size = get_mode_size_bytes(mode);
2013 ir_type *find_pointer_type_to_type(ir_type *tp)
2015 size_t i, n = get_irp_n_types();
2016 for (i = 0; i < n; ++i) {
2017 ir_type *found = get_irp_type(i);
2018 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
2021 return firm_unknown_type;
2026 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
2028 ir_type *res = new_type(type_primitive, mode, db);
2029 res->size = get_mode_size_bytes(mode);
2030 res->flags |= tf_layout_fixed;
2031 res->attr.ba.base_type = NULL;
2036 ir_type *new_type_primitive(ir_mode *mode)
2038 return new_d_type_primitive(mode, NULL);
2041 int (is_Primitive_type)(const ir_type *primitive)
2043 return _is_primitive_type(primitive);
2046 void set_primitive_mode(ir_type *tp, ir_mode *mode)
2048 /* Modes of primitives must be data */
2049 assert(mode_is_data(mode));
2051 /* For primitive size depends on the mode. */
2052 tp->size = get_mode_size_bytes(mode);
2056 ir_type *get_primitive_base_type(const ir_type *tp)
2058 assert(is_Primitive_type(tp));
2059 return tp->attr.ba.base_type;
2062 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
2064 assert(is_Primitive_type(tp));
2065 tp->attr.ba.base_type = base_tp;
2070 int (is_atomic_type)(const ir_type *tp)
2072 return _is_atomic_type(tp);
2075 size_t get_compound_n_members(const ir_type *tp)
2077 const tp_op *op = get_type_tpop(tp);
2078 return op->ops.get_n_members(tp);
2081 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
2083 const tp_op *op = get_type_tpop(tp);
2084 return op->ops.get_member(tp, pos);
2087 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
2089 const tp_op *op = get_type_tpop(tp);
2090 return op->ops.get_member_index(tp, member);
2093 int is_compound_type(const ir_type *tp)
2095 assert(tp->kind == k_type);
2096 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2099 ident *get_compound_ident(const ir_type *tp)
2101 assert(is_compound_type(tp));
2105 const char *get_compound_name(const ir_type *tp)
2107 if (get_compound_ident(tp) == NULL)
2109 return get_id_str(get_compound_ident(tp));
2112 void remove_compound_member(ir_type *compound, ir_entity *entity)
2114 switch (get_type_tpop_code(compound)) {
2115 case tpo_class: remove_class_member(compound, entity); break;
2116 case tpo_struct: remove_struct_member(compound, entity); break;
2117 case tpo_union: remove_union_member(compound, entity); break;
2119 panic("argument for remove_compound_member not a compound type");
2123 void add_compound_member(ir_type *compound, ir_entity *entity)
2125 switch (get_type_tpop_code(compound)) {
2126 case tpo_class: add_class_member(compound, entity); break;
2127 case tpo_struct: add_struct_member(compound, entity); break;
2128 case tpo_union: add_union_member(compound, entity); break;
2130 panic("argument for add_compound_member not a compound type");
2136 int is_code_type(const ir_type *tp)
2138 assert(tp && tp->kind == k_type);
2139 return tp->type_op == tpop_code;
2142 int is_frame_type(const ir_type *tp)
2144 return tp->flags & tf_frame_type;
2147 int is_value_param_type(const ir_type *tp)
2149 return tp->flags & tf_value_param_type;
2152 int is_lowered_type(const ir_type *tp)
2154 return tp->flags & tf_lowered_type;
2157 ir_type *new_type_value(void)
2159 ir_type *res = new_type_struct(new_id_from_str("<value_type>"));
2161 res->flags |= tf_value_param_type;
2163 /* Remove type from type list. Must be treated differently than other types. */
2164 remove_irp_type(res);
2169 ir_type *new_type_frame(void)
2171 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
2173 res->flags |= tf_frame_type;
2175 /* Remove type from type list. Must be treated differently than other types. */
2176 remove_irp_type(res);
2178 /* It is not possible to derive from the frame type. Set the final flag. */
2179 set_class_final(res, 1);
2184 ir_type *clone_frame_type(ir_type *type)
2189 assert(is_frame_type(type));
2190 /* the entity link resource should be allocated if this function is called */
2191 assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2193 res = new_type_frame();
2194 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2195 ir_entity *ent = get_class_member(type, i);
2196 ir_entity *nent = copy_entity_own(ent, res);
2197 set_entity_link(ent, nent);
2198 set_entity_link(nent, ent);
2203 void set_lowered_type(ir_type *tp, ir_type *lowered_type)
2205 assert(is_type(tp) && is_type(lowered_type));
2206 lowered_type->flags |= tf_lowered_type;
2207 tp->assoc_type = lowered_type;
2208 lowered_type->assoc_type = tp;
2211 ir_type *get_associated_type(const ir_type *tp)
2213 return tp->assoc_type;
2216 void set_default_size(ir_type *tp, unsigned size)
2221 void default_layout_compound_type(ir_type *type)
2224 size_t n = get_compound_n_members(type);
2226 unsigned align_all = 1;
2228 for (i = 0; i < n; ++i) {
2229 ir_entity *entity = get_compound_member(type, i);
2230 ir_type *entity_type = get_entity_type(entity);
2234 if (is_Method_type(entity_type))
2237 assert(get_type_state(entity_type) == layout_fixed);
2238 align = get_type_alignment_bytes(entity_type);
2239 align_all = align > align_all ? align : align_all;
2240 misalign = (align ? size % align : 0);
2241 size += (misalign ? align - misalign : 0);
2243 set_entity_offset(entity, size);
2244 if (!is_Union_type(type)) {
2245 size += get_type_size_bytes(entity_type);
2248 if (align_all > 0 && size % align_all) {
2249 size += align_all - (size % align_all);
2251 if (align_all > get_type_alignment_bytes(type)) {
2252 set_type_alignment_bytes(type, align_all);
2254 set_type_size_bytes(type, size);
2255 set_type_state(type, layout_fixed);
2258 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2265 unsigned frame_align;
2266 int offset, frame_size;
2267 static unsigned area_cnt = 0;
2268 static ir_type *a_byte = NULL;
2270 assert(is_frame_type(frame_type));
2271 assert(get_type_state(frame_type) == layout_fixed);
2272 assert(get_type_alignment_bytes(frame_type) > 0);
2273 set_type_state(frame_type, layout_undefined);
2276 a_byte = new_type_primitive(mode_Bu);
2278 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2279 name = new_id_from_str(buf);
2281 /* align the size */
2282 frame_align = get_type_alignment_bytes(frame_type);
2283 size = (size + frame_align - 1) & ~(frame_align - 1);
2285 tp = new_type_array(1, a_byte);
2286 set_array_bounds_int(tp, 0, 0, size);
2287 set_type_alignment_bytes(tp, alignment);
2289 frame_size = get_type_size_bytes(frame_type);
2292 /* fix all offsets so far */
2293 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2294 ir_entity *ent = get_class_member(frame_type, i);
2296 set_entity_offset(ent, get_entity_offset(ent) + size);
2298 /* calculate offset and new type size */
2302 /* increase size to match alignment... */
2303 if (alignment > frame_align) {
2304 frame_align = alignment;
2305 set_type_alignment_bytes(frame_type, frame_align);
2306 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2309 /* calculate offset and new type size */
2310 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2311 frame_size = offset + size;
2314 area = new_entity(frame_type, name, tp);
2315 set_entity_offset(area, offset);
2316 set_type_size_bytes(frame_type, frame_size);
2318 /* mark this entity as compiler generated */
2319 set_entity_compiler_generated(area, 1);
2321 set_type_state(frame_type, layout_fixed);
2325 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2329 type_dbg_info *tdbgi = get_type_dbg_info(type);
2330 if (tdbgi != NULL) {
2331 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2335 /* we have to construct some name... */
2336 switch (get_type_tpop_code(type)) {
2337 case tpo_uninitialized:
2340 snprintf(buffer, buffer_size, "code");
2344 id = get_class_ident(type);
2345 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2349 id = get_struct_ident(type);
2350 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2354 id = get_union_ident(type);
2355 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2358 case tpo_enumeration:
2359 id = get_enumeration_ident(type);
2360 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2364 snprintf(buffer, buffer_size, "unknown type");
2368 p = snprintf(buffer, buffer_size, "pointer to ");
2371 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2375 p = snprintf(buffer, buffer_size, "array of ");
2378 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2382 id = get_mode_ident(get_type_mode(type));
2383 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2387 snprintf(buffer, buffer_size, "none");
2390 /* TODO: we should print argument and return types here... */
2391 snprintf(buffer, buffer_size, "method type");
2394 snprintf(buffer, buffer_size, "invalid type");