3 * file type.c - implementation of the datastructure to hold
5 * (C) 2001 by Universitaet Karlsruhe
6 * Martin Trapp, Christian Schaefer, Goetz Lindenmaier
8 * This module supplies a datastructure to represent all types
9 * known in the compiled program. This includes types specified
10 * in the program as well as types defined by the language. In the
11 * view of the intermediate representation there is no difference
12 * between these types.
14 * There exist several kinds of types, arranged by the structure of
15 * the type. A type is described by a set of attributes. Some of
16 * these attributes are common to all types, others depend on the
19 * Types are different from the modes defined in irmode: Types are
20 * on the level of the programming language, modes at the level of
21 * the target processor.
23 * @see type_t.h type tpop
37 # include "irprog_t.h"
38 # include "typegmod_t.h"
45 /*******************************************************************/
47 /*******************************************************************/
50 /** Returns a new, unique number to number nodes or the like. */
51 int get_irp_new_node_nr(void);
54 /* Suffixes added to types used for pass-by-value representations. */
55 static ident *value_params_suffix = NULL;
56 static ident *value_ress_suffix = NULL;
58 void init_type(void) {
59 value_params_suffix = id_from_str(VALUE_PARAMS_SUFFIX, strlen(VALUE_PARAMS_SUFFIX));
60 value_ress_suffix = id_from_str(VALUE_RESS_SUFFIX, strlen(VALUE_RESS_SUFFIX));
63 unsigned long type_visited;
64 INLINE void set_master_type_visited(unsigned long val) { type_visited = val; }
65 INLINE unsigned long get_master_type_visited() { return type_visited; }
66 INLINE void inc_master_type_visited() { type_visited++; }
68 void free_type(type *tp) {
69 /* Remove from list of all types */
71 /* Free the attributes of the type. */
73 /* Free entities automatically allocated with the type */
74 if (is_array_type(tp))
75 free_entity(get_array_element_entity(tp));
76 /* And now the type itself... */
81 new_type(tp_op *type_op, ir_mode *mode, ident* name) {
85 assert(type_op != type_id);
86 assert(!id_contains_char(name, ' ') && "type name should not contain spaces");
88 node_size = offsetof (type, attr) + type_op->attr_size;
89 res = (type *) xmalloc (node_size);
90 add_irp_type(res); /* Remember the new type global. */
93 res->type_op = type_op;
96 res->state = layout_undefined;
101 res->nr = get_irp_new_node_nr();
107 void free_type_attrs(type *tp) {
108 switch(get_type_tpop_code(tp)) {
109 case tpo_class: { free_class_attrs(tp); } break;
110 case tpo_struct: { free_struct_attrs(tp); } break;
111 case tpo_method: { free_method_attrs(tp); } break;
112 case tpo_union: { free_union_attrs(tp); } break;
113 case tpo_array: { free_array_attrs(tp); } break;
114 case tpo_enumeration: { free_enumeration_attrs(tp); } break;
115 case tpo_pointer: { free_pointer_attrs(tp); } break;
116 case tpo_primitive: { free_primitive_attrs(tp); } break;
121 /* set/get the link field */
122 void *get_type_link(type *tp)
124 assert(tp && tp->kind == k_type);
128 void set_type_link(type *tp, void *l)
130 assert(tp && tp->kind == k_type);
134 tp_op* get_type_tpop(type *tp) {
135 assert(tp && tp->kind == k_type);
139 ident* get_type_tpop_nameid(type *tp) {
140 assert(tp && tp->kind == k_type);
141 return tp->type_op->name;
144 const char* get_type_tpop_name(type *tp) {
145 assert(tp && tp->kind == k_type);
146 return id_to_str(tp->type_op->name);
149 tp_opcode get_type_tpop_code(type *tp) {
150 assert(tp && tp->kind == k_type);
151 return tp->type_op->code;
154 ir_mode* get_type_mode(type *tp) {
155 assert(tp && tp->kind == k_type);
159 void set_type_mode(type *tp, ir_mode* m) {
160 assert(tp && tp->kind == k_type);
162 assert(((tp->type_op != type_primitive) || mode_is_data(m)) &&
163 /* Modes of primitives must be data */
164 ((tp->type_op != type_enumeration) || mode_is_int(m)));
165 /* Modes of enumerations must be integers */
167 if ((tp->type_op == type_primitive) || (tp->type_op == type_enumeration)) {
168 /* For pointer, primitive and enumeration size depends on the mode. */
169 assert((get_mode_size_bytes(m) != -1) && "unorthodox modes not implemented");
170 tp->size = get_mode_size_bytes(m);
175 ident* get_type_ident(type *tp) {
176 assert(tp && tp->kind == k_type);
180 void set_type_ident(type *tp, ident* id) {
181 assert(tp && tp->kind == k_type);
185 /* Outputs a unique number for this node */
187 get_type_nr(type *tp) {
196 const char* get_type_name(type *tp) {
197 assert(tp && tp->kind == k_type);
198 return (id_to_str(tp->name));
201 int get_type_size(type *tp) {
202 assert(tp && tp->kind == k_type);
207 set_type_size(type *tp, int size) {
208 assert(tp && tp->kind == k_type);
209 /* For pointer enumeration and primitive size depends on the mode.
210 Methods don't have a size. */
211 if ((tp->type_op != type_pointer) && (tp->type_op != type_primitive) &&
212 (tp->type_op != type_enumeration) && (tp->type_op != type_method))
217 get_type_state(type *tp) {
218 assert(tp && tp->kind == k_type);
223 set_type_state(type *tp, type_state state) {
224 assert(tp && tp->kind == k_type);
226 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
227 (tp->type_op == type_method))
230 /* Just a correctness check: */
231 if (state == layout_fixed) {
233 switch (get_type_tpop_code(tp)) {
236 assert(get_type_size(tp) > -1);
237 if (tp != get_glob_type())
238 for (i = 0; i < get_class_n_members(tp); i++) {
239 assert(get_entity_offset(get_class_member(tp, i)) > -1);
240 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
241 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
246 assert(get_type_size(tp) > -1);
247 for (i = 0; i < get_struct_n_members(tp); i++) {
248 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
249 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
258 Assure that only innermost dimension is dynamic? */
260 case tpo_enumeration:
262 assert(get_type_mode != NULL);
263 for (i = 0; i < get_enumeration_n_enums(tp); i++)
264 assert(get_enumeration_enum(tp, i) != NULL);
272 unsigned long get_type_visited(type *tp) {
273 assert(tp && tp->kind == k_type);
277 void set_type_visited(type *tp, unsigned long num) {
278 assert(tp && tp->kind == k_type);
281 /* Sets visited field in type to type_visited. */
282 void mark_type_visited(type *tp) {
283 assert(tp && tp->kind == k_type);
284 assert(tp->visit < type_visited);
285 tp->visit = type_visited;
287 /* @@@ name clash with master flag
288 bool type_visited(type *tp) {
289 assert(tp && tp->kind == k_type);
290 return tp->visit >= type_visited;
292 bool type_not_visited(type *tp) {
293 assert(tp && tp->kind == k_type);
294 return tp->visit < type_visited;
298 int is_type (void *thing) {
300 if (get_kind(thing) == k_type)
307 bool equal_type(type *typ1, type *typ2) {
312 if (typ1 == typ2) return true;
314 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
315 (get_type_ident(typ1) != get_type_ident(typ2)) ||
316 (get_type_mode(typ1) != get_type_mode(typ2)) ||
317 (get_type_state(typ1) != get_type_state(typ2)))
319 if ((get_type_state(typ1) == layout_fixed) &&
320 (get_type_size(typ1) != get_type_size(typ2)))
323 switch(get_type_tpop_code(typ1)) {
325 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
326 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
327 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
328 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
329 /** Compare the members **/
330 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
331 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
332 /* First sort the members of typ2 */
333 for (i = 0; i < get_class_n_members(typ1); i++) {
334 entity *e1 = get_class_member(typ1, i);
335 for (j = 0; j < get_class_n_members(typ2); j++) {
336 entity *e2 = get_class_member(typ2, j);
337 if (get_entity_name(e1) == get_entity_name(e2))
341 for (i = 0; i < get_class_n_members(typ1); i++) {
342 if (!m[i] || /* Found no counterpart */
343 !equal_entity(get_class_member(typ1, i), m[i]))
346 /** Compare the supertypes **/
347 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
348 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
349 /* First sort the supertypes of typ2 */
350 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
351 type *t1 = get_class_supertype(typ1, i);
352 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
353 type *t2 = get_class_supertype(typ2, j);
354 if (get_type_ident(t2) == get_type_ident(t1))
358 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
359 if (!t[i] || /* Found no counterpart */
360 get_class_supertype(typ1, i) != t[i])
365 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
366 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
367 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
368 /* First sort the members of lt */
369 for (i = 0; i < get_struct_n_members(typ1); i++) {
370 entity *e1 = get_struct_member(typ1, i);
371 for (j = 0; j < get_struct_n_members(typ2); j++) {
372 entity *e2 = get_struct_member(typ2, j);
373 if (get_entity_name(e1) == get_entity_name(e2))
377 for (i = 0; i < get_struct_n_members(typ1); i++) {
378 if (!m[i] || /* Found no counterpart */
379 !equal_entity(get_struct_member(typ1, i), m[i]))
384 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
385 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
386 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
387 for (i = 0; i < get_method_n_params(typ1); i++) {
388 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
391 for (i = 0; i < get_method_n_ress(typ1); i++) {
392 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
397 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
398 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
399 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
400 /* First sort the members of lt */
401 for (i = 0; i < get_union_n_members(typ1); i++) {
402 entity *e1 = get_union_member(typ1, i);
403 for (j = 0; j < get_union_n_members(typ2); j++) {
404 entity *e2 = get_union_member(typ2, j);
405 if (get_entity_name(e1) == get_entity_name(e2))
409 for (i = 0; i < get_union_n_members(typ1); i++) {
410 if (!m[i] || /* Found no counterpart */
411 !equal_entity(get_union_member(typ1, i), m[i]))
416 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
418 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
420 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
421 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
422 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
424 if (get_array_order(typ1, i) != get_array_order(typ2, i))
425 assert(0 && "type compare with different dimension orders not implemented");
428 case tpo_enumeration: {
429 assert(0 && "enumerations not implemented");
432 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
435 case tpo_primitive: {
442 bool smaller_type (type *st, type *lt) {
446 if (st == lt) return true;
448 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
451 switch(get_type_tpop_code(st)) {
453 return is_subclass_of(st, lt);
456 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
457 m = alloca(sizeof(entity *) * get_struct_n_members(st));
458 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
459 /* First sort the members of lt */
460 for (i = 0; i < get_struct_n_members(st); i++) {
461 entity *se = get_struct_member(st, i);
462 for (j = 0; j < get_struct_n_members(lt); j++) {
463 entity *le = get_struct_member(lt, j);
464 if (get_entity_name(le) == get_entity_name(se))
468 for (i = 0; i < get_struct_n_members(st); i++) {
469 if (!m[i] || /* Found no counterpart */
470 !smaller_type(get_entity_type(get_struct_member(st, i)),
471 get_entity_type(m[i])))
476 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
477 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
478 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
479 for (i = 0; i < get_method_n_params(st); i++) {
480 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
483 for (i = 0; i < get_method_n_ress(st); i++) {
484 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
489 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
490 m = alloca(sizeof(entity *) * get_union_n_members(st));
491 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
492 /* First sort the members of lt */
493 for (i = 0; i < get_union_n_members(st); i++) {
494 entity *se = get_union_member(st, i);
495 for (j = 0; j < get_union_n_members(lt); j++) {
496 entity *le = get_union_member(lt, j);
497 if (get_entity_name(le) == get_entity_name(se))
501 for (i = 0; i < get_union_n_members(st); i++) {
502 if (!m[i] || /* Found no counterpart */
503 !smaller_type(get_entity_type(get_union_member(st, i)),
504 get_entity_type(m[i])))
509 type *set, *let; /* small/large elt. type */
510 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
512 set = get_array_element_type(st);
513 let = get_array_element_type(lt);
515 /* If the elt types are different, set must be convertible
516 to let, and they must have the same size so that address
517 computations work out. To have a size the layout must
519 if ((get_type_state(set) != layout_fixed) ||
520 (get_type_state(let) != layout_fixed))
522 if (!smaller_type(set, let) ||
523 get_type_size(set) != get_type_size(let))
526 for(i = 0; i < get_array_n_dimensions(st); i++) {
527 if (get_array_lower_bound(lt, i))
528 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
530 if (get_array_upper_bound(lt, i))
531 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
535 case tpo_enumeration: {
536 assert(0 && "enumerations not implemented");
539 if (!smaller_type(get_pointer_points_to_type(st),
540 get_pointer_points_to_type(lt)))
543 case tpo_primitive: {
544 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
552 /*******************************************************************/
554 /*******************************************************************/
556 /* create a new class type */
557 INLINE type *new_type_class (ident *name) {
560 res = new_type(type_class, NULL, name);
562 res->attr.ca.members = NEW_ARR_F (entity *, 1);
563 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
564 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
565 res->attr.ca.peculiarity = existent;
566 res->attr.ca.dfn = 0;
570 type *new_d_type_class (ident *name, dbg_info* db) {
571 type *res = new_type_class (name);
572 set_type_dbg_info(res, db);
575 INLINE void free_class_attrs(type *clss) {
576 assert(clss && (clss->type_op == type_class));
577 DEL_ARR_F(clss->attr.ca.members);
578 DEL_ARR_F(clss->attr.ca.subtypes);
579 DEL_ARR_F(clss->attr.ca.supertypes);
582 /* manipulate private fields of class type */
583 void add_class_member (type *clss, entity *member) {
584 assert(clss && (clss->type_op == type_class));
585 ARR_APP1 (entity *, clss->attr.ca.members, member);
587 int get_class_n_members (type *clss) {
588 assert(clss && (clss->type_op == type_class));
589 return (ARR_LEN (clss->attr.ca.members))-1;
591 int get_class_member_index(type *clss, entity *mem) {
593 assert(clss && (clss->type_op == type_class));
594 for (i = 0; i < get_class_n_members(clss); i++)
595 if (get_class_member(clss, i) == mem)
599 entity *get_class_member (type *clss, int pos) {
600 assert(clss && (clss->type_op == type_class));
601 assert(pos >= 0 && pos < get_class_n_members(clss));
602 return clss->attr.ca.members[pos+1];
604 void set_class_member (type *clss, entity *member, int pos) {
605 assert(clss && (clss->type_op == type_class));
606 assert(pos >= 0 && pos < get_class_n_members(clss));
607 clss->attr.ca.members[pos+1] = member;
609 void set_class_members (type *clss, entity **members, int arity) {
611 assert(clss && (clss->type_op == type_class));
612 DEL_ARR_F(clss->attr.ca.members);
613 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
614 for (i = 0; i < arity; i++) {
615 set_entity_owner(members[i], clss);
616 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
619 void remove_class_member(type *clss, entity *member) {
621 assert(clss && (clss->type_op == type_class));
622 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
623 if (clss->attr.ca.members[i] == member) {
624 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
625 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
626 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
632 void add_class_subtype (type *clss, type *subtype) {
634 assert(clss && (clss->type_op == type_class));
635 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
636 for (i = 0; i < get_class_n_supertypes(subtype); i++)
637 if (get_class_supertype(subtype, i) == clss)
638 /* Class already registered */
640 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
642 int get_class_n_subtypes (type *clss) {
643 assert(clss && (clss->type_op == type_class));
644 return (ARR_LEN (clss->attr.ca.subtypes))-1;
646 type *get_class_subtype (type *clss, int pos) {
647 assert(clss && (clss->type_op == type_class));
648 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
649 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
651 void set_class_subtype (type *clss, type *subtype, int pos) {
652 assert(clss && (clss->type_op == type_class));
653 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
654 clss->attr.ca.subtypes[pos+1] = subtype;
656 void remove_class_subtype(type *clss, type *subtype) {
658 assert(clss && (clss->type_op == type_class));
659 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
660 if (clss->attr.ca.subtypes[i] == subtype) {
661 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
662 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
663 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
668 void add_class_supertype (type *clss, type *supertype) {
670 assert(clss && (clss->type_op == type_class));
671 assert(supertype && (supertype -> type_op == type_class));
672 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
673 for (i = 0; i < get_class_n_subtypes(supertype); i++)
674 if (get_class_subtype(supertype, i) == clss)
675 /* Class already registered */
677 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
679 int get_class_n_supertypes (type *clss) {
680 assert(clss && (clss->type_op == type_class));
681 return (ARR_LEN (clss->attr.ca.supertypes))-1;
683 int get_class_supertype_index(type *clss, type *super_clss) {
685 assert(clss && (clss->type_op == type_class));
686 assert(super_clss && (super_clss->type_op == type_class));
687 for (i = 0; i < get_class_n_supertypes(clss); i++)
688 if (get_class_supertype(clss, i) == super_clss)
692 type *get_class_supertype (type *clss, int pos) {
693 assert(clss && (clss->type_op == type_class));
694 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
695 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
697 void set_class_supertype (type *clss, type *supertype, int pos) {
698 assert(clss && (clss->type_op == type_class));
699 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
700 clss->attr.ca.supertypes[pos+1] = supertype;
702 void remove_class_supertype(type *clss, type *supertype) {
704 assert(clss && (clss->type_op == type_class));
705 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
706 if (clss->attr.ca.supertypes[i] == supertype) {
707 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
708 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
709 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
714 INLINE peculiarity get_class_peculiarity (type *clss) {
715 assert(clss && (clss->type_op == type_class));
716 return clss->attr.ca.peculiarity;
718 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
719 assert(clss && (clss->type_op == type_class));
720 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
721 clss->attr.ca.peculiarity = pec;
724 void set_class_dfn (type *clss, int dfn)
726 clss->attr.ca.dfn = dfn;
729 int get_class_dfn (type *clss)
731 return (clss->attr.ca.dfn);
735 bool is_class_type(type *clss) {
737 if (clss->type_op == type_class) return 1; else return 0;
740 bool is_subclass_of(type *low, type *high) {
742 assert(is_class_type(low) && is_class_type(high));
743 if (low == high) return true;
744 /* depth first search from high downwards. */
745 for (i = 0; i < get_class_n_subtypes(high); i++) {
746 if (low == get_class_subtype(high, i))
748 if (is_subclass_of(low, get_class_subtype(high, i)))
754 /*******************************************************************/
756 /*******************************************************************/
758 /* create a new type struct */
759 INLINE type *new_type_struct (ident *name) {
761 res = new_type(type_struct, NULL, name);
762 res->attr.sa.members = NEW_ARR_F (entity *, 1);
765 type *new_d_type_struct (ident *name, dbg_info* db) {
766 type *res = new_type_struct (name);
767 set_type_dbg_info(res, db);
770 INLINE void free_struct_attrs (type *strct) {
771 assert(strct && (strct->type_op == type_struct));
772 DEL_ARR_F(strct->attr.sa.members);
775 /* manipulate private fields of struct */
776 int get_struct_n_members (type *strct) {
777 assert(strct && (strct->type_op == type_struct));
778 return (ARR_LEN (strct->attr.sa.members))-1;
780 void add_struct_member (type *strct, entity *member) {
781 assert(strct && (strct->type_op == type_struct));
782 assert(get_type_tpop(get_entity_type(member)) != type_method);
783 /* @@@ lowerfirm geht nicht durch */
784 ARR_APP1 (entity *, strct->attr.sa.members, member);
786 entity *get_struct_member (type *strct, int pos) {
787 assert(strct && (strct->type_op == type_struct));
788 assert(pos >= 0 && pos < get_struct_n_members(strct));
789 return strct->attr.sa.members[pos+1];
791 void set_struct_member (type *strct, int pos, entity *member) {
792 assert(strct && (strct->type_op == type_struct));
793 assert(pos >= 0 && pos < get_struct_n_members(strct));
794 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
795 strct->attr.sa.members[pos+1] = member;
797 void remove_struct_member(type *strct, entity *member) {
799 assert(strct && (strct->type_op == type_struct));
800 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
801 if (strct->attr.sa.members[i] == member) {
802 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
803 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
804 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
810 bool is_struct_type(type *strct) {
812 if (strct->type_op == type_struct) return 1; else return 0;
815 /*******************************************************************/
817 /*******************************************************************/
819 /* Lazy construction of value argument / result representation. */
821 build_value_type(ident *name, int len, type **tps) {
823 type *res = new_type_struct(name);
824 /* Remove type from type list. Must be treated differently than other types. */
825 remove_irp_type_from_list(res);
826 for (i = 0; i < len; i++) {
827 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
828 if (tps[i]) elt_type = tps[i];
829 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
834 /* Create a new method type.
835 N_param is the number of parameters, n_res the number of results. */
836 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
838 res = new_type(type_method, mode_P, name);
839 res->state = layout_fixed;
840 assert((get_mode_size_bytes(mode_P) != -1) && "unorthodox modes not implemented");
841 res->size = get_mode_size_bytes(mode_P);
842 res->attr.ma.n_params = n_param;
843 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
844 res->attr.ma.value_params = NULL;
845 res->attr.ma.n_res = n_res;
846 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
847 res->attr.ma.value_ress = NULL;
848 res->attr.ma.variadicity = non_variadic;
852 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
853 type *res = new_type_method (name, n_param, n_res);
854 set_type_dbg_info(res, db);
857 INLINE void free_method_attrs(type *method) {
858 assert(method && (method->type_op == type_method));
859 free(method->attr.ma.param_type);
860 free(method->attr.ma.res_type);
862 /* manipulate private fields of method. */
863 int get_method_n_params (type *method) {
864 assert(method && (method->type_op == type_method));
865 return method->attr.ma.n_params;
867 type *get_method_param_type(type *method, int pos) {
868 assert(method && (method->type_op == type_method));
869 assert(pos >= 0 && pos < get_method_n_params(method));
870 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
872 void set_method_param_type(type *method, int pos, type* tp) {
873 assert(method && (method->type_op == type_method));
874 assert(pos >= 0 && pos < get_method_n_params(method));
875 method->attr.ma.param_type[pos] = tp;
876 /* If information constructed set pass-by-value representation. */
877 if (method->attr.ma.value_params) {
878 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
879 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
882 /* Returns an entity that represents the copied value argument. Only necessary
883 for compounds passed by value. */
884 entity *get_method_value_param_ent(type *method, int pos) {
885 assert(method && (method->type_op == type_method));
886 assert(pos >= 0 && pos < get_method_n_params(method));
887 if (!method->attr.ma.value_params)
888 method->attr.ma.value_params
889 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
890 get_method_n_params(method), method->attr.ma.param_type);
891 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos)) != method->attr.ma.value_params)
892 && "param type not yet set");
893 return get_struct_member(method->attr.ma.value_params, pos);
896 int get_method_n_ress (type *method) {
897 assert(method && (method->type_op == type_method));
898 return method->attr.ma.n_res;
900 type *get_method_res_type(type *method, int pos) {
901 assert(method && (method->type_op == type_method));
902 assert(pos >= 0 && pos < get_method_n_ress(method));
903 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
905 void set_method_res_type(type *method, int pos, type* tp) {
906 assert(method && (method->type_op == type_method));
907 assert(pos >= 0 && pos < get_method_n_ress(method));
908 /* set the result type */
909 method->attr.ma.res_type[pos] = tp;
910 /* If information constructed set pass-by-value representation. */
911 if (method->attr.ma.value_ress) {
912 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
913 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
916 /* Returns an entity that represents the copied value result. Only necessary
917 for compounds passed by value. */
918 entity *get_method_value_res_ent(type *method, int pos) {
919 assert(method && (method->type_op == type_method));
920 assert(pos >= 0 && pos < get_method_n_ress(method));
921 if (!method->attr.ma.value_ress)
922 method->attr.ma.value_ress
923 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
924 get_method_n_ress(method), method->attr.ma.res_type);
925 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
926 && "result type not yet set");
927 return get_struct_member(method->attr.ma.value_ress, pos);
930 variadicity get_method_variadicity(type *method)
932 assert(method && (method->type_op == type_method));
933 return method->attr.ma.variadicity;
936 void set_method_variadicity(type *method, variadicity vari)
938 assert(method && (method->type_op == type_method));
939 method->attr.ma.variadicity = vari;
943 bool is_method_type (type *method) {
945 if (method->type_op == type_method) return 1; else return 0;
948 /*******************************************************************/
950 /*******************************************************************/
952 /* create a new type uni */
953 INLINE type *new_type_union (ident *name) {
955 res = new_type(type_union, NULL, name);
956 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
957 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
958 res->attr.ua.members = NEW_ARR_F (entity *, 1);
961 type *new_d_type_union (ident *name, dbg_info* db) {
962 type *res = new_type_union (name);
963 set_type_dbg_info(res, db);
966 INLINE void free_union_attrs (type *uni) {
967 assert(uni && (uni->type_op == type_union));
968 DEL_ARR_F(uni->attr.ua.members);
970 /* manipulate private fields of union */
972 int get_union_n_types (type *uni) {
973 assert(uni && (uni->type_op == type_union));
974 return uni->attr.ua.n_types;
976 type *get_union_unioned_type (type *uni, int pos) {
977 assert(uni && (uni->type_op == type_union));
978 assert(pos >= 0 && pos < get_union_n_types(uni));
979 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
981 void set_union_unioned_type (type *uni, int pos, type *tp) {
982 assert(uni && (uni->type_op == type_union));
983 assert(pos >= 0 && pos < get_union_n_types(uni));
984 uni->attr.ua.unioned_type[pos] = tp;
986 ident *get_union_delim_nameid (type *uni, int pos) {
987 assert(uni && (uni->type_op == type_union));
988 assert(pos >= 0 && pos < get_union_n_types(uni));
989 return uni->attr.ua.delim_names[pos];
991 const char *get_union_delim_name (type *uni, int pos) {
992 assert(uni && (uni->type_op == type_union));
993 assert(pos >= 0 && pos < get_union_n_types(uni));
994 return id_to_str(uni->attr.ua.delim_names[pos]);
996 void set_union_delim_nameid (type *uni, int pos, ident *id) {
997 assert(uni && (uni->type_op == type_union));
998 assert(pos >= 0 && pos < get_union_n_types(uni));
999 uni->attr.ua.delim_names[pos] = id;
1002 int get_union_n_members (type *uni) {
1003 assert(uni && (uni->type_op == type_union));
1004 return (ARR_LEN (uni->attr.ua.members))-1;
1006 void add_union_member (type *uni, entity *member) {
1007 assert(uni && (uni->type_op == type_union));
1008 ARR_APP1 (entity *, uni->attr.ua.members, member);
1010 entity *get_union_member (type *uni, int pos) {
1011 assert(uni && (uni->type_op == type_union));
1012 assert(pos >= 0 && pos < get_union_n_members(uni));
1013 return uni->attr.ua.members[pos+1];
1015 void set_union_member (type *uni, int pos, entity *member) {
1016 assert(uni && (uni->type_op == type_union));
1017 assert(pos >= 0 && pos < get_union_n_members(uni));
1018 uni->attr.ua.members[pos+1] = member;
1020 void remove_union_member(type *uni, entity *member) {
1022 assert(uni && (uni->type_op == type_union));
1023 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
1024 if (uni->attr.ua.members[i] == member) {
1025 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1026 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1027 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1033 bool is_union_type (type *uni) {
1035 if (uni->type_op == type_union) return 1; else return 0;
1038 /*******************************************************************/
1040 /*******************************************************************/
1043 /* create a new type array -- set dimension sizes independently */
1044 INLINE type *new_type_array (ident *name, int n_dimensions,
1045 type *element_type) {
1048 assert(!is_method_type(element_type));
1049 res = new_type(type_array, NULL, name);
1050 res->attr.aa.n_dimensions = n_dimensions;
1051 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1052 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1053 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
1055 for (i = 0; i < n_dimensions; i++) {
1056 res->attr.aa.lower_bound[i] = NULL;
1057 res->attr.aa.upper_bound[i] = NULL;
1058 res->attr.aa.order[i] = i;
1060 res->attr.aa.element_type = element_type;
1061 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
1065 type *new_d_type_array (ident *name, int n_dimensions,
1066 type *element_type, dbg_info* db) {
1067 type *res = new_type_array (name, n_dimensions, element_type);
1068 set_type_dbg_info(res, db);
1072 INLINE void free_array_attrs (type *array) {
1073 assert(array && (array->type_op == type_array));
1074 free(array->attr.aa.lower_bound);
1075 free(array->attr.aa.upper_bound);
1078 /* manipulate private fields of array type */
1079 int get_array_n_dimensions (type *array) {
1080 assert(array && (array->type_op == type_array));
1081 return array->attr.aa.n_dimensions;
1085 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1086 ir_node * upper_bound) {
1087 assert(array && (array->type_op == type_array));
1088 array->attr.aa.lower_bound[dimension] = lower_bound;
1089 array->attr.aa.upper_bound[dimension] = upper_bound;
1092 set_array_bounds_int (type *array, int dimension, int lower_bound,
1094 ir_graph *rem = current_ir_graph;
1095 current_ir_graph = get_const_code_irg();
1096 set_array_bounds (array, dimension,
1097 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1098 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1099 current_ir_graph = rem;
1102 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1103 assert(array && (array->type_op == type_array));
1104 array->attr.aa.lower_bound[dimension] = lower_bound;
1106 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1107 ir_graph *rem = current_ir_graph;
1108 current_ir_graph = get_const_code_irg();
1109 set_array_lower_bound (array, dimension,
1110 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1111 current_ir_graph = rem;
1114 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1115 assert(array && (array->type_op == type_array));
1116 array->attr.aa.upper_bound[dimension] = upper_bound;
1118 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1119 ir_graph *rem = current_ir_graph;
1120 current_ir_graph = get_const_code_irg();
1121 set_array_upper_bound (array, dimension,
1122 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1123 current_ir_graph = rem;
1125 ir_node * get_array_lower_bound (type *array, int dimension) {
1126 assert(array && (array->type_op == type_array));
1127 return array->attr.aa.lower_bound[dimension];
1129 ir_node * get_array_upper_bound (type *array, int dimension) {
1130 assert(array && (array->type_op == type_array));
1131 return array->attr.aa.upper_bound[dimension];
1134 void set_array_order (type *array, int dimension, int order) {
1135 assert(array && (array->type_op == type_array));
1136 array->attr.aa.order[dimension] = order;
1138 int get_array_order (type *array, int dimension) {
1139 assert(array && (array->type_op == type_array));
1140 return array->attr.aa.order[dimension];
1143 void set_array_element_type (type *array, type *tp) {
1144 assert(array && (array->type_op == type_array));
1145 assert(!is_method_type(tp));
1146 array->attr.aa.element_type = tp;
1148 type *get_array_element_type (type *array) {
1149 assert(array && (array->type_op == type_array));
1150 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1153 void set_array_element_entity (type *array, entity *ent) {
1154 assert(array && (array->type_op == type_array));
1155 assert((get_entity_type(ent)->type_op != type_method));
1156 array->attr.aa.element_ent = ent;
1157 array->attr.aa.element_type = get_entity_type(ent);
1159 entity *get_array_element_entity (type *array) {
1160 assert(array && (array->type_op == type_array));
1161 return array->attr.aa.element_ent;
1165 bool is_array_type (type *array) {
1167 if (array->type_op == type_array) return 1; else return 0;
1170 /*******************************************************************/
1171 /** TYPE_ENUMERATION **/
1172 /*******************************************************************/
1174 /* create a new type enumeration -- set the enumerators independently */
1175 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1178 res = new_type(type_enumeration, NULL, name);
1179 res->attr.ea.n_enums = n_enums;
1180 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1181 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1182 for (i = 0; i < n_enums; i++) {
1183 res->attr.ea.enumer[i] = NULL;
1184 res->attr.ea.enum_nameid = NULL;
1188 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1189 type *res = new_type_enumeration (name, n_enums);
1190 set_type_dbg_info(res, db);
1194 INLINE void free_enumeration_attrs(type *enumeration) {
1195 assert(enumeration && (enumeration->type_op == type_enumeration));
1196 free(enumeration->attr.ea.enumer);
1197 free(enumeration->attr.ea.enum_nameid);
1200 /* manipulate fields of enumeration type. */
1201 int get_enumeration_n_enums (type *enumeration) {
1202 assert(enumeration && (enumeration->type_op == type_enumeration));
1203 return enumeration->attr.ea.n_enums;
1205 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1206 assert(enumeration && (enumeration->type_op == type_enumeration));
1207 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1208 enumeration->attr.ea.enumer[pos] = con;
1210 tarval *get_enumeration_enum (type *enumeration, int pos) {
1211 assert(enumeration && (enumeration->type_op == type_enumeration));
1212 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1213 return enumeration->attr.ea.enumer[pos];
1215 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1216 assert(enumeration && (enumeration->type_op == type_enumeration));
1217 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1218 enumeration->attr.ea.enum_nameid[pos] = id;
1220 ident *get_enumeration_nameid (type *enumeration, int pos) {
1221 assert(enumeration && (enumeration->type_op == type_enumeration));
1222 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1223 return enumeration->attr.ea.enum_nameid[pos];
1225 const char *get_enumeration_name(type *enumeration, int pos) {
1226 assert(enumeration && (enumeration->type_op == type_enumeration));
1227 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1228 return id_to_str(enumeration->attr.ea.enum_nameid[pos]);
1232 bool is_enumeration_type (type *enumeration) {
1233 assert(enumeration);
1234 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1237 /*******************************************************************/
1238 /** TYPE_POINTER **/
1239 /*******************************************************************/
1241 /* Create a new type pointer */
1242 INLINE type *new_type_pointer (ident *name, type *points_to) {
1244 res = new_type(type_pointer, mode_P, name);
1245 res->attr.pa.points_to = points_to;
1246 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1247 res->size = get_mode_size_bytes(res->mode);
1248 res->state = layout_fixed;
1251 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db) {
1252 type *res = new_type_pointer (name, points_to);
1253 set_type_dbg_info(res, db);
1256 INLINE void free_pointer_attrs (type *pointer) {
1257 assert(pointer && (pointer->type_op == type_pointer));
1259 /* manipulate fields of type_pointer */
1260 void set_pointer_points_to_type (type *pointer, type *tp) {
1261 assert(pointer && (pointer->type_op == type_pointer));
1262 pointer->attr.pa.points_to = tp;
1264 type *get_pointer_points_to_type (type *pointer) {
1265 assert(pointer && (pointer->type_op == type_pointer));
1266 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1270 bool is_pointer_type (type *pointer) {
1272 if (pointer->type_op == type_pointer) return 1; else return 0;
1276 /*******************************************************************/
1277 /** TYPE_PRIMITIVE **/
1278 /*******************************************************************/
1280 /* create a new type primitive */
1281 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1283 /* @@@ assert( mode_is_data(mode) && (!mode == mode_P)); */
1284 res = new_type(type_primitive, mode, name);
1285 assert((get_mode_size_bytes(mode) != -1) && "unorthodox modes not implemented");
1286 res->size = get_mode_size_bytes(mode);
1287 res->state = layout_fixed;
1290 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1291 type *res = new_type_primitive (name, mode);
1292 set_type_dbg_info(res, db);
1295 INLINE void free_primitive_attrs (type *primitive) {
1296 assert(primitive && (primitive->type_op == type_primitive));
1300 bool is_primitive_type (type *primitive) {
1301 assert(primitive && primitive->kind == k_type);
1302 if (primitive->type_op == type_primitive) return 1; else return 0;
1305 /*******************************************************************/
1306 /** common functionality **/
1307 /*******************************************************************/
1310 INLINE int is_atomic_type(type *tp) {
1311 assert(tp && tp->kind == k_type);
1312 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1313 is_enumeration_type(tp));
1317 * Gets the number of elements in a firm compound type.
1319 int get_compound_n_members(type *tp)
1323 if (is_struct_type(tp))
1324 res = get_struct_n_members(tp);
1325 else if (is_class_type(tp))
1326 res = get_class_n_members(tp);
1327 else if (is_union_type(tp))
1328 res = get_union_n_members(tp);
1330 assert(0 && "need struct, union or class for member count");
1336 * Gets the member of a firm compound type at position pos.
1338 entity *get_compound_member(type *tp, int pos)
1342 if (is_struct_type(tp))
1343 res = get_struct_member(tp, pos);
1344 else if (is_class_type(tp))
1345 res = get_class_member(tp, pos);
1346 else if (is_union_type(tp))
1347 res = get_union_member(tp, pos);
1349 assert(0 && "need struct, union or class to get a member");
1355 INLINE int is_compound_type(type *tp) {
1356 assert(tp && tp->kind == k_type);
1357 return (is_class_type(tp) || is_struct_type(tp) ||
1358 is_array_type(tp) || is_union_type(tp));