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 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 int 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 int 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 assert(get_type_mode(tp) != NULL);
391 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
392 ir_enum_const *ec = get_enumeration_const(tp, i);
393 ir_tarval *tv = get_enumeration_value(ec);
394 assert(tv != NULL && tv != tarval_bad);
401 if (state == layout_fixed)
402 tp->flags |= tf_layout_fixed;
404 tp->flags &= ~tf_layout_fixed;
407 ir_visited_t (get_type_visited)(const ir_type *tp)
409 return _get_type_visited(tp);
412 void (set_type_visited)(ir_type *tp, ir_visited_t num)
414 _set_type_visited(tp, num);
417 void (mark_type_visited)(ir_type *tp)
419 _mark_type_visited(tp);
422 int (type_visited)(const ir_type *tp)
424 return _type_visited(tp);
427 int (type_not_visited)(const ir_type *tp)
429 return _type_not_visited(tp);
432 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
434 return _get_type_dbg_info(tp);
437 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
439 _set_type_dbg_info(tp, db);
442 int (is_type)(const void *thing)
444 return _is_type(thing);
447 /* Checks whether two types are structural equal.*/
448 int equal_type(ir_type *typ1, ir_type *typ2)
454 if (typ1 == typ2) return 1;
456 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
457 typ1->name != typ2->name ||
458 (get_type_mode(typ1) != get_type_mode(typ2)) ||
459 (get_type_state(typ1) != get_type_state(typ2)))
461 if ((get_type_state(typ1) == layout_fixed) &&
462 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
465 switch (get_type_tpop_code(typ1)) {
467 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
468 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
469 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
470 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
471 /** Compare the members **/
472 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
473 /* First sort the members of typ2 */
474 for (i = 0; i < get_class_n_members(typ1); i++) {
475 ir_entity *e1 = get_class_member(typ1, i);
476 for (j = 0; j < get_class_n_members(typ2); j++) {
477 ir_entity *e2 = get_class_member(typ2, j);
478 if (get_entity_name(e1) == get_entity_name(e2))
482 for (i = 0; i < get_class_n_members(typ1); i++) {
483 if (!m[i] || get_class_member(typ1, i) != m[i])
486 /** Compare the supertypes **/
487 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
488 /* First sort the supertypes of typ2 */
489 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
490 ir_type *t1 = get_class_supertype(typ1, i);
491 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
492 ir_type *t2 = get_class_supertype(typ2, j);
493 if (t2->name == t1->name)
497 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
498 if (!t[i] || /* Found no counterpart */
499 get_class_supertype(typ1, i) != t[i])
505 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
506 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
507 /* First sort the members of lt */
508 for (i = 0; i < get_struct_n_members(typ1); i++) {
509 ir_entity *e1 = get_struct_member(typ1, i);
510 for (j = 0; j < get_struct_n_members(typ2); j++) {
511 ir_entity *e2 = get_struct_member(typ2, j);
512 if (get_entity_name(e1) == get_entity_name(e2))
516 for (i = 0; i < get_struct_n_members(typ1); i++) {
517 if (!m[i] || get_struct_member(typ1, i) != m[i])
523 int n_param1, n_param2;
525 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
526 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
527 if (get_method_calling_convention(typ1) !=
528 get_method_calling_convention(typ2)) return 0;
530 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
531 n_param1 = get_method_n_params(typ1);
532 n_param2 = get_method_n_params(typ2);
534 n_param1 = get_method_first_variadic_param_index(typ1);
535 n_param2 = get_method_first_variadic_param_index(typ2);
538 if (n_param1 != n_param2) return 0;
540 for (i = 0; i < n_param1; i++) {
541 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
544 for (i = 0; i < get_method_n_ress(typ1); i++) {
545 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
551 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
552 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
553 /* First sort the members of lt */
554 for (i = 0; i < get_union_n_members(typ1); i++) {
555 ir_entity *e1 = get_union_member(typ1, i);
556 for (j = 0; j < get_union_n_members(typ2); j++) {
557 ir_entity *e2 = get_union_member(typ2, j);
558 if (get_entity_name(e1) == get_entity_name(e2))
562 for (i = 0; i < get_union_n_members(typ1); i++) {
563 if (!m[i] || get_union_member(typ1, i) != m[i])
569 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
571 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
573 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
574 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
575 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
577 if (get_array_order(typ1, i) != get_array_order(typ2, i))
578 assert(0 && "type compare with different dimension orders not implemented");
582 case tpo_enumeration:
583 assert(0 && "enumerations not implemented");
587 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
599 int smaller_type(ir_type *st, ir_type *lt)
602 int i, j, n_st_members;
604 if (st == lt) return 1;
606 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
609 switch (get_type_tpop_code(st)) {
611 return is_SubClass_of(st, lt);
614 n_st_members = get_struct_n_members(st);
615 if (n_st_members != get_struct_n_members(lt))
618 m = ALLOCANZ(ir_entity*, n_st_members);
619 /* First sort the members of lt */
620 for (i = 0; i < n_st_members; ++i) {
621 ir_entity *se = get_struct_member(st, i);
622 int n = get_struct_n_members(lt);
623 for (j = 0; j < n; ++j) {
624 ir_entity *le = get_struct_member(lt, j);
625 if (get_entity_name(le) == get_entity_name(se))
629 for (i = 0; i < n_st_members; i++) {
630 if (!m[i] || /* Found no counterpart */
631 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
637 int n_param1, n_param2;
639 /** FIXME: is this still 1? */
640 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
641 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
642 if (get_method_calling_convention(st) !=
643 get_method_calling_convention(lt)) return 0;
645 if (get_method_variadicity(st) == variadicity_non_variadic) {
646 n_param1 = get_method_n_params(st);
647 n_param2 = get_method_n_params(lt);
649 n_param1 = get_method_first_variadic_param_index(st);
650 n_param2 = get_method_first_variadic_param_index(lt);
653 if (n_param1 != n_param2) return 0;
655 for (i = 0; i < get_method_n_params(st); i++) {
656 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
659 for (i = 0; i < get_method_n_ress(st); i++) {
660 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
666 n_st_members = get_union_n_members(st);
667 if (n_st_members != get_union_n_members(lt)) return 0;
668 m = ALLOCANZ(ir_entity*, n_st_members);
669 /* First sort the members of lt */
670 for (i = 0; i < n_st_members; ++i) {
671 ir_entity *se = get_union_member(st, i);
672 int n = get_union_n_members(lt);
673 for (j = 0; j < n; ++j) {
674 ir_entity *le = get_union_member(lt, j);
675 if (get_entity_name(le) == get_entity_name(se))
679 for (i = 0; i < n_st_members; ++i) {
680 if (!m[i] || /* Found no counterpart */
681 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
687 ir_type *set, *let; /* small/large elt. ir_type */
688 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
690 set = get_array_element_type(st);
691 let = get_array_element_type(lt);
693 /* If the element types are different, set must be convertible
694 to let, and they must have the same size so that address
695 computations work out. To have a size the layout must
697 if ((get_type_state(set) != layout_fixed) ||
698 (get_type_state(let) != layout_fixed))
700 if (!smaller_type(set, let) ||
701 get_type_size_bytes(set) != get_type_size_bytes(let))
704 for (i = 0; i < get_array_n_dimensions(st); i++) {
705 if (get_array_lower_bound(lt, i))
706 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
708 if (get_array_upper_bound(lt, i))
709 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
714 case tpo_enumeration:
715 assert(0 && "enumerations not implemented");
719 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
724 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
734 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
738 res = new_type(type_class, NULL, db);
741 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
742 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
743 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
744 res->attr.ca.peculiarity = peculiarity_existent;
745 res->attr.ca.type_info = NULL;
746 res->attr.ca.vtable_size = 0;
747 res->attr.ca.clss_flags = cf_none;
748 res->attr.ca.dfn = 0;
753 ir_type *new_type_class(ident *name)
755 return new_d_type_class (name, NULL);
758 void free_class_entities(ir_type *clss)
761 assert(clss && (clss->type_op == type_class));
762 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
763 free_entity(get_class_member(clss, i));
764 /* do NOT free the type info here. It belongs to another class */
767 void free_class_attrs(ir_type *clss)
769 assert(clss && (clss->type_op == type_class));
770 DEL_ARR_F(clss->attr.ca.members);
771 DEL_ARR_F(clss->attr.ca.subtypes);
772 DEL_ARR_F(clss->attr.ca.supertypes);
775 ident *get_class_ident(const ir_type *clss)
777 assert(clss->type_op == type_class);
781 const char *get_class_name(const ir_type *clss)
783 if (get_class_ident(clss) == NULL)
785 return get_id_str(get_class_ident(clss));
788 static void add_class_member(ir_type *clss, ir_entity *member)
790 assert(clss && (clss->type_op == type_class));
791 assert(clss != get_entity_type(member) && "recursive type");
792 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
795 int (get_class_n_members)(const ir_type *clss)
797 return _get_class_n_members(clss);
800 int get_class_member_index(const ir_type *clss, ir_entity *mem)
803 assert(clss && (clss->type_op == type_class));
804 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
805 if (get_class_member(clss, i) == mem)
810 ir_entity *(get_class_member)(const ir_type *clss, int pos)
812 return _get_class_member(clss, pos);
815 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
818 assert(clss && (clss->type_op == type_class));
819 n_mem = get_class_n_members(clss);
820 for (i = 0; i < n_mem; ++i) {
821 ir_entity *mem = get_class_member(clss, i);
822 if (get_entity_ident(mem) == name) return mem;
827 static void remove_class_member(ir_type *clss, ir_entity *member)
830 assert(clss && (clss->type_op == type_class));
831 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
832 if (clss->attr.ca.members[i] == member) {
833 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
834 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
835 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
841 void add_class_subtype(ir_type *clss, ir_type *subtype)
844 assert(clss && (clss->type_op == type_class));
845 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
846 for (i = 0; i < get_class_n_supertypes(subtype); i++)
847 if (get_class_supertype(subtype, i) == clss)
848 /* Class already registered */
850 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
853 int get_class_n_subtypes(const ir_type *clss)
855 assert(clss && (clss->type_op == type_class));
856 return (ARR_LEN (clss->attr.ca.subtypes));
859 ir_type *get_class_subtype(ir_type *clss, int pos)
861 assert(clss && (clss->type_op == type_class));
862 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
863 return clss->attr.ca.subtypes[pos];
866 int get_class_subtype_index(ir_type *clss, const ir_type *subclass)
868 int i, n_subtypes = get_class_n_subtypes(clss);
869 assert(is_Class_type(subclass));
870 for (i = 0; i < n_subtypes; ++i) {
871 if (get_class_subtype(clss, i) == subclass) return i;
876 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos)
878 assert(clss && (clss->type_op == type_class));
879 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
880 clss->attr.ca.subtypes[pos] = subtype;
883 void remove_class_subtype(ir_type *clss, ir_type *subtype)
886 assert(clss && (clss->type_op == type_class));
887 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
888 if (clss->attr.ca.subtypes[i] == subtype) {
889 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
890 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
891 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
896 void add_class_supertype(ir_type *clss, ir_type *supertype)
899 assert(clss && (clss->type_op == type_class));
900 assert(supertype && (supertype -> type_op == type_class));
901 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
902 for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
903 if (get_class_subtype(supertype, i) == clss)
904 /* Class already registered */
906 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
909 int get_class_n_supertypes(const ir_type *clss)
911 assert(clss && (clss->type_op == type_class));
912 return ARR_LEN(clss->attr.ca.supertypes);
915 int get_class_supertype_index(ir_type *clss, ir_type *super_clss)
917 int i, n_supertypes = get_class_n_supertypes(clss);
918 assert(super_clss && (super_clss->type_op == type_class));
919 for (i = 0; i < n_supertypes; i++)
920 if (get_class_supertype(clss, i) == super_clss)
925 ir_type *get_class_supertype(ir_type *clss, int pos)
927 assert(clss && (clss->type_op == type_class));
928 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
929 return clss->attr.ca.supertypes[pos];
932 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos)
934 assert(clss && (clss->type_op == type_class));
935 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
936 clss->attr.ca.supertypes[pos] = supertype;
939 void remove_class_supertype(ir_type *clss, ir_type *supertype)
942 assert(clss && (clss->type_op == type_class));
943 for (i = 0; i < (ARR_LEN(clss->attr.ca.supertypes)); i++)
944 if (clss->attr.ca.supertypes[i] == supertype) {
945 for (; i < (ARR_LEN(clss->attr.ca.supertypes))-1; i++)
946 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
947 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
952 ir_entity *get_class_type_info(const ir_type *clss)
954 return clss->attr.ca.type_info;
957 void set_class_type_info(ir_type *clss, ir_entity *ent)
959 clss->attr.ca.type_info = ent;
961 ent->repr_class = clss;
964 ir_peculiarity get_class_peculiarity(const ir_type *clss)
966 assert(clss && (clss->type_op == type_class));
967 return clss->attr.ca.peculiarity;
970 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
972 assert(clss && (clss->type_op == type_class));
973 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
974 clss->attr.ca.peculiarity = pec;
977 unsigned (get_class_vtable_size)(const ir_type *clss)
979 return _get_class_vtable_size(clss);
982 void (set_class_vtable_size)(ir_type *clss, unsigned size)
984 _set_class_vtable_size(clss, size);
987 int (is_class_final)(const ir_type *clss)
989 return _is_class_final(clss);
992 void (set_class_final)(ir_type *clss, int flag)
994 _set_class_final(clss, flag);
997 int (is_class_interface)(const ir_type *clss)
999 return _is_class_interface(clss);
1002 void (set_class_interface)(ir_type *clss, int flag)
1004 _set_class_interface(clss, flag);
1007 int (is_class_abstract)(const ir_type *clss)
1009 return _is_class_abstract(clss);
1012 void (set_class_abstract)(ir_type *clss, int final)
1014 _set_class_abstract(clss, final);
1017 void set_class_dfn(ir_type *clss, int dfn)
1019 clss->attr.ca.dfn = dfn;
1022 int get_class_dfn(const ir_type *clss)
1024 return (clss->attr.ca.dfn);
1027 int (is_Class_type)(const ir_type *clss)
1029 return _is_class_type(clss);
1032 void set_class_mode(ir_type *tp, ir_mode *mode)
1034 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1035 assert(get_type_state(tp) == layout_fixed &&
1036 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1040 void set_class_size(ir_type *tp, unsigned size)
1046 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1048 ir_type *res = new_type(type_struct, NULL, db);
1051 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1056 ir_type *new_type_struct(ident *name)
1058 return new_d_type_struct (name, NULL);
1061 void free_struct_entities(ir_type *strct)
1064 assert(strct && (strct->type_op == type_struct));
1065 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1066 free_entity(get_struct_member(strct, i));
1069 void free_struct_attrs(ir_type *strct)
1071 assert(strct && (strct->type_op == type_struct));
1072 DEL_ARR_F(strct->attr.sa.members);
1075 ident *get_struct_ident(const ir_type *strct)
1077 assert(strct->type_op == type_struct);
1081 const char *get_struct_name(const ir_type *strct)
1083 if (get_struct_ident(strct) == NULL)
1085 return get_id_str(get_struct_ident(strct));
1088 int get_struct_n_members(const ir_type *strct)
1090 assert(strct && (strct->type_op == type_struct));
1091 return ARR_LEN(strct->attr.sa.members);
1094 static void add_struct_member(ir_type *strct, ir_entity *member)
1096 assert(strct && (strct->type_op == type_struct));
1097 assert(get_type_tpop(get_entity_type(member)) != type_method);
1098 assert(strct != get_entity_type(member) && "recursive type");
1099 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1102 ir_entity *get_struct_member(const ir_type *strct, int pos)
1104 assert(strct && (strct->type_op == type_struct));
1105 assert(pos >= 0 && pos < get_struct_n_members(strct));
1106 return strct->attr.sa.members[pos];
1109 int get_struct_member_index(const ir_type *strct, ir_entity *mem)
1112 assert(strct && (strct->type_op == type_struct));
1113 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1114 if (get_struct_member(strct, i) == mem)
1119 static void remove_struct_member(ir_type *strct, ir_entity *member)
1122 assert(strct && (strct->type_op == type_struct));
1123 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1124 if (strct->attr.sa.members[i] == member) {
1125 for (; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1126 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1127 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1132 int (is_Struct_type)(const ir_type *strct)
1134 return _is_struct_type(strct);
1137 void set_struct_mode(ir_type *tp, ir_mode *mode)
1139 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1140 assert(get_type_state(tp) == layout_fixed &&
1141 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1145 void set_struct_size(ir_type *tp, unsigned size)
1152 * Lazy construction of value argument / result representation.
1153 * Constructs a struct type and its member. The types of the members
1154 * are passed in the argument list.
1156 * @param name name of the type constructed
1157 * @param len number of fields
1158 * @param tps array of field types with length len
1160 static ir_type *build_value_type(char const* name, int len, tp_ent_pair *tps)
1163 ir_type *res = new_type_struct(new_id_from_str(name));
1164 res->flags |= tf_value_param_type;
1165 /* Remove type from type list. Must be treated differently than other types. */
1166 remove_irp_type(res);
1167 for (i = 0; i < len; i++) {
1168 ident *id = tps[i].param_name;
1170 /* use res as default if corresponding type is not yet set. */
1171 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1173 /* use the parameter name if specified */
1175 id = new_id_from_str("elt");
1177 tps[i].ent = new_entity(res, id, elt_type);
1178 set_entity_allocation(tps[i].ent, allocation_parameter);
1183 ir_type *new_d_type_method(int n_param, int n_res, type_dbg_info *db)
1187 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1188 res = new_type(type_method, mode_P_code, db);
1189 res->flags |= tf_layout_fixed;
1190 res->size = get_mode_size_bytes(mode_P_code);
1191 res->attr.ma.n_params = n_param;
1192 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1193 res->attr.ma.value_params = NULL;
1194 res->attr.ma.n_res = n_res;
1195 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1196 res->attr.ma.value_ress = NULL;
1197 res->attr.ma.variadicity = variadicity_non_variadic;
1198 res->attr.ma.first_variadic_param = -1;
1199 res->attr.ma.additional_properties = mtp_no_property;
1204 ir_type *new_type_method(int n_param, int n_res)
1206 return new_d_type_method(n_param, n_res, NULL);
1209 ir_type *clone_type_method(ir_type *tp)
1213 int n_params, n_res;
1216 assert(is_Method_type(tp));
1219 n_params = tp->attr.ma.n_params;
1220 n_res = tp->attr.ma.n_res;
1223 res = new_type(type_method, mode, db);
1225 res->flags = tp->flags;
1226 res->assoc_type = tp->assoc_type;
1227 res->size = tp->size;
1228 res->attr.ma.n_params = n_params;
1229 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1230 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1231 res->attr.ma.value_params = tp->attr.ma.value_params;
1232 res->attr.ma.n_res = n_res;
1233 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1234 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1235 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1236 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1237 res->attr.ma.first_variadic_param = tp->attr.ma.first_variadic_param;
1238 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1239 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1244 void free_method_entities(ir_type *method)
1247 assert(method && (method->type_op == type_method));
1250 void free_method_attrs(ir_type *method)
1252 assert(method && (method->type_op == type_method));
1253 free(method->attr.ma.params);
1254 free(method->attr.ma.res_type);
1255 /* cannot free it yet, type could be cloned ...
1256 if (method->attr.ma.value_params) {
1257 free_type_entities(method->attr.ma.value_params);
1258 free_type(method->attr.ma.value_params);
1261 if (method->attr.ma.value_ress) {
1262 free_type_entities(method->attr.ma.value_ress);
1263 free_type(method->attr.ma.value_ress);
1267 int (get_method_n_params)(const ir_type *method)
1269 return _get_method_n_params(method);
1272 ir_type *get_method_param_type(ir_type *method, int pos)
1275 assert(method && (method->type_op == type_method));
1276 assert(pos >= 0 && pos < get_method_n_params(method));
1277 res = method->attr.ma.params[pos].tp;
1278 assert(res != NULL && "empty method param type");
1282 void set_method_param_type(ir_type *method, int pos, ir_type *tp)
1284 assert(method && (method->type_op == type_method));
1285 assert(pos >= 0 && pos < get_method_n_params(method));
1286 method->attr.ma.params[pos].tp = tp;
1287 /* If information constructed set pass-by-value representation. */
1288 if (method->attr.ma.value_params) {
1289 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1290 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1294 ident *get_method_param_ident(ir_type *method, int pos)
1296 assert(method && (method->type_op == type_method));
1297 assert(pos >= 0 && pos < get_method_n_params(method));
1298 return method->attr.ma.params[pos].param_name;
1301 const char *get_method_param_name(ir_type *method, int pos)
1303 ident *id = get_method_param_ident(method, pos);
1304 return id ? get_id_str(id) : NULL;
1307 void set_method_param_ident(ir_type *method, int pos, ident *id)
1309 assert(method && (method->type_op == type_method));
1310 assert(pos >= 0 && pos < get_method_n_params(method));
1311 method->attr.ma.params[pos].param_name = id;
1314 ir_entity *get_method_value_param_ent(ir_type *method, int pos)
1316 assert(method && (method->type_op == type_method));
1317 assert(pos >= 0 && pos < get_method_n_params(method));
1319 if (!method->attr.ma.value_params) {
1320 /* parameter value type not created yet, build */
1321 method->attr.ma.value_params = build_value_type("<value param>",
1322 get_method_n_params(method), method->attr.ma.params);
1325 * build_value_type() sets the method->attr.ma.value_params type as default if
1328 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1329 && "param type not yet set");
1330 return method->attr.ma.params[pos].ent;
1333 void set_method_value_param_type(ir_type *method, ir_type *tp)
1337 assert(method && (method->type_op == type_method));
1338 assert(is_value_param_type(tp));
1339 assert(get_method_n_params(method) == get_struct_n_members(tp));
1341 method->attr.ma.value_params = tp;
1343 n = get_struct_n_members(tp);
1344 for (i = 0; i < n; i++) {
1345 ir_entity *ent = get_struct_member(tp, i);
1346 method->attr.ma.params[i].ent = ent;
1350 ir_type *get_method_value_param_type(const ir_type *method)
1352 assert(method && (method->type_op == type_method));
1353 return method->attr.ma.value_params;
1356 int (get_method_n_ress)(const ir_type *method)
1358 return _get_method_n_ress(method);
1361 ir_type *get_method_res_type(ir_type *method, int pos)
1364 assert(method && (method->type_op == type_method));
1365 assert(pos >= 0 && pos < get_method_n_ress(method));
1366 res = method->attr.ma.res_type[pos].tp;
1367 assert(res != NULL && "empty method return type");
1371 void set_method_res_type(ir_type *method, int pos, ir_type *tp)
1373 assert(method && (method->type_op == type_method));
1374 assert(pos >= 0 && pos < get_method_n_ress(method));
1375 /* set the result ir_type */
1376 method->attr.ma.res_type[pos].tp = tp;
1377 /* If information constructed set pass-by-value representation. */
1378 if (method->attr.ma.value_ress) {
1379 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1380 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1384 ir_entity *get_method_value_res_ent(ir_type *method, int pos)
1386 assert(method && (method->type_op == type_method));
1387 assert(pos >= 0 && pos < get_method_n_ress(method));
1389 if (!method->attr.ma.value_ress) {
1390 /* result value type not created yet, build */
1391 method->attr.ma.value_ress = build_value_type("<value result>",
1392 get_method_n_ress(method), method->attr.ma.res_type);
1395 * build_value_type() sets the method->attr.ma.value_ress type as default if
1398 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1399 && "result type not yet set");
1401 return method->attr.ma.res_type[pos].ent;
1404 ir_type *get_method_value_res_type(const ir_type *method)
1406 assert(method && (method->type_op == type_method));
1407 return method->attr.ma.value_ress;
1410 const char *get_variadicity_name(ir_variadicity vari)
1412 #define X(a) case a: return #a
1414 X(variadicity_non_variadic);
1415 X(variadicity_variadic);
1422 ir_variadicity get_method_variadicity(const ir_type *method)
1424 assert(method && (method->type_op == type_method));
1425 return method->attr.ma.variadicity;
1428 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1430 assert(method && (method->type_op == type_method));
1431 method->attr.ma.variadicity = vari;
1434 int get_method_first_variadic_param_index(const ir_type *method)
1436 assert(method && (method->type_op == type_method));
1438 if (method->attr.ma.variadicity == variadicity_non_variadic)
1441 if (method->attr.ma.first_variadic_param == -1)
1442 return get_method_n_params(method);
1443 return method->attr.ma.first_variadic_param;
1446 void set_method_first_variadic_param_index(ir_type *method, int index)
1448 assert(method && (method->type_op == type_method));
1449 assert(index >= 0 && index <= get_method_n_params(method));
1451 method->attr.ma.first_variadic_param = index;
1454 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1456 return _get_method_additional_properties(method);
1459 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1461 _set_method_additional_properties(method, mask);
1464 void (add_method_additional_property)(ir_type *method,
1465 mtp_additional_properties flag)
1467 _add_method_additional_properties(method, flag);
1470 unsigned (get_method_calling_convention)(const ir_type *method)
1472 return _get_method_calling_convention(method);
1475 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1477 _set_method_calling_convention(method, cc_mask);
1480 unsigned get_method_n_regparams(ir_type *method)
1482 unsigned cc = get_method_calling_convention(method);
1483 assert(IS_FASTCALL(cc));
1485 return cc & ~cc_bits;
1488 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1490 unsigned cc = get_method_calling_convention(method);
1491 assert(IS_FASTCALL(cc));
1493 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1496 int (is_Method_type)(const ir_type *method)
1498 return _is_method_type(method);
1502 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1504 ir_type *res = new_type(type_union, NULL, db);
1507 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1512 ir_type *new_type_union(ident *name)
1514 return new_d_type_union(name, NULL);
1517 void free_union_entities(ir_type *uni)
1520 assert(uni && (uni->type_op == type_union));
1521 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1522 free_entity(get_union_member(uni, i));
1525 void free_union_attrs (ir_type *uni)
1527 assert(uni && (uni->type_op == type_union));
1528 DEL_ARR_F(uni->attr.ua.members);
1531 ident *get_union_ident(const ir_type *uni)
1533 assert(uni->type_op == type_union);
1537 const char *get_union_name(const ir_type *uni)
1539 if (get_union_ident(uni) == NULL)
1541 return get_id_str(get_union_ident(uni));
1544 int get_union_n_members(const ir_type *uni)
1546 assert(uni && (uni->type_op == type_union));
1547 return ARR_LEN(uni->attr.ua.members);
1550 static void add_union_member(ir_type *uni, ir_entity *member)
1552 assert(uni && (uni->type_op == type_union));
1553 assert(uni != get_entity_type(member) && "recursive type");
1554 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1557 ir_entity *get_union_member(const ir_type *uni, int pos)
1559 assert(uni && (uni->type_op == type_union));
1560 assert(pos >= 0 && pos < get_union_n_members(uni));
1561 return uni->attr.ua.members[pos];
1564 int get_union_member_index(const ir_type *uni, ir_entity *mem)
1567 assert(uni && (uni->type_op == type_union));
1568 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1569 if (get_union_member(uni, i) == mem)
1575 static void remove_union_member(ir_type *uni, ir_entity *member)
1578 assert(uni && (uni->type_op == type_union));
1579 for (i = 0; i < (ARR_LEN(uni->attr.ua.members)); i++) {
1580 if (uni->attr.ua.members[i] == member) {
1581 for (; i < (ARR_LEN(uni->attr.ua.members))-1; i++)
1582 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1583 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1589 int (is_Union_type)(const ir_type *uni)
1591 return _is_union_type(uni);
1594 void set_union_size(ir_type *tp, unsigned size)
1601 ir_type *new_d_type_array(int n_dimensions, ir_type *element_type,
1607 ir_graph *irg = get_const_code_irg();
1609 assert(!is_Method_type(element_type));
1611 res = new_type(type_array, NULL, db);
1612 res->attr.aa.n_dimensions = n_dimensions;
1613 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1614 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1615 res->attr.aa.order = XMALLOCNZ(int, n_dimensions);
1617 unk = new_r_Unknown(irg, mode_Iu);
1618 for (i = 0; i < n_dimensions; i++) {
1619 res->attr.aa.lower_bound[i] =
1620 res->attr.aa.upper_bound[i] = unk;
1621 res->attr.aa.order[i] = i;
1624 res->attr.aa.element_type = element_type;
1625 res->attr.aa.element_ent
1626 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1627 res->attr.aa.element_ent->owner = res;
1633 ir_type *new_type_array(int n_dimensions, ir_type *element_type)
1635 return new_d_type_array(n_dimensions, element_type, NULL);
1638 void free_array_automatic_entities(ir_type *array)
1640 assert(array && (array->type_op == type_array));
1641 free_entity(get_array_element_entity(array));
1644 void free_array_entities (ir_type *array)
1647 assert(array && (array->type_op == type_array));
1650 void free_array_attrs (ir_type *array)
1652 assert(array && (array->type_op == type_array));
1653 free(array->attr.aa.lower_bound);
1654 free(array->attr.aa.upper_bound);
1655 free(array->attr.aa.order);
1658 /* manipulate private fields of array ir_type */
1659 int get_array_n_dimensions (const ir_type *array)
1661 assert(array && (array->type_op == type_array));
1662 return array->attr.aa.n_dimensions;
1665 void set_array_bounds(ir_type *array, int dimension, ir_node *lower_bound,
1666 ir_node *upper_bound)
1668 assert(array && (array->type_op == type_array));
1669 assert(lower_bound && "lower_bound node may not be NULL.");
1670 assert(upper_bound && "upper_bound node may not be NULL.");
1671 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1672 array->attr.aa.lower_bound[dimension] = lower_bound;
1673 array->attr.aa.upper_bound[dimension] = upper_bound;
1676 void set_array_bounds_int(ir_type *array, int dimension, int lower_bound,
1679 ir_graph *irg = get_const_code_irg();
1680 set_array_bounds(array, dimension,
1681 new_r_Const_long(irg, mode_Iu, lower_bound),
1682 new_r_Const_long(irg, mode_Iu, upper_bound));
1685 void set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound)
1687 assert(array && (array->type_op == type_array));
1688 assert(lower_bound && "lower_bound node may not be NULL.");
1689 array->attr.aa.lower_bound[dimension] = lower_bound;
1692 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound)
1694 ir_graph *irg = get_const_code_irg();
1695 set_array_lower_bound(array, dimension,
1696 new_r_Const_long(irg, mode_Iu, lower_bound));
1699 void set_array_upper_bound(ir_type *array, int dimension, ir_node *upper_bound)
1701 assert(array && (array->type_op == type_array));
1702 assert(upper_bound && "upper_bound node may not be NULL.");
1703 array->attr.aa.upper_bound[dimension] = upper_bound;
1706 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound)
1708 ir_graph *irg = get_const_code_irg();
1709 set_array_upper_bound(array, dimension,
1710 new_r_Const_long(irg, mode_Iu, upper_bound));
1713 int has_array_lower_bound(const ir_type *array, int dimension)
1715 assert(array && (array->type_op == type_array));
1716 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1719 ir_node *get_array_lower_bound(const ir_type *array, int dimension)
1721 assert(array && (array->type_op == type_array));
1722 return array->attr.aa.lower_bound[dimension];
1725 long get_array_lower_bound_int(const ir_type *array, int dimension)
1728 assert(array && (array->type_op == type_array));
1729 node = array->attr.aa.lower_bound[dimension];
1730 assert(is_Const(node));
1731 return get_tarval_long(get_Const_tarval(node));
1734 int has_array_upper_bound(const ir_type *array, int dimension)
1736 assert(array && (array->type_op == type_array));
1737 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1740 ir_node *get_array_upper_bound(const ir_type *array, int dimension)
1742 assert(array && (array->type_op == type_array));
1743 return array->attr.aa.upper_bound[dimension];
1746 long get_array_upper_bound_int(const ir_type *array, int dimension)
1749 assert(array && (array->type_op == type_array));
1750 node = array->attr.aa.upper_bound[dimension];
1751 assert(is_Const(node));
1752 return get_tarval_long(get_Const_tarval(node));
1755 void set_array_order(ir_type *array, int dimension, int order)
1757 assert(array && (array->type_op == type_array));
1758 array->attr.aa.order[dimension] = order;
1761 int get_array_order(const ir_type *array, int dimension)
1763 assert(array && (array->type_op == type_array));
1764 return array->attr.aa.order[dimension];
1767 int find_array_dimension(const ir_type *array, int order)
1771 assert(array && (array->type_op == type_array));
1773 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1774 if (array->attr.aa.order[dim] == order)
1780 void set_array_element_type(ir_type *array, ir_type *tp)
1782 assert(array && (array->type_op == type_array));
1783 assert(!is_Method_type(tp));
1784 array->attr.aa.element_type = tp;
1787 ir_type *get_array_element_type(const ir_type *array)
1789 assert(array && (array->type_op == type_array));
1790 return array->attr.aa.element_type;
1793 void set_array_element_entity(ir_type *array, ir_entity *ent)
1795 assert(array && (array->type_op == type_array));
1796 assert((get_entity_type(ent)->type_op != type_method));
1797 array->attr.aa.element_ent = ent;
1798 array->attr.aa.element_type = get_entity_type(ent);
1801 ir_entity *get_array_element_entity(const ir_type *array)
1803 assert(array && (array->type_op == type_array));
1804 return array->attr.aa.element_ent;
1807 int (is_Array_type)(const ir_type *array)
1809 return _is_array_type(array);
1812 void set_array_size(ir_type *tp, unsigned size)
1814 /* FIXME: Here we should make some checks with the element type size */
1819 ir_type *new_d_type_enumeration(ident *name, int n_enums, type_dbg_info *db)
1823 assert(n_enums >= 0);
1824 res = new_type(type_enumeration, NULL, db);
1826 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1831 ir_type *new_type_enumeration(ident *name, int n_enums)
1833 return new_d_type_enumeration(name, n_enums, NULL);
1836 void free_enumeration_entities(ir_type *enumeration)
1839 assert(enumeration && (enumeration->type_op == type_enumeration));
1842 void free_enumeration_attrs(ir_type *enumeration)
1844 assert(enumeration && (enumeration->type_op == type_enumeration));
1845 DEL_ARR_F(enumeration->attr.ea.enumer);
1848 ident *get_enumeration_ident(const ir_type *enumeration)
1850 assert(enumeration->type_op == type_enumeration);
1851 return enumeration->name;
1854 const char *get_enumeration_name(const ir_type *enumeration)
1856 if (get_enumeration_ident(enumeration) == NULL)
1858 return get_id_str(get_enumeration_ident(enumeration));
1861 int get_enumeration_n_enums(const ir_type *enumeration)
1863 assert(enumeration && (enumeration->type_op == type_enumeration));
1864 return ARR_LEN(enumeration->attr.ea.enumer);
1867 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid,
1870 assert(0 <= pos && pos < ARR_LEN(enumeration->attr.ea.enumer));
1871 enumeration->attr.ea.enumer[pos].nameid = nameid;
1872 enumeration->attr.ea.enumer[pos].value = con;
1873 enumeration->attr.ea.enumer[pos].owner = enumeration;
1876 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos)
1878 assert(enumeration && (enumeration->type_op == type_enumeration));
1879 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1880 return &enumeration->attr.ea.enumer[pos];
1883 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1885 return enum_cnst->owner;
1888 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1890 enum_cnst->value = con;
1893 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1895 return enum_cnst->value;
1898 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1900 enum_cnst->nameid = id;
1903 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1905 return enum_cnst->nameid;
1908 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1910 return get_id_str(enum_cnst->nameid);
1913 int (is_Enumeration_type)(const ir_type *enumeration)
1915 return _is_enumeration_type(enumeration);
1918 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1920 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1921 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1922 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1924 tp->size = get_mode_size_bytes(mode);
1930 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1935 if (is_Method_type(points_to) || is_code_type(points_to)) {
1941 res = new_type(type_pointer, mode, db);
1942 res->attr.pa.points_to = points_to;
1943 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1944 res->size = get_mode_size_bytes(res->mode);
1945 res->flags |= tf_layout_fixed;
1950 ir_type *new_type_pointer(ir_type *points_to)
1952 return new_d_type_pointer(points_to, NULL);
1955 void free_pointer_entities(ir_type *pointer)
1958 assert(pointer && (pointer->type_op == type_pointer));
1961 void free_pointer_attrs(ir_type *pointer)
1964 assert(pointer && (pointer->type_op == type_pointer));
1967 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1969 assert(pointer && (pointer->type_op == type_pointer));
1970 pointer->attr.pa.points_to = tp;
1973 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1975 assert(pointer && (pointer->type_op == type_pointer));
1976 return pointer->attr.pa.points_to;
1979 int (is_Pointer_type)(const ir_type *pointer)
1981 return _is_pointer_type(pointer);
1984 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1986 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1987 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1988 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1990 tp->size = get_mode_size_bytes(mode);
1994 ir_type *find_pointer_type_to_type(ir_type *tp)
1996 int i, n = get_irp_n_types();
1997 for (i = 0; i < n; ++i) {
1998 ir_type *found = get_irp_type(i);
1999 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
2002 return firm_unknown_type;
2007 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
2009 ir_type *res = new_type(type_primitive, mode, db);
2010 res->size = get_mode_size_bytes(mode);
2011 res->flags |= tf_layout_fixed;
2012 res->attr.ba.base_type = NULL;
2017 ir_type *new_type_primitive(ir_mode *mode)
2019 return new_d_type_primitive(mode, NULL);
2022 int (is_Primitive_type)(const ir_type *primitive)
2024 return _is_primitive_type(primitive);
2027 void set_primitive_mode(ir_type *tp, ir_mode *mode)
2029 /* Modes of primitives must be data */
2030 assert(mode_is_data(mode));
2032 /* For primitive size depends on the mode. */
2033 tp->size = get_mode_size_bytes(mode);
2037 ir_type *get_primitive_base_type(const ir_type *tp)
2039 assert(is_Primitive_type(tp));
2040 return tp->attr.ba.base_type;
2043 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
2045 assert(is_Primitive_type(tp));
2046 tp->attr.ba.base_type = base_tp;
2051 int (is_atomic_type)(const ir_type *tp)
2053 return _is_atomic_type(tp);
2056 int get_compound_n_members(const ir_type *tp)
2058 const tp_op *op = get_type_tpop(tp);
2061 if (op->ops.get_n_members)
2062 res = op->ops.get_n_members(tp);
2064 assert(0 && "no member count for this type");
2069 ir_entity *get_compound_member(const ir_type *tp, int pos)
2071 const tp_op *op = get_type_tpop(tp);
2072 ir_entity *res = NULL;
2074 if (op->ops.get_member)
2075 res = op->ops.get_member(tp, pos);
2077 assert(0 && "no members in this type");
2082 int get_compound_member_index(const ir_type *tp, ir_entity *member)
2084 const tp_op *op = get_type_tpop(tp);
2087 if (op->ops.get_member_index)
2088 index = op->ops.get_member_index(tp, member);
2090 assert(0 && "no members in this type");
2095 int is_compound_type(const ir_type *tp)
2097 assert(tp && tp->kind == k_type);
2098 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2101 ident *get_compound_ident(const ir_type *tp)
2103 assert(is_compound_type(tp));
2107 const char *get_compound_name(const ir_type *tp)
2109 if (get_compound_ident(tp) == NULL)
2111 return get_id_str(get_compound_ident(tp));
2114 void remove_compound_member(ir_type *compound, ir_entity *entity)
2116 switch (get_type_tpop_code(compound)) {
2117 case tpo_class: remove_class_member(compound, entity); break;
2118 case tpo_struct: remove_struct_member(compound, entity); break;
2119 case tpo_union: remove_union_member(compound, entity); break;
2121 panic("argument for remove_compound_member not a compound type");
2125 void add_compound_member(ir_type *compound, ir_entity *entity)
2127 switch (get_type_tpop_code(compound)) {
2128 case tpo_class: add_class_member(compound, entity); break;
2129 case tpo_struct: add_struct_member(compound, entity); break;
2130 case tpo_union: add_union_member(compound, entity); break;
2132 panic("argument for add_compound_member not a compound type");
2138 int is_code_type(const ir_type *tp)
2140 assert(tp && tp->kind == k_type);
2141 return tp->type_op == tpop_code;
2144 int is_frame_type(const ir_type *tp)
2146 return tp->flags & tf_frame_type;
2149 int is_value_param_type(const ir_type *tp)
2151 return tp->flags & tf_value_param_type;
2154 int is_lowered_type(const ir_type *tp)
2156 return tp->flags & tf_lowered_type;
2159 ir_type *new_type_value(void)
2161 ir_type *res = new_type_struct(new_id_from_str("<value_type>"));
2163 res->flags |= tf_value_param_type;
2165 /* Remove type from type list. Must be treated differently than other types. */
2166 remove_irp_type(res);
2171 ir_type *new_type_frame(void)
2173 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
2175 res->flags |= tf_frame_type;
2177 /* Remove type from type list. Must be treated differently than other types. */
2178 remove_irp_type(res);
2180 /* It is not possible to derive from the frame type. Set the final flag. */
2181 set_class_final(res, 1);
2186 ir_type *clone_frame_type(ir_type *type)
2191 assert(is_frame_type(type));
2192 /* the entity link resource should be allocated if this function is called */
2193 assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2195 res = new_type_frame();
2196 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2197 ir_entity *ent = get_class_member(type, i);
2198 ir_entity *nent = copy_entity_own(ent, res);
2199 set_entity_link(ent, nent);
2200 set_entity_link(nent, ent);
2205 void set_lowered_type(ir_type *tp, ir_type *lowered_type)
2207 assert(is_type(tp) && is_type(lowered_type));
2208 lowered_type->flags |= tf_lowered_type;
2209 tp->assoc_type = lowered_type;
2210 lowered_type->assoc_type = tp;
2213 ir_type *get_associated_type(const ir_type *tp)
2215 return tp->assoc_type;
2218 void set_default_size(ir_type *tp, unsigned size)
2223 void default_layout_compound_type(ir_type *type)
2226 int n = get_compound_n_members(type);
2228 unsigned align_all = 1;
2230 for (i = 0; i < n; ++i) {
2231 ir_entity *entity = get_compound_member(type, i);
2232 ir_type *entity_type = get_entity_type(entity);
2236 if (is_Method_type(entity_type))
2239 assert(get_type_state(entity_type) == layout_fixed);
2240 align = get_type_alignment_bytes(entity_type);
2241 align_all = align > align_all ? align : align_all;
2242 misalign = (align ? size % align : 0);
2243 size += (misalign ? align - misalign : 0);
2245 set_entity_offset(entity, size);
2246 if (!is_Union_type(type)) {
2247 size += get_type_size_bytes(entity_type);
2250 if (align_all > 0 && size % align_all) {
2251 size += align_all - (size % align_all);
2253 if (align_all > get_type_alignment_bytes(type)) {
2254 set_type_alignment_bytes(type, align_all);
2256 set_type_size_bytes(type, size);
2257 set_type_state(type, layout_fixed);
2260 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2267 unsigned frame_align;
2268 int i, offset, frame_size;
2269 static unsigned area_cnt = 0;
2270 static ir_type *a_byte = NULL;
2272 assert(is_frame_type(frame_type));
2273 assert(get_type_state(frame_type) == layout_fixed);
2274 assert(get_type_alignment_bytes(frame_type) > 0);
2275 set_type_state(frame_type, layout_undefined);
2278 a_byte = new_type_primitive(mode_Bu);
2280 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2281 name = new_id_from_str(buf);
2283 /* align the size */
2284 frame_align = get_type_alignment_bytes(frame_type);
2285 size = (size + frame_align - 1) & ~(frame_align - 1);
2287 tp = new_type_array(1, a_byte);
2288 set_array_bounds_int(tp, 0, 0, size);
2289 set_type_alignment_bytes(tp, alignment);
2291 frame_size = get_type_size_bytes(frame_type);
2293 /* fix all offsets so far */
2294 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2295 ir_entity *ent = get_class_member(frame_type, i);
2297 set_entity_offset(ent, get_entity_offset(ent) + size);
2299 /* calculate offset and new type size */
2303 /* increase size to match alignment... */
2304 if (alignment > frame_align) {
2305 frame_align = alignment;
2306 set_type_alignment_bytes(frame_type, frame_align);
2307 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2310 /* calculate offset and new type size */
2311 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2312 frame_size = offset + size;
2315 area = new_entity(frame_type, name, tp);
2316 set_entity_offset(area, offset);
2317 set_type_size_bytes(frame_type, frame_size);
2319 /* mark this entity as compiler generated */
2320 set_entity_compiler_generated(area, 1);
2322 set_type_state(frame_type, layout_fixed);
2326 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2330 type_dbg_info *tdbgi = get_type_dbg_info(type);
2331 if (tdbgi != NULL) {
2332 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2336 /* we have to construct some name... */
2337 switch (get_type_tpop_code(type)) {
2338 case tpo_uninitialized:
2341 snprintf(buffer, buffer_size, "code");
2345 id = get_class_ident(type);
2346 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2350 id = get_struct_ident(type);
2351 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2355 id = get_union_ident(type);
2356 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2359 case tpo_enumeration:
2360 id = get_enumeration_ident(type);
2361 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2365 snprintf(buffer, buffer_size, "unknown type");
2369 p = snprintf(buffer, buffer_size, "pointer to ");
2372 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2376 p = snprintf(buffer, buffer_size, "array of ");
2379 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2383 id = get_mode_ident(get_type_mode(type));
2384 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2388 snprintf(buffer, buffer_size, "none");
2391 /* TODO: we should print argument and return types here... */
2392 snprintf(buffer, buffer_size, "method type");
2395 snprintf(buffer, buffer_size, "invalid type");