2 * Copyright (C) 1995-2007 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
74 /*-----------------------------------------------------------------*/
76 /*-----------------------------------------------------------------*/
78 ir_type *firm_none_type; ir_type *get_none_type(void) { return firm_none_type; }
79 ir_type *firm_unknown_type; ir_type *get_unknown_type(void) { return firm_unknown_type; }
82 /* Suffixes added to types used for pass-by-value representations. */
83 static ident *value_params_suffix = NULL;
84 static ident *value_ress_suffix = NULL;
86 /** The default calling convention for method types. */
87 static unsigned default_cc_mask;
89 /* return the default calling convention for method types */
90 unsigned get_default_cc_mask(void) {
91 return default_cc_mask;
94 /* Initialize the type module. */
95 void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask) {
96 default_cc_mask = def_cc_mask;
97 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
98 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
100 /* construct none and unknown type. */
101 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"), builtin_db);
102 set_type_size_bytes(firm_none_type, 0);
103 set_type_state (firm_none_type, layout_fixed);
104 remove_irp_type(firm_none_type);
106 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
107 set_type_size_bytes(firm_unknown_type, 0);
108 set_type_state (firm_unknown_type, layout_fixed);
109 remove_irp_type(firm_unknown_type);
112 /** the global type visited flag */
113 unsigned long firm_type_visited;
115 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
116 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
117 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
120 * Creates a new type representation.
123 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
127 assert(type_op != type_id);
128 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
130 node_size = offsetof(ir_type, attr) + type_op->attr_size;
131 res = xmalloc(node_size);
132 memset(res, 0, node_size);
135 res->type_op = type_op;
138 res->visibility = visibility_external_allocated;
139 res->flags = tf_none;
145 res->assoc_type = NULL;
147 res->nr = get_irp_new_node_nr();
148 #endif /* defined DEBUG_libfirm */
150 add_irp_type(res); /* Remember the new type global. */
155 void free_type(ir_type *tp) {
156 const tp_op *op = get_type_tpop(tp);
158 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
160 /* Remove from list of all types */
162 /* Free the attributes of the type. */
164 /* Free entities automatically allocated with the ir_type */
165 if (op->ops.free_auto_entities)
166 op->ops.free_auto_entities(tp);
167 /* And now the type itself... */
172 void free_type_entities(ir_type *tp) {
173 const tp_op *tpop = get_type_tpop(tp);
175 if (tpop->ops.free_entities)
176 tpop->ops.free_entities(tp);
179 void free_type_attrs(ir_type *tp) {
180 const tp_op *tpop = get_type_tpop(tp);
182 if (tpop->ops.free_attrs)
183 tpop->ops.free_attrs(tp);
186 /* set/get the link field */
187 void *(get_type_link)(const ir_type *tp) {
188 return _get_type_link(tp);
191 void (set_type_link)(ir_type *tp, void *l) {
192 _set_type_link(tp, l);
195 const tp_op *(get_type_tpop)(const ir_type *tp) {
196 return _get_type_tpop(tp);
199 ident *(get_type_tpop_nameid)(const ir_type *tp) {
200 return _get_type_tpop_nameid(tp);
203 const char* get_type_tpop_name(const ir_type *tp) {
204 assert(tp && tp->kind == k_type);
205 return get_id_str(tp->type_op->name);
208 tp_opcode (get_type_tpop_code)(const ir_type *tp) {
209 return _get_type_tpop_code(tp);
212 ir_mode *(get_type_mode)(const ir_type *tp) {
213 return _get_type_mode(tp);
216 void set_type_mode(ir_type *tp, ir_mode *mode) {
217 const tp_op *tpop = get_type_tpop(tp);
219 if (tpop->ops.set_type_mode)
220 tpop->ops.set_type_mode(tp, mode);
222 assert(0 && "setting a mode is NOT allowed for this type");
225 ident *(get_type_ident)(const ir_type *tp) {
226 return _get_type_ident(tp);
229 void (set_type_ident)(ir_type *tp, ident* id) {
230 _set_type_ident(tp, id);
233 /* Outputs a unique number for this node */
234 long get_type_nr(const ir_type *tp) {
239 return (long)PTR_TO_INT(tp);
243 const char *get_type_name(const ir_type *tp) {
244 assert(tp && tp->kind == k_type);
245 return (get_id_str(tp->name));
248 unsigned (get_type_size_bytes)(const ir_type *tp) {
249 return _get_type_size_bytes(tp);
252 ir_visibility get_type_visibility(const ir_type *tp) {
254 visibility res = visibility_local;
255 if (is_compound_type(tp)) {
257 if (is_Array_type(tp)) {
258 ir_entity *mem = get_array_element_entity(tp);
259 if (get_entity_visibility(mem) != visibility_local)
260 res = visibility_external_visible;
262 int i, n_mems = get_compound_n_members(tp);
263 for (i = 0; i < n_mems; ++i) {
264 ir_entity *mem = get_compound_member(tp, i);
265 if (get_entity_visibility(mem) != visibility_local)
266 res = visibility_external_visible;
273 return tp->visibility;
276 void set_type_visibility(ir_type *tp, ir_visibility v) {
279 /* check for correctness */
280 if (v != visibility_external_allocated) {
281 visibility res = visibility_local;
282 if (is_compound_type(tp)) {
283 if (is_Array_type(tp)) {
284 ir_entity *mem = get_array_element_entity(tp);
285 if (get_entity_visibility(mem) > res)
286 res = get_entity_visibility(mem);
288 int i, n_mems = get_compound_n_members(tp);
289 for (i = 0; i < n_mems; ++i) {
290 ir_entity *mem = get_compound_member(tp, i);
291 if (get_entity_visibility(mem) > res)
292 res = get_entity_visibility(mem);
303 set_type_size_bytes(ir_type *tp, unsigned size) {
304 const tp_op *tpop = get_type_tpop(tp);
306 if (tpop->ops.set_type_size)
307 tpop->ops.set_type_size(tp, size);
309 assert(0 && "Cannot set size for this type");
312 unsigned get_type_alignment_bytes(ir_type *tp) {
318 /* alignment NOT set calculate it "on demand" */
320 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
321 else if (is_Array_type(tp))
322 align = get_type_alignment_bytes(get_array_element_type(tp));
323 else if (is_compound_type(tp)) {
324 int i, n = get_compound_n_members(tp);
327 for (i = 0; i < n; ++i) {
328 ir_type *t = get_entity_type(get_compound_member(tp, i));
329 unsigned a = get_type_alignment_bytes(t);
334 } else if (is_Method_type(tp)) {
345 set_type_alignment_bytes(ir_type *tp, unsigned align) {
346 assert(tp && tp->kind == k_type);
347 /* Methods don't have an alignment. */
348 if (tp->type_op != type_method) {
353 /* Returns a human readable string for the enum entry. */
354 const char *get_type_state_name(ir_type_state s) {
355 #define X(a) case a: return #a;
365 ir_type_state (get_type_state)(const ir_type *tp) {
366 return _get_type_state(tp);
370 set_type_state(ir_type *tp, ir_type_state state) {
371 assert(tp && tp->kind == k_type);
373 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
374 (tp->type_op == type_method))
377 /* Just a correctness check: */
378 if (state == layout_fixed) {
380 switch (get_type_tpop_code(tp)) {
382 if (tp != get_glob_type()) {
383 int n_mem = get_class_n_members(tp);
384 for (i = 0; i < n_mem; i++) {
385 assert(get_entity_offset(get_class_member(tp, i)) > -1);
387 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
388 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
394 for (i = 0; i < get_struct_n_members(tp); i++) {
395 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
396 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
405 Assure that only innermost dimension is dynamic? */
407 case tpo_enumeration:
409 assert(get_type_mode != NULL);
410 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
411 ir_enum_const *ec = get_enumeration_const(tp, i);
412 tarval *tv = get_enumeration_value(ec);
413 assert(tv != NULL && tv != tarval_bad);
420 if (state == layout_fixed)
421 tp->flags |= tf_layout_fixed;
423 tp->flags &= ~tf_layout_fixed;
426 unsigned long (get_type_visited)(const ir_type *tp) {
427 return _get_type_visited(tp);
430 void (set_type_visited)(ir_type *tp, unsigned long num) {
431 _set_type_visited(tp, num);
434 /* Sets visited field in type to type_visited. */
435 void (mark_type_visited)(ir_type *tp) {
436 _mark_type_visited(tp);
439 int (type_visited)(const ir_type *tp) {
440 return _type_visited(tp);
443 int (type_not_visited)(const ir_type *tp) {
444 return _type_not_visited(tp);
447 dbg_info *(get_type_dbg_info)(const ir_type *tp) {
448 return _get_type_dbg_info(tp);
451 void (set_type_dbg_info)(ir_type *tp, dbg_info *db) {
452 _set_type_dbg_info(tp, db);
455 int (is_type)(const void *thing) {
456 return _is_type(thing);
459 /* Checks whether two types are structural equal.*/
460 int equal_type(ir_type *typ1, ir_type *typ2) {
465 if (typ1 == typ2) return 1;
467 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
468 (get_type_ident(typ1) != get_type_ident(typ2)) ||
469 (get_type_mode(typ1) != get_type_mode(typ2)) ||
470 (get_type_state(typ1) != get_type_state(typ2)))
472 if ((get_type_state(typ1) == layout_fixed) &&
473 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
476 switch (get_type_tpop_code(typ1)) {
478 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
479 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
480 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
481 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
482 /** Compare the members **/
483 m = alloca(sizeof(ir_entity *) * get_class_n_members(typ1));
484 memset(m, 0, sizeof(ir_entity *) * get_class_n_members(typ1));
485 /* First sort the members of typ2 */
486 for (i = 0; i < get_class_n_members(typ1); i++) {
487 ir_entity *e1 = get_class_member(typ1, i);
488 for (j = 0; j < get_class_n_members(typ2); j++) {
489 ir_entity *e2 = get_class_member(typ2, j);
490 if (get_entity_name(e1) == get_entity_name(e2))
494 for (i = 0; i < get_class_n_members(typ1); i++) {
495 if (!m[i] || /* Found no counterpart */
496 !equal_entity(get_class_member(typ1, i), m[i]))
499 /** Compare the supertypes **/
500 t = alloca(sizeof(ir_entity *) * get_class_n_supertypes(typ1));
501 memset(t, 0, sizeof(ir_entity *) * get_class_n_supertypes(typ1));
502 /* First sort the supertypes of typ2 */
503 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
504 ir_type *t1 = get_class_supertype(typ1, i);
505 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
506 ir_type *t2 = get_class_supertype(typ2, j);
507 if (get_type_ident(t2) == get_type_ident(t1))
511 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
512 if (!t[i] || /* Found no counterpart */
513 get_class_supertype(typ1, i) != t[i])
519 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
520 m = alloca(sizeof(ir_entity *) * get_struct_n_members(typ1));
521 memset(m, 0, sizeof(ir_entity *) * get_struct_n_members(typ1));
522 /* First sort the members of lt */
523 for (i = 0; i < get_struct_n_members(typ1); i++) {
524 ir_entity *e1 = get_struct_member(typ1, i);
525 for (j = 0; j < get_struct_n_members(typ2); j++) {
526 ir_entity *e2 = get_struct_member(typ2, j);
527 if (get_entity_name(e1) == get_entity_name(e2))
531 for (i = 0; i < get_struct_n_members(typ1); i++) {
532 if (!m[i] || /* Found no counterpart */
533 !equal_entity(get_struct_member(typ1, i), m[i]))
539 int n_param1, n_param2;
541 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
542 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
543 if (get_method_calling_convention(typ1) !=
544 get_method_calling_convention(typ2)) return 0;
546 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
547 n_param1 = get_method_n_params(typ1);
548 n_param2 = get_method_n_params(typ2);
550 n_param1 = get_method_first_variadic_param_index(typ1);
551 n_param2 = get_method_first_variadic_param_index(typ2);
554 if (n_param1 != n_param2) return 0;
556 for (i = 0; i < n_param1; i++) {
557 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
560 for (i = 0; i < get_method_n_ress(typ1); i++) {
561 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
567 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
568 m = alloca(sizeof(ir_entity *) * get_union_n_members(typ1));
569 memset(m, 0, sizeof(ir_entity *) * get_union_n_members(typ1));
570 /* First sort the members of lt */
571 for (i = 0; i < get_union_n_members(typ1); i++) {
572 ir_entity *e1 = get_union_member(typ1, i);
573 for (j = 0; j < get_union_n_members(typ2); j++) {
574 ir_entity *e2 = get_union_member(typ2, j);
575 if (get_entity_name(e1) == get_entity_name(e2))
579 for (i = 0; i < get_union_n_members(typ1); i++) {
580 if (!m[i] || /* Found no counterpart */
581 !equal_entity(get_union_member(typ1, i), m[i]))
587 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
589 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
591 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
592 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
593 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
595 if (get_array_order(typ1, i) != get_array_order(typ2, i))
596 assert(0 && "type compare with different dimension orders not implemented");
600 case tpo_enumeration:
601 assert(0 && "enumerations not implemented");
605 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
617 /* Checks whether two types are structural comparable. */
618 int smaller_type(ir_type *st, ir_type *lt) {
620 int i, j, n_st_members;
622 if (st == lt) return 1;
624 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
627 switch(get_type_tpop_code(st)) {
629 return is_SubClass_of(st, lt);
632 n_st_members = get_struct_n_members(st);
633 if (n_st_members != get_struct_n_members(lt))
636 m = alloca(sizeof(ir_entity *) * n_st_members);
637 memset(m, 0, sizeof(ir_entity *) * n_st_members);
638 /* First sort the members of lt */
639 for (i = 0; i < n_st_members; ++i) {
640 ir_entity *se = get_struct_member(st, i);
641 int n = get_struct_n_members(lt);
642 for (j = 0; j < n; ++j) {
643 ir_entity *le = get_struct_member(lt, j);
644 if (get_entity_name(le) == get_entity_name(se))
648 for (i = 0; i < n_st_members; i++) {
649 if (!m[i] || /* Found no counterpart */
650 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
656 int n_param1, n_param2;
658 /** FIXME: is this still 1? */
659 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
660 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
661 if (get_method_calling_convention(st) !=
662 get_method_calling_convention(lt)) return 0;
664 if (get_method_variadicity(st) == variadicity_non_variadic) {
665 n_param1 = get_method_n_params(st);
666 n_param2 = get_method_n_params(lt);
668 n_param1 = get_method_first_variadic_param_index(st);
669 n_param2 = get_method_first_variadic_param_index(lt);
672 if (n_param1 != n_param2) return 0;
674 for (i = 0; i < get_method_n_params(st); i++) {
675 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
678 for (i = 0; i < get_method_n_ress(st); i++) {
679 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
685 n_st_members = get_union_n_members(st);
686 if (n_st_members != get_union_n_members(lt)) return 0;
687 m = alloca(sizeof(ir_entity *) * n_st_members);
688 memset(m, 0, sizeof(ir_entity *) * n_st_members);
689 /* First sort the members of lt */
690 for (i = 0; i < n_st_members; ++i) {
691 ir_entity *se = get_union_member(st, i);
692 int n = get_union_n_members(lt);
693 for (j = 0; j < n; ++j) {
694 ir_entity *le = get_union_member(lt, j);
695 if (get_entity_name(le) == get_entity_name(se))
699 for (i = 0; i < n_st_members; ++i) {
700 if (!m[i] || /* Found no counterpart */
701 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
707 ir_type *set, *let; /* small/large elt. ir_type */
708 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
710 set = get_array_element_type(st);
711 let = get_array_element_type(lt);
713 /* If the element types are different, set must be convertible
714 to let, and they must have the same size so that address
715 computations work out. To have a size the layout must
717 if ((get_type_state(set) != layout_fixed) ||
718 (get_type_state(let) != layout_fixed))
720 if (!smaller_type(set, let) ||
721 get_type_size_bytes(set) != get_type_size_bytes(let))
724 for(i = 0; i < get_array_n_dimensions(st); i++) {
725 if (get_array_lower_bound(lt, i))
726 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
728 if (get_array_upper_bound(lt, i))
729 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
734 case tpo_enumeration:
735 assert(0 && "enumerations not implemented");
739 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
744 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
753 /*-----------------------------------------------------------------*/
755 /*-----------------------------------------------------------------*/
757 /* create a new class ir_type */
758 ir_type *new_d_type_class (ident *name, dbg_info *db) {
761 res = new_type(type_class, NULL, name, db);
763 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
764 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
765 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
766 res->attr.ca.peculiarity = peculiarity_existent;
767 res->attr.ca.type_info = NULL;
768 res->attr.ca.vtable_size = 0;
769 res->attr.ca.clss_flags = cf_none;
770 res->attr.ca.dfn = 0;
775 ir_type *new_type_class (ident *name) {
776 return new_d_type_class (name, NULL);
779 /* free all entities of a class */
780 void free_class_entities(ir_type *clss) {
782 assert(clss && (clss->type_op == type_class));
783 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
784 free_entity(get_class_member(clss, i));
785 /* do NOT free the type info here. It belongs to another class */
788 void free_class_attrs(ir_type *clss) {
789 assert(clss && (clss->type_op == type_class));
790 DEL_ARR_F(clss->attr.ca.members);
791 DEL_ARR_F(clss->attr.ca.subtypes);
792 DEL_ARR_F(clss->attr.ca.supertypes);
795 /* manipulate private fields of class type */
796 void add_class_member(ir_type *clss, ir_entity *member) {
797 assert(clss && (clss->type_op == type_class));
798 assert(clss != get_entity_type(member) && "recursive type");
799 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
802 int (get_class_n_members)(const ir_type *clss) {
803 return _get_class_n_members(clss);
806 int get_class_member_index(const ir_type *clss, ir_entity *mem) {
808 assert(clss && (clss->type_op == type_class));
809 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
810 if (get_class_member(clss, i) == mem)
815 ir_entity *(get_class_member)(const ir_type *clss, int pos) {
816 return _get_class_member(clss, pos);
819 ir_entity *get_class_member_by_name(ir_type *clss, ident *name) {
821 assert(clss && (clss->type_op == type_class));
822 n_mem = get_class_n_members(clss);
823 for (i = 0; i < n_mem; ++i) {
824 ir_entity *mem = get_class_member(clss, i);
825 if (get_entity_ident(mem) == name) return mem;
830 void set_class_member(ir_type *clss, ir_entity *member, int pos) {
831 assert(clss && (clss->type_op == type_class));
832 assert(pos >= 0 && pos < get_class_n_members(clss));
833 clss->attr.ca.members[pos] = member;
836 void set_class_members(ir_type *clss, ir_entity **members, int arity) {
838 assert(clss && (clss->type_op == type_class));
839 DEL_ARR_F(clss->attr.ca.members);
840 clss->attr.ca.members = NEW_ARR_F(ir_entity *, 0);
841 for (i = 0; i < arity; ++i) {
842 set_entity_owner(members[i], clss);
843 ARR_APP1(ir_entity *, clss->attr.ca.members, members[i]);
847 void remove_class_member(ir_type *clss, ir_entity *member) {
849 assert(clss && (clss->type_op == type_class));
850 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
851 if (clss->attr.ca.members[i] == member) {
852 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
853 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
854 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
860 void add_class_subtype(ir_type *clss, ir_type *subtype) {
862 assert(clss && (clss->type_op == type_class));
863 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
864 for (i = 0; i < get_class_n_supertypes(subtype); i++)
865 if (get_class_supertype(subtype, i) == clss)
866 /* Class already registered */
868 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
871 int get_class_n_subtypes(const ir_type *clss) {
872 assert(clss && (clss->type_op == type_class));
873 return (ARR_LEN (clss->attr.ca.subtypes));
876 ir_type *get_class_subtype(ir_type *clss, int pos) {
877 assert(clss && (clss->type_op == type_class));
878 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
879 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
882 int get_class_subtype_index(ir_type *clss, const ir_type *subclass) {
883 int i, n_subtypes = get_class_n_subtypes(clss);
884 assert(is_Class_type(subclass));
885 for (i = 0; i < n_subtypes; ++i) {
886 if (get_class_subtype(clss, i) == subclass) return i;
891 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos) {
892 assert(clss && (clss->type_op == type_class));
893 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
894 clss->attr.ca.subtypes[pos] = subtype;
897 void remove_class_subtype(ir_type *clss, ir_type *subtype) {
899 assert(clss && (clss->type_op == type_class));
900 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
901 if (clss->attr.ca.subtypes[i] == subtype) {
902 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
903 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
904 ARR_SETLEN(ir_entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
909 void add_class_supertype(ir_type *clss, ir_type *supertype) {
911 assert(clss && (clss->type_op == type_class));
912 assert(supertype && (supertype -> type_op == type_class));
913 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
914 for (i = get_class_n_subtypes(supertype) - 1; i >= 0; --i)
915 if (get_class_subtype(supertype, i) == clss)
916 /* Class already registered */
918 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
921 int get_class_n_supertypes(const ir_type *clss) {
922 assert(clss && (clss->type_op == type_class));
923 return ARR_LEN(clss->attr.ca.supertypes);
926 int get_class_supertype_index(ir_type *clss, ir_type *super_clss) {
927 int i, n_supertypes = get_class_n_supertypes(clss);
928 assert(super_clss && (super_clss->type_op == type_class));
929 for (i = 0; i < n_supertypes; i++)
930 if (get_class_supertype(clss, i) == super_clss)
935 ir_type *get_class_supertype(ir_type *clss, int pos) {
936 assert(clss && (clss->type_op == type_class));
937 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
938 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
941 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos) {
942 assert(clss && (clss->type_op == type_class));
943 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
944 clss->attr.ca.supertypes[pos] = supertype;
947 void remove_class_supertype(ir_type *clss, ir_type *supertype) {
949 assert(clss && (clss->type_op == type_class));
950 for (i = 0; i < (ARR_LEN(clss->attr.ca.supertypes)); i++)
951 if (clss->attr.ca.supertypes[i] == supertype) {
952 for(; i < (ARR_LEN(clss->attr.ca.supertypes))-1; i++)
953 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
954 ARR_SETLEN(ir_entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
959 ir_entity *get_class_type_info(const ir_type *clss) {
960 return clss->attr.ca.type_info;
963 void set_class_type_info(ir_type *clss, ir_entity *ent) {
964 clss->attr.ca.type_info = ent;
966 ent->repr_class = clss;
969 const char *get_peculiarity_name(ir_peculiarity p) {
970 #define X(a) case a: return #a
972 X(peculiarity_description);
973 X(peculiarity_inherited);
974 X(peculiarity_existent);
977 return "invalid peculiarity";
980 ir_peculiarity get_class_peculiarity(const ir_type *clss) {
981 assert(clss && (clss->type_op == type_class));
982 return clss->attr.ca.peculiarity;
985 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec) {
986 assert(clss && (clss->type_op == type_class));
987 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
988 clss->attr.ca.peculiarity = pec;
991 /* Returns the size of the virtual function table. */
992 unsigned (get_class_vtable_size)(const ir_type *clss) {
993 return _get_class_vtable_size(clss);
996 /* Sets a new size of the virtual function table. */
997 void (set_class_vtable_size)(ir_type *clss, unsigned size) {
998 _set_class_vtable_size(clss, size);
1001 /* Returns non-zero if a class is final. */
1002 int (is_class_final)(const ir_type *clss) {
1003 return _is_class_final(clss);
1006 /* Sets if a class is final. */
1007 void (set_class_final)(ir_type *clss, int flag) {
1008 _set_class_final(clss, flag);
1011 /* Returns non-zero if a class is an interface. */
1012 int (is_class_interface)(const ir_type *clss) {
1013 return _is_class_interface(clss);
1016 /* Sets the class interface flag. */
1017 void (set_class_interface)(ir_type *clss, int flag) {
1018 _set_class_interface(clss, flag);
1021 /* Returns non-zero if a class is abstract. */
1022 int (is_class_abstract)(const ir_type *clss) {
1023 return _is_class_abstract(clss);
1026 /* Sets the class abstract flag. */
1027 void (set_class_abstract)(ir_type *clss, int final) {
1028 _set_class_abstract(clss, final);
1031 void set_class_dfn(ir_type *clss, int dfn) {
1032 clss->attr.ca.dfn = dfn;
1035 int get_class_dfn(const ir_type *clss) {
1036 return (clss->attr.ca.dfn);
1040 int (is_Class_type)(const ir_type *clss) {
1041 return _is_class_type(clss);
1044 void set_class_mode(ir_type *tp, ir_mode *mode) {
1045 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1046 assert(get_type_state(tp) == layout_fixed &&
1047 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1051 void set_class_size(ir_type *tp, unsigned size) {
1055 /*----------------------------------------------------------------**/
1057 /*----------------------------------------------------------------**/
1059 /* create a new type struct */
1060 ir_type *new_d_type_struct(ident *name, dbg_info *db) {
1061 ir_type *res = new_type(type_struct, NULL, name, db);
1063 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1068 ir_type *new_type_struct(ident *name) {
1069 return new_d_type_struct (name, NULL);
1072 void free_struct_entities(ir_type *strct) {
1074 assert(strct && (strct->type_op == type_struct));
1075 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1076 free_entity(get_struct_member(strct, i));
1079 void free_struct_attrs(ir_type *strct) {
1080 assert(strct && (strct->type_op == type_struct));
1081 DEL_ARR_F(strct->attr.sa.members);
1084 /* manipulate private fields of struct */
1085 int get_struct_n_members(const ir_type *strct) {
1086 assert(strct && (strct->type_op == type_struct));
1087 return ARR_LEN(strct->attr.sa.members);
1090 void add_struct_member(ir_type *strct, ir_entity *member) {
1091 assert(strct && (strct->type_op == type_struct));
1092 assert(get_type_tpop(get_entity_type(member)) != type_method);
1093 assert(strct != get_entity_type(member) && "recursive type");
1094 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1097 ir_entity *get_struct_member(const ir_type *strct, int pos) {
1098 assert(strct && (strct->type_op == type_struct));
1099 assert(pos >= 0 && pos < get_struct_n_members(strct));
1100 return strct->attr.sa.members[pos];
1103 int get_struct_member_index(const ir_type *strct, ir_entity *mem) {
1105 assert(strct && (strct->type_op == type_struct));
1106 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1107 if (get_struct_member(strct, i) == mem)
1112 void set_struct_member(ir_type *strct, int pos, ir_entity *member) {
1113 assert(strct && (strct->type_op == type_struct));
1114 assert(pos >= 0 && pos < get_struct_n_members(strct));
1115 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1116 strct->attr.sa.members[pos] = member;
1119 void remove_struct_member(ir_type *strct, ir_entity *member) {
1121 assert(strct && (strct->type_op == type_struct));
1122 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1123 if (strct->attr.sa.members[i] == member) {
1124 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1125 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1126 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1132 int (is_Struct_type)(const ir_type *strct) {
1133 return _is_struct_type(strct);
1136 void set_struct_mode(ir_type *tp, ir_mode *mode) {
1137 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1138 assert(get_type_state(tp) == layout_fixed &&
1139 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1143 void set_struct_size(ir_type *tp, unsigned size) {
1147 /*******************************************************************/
1149 /*******************************************************************/
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
1161 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1163 ir_type *res = new_type_struct(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 = mangle_u(name, get_type_ident(elt_type));
1176 tps[i].ent = new_entity(res, id, elt_type);
1177 set_entity_allocation(tps[i].ent, allocation_parameter);
1182 /* Create a new method type.
1183 N_param is the number of parameters, n_res the number of results. */
1184 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1187 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1188 res = new_type(type_method, mode_P_code, name, 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 = xcalloc(n_param, sizeof(res->attr.ma.params[0]));
1193 res->attr.ma.value_params = NULL;
1194 res->attr.ma.n_res = n_res;
1195 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
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;
1200 res->attr.ma.irg_calling_conv = default_cc_mask;
1205 ir_type *new_type_method(ident *name, int n_param, int n_res) {
1206 return new_d_type_method(name, n_param, n_res, NULL);
1209 /* clone an existing method type */
1210 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_P_code, name, db);
1225 res->flags = tp->flags;
1226 res->size = tp->size;
1227 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1228 res->attr.ma.value_params = tp->attr.ma.value_params;
1229 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1230 res->attr.ma.value_ress = tp->attr.ma.value_ress;
1231 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1232 res->attr.ma.first_variadic_param = tp->attr.ma.first_variadic_param;
1233 res->attr.ma.additional_properties = tp->attr.ma.additional_properties;
1234 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1239 void free_method_entities(ir_type *method) {
1241 assert(method && (method->type_op == type_method));
1244 /* Attention: also frees entities in value parameter subtypes! */
1245 void free_method_attrs(ir_type *method) {
1246 assert(method && (method->type_op == type_method));
1247 free(method->attr.ma.params);
1248 free(method->attr.ma.res_type);
1249 /* cannot free it yet, type could be cloned ...
1250 if (method->attr.ma.value_params) {
1251 free_type_entities(method->attr.ma.value_params);
1252 free_type(method->attr.ma.value_params);
1255 if (method->attr.ma.value_ress) {
1256 free_type_entities(method->attr.ma.value_ress);
1257 free_type(method->attr.ma.value_ress);
1261 /* manipulate private fields of method. */
1262 int (get_method_n_params)(const ir_type *method) {
1263 return _get_method_n_params(method);
1266 /* Returns the type of the parameter at position pos of a method. */
1267 ir_type *get_method_param_type(ir_type *method, int pos) {
1269 assert(method && (method->type_op == type_method));
1270 assert(pos >= 0 && pos < get_method_n_params(method));
1271 res = method->attr.ma.params[pos].tp;
1272 assert(res != NULL && "empty method param type");
1273 return method->attr.ma.params[pos].tp = skip_tid(res);
1276 void set_method_param_type(ir_type *method, int pos, ir_type *tp) {
1277 assert(method && (method->type_op == type_method));
1278 assert(pos >= 0 && pos < get_method_n_params(method));
1279 method->attr.ma.params[pos].tp = tp;
1280 /* If information constructed set pass-by-value representation. */
1281 if (method->attr.ma.value_params) {
1282 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1283 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1287 /* Returns an ident representing the parameters name. Returns NULL if not set.
1288 For debug support only. */
1289 ident *get_method_param_ident(ir_type *method, int pos) {
1290 assert(method && (method->type_op == type_method));
1291 assert(pos >= 0 && pos < get_method_n_params(method));
1292 return method->attr.ma.params[pos].param_name;
1295 /* Returns a string representing the parameters name. Returns NULL if not set.
1296 For debug support only. */
1297 const char *get_method_param_name(ir_type *method, int pos) {
1298 ident *id = get_method_param_ident(method, pos);
1299 return id ? get_id_str(id) : NULL;
1302 /* Sets an ident representing the parameters name. For debug support only. */
1303 void set_method_param_ident(ir_type *method, int pos, ident *id) {
1304 assert(method && (method->type_op == type_method));
1305 assert(pos >= 0 && pos < get_method_n_params(method));
1306 method->attr.ma.params[pos].param_name = id;
1309 /* Returns an entity that represents the copied value argument. Only necessary
1310 for compounds passed by value. */
1311 ir_entity *get_method_value_param_ent(ir_type *method, int pos) {
1312 assert(method && (method->type_op == type_method));
1313 assert(pos >= 0 && pos < get_method_n_params(method));
1315 if (!method->attr.ma.value_params) {
1316 /* parameter value type not created yet, build */
1317 method->attr.ma.value_params
1318 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1319 get_method_n_params(method), method->attr.ma.params);
1322 * build_value_type() sets the method->attr.ma.value_params type as default if
1325 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1326 && "param type not yet set");
1327 return method->attr.ma.params[pos].ent;
1331 * Returns a type that represents the copied value arguments.
1333 ir_type *get_method_value_param_type(const ir_type *method) {
1334 assert(method && (method->type_op == type_method));
1335 return method->attr.ma.value_params;
1338 int (get_method_n_ress)(const ir_type *method) {
1339 return _get_method_n_ress(method);
1342 ir_type *get_method_res_type(ir_type *method, int pos) {
1344 assert(method && (method->type_op == type_method));
1345 assert(pos >= 0 && pos < get_method_n_ress(method));
1346 res = method->attr.ma.res_type[pos].tp;
1347 assert(res != NULL && "empty method return type");
1348 return method->attr.ma.res_type[pos].tp = skip_tid(res);
1351 void set_method_res_type(ir_type *method, int pos, ir_type *tp) {
1352 assert(method && (method->type_op == type_method));
1353 assert(pos >= 0 && pos < get_method_n_ress(method));
1354 /* set the result ir_type */
1355 method->attr.ma.res_type[pos].tp = tp;
1356 /* If information constructed set pass-by-value representation. */
1357 if (method->attr.ma.value_ress) {
1358 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1359 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1363 /* Returns an entity that represents the copied value result. Only necessary
1364 for compounds passed by value. */
1365 ir_entity *get_method_value_res_ent(ir_type *method, int pos) {
1366 assert(method && (method->type_op == type_method));
1367 assert(pos >= 0 && pos < get_method_n_ress(method));
1369 if (!method->attr.ma.value_ress) {
1370 /* result value type not created yet, build */
1371 method->attr.ma.value_ress
1372 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1373 get_method_n_ress(method), method->attr.ma.res_type);
1376 * build_value_type() sets the method->attr.ma.value_ress type as default if
1379 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1380 && "result type not yet set");
1382 return method->attr.ma.res_type[pos].ent;
1386 * Returns a type that represents the copied value results.
1388 ir_type *get_method_value_res_type(const ir_type *method) {
1389 assert(method && (method->type_op == type_method));
1390 return method->attr.ma.value_ress;
1393 /* Returns the null-terminated name of this variadicity. */
1394 const char *get_variadicity_name(variadicity vari) {
1395 #define X(a) case a: return #a
1397 X(variadicity_non_variadic);
1398 X(variadicity_variadic);
1405 variadicity get_method_variadicity(const ir_type *method) {
1406 assert(method && (method->type_op == type_method));
1407 return method->attr.ma.variadicity;
1410 void set_method_variadicity(ir_type *method, variadicity vari) {
1411 assert(method && (method->type_op == type_method));
1412 method->attr.ma.variadicity = vari;
1416 * Returns the first variadic parameter index of a type.
1417 * If this index was NOT set, the index of the last parameter
1418 * of the method type plus one is returned for variadic functions.
1419 * Non-variadic function types always return -1 here.
1421 int get_method_first_variadic_param_index(const ir_type *method) {
1422 assert(method && (method->type_op == type_method));
1424 if (method->attr.ma.variadicity == variadicity_non_variadic)
1427 if (method->attr.ma.first_variadic_param == -1)
1428 return get_method_n_params(method);
1429 return method->attr.ma.first_variadic_param;
1433 * Sets the first variadic parameter index. This allows to specify
1434 * a complete call type (containing the type of all parameters)
1435 * but still have the knowledge, which parameter must be passed as
1438 void set_method_first_variadic_param_index(ir_type *method, int index) {
1439 assert(method && (method->type_op == type_method));
1440 assert(index >= 0 && index <= get_method_n_params(method));
1442 method->attr.ma.first_variadic_param = index;
1445 unsigned (get_method_additional_properties)(const ir_type *method) {
1446 return _get_method_additional_properties(method);
1449 void (set_method_additional_properties)(ir_type *method, unsigned mask) {
1450 _set_method_additional_properties(method, mask);
1453 void (set_method_additional_property)(ir_type *method, mtp_additional_property flag) {
1454 _set_method_additional_property(method, flag);
1457 /* Returns the calling convention of an entities graph. */
1458 unsigned (get_method_calling_convention)(const ir_type *method) {
1459 return _get_method_calling_convention(method);
1462 /* Sets the calling convention of an entities graph. */
1463 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask) {
1464 _set_method_calling_convention(method, cc_mask);
1467 /* Returns the number of registers parameters, 0 means default. */
1468 unsigned get_method_n_regparams(ir_type *method) {
1469 unsigned cc = get_method_calling_convention(method);
1470 assert(IS_FASTCALL(cc));
1472 return cc & ~cc_bits;
1475 /* Sets the number of registers parameters, 0 means default. */
1476 void set_method_n_regparams(ir_type *method, unsigned n_regs) {
1477 unsigned cc = get_method_calling_convention(method);
1478 assert(IS_FASTCALL(cc));
1480 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1484 int (is_Method_type)(const ir_type *method) {
1485 return _is_method_type(method);
1488 /*-----------------------------------------------------------------*/
1490 /*-----------------------------------------------------------------*/
1492 /* create a new type uni */
1493 ir_type *new_d_type_union(ident *name, dbg_info *db) {
1494 ir_type *res = new_type(type_union, NULL, name, db);
1496 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1501 ir_type *new_type_union(ident *name) {
1502 return new_d_type_union(name, NULL);
1505 void free_union_entities(ir_type *uni) {
1507 assert(uni && (uni->type_op == type_union));
1508 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1509 free_entity(get_union_member(uni, i));
1512 void free_union_attrs (ir_type *uni) {
1513 assert(uni && (uni->type_op == type_union));
1514 DEL_ARR_F(uni->attr.ua.members);
1517 /* manipulate private fields of union */
1518 int get_union_n_members(const ir_type *uni) {
1519 assert(uni && (uni->type_op == type_union));
1520 return ARR_LEN(uni->attr.ua.members);
1523 void add_union_member(ir_type *uni, ir_entity *member) {
1524 assert(uni && (uni->type_op == type_union));
1525 assert(uni != get_entity_type(member) && "recursive type");
1526 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1529 ir_entity *get_union_member(const ir_type *uni, int pos) {
1530 assert(uni && (uni->type_op == type_union));
1531 assert(pos >= 0 && pos < get_union_n_members(uni));
1532 return uni->attr.ua.members[pos];
1535 int get_union_member_index(const ir_type *uni, ir_entity *mem) {
1537 assert(uni && (uni->type_op == type_union));
1538 for (i = 0, n = get_union_n_members(uni); i < n; ++i)
1539 if (get_union_member(uni, i) == mem)
1544 void set_union_member(ir_type *uni, int pos, ir_entity *member) {
1545 assert(uni && (uni->type_op == type_union));
1546 assert(pos >= 0 && pos < get_union_n_members(uni));
1547 uni->attr.ua.members[pos] = member;
1550 void remove_union_member(ir_type *uni, ir_entity *member) {
1552 assert(uni && (uni->type_op == type_union));
1553 for (i = 0; i < (ARR_LEN(uni->attr.ua.members)); i++)
1554 if (uni->attr.ua.members[i] == member) {
1555 for(; i < (ARR_LEN(uni->attr.ua.members))-1; i++)
1556 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1557 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1563 int (is_Union_type)(const ir_type *uni) {
1564 return _is_union_type(uni);
1567 void set_union_size(ir_type *tp, unsigned size) {
1571 /*-----------------------------------------------------------------*/
1573 /*-----------------------------------------------------------------*/
1576 /* create a new type array -- set dimension sizes independently */
1577 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type, dbg_info *db) {
1581 ir_graph *rem = current_ir_graph;
1583 assert(!is_Method_type(element_type));
1585 res = new_type(type_array, NULL, name, db);
1586 res->attr.aa.n_dimensions = n_dimensions;
1587 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1588 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1589 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1591 current_ir_graph = get_const_code_irg();
1592 unk = new_Unknown(mode_Iu);
1593 for (i = 0; i < n_dimensions; i++) {
1594 res->attr.aa.lower_bound[i] =
1595 res->attr.aa.upper_bound[i] = unk;
1596 res->attr.aa.order[i] = i;
1598 current_ir_graph = rem;
1600 res->attr.aa.element_type = element_type;
1601 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1606 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type) {
1607 return new_d_type_array(name, n_dimensions, element_type, NULL);
1610 void free_array_automatic_entities(ir_type *array) {
1611 assert(array && (array->type_op == type_array));
1612 free_entity(get_array_element_entity(array));
1615 void free_array_entities (ir_type *array) {
1617 assert(array && (array->type_op == type_array));
1620 void free_array_attrs (ir_type *array) {
1621 assert(array && (array->type_op == type_array));
1622 free(array->attr.aa.lower_bound);
1623 free(array->attr.aa.upper_bound);
1624 free(array->attr.aa.order);
1627 /* manipulate private fields of array ir_type */
1628 int get_array_n_dimensions (const ir_type *array) {
1629 assert(array && (array->type_op == type_array));
1630 return array->attr.aa.n_dimensions;
1634 set_array_bounds(ir_type *array, int dimension, ir_node * lower_bound, ir_node * upper_bound) {
1635 assert(array && (array->type_op == type_array));
1636 assert(lower_bound && "lower_bound node may not be NULL.");
1637 assert(upper_bound && "upper_bound node may not be NULL.");
1638 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1639 array->attr.aa.lower_bound[dimension] = lower_bound;
1640 array->attr.aa.upper_bound[dimension] = upper_bound;
1644 set_array_bounds_int(ir_type *array, int dimension, int lower_bound, int upper_bound) {
1645 ir_graph *rem = current_ir_graph;
1646 current_ir_graph = get_const_code_irg();
1647 set_array_bounds(array, dimension,
1648 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1649 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1650 current_ir_graph = rem;
1654 set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound) {
1655 assert(array && (array->type_op == type_array));
1656 assert(lower_bound && "lower_bound node may not be NULL.");
1657 array->attr.aa.lower_bound[dimension] = lower_bound;
1660 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound) {
1661 ir_graph *rem = current_ir_graph;
1662 current_ir_graph = get_const_code_irg();
1663 set_array_lower_bound(array, dimension,
1664 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1665 current_ir_graph = rem;
1668 set_array_upper_bound (ir_type *array, int dimension, ir_node * upper_bound) {
1669 assert(array && (array->type_op == type_array));
1670 assert(upper_bound && "upper_bound node may not be NULL.");
1671 array->attr.aa.upper_bound[dimension] = upper_bound;
1673 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound) {
1674 ir_graph *rem = current_ir_graph;
1675 current_ir_graph = get_const_code_irg();
1676 set_array_upper_bound(array, dimension,
1677 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1678 current_ir_graph = rem;
1681 int has_array_lower_bound(const ir_type *array, int dimension) {
1682 assert(array && (array->type_op == type_array));
1683 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1686 ir_node *get_array_lower_bound(const ir_type *array, int dimension) {
1687 assert(array && (array->type_op == type_array));
1688 return array->attr.aa.lower_bound[dimension];
1691 long get_array_lower_bound_int(const ir_type *array, int dimension) {
1693 assert(array && (array->type_op == type_array));
1694 node = array->attr.aa.lower_bound[dimension];
1695 assert(get_irn_op(node) == op_Const);
1696 return get_tarval_long(get_Const_tarval(node));
1699 int has_array_upper_bound(const ir_type *array, int dimension) {
1700 assert(array && (array->type_op == type_array));
1701 return get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown;
1704 ir_node *get_array_upper_bound(const ir_type *array, int dimension) {
1705 assert(array && (array->type_op == type_array));
1706 return array->attr.aa.upper_bound[dimension];
1709 long get_array_upper_bound_int(const ir_type *array, int dimension) {
1711 assert(array && (array->type_op == type_array));
1712 node = array->attr.aa.upper_bound[dimension];
1713 assert(get_irn_op(node) == op_Const);
1714 return get_tarval_long(get_Const_tarval(node));
1717 void set_array_order(ir_type *array, int dimension, int order) {
1718 assert(array && (array->type_op == type_array));
1719 array->attr.aa.order[dimension] = order;
1722 int get_array_order(const ir_type *array, int dimension) {
1723 assert(array && (array->type_op == type_array));
1724 return array->attr.aa.order[dimension];
1727 int find_array_dimension(const ir_type *array, int order) {
1730 assert(array && (array->type_op == type_array));
1732 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1733 if (array->attr.aa.order[dim] == order)
1739 void set_array_element_type(ir_type *array, ir_type *tp) {
1740 assert(array && (array->type_op == type_array));
1741 assert(!is_Method_type(tp));
1742 array->attr.aa.element_type = tp;
1745 ir_type *get_array_element_type(ir_type *array) {
1746 assert(array && (array->type_op == type_array));
1747 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1750 void set_array_element_entity(ir_type *array, ir_entity *ent) {
1751 assert(array && (array->type_op == type_array));
1752 assert((get_entity_type(ent)->type_op != type_method));
1753 array->attr.aa.element_ent = ent;
1754 array->attr.aa.element_type = get_entity_type(ent);
1757 ir_entity *get_array_element_entity(const ir_type *array) {
1758 assert(array && (array->type_op == type_array));
1759 return array->attr.aa.element_ent;
1763 int (is_Array_type)(const ir_type *array) {
1764 return _is_array_type(array);
1767 void set_array_size(ir_type *tp, unsigned size) {
1768 /* FIXME: Here we should make some checks with the element type size */
1771 /*-----------------------------------------------------------------*/
1772 /* TYPE_ENUMERATION */
1773 /*-----------------------------------------------------------------*/
1775 /* create a new type enumeration -- set the enumerators independently */
1776 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1779 assert(n_enums >= 0);
1780 res = new_type(type_enumeration, NULL, name, db);
1781 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1786 ir_type *new_type_enumeration(ident *name, int n_enums) {
1787 return new_d_type_enumeration(name, n_enums, NULL);
1790 void free_enumeration_entities(ir_type *enumeration) {
1792 assert(enumeration && (enumeration->type_op == type_enumeration));
1794 void free_enumeration_attrs(ir_type *enumeration) {
1795 assert(enumeration && (enumeration->type_op == type_enumeration));
1796 DEL_ARR_F(enumeration->attr.ea.enumer);
1799 /* manipulate fields of enumeration type. */
1800 int get_enumeration_n_enums(const ir_type *enumeration) {
1801 assert(enumeration && (enumeration->type_op == type_enumeration));
1802 return ARR_LEN(enumeration->attr.ea.enumer);
1805 /* create a new constant */
1806 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con) {
1807 assert(0 <= pos && pos < ARR_LEN(enumeration->attr.ea.enumer));
1808 enumeration->attr.ea.enumer[pos].nameid = nameid;
1809 enumeration->attr.ea.enumer[pos].value = con;
1810 enumeration->attr.ea.enumer[pos].owner = enumeration;
1813 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos) {
1814 assert(enumeration && (enumeration->type_op == type_enumeration));
1815 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1816 return &enumeration->attr.ea.enumer[pos];
1819 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst) {
1820 return enum_cnst->owner;
1823 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con) {
1824 enum_cnst->value = con;
1827 tarval *get_enumeration_value(const ir_enum_const *enum_cnst) {
1828 return enum_cnst->value;
1831 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id) {
1832 enum_cnst->nameid = id;
1835 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst) {
1836 return enum_cnst->nameid;
1839 const char *get_enumeration_name(const ir_enum_const *enum_cnst) {
1840 return get_id_str(enum_cnst->nameid);
1844 int (is_Enumeration_type)(const ir_type *enumeration) {
1845 return _is_enumeration_type(enumeration);
1848 void set_enumeration_mode(ir_type *tp, ir_mode *mode) {
1849 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1850 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1851 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1853 tp->size = get_mode_size_bytes(mode);
1857 /*-----------------------------------------------------------------*/
1859 /*-----------------------------------------------------------------*/
1861 /* Create a new type pointer */
1862 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1865 assert(mode_is_reference(ptr_mode));
1866 res = new_type(type_pointer, ptr_mode, name, db);
1867 res->attr.pa.points_to = points_to;
1868 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1869 res->size = get_mode_size_bytes(res->mode);
1870 res->flags |= tf_layout_fixed;
1875 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode) {
1876 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1879 void free_pointer_entities(ir_type *pointer) {
1881 assert(pointer && (pointer->type_op == type_pointer));
1884 void free_pointer_attrs(ir_type *pointer) {
1886 assert(pointer && (pointer->type_op == type_pointer));
1889 /* manipulate fields of type_pointer */
1890 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp) {
1891 assert(pointer && (pointer->type_op == type_pointer));
1892 pointer->attr.pa.points_to = tp;
1895 ir_type *get_pointer_points_to_type(ir_type *pointer) {
1896 assert(pointer && (pointer->type_op == type_pointer));
1897 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1901 int (is_Pointer_type)(const ir_type *pointer) {
1902 return _is_pointer_type(pointer);
1905 void set_pointer_mode(ir_type *tp, ir_mode *mode) {
1906 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1907 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1908 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1910 tp->size = get_mode_size_bytes(mode);
1914 /* Returns the first pointer type that has as points_to tp.
1915 * Not efficient: O(#types).
1916 * If not found returns firm_unknown_type. */
1917 ir_type *find_pointer_type_to_type (ir_type *tp) {
1918 int i, n = get_irp_n_types();
1919 for (i = 0; i < n; ++i) {
1920 ir_type *found = get_irp_type(i);
1921 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1924 return firm_unknown_type;
1928 /*-----------------------------------------------------------------*/
1929 /* TYPE_PRIMITIVE */
1930 /*-----------------------------------------------------------------*/
1932 /* create a new type primitive */
1933 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1934 ir_type *res = new_type(type_primitive, mode, name, db);
1935 res->size = get_mode_size_bytes(mode);
1936 res->flags |= tf_layout_fixed;
1937 res->attr.ba.base_type = NULL;
1942 ir_type *new_type_primitive(ident *name, ir_mode *mode) {
1943 return new_d_type_primitive(name, mode, NULL);
1947 int (is_Primitive_type)(const ir_type *primitive) {
1948 return _is_primitive_type(primitive);
1951 void set_primitive_mode(ir_type *tp, ir_mode *mode) {
1952 /* Modes of primitives must be data */
1953 assert(mode_is_data(mode));
1955 /* For primitive size depends on the mode. */
1956 tp->size = get_mode_size_bytes(mode);
1960 /* Return the base type of a primitive (bitfield) type or NULL if none. */
1961 ir_type *get_primitive_base_type(ir_type *tp) {
1962 assert(is_Primitive_type(tp));
1963 return tp->attr.ba.base_type;
1966 /* Sets the base type of a primitive (bitfield) type. */
1967 void set_primitive_base_type(ir_type *tp, ir_type *base_tp) {
1968 assert(is_Primitive_type(tp));
1969 tp->attr.ba.base_type = base_tp;
1972 /*-----------------------------------------------------------------*/
1973 /* common functionality */
1974 /*-----------------------------------------------------------------*/
1977 int (is_atomic_type)(const ir_type *tp) {
1978 return _is_atomic_type(tp);
1982 * Gets the number of elements in a firm compound type.
1984 int get_compound_n_members(const ir_type *tp) {
1985 const tp_op *op = get_type_tpop(tp);
1988 if (op->ops.get_n_members)
1989 res = op->ops.get_n_members(tp);
1991 assert(0 && "no member count for this type");
1997 * Gets the member of a firm compound type at position pos.
1999 ir_entity *get_compound_member(const ir_type *tp, int pos) {
2000 const tp_op *op = get_type_tpop(tp);
2001 ir_entity *res = NULL;
2003 if (op->ops.get_member)
2004 res = op->ops.get_member(tp, pos);
2006 assert(0 && "no members in this type");
2011 /* Returns index of member in tp, -1 if not contained. */
2012 int get_compound_member_index(const ir_type *tp, ir_entity *member) {
2013 const tp_op *op = get_type_tpop(tp);
2016 if (op->ops.get_member_index)
2017 index = op->ops.get_member_index(tp, member);
2019 assert(0 && "no members in this type");
2024 int is_compound_type(const ir_type *tp) {
2025 assert(tp && tp->kind == k_type);
2026 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
2029 /* Checks, whether a type is a frame type */
2030 int is_frame_type(const ir_type *tp) {
2031 return tp->flags & tf_frame_type;
2034 /* Checks, whether a type is a value parameter type */
2035 int is_value_param_type(const ir_type *tp) {
2036 return tp->flags & tf_value_param_type;
2039 /* Checks, whether a type is a lowered type */
2040 int is_lowered_type(const ir_type *tp) {
2041 return tp->flags & tf_lowered_type;
2044 /* Makes a new frame type. */
2045 ir_type *new_type_frame(ident *name) {
2046 ir_type *res = new_type_class(name);
2048 res->flags |= tf_frame_type;
2050 /* Remove type from type list. Must be treated differently than other types. */
2051 remove_irp_type(res);
2053 /* It is not possible to derive from the frame type. Set the final flag. */
2054 set_class_final(res, 1);
2059 /* Sets a lowered type for a type. This sets both associations. */
2060 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
2061 assert(is_type(tp) && is_type(lowered_type));
2062 lowered_type->flags |= tf_lowered_type;
2063 tp->assoc_type = lowered_type;
2064 lowered_type->assoc_type = tp;
2068 * Gets the lowered/unlowered type of a type or NULL if this type
2069 * has no lowered/unlowered one.
2071 ir_type *get_associated_type(const ir_type *tp) {
2072 return tp->assoc_type;
2075 /* set the type size for the unknown and none ir_type */
2076 void set_default_size(ir_type *tp, unsigned size) {
2081 * Allocate an area of size bytes aligned at alignment
2082 * at the start or the end of a frame type.
2083 * The frame type must have already an fixed layout.
2085 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, int at_start) {
2090 unsigned frame_align;
2091 int i, offset, frame_size;
2092 static unsigned area_cnt = 0;
2093 static ir_type *a_byte = NULL;
2095 assert(is_frame_type(frame_type));
2096 assert(get_type_state(frame_type) == layout_fixed);
2097 assert(get_type_alignment_bytes(frame_type) > 0);
2100 a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
2102 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2103 name = new_id_from_str(buf);
2105 /* align the size */
2106 frame_align = get_type_alignment_bytes(frame_type);
2107 size = (size + frame_align - 1) & ~(frame_align - 1);
2109 tp = new_type_array(mangle_u(get_type_ident(frame_type), name), 1, a_byte);
2110 set_array_bounds_int(tp, 0, 0, size);
2111 set_type_alignment_bytes(tp, alignment);
2113 frame_size = get_type_size_bytes(frame_type);
2115 /* fix all offsets so far */
2116 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2117 ir_entity *ent = get_class_member(frame_type, i);
2119 set_entity_offset(ent, get_entity_offset(ent) + size);
2121 /* calculate offset and new type size */
2126 /* calculate offset and new type size */
2127 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2128 frame_size = offset + size;
2131 area = new_entity(frame_type, name, tp);
2132 set_entity_offset(area, offset);
2133 set_type_size_bytes(frame_type, frame_size);
2135 /* mark this entity as compiler generated */
2136 set_entity_compiler_generated(area, 1);