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
76 /*-----------------------------------------------------------------*/
78 /*-----------------------------------------------------------------*/
80 ir_type *firm_none_type; ir_type *get_none_type(void) { return firm_none_type; }
81 ir_type *firm_unknown_type; ir_type *get_unknown_type(void) { return firm_unknown_type; }
84 /* Suffixes added to types used for pass-by-value representations. */
85 static ident *value_params_suffix = NULL;
86 static ident *value_ress_suffix = NULL;
88 /** The default calling convention for method types. */
89 static unsigned default_cc_mask;
91 /* return the default calling convention for method types */
92 unsigned get_default_cc_mask(void) {
93 return default_cc_mask;
96 /* Initialize the type module. */
97 void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask)
99 default_cc_mask = def_cc_mask;
100 value_params_suffix = new_id_from_str(VALUE_PARAMS_SUFFIX);
101 value_ress_suffix = new_id_from_str(VALUE_RESS_SUFFIX);
103 /* construct none and unknown type. */
104 firm_none_type = new_type(tpop_none, mode_BAD, new_id_from_str("type_none"), builtin_db);
105 set_type_size_bits(firm_none_type, 0);
106 set_type_state (firm_none_type, layout_fixed);
107 remove_irp_type(firm_none_type);
109 firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
110 set_type_size_bits(firm_unknown_type, 0);
111 set_type_state (firm_unknown_type, layout_fixed);
112 remove_irp_type(firm_unknown_type);
115 /** the global type visited flag */
116 unsigned long firm_type_visited;
118 void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
119 unsigned long (get_master_type_visited)(void) { return _get_master_type_visited(); }
120 void (inc_master_type_visited)(void) { _inc_master_type_visited(); }
123 * Creates a new type representation.
126 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
130 assert(type_op != type_id);
131 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
133 node_size = offsetof(ir_type, attr) + type_op->attr_size;
134 res = xmalloc(node_size);
135 memset(res, 0, node_size);
138 res->type_op = type_op;
141 res->visibility = visibility_external_allocated;
142 res->flags = tf_none;
148 res->assoc_type = NULL;
150 res->nr = get_irp_new_node_nr();
151 #endif /* defined DEBUG_libfirm */
153 add_irp_type(res); /* Remember the new type global. */
158 void free_type(ir_type *tp) {
159 const tp_op *op = get_type_tpop(tp);
161 if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
163 /* Remove from list of all types */
165 /* Free the attributes of the type. */
167 /* Free entities automatically allocated with the ir_type */
168 if (op->ops.free_auto_entities)
169 op->ops.free_auto_entities(tp);
170 /* And now the type itself... */
175 void free_type_entities(ir_type *tp) {
176 const tp_op *tpop = get_type_tpop(tp);
178 if (tpop->ops.free_entities)
179 tpop->ops.free_entities(tp);
182 void free_type_attrs(ir_type *tp) {
183 const tp_op *tpop = get_type_tpop(tp);
185 if (tpop->ops.free_attrs)
186 tpop->ops.free_attrs(tp);
189 /* set/get the link field */
190 void *(get_type_link)(const ir_type *tp) {
191 return _get_type_link(tp);
194 void (set_type_link)(ir_type *tp, void *l) {
195 _set_type_link(tp, l);
198 const tp_op *(get_type_tpop)(const ir_type *tp) {
199 return _get_type_tpop(tp);
202 ident *(get_type_tpop_nameid)(const ir_type *tp) {
203 return _get_type_tpop_nameid(tp);
206 const char* get_type_tpop_name(const ir_type *tp) {
207 assert(tp && tp->kind == k_type);
208 return get_id_str(tp->type_op->name);
211 tp_opcode (get_type_tpop_code)(const ir_type *tp) {
212 return _get_type_tpop_code(tp);
215 ir_mode *(get_type_mode)(const ir_type *tp) {
216 return _get_type_mode(tp);
219 void set_type_mode(ir_type *tp, ir_mode *mode) {
220 const tp_op *tpop = get_type_tpop(tp);
222 if (tpop->ops.set_type_mode)
223 tpop->ops.set_type_mode(tp, mode);
225 assert(0 && "setting a mode is NOT allowed for this type");
228 ident *(get_type_ident)(const ir_type *tp) {
229 return _get_type_ident(tp);
232 void (set_type_ident)(ir_type *tp, ident* id) {
233 _set_type_ident(tp, id);
236 /* Outputs a unique number for this node */
237 long get_type_nr(const ir_type *tp) {
242 return (long)PTR_TO_INT(tp);
246 const char* get_type_name(const ir_type *tp) {
247 assert(tp && tp->kind == k_type);
248 return (get_id_str(tp->name));
251 int (get_type_size_bytes)(const ir_type *tp) {
252 return _get_type_size_bytes(tp);
255 int (get_type_size_bits)(const ir_type *tp) {
256 return _get_type_size_bits(tp);
260 ir_visibility get_type_visibility (const ir_type *tp) {
262 visibility res = visibility_local;
263 if (is_compound_type(tp)) {
265 if (is_Array_type(tp)) {
266 ir_entity *mem = get_array_element_entity(tp);
267 if (get_entity_visibility(mem) != visibility_local)
268 res = visibility_external_visible;
270 int i, n_mems = get_compound_n_members(tp);
271 for (i = 0; i < n_mems; ++i) {
272 ir_entity *mem = get_compound_member(tp, i);
273 if (get_entity_visibility(mem) != visibility_local)
274 res = visibility_external_visible;
281 return tp->visibility;
284 void set_type_visibility (ir_type *tp, ir_visibility v) {
287 /* check for correctness */
288 if (v != visibility_external_allocated) {
289 visibility res = visibility_local;
290 if (is_compound_type(tp)) {
291 if (is_Array_type(tp)) {
292 ir_entity *mem = get_array_element_entity(tp);
293 if (get_entity_visibility(mem) > res)
294 res = get_entity_visibility(mem);
296 int i, n_mems = get_compound_n_members(tp);
297 for (i = 0; i < n_mems; ++i) {
298 ir_entity *mem = get_compound_member(tp, i);
299 if (get_entity_visibility(mem) > res)
300 res = get_entity_visibility(mem);
311 set_type_size_bits(ir_type *tp, int size) {
312 const tp_op *tpop = get_type_tpop(tp);
314 if (tpop->ops.set_type_size)
315 tpop->ops.set_type_size(tp, size);
317 assert(0 && "Cannot set size for this type");
321 set_type_size_bytes(ir_type *tp, int size) {
322 set_type_size_bits(tp, 8*size);
325 int get_type_alignment_bytes(ir_type *tp) {
326 int align = get_type_alignment_bits(tp);
328 return align < 0 ? align : (align + 7) >> 3;
331 int get_type_alignment_bits(ir_type *tp) {
337 /* alignment NOT set calculate it "on demand" */
339 align = get_mode_size_bits(tp->mode);
340 else if (is_Array_type(tp))
341 align = get_type_alignment_bits(get_array_element_type(tp));
342 else if (is_compound_type(tp)) {
343 int i, n = get_compound_n_members(tp);
346 for (i = 0; i < n; ++i) {
347 ir_type *t = get_entity_type(get_compound_member(tp, i));
348 int a = get_type_alignment_bits(t);
354 else if (is_Method_type(tp))
364 set_type_alignment_bits(ir_type *tp, int align) {
365 assert(tp && tp->kind == k_type);
366 assert((align == -1 || (align & (align - 1)) == 0) && "type alignment not power of two");
367 /* Methods don't have an alignment. */
368 if (tp->type_op != type_method) {
374 set_type_alignment_bytes(ir_type *tp, int align) {
376 set_type_alignment_bits(tp, -1);
378 set_type_alignment_bits(tp, 8*align);
382 /* Returns a human readable string for the enum entry. */
383 const char *get_type_state_name(type_state s) {
384 #define X(a) case a: return #a;
394 type_state (get_type_state)(const ir_type *tp) {
395 return _get_type_state(tp);
399 set_type_state(ir_type *tp, type_state state) {
400 assert(tp && tp->kind == k_type);
402 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
403 (tp->type_op == type_method))
406 /* Just a correctness check: */
407 if (state == layout_fixed) {
409 switch (get_type_tpop_code(tp)) {
411 assert(get_type_size_bits(tp) > -1);
412 if (tp != get_glob_type()) {
413 int n_mem = get_class_n_members(tp);
414 for (i = 0; i < n_mem; i++) {
415 assert(get_entity_offset(get_class_member(tp, i)) > -1);
417 assert(is_Method_type(get_entity_type(get_class_member(tp, i))) ||
418 (get_entity_allocation(get_class_member(tp, i)) == allocation_automatic));
424 assert(get_type_size_bits(tp) > -1);
425 for (i = 0; i < get_struct_n_members(tp); i++) {
426 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
427 assert((get_entity_allocation(get_struct_member(tp, i)) == allocation_automatic));
436 Assure that only innermost dimension is dynamic? */
438 case tpo_enumeration:
440 assert(get_type_mode != NULL);
441 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
442 ir_enum_const *ec = get_enumeration_const(tp, i);
443 tarval *tv = get_enumeration_value(ec);
444 assert(tv != NULL && tv != tarval_bad);
451 if (state == layout_fixed)
452 tp->flags |= tf_layout_fixed;
454 tp->flags &= ~tf_layout_fixed;
457 unsigned long (get_type_visited)(const ir_type *tp) {
458 return _get_type_visited(tp);
461 void (set_type_visited)(ir_type *tp, unsigned long num) {
462 _set_type_visited(tp, num);
465 /* Sets visited field in type to type_visited. */
466 void (mark_type_visited)(ir_type *tp) {
467 _mark_type_visited(tp);
470 int (type_visited)(const ir_type *tp) {
471 return _type_visited(tp);
474 int (type_not_visited)(const ir_type *tp) {
475 return _type_not_visited(tp);
478 int (is_type)(const void *thing) {
479 return _is_type(thing);
482 /* Checks whether two types are structural equal.*/
483 int equal_type(ir_type *typ1, ir_type *typ2) {
488 if (typ1 == typ2) return 1;
490 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
491 (get_type_ident(typ1) != get_type_ident(typ2)) ||
492 (get_type_mode(typ1) != get_type_mode(typ2)) ||
493 (get_type_state(typ1) != get_type_state(typ2)))
495 if ((get_type_state(typ1) == layout_fixed) &&
496 (get_type_size_bits(typ1) != get_type_size_bits(typ2)))
499 switch (get_type_tpop_code(typ1)) {
501 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
502 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
503 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
504 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
505 /** Compare the members **/
506 m = alloca(sizeof(ir_entity *) * get_class_n_members(typ1));
507 memset(m, 0, sizeof(ir_entity *) * get_class_n_members(typ1));
508 /* First sort the members of typ2 */
509 for (i = 0; i < get_class_n_members(typ1); i++) {
510 ir_entity *e1 = get_class_member(typ1, i);
511 for (j = 0; j < get_class_n_members(typ2); j++) {
512 ir_entity *e2 = get_class_member(typ2, j);
513 if (get_entity_name(e1) == get_entity_name(e2))
517 for (i = 0; i < get_class_n_members(typ1); i++) {
518 if (!m[i] || /* Found no counterpart */
519 !equal_entity(get_class_member(typ1, i), m[i]))
522 /** Compare the supertypes **/
523 t = alloca(sizeof(ir_entity *) * get_class_n_supertypes(typ1));
524 memset(t, 0, sizeof(ir_entity *) * get_class_n_supertypes(typ1));
525 /* First sort the supertypes of typ2 */
526 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
527 ir_type *t1 = get_class_supertype(typ1, i);
528 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
529 ir_type *t2 = get_class_supertype(typ2, j);
530 if (get_type_ident(t2) == get_type_ident(t1))
534 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
535 if (!t[i] || /* Found no counterpart */
536 get_class_supertype(typ1, i) != t[i])
541 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
542 m = alloca(sizeof(ir_entity *) * get_struct_n_members(typ1));
543 memset(m, 0, sizeof(ir_entity *) * get_struct_n_members(typ1));
544 /* First sort the members of lt */
545 for (i = 0; i < get_struct_n_members(typ1); i++) {
546 ir_entity *e1 = get_struct_member(typ1, i);
547 for (j = 0; j < get_struct_n_members(typ2); j++) {
548 ir_entity *e2 = get_struct_member(typ2, j);
549 if (get_entity_name(e1) == get_entity_name(e2))
553 for (i = 0; i < get_struct_n_members(typ1); i++) {
554 if (!m[i] || /* Found no counterpart */
555 !equal_entity(get_struct_member(typ1, i), m[i]))
560 int n_param1, n_param2;
562 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
563 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
564 if (get_method_calling_convention(typ1) !=
565 get_method_calling_convention(typ2)) return 0;
567 if (get_method_variadicity(typ1) == variadicity_non_variadic) {
568 n_param1 = get_method_n_params(typ1);
569 n_param2 = get_method_n_params(typ2);
572 n_param1 = get_method_first_variadic_param_index(typ1);
573 n_param2 = get_method_first_variadic_param_index(typ2);
576 if (n_param1 != n_param2) return 0;
578 for (i = 0; i < n_param1; i++) {
579 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
582 for (i = 0; i < get_method_n_ress(typ1); i++) {
583 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
588 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
589 m = alloca(sizeof(ir_entity *) * get_union_n_members(typ1));
590 memset(m, 0, sizeof(ir_entity *) * get_union_n_members(typ1));
591 /* First sort the members of lt */
592 for (i = 0; i < get_union_n_members(typ1); i++) {
593 ir_entity *e1 = get_union_member(typ1, i);
594 for (j = 0; j < get_union_n_members(typ2); j++) {
595 ir_entity *e2 = get_union_member(typ2, j);
596 if (get_entity_name(e1) == get_entity_name(e2))
600 for (i = 0; i < get_union_n_members(typ1); i++) {
601 if (!m[i] || /* Found no counterpart */
602 !equal_entity(get_union_member(typ1, i), m[i]))
607 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
609 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
611 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
612 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
613 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
615 if (get_array_order(typ1, i) != get_array_order(typ2, i))
616 assert(0 && "type compare with different dimension orders not implemented");
619 case tpo_enumeration: {
620 assert(0 && "enumerations not implemented");
623 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
626 case tpo_primitive: {
633 /* Checks whether two types are structural comparable. */
634 int smaller_type (ir_type *st, ir_type *lt) {
638 if (st == lt) return 1;
640 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
643 switch(get_type_tpop_code(st)) {
645 return is_SubClass_of(st, lt);
648 if (get_struct_n_members(st) != get_struct_n_members(lt)) return 0;
649 m = alloca(sizeof(ir_entity *) * get_struct_n_members(st));
650 memset(m, 0, sizeof(ir_entity *) * get_struct_n_members(st));
651 /* First sort the members of lt */
652 for (i = 0; i < get_struct_n_members(st); i++) {
653 ir_entity *se = get_struct_member(st, i);
654 for (j = 0; j < get_struct_n_members(lt); j++) {
655 ir_entity *le = get_struct_member(lt, j);
656 if (get_entity_name(le) == get_entity_name(se))
660 for (i = 0; i < get_struct_n_members(st); i++) {
661 if (!m[i] || /* Found no counterpart */
662 !smaller_type(get_entity_type(get_struct_member(st, i)),
663 get_entity_type(m[i])))
668 int n_param1, n_param2;
670 /** FIXME: is this still 1? */
671 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
672 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
673 if (get_method_calling_convention(st) !=
674 get_method_calling_convention(lt)) return 0;
676 if (get_method_variadicity(st) == variadicity_non_variadic) {
677 n_param1 = get_method_n_params(st);
678 n_param2 = get_method_n_params(lt);
681 n_param1 = get_method_first_variadic_param_index(st);
682 n_param2 = get_method_first_variadic_param_index(lt);
685 if (n_param1 != n_param2) return 0;
687 for (i = 0; i < get_method_n_params(st); i++) {
688 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
691 for (i = 0; i < get_method_n_ress(st); i++) {
692 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
697 if (get_union_n_members(st) != get_union_n_members(lt)) return 0;
698 m = alloca(sizeof(ir_entity *) * get_union_n_members(st));
699 memset(m, 0, sizeof(ir_entity *) * get_union_n_members(st));
700 /* First sort the members of lt */
701 for (i = 0; i < get_union_n_members(st); i++) {
702 ir_entity *se = get_union_member(st, i);
703 for (j = 0; j < get_union_n_members(lt); j++) {
704 ir_entity *le = get_union_member(lt, j);
705 if (get_entity_name(le) == get_entity_name(se))
709 for (i = 0; i < get_union_n_members(st); i++) {
710 if (!m[i] || /* Found no counterpart */
711 !smaller_type(get_entity_type(get_union_member(st, i)),
712 get_entity_type(m[i])))
717 ir_type *set, *let; /* small/large elt. ir_type */
718 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
720 set = get_array_element_type(st);
721 let = get_array_element_type(lt);
723 /* If the element types are different, set must be convertible
724 to let, and they must have the same size so that address
725 computations work out. To have a size the layout must
727 if ((get_type_state(set) != layout_fixed) ||
728 (get_type_state(let) != layout_fixed))
730 if (!smaller_type(set, let) ||
731 get_type_size_bits(set) != get_type_size_bits(let))
734 for(i = 0; i < get_array_n_dimensions(st); i++) {
735 if (get_array_lower_bound(lt, i))
736 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
738 if (get_array_upper_bound(lt, i))
739 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
743 case tpo_enumeration: {
744 assert(0 && "enumerations not implemented");
747 if (!smaller_type(get_pointer_points_to_type(st),
748 get_pointer_points_to_type(lt)))
751 case tpo_primitive: {
752 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
760 /*-----------------------------------------------------------------*/
762 /*-----------------------------------------------------------------*/
764 /* create a new class ir_type */
765 ir_type *new_d_type_class (ident *name, dbg_info *db) {
768 res = new_type(type_class, NULL, name, db);
770 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
771 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
772 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
773 res->attr.ca.peculiarity = peculiarity_existent;
774 res->attr.ca.type_info = NULL;
775 res->attr.ca.vtable_size = 0;
776 res->attr.ca.clss_flags = cf_none;
777 res->attr.ca.dfn = 0;
782 ir_type *new_type_class (ident *name) {
783 return new_d_type_class (name, NULL);
786 /* free all entities of a class */
787 void free_class_entities(ir_type *clss) {
789 assert(clss && (clss->type_op == type_class));
790 for (i = get_class_n_members(clss) - 1; i >= 0; --i)
791 free_entity(get_class_member(clss, i));
792 /* do NOT free the type info here. It belongs to another class */
795 void free_class_attrs(ir_type *clss) {
796 assert(clss && (clss->type_op == type_class));
797 DEL_ARR_F(clss->attr.ca.members);
798 DEL_ARR_F(clss->attr.ca.subtypes);
799 DEL_ARR_F(clss->attr.ca.supertypes);
802 /* manipulate private fields of class type */
803 void add_class_member (ir_type *clss, ir_entity *member) {
804 assert(clss && (clss->type_op == type_class));
805 assert(clss != get_entity_type(member) && "recursive type");
806 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
809 int (get_class_n_members) (const ir_type *clss) {
810 return _get_class_n_members(clss);
813 int get_class_member_index(const ir_type *clss, ir_entity *mem) {
815 assert(clss && (clss->type_op == type_class));
816 for (i = 0, n = get_class_n_members(clss); i < n; ++i)
817 if (get_class_member(clss, i) == mem)
822 ir_entity *(get_class_member) (const ir_type *clss, int pos) {
823 return _get_class_member(clss, pos);
826 ir_entity *get_class_member_by_name(ir_type *clss, ident *name) {
828 assert(clss && (clss->type_op == type_class));
829 n_mem = get_class_n_members(clss);
830 for (i = 0; i < n_mem; ++i) {
831 ir_entity *mem = get_class_member(clss, i);
832 if (get_entity_ident(mem) == name) return mem;
837 void set_class_member (ir_type *clss, ir_entity *member, int pos) {
838 assert(clss && (clss->type_op == type_class));
839 assert(pos >= 0 && pos < get_class_n_members(clss));
840 clss->attr.ca.members[pos] = member;
842 void set_class_members (ir_type *clss, ir_entity **members, int arity) {
844 assert(clss && (clss->type_op == type_class));
845 DEL_ARR_F(clss->attr.ca.members);
846 clss->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
847 for (i = 0; i < arity; i++) {
848 set_entity_owner(members[i], clss);
849 ARR_APP1 (ir_entity *, clss->attr.ca.members, members[i]);
852 void remove_class_member(ir_type *clss, ir_entity *member) {
854 assert(clss && (clss->type_op == type_class));
855 for (i = 0; i < (ARR_LEN (clss->attr.ca.members)); i++) {
856 if (clss->attr.ca.members[i] == member) {
857 for (; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
858 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
859 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
865 void add_class_subtype (ir_type *clss, ir_type *subtype) {
867 assert(clss && (clss->type_op == type_class));
868 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
869 for (i = 0; i < get_class_n_supertypes(subtype); i++)
870 if (get_class_supertype(subtype, i) == clss)
871 /* Class already registered */
873 ARR_APP1 (ir_type *, subtype->attr.ca.supertypes, clss);
875 int get_class_n_subtypes (const ir_type *clss) {
876 assert(clss && (clss->type_op == type_class));
877 return (ARR_LEN (clss->attr.ca.subtypes));
879 ir_type *get_class_subtype (ir_type *clss, int pos) {
880 assert(clss && (clss->type_op == type_class));
881 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
882 return clss->attr.ca.subtypes[pos] = skip_tid(clss->attr.ca.subtypes[pos]);
884 int get_class_subtype_index(ir_type *clss, const ir_type *subclass) {
885 int i, n_subtypes = get_class_n_subtypes(clss);
886 assert(is_Class_type(subclass));
887 for (i = 0; i < n_subtypes; ++i) {
888 if (get_class_subtype(clss, i) == subclass) return i;
892 void set_class_subtype (ir_type *clss, ir_type *subtype, int pos) {
893 assert(clss && (clss->type_op == type_class));
894 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
895 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);
920 int get_class_n_supertypes (const ir_type *clss) {
921 assert(clss && (clss->type_op == type_class));
922 return (ARR_LEN (clss->attr.ca.supertypes));
924 int get_class_supertype_index(ir_type *clss, ir_type *super_clss) {
925 int i, n_supertypes = get_class_n_supertypes(clss);
926 assert(super_clss && (super_clss->type_op == type_class));
927 for (i = 0; i < n_supertypes; i++)
928 if (get_class_supertype(clss, i) == super_clss)
932 ir_type *get_class_supertype (ir_type *clss, int pos) {
933 assert(clss && (clss->type_op == type_class));
934 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
935 return clss->attr.ca.supertypes[pos] = skip_tid(clss->attr.ca.supertypes[pos]);
937 void set_class_supertype (ir_type *clss, ir_type *supertype, int pos) {
938 assert(clss && (clss->type_op == type_class));
939 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
940 clss->attr.ca.supertypes[pos] = supertype;
942 void remove_class_supertype(ir_type *clss, ir_type *supertype) {
944 assert(clss && (clss->type_op == type_class));
945 for (i = 0; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
946 if (clss->attr.ca.supertypes[i] == supertype) {
947 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
948 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
949 ARR_SETLEN(ir_entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
953 ir_entity *get_class_type_info(const ir_type *clss) {
954 return clss->attr.ca.type_info;
956 void set_class_type_info(ir_type *clss, ir_entity *ent) {
957 clss->attr.ca.type_info = ent;
959 ent->repr_class = clss;
962 const char *get_peculiarity_name(ir_peculiarity p) {
963 #define X(a) case a: return #a
965 X(peculiarity_description);
966 X(peculiarity_inherited);
967 X(peculiarity_existent);
970 return "invalid peculiarity";
973 ir_peculiarity get_class_peculiarity (const ir_type *clss) {
974 assert(clss && (clss->type_op == type_class));
975 return clss->attr.ca.peculiarity;
978 void set_class_peculiarity (ir_type *clss, ir_peculiarity pec) {
979 assert(clss && (clss->type_op == type_class));
980 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
981 clss->attr.ca.peculiarity = pec;
984 /* Returns the size of the virtual function table. */
985 unsigned (get_class_vtable_size)(const ir_type *clss) {
986 return _get_class_vtable_size(clss);
989 /* Sets a new size of the virtual function table. */
990 void (set_class_vtable_size)(ir_type *clss, unsigned size) {
991 _set_class_vtable_size(clss, size);
994 /* Returns non-zero if a class is final. */
995 int (is_class_final)(const ir_type *clss) {
996 return _is_class_final(clss);
999 /* Sets if a class is final. */
1000 void (set_class_final)(ir_type *clss, int flag) {
1001 _set_class_final(clss, flag);
1004 /* Returns non-zero if a class is an interface. */
1005 int (is_class_interface)(const ir_type *clss) {
1006 return _is_class_interface(clss);
1009 /* Sets the class interface flag. */
1010 void (set_class_interface)(ir_type *clss, int flag) {
1011 _set_class_interface(clss, flag);
1014 /* Returns non-zero if a class is abstract. */
1015 int (is_class_abstract)(const ir_type *clss) {
1016 return _is_class_abstract(clss);
1019 /* Sets the class abstract flag. */
1020 void (set_class_abstract)(ir_type *clss, int final) {
1021 _set_class_abstract(clss, final);
1024 void set_class_dfn (ir_type *clss, int dfn) {
1025 clss->attr.ca.dfn = dfn;
1028 int get_class_dfn (const ir_type *clss) {
1029 return (clss->attr.ca.dfn);
1033 int (is_Class_type)(const ir_type *clss) {
1034 return _is_class_type(clss);
1037 void set_class_mode(ir_type *tp, ir_mode *mode) {
1038 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1039 assert(get_type_state(tp) == layout_fixed &&
1040 tp->size == get_mode_size_bits(mode) && "mode don't match class layout");
1044 void set_class_size_bits(ir_type *tp, int size) {
1045 /* argh: we must allow to set negative values as "invalid size" */
1046 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1047 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1050 /*----------------------------------------------------------------**/
1052 /*----------------------------------------------------------------**/
1054 /* create a new type struct */
1055 ir_type *new_d_type_struct(ident *name, dbg_info *db) {
1056 ir_type *res = new_type(type_struct, NULL, name, db);
1058 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1063 ir_type *new_type_struct (ident *name) {
1064 return new_d_type_struct (name, NULL);
1067 void free_struct_entities (ir_type *strct) {
1069 assert(strct && (strct->type_op == type_struct));
1070 for (i = get_struct_n_members(strct)-1; i >= 0; --i)
1071 free_entity(get_struct_member(strct, i));
1073 void free_struct_attrs (ir_type *strct) {
1074 assert(strct && (strct->type_op == type_struct));
1075 DEL_ARR_F(strct->attr.sa.members);
1078 /* manipulate private fields of struct */
1079 int get_struct_n_members (const ir_type *strct) {
1080 assert(strct && (strct->type_op == type_struct));
1081 return (ARR_LEN (strct->attr.sa.members));
1084 void add_struct_member (ir_type *strct, ir_entity *member) {
1085 assert(strct && (strct->type_op == type_struct));
1086 assert(get_type_tpop(get_entity_type(member)) != type_method);
1087 /* @@@ lowerfirm geht nicht durch */
1088 assert(strct != get_entity_type(member) && "recursive type");
1089 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1092 ir_entity *get_struct_member (const ir_type *strct, int pos) {
1093 assert(strct && (strct->type_op == type_struct));
1094 assert(pos >= 0 && pos < get_struct_n_members(strct));
1095 return strct->attr.sa.members[pos];
1098 int get_struct_member_index(const ir_type *strct, ir_entity *mem) {
1100 assert(strct && (strct->type_op == type_struct));
1101 for (i = 0, n = get_struct_n_members(strct); i < n; ++i)
1102 if (get_struct_member(strct, i) == mem)
1107 void set_struct_member (ir_type *strct, int pos, ir_entity *member) {
1108 assert(strct && (strct->type_op == type_struct));
1109 assert(pos >= 0 && pos < get_struct_n_members(strct));
1110 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
1111 strct->attr.sa.members[pos] = member;
1113 void remove_struct_member(ir_type *strct, ir_entity *member) {
1115 assert(strct && (strct->type_op == type_struct));
1116 for (i = 0; i < (ARR_LEN (strct->attr.sa.members)); i++)
1117 if (strct->attr.sa.members[i] == member) {
1118 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
1119 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1120 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1126 int (is_Struct_type)(const ir_type *strct) {
1127 return _is_struct_type(strct);
1130 void set_struct_mode(ir_type *tp, ir_mode *mode) {
1131 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1132 assert(get_type_state(tp) == layout_fixed &&
1133 tp->size == get_mode_size_bits(mode) && "mode don't match struct layout");
1137 void set_struct_size_bits(ir_type *tp, int size) {
1138 /* argh: we must allow to set negative values as "invalid size" */
1139 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1140 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1143 /*******************************************************************/
1145 /*******************************************************************/
1148 * Lazy construction of value argument / result representation.
1149 * Constructs a struct type and its member. The types of the members
1150 * are passed in the argument list.
1152 * @param name name of the type constructed
1153 * @param len number of fields
1154 * @param tps array of field types with length len
1156 static INLINE ir_type *
1157 build_value_type(ident *name, int len, tp_ent_pair *tps) {
1159 ir_type *res = new_type_struct(name);
1160 res->flags |= tf_value_param_type;
1161 /* Remove type from type list. Must be treated differently than other types. */
1162 remove_irp_type(res);
1163 for (i = 0; i < len; i++) {
1164 ident *id = tps[i].param_name;
1166 /* use res as default if corresponding type is not yet set. */
1167 ir_type *elt_type = tps[i].tp ? tps[i].tp : res;
1169 /* use the parameter name if specified */
1171 id = mangle_u(name, get_type_ident(elt_type));
1172 tps[i].ent = new_entity(res, id, elt_type);
1173 set_entity_allocation(tps[i].ent, allocation_parameter);
1178 /* Create a new method type.
1179 N_param is the number of parameters, n_res the number of results. */
1180 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
1183 assert((get_mode_size_bytes(mode_P_code) != -1) && "unorthodox modes not implemented");
1184 res = new_type(type_method, mode_P_code, name, db);
1185 res->flags |= tf_layout_fixed;
1186 res->size = get_mode_size_bits(mode_P_code);
1187 res->attr.ma.n_params = n_param;
1188 res->attr.ma.params = xcalloc(n_param, sizeof(res->attr.ma.params[0]));
1189 res->attr.ma.value_params = NULL;
1190 res->attr.ma.n_res = n_res;
1191 res->attr.ma.res_type = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
1192 res->attr.ma.value_ress = NULL;
1193 res->attr.ma.variadicity = variadicity_non_variadic;
1194 res->attr.ma.first_variadic_param = -1;
1195 res->attr.ma.additional_properties = mtp_no_property;
1196 res->attr.ma.irg_calling_conv = default_cc_mask;
1201 ir_type *new_type_method(ident *name, int n_param, int n_res) {
1202 return new_d_type_method(name, n_param, n_res, NULL);
1205 void free_method_entities(ir_type *method) {
1206 assert(method && (method->type_op == type_method));
1209 /* Attention: also frees entities in value parameter subtypes! */
1210 void free_method_attrs(ir_type *method) {
1211 assert(method && (method->type_op == type_method));
1212 free(method->attr.ma.params);
1213 free(method->attr.ma.res_type);
1214 if (method->attr.ma.value_params) {
1215 free_type_entities(method->attr.ma.value_params);
1216 free_type(method->attr.ma.value_params);
1218 if (method->attr.ma.value_ress) {
1219 free_type_entities(method->attr.ma.value_ress);
1220 free_type(method->attr.ma.value_ress);
1224 /* manipulate private fields of method. */
1225 int (get_method_n_params)(const ir_type *method) {
1226 return _get_method_n_params(method);
1229 /* Returns the type of the parameter at position pos of a method. */
1230 ir_type *get_method_param_type(ir_type *method, int pos) {
1232 assert(method && (method->type_op == type_method));
1233 assert(pos >= 0 && pos < get_method_n_params(method));
1234 res = method->attr.ma.params[pos].tp;
1235 assert(res != NULL && "empty method param type");
1236 return method->attr.ma.params[pos].tp = skip_tid(res);
1239 void set_method_param_type(ir_type *method, int pos, ir_type *tp) {
1240 assert(method && (method->type_op == type_method));
1241 assert(pos >= 0 && pos < get_method_n_params(method));
1242 method->attr.ma.params[pos].tp = tp;
1243 /* If information constructed set pass-by-value representation. */
1244 if (method->attr.ma.value_params) {
1245 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
1246 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
1250 /* Returns an ident representing the parameters name. Returns NULL if not set.
1251 For debug support only. */
1252 ident *get_method_param_ident(ir_type *method, int pos) {
1253 assert(method && (method->type_op == type_method));
1254 assert(pos >= 0 && pos < get_method_n_params(method));
1255 return method->attr.ma.params[pos].param_name;
1258 /* Returns a string representing the parameters name. Returns NULL if not set.
1259 For debug support only. */
1260 const char *get_method_param_name(ir_type *method, int pos) {
1261 ident *id = get_method_param_ident(method, pos);
1262 return id ? get_id_str(id) : NULL;
1265 /* Sets an ident representing the parameters name. For debug support only. */
1266 void set_method_param_ident(ir_type *method, int pos, ident *id) {
1267 assert(method && (method->type_op == type_method));
1268 assert(pos >= 0 && pos < get_method_n_params(method));
1269 method->attr.ma.params[pos].param_name = id;
1272 /* Returns an entity that represents the copied value argument. Only necessary
1273 for compounds passed by value. */
1274 ir_entity *get_method_value_param_ent(ir_type *method, int pos) {
1275 assert(method && (method->type_op == type_method));
1276 assert(pos >= 0 && pos < get_method_n_params(method));
1278 if (!method->attr.ma.value_params) {
1279 /* parameter value type not created yet, build */
1280 method->attr.ma.value_params
1281 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
1282 get_method_n_params(method), method->attr.ma.params);
1285 * build_value_type() sets the method->attr.ma.value_params type as default if
1288 assert((get_entity_type(method->attr.ma.params[pos].ent) != method->attr.ma.value_params)
1289 && "param type not yet set");
1290 return method->attr.ma.params[pos].ent;
1294 * Returns a type that represents the copied value arguments.
1296 ir_type *get_method_value_param_type(const ir_type *method)
1298 assert(method && (method->type_op == type_method));
1299 return method->attr.ma.value_params;
1302 int (get_method_n_ress)(const ir_type *method) {
1303 return _get_method_n_ress(method);
1306 ir_type *get_method_res_type(ir_type *method, int pos) {
1308 assert(method && (method->type_op == type_method));
1309 assert(pos >= 0 && pos < get_method_n_ress(method));
1310 res = method->attr.ma.res_type[pos].tp;
1311 assert(res != NULL && "empty method return type");
1312 return method->attr.ma.res_type[pos].tp = skip_tid(res);
1315 void set_method_res_type(ir_type *method, int pos, ir_type *tp) {
1316 assert(method && (method->type_op == type_method));
1317 assert(pos >= 0 && pos < get_method_n_ress(method));
1318 /* set the result ir_type */
1319 method->attr.ma.res_type[pos].tp = tp;
1320 /* If information constructed set pass-by-value representation. */
1321 if (method->attr.ma.value_ress) {
1322 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
1323 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
1327 /* Returns an entity that represents the copied value result. Only necessary
1328 for compounds passed by value. */
1329 ir_entity *get_method_value_res_ent(ir_type *method, int pos) {
1330 assert(method && (method->type_op == type_method));
1331 assert(pos >= 0 && pos < get_method_n_ress(method));
1333 if (!method->attr.ma.value_ress) {
1334 /* result value type not created yet, build */
1335 method->attr.ma.value_ress
1336 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
1337 get_method_n_ress(method), method->attr.ma.res_type);
1340 * build_value_type() sets the method->attr.ma.value_ress type as default if
1343 assert((get_entity_type(method->attr.ma.res_type[pos].ent) != method->attr.ma.value_ress)
1344 && "result type not yet set");
1346 return method->attr.ma.res_type[pos].ent;
1350 * Returns a type that represents the copied value results.
1352 ir_type *get_method_value_res_type(const ir_type *method) {
1353 assert(method && (method->type_op == type_method));
1354 return method->attr.ma.value_ress;
1357 /* Returns the null-terminated name of this variadicity. */
1358 const char *get_variadicity_name(variadicity vari)
1360 #define X(a) case a: return #a
1362 X(variadicity_non_variadic);
1363 X(variadicity_variadic);
1370 variadicity get_method_variadicity(const ir_type *method)
1372 assert(method && (method->type_op == type_method));
1373 return method->attr.ma.variadicity;
1376 void set_method_variadicity(ir_type *method, variadicity vari)
1378 assert(method && (method->type_op == type_method));
1379 method->attr.ma.variadicity = vari;
1383 * Returns the first variadic parameter index of a type.
1384 * If this index was NOT set, the index of the last parameter
1385 * of the method type plus one is returned for variadic functions.
1386 * Non-variadic function types always return -1 here.
1388 int get_method_first_variadic_param_index(const ir_type *method)
1390 assert(method && (method->type_op == type_method));
1392 if (method->attr.ma.variadicity == variadicity_non_variadic)
1395 if (method->attr.ma.first_variadic_param == -1)
1396 return get_method_n_params(method);
1397 return method->attr.ma.first_variadic_param;
1401 * Sets the first variadic parameter index. This allows to specify
1402 * a complete call type (containing the type of all parameters)
1403 * but still have the knowledge, which parameter must be passed as
1406 void set_method_first_variadic_param_index(ir_type *method, int index)
1408 assert(method && (method->type_op == type_method));
1409 assert(index >= 0 && index <= get_method_n_params(method));
1411 method->attr.ma.first_variadic_param = index;
1414 unsigned (get_method_additional_properties)(const ir_type *method) {
1415 return _get_method_additional_properties(method);
1418 void (set_method_additional_properties)(ir_type *method, unsigned mask) {
1419 _set_method_additional_properties(method, mask);
1422 void (set_method_additional_property)(ir_type *method, mtp_additional_property flag) {
1423 _set_method_additional_property(method, flag);
1426 /* Returns the calling convention of an entities graph. */
1427 unsigned (get_method_calling_convention)(const ir_type *method) {
1428 return _get_method_calling_convention(method);
1431 /* Sets the calling convention of an entities graph. */
1432 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask) {
1433 _set_method_calling_convention(method, cc_mask);
1436 /* Returns the number of registers parameters, 0 means default. */
1437 unsigned get_method_n_regparams(ir_type *method) {
1438 unsigned cc = get_method_calling_convention(method);
1439 assert(IS_FASTCALL(cc));
1441 return cc & ~cc_bits;
1444 /* Sets the number of registers parameters, 0 means default. */
1445 void set_method_n_regparams(ir_type *method, unsigned n_regs) {
1446 unsigned cc = get_method_calling_convention(method);
1447 assert(IS_FASTCALL(cc));
1449 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1453 int (is_Method_type)(const ir_type *method) {
1454 return _is_method_type(method);
1457 /*-----------------------------------------------------------------*/
1459 /*-----------------------------------------------------------------*/
1461 /* create a new type uni */
1462 ir_type *new_d_type_union(ident *name, dbg_info *db) {
1463 ir_type *res = new_type(type_union, NULL, name, db);
1465 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1470 ir_type *new_type_union(ident *name) {
1471 return new_d_type_union(name, NULL);
1474 void free_union_entities(ir_type *uni) {
1476 assert(uni && (uni->type_op == type_union));
1477 for (i = get_union_n_members(uni) - 1; i >= 0; --i)
1478 free_entity(get_union_member(uni, i));
1481 void free_union_attrs (ir_type *uni) {
1482 assert(uni && (uni->type_op == type_union));
1483 DEL_ARR_F(uni->attr.ua.members);
1486 /* manipulate private fields of union */
1487 int get_union_n_members (const ir_type *uni) {
1488 assert(uni && (uni->type_op == type_union));
1489 return (ARR_LEN (uni->attr.ua.members));
1491 void add_union_member (ir_type *uni, ir_entity *member) {
1492 assert(uni && (uni->type_op == type_union));
1493 assert(uni != get_entity_type(member) && "recursive type");
1494 ARR_APP1 (ir_entity *, uni->attr.ua.members, member);
1496 ir_entity *get_union_member (const ir_type *uni, int pos) {
1497 assert(uni && (uni->type_op == type_union));
1498 assert(pos >= 0 && pos < get_union_n_members(uni));
1499 return uni->attr.ua.members[pos];
1501 int get_union_member_index(const ir_type *uni, ir_entity *mem) {
1503 assert(uni && (uni->type_op == type_union));
1504 for (i = 0, n = get_union_n_members(uni); i < n; ++i)
1505 if (get_union_member(uni, i) == mem)
1509 void set_union_member (ir_type *uni, int pos, ir_entity *member) {
1510 assert(uni && (uni->type_op == type_union));
1511 assert(pos >= 0 && pos < get_union_n_members(uni));
1512 uni->attr.ua.members[pos] = member;
1514 void remove_union_member(ir_type *uni, ir_entity *member) {
1516 assert(uni && (uni->type_op == type_union));
1517 for (i = 0; i < (ARR_LEN (uni->attr.ua.members)); i++)
1518 if (uni->attr.ua.members[i] == member) {
1519 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1520 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1521 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1527 int (is_Union_type)(const ir_type *uni) {
1528 return _is_union_type(uni);
1531 void set_union_size_bits(ir_type *tp, int size) {
1532 /* argh: we must allow to set negative values as "invalid size" */
1533 tp->size = (size >= 0) ? (size + 7) & ~7 : size;
1534 assert(tp->size == size && "setting a bit size is NOT allowed for this type");
1537 /*-----------------------------------------------------------------*/
1539 /*-----------------------------------------------------------------*/
1542 /* create a new type array -- set dimension sizes independently */
1543 ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type, dbg_info *db) {
1547 ir_graph *rem = current_ir_graph;
1549 assert(!is_Method_type(element_type));
1551 res = new_type(type_array, NULL, name, db);
1552 res->attr.aa.n_dimensions = n_dimensions;
1553 res->attr.aa.lower_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
1554 res->attr.aa.upper_bound = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
1555 res->attr.aa.order = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
1557 current_ir_graph = get_const_code_irg();
1558 unk = new_Unknown( mode_Iu);
1559 for (i = 0; i < n_dimensions; i++) {
1560 res->attr.aa.lower_bound[i] =
1561 res->attr.aa.upper_bound[i] = unk;
1562 res->attr.aa.order[i] = i;
1564 current_ir_graph = rem;
1566 res->attr.aa.element_type = element_type;
1567 new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
1572 ir_type *new_type_array(ident *name, int n_dimensions, ir_type *element_type) {
1573 return new_d_type_array(name, n_dimensions, element_type, NULL);
1576 void free_array_automatic_entities(ir_type *array) {
1577 assert(array && (array->type_op == type_array));
1578 free_entity(get_array_element_entity(array));
1581 void free_array_entities (ir_type *array) {
1582 assert(array && (array->type_op == type_array));
1585 void free_array_attrs (ir_type *array) {
1586 assert(array && (array->type_op == type_array));
1587 free(array->attr.aa.lower_bound);
1588 free(array->attr.aa.upper_bound);
1589 free(array->attr.aa.order);
1592 /* manipulate private fields of array ir_type */
1593 int get_array_n_dimensions (const ir_type *array) {
1594 assert(array && (array->type_op == type_array));
1595 return array->attr.aa.n_dimensions;
1599 set_array_bounds (ir_type *array, int dimension, ir_node * lower_bound,
1600 ir_node * upper_bound) {
1601 assert(array && (array->type_op == type_array));
1602 assert(lower_bound && "lower_bound node may not be NULL.");
1603 assert(upper_bound && "upper_bound node may not be NULL.");
1604 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1605 array->attr.aa.lower_bound[dimension] = lower_bound;
1606 array->attr.aa.upper_bound[dimension] = upper_bound;
1609 set_array_bounds_int (ir_type *array, int dimension, int lower_bound,
1611 ir_graph *rem = current_ir_graph;
1612 current_ir_graph = get_const_code_irg();
1613 set_array_bounds (array, dimension,
1614 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1615 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1616 current_ir_graph = rem;
1619 set_array_lower_bound (ir_type *array, int dimension, ir_node * lower_bound) {
1620 assert(array && (array->type_op == type_array));
1621 assert(lower_bound && "lower_bound node may not be NULL.");
1622 array->attr.aa.lower_bound[dimension] = lower_bound;
1624 void set_array_lower_bound_int (ir_type *array, int dimension, int lower_bound) {
1625 ir_graph *rem = current_ir_graph;
1626 current_ir_graph = get_const_code_irg();
1627 set_array_lower_bound (array, dimension,
1628 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1629 current_ir_graph = rem;
1632 set_array_upper_bound (ir_type *array, int dimension, ir_node * upper_bound) {
1633 assert(array && (array->type_op == type_array));
1634 assert(upper_bound && "upper_bound node may not be NULL.");
1635 array->attr.aa.upper_bound[dimension] = upper_bound;
1637 void set_array_upper_bound_int (ir_type *array, int dimension, int upper_bound) {
1638 ir_graph *rem = current_ir_graph;
1639 current_ir_graph = get_const_code_irg();
1640 set_array_upper_bound (array, dimension,
1641 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1642 current_ir_graph = rem;
1644 int has_array_lower_bound (const ir_type *array, int dimension) {
1645 assert(array && (array->type_op == type_array));
1646 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1648 ir_node *get_array_lower_bound (const ir_type *array, int dimension) {
1649 assert(array && (array->type_op == type_array));
1650 return array->attr.aa.lower_bound[dimension];
1652 long get_array_lower_bound_int (const ir_type *array, int dimension) {
1654 assert(array && (array->type_op == type_array));
1655 node = array->attr.aa.lower_bound[dimension];
1656 assert(get_irn_op(node) == op_Const);
1657 return get_tarval_long(get_Const_tarval(node));
1659 int has_array_upper_bound (const ir_type *array, int dimension) {
1660 assert(array && (array->type_op == type_array));
1661 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1663 ir_node * get_array_upper_bound (const ir_type *array, int dimension) {
1664 assert(array && (array->type_op == type_array));
1665 return array->attr.aa.upper_bound[dimension];
1667 long get_array_upper_bound_int (const ir_type *array, int dimension) {
1669 assert(array && (array->type_op == type_array));
1670 node = array->attr.aa.upper_bound[dimension];
1671 assert(get_irn_op(node) == op_Const);
1672 return get_tarval_long(get_Const_tarval(node));
1675 void set_array_order (ir_type *array, int dimension, int order) {
1676 assert(array && (array->type_op == type_array));
1677 array->attr.aa.order[dimension] = order;
1680 int get_array_order (const ir_type *array, int dimension) {
1681 assert(array && (array->type_op == type_array));
1682 return array->attr.aa.order[dimension];
1685 int find_array_dimension(const ir_type *array, int order) {
1688 assert(array && (array->type_op == type_array));
1690 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1691 if (array->attr.aa.order[dim] == order)
1697 void set_array_element_type (ir_type *array, ir_type *tp) {
1698 assert(array && (array->type_op == type_array));
1699 assert(!is_Method_type(tp));
1700 array->attr.aa.element_type = tp;
1702 ir_type *get_array_element_type (ir_type *array) {
1703 assert(array && (array->type_op == type_array));
1704 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1707 void set_array_element_entity (ir_type *array, ir_entity *ent) {
1708 assert(array && (array->type_op == type_array));
1709 assert((get_entity_type(ent)->type_op != type_method));
1710 array->attr.aa.element_ent = ent;
1711 array->attr.aa.element_type = get_entity_type(ent);
1713 ir_entity *get_array_element_entity (const ir_type *array) {
1714 assert(array && (array->type_op == type_array));
1715 return array->attr.aa.element_ent;
1719 int (is_Array_type)(const ir_type *array) {
1720 return _is_array_type(array);
1723 void set_array_size_bits(ir_type *tp, int size) {
1724 /* FIXME: Here we should make some checks with the element type size */
1727 /*-----------------------------------------------------------------*/
1728 /* TYPE_ENUMERATION */
1729 /*-----------------------------------------------------------------*/
1731 /* create a new type enumeration -- set the enumerators independently */
1732 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db) {
1735 assert(n_enums >= 0);
1736 res = new_type(type_enumeration, NULL, name, db);
1737 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1742 ir_type *new_type_enumeration(ident *name, int n_enums) {
1743 return new_d_type_enumeration(name, n_enums, NULL);
1746 void free_enumeration_entities(ir_type *enumeration) {
1747 assert(enumeration && (enumeration->type_op == type_enumeration));
1749 void free_enumeration_attrs(ir_type *enumeration) {
1750 assert(enumeration && (enumeration->type_op == type_enumeration));
1751 DEL_ARR_F(enumeration->attr.ea.enumer);
1754 /* manipulate fields of enumeration type. */
1755 int get_enumeration_n_enums(const ir_type *enumeration) {
1756 assert(enumeration && (enumeration->type_op == type_enumeration));
1757 return ARR_LEN(enumeration->attr.ea.enumer);
1760 /* create a new constant */
1761 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con) {
1762 assert(0 <= pos && pos < ARR_LEN(enumeration->attr.ea.enumer));
1763 enumeration->attr.ea.enumer[pos].nameid = nameid;
1764 enumeration->attr.ea.enumer[pos].value = con;
1765 enumeration->attr.ea.enumer[pos].owner = enumeration;
1768 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos) {
1769 assert(enumeration && (enumeration->type_op == type_enumeration));
1770 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1771 return &enumeration->attr.ea.enumer[pos];
1774 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst) {
1775 return enum_cnst->owner;
1777 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con) {
1778 enum_cnst->value = con;
1780 tarval *get_enumeration_value(const ir_enum_const *enum_cnst) {
1781 return enum_cnst->value;
1783 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id) {
1784 enum_cnst->nameid = id;
1786 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst) {
1787 return enum_cnst->nameid;
1789 const char *get_enumeration_name(const ir_enum_const *enum_cnst) {
1790 return get_id_str(enum_cnst->nameid);
1794 int (is_Enumeration_type)(const ir_type *enumeration) {
1795 return _is_enumeration_type(enumeration);
1798 void set_enumeration_mode(ir_type *tp, ir_mode *mode) {
1799 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1800 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1801 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1803 tp->size = get_mode_size_bits(mode);
1807 /*-----------------------------------------------------------------*/
1809 /*-----------------------------------------------------------------*/
1811 /* Create a new type pointer */
1812 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info *db) {
1815 assert(mode_is_reference(ptr_mode));
1816 res = new_type(type_pointer, ptr_mode, name, db);
1817 res->attr.pa.points_to = points_to;
1818 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1819 res->size = get_mode_size_bits(res->mode);
1820 res->flags |= tf_layout_fixed;
1825 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode) {
1826 return new_d_type_pointer(name, points_to, ptr_mode, NULL);
1829 void free_pointer_entities (ir_type *pointer) {
1830 assert(pointer && (pointer->type_op == type_pointer));
1833 void free_pointer_attrs (ir_type *pointer) {
1834 assert(pointer && (pointer->type_op == type_pointer));
1837 /* manipulate fields of type_pointer */
1838 void set_pointer_points_to_type (ir_type *pointer, ir_type *tp) {
1839 assert(pointer && (pointer->type_op == type_pointer));
1840 pointer->attr.pa.points_to = tp;
1843 ir_type *get_pointer_points_to_type (ir_type *pointer) {
1844 assert(pointer && (pointer->type_op == type_pointer));
1845 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1849 int (is_Pointer_type)(const ir_type *pointer) {
1850 return _is_pointer_type(pointer);
1853 void set_pointer_mode(ir_type *tp, ir_mode *mode) {
1854 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1855 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1856 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1858 tp->size = get_mode_size_bits(mode);
1862 /* Returns the first pointer type that has as points_to tp.
1863 * Not efficient: O(#types).
1864 * If not found returns firm_unknown_type. */
1865 ir_type *find_pointer_type_to_type (ir_type *tp) {
1866 int i, n = get_irp_n_types();
1867 for (i = 0; i < n; ++i) {
1868 ir_type *found = get_irp_type(i);
1869 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1872 return firm_unknown_type;
1876 /*-----------------------------------------------------------------*/
1877 /* TYPE_PRIMITIVE */
1878 /*-----------------------------------------------------------------*/
1880 /* create a new type primitive */
1881 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info *db) {
1883 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1884 res = new_type(type_primitive, mode, name, db);
1885 res->size = get_mode_size_bits(mode);
1886 res->flags |= tf_layout_fixed;
1891 ir_type *new_type_primitive(ident *name, ir_mode *mode) {
1892 return new_d_type_primitive(name, mode, NULL);
1896 int (is_Primitive_type)(const ir_type *primitive) {
1897 return _is_primitive_type(primitive);
1900 void set_primitive_mode(ir_type *tp, ir_mode *mode) {
1901 /* Modes of primitives must be data */
1902 assert(mode_is_data(mode));
1904 /* For primitive size depends on the mode. */
1905 tp->size = get_mode_size_bits(mode);
1910 /*-----------------------------------------------------------------*/
1911 /* common functionality */
1912 /*-----------------------------------------------------------------*/
1915 int (is_atomic_type)(const ir_type *tp) {
1916 return _is_atomic_type(tp);
1920 * Gets the number of elements in a firm compound type.
1922 int get_compound_n_members(const ir_type *tp)
1924 const tp_op *op = get_type_tpop(tp);
1927 if (op->ops.get_n_members)
1928 res = op->ops.get_n_members(tp);
1930 assert(0 && "no member count for this type");
1936 * Gets the member of a firm compound type at position pos.
1938 ir_entity *get_compound_member(const ir_type *tp, int pos)
1940 const tp_op *op = get_type_tpop(tp);
1941 ir_entity *res = NULL;
1943 if (op->ops.get_member)
1944 res = op->ops.get_member(tp, pos);
1946 assert(0 && "no members in this type");
1951 /* Returns index of member in tp, -1 if not contained. */
1952 int get_compound_member_index(const ir_type *tp, ir_entity *member)
1954 const tp_op *op = get_type_tpop(tp);
1957 if (op->ops.get_member_index)
1958 index = op->ops.get_member_index(tp, member);
1960 assert(0 && "no members in this type");
1965 int is_compound_type(const ir_type *tp) {
1966 assert(tp && tp->kind == k_type);
1967 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1970 /* Checks, whether a type is a frame type */
1971 int is_frame_type(const ir_type *tp) {
1972 return tp->flags & tf_frame_type;
1975 /* Checks, whether a type is a value parameter type */
1976 int is_value_param_type(const ir_type *tp) {
1977 return tp->flags & tf_value_param_type;
1980 /* Checks, whether a type is a lowered type */
1981 int is_lowered_type(const ir_type *tp) {
1982 return tp->flags & tf_lowered_type;
1985 /* Makes a new frame type. */
1986 ir_type *new_type_frame(ident *name)
1988 ir_type *res = new_type_class(name);
1990 res->flags |= tf_frame_type;
1992 /* Remove type from type list. Must be treated differently than other types. */
1993 remove_irp_type(res);
1995 /* It is not possible to derive from the frame type. Set the final flag. */
1996 set_class_final(res, 1);
2001 /* Sets a lowered type for a type. This sets both associations. */
2002 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
2003 assert(is_type(tp) && is_type(lowered_type));
2004 lowered_type->flags |= tf_lowered_type;
2005 tp->assoc_type = lowered_type;
2006 lowered_type->assoc_type = tp;
2010 * Gets the lowered/unlowered type of a type or NULL if this type
2011 * has no lowered/unlowered one.
2013 ir_type *get_associated_type(const ir_type *tp) {
2014 return tp->assoc_type;
2017 /* set the type size for the unknown and none ir_type */
2018 void set_default_size_bits(ir_type *tp, int size) {
2023 * Allocate an area of size bytes aligned at alignment
2024 * at the start or the end of a frame type.
2025 * The frame type must have already an fixed layout.
2027 ir_entity *frame_alloc_area(ir_type *frame_type, int size, int alignment, int at_start)
2033 int frame_align, i, offset, frame_size;
2034 static unsigned area_cnt = 0;
2035 static ir_type *a_byte = NULL;
2037 assert(is_frame_type(frame_type));
2038 assert(get_type_state(frame_type) == layout_fixed);
2039 assert(get_type_alignment_bytes(frame_type) > 0);
2042 a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
2044 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2045 name = new_id_from_str(buf);
2047 /* align the size */
2048 frame_align = get_type_alignment_bytes(frame_type);
2049 size = (size + frame_align - 1) & -frame_align;
2051 tp = new_type_array(mangle_u(get_type_ident(frame_type), name), 1, a_byte);
2052 set_array_bounds_int(tp, 0, 0, size);
2053 set_type_alignment_bytes(tp, alignment);
2055 frame_size = get_type_size_bytes(frame_type);
2057 /* fix all offsets so far */
2058 for (i = get_class_n_members(frame_type) - 1; i >= 0; --i) {
2059 ir_entity *ent = get_class_member(frame_type, i);
2061 set_entity_offset(ent, get_entity_offset(ent) + size);
2063 /* calculate offset and new type size */
2068 /* calculate offset and new type size */
2069 offset = (frame_size + alignment - 1) & -alignment;
2070 frame_size = offset + size;
2073 area = new_entity(frame_type, name, tp);
2074 set_entity_offset(area, offset);
2075 set_type_size_bytes(frame_type, frame_size);
2077 /* mark this entity as compiler generated */
2078 set_entity_compiler_generated(area, 1);