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.
45 * @see type_t.h type tpop
72 /*-----------------------------------------------------------------*/
74 /*-----------------------------------------------------------------*/
76 ir_type *firm_none_type; ir_type *get_none_type(void) { return firm_none_type; }
77 ir_type *firm_unknown_type; ir_type *get_unknown_type(void) { return firm_unknown_type; }
80 /* Suffixes added to types used for pass-by-value representations. */
81 static ident *value_params_suffix = NULL;
82 static ident *value_ress_suffix = NULL;
84 /** The default calling convention for method types. */
85 static unsigned default_cc_mask;
87 /* return the default calling convention for method types */
88 unsigned get_default_cc_mask(void) {
89 return default_cc_mask;
92 /* Initialize the type module. */
93 void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask) {
94 default_cc_mask = def_cc_mask;
95 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
96 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
98 /* construct none and unknown type. */
99 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"), builtin_db);
100 set_type_size_bytes(firm_none_type, 0);
101 set_type_state (firm_none_type, layout_fixed);
102 remove_irp_type(firm_none_type);
104 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
105 set_type_size_bytes(firm_unknown_type, 0);
106 set_type_state (firm_unknown_type, layout_fixed);
107 remove_irp_type(firm_unknown_type);
110 /** the global type visited flag */
111 ir_visited_t firm_type_visited;
113 void (set_master_type_visited)(ir_visited_t val) { _set_master_type_visited(val); }
114 ir_visited_t (get_master_type_visited)(void) { return _get_master_type_visited(); }
115 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
118 * Creates a new type representation.
121 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
125 assert(type_op != type_id);
126 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
128 node_size = offsetof(ir_type, attr) + type_op->attr_size;
129 res = xmalloc(node_size);
130 memset(res, 0, node_size);
133 res->type_op = type_op;
136 res->visibility = visibility_external_allocated;
137 res->flags = tf_none;
143 res->assoc_type = NULL;
145 res->nr = get_irp_new_node_nr();
146 #endif /* defined DEBUG_libfirm */
148 add_irp_type(res); /* Remember the new type global. */
153 void free_type(ir_type *tp) {
154 const tp_op *op = get_type_tpop(tp);
156 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
158 /* Remove from list of all types */
160 /* Free the attributes of the type. */
162 /* Free entities automatically allocated with the ir_type */
163 if (op->ops.free_auto_entities)
164 op->ops.free_auto_entities(tp);
165 /* And now the type itself... */
170 void free_type_entities(ir_type *tp) {
171 const tp_op *tpop = get_type_tpop(tp);
173 if (tpop->ops.free_entities)
174 tpop->ops.free_entities(tp);
177 void free_type_attrs(ir_type *tp) {
178 const tp_op *tpop = get_type_tpop(tp);
180 if (tpop->ops.free_attrs)
181 tpop->ops.free_attrs(tp);
184 /* set/get the link field */
185 void *(get_type_link)(const ir_type *tp) {
186 return _get_type_link(tp);
189 void (set_type_link)(ir_type *tp, void *l) {
190 _set_type_link(tp, l);
193 const tp_op *(get_type_tpop)(const ir_type *tp) {
194 return _get_type_tpop(tp);
197 ident *(get_type_tpop_nameid)(const ir_type *tp) {
198 return _get_type_tpop_nameid(tp);
201 const char* get_type_tpop_name(const ir_type *tp) {
202 assert(tp && tp->kind == k_type);
203 return get_id_str(tp->type_op->name);
206 tp_opcode (get_type_tpop_code)(const ir_type *tp) {
207 return _get_type_tpop_code(tp);
210 ir_mode *(get_type_mode)(const ir_type *tp) {
211 return _get_type_mode(tp);
214 void set_type_mode(ir_type *tp, ir_mode *mode) {
215 const tp_op *tpop = get_type_tpop(tp);
217 if (tpop->ops.set_type_mode)
218 tpop->ops.set_type_mode(tp, mode);
220 assert(0 && "setting a mode is NOT allowed for this type");
223 ident *(get_type_ident)(const ir_type *tp) {
224 return _get_type_ident(tp);
227 void (set_type_ident)(ir_type *tp, ident* id) {
228 _set_type_ident(tp, id);
231 /* Outputs a unique number for this node */
232 long get_type_nr(const ir_type *tp) {
237 return (long)PTR_TO_INT(tp);
241 const char *get_type_name(const ir_type *tp) {
242 assert(tp && tp->kind == k_type);
243 return (get_id_str(tp->name));
246 unsigned (get_type_size_bytes)(const ir_type *tp) {
247 return _get_type_size_bytes(tp);
250 ir_visibility get_type_visibility(const ir_type *tp) {
252 visibility res = visibility_local;
253 if (is_compound_type(tp)) {
255 if (is_Array_type(tp)) {
256 ir_entity *mem = get_array_element_entity(tp);
257 if (get_entity_visibility(mem) != visibility_local)
258 res = visibility_external_visible;
260 int i, n_mems = get_compound_n_members(tp);
261 for (i = 0; i < n_mems; ++i) {
262 ir_entity *mem = get_compound_member(tp, i);
263 if (get_entity_visibility(mem) != visibility_local)
264 res = visibility_external_visible;
271 return tp->visibility;
274 void set_type_visibility(ir_type *tp, ir_visibility v) {
277 /* check for correctness */
278 if (v != visibility_external_allocated) {
279 visibility res = visibility_local;
280 if (is_compound_type(tp)) {
281 if (is_Array_type(tp)) {
282 ir_entity *mem = get_array_element_entity(tp);
283 if (get_entity_visibility(mem) > res)
284 res = get_entity_visibility(mem);
286 int i, n_mems = get_compound_n_members(tp);
287 for (i = 0; i < n_mems; ++i) {
288 ir_entity *mem = get_compound_member(tp, i);
289 if (get_entity_visibility(mem) > res)
290 res = get_entity_visibility(mem);
301 set_type_size_bytes(ir_type *tp, unsigned size) {
302 const tp_op *tpop = get_type_tpop(tp);
304 if (tpop->ops.set_type_size)
305 tpop->ops.set_type_size(tp, size);
307 assert(0 && "Cannot set size for this type");
310 unsigned get_type_alignment_bytes(ir_type *tp) {
316 /* alignment NOT set calculate it "on demand" */
318 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
319 else if (is_Array_type(tp))
320 align = get_type_alignment_bytes(get_array_element_type(tp));
321 else if (is_compound_type(tp)) {
322 int i, n = get_compound_n_members(tp);
325 for (i = 0; i < n; ++i) {
326 ir_type *t = get_entity_type(get_compound_member(tp, i));
327 unsigned a = get_type_alignment_bytes(t);
332 } else if (is_Method_type(tp)) {
343 set_type_alignment_bytes(ir_type *tp, unsigned align) {
344 assert(tp && tp->kind == k_type);
345 /* Methods don't have an alignment. */
346 if (tp->type_op != type_method) {
351 /* Returns a human readable string for the enum entry. */
352 const char *get_type_state_name(ir_type_state s) {
353 #define X(a) case a: return #a;
363 ir_type_state (get_type_state)(const ir_type *tp) {
364 return _get_type_state(tp);
368 set_type_state(ir_type *tp, ir_type_state state) {
369 assert(tp && tp->kind == k_type);
371 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
372 (tp->type_op == type_method))
375 /* Just a correctness check: */
376 if (state == layout_fixed) {
378 switch (get_type_tpop_code(tp)) {
380 if (tp != get_glob_type()) {
381 int n_mem = get_class_n_members(tp);
382 for (i = 0; i < n_mem; i++) {
383 assert(get_entity_offset(get_class_member(tp, i)) > -1);
385 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
386 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
392 for (i = 0; i < get_struct_n_members(tp); i++) {
393 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
394 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
403 Assure that only innermost dimension is dynamic? */
405 case tpo_enumeration:
407 assert(get_type_mode != NULL);
408 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
409 ir_enum_const *ec = get_enumeration_const(tp, i);
410 tarval *tv = get_enumeration_value(ec);
411 assert(tv != NULL && tv != tarval_bad);
418 if (state == layout_fixed)
419 tp->flags |= tf_layout_fixed;
421 tp->flags &= ~tf_layout_fixed;
424 ir_visited_t (get_type_visited)(const ir_type *tp) {
425 return _get_type_visited(tp);
428 void (set_type_visited)(ir_type *tp, ir_visited_t num) {
429 _set_type_visited(tp, num);
432 /* Sets visited field in type to type_visited. */
433 void (mark_type_visited)(ir_type *tp) {
434 _mark_type_visited(tp);
437 int (type_visited)(const ir_type *tp) {
438 return _type_visited(tp);
441 int (type_not_visited)(const ir_type *tp) {
442 return _type_not_visited(tp);
445 dbg_info *(get_type_dbg_info)(const ir_type *tp) {
446 return _get_type_dbg_info(tp);
449 void (set_type_dbg_info)(ir_type *tp, dbg_info *db) {
450 _set_type_dbg_info(tp, db);
453 int (is_type)(const void *thing) {
454 return _is_type(thing);
457 /* Checks whether two types are structural equal.*/
458 int equal_type(ir_type *typ1, ir_type *typ2) {
463 if (typ1 == typ2) return 1;
465 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
466 (get_type_ident(typ1) != get_type_ident(typ2)) ||
467 (get_type_mode(typ1) != get_type_mode(typ2)) ||
468 (get_type_state(typ1) != get_type_state(typ2)))
470 if ((get_type_state(typ1) == layout_fixed) &&
471 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
474 switch (get_type_tpop_code(typ1)) {
476 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
477 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
478 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
479 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
480 /** Compare the members **/
481 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
482 /* First sort the members of typ2 */
483 for (i = 0; i < get_class_n_members(typ1); i++) {
484 ir_entity *e1 = get_class_member(typ1, i);
485 for (j = 0; j < get_class_n_members(typ2); j++) {
486 ir_entity *e2 = get_class_member(typ2, j);
487 if (get_entity_name(e1) == get_entity_name(e2))
491 for (i = 0; i < get_class_n_members(typ1); i++) {
492 if (!m[i] || /* Found no counterpart */
493 !equal_entity(get_class_member(typ1, i), m[i]))
496 /** Compare the supertypes **/
497 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
498 /* First sort the supertypes of typ2 */
499 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
500 ir_type *t1 = get_class_supertype(typ1, i);
501 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
502 ir_type *t2 = get_class_supertype(typ2, j);
503 if (get_type_ident(t2) == get_type_ident(t1))
507 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
508 if (!t[i] || /* Found no counterpart */
509 get_class_supertype(typ1, i) != t[i])
515 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
516 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
517 /* First sort the members of lt */
518 for (i = 0; i < get_struct_n_members(typ1); i++) {
519 ir_entity *e1 = get_struct_member(typ1, i);
520 for (j = 0; j < get_struct_n_members(typ2); j++) {
521 ir_entity *e2 = get_struct_member(typ2, j);
522 if (get_entity_name(e1) == get_entity_name(e2))
526 for (i = 0; i < get_struct_n_members(typ1); i++) {
527 if (!m[i] || /* Found no counterpart */
528 !equal_entity(get_struct_member(typ1, i), m[i]))
534 int n_param1, n_param2;
536 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
537 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
538 if (get_method_calling_convention(typ1) !=
539 get_method_calling_convention(typ2)) return 0;
541 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
542 n_param1 = get_method_n_params(typ1);
543 n_param2 = get_method_n_params(typ2);
545 n_param1 = get_method_first_variadic_param_index(typ1);
546 n_param2 = get_method_first_variadic_param_index(typ2);
549 if (n_param1 != n_param2) return 0;
551 for (i = 0; i < n_param1; i++) {
552 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
555 for (i = 0; i < get_method_n_ress(typ1); i++) {
556 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
562 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
563 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
564 /* First sort the members of lt */
565 for (i = 0; i < get_union_n_members(typ1); i++) {
566 ir_entity *e1 = get_union_member(typ1, i);
567 for (j = 0; j < get_union_n_members(typ2); j++) {
568 ir_entity *e2 = get_union_member(typ2, j);
569 if (get_entity_name(e1) == get_entity_name(e2))
573 for (i = 0; i < get_union_n_members(typ1); i++) {
574 if (!m[i] || /* Found no counterpart */
575 !equal_entity(get_union_member(typ1, i), m[i]))
581 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
583 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
585 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
586 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
587 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
589 if (get_array_order(typ1, i) != get_array_order(typ2, i))
590 assert(0 && "type compare with different dimension orders not implemented");
594 case tpo_enumeration:
595 assert(0 && "enumerations not implemented");
599 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
611 /* Checks whether two types are structural comparable. */
612 int smaller_type(ir_type *st, ir_type *lt) {
614 int i, j, n_st_members;
616 if (st == lt) return 1;
618 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
621 switch(get_type_tpop_code(st)) {
623 return is_SubClass_of(st, lt);
626 n_st_members = get_struct_n_members(st);
627 if (n_st_members != get_struct_n_members(lt))
630 m = ALLOCANZ(ir_entity*, n_st_members);
631 /* First sort the members of lt */
632 for (i = 0; i < n_st_members; ++i) {
633 ir_entity *se = get_struct_member(st, i);
634 int n = get_struct_n_members(lt);
635 for (j = 0; j < n; ++j) {
636 ir_entity *le = get_struct_member(lt, j);
637 if (get_entity_name(le) == get_entity_name(se))
641 for (i = 0; i < n_st_members; i++) {
642 if (!m[i] || /* Found no counterpart */
643 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
649 int n_param1, n_param2;
651 /** FIXME: is this still 1? */
652 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
653 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
654 if (get_method_calling_convention(st) !=
655 get_method_calling_convention(lt)) return 0;
657 if (get_method_variadicity(st) == variadicity_non_variadic) {
658 n_param1 = get_method_n_params(st);
659 n_param2 = get_method_n_params(lt);
661 n_param1 = get_method_first_variadic_param_index(st);
662 n_param2 = get_method_first_variadic_param_index(lt);
665 if (n_param1 != n_param2) return 0;
667 for (i = 0; i < get_method_n_params(st); i++) {
668 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
671 for (i = 0; i < get_method_n_ress(st); i++) {
672 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
678 n_st_members = get_union_n_members(st);
679 if (n_st_members != get_union_n_members(lt)) return 0;
680 m = ALLOCANZ(ir_entity*, n_st_members);
681 /* First sort the members of lt */
682 for (i = 0; i < n_st_members; ++i) {
683 ir_entity *se = get_union_member(st, i);
684 int n = get_union_n_members(lt);
685 for (j = 0; j < n; ++j) {
686 ir_entity *le = get_union_member(lt, j);
687 if (get_entity_name(le) == get_entity_name(se))
691 for (i = 0; i < n_st_members; ++i) {
692 if (!m[i] || /* Found no counterpart */
693 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
699 ir_type *set, *let; /* small/large elt. ir_type */
700 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
702 set = get_array_element_type(st);
703 let = get_array_element_type(lt);
705 /* If the element types are different, set must be convertible
706 to let, and they must have the same size so that address
707 computations work out. To have a size the layout must
709 if ((get_type_state(set) != layout_fixed) ||
710 (get_type_state(let) != layout_fixed))
712 if (!smaller_type(set, let) ||
713 get_type_size_bytes(set) != get_type_size_bytes(let))
716 for(i = 0; i < get_array_n_dimensions(st); i++) {
717 if (get_array_lower_bound(lt, i))
718 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
720 if (get_array_upper_bound(lt, i))
721 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
726 case tpo_enumeration:
727 assert(0 && "enumerations not implemented");
731 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
736 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
745 /*-----------------------------------------------------------------*/
747 /*-----------------------------------------------------------------*/
749 /* create a new class ir_type */
750 ir_type *new_d_type_class (ident *name, dbg_info *db) {
753 res = new_type(type_class, NULL, name, db);
755 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
756 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
757 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
758 res->attr.ca.peculiarity = peculiarity_existent;
759 res->attr.ca.type_info = NULL;
760 res->attr.ca.vtable_size = 0;
761 res->attr.ca.clss_flags = cf_none;
762 res->attr.ca.dfn = 0;
767 ir_type *new_type_class (ident *name) {
768 return new_d_type_class (name, NULL);
771 /* free all entities of a class */
772 void free_class_entities(ir_type *clss) {
774 assert(clss && (clss->type_op == type_class));
775 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
776 free_entity(get_class_member(clss, i));
777 /* do NOT free the type info here. It belongs to another class */
780 void free_class_attrs(ir_type *clss) {
781 assert(clss && (clss->type_op == type_class));
782 DEL_ARR_F(clss->attr.ca.members);
783 DEL_ARR_F(clss->attr.ca.subtypes);
784 DEL_ARR_F(clss->attr.ca.supertypes);
787 /* manipulate private fields of class type */
788 void add_class_member(ir_type *clss, ir_entity *member) {
789 assert(clss && (clss->type_op == type_class));
790 assert(clss != get_entity_type(member) && "recursive type");
791 assert(get_type_state(clss) != layout_fixed);
792 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
795 int (get_class_n_members)(const ir_type *clss) {
796 return _get_class_n_members(clss);
799 int get_class_member_index(const ir_type *clss, ir_entity *mem) {
801 assert(clss && (clss->type_op == type_class));
802 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
803 if (get_class_member(clss, i) == mem)
808 ir_entity *(get_class_member)(const ir_type *clss, int pos) {
809 return _get_class_member(clss, pos);
812 ir_entity *get_class_member_by_name(ir_type *clss, ident *name) {
814 assert(clss && (clss->type_op == type_class));
815 n_mem = get_class_n_members(clss);
816 for (i = 0; i < n_mem; ++i) {
817 ir_entity *mem = get_class_member(clss, i);
818 if (get_entity_ident(mem) == name) return mem;
823 void set_class_member(ir_type *clss, ir_entity *member, int pos) {
824 assert(clss && (clss->type_op == type_class));
825 assert(pos >= 0 && pos < get_class_n_members(clss));
826 clss->attr.ca.members[pos] = member;
829 void set_class_members(ir_type *clss, ir_entity **members, int arity) {
831 assert(clss && (clss->type_op == type_class));
832 DEL_ARR_F(clss->attr.ca.members);
833 clss->attr.ca.members = NEW_ARR_F(ir_entity *, 0);
834 for (i = 0; i < arity; ++i) {
835 set_entity_owner(members[i], clss);
836 ARR_APP1(ir_entity *, clss->attr.ca.members, members[i]);
840 void remove_class_member(ir_type *clss, ir_entity *member) {
842 assert(clss && (clss->type_op == type_class));
843 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
844 if (clss->attr.ca.members[i] == member) {
845 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
846 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
847 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
853 void add_class_subtype(ir_type *clss, ir_type *subtype) {
855 assert(clss && (clss->type_op == type_class));
856 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
857 for (i = 0; i < get_class_n_supertypes(subtype); i++)
858 if (get_class_supertype(subtype, i) == clss)
859 /* Class already registered */
861 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
864 int get_class_n_subtypes(const ir_type *clss) {
865 assert(clss && (clss->type_op == type_class));
866 return (ARR_LEN (clss->attr.ca.subtypes));
869 ir_type *get_class_subtype(ir_type *clss, int pos) {
870 assert(clss && (clss->type_op == type_class));
871 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
872 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
875 int get_class_subtype_index(ir_type *clss, const ir_type *subclass) {
876 int i, n_subtypes = get_class_n_subtypes(clss);
877 assert(is_Class_type(subclass));
878 for (i = 0; i < n_subtypes; ++i) {
879 if (get_class_subtype(clss, i) == subclass) return i;
884 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos) {
885 assert(clss && (clss->type_op == type_class));
886 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
887 clss->attr.ca.subtypes[pos] = subtype;
890 void remove_class_subtype(ir_type *clss, ir_type *subtype) {
892 assert(clss && (clss->type_op == type_class));
893 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
894 if (clss->attr.ca.subtypes[i] == subtype) {
895 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
896 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
897 ARR_SETLEN(ir_entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
902 void add_class_supertype(ir_type *clss, ir_type *supertype) {
904 assert(clss && (clss->type_op == type_class));
905 assert(supertype && (supertype -> type_op == type_class));
906 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
907 for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
908 if (get_class_subtype(supertype, i) == clss)
909 /* Class already registered */
911 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
914 int get_class_n_supertypes(const ir_type *clss) {
915 assert(clss && (clss->type_op == type_class));
916 return ARR_LEN(clss->attr.ca.supertypes);
919 int get_class_supertype_index(ir_type *clss, ir_type *super_clss) {
920 int i, n_supertypes = get_class_n_supertypes(clss);
921 assert(super_clss && (super_clss->type_op == type_class));
922 for (i = 0; i < n_supertypes; i++)
923 if (get_class_supertype(clss, i) == super_clss)
928 ir_type *get_class_supertype(ir_type *clss, int pos) {
929 assert(clss && (clss->type_op == type_class));
930 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
931 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
934 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos) {
935 assert(clss && (clss->type_op == type_class));
936 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
937 clss->attr.ca.supertypes[pos] = supertype;
940 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_entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
952 ir_entity *get_class_type_info(const ir_type *clss) {
953 return clss->attr.ca.type_info;
956 void set_class_type_info(ir_type *clss, ir_entity *ent) {
957 clss->attr.ca.type_info = ent;
959 ent->repr_class = clss;
962 const char *get_peculiarity_name(ir_peculiarity p) {
963 #define X(a) case a: return #a
965 X(peculiarity_description);
966 X(peculiarity_inherited);
967 X(peculiarity_existent);
970 return "invalid peculiarity";
973 ir_peculiarity get_class_peculiarity(const ir_type *clss) {
974 assert(clss && (clss->type_op == type_class));
975 return clss->attr.ca.peculiarity;
978 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec) {
979 assert(clss && (clss->type_op == type_class));
980 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
981 clss->attr.ca.peculiarity = pec;
984 /* Returns the size of the virtual function table. */
985 unsigned (get_class_vtable_size)(const ir_type *clss) {
986 return _get_class_vtable_size(clss);
989 /* Sets a new size of the virtual function table. */
990 void (set_class_vtable_size)(ir_type *clss, unsigned size) {
991 _set_class_vtable_size(clss, size);
994 /* Returns non-zero if a class is final. */
995 int (is_class_final)(const ir_type *clss) {
996 return _is_class_final(clss);
999 /* Sets if a class is final. */
1000 void (set_class_final)(ir_type *clss, int flag) {
1001 _set_class_final(clss, flag);
1004 /* Returns non-zero if a class is an interface. */
1005 int (is_class_interface)(const ir_type *clss) {
1006 return _is_class_interface(clss);
1009 /* Sets the class interface flag. */
1010 void (set_class_interface)(ir_type *clss, int flag) {
1011 _set_class_interface(clss, flag);
1014 /* Returns non-zero if a class is abstract. */
1015 int (is_class_abstract)(const ir_type *clss) {
1016 return _is_class_abstract(clss);
1019 /* Sets the class abstract flag. */
1020 void (set_class_abstract)(ir_type *clss, int final) {
1021 _set_class_abstract(clss, final);
1024 void set_class_dfn(ir_type *clss, int dfn) {
1025 clss->attr.ca.dfn = dfn;
1028 int get_class_dfn(const ir_type *clss) {
1029 return (clss->attr.ca.dfn);
1033 int (is_Class_type)(const ir_type *clss) {
1034 return _is_class_type(clss);
1037 void set_class_mode(ir_type *tp, ir_mode *mode) {
1038 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1039 assert(get_type_state(tp) == layout_fixed &&
1040 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1044 void set_class_size(ir_type *tp, unsigned size) {
1048 /*----------------------------------------------------------------**/
1050 /*----------------------------------------------------------------**/
1052 /* create a new type struct */
1053 ir_type *new_d_type_struct(ident *name, dbg_info *db) {
1054 ir_type *res = new_type(type_struct, NULL, name, db);
1056 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1061 ir_type *new_type_struct(ident *name) {
1062 return new_d_type_struct (name, NULL);
1065 void free_struct_entities(ir_type *strct) {
1067 assert(strct && (strct->type_op == type_struct));
1068 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1069 free_entity(get_struct_member(strct, i));
1072 void free_struct_attrs(ir_type *strct) {
1073 assert(strct && (strct->type_op == type_struct));
1074 DEL_ARR_F(strct->attr.sa.members);
1077 /* manipulate private fields of struct */
1078 int get_struct_n_members(const ir_type *strct) {
1079 assert(strct && (strct->type_op == type_struct));
1080 return ARR_LEN(strct->attr.sa.members);
1083 void add_struct_member(ir_type *strct, ir_entity *member) {
1084 assert(strct && (strct->type_op == type_struct));
1085 assert(get_type_tpop(get_entity_type(member)) != type_method);
1086 assert(strct != get_entity_type(member) && "recursive type");
1087 assert(get_type_state(strct) != layout_fixed);
1088 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1091 ir_entity *get_struct_member(const ir_type *strct, int pos) {
1092 assert(strct && (strct->type_op == type_struct));
1093 assert(pos >= 0 && pos < get_struct_n_members(strct));
1094 return strct->attr.sa.members[pos];
1097 int get_struct_member_index(const ir_type *strct, ir_entity *mem) {
1099 assert(strct && (strct->type_op == type_struct));
1100 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1101 if (get_struct_member(strct, i) == mem)
1106 void set_struct_member(ir_type *strct, int pos, ir_entity *member) {
1107 assert(strct && (strct->type_op == type_struct));
1108 assert(pos >= 0 && pos < get_struct_n_members(strct));
1109 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1110 strct->attr.sa.members[pos] = member;
1113 void remove_struct_member(ir_type *strct, ir_entity *member) {
1115 assert(strct && (strct->type_op == type_struct));
1116 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1117 if (strct->attr.sa.members[i] == member) {
1118 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1119 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1120 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1126 int (is_Struct_type)(const ir_type *strct) {
1127 return _is_struct_type(strct);
1130 void set_struct_mode(ir_type *tp, ir_mode *mode) {
1131 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1132 assert(get_type_state(tp) == layout_fixed &&
1133 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1137 void set_struct_size(ir_type *tp, unsigned size) {
1141 /*******************************************************************/
1143 /*******************************************************************/
1146 * Lazy construction of value argument / result representation.
1147 * Constructs a struct type and its member. The types of the members
1148 * are passed in the argument list.
1150 * @param name name of the type constructed
1151 * @param len number of fields
1152 * @param tps array of field types with length len
1155 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1157 ir_type *res = new_type_struct(name);
1158 res->flags |= tf_value_param_type;
1159 /* Remove type from type list. Must be treated differently than other types. */
1160 remove_irp_type(res);
1161 for (i = 0; i < len; i++) {
1162 ident *id = tps[i].param_name;
1164 /* use res as default if corresponding type is not yet set. */
1165 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1167 /* use the parameter name if specified */
1169 id = id_mangle_u(name, get_type_ident(elt_type));
1170 tps[i].ent = new_entity(res, id, elt_type);
1171 set_entity_allocation(tps[i].ent, allocation_parameter);
1176 /* Create a new method type.
1177 N_param is the number of parameters, n_res the number of results. */
1178 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1181 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1182 res = new_type(type_method, mode_P_code, name, db);
1183 res->flags |= tf_layout_fixed;
1184 res->size = get_mode_size_bytes(mode_P_code);
1185 res->attr.ma.n_params = n_param;
1186 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1187 res->attr.ma.value_params = NULL;
1188 res->attr.ma.n_res = n_res;
1189 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1190 res->attr.ma.value_ress = NULL;
1191 res->attr.ma.variadicity = variadicity_non_variadic;
1192 res->attr.ma.first_variadic_param = -1;
1193 res->attr.ma.additional_properties = mtp_no_property;
1194 res->attr.ma.irg_calling_conv = default_cc_mask;
1199 ir_type *new_type_method(ident *name, int n_param, int n_res) {
1200 return new_d_type_method(name, n_param, n_res, NULL);
1203 /* clone an existing method type */
1204 ir_type *clone_type_method(ir_type *tp, ident *prefix) {
1208 int n_params, n_res;
1211 assert(is_Method_type(tp));
1215 name = id_mangle(prefix, name);
1218 n_params = tp->attr.ma.n_params;
1219 n_res = tp->attr.ma.n_res;
1222 res = new_type(type_method, mode, name, db);
1224 res->flags = tp->flags;
1225 res->assoc_type = tp->assoc_type;
1226 res->size = tp->size;
1227 res->attr.ma.n_params = n_params;
1228 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1229 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1230 res->attr.ma.value_params = tp->attr.ma.value_params;
1231 res->attr.ma.n_res = n_res;
1232 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1233 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1234 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1235 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1236 res->attr.ma.first_variadic_param = tp->attr.ma.first_variadic_param;
1237 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1238 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1243 void free_method_entities(ir_type *method) {
1245 assert(method && (method->type_op == type_method));
1248 /* Attention: also frees entities in value parameter subtypes! */
1249 void free_method_attrs(ir_type *method) {
1250 assert(method && (method->type_op == type_method));
1251 free(method->attr.ma.params);
1252 free(method->attr.ma.res_type);
1253 /* cannot free it yet, type could be cloned ...
1254 if (method->attr.ma.value_params) {
1255 free_type_entities(method->attr.ma.value_params);
1256 free_type(method->attr.ma.value_params);
1259 if (method->attr.ma.value_ress) {
1260 free_type_entities(method->attr.ma.value_ress);
1261 free_type(method->attr.ma.value_ress);
1265 /* manipulate private fields of method. */
1266 int (get_method_n_params)(const ir_type *method) {
1267 return _get_method_n_params(method);
1270 /* Returns the type of the parameter at position pos of a method. */
1271 ir_type *get_method_param_type(ir_type *method, int pos) {
1273 assert(method && (method->type_op == type_method));
1274 assert(pos >= 0 && pos < get_method_n_params(method));
1275 res = method->attr.ma.params[pos].tp;
1276 assert(res != NULL && "empty method param type");
1277 return method->attr.ma.params[pos].tp = skip_tid(res);
1280 void set_method_param_type(ir_type *method, int pos, ir_type *tp) {
1281 assert(method && (method->type_op == type_method));
1282 assert(pos >= 0 && pos < get_method_n_params(method));
1283 method->attr.ma.params[pos].tp = tp;
1284 /* If information constructed set pass-by-value representation. */
1285 if (method->attr.ma.value_params) {
1286 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1287 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1291 /* Returns an ident representing the parameters name. Returns NULL if not set.
1292 For debug support only. */
1293 ident *get_method_param_ident(ir_type *method, int pos) {
1294 assert(method && (method->type_op == type_method));
1295 assert(pos >= 0 && pos < get_method_n_params(method));
1296 return method->attr.ma.params[pos].param_name;
1299 /* Returns a string representing the parameters name. Returns NULL if not set.
1300 For debug support only. */
1301 const char *get_method_param_name(ir_type *method, int pos) {
1302 ident *id = get_method_param_ident(method, pos);
1303 return id ? get_id_str(id) : NULL;
1306 /* Sets an ident representing the parameters name. For debug support only. */
1307 void set_method_param_ident(ir_type *method, int pos, ident *id) {
1308 assert(method && (method->type_op == type_method));
1309 assert(pos >= 0 && pos < get_method_n_params(method));
1310 method->attr.ma.params[pos].param_name = id;
1313 /* Returns an entity that represents the copied value argument. Only necessary
1314 for compounds passed by value. */
1315 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
1322 = build_value_type(id_mangle_u(get_type_ident(method), value_params_suffix),
1323 get_method_n_params(method), method->attr.ma.params);
1326 * build_value_type() sets the method->attr.ma.value_params type as default if
1329 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1330 && "param type not yet set");
1331 return method->attr.ma.params[pos].ent;
1335 * Returns a type that represents the copied value arguments.
1337 ir_type *get_method_value_param_type(const ir_type *method) {
1338 assert(method && (method->type_op == type_method));
1339 return method->attr.ma.value_params;
1342 int (get_method_n_ress)(const ir_type *method) {
1343 return _get_method_n_ress(method);
1346 ir_type *get_method_res_type(ir_type *method, int pos) {
1348 assert(method && (method->type_op == type_method));
1349 assert(pos >= 0 && pos < get_method_n_ress(method));
1350 res = method->attr.ma.res_type[pos].tp;
1351 assert(res != NULL && "empty method return type");
1352 return method->attr.ma.res_type[pos].tp = skip_tid(res);
1355 void set_method_res_type(ir_type *method, int pos, ir_type *tp) {
1356 assert(method && (method->type_op == type_method));
1357 assert(pos >= 0 && pos < get_method_n_ress(method));
1358 /* set the result ir_type */
1359 method->attr.ma.res_type[pos].tp = tp;
1360 /* If information constructed set pass-by-value representation. */
1361 if (method->attr.ma.value_ress) {
1362 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1363 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1367 /* Returns an entity that represents the copied value result. Only necessary
1368 for compounds passed by value. */
1369 ir_entity *get_method_value_res_ent(ir_type *method, int pos) {
1370 assert(method && (method->type_op == type_method));
1371 assert(pos >= 0 && pos < get_method_n_ress(method));
1373 if (!method->attr.ma.value_ress) {
1374 /* result value type not created yet, build */
1375 method->attr.ma.value_ress
1376 = build_value_type(id_mangle_u(get_type_ident(method), value_ress_suffix),
1377 get_method_n_ress(method), method->attr.ma.res_type);
1380 * build_value_type() sets the method->attr.ma.value_ress type as default if
1383 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1384 && "result type not yet set");
1386 return method->attr.ma.res_type[pos].ent;
1390 * Returns a type that represents the copied value results.
1392 ir_type *get_method_value_res_type(const ir_type *method) {
1393 assert(method && (method->type_op == type_method));
1394 return method->attr.ma.value_ress;
1397 /* Returns the null-terminated name of this variadicity. */
1398 const char *get_variadicity_name(variadicity vari) {
1399 #define X(a) case a: return #a
1401 X(variadicity_non_variadic);
1402 X(variadicity_variadic);
1409 variadicity get_method_variadicity(const ir_type *method) {
1410 assert(method && (method->type_op == type_method));
1411 return method->attr.ma.variadicity;
1414 void set_method_variadicity(ir_type *method, variadicity vari) {
1415 assert(method && (method->type_op == type_method));
1416 method->attr.ma.variadicity = vari;
1420 * Returns the first variadic parameter index of a type.
1421 * If this index was NOT set, the index of the last parameter
1422 * of the method type plus one is returned for variadic functions.
1423 * Non-variadic function types always return -1 here.
1425 int get_method_first_variadic_param_index(const ir_type *method) {
1426 assert(method && (method->type_op == type_method));
1428 if (method->attr.ma.variadicity == variadicity_non_variadic)
1431 if (method->attr.ma.first_variadic_param == -1)
1432 return get_method_n_params(method);
1433 return method->attr.ma.first_variadic_param;
1437 * Sets the first variadic parameter index. This allows to specify
1438 * a complete call type (containing the type of all parameters)
1439 * but still have the knowledge, which parameter must be passed as
1442 void set_method_first_variadic_param_index(ir_type *method, int index) {
1443 assert(method && (method->type_op == type_method));
1444 assert(index >= 0 && index <= get_method_n_params(method));
1446 method->attr.ma.first_variadic_param = index;
1449 unsigned (get_method_additional_properties)(const ir_type *method) {
1450 return _get_method_additional_properties(method);
1453 void (set_method_additional_properties)(ir_type *method, unsigned mask) {
1454 _set_method_additional_properties(method, mask);
1457 void (set_method_additional_property)(ir_type *method, mtp_additional_property flag) {
1458 _set_method_additional_property(method, flag);
1461 /* Returns the calling convention of an entities graph. */
1462 unsigned (get_method_calling_convention)(const ir_type *method) {
1463 return _get_method_calling_convention(method);
1466 /* Sets the calling convention of an entities graph. */
1467 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask) {
1468 _set_method_calling_convention(method, cc_mask);
1471 /* Returns the number of registers parameters, 0 means default. */
1472 unsigned get_method_n_regparams(ir_type *method) {
1473 unsigned cc = get_method_calling_convention(method);
1474 assert(IS_FASTCALL(cc));
1476 return cc & ~cc_bits;
1479 /* Sets the number of registers parameters, 0 means default. */
1480 void set_method_n_regparams(ir_type *method, unsigned n_regs) {
1481 unsigned cc = get_method_calling_convention(method);
1482 assert(IS_FASTCALL(cc));
1484 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1488 int (is_Method_type)(const ir_type *method) {
1489 return _is_method_type(method);
1492 /*-----------------------------------------------------------------*/
1494 /*-----------------------------------------------------------------*/
1496 /* create a new type uni */
1497 ir_type *new_d_type_union(ident *name, dbg_info *db) {
1498 ir_type *res = new_type(type_union, NULL, name, db);
1500 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1505 ir_type *new_type_union(ident *name) {
1506 return new_d_type_union(name, NULL);
1509 void free_union_entities(ir_type *uni) {
1511 assert(uni && (uni->type_op == type_union));
1512 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1513 free_entity(get_union_member(uni, i));
1516 void free_union_attrs (ir_type *uni) {
1517 assert(uni && (uni->type_op == type_union));
1518 DEL_ARR_F(uni->attr.ua.members);
1521 /* manipulate private fields of union */
1522 int get_union_n_members(const ir_type *uni) {
1523 assert(uni && (uni->type_op == type_union));
1524 return ARR_LEN(uni->attr.ua.members);
1527 void add_union_member(ir_type *uni, ir_entity *member) {
1528 assert(uni && (uni->type_op == type_union));
1529 assert(uni != get_entity_type(member) && "recursive type");
1530 assert(get_type_state(uni) != layout_fixed);
1531 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1534 ir_entity *get_union_member(const ir_type *uni, int pos) {
1535 assert(uni && (uni->type_op == type_union));
1536 assert(pos >= 0 && pos < get_union_n_members(uni));
1537 return uni->attr.ua.members[pos];
1540 int get_union_member_index(const ir_type *uni, ir_entity *mem) {
1542 assert(uni && (uni->type_op == type_union));
1543 for (i = 0, n = get_union_n_members(uni); i < n; ++i)
1544 if (get_union_member(uni, i) == mem)
1549 void set_union_member(ir_type *uni, int pos, ir_entity *member) {
1550 assert(uni && (uni->type_op == type_union));
1551 assert(pos >= 0 && pos < get_union_n_members(uni));
1552 uni->attr.ua.members[pos] = member;
1555 void remove_union_member(ir_type *uni, ir_entity *member) {
1557 assert(uni && (uni->type_op == type_union));
1558 for (i = 0; i < (ARR_LEN(uni->attr.ua.members)); i++)
1559 if (uni->attr.ua.members[i] == member) {
1560 for(; i < (ARR_LEN(uni->attr.ua.members))-1; i++)
1561 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1562 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1568 int (is_Union_type)(const ir_type *uni) {
1569 return _is_union_type(uni);
1572 void set_union_size(ir_type *tp, unsigned size) {
1576 /*-----------------------------------------------------------------*/
1578 /*-----------------------------------------------------------------*/
1581 /* create a new type array -- set dimension sizes independently */
1582 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type, dbg_info *db) {
1586 ir_graph *rem = current_ir_graph;
1588 assert(!is_Method_type(element_type));
1590 res = new_type(type_array, NULL, name, db);
1591 res->attr.aa.n_dimensions = n_dimensions;
1592 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1593 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1594 res->attr.aa.order = XMALLOCNZ(int, n_dimensions);
1596 current_ir_graph = get_const_code_irg();
1597 unk = new_Unknown(mode_Iu);
1598 for (i = 0; i < n_dimensions; i++) {
1599 res->attr.aa.lower_bound[i] =
1600 res->attr.aa.upper_bound[i] = unk;
1601 res->attr.aa.order[i] = i;
1603 current_ir_graph = rem;
1605 res->attr.aa.element_type = element_type;
1606 new_entity(res, id_mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1611 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type) {
1612 return new_d_type_array(name, n_dimensions, element_type, NULL);
1615 void free_array_automatic_entities(ir_type *array) {
1616 assert(array && (array->type_op == type_array));
1617 free_entity(get_array_element_entity(array));
1620 void free_array_entities (ir_type *array) {
1622 assert(array && (array->type_op == type_array));
1625 void free_array_attrs (ir_type *array) {
1626 assert(array && (array->type_op == type_array));
1627 free(array->attr.aa.lower_bound);
1628 free(array->attr.aa.upper_bound);
1629 free(array->attr.aa.order);
1632 /* manipulate private fields of array ir_type */
1633 int get_array_n_dimensions (const ir_type *array) {
1634 assert(array && (array->type_op == type_array));
1635 return array->attr.aa.n_dimensions;
1639 set_array_bounds(ir_type *array, int dimension, ir_node * lower_bound, ir_node * upper_bound) {
1640 assert(array && (array->type_op == type_array));
1641 assert(lower_bound && "lower_bound node may not be NULL.");
1642 assert(upper_bound && "upper_bound node may not be NULL.");
1643 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1644 array->attr.aa.lower_bound[dimension] = lower_bound;
1645 array->attr.aa.upper_bound[dimension] = upper_bound;
1649 set_array_bounds_int(ir_type *array, int dimension, int lower_bound, int upper_bound) {
1650 ir_graph *rem = current_ir_graph;
1651 current_ir_graph = get_const_code_irg();
1652 set_array_bounds(array, dimension,
1653 new_Const_long(mode_Iu, lower_bound),
1654 new_Const_long(mode_Iu, upper_bound));
1655 current_ir_graph = rem;
1659 set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound) {
1660 assert(array && (array->type_op == type_array));
1661 assert(lower_bound && "lower_bound node may not be NULL.");
1662 array->attr.aa.lower_bound[dimension] = lower_bound;
1665 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound) {
1666 ir_graph *rem = current_ir_graph;
1667 current_ir_graph = get_const_code_irg();
1668 set_array_lower_bound(array, dimension,
1669 new_Const_long(mode_Iu, lower_bound));
1670 current_ir_graph = rem;
1673 set_array_upper_bound (ir_type *array, int dimension, ir_node * upper_bound) {
1674 assert(array && (array->type_op == type_array));
1675 assert(upper_bound && "upper_bound node may not be NULL.");
1676 array->attr.aa.upper_bound[dimension] = upper_bound;
1678 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound) {
1679 ir_graph *rem = current_ir_graph;
1680 current_ir_graph = get_const_code_irg();
1681 set_array_upper_bound(array, dimension,
1682 new_Const_long(mode_Iu, upper_bound));
1683 current_ir_graph = rem;
1686 int has_array_lower_bound(const ir_type *array, int dimension) {
1687 assert(array && (array->type_op == type_array));
1688 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1691 ir_node *get_array_lower_bound(const ir_type *array, int dimension) {
1692 assert(array && (array->type_op == type_array));
1693 return array->attr.aa.lower_bound[dimension];
1696 long get_array_lower_bound_int(const ir_type *array, int dimension) {
1698 assert(array && (array->type_op == type_array));
1699 node = array->attr.aa.lower_bound[dimension];
1700 assert(is_Const(node));
1701 return get_tarval_long(get_Const_tarval(node));
1704 int has_array_upper_bound(const ir_type *array, int dimension) {
1705 assert(array && (array->type_op == type_array));
1706 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1709 ir_node *get_array_upper_bound(const ir_type *array, int dimension) {
1710 assert(array && (array->type_op == type_array));
1711 return array->attr.aa.upper_bound[dimension];
1714 long get_array_upper_bound_int(const ir_type *array, int dimension) {
1716 assert(array && (array->type_op == type_array));
1717 node = array->attr.aa.upper_bound[dimension];
1718 assert(is_Const(node));
1719 return get_tarval_long(get_Const_tarval(node));
1722 void set_array_order(ir_type *array, int dimension, int order) {
1723 assert(array && (array->type_op == type_array));
1724 array->attr.aa.order[dimension] = order;
1727 int get_array_order(const ir_type *array, int dimension) {
1728 assert(array && (array->type_op == type_array));
1729 return array->attr.aa.order[dimension];
1732 int find_array_dimension(const ir_type *array, int order) {
1735 assert(array && (array->type_op == type_array));
1737 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1738 if (array->attr.aa.order[dim] == order)
1744 void set_array_element_type(ir_type *array, ir_type *tp) {
1745 assert(array && (array->type_op == type_array));
1746 assert(!is_Method_type(tp));
1747 array->attr.aa.element_type = tp;
1750 ir_type *get_array_element_type(ir_type *array) {
1751 assert(array && (array->type_op == type_array));
1752 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1755 void set_array_element_entity(ir_type *array, ir_entity *ent) {
1756 assert(array && (array->type_op == type_array));
1757 assert((get_entity_type(ent)->type_op != type_method));
1758 array->attr.aa.element_ent = ent;
1759 array->attr.aa.element_type = get_entity_type(ent);
1762 ir_entity *get_array_element_entity(const ir_type *array) {
1763 assert(array && (array->type_op == type_array));
1764 return array->attr.aa.element_ent;
1768 int (is_Array_type)(const ir_type *array) {
1769 return _is_array_type(array);
1772 void set_array_size(ir_type *tp, unsigned size) {
1773 /* FIXME: Here we should make some checks with the element type size */
1776 /*-----------------------------------------------------------------*/
1777 /* TYPE_ENUMERATION */
1778 /*-----------------------------------------------------------------*/
1780 /* create a new type enumeration -- set the enumerators independently */
1781 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1784 assert(n_enums >= 0);
1785 res = new_type(type_enumeration, NULL, name, db);
1786 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1791 ir_type *new_type_enumeration(ident *name, int n_enums) {
1792 return new_d_type_enumeration(name, n_enums, NULL);
1795 void free_enumeration_entities(ir_type *enumeration) {
1797 assert(enumeration && (enumeration->type_op == type_enumeration));
1799 void free_enumeration_attrs(ir_type *enumeration) {
1800 assert(enumeration && (enumeration->type_op == type_enumeration));
1801 DEL_ARR_F(enumeration->attr.ea.enumer);
1804 /* manipulate fields of enumeration type. */
1805 int get_enumeration_n_enums(const ir_type *enumeration) {
1806 assert(enumeration && (enumeration->type_op == type_enumeration));
1807 return ARR_LEN(enumeration->attr.ea.enumer);
1810 /* create a new constant */
1811 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con) {
1812 assert(0 <= pos && pos < ARR_LEN(enumeration->attr.ea.enumer));
1813 enumeration->attr.ea.enumer[pos].nameid = nameid;
1814 enumeration->attr.ea.enumer[pos].value = con;
1815 enumeration->attr.ea.enumer[pos].owner = enumeration;
1818 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos) {
1819 assert(enumeration && (enumeration->type_op == type_enumeration));
1820 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1821 return &enumeration->attr.ea.enumer[pos];
1824 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst) {
1825 return enum_cnst->owner;
1828 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con) {
1829 enum_cnst->value = con;
1832 tarval *get_enumeration_value(const ir_enum_const *enum_cnst) {
1833 return enum_cnst->value;
1836 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id) {
1837 enum_cnst->nameid = id;
1840 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst) {
1841 return enum_cnst->nameid;
1844 const char *get_enumeration_name(const ir_enum_const *enum_cnst) {
1845 return get_id_str(enum_cnst->nameid);
1849 int (is_Enumeration_type)(const ir_type *enumeration) {
1850 return _is_enumeration_type(enumeration);
1853 void set_enumeration_mode(ir_type *tp, ir_mode *mode) {
1854 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1855 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1856 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1858 tp->size = get_mode_size_bytes(mode);
1862 /*-----------------------------------------------------------------*/
1864 /*-----------------------------------------------------------------*/
1866 /* Create a new type pointer */
1867 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1870 assert(mode_is_reference(ptr_mode));
1871 res = new_type(type_pointer, ptr_mode, name, db);
1872 res->attr.pa.points_to = points_to;
1873 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1874 res->size = get_mode_size_bytes(res->mode);
1875 res->flags |= tf_layout_fixed;
1880 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode) {
1881 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1884 void free_pointer_entities(ir_type *pointer) {
1886 assert(pointer && (pointer->type_op == type_pointer));
1889 void free_pointer_attrs(ir_type *pointer) {
1891 assert(pointer && (pointer->type_op == type_pointer));
1894 /* manipulate fields of type_pointer */
1895 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp) {
1896 assert(pointer && (pointer->type_op == type_pointer));
1897 pointer->attr.pa.points_to = tp;
1900 ir_type *get_pointer_points_to_type(ir_type *pointer) {
1901 assert(pointer && (pointer->type_op == type_pointer));
1902 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1906 int (is_Pointer_type)(const ir_type *pointer) {
1907 return _is_pointer_type(pointer);
1910 void set_pointer_mode(ir_type *tp, ir_mode *mode) {
1911 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1912 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1913 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1915 tp->size = get_mode_size_bytes(mode);
1919 /* Returns the first pointer type that has as points_to tp.
1920 * Not efficient: O(#types).
1921 * If not found returns firm_unknown_type. */
1922 ir_type *find_pointer_type_to_type (ir_type *tp) {
1923 int i, n = get_irp_n_types();
1924 for (i = 0; i < n; ++i) {
1925 ir_type *found = get_irp_type(i);
1926 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1929 return firm_unknown_type;
1933 /*-----------------------------------------------------------------*/
1934 /* TYPE_PRIMITIVE */
1935 /*-----------------------------------------------------------------*/
1937 /* create a new type primitive */
1938 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1939 ir_type *res = new_type(type_primitive, mode, name, db);
1940 res->size = get_mode_size_bytes(mode);
1941 res->flags |= tf_layout_fixed;
1942 res->attr.ba.base_type = NULL;
1947 ir_type *new_type_primitive(ident *name, ir_mode *mode) {
1948 return new_d_type_primitive(name, mode, NULL);
1952 int (is_Primitive_type)(const ir_type *primitive) {
1953 return _is_primitive_type(primitive);
1956 void set_primitive_mode(ir_type *tp, ir_mode *mode) {
1957 /* Modes of primitives must be data */
1958 assert(mode_is_data(mode));
1960 /* For primitive size depends on the mode. */
1961 tp->size = get_mode_size_bytes(mode);
1965 /* Return the base type of a primitive (bitfield) type or NULL if none. */
1966 ir_type *get_primitive_base_type(ir_type *tp) {
1967 assert(is_Primitive_type(tp));
1968 return tp->attr.ba.base_type;
1971 /* Sets the base type of a primitive (bitfield) type. */
1972 void set_primitive_base_type(ir_type *tp, ir_type *base_tp) {
1973 assert(is_Primitive_type(tp));
1974 tp->attr.ba.base_type = base_tp;
1977 /*-----------------------------------------------------------------*/
1978 /* common functionality */
1979 /*-----------------------------------------------------------------*/
1982 int (is_atomic_type)(const ir_type *tp) {
1983 return _is_atomic_type(tp);
1987 * Gets the number of elements in a firm compound type.
1989 int get_compound_n_members(const ir_type *tp) {
1990 const tp_op *op = get_type_tpop(tp);
1993 if (op->ops.get_n_members)
1994 res = op->ops.get_n_members(tp);
1996 assert(0 && "no member count for this type");
2002 * Gets the member of a firm compound type at position pos.
2004 ir_entity *get_compound_member(const ir_type *tp, int pos) {
2005 const tp_op *op = get_type_tpop(tp);
2006 ir_entity *res = NULL;
2008 if (op->ops.get_member)
2009 res = op->ops.get_member(tp, pos);
2011 assert(0 && "no members in this type");
2016 /* Returns index of member in tp, -1 if not contained. */
2017 int get_compound_member_index(const ir_type *tp, ir_entity *member) {
2018 const tp_op *op = get_type_tpop(tp);
2021 if (op->ops.get_member_index)
2022 index = op->ops.get_member_index(tp, member);
2024 assert(0 && "no members in this type");
2029 int is_compound_type(const ir_type *tp) {
2030 assert(tp && tp->kind == k_type);
2031 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2034 /* Checks, whether a type is a frame type */
2035 int is_frame_type(const ir_type *tp) {
2036 return tp->flags & tf_frame_type;
2039 /* Checks, whether a type is a value parameter type */
2040 int is_value_param_type(const ir_type *tp) {
2041 return tp->flags & tf_value_param_type;
2044 /* Checks, whether a type is a lowered type */
2045 int is_lowered_type(const ir_type *tp) {
2046 return tp->flags & tf_lowered_type;
2049 /* Makes a new frame type. */
2050 ir_type *new_type_frame(ident *name) {
2051 ir_type *res = new_type_class(name);
2053 res->flags |= tf_frame_type;
2055 /* Remove type from type list. Must be treated differently than other types. */
2056 remove_irp_type(res);
2058 /* It is not possible to derive from the frame type. Set the final flag. */
2059 set_class_final(res, 1);
2064 /* Makes a clone of a frame type. */
2065 ir_type *clone_frame_type(ir_type *type) {
2069 assert(is_frame_type(type));
2070 /* the entity link resource should be allocated if this function is called */
2071 assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
2073 res = new_type_frame(type->name);
2074 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2075 ir_entity *ent = get_class_member(type, i);
2076 ir_entity *nent = copy_entity_own(ent, res);
2077 set_entity_link(ent, nent);
2078 set_entity_link(nent, ent);
2083 /* Sets a lowered type for a type. This sets both associations. */
2084 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
2085 assert(is_type(tp) && is_type(lowered_type));
2086 lowered_type->flags |= tf_lowered_type;
2087 tp->assoc_type = lowered_type;
2088 lowered_type->assoc_type = tp;
2092 * Gets the lowered/unlowered type of a type or NULL if this type
2093 * has no lowered/unlowered one.
2095 ir_type *get_associated_type(const ir_type *tp) {
2096 return tp->assoc_type;
2099 /* set the type size for the unknown and none ir_type */
2100 void set_default_size(ir_type *tp, unsigned size) {
2105 * Allocate an area of size bytes aligned at alignment
2106 * at the start or the end of a frame type.
2107 * The frame type must have already an fixed layout.
2109 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, int at_start) {
2114 unsigned frame_align;
2115 int i, offset, frame_size;
2116 static unsigned area_cnt = 0;
2117 static ir_type *a_byte = NULL;
2119 assert(is_frame_type(frame_type));
2120 assert(get_type_state(frame_type) == layout_fixed);
2121 assert(get_type_alignment_bytes(frame_type) > 0);
2122 set_type_state(frame_type, layout_undefined);
2125 a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
2127 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2128 name = new_id_from_str(buf);
2130 /* align the size */
2131 frame_align = get_type_alignment_bytes(frame_type);
2132 size = (size + frame_align - 1) & ~(frame_align - 1);
2134 tp = new_type_array(id_mangle_u(get_type_ident(frame_type), name), 1, a_byte);
2135 set_array_bounds_int(tp, 0, 0, size);
2136 set_type_alignment_bytes(tp, alignment);
2138 frame_size = get_type_size_bytes(frame_type);
2140 /* fix all offsets so far */
2141 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2142 ir_entity *ent = get_class_member(frame_type, i);
2144 set_entity_offset(ent, get_entity_offset(ent) + size);
2146 /* calculate offset and new type size */
2150 /* increase size to match alignment... */
2151 if (alignment > frame_align) {
2152 frame_align = alignment;
2153 set_type_alignment_bytes(frame_type, frame_align);
2154 frame_size = (frame_size + frame_align - 1) & ~(frame_align - 1);
2157 /* calculate offset and new type size */
2158 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2159 frame_size = offset + size;
2162 area = new_entity(frame_type, name, tp);
2163 set_entity_offset(area, offset);
2164 set_type_size_bytes(frame_type, frame_size);
2166 /* mark this entity as compiler generated */
2167 set_entity_compiler_generated(area, 1);
2169 set_type_state(frame_type, layout_fixed);