3 * File name: ir/tr/type.c
4 * Purpose: Representation of types.
5 * Author: Goetz Lindenmaier
6 * Modified by: Michael Beck
9 * Copyright: (c) 2001-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 * Implementation of the datastructure to hold
20 * (C) 2001-2005 by Universitaet Karlsruhe
23 * This module supplies a datastructure to represent all types
24 * known in the compiled program. This includes types specified
25 * in the program as well as types defined by the language. In the
26 * view of the intermediate representation there is no difference
27 * between these types.
29 * There exist several kinds of types, arranged by the structure of
30 * the type. A type is described by a set of attributes. Some of
31 * these attributes are common to all types, others depend on the
34 * Types are different from the modes defined in irmode: Types are
35 * on the level of the programming language, modes at the level of
36 * the target processor.
38 * @see type_t.h type tpop
63 # include "irprog_t.h"
66 # include "typegmod.h"
73 /*-----------------------------------------------------------------*/
75 /*-----------------------------------------------------------------*/
77 type *firm_none_type; type *get_none_type(void) { return firm_none_type; }
78 type *firm_unknown_type; type *get_unknown_type(void) { return firm_unknown_type; }
82 /* Returns a new, unique number to number nodes or the like. */
83 int get_irp_new_node_nr(void);
86 /* Suffixes added to types used for pass-by-value representations. */
87 static ident *value_params_suffix = NULL;
88 static ident *value_ress_suffix = NULL;
90 /** The default calling convention for method types. */
91 static unsigned default_cc_mask;
93 /* Initialize the type module. */
94 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_bits(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_bits(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(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;
140 res->state = layout_undefined;
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(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 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(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(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 type *tp) {
188 return _get_type_link(tp);
191 void (set_type_link)(type *tp, void *l) {
192 _set_type_link(tp, l);
195 const tp_op *(get_type_tpop)(const type *tp) {
196 return _get_type_tpop(tp);
199 ident *(get_type_tpop_nameid)(const type *tp) {
200 return _get_type_tpop_nameid(tp);
203 const char* get_type_tpop_name(const 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 type *tp) {
209 return _get_type_tpop_code(tp);
212 ir_mode *(get_type_mode)(const type *tp) {
213 return _get_type_mode(tp);
216 void set_type_mode(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 type *tp) {
226 return _get_type_ident(tp);
229 void (set_type_ident)(type *tp, ident* id) {
230 _set_type_ident(tp, id);
233 /* Outputs a unique number for this node */
234 long get_type_nr(const type *tp) {
243 const char* get_type_name(const type *tp) {
244 assert(tp && tp->kind == k_type);
245 return (get_id_str(tp->name));
248 int (get_type_size_bytes)(const type *tp) {
249 return _get_type_size_bytes(tp);
252 int (get_type_size_bits)(const type *tp) {
253 return _get_type_size_bits(tp);
257 visibility get_type_visibility (const type *tp) {
259 visibility res = visibility_local;
260 if (is_compound_type(tp)) {
262 if (is_Array_type(tp)) {
263 entity *mem = get_array_element_entity(tp);
264 if (get_entity_visibility(mem) != visibility_local)
265 res = visibility_external_visible;
267 int i, n_mems = get_compound_n_members(tp);
268 for (i = 0; i < n_mems; ++i) {
269 entity *mem = get_compound_member(tp, i);
270 if (get_entity_visibility(mem) != visibility_local)
271 res = visibility_external_visible;
278 return tp->visibility;
281 void set_type_visibility (type *tp, visibility v) {
284 /* check for correctness */
285 if (v != visibility_external_allocated) {
286 visibility res = visibility_local;
287 if (is_compound_type(tp)) {
288 if (is_Array_type(tp)) {
289 entity *mem = get_array_element_entity(tp);
290 if (get_entity_visibility(mem) > res)
291 res = get_entity_visibility(mem);
293 int i, n_mems = get_compound_n_members(tp);
294 for (i = 0; i < n_mems; ++i) {
295 entity *mem = get_compound_member(tp, i);
296 if (get_entity_visibility(mem) > res)
297 res = get_entity_visibility(mem);
308 set_type_size_bits(type *tp, int size) {
309 const tp_op *tpop = get_type_tpop(tp);
311 if (tpop->ops.set_type_size)
312 tpop->ops.set_type_size(tp, size);
314 assert(0 && "Cannot set size for this type");
318 set_type_size_bytes(type *tp, int size) {
319 set_type_size_bits(tp, 8*size);
322 int get_type_alignment_bytes(type *tp) {
323 int align = get_type_alignment_bits(tp);
325 return align < 0 ? align : (align + 7) >> 3;
328 int get_type_alignment_bits(type *tp) {
334 /* alignment NOT set calculate it "on demand" */
336 align = get_mode_size_bits(tp->mode);
337 else if (is_Array_type(tp))
338 align = get_type_alignment_bits(get_array_element_type(tp));
339 else if (is_compound_type(tp)) {
340 int i, n = get_compound_n_members(tp);
343 for (i = 0; i < n; ++i) {
344 type *t = get_entity_type(get_compound_member(tp, i));
345 int a = get_type_alignment_bits(t);
351 else if (is_Method_type(tp))
361 set_type_alignment_bits(type *tp, int align) {
362 assert(tp && tp->kind == k_type);
363 /* Methods don't have an alignment. */
364 if (tp->type_op != type_method) {
370 set_type_alignment_bytes(type *tp, int align) {
371 set_type_alignment_bits(tp, 8*align);
374 /* Returns a human readable string for the enum entry. */
375 const char *get_type_state_name(type_state s) {
376 #define X(a) case a: return #a;
386 type_state (get_type_state)(const type *tp) {
387 return _get_type_state(tp);
391 set_type_state(type *tp, type_state state) {
392 assert(tp && tp->kind == k_type);
394 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
395 (tp->type_op == type_method))
398 /* Just a correctness check: */
399 if (state == layout_fixed) {
401 switch (get_type_tpop_code(tp)) {
404 assert(get_type_size_bits(tp) > -1);
405 if (tp != get_glob_type()) {
406 int n_mem = get_class_n_members(tp);
407 for (i = 0; i < n_mem; i++) {
408 if (get_entity_offset_bits(get_class_member(tp, i)) <= -1)
409 { DDMT(tp); DDME(get_class_member(tp, i)); }
410 assert(get_entity_offset_bits(get_class_member(tp, i)) > -1);
412 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
413 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
420 assert(get_type_size_bits(tp) > -1);
421 for (i = 0; i < get_struct_n_members(tp); i++) {
422 assert(get_entity_offset_bits(get_struct_member(tp, i)) > -1);
423 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
432 Assure that only innermost dimension is dynamic? */
434 case tpo_enumeration:
436 assert(get_type_mode != NULL);
437 for (i = 0; i < get_enumeration_n_enums(tp); i++)
438 assert(get_enumeration_enum(tp, i) != NULL);
446 unsigned long (get_type_visited)(const type *tp) {
447 return _get_type_visited(tp);
450 void (set_type_visited)(type *tp, unsigned long num) {
451 _set_type_visited(tp, num);
454 /* Sets visited field in type to type_visited. */
455 void (mark_type_visited)(type *tp) {
456 _mark_type_visited(tp);
459 int (type_visited)(const type *tp) {
460 return _type_visited(tp);
463 int (type_not_visited)(const type *tp) {
464 return _type_not_visited(tp);
467 int (is_type)(const void *thing) {
468 return _is_type(thing);
471 /* Checks whether two types are structural equal.*/
472 int equal_type(type *typ1, type *typ2) {
477 if (typ1 == typ2) return 1;
479 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
480 (get_type_ident(typ1) != get_type_ident(typ2)) ||
481 (get_type_mode(typ1) != get_type_mode(typ2)) ||
482 (get_type_state(typ1) != get_type_state(typ2)))
484 if ((get_type_state(typ1) == layout_fixed) &&
485 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
488 switch (get_type_tpop_code(typ1)) {
490 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
491 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
492 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
493 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
494 /** Compare the members **/
495 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
496 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
497 /* First sort the members of typ2 */
498 for (i = 0; i < get_class_n_members(typ1); i++) {
499 entity *e1 = get_class_member(typ1, i);
500 for (j = 0; j < get_class_n_members(typ2); j++) {
501 entity *e2 = get_class_member(typ2, j);
502 if (get_entity_name(e1) == get_entity_name(e2))
506 for (i = 0; i < get_class_n_members(typ1); i++) {
507 if (!m[i] || /* Found no counterpart */
508 !equal_entity(get_class_member(typ1, i), m[i]))
511 /** Compare the supertypes **/
512 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
513 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
514 /* First sort the supertypes of typ2 */
515 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
516 type *t1 = get_class_supertype(typ1, i);
517 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
518 type *t2 = get_class_supertype(typ2, j);
519 if (get_type_ident(t2) == get_type_ident(t1))
523 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
524 if (!t[i] || /* Found no counterpart */
525 get_class_supertype(typ1, i) != t[i])
530 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
531 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
532 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
533 /* First sort the members of lt */
534 for (i = 0; i < get_struct_n_members(typ1); i++) {
535 entity *e1 = get_struct_member(typ1, i);
536 for (j = 0; j < get_struct_n_members(typ2); j++) {
537 entity *e2 = get_struct_member(typ2, j);
538 if (get_entity_name(e1) == get_entity_name(e2))
542 for (i = 0; i < get_struct_n_members(typ1); i++) {
543 if (!m[i] || /* Found no counterpart */
544 !equal_entity(get_struct_member(typ1, i), m[i]))
549 int n_param1, n_param2;
551 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
552 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
553 if (get_method_calling_convention(typ1) !=
554 get_method_calling_convention(typ2)) return 0;
556 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
557 n_param1 = get_method_n_params(typ1);
558 n_param2 = get_method_n_params(typ2);
561 n_param1 = get_method_first_variadic_param_index(typ1);
562 n_param2 = get_method_first_variadic_param_index(typ2);
565 if (n_param1 != n_param2) return 0;
567 for (i = 0; i < n_param1; i++) {
568 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
571 for (i = 0; i < get_method_n_ress(typ1); i++) {
572 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
577 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
578 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
579 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
580 /* First sort the members of lt */
581 for (i = 0; i < get_union_n_members(typ1); i++) {
582 entity *e1 = get_union_member(typ1, i);
583 for (j = 0; j < get_union_n_members(typ2); j++) {
584 entity *e2 = get_union_member(typ2, j);
585 if (get_entity_name(e1) == get_entity_name(e2))
589 for (i = 0; i < get_union_n_members(typ1); i++) {
590 if (!m[i] || /* Found no counterpart */
591 !equal_entity(get_union_member(typ1, i), m[i]))
596 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
598 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
600 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
601 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
602 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
604 if (get_array_order(typ1, i) != get_array_order(typ2, i))
605 assert(0 && "type compare with different dimension orders not implemented");
608 case tpo_enumeration: {
609 assert(0 && "enumerations not implemented");
612 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
615 case tpo_primitive: {
622 /* Checks whether two types are structural comparable. */
623 int smaller_type (type *st, type *lt) {
627 if (st == lt) return 1;
629 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
632 switch(get_type_tpop_code(st)) {
634 return is_subclass_of(st, lt);
637 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
638 m = alloca(sizeof(entity *) * get_struct_n_members(st));
639 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
640 /* First sort the members of lt */
641 for (i = 0; i < get_struct_n_members(st); i++) {
642 entity *se = get_struct_member(st, i);
643 for (j = 0; j < get_struct_n_members(lt); j++) {
644 entity *le = get_struct_member(lt, j);
645 if (get_entity_name(le) == get_entity_name(se))
649 for (i = 0; i < get_struct_n_members(st); i++) {
650 if (!m[i] || /* Found no counterpart */
651 !smaller_type(get_entity_type(get_struct_member(st, i)),
652 get_entity_type(m[i])))
657 int n_param1, n_param2;
659 /** FIXME: is this still 1? */
660 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
661 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
662 if (get_method_calling_convention(st) !=
663 get_method_calling_convention(lt)) return 0;
665 if (get_method_variadicity(st) == variadicity_non_variadic) {
666 n_param1 = get_method_n_params(st);
667 n_param2 = get_method_n_params(lt);
670 n_param1 = get_method_first_variadic_param_index(st);
671 n_param2 = get_method_first_variadic_param_index(lt);
674 if (n_param1 != n_param2) return 0;
676 for (i = 0; i < get_method_n_params(st); i++) {
677 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
680 for (i = 0; i < get_method_n_ress(st); i++) {
681 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
686 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
687 m = alloca(sizeof(entity *) * get_union_n_members(st));
688 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
689 /* First sort the members of lt */
690 for (i = 0; i < get_union_n_members(st); i++) {
691 entity *se = get_union_member(st, i);
692 for (j = 0; j < get_union_n_members(lt); j++) {
693 entity *le = get_union_member(lt, j);
694 if (get_entity_name(le) == get_entity_name(se))
698 for (i = 0; i < get_union_n_members(st); i++) {
699 if (!m[i] || /* Found no counterpart */
700 !smaller_type(get_entity_type(get_union_member(st, i)),
701 get_entity_type(m[i])))
706 type *set, *let; /* small/large elt. type */
707 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
709 set = get_array_element_type(st);
710 let = get_array_element_type(lt);
712 /* If the element types are different, set must be convertible
713 to let, and they must have the same size so that address
714 computations work out. To have a size the layout must
716 if ((get_type_state(set) != layout_fixed) ||
717 (get_type_state(let) != layout_fixed))
719 if (!smaller_type(set, let) ||
720 get_type_size_bits(set) != get_type_size_bits(let))
723 for(i = 0; i < get_array_n_dimensions(st); i++) {
724 if (get_array_lower_bound(lt, i))
725 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
727 if (get_array_upper_bound(lt, i))
728 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
732 case tpo_enumeration: {
733 assert(0 && "enumerations not implemented");
736 if (!smaller_type(get_pointer_points_to_type(st),
737 get_pointer_points_to_type(lt)))
740 case tpo_primitive: {
741 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
749 /*-----------------------------------------------------------------*/
751 /*-----------------------------------------------------------------*/
753 /* create a new class type */
754 type *new_d_type_class (ident *name, dbg_info *db) {
757 res = new_type(type_class, NULL, name, db);
759 res->attr.ca.members = NEW_ARR_F (entity *, 0);
760 res->attr.ca.subtypes = NEW_ARR_F (type *, 0);
761 res->attr.ca.supertypes = NEW_ARR_F (type *, 0);
762 res->attr.ca.peculiarity = peculiarity_existent;
763 res->attr.ca.dfn = 0;
768 type *new_type_class (ident *name) {
769 return new_d_type_class (name, NULL);
772 void free_class_entities(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));
779 void free_class_attrs(type *clss) {
780 assert(clss && (clss->type_op == type_class));
781 DEL_ARR_F(clss->attr.ca.members);
782 DEL_ARR_F(clss->attr.ca.subtypes);
783 DEL_ARR_F(clss->attr.ca.supertypes);
786 /* manipulate private fields of class type */
787 void add_class_member (type *clss, entity *member) {
788 assert(clss && (clss->type_op == type_class));
789 assert(clss != get_entity_type(member) && "recursive type");
790 ARR_APP1 (entity *, clss->attr.ca.members, member);
793 int (get_class_n_members) (const type *clss) {
794 return _get_class_n_members(clss);
797 int get_class_member_index(type *clss, entity *mem) {
799 assert(clss && (clss->type_op == type_class));
800 for (i = 0; i < get_class_n_members(clss); i++)
801 if (get_class_member(clss, i) == mem)
806 entity *(get_class_member) (const type *clss, int pos) {
807 return _get_class_member(clss, pos);
810 entity *get_class_member_by_name(type *clss, ident *name) {
812 assert(clss && (clss->type_op == type_class));
813 n_mem = get_class_n_members(clss);
814 for (i = 0; i < n_mem; ++i) {
815 entity *mem = get_class_member(clss, i);
816 if (get_entity_ident(mem) == name) return mem;
821 void set_class_member (type *clss, entity *member, int pos) {
822 assert(clss && (clss->type_op == type_class));
823 assert(pos >= 0 && pos < get_class_n_members(clss));
824 clss->attr.ca.members[pos] = member;
826 void set_class_members (type *clss, entity **members, int arity) {
828 assert(clss && (clss->type_op == type_class));
829 DEL_ARR_F(clss->attr.ca.members);
830 clss->attr.ca.members = NEW_ARR_F (entity *, 0);
831 for (i = 0; i < arity; i++) {
832 set_entity_owner(members[i], clss);
833 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
836 void remove_class_member(type *clss, entity *member) {
838 assert(clss && (clss->type_op == type_class));
839 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
840 if (clss->attr.ca.members[i] == member) {
841 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
842 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
843 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
849 void add_class_subtype (type *clss, type *subtype) {
851 assert(clss && (clss->type_op == type_class));
852 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
853 for (i = 0; i < get_class_n_supertypes(subtype); i++)
854 if (get_class_supertype(subtype, i) == clss)
855 /* Class already registered */
857 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
859 int get_class_n_subtypes (const type *clss) {
860 assert(clss && (clss->type_op == type_class));
861 return (ARR_LEN (clss->attr.ca.subtypes));
863 type *get_class_subtype (type *clss, int pos) {
864 assert(clss && (clss->type_op == type_class));
865 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
866 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
868 int get_class_subtype_index(type *clss, const type *subclass) {
869 int i, n_subtypes = get_class_n_subtypes(clss);
870 assert(is_Class_type(subclass));
871 for (i = 0; i < n_subtypes; ++i) {
872 if (get_class_subtype(clss, i) == subclass) return i;
876 void set_class_subtype (type *clss, type *subtype, int pos) {
877 assert(clss && (clss->type_op == type_class));
878 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
879 clss->attr.ca.subtypes[pos] = subtype;
881 void remove_class_subtype(type *clss, type *subtype) {
883 assert(clss && (clss->type_op == type_class));
884 for (i = 0; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
885 if (clss->attr.ca.subtypes[i] == subtype) {
886 for (; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
887 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
888 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
893 void add_class_supertype (type *clss, type *supertype) {
895 assert(clss && (clss->type_op == type_class));
896 assert(supertype && (supertype -> type_op == type_class));
897 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
898 for (i = 0; i < get_class_n_subtypes(supertype); i++)
899 if (get_class_subtype(supertype, i) == clss)
900 /* Class already registered */
902 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
904 int get_class_n_supertypes (const type *clss) {
905 assert(clss && (clss->type_op == type_class));
906 return (ARR_LEN (clss->attr.ca.supertypes));
908 int get_class_supertype_index(type *clss, type *super_clss) {
909 int i, n_supertypes = get_class_n_supertypes(clss);
910 assert(super_clss && (super_clss->type_op == type_class));
911 for (i = 0; i < n_supertypes; i++)
912 if (get_class_supertype(clss, i) == super_clss)
916 type *get_class_supertype (type *clss, int pos) {
917 assert(clss && (clss->type_op == type_class));
918 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
919 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
921 void set_class_supertype (type *clss, type *supertype, int pos) {
922 assert(clss && (clss->type_op == type_class));
923 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
924 clss->attr.ca.supertypes[pos] = supertype;
926 void remove_class_supertype(type *clss, type *supertype) {
928 assert(clss && (clss->type_op == type_class));
929 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
930 if (clss->attr.ca.supertypes[i] == supertype) {
931 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
932 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
933 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
938 const char *get_peculiarity_string(peculiarity p) {
939 #define X(a) case a: return #a
941 X(peculiarity_description);
942 X(peculiarity_inherited);
943 X(peculiarity_existent);
946 return "invalid peculiarity";
949 peculiarity get_class_peculiarity (const type *clss) {
950 assert(clss && (clss->type_op == type_class));
951 return clss->attr.ca.peculiarity;
954 void set_class_peculiarity (type *clss, peculiarity pec) {
955 assert(clss && (clss->type_op == type_class));
956 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
957 clss->attr.ca.peculiarity = pec;
960 void set_class_dfn (type *clss, int dfn)
962 clss->attr.ca.dfn = dfn;
965 int get_class_dfn (const type *clss)
967 return (clss->attr.ca.dfn);
971 int (is_Class_type)(const type *clss) {
972 return _is_class_type(clss);
975 void set_class_mode(type *tp, ir_mode *mode) {
976 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
977 assert(get_type_state(tp) == layout_fixed &&
978 tp->size == get_mode_size_bits(mode) && "mode don't match class layout");
982 void set_class_size_bits(type *tp, int size) {
983 /* argh: we must allow to set negative values as "invalid size" */
984 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
985 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
988 /*----------------------------------------------------------------**/
990 /*----------------------------------------------------------------**/
992 /* create a new type struct */
993 type *new_d_type_struct(ident *name, dbg_info *db) {
994 type *res = new_type(type_struct, NULL, name, db);
996 res->attr.sa.members = NEW_ARR_F(entity *, 0);
1001 type *new_type_struct (ident *name) {
1002 return new_d_type_struct (name, NULL);
1005 void free_struct_entities (type *strct) {
1007 assert(strct && (strct->type_op == type_struct));
1008 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1009 free_entity(get_struct_member(strct, i));
1011 void free_struct_attrs (type *strct) {
1012 assert(strct && (strct->type_op == type_struct));
1013 DEL_ARR_F(strct->attr.sa.members);
1016 /* manipulate private fields of struct */
1017 int get_struct_n_members (const type *strct) {
1018 assert(strct && (strct->type_op == type_struct));
1019 return (ARR_LEN (strct->attr.sa.members));
1022 void add_struct_member (type *strct, entity *member) {
1023 assert(strct && (strct->type_op == type_struct));
1024 assert(get_type_tpop(get_entity_type(member)) != type_method);
1025 /* @@@ lowerfirm geht nicht durch */
1026 assert(strct != get_entity_type(member) && "recursive type");
1027 ARR_APP1 (entity *, strct->attr.sa.members, member);
1030 entity *get_struct_member (const type *strct, int pos) {
1031 assert(strct && (strct->type_op == type_struct));
1032 assert(pos >= 0 && pos < get_struct_n_members(strct));
1033 return strct->attr.sa.members[pos];
1036 int get_struct_member_index(type *strct, entity *mem) {
1038 assert(strct && (strct->type_op == type_struct));
1039 for (i = 0; i < get_struct_n_members(strct); i++)
1040 if (get_struct_member(strct, i) == mem)
1045 void set_struct_member (type *strct, int pos, entity *member) {
1046 assert(strct && (strct->type_op == type_struct));
1047 assert(pos >= 0 && pos < get_struct_n_members(strct));
1048 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1049 strct->attr.sa.members[pos] = member;
1051 void remove_struct_member(type *strct, entity *member) {
1053 assert(strct && (strct->type_op == type_struct));
1054 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1055 if (strct->attr.sa.members[i] == member) {
1056 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1057 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1058 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1064 int (is_Struct_type)(const type *strct) {
1065 return _is_struct_type(strct);
1068 void set_struct_mode(type *tp, ir_mode *mode) {
1069 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1070 assert(get_type_state(tp) == layout_fixed &&
1071 tp->size == get_mode_size_bits(mode) && "mode don't match struct layout");
1075 void set_struct_size_bits(type *tp, int size) {
1076 /* argh: we must allow to set negative values as "invalid size" */
1077 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1078 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1081 /*******************************************************************/
1083 /*******************************************************************/
1086 * Lazy construction of value argument / result representation.
1087 * Constructs a struct type and its member. The types of the members
1088 * are passed in the argument list.
1090 * @param name name of the type constructed
1091 * @param len number of fields
1092 * @param tps array of field types with length len
1094 static INLINE type *
1095 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1097 type *res = new_type_struct(name);
1098 /* Remove type from type list. Must be treated differently than other types. */
1099 remove_irp_type(res);
1100 for (i = 0; i < len; i++) {
1101 /* use res as default if corresponding type is not yet set. */
1102 type *elt_type = tps[i].tp ? tps[i].tp : res;
1104 tps[i].ent = new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
1109 /* Create a new method type.
1110 N_param is the number of parameters, n_res the number of results. */
1111 type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1114 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1115 res = new_type(type_method, mode_P_code, name, db);
1116 res->state = layout_fixed;
1117 res->size = get_mode_size_bits(mode_P_code);
1118 res->attr.ma.n_params = n_param;
1119 res->attr.ma.param_type = xcalloc(n_param, sizeof(res->attr.ma.param_type[0]));
1120 res->attr.ma.value_params = NULL;
1121 res->attr.ma.n_res = n_res;
1122 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1123 res->attr.ma.value_ress = NULL;
1124 res->attr.ma.variadicity = variadicity_non_variadic;
1125 res->attr.ma.first_variadic_param = -1;
1126 res->attr.ma.irg_calling_conv = default_cc_mask;
1131 type *new_type_method(ident *name, int n_param, int n_res) {
1132 return new_d_type_method(name, n_param, n_res, NULL);
1135 void free_method_entities(type *method) {
1136 assert(method && (method->type_op == type_method));
1139 /* Attention: also frees entities in value parameter subtypes! */
1140 void free_method_attrs(type *method) {
1141 assert(method && (method->type_op == type_method));
1142 free(method->attr.ma.param_type);
1143 free(method->attr.ma.res_type);
1144 if (method->attr.ma.value_params) {
1145 free_type_entities(method->attr.ma.value_params);
1146 free_type(method->attr.ma.value_params);
1148 if (method->attr.ma.value_ress) {
1149 free_type_entities(method->attr.ma.value_ress);
1150 free_type(method->attr.ma.value_ress);
1154 /* manipulate private fields of method. */
1155 int (get_method_n_params)(const type *method) {
1156 return _get_method_n_params(method);
1159 type *get_method_param_type(type *method, int pos) {
1161 assert(method && (method->type_op == type_method));
1162 assert(pos >= 0 && pos < get_method_n_params(method));
1163 res = method->attr.ma.param_type[pos].tp;
1164 assert(res != NULL && "empty method param type");
1165 return method->attr.ma.param_type[pos].tp = skip_tid(res);
1168 void set_method_param_type(type *method, int pos, type* tp) {
1169 assert(method && (method->type_op == type_method));
1170 assert(pos >= 0 && pos < get_method_n_params(method));
1171 method->attr.ma.param_type[pos].tp = tp;
1172 /* If information constructed set pass-by-value representation. */
1173 if (method->attr.ma.value_params) {
1174 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1175 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1179 /* Returns an entity that represents the copied value argument. Only necessary
1180 for compounds passed by value. */
1181 entity *get_method_value_param_ent(type *method, int pos) {
1182 assert(method && (method->type_op == type_method));
1183 assert(pos >= 0 && pos < get_method_n_params(method));
1185 if (!method->attr.ma.value_params) {
1186 /* parameter value type not created yet, build */
1187 method->attr.ma.value_params
1188 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1189 get_method_n_params(method), method->attr.ma.param_type);
1192 * build_value_type() sets the method->attr.ma.value_params type as default if
1195 assert((get_entity_type(method->attr.ma.param_type[pos].ent) != method->attr.ma.value_params)
1196 && "param type not yet set");
1197 return method->attr.ma.param_type[pos].ent;
1201 * Returns a type that represents the copied value arguments.
1203 type *get_method_value_param_type(const type *method)
1205 assert(method && (method->type_op == type_method));
1206 return method->attr.ma.value_params;
1209 int (get_method_n_ress)(const type *method) {
1210 return _get_method_n_ress(method);
1213 type *get_method_res_type(type *method, int pos) {
1215 assert(method && (method->type_op == type_method));
1216 assert(pos >= 0 && pos < get_method_n_ress(method));
1217 res = method->attr.ma.res_type[pos].tp;
1218 assert(res != NULL && "empty method return type");
1219 return method->attr.ma.res_type[pos].tp = skip_tid(res);
1222 void set_method_res_type(type *method, int pos, type* tp) {
1223 assert(method && (method->type_op == type_method));
1224 assert(pos >= 0 && pos < get_method_n_ress(method));
1225 /* set the result type */
1226 method->attr.ma.res_type[pos].tp = tp;
1227 /* If information constructed set pass-by-value representation. */
1228 if (method->attr.ma.value_ress) {
1229 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1230 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1234 /* Returns an entity that represents the copied value result. Only necessary
1235 for compounds passed by value. */
1236 entity *get_method_value_res_ent(type *method, int pos) {
1237 assert(method && (method->type_op == type_method));
1238 assert(pos >= 0 && pos < get_method_n_ress(method));
1240 if (!method->attr.ma.value_ress) {
1241 /* result value type not created yet, build */
1242 method->attr.ma.value_ress
1243 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1244 get_method_n_ress(method), method->attr.ma.res_type);
1247 * build_value_type() sets the method->attr.ma.value_ress type as default if
1250 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1251 && "result type not yet set");
1253 return method->attr.ma.res_type[pos].ent;
1257 * Returns a type that represents the copied value results.
1259 type *get_method_value_res_type(const type *method) {
1260 assert(method && (method->type_op == type_method));
1261 return method->attr.ma.value_ress;
1264 /* Returns the null-terminated name of this variadicity. */
1265 const char *get_variadicity_name(variadicity vari)
1267 #define X(a) case a: return #a
1269 X(variadicity_non_variadic);
1270 X(variadicity_variadic);
1277 variadicity get_method_variadicity(const type *method)
1279 assert(method && (method->type_op == type_method));
1280 return method->attr.ma.variadicity;
1283 void set_method_variadicity(type *method, variadicity vari)
1285 assert(method && (method->type_op == type_method));
1286 method->attr.ma.variadicity = vari;
1290 * Returns the first variadic parameter index of a type.
1291 * If this index was NOT set, the index of the last parameter
1292 * of the method type plus one is returned for variadic functions.
1293 * Non-variadic function types always return -1 here.
1295 int get_method_first_variadic_param_index(const type *method)
1297 assert(method && (method->type_op == type_method));
1299 if (method->attr.ma.variadicity == variadicity_non_variadic)
1302 if (method->attr.ma.first_variadic_param == -1)
1303 return get_method_n_params(method);
1304 return method->attr.ma.first_variadic_param;
1308 * Sets the first variadic parameter index. This allows to specify
1309 * a complete call type (containing the type of all parameters)
1310 * but still have the knowledge, which parameter must be passed as
1313 void set_method_first_variadic_param_index(type *method, int index)
1315 assert(method && (method->type_op == type_method));
1316 assert(index >= 0 && index <= get_method_n_params(method));
1318 method->attr.ma.first_variadic_param = index;
1321 unsigned (get_method_additional_properties)(const type *method) {
1322 return _get_method_additional_properties(method);
1325 void (set_method_additional_properties)(type *method, unsigned mask) {
1326 _set_method_additional_properties(method, mask);
1329 void (set_method_additional_property)(type *method, mtp_additional_property flag) {
1330 _set_method_additional_property(method, flag);
1333 /* Returns the calling convention of an entities graph. */
1334 unsigned (get_method_calling_convention)(const type *method) {
1335 return _get_method_calling_convention(method);
1338 /* Sets the calling convention of an entities graph. */
1339 void (set_method_calling_convention)(type *method, unsigned cc_mask) {
1340 _set_method_calling_convention(method, cc_mask);
1344 int (is_Method_type)(const type *method) {
1345 return _is_method_type(method);
1348 /*-----------------------------------------------------------------*/
1350 /*-----------------------------------------------------------------*/
1352 /* create a new type uni */
1353 type *new_d_type_union(ident *name, dbg_info *db) {
1354 type *res = new_type(type_union, NULL, name, db);
1356 res->attr.ua.members = NEW_ARR_F(entity *, 0);
1361 type *new_type_union(ident *name) {
1362 return new_d_type_union(name, NULL);
1365 void free_union_entities(type *uni) {
1367 assert(uni && (uni->type_op == type_union));
1368 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1369 free_entity(get_union_member(uni, i));
1372 void free_union_attrs (type *uni) {
1373 assert(uni && (uni->type_op == type_union));
1374 DEL_ARR_F(uni->attr.ua.members);
1377 /* manipulate private fields of union */
1378 int get_union_n_members (const type *uni) {
1379 assert(uni && (uni->type_op == type_union));
1380 return (ARR_LEN (uni->attr.ua.members));
1382 void add_union_member (type *uni, entity *member) {
1383 assert(uni && (uni->type_op == type_union));
1384 assert(uni != get_entity_type(member) && "recursive type");
1385 ARR_APP1 (entity *, uni->attr.ua.members, member);
1387 entity *get_union_member (const type *uni, int pos) {
1388 assert(uni && (uni->type_op == type_union));
1389 assert(pos >= 0 && pos < get_union_n_members(uni));
1390 return uni->attr.ua.members[pos];
1392 void set_union_member (type *uni, int pos, entity *member) {
1393 assert(uni && (uni->type_op == type_union));
1394 assert(pos >= 0 && pos < get_union_n_members(uni));
1395 uni->attr.ua.members[pos] = member;
1397 void remove_union_member(type *uni, entity *member) {
1399 assert(uni && (uni->type_op == type_union));
1400 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1401 if (uni->attr.ua.members[i] == member) {
1402 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1403 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1404 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1410 int (is_Union_type)(const type *uni) {
1411 return _is_union_type(uni);
1414 void set_union_size_bits(type *tp, int size) {
1415 /* argh: we must allow to set negative values as "invalid size" */
1416 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1417 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1420 /*-----------------------------------------------------------------*/
1422 /*-----------------------------------------------------------------*/
1425 /* create a new type array -- set dimension sizes independently */
1426 type *new_d_type_array(ident *name, int n_dimensions, type *element_type, dbg_info *db) {
1430 ir_graph *rem = current_ir_graph;
1432 assert(!is_Method_type(element_type));
1434 res = new_type(type_array, NULL, name, db);
1435 res->attr.aa.n_dimensions = n_dimensions;
1436 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1437 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1438 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1440 current_ir_graph = get_const_code_irg();
1441 unk = new_Unknown( mode_Iu);
1442 for (i = 0; i < n_dimensions; i++) {
1443 res->attr.aa.lower_bound[i] =
1444 res->attr.aa.upper_bound[i] = unk;
1445 res->attr.aa.order[i] = i;
1447 current_ir_graph = rem;
1449 res->attr.aa.element_type = element_type;
1450 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1455 type *new_type_array(ident *name, int n_dimensions, type *element_type) {
1456 return new_d_type_array(name, n_dimensions, element_type, NULL);
1459 void free_array_automatic_entities(type *array) {
1460 assert(array && (array->type_op == type_array));
1461 free_entity(get_array_element_entity(array));
1464 void free_array_entities (type *array) {
1465 assert(array && (array->type_op == type_array));
1468 void free_array_attrs (type *array) {
1469 assert(array && (array->type_op == type_array));
1470 free(array->attr.aa.lower_bound);
1471 free(array->attr.aa.upper_bound);
1474 /* manipulate private fields of array type */
1475 int get_array_n_dimensions (const type *array) {
1476 assert(array && (array->type_op == type_array));
1477 return array->attr.aa.n_dimensions;
1481 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1482 ir_node * upper_bound) {
1483 assert(array && (array->type_op == type_array));
1484 assert(lower_bound && "lower_bound node may not be NULL.");
1485 assert(upper_bound && "upper_bound node may not be NULL.");
1486 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1487 array->attr.aa.lower_bound[dimension] = lower_bound;
1488 array->attr.aa.upper_bound[dimension] = upper_bound;
1491 set_array_bounds_int (type *array, int dimension, int lower_bound,
1493 ir_graph *rem = current_ir_graph;
1494 current_ir_graph = get_const_code_irg();
1495 set_array_bounds (array, dimension,
1496 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1497 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1498 current_ir_graph = rem;
1501 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1502 assert(array && (array->type_op == type_array));
1503 assert(lower_bound && "lower_bound node may not be NULL.");
1504 array->attr.aa.lower_bound[dimension] = lower_bound;
1506 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1507 ir_graph *rem = current_ir_graph;
1508 current_ir_graph = get_const_code_irg();
1509 set_array_lower_bound (array, dimension,
1510 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1511 current_ir_graph = rem;
1514 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1515 assert(array && (array->type_op == type_array));
1516 assert(upper_bound && "upper_bound node may not be NULL.");
1517 array->attr.aa.upper_bound[dimension] = upper_bound;
1519 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1520 ir_graph *rem = current_ir_graph;
1521 current_ir_graph = get_const_code_irg();
1522 set_array_upper_bound (array, dimension,
1523 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1524 current_ir_graph = rem;
1526 int has_array_lower_bound (const type *array, int dimension) {
1527 assert(array && (array->type_op == type_array));
1528 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1530 ir_node *get_array_lower_bound (const type *array, int dimension) {
1531 assert(array && (array->type_op == type_array));
1532 return array->attr.aa.lower_bound[dimension];
1534 long get_array_lower_bound_int (const type *array, int dimension) {
1536 assert(array && (array->type_op == type_array));
1537 node = array->attr.aa.lower_bound[dimension];
1538 assert(get_irn_op(node) == op_Const);
1539 return get_tarval_long(get_Const_tarval(node));
1541 int has_array_upper_bound (const type *array, int dimension) {
1542 assert(array && (array->type_op == type_array));
1543 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1545 ir_node * get_array_upper_bound (const type *array, int dimension) {
1546 assert(array && (array->type_op == type_array));
1547 return array->attr.aa.upper_bound[dimension];
1549 long get_array_upper_bound_int (const type *array, int dimension) {
1551 assert(array && (array->type_op == type_array));
1552 node = array->attr.aa.upper_bound[dimension];
1553 assert(get_irn_op(node) == op_Const);
1554 return get_tarval_long(get_Const_tarval(node));
1557 void set_array_order (type *array, int dimension, int order) {
1558 assert(array && (array->type_op == type_array));
1559 array->attr.aa.order[dimension] = order;
1562 int get_array_order (const type *array, int dimension) {
1563 assert(array && (array->type_op == type_array));
1564 return array->attr.aa.order[dimension];
1567 int find_array_dimension(const type *array, int order) {
1570 assert(array && (array->type_op == type_array));
1572 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1573 if (array->attr.aa.order[dim] == order)
1579 void set_array_element_type (type *array, type *tp) {
1580 assert(array && (array->type_op == type_array));
1581 assert(!is_Method_type(tp));
1582 array->attr.aa.element_type = tp;
1584 type *get_array_element_type (type *array) {
1585 assert(array && (array->type_op == type_array));
1586 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1589 void set_array_element_entity (type *array, entity *ent) {
1590 assert(array && (array->type_op == type_array));
1591 assert((get_entity_type(ent)->type_op != type_method));
1592 array->attr.aa.element_ent = ent;
1593 array->attr.aa.element_type = get_entity_type(ent);
1595 entity *get_array_element_entity (const type *array) {
1596 assert(array && (array->type_op == type_array));
1597 return array->attr.aa.element_ent;
1601 int (is_Array_type)(const type *array) {
1602 return _is_array_type(array);
1605 void set_array_size_bits(type *tp, int size) {
1606 /* FIXME: Here we should make some checks with the element type size */
1609 /*-----------------------------------------------------------------*/
1610 /* TYPE_ENUMERATION */
1611 /*-----------------------------------------------------------------*/
1613 /* create a new type enumeration -- set the enumerators independently */
1614 type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1615 type *res = new_type(type_enumeration, NULL, name, db);
1617 res->attr.ea.n_enums = n_enums;
1618 res->attr.ea.enumer = xcalloc(n_enums, sizeof(res->attr.ea.enumer[0]));
1619 res->attr.ea.enum_nameid = xcalloc(n_enums, sizeof(res->attr.ea.enum_nameid[0]));
1624 type *new_type_enumeration(ident *name, int n_enums) {
1625 return new_d_type_enumeration(name, n_enums, NULL);
1628 void free_enumeration_entities(type *enumeration) {
1629 assert(enumeration && (enumeration->type_op == type_enumeration));
1631 void free_enumeration_attrs(type *enumeration) {
1632 assert(enumeration && (enumeration->type_op == type_enumeration));
1633 free(enumeration->attr.ea.enumer);
1634 free(enumeration->attr.ea.enum_nameid);
1637 /* manipulate fields of enumeration type. */
1638 int get_enumeration_n_enums (const type *enumeration) {
1639 assert(enumeration && (enumeration->type_op == type_enumeration));
1640 return enumeration->attr.ea.n_enums;
1642 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1643 assert(enumeration && (enumeration->type_op == type_enumeration));
1644 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1645 enumeration->attr.ea.enumer[pos] = con;
1647 tarval *get_enumeration_enum (const type *enumeration, int pos) {
1648 assert(enumeration && (enumeration->type_op == type_enumeration));
1649 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1650 return enumeration->attr.ea.enumer[pos];
1652 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1653 assert(enumeration && (enumeration->type_op == type_enumeration));
1654 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1655 enumeration->attr.ea.enum_nameid[pos] = id;
1657 ident *get_enumeration_nameid (const type *enumeration, int pos) {
1658 assert(enumeration && (enumeration->type_op == type_enumeration));
1659 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1660 return enumeration->attr.ea.enum_nameid[pos];
1662 const char *get_enumeration_name(const type *enumeration, int pos) {
1663 assert(enumeration && (enumeration->type_op == type_enumeration));
1664 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1665 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1669 int (is_Enumeration_type)(const type *enumeration) {
1670 return _is_enumeration_type(enumeration);
1673 void set_enumeration_mode(type *tp, ir_mode *mode) {
1674 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1675 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1676 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1678 tp->size = get_mode_size_bits(mode);
1682 /*-----------------------------------------------------------------*/
1684 /*-----------------------------------------------------------------*/
1686 /* Create a new type pointer */
1687 type *new_d_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1690 assert(mode_is_reference(ptr_mode));
1691 res = new_type(type_pointer, ptr_mode, name, db);
1692 res->attr.pa.points_to = points_to;
1693 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1694 res->size = get_mode_size_bits(res->mode);
1695 res->state = layout_fixed;
1700 type *new_type_pointer(ident *name, type *points_to, ir_mode *ptr_mode) {
1701 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1704 void free_pointer_entities (type *pointer) {
1705 assert(pointer && (pointer->type_op == type_pointer));
1708 void free_pointer_attrs (type *pointer) {
1709 assert(pointer && (pointer->type_op == type_pointer));
1712 /* manipulate fields of type_pointer */
1713 void set_pointer_points_to_type (type *pointer, type *tp) {
1714 assert(pointer && (pointer->type_op == type_pointer));
1715 pointer->attr.pa.points_to = tp;
1718 type *get_pointer_points_to_type (type *pointer) {
1719 assert(pointer && (pointer->type_op == type_pointer));
1720 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1724 int (is_Pointer_type)(const type *pointer) {
1725 return _is_pointer_type(pointer);
1728 void set_pointer_mode(type *tp, ir_mode *mode) {
1729 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1730 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1731 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1733 tp->size = get_mode_size_bits(mode);
1737 /* Returns the first pointer type that has as points_to tp.
1738 * Not efficient: O(#types).
1739 * If not found returns firm_unknown_type. */
1740 type *find_pointer_type_to_type (type *tp) {
1742 for (i = 0; i < get_irp_n_types(); ++i) {
1743 type *found = get_irp_type(i);
1744 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1747 return firm_unknown_type;
1751 /*-----------------------------------------------------------------*/
1752 /* TYPE_PRIMITIVE */
1753 /*-----------------------------------------------------------------*/
1755 /* create a new type primitive */
1756 type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1758 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1759 res = new_type(type_primitive, mode, name, db);
1760 res->size = get_mode_size_bits(mode);
1761 res->state = layout_fixed;
1766 type *new_type_primitive(ident *name, ir_mode *mode) {
1767 return new_d_type_primitive(name, mode, NULL);
1771 int (is_Primitive_type)(const type *primitive) {
1772 return _is_primitive_type(primitive);
1775 void set_primitive_mode(type *tp, ir_mode *mode) {
1776 /* Modes of primitives must be data */
1777 assert(mode_is_data(mode));
1779 /* For primitive size depends on the mode. */
1780 tp->size = get_mode_size_bits(mode);
1785 /*-----------------------------------------------------------------*/
1786 /* common functionality */
1787 /*-----------------------------------------------------------------*/
1790 int (is_atomic_type)(const type *tp) {
1791 return _is_atomic_type(tp);
1795 * Gets the number of elements in a firm compound type.
1797 int get_compound_n_members(const type *tp)
1799 const tp_op *op = get_type_tpop(tp);
1802 if (op->ops.get_n_members)
1803 res = op->ops.get_n_members(tp);
1805 assert(0 && "no member count for this type");
1811 * Gets the member of a firm compound type at position pos.
1813 entity *get_compound_member(const type *tp, int pos)
1815 const tp_op *op = get_type_tpop(tp);
1818 if (op->ops.get_member)
1819 res = op->ops.get_member(tp, pos);
1821 assert(0 && "no members in this type");
1826 int is_compound_type(const type *tp) {
1827 assert(tp && tp->kind == k_type);
1828 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1831 /* Checks, whether a type is a frame type */
1832 int is_frame_type(const type *tp) {
1833 return tp->frame_type;
1836 /* Makes a new frame type. */
1837 type *new_type_frame(ident *name)
1839 type *res = new_type_class(name);
1841 res->frame_type = 1;
1843 /* Remove type from type list. Must be treated differently than other types. */
1844 remove_irp_type(res);
1849 /* set the type size for the unknown and none type */
1850 void set_default_size_bits(type *tp, int size) {