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 get_id_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 (get_id_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 if (get_entity_offset(get_class_member(tp, i)) <= -1)
240 { DDMT(tp); DDME(get_class_member(tp, i)); }
241 assert(get_entity_offset(get_class_member(tp, i)) > -1);
242 assert(is_method_type(get_entity_type(get_class_member(tp, i))) ||
243 (get_entity_allocation(get_class_member(tp, i)) == automatic_allocated));
248 assert(get_type_size(tp) > -1);
249 for (i = 0; i < get_struct_n_members(tp); i++) {
250 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
251 assert((get_entity_allocation(get_struct_member(tp, i)) == automatic_allocated));
260 Assure that only innermost dimension is dynamic? */
262 case tpo_enumeration:
264 assert(get_type_mode != NULL);
265 for (i = 0; i < get_enumeration_n_enums(tp); i++)
266 assert(get_enumeration_enum(tp, i) != NULL);
274 unsigned long get_type_visited(type *tp) {
275 assert(tp && tp->kind == k_type);
279 void set_type_visited(type *tp, unsigned long num) {
280 assert(tp && tp->kind == k_type);
283 /* Sets visited field in type to type_visited. */
284 void mark_type_visited(type *tp) {
285 assert(tp && tp->kind == k_type);
286 assert(tp->visit < type_visited);
287 tp->visit = type_visited;
289 /* @@@ name clash with master flag
290 bool type_visited(type *tp) {
291 assert(tp && tp->kind == k_type);
292 return tp->visit >= type_visited;
294 bool type_not_visited(type *tp) {
295 assert(tp && tp->kind == k_type);
296 return tp->visit < type_visited;
300 int is_type (void *thing) {
302 if (get_kind(thing) == k_type)
309 bool equal_type(type *typ1, type *typ2) {
314 if (typ1 == typ2) return true;
316 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
317 (get_type_ident(typ1) != get_type_ident(typ2)) ||
318 (get_type_mode(typ1) != get_type_mode(typ2)) ||
319 (get_type_state(typ1) != get_type_state(typ2)))
321 if ((get_type_state(typ1) == layout_fixed) &&
322 (get_type_size(typ1) != get_type_size(typ2)))
325 switch(get_type_tpop_code(typ1)) {
327 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return false;
328 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return false;
329 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return false;
330 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return false;
331 /** Compare the members **/
332 m = alloca(sizeof(entity *) * get_class_n_members(typ1));
333 memset(m, 0, sizeof(entity *) * get_class_n_members(typ1));
334 /* First sort the members of typ2 */
335 for (i = 0; i < get_class_n_members(typ1); i++) {
336 entity *e1 = get_class_member(typ1, i);
337 for (j = 0; j < get_class_n_members(typ2); j++) {
338 entity *e2 = get_class_member(typ2, j);
339 if (get_entity_name(e1) == get_entity_name(e2))
343 for (i = 0; i < get_class_n_members(typ1); i++) {
344 if (!m[i] || /* Found no counterpart */
345 !equal_entity(get_class_member(typ1, i), m[i]))
348 /** Compare the supertypes **/
349 t = alloca(sizeof(entity *) * get_class_n_supertypes(typ1));
350 memset(t, 0, sizeof(entity *) * get_class_n_supertypes(typ1));
351 /* First sort the supertypes of typ2 */
352 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
353 type *t1 = get_class_supertype(typ1, i);
354 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
355 type *t2 = get_class_supertype(typ2, j);
356 if (get_type_ident(t2) == get_type_ident(t1))
360 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
361 if (!t[i] || /* Found no counterpart */
362 get_class_supertype(typ1, i) != t[i])
367 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return false;
368 m = alloca(sizeof(entity *) * get_struct_n_members(typ1));
369 memset(m, 0, sizeof(entity *) * get_struct_n_members(typ1));
370 /* First sort the members of lt */
371 for (i = 0; i < get_struct_n_members(typ1); i++) {
372 entity *e1 = get_struct_member(typ1, i);
373 for (j = 0; j < get_struct_n_members(typ2); j++) {
374 entity *e2 = get_struct_member(typ2, j);
375 if (get_entity_name(e1) == get_entity_name(e2))
379 for (i = 0; i < get_struct_n_members(typ1); i++) {
380 if (!m[i] || /* Found no counterpart */
381 !equal_entity(get_struct_member(typ1, i), m[i]))
386 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return false;
387 if (get_method_n_params(typ1) != get_method_n_params(typ2)) return false;
388 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return false;
389 for (i = 0; i < get_method_n_params(typ1); i++) {
390 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
393 for (i = 0; i < get_method_n_ress(typ1); i++) {
394 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
399 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return false;
400 m = alloca(sizeof(entity *) * get_union_n_members(typ1));
401 memset(m, 0, sizeof(entity *) * get_union_n_members(typ1));
402 /* First sort the members of lt */
403 for (i = 0; i < get_union_n_members(typ1); i++) {
404 entity *e1 = get_union_member(typ1, i);
405 for (j = 0; j < get_union_n_members(typ2); j++) {
406 entity *e2 = get_union_member(typ2, j);
407 if (get_entity_name(e1) == get_entity_name(e2))
411 for (i = 0; i < get_union_n_members(typ1); i++) {
412 if (!m[i] || /* Found no counterpart */
413 !equal_entity(get_union_member(typ1, i), m[i]))
418 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
420 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
422 for(i = 0; i < get_array_n_dimensions(typ1); i++) {
423 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
424 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
426 if (get_array_order(typ1, i) != get_array_order(typ2, i))
427 assert(0 && "type compare with different dimension orders not implemented");
430 case tpo_enumeration: {
431 assert(0 && "enumerations not implemented");
434 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
437 case tpo_primitive: {
444 bool smaller_type (type *st, type *lt) {
448 if (st == lt) return true;
450 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
453 switch(get_type_tpop_code(st)) {
455 return is_subclass_of(st, lt);
458 if (get_struct_n_members(st) != get_struct_n_members(lt)) return false;
459 m = alloca(sizeof(entity *) * get_struct_n_members(st));
460 memset(m, 0, sizeof(entity *) * get_struct_n_members(st));
461 /* First sort the members of lt */
462 for (i = 0; i < get_struct_n_members(st); i++) {
463 entity *se = get_struct_member(st, i);
464 for (j = 0; j < get_struct_n_members(lt); j++) {
465 entity *le = get_struct_member(lt, j);
466 if (get_entity_name(le) == get_entity_name(se))
470 for (i = 0; i < get_struct_n_members(st); i++) {
471 if (!m[i] || /* Found no counterpart */
472 !smaller_type(get_entity_type(get_struct_member(st, i)),
473 get_entity_type(m[i])))
478 if (get_method_variadicity(st) != get_method_variadicity(lt)) return false;
479 if (get_method_n_params(st) != get_method_n_params(lt)) return false;
480 if (get_method_n_ress(st) != get_method_n_ress(lt)) return false;
481 for (i = 0; i < get_method_n_params(st); i++) {
482 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
485 for (i = 0; i < get_method_n_ress(st); i++) {
486 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
491 if (get_union_n_members(st) != get_union_n_members(lt)) return false;
492 m = alloca(sizeof(entity *) * get_union_n_members(st));
493 memset(m, 0, sizeof(entity *) * get_union_n_members(st));
494 /* First sort the members of lt */
495 for (i = 0; i < get_union_n_members(st); i++) {
496 entity *se = get_union_member(st, i);
497 for (j = 0; j < get_union_n_members(lt); j++) {
498 entity *le = get_union_member(lt, j);
499 if (get_entity_name(le) == get_entity_name(se))
503 for (i = 0; i < get_union_n_members(st); i++) {
504 if (!m[i] || /* Found no counterpart */
505 !smaller_type(get_entity_type(get_union_member(st, i)),
506 get_entity_type(m[i])))
511 type *set, *let; /* small/large elt. type */
512 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
514 set = get_array_element_type(st);
515 let = get_array_element_type(lt);
517 /* If the elt types are different, set must be convertible
518 to let, and they must have the same size so that address
519 computations work out. To have a size the layout must
521 if ((get_type_state(set) != layout_fixed) ||
522 (get_type_state(let) != layout_fixed))
524 if (!smaller_type(set, let) ||
525 get_type_size(set) != get_type_size(let))
528 for(i = 0; i < get_array_n_dimensions(st); i++) {
529 if (get_array_lower_bound(lt, i))
530 if(get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
532 if (get_array_upper_bound(lt, i))
533 if(get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
537 case tpo_enumeration: {
538 assert(0 && "enumerations not implemented");
541 if (!smaller_type(get_pointer_points_to_type(st),
542 get_pointer_points_to_type(lt)))
545 case tpo_primitive: {
546 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
554 /*******************************************************************/
556 /*******************************************************************/
558 /* create a new class type */
559 INLINE type *new_type_class (ident *name) {
562 res = new_type(type_class, NULL, name);
564 res->attr.ca.members = NEW_ARR_F (entity *, 1);
565 res->attr.ca.subtypes = NEW_ARR_F (type *, 1);
566 res->attr.ca.supertypes = NEW_ARR_F (type *, 1);
567 res->attr.ca.peculiarity = existent;
568 res->attr.ca.dfn = 0;
572 type *new_d_type_class (ident *name, dbg_info* db) {
573 type *res = new_type_class (name);
574 set_type_dbg_info(res, db);
577 INLINE void free_class_attrs(type *clss) {
578 assert(clss && (clss->type_op == type_class));
579 DEL_ARR_F(clss->attr.ca.members);
580 DEL_ARR_F(clss->attr.ca.subtypes);
581 DEL_ARR_F(clss->attr.ca.supertypes);
584 /* manipulate private fields of class type */
585 void add_class_member (type *clss, entity *member) {
586 assert(clss && (clss->type_op == type_class));
587 ARR_APP1 (entity *, clss->attr.ca.members, member);
589 int get_class_n_members (type *clss) {
590 assert(clss && (clss->type_op == type_class));
591 return (ARR_LEN (clss->attr.ca.members))-1;
593 int get_class_member_index(type *clss, entity *mem) {
595 assert(clss && (clss->type_op == type_class));
596 for (i = 0; i < get_class_n_members(clss); i++)
597 if (get_class_member(clss, i) == mem)
601 entity *get_class_member (type *clss, int pos) {
602 assert(clss && (clss->type_op == type_class));
603 assert(pos >= 0 && pos < get_class_n_members(clss));
604 return clss->attr.ca.members[pos+1];
606 void set_class_member (type *clss, entity *member, int pos) {
607 assert(clss && (clss->type_op == type_class));
608 assert(pos >= 0 && pos < get_class_n_members(clss));
609 clss->attr.ca.members[pos+1] = member;
611 void set_class_members (type *clss, entity **members, int arity) {
613 assert(clss && (clss->type_op == type_class));
614 DEL_ARR_F(clss->attr.ca.members);
615 clss->attr.ca.members = NEW_ARR_F (entity *, 1);
616 for (i = 0; i < arity; i++) {
617 set_entity_owner(members[i], clss);
618 ARR_APP1 (entity *, clss->attr.ca.members, members[i]);
621 void remove_class_member(type *clss, entity *member) {
623 assert(clss && (clss->type_op == type_class));
624 for (i = 1; i < (ARR_LEN (clss->attr.ca.members)); i++) {
625 if (clss->attr.ca.members[i] == member) {
626 for(; i < (ARR_LEN (clss->attr.ca.members)) - 1; i++)
627 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
628 ARR_SETLEN(entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
634 void add_class_subtype (type *clss, type *subtype) {
636 assert(clss && (clss->type_op == type_class));
637 ARR_APP1 (type *, clss->attr.ca.subtypes, subtype);
638 for (i = 0; i < get_class_n_supertypes(subtype); i++)
639 if (get_class_supertype(subtype, i) == clss)
640 /* Class already registered */
642 ARR_APP1 (type *, subtype->attr.ca.supertypes, clss);
644 int get_class_n_subtypes (type *clss) {
645 assert(clss && (clss->type_op == type_class));
646 return (ARR_LEN (clss->attr.ca.subtypes))-1;
648 type *get_class_subtype (type *clss, int pos) {
649 assert(clss && (clss->type_op == type_class));
650 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
651 return clss->attr.ca.subtypes[pos+1] = skip_tid(clss->attr.ca.subtypes[pos+1]);
653 void set_class_subtype (type *clss, type *subtype, int pos) {
654 assert(clss && (clss->type_op == type_class));
655 assert(pos >= 0 && pos < get_class_n_subtypes(clss));
656 clss->attr.ca.subtypes[pos+1] = subtype;
658 void remove_class_subtype(type *clss, type *subtype) {
660 assert(clss && (clss->type_op == type_class));
661 for (i = 1; i < (ARR_LEN (clss->attr.ca.subtypes)); i++)
662 if (clss->attr.ca.subtypes[i] == subtype) {
663 for(; i < (ARR_LEN (clss->attr.ca.subtypes))-1; i++)
664 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
665 ARR_SETLEN(entity*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
670 void add_class_supertype (type *clss, type *supertype) {
672 assert(clss && (clss->type_op == type_class));
673 assert(supertype && (supertype -> type_op == type_class));
674 ARR_APP1 (type *, clss->attr.ca.supertypes, supertype);
675 for (i = 0; i < get_class_n_subtypes(supertype); i++)
676 if (get_class_subtype(supertype, i) == clss)
677 /* Class already registered */
679 ARR_APP1 (type *, supertype->attr.ca.subtypes, clss);
681 int get_class_n_supertypes (type *clss) {
682 assert(clss && (clss->type_op == type_class));
683 return (ARR_LEN (clss->attr.ca.supertypes))-1;
685 int get_class_supertype_index(type *clss, type *super_clss) {
687 assert(clss && (clss->type_op == type_class));
688 assert(super_clss && (super_clss->type_op == type_class));
689 for (i = 0; i < get_class_n_supertypes(clss); i++)
690 if (get_class_supertype(clss, i) == super_clss)
694 type *get_class_supertype (type *clss, int pos) {
695 assert(clss && (clss->type_op == type_class));
696 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
697 return clss->attr.ca.supertypes[pos+1] = skip_tid(clss->attr.ca.supertypes[pos+1]);
699 void set_class_supertype (type *clss, type *supertype, int pos) {
700 assert(clss && (clss->type_op == type_class));
701 assert(pos >= 0 && pos < get_class_n_supertypes(clss));
702 clss->attr.ca.supertypes[pos+1] = supertype;
704 void remove_class_supertype(type *clss, type *supertype) {
706 assert(clss && (clss->type_op == type_class));
707 for (i = 1; i < (ARR_LEN (clss->attr.ca.supertypes)); i++)
708 if (clss->attr.ca.supertypes[i] == supertype) {
709 for(; i < (ARR_LEN (clss->attr.ca.supertypes))-1; i++)
710 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
711 ARR_SETLEN(entity*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
716 INLINE peculiarity get_class_peculiarity (type *clss) {
717 assert(clss && (clss->type_op == type_class));
718 return clss->attr.ca.peculiarity;
720 INLINE void set_class_peculiarity (type *clss, peculiarity pec) {
721 assert(clss && (clss->type_op == type_class));
722 assert(pec != inherited); /* There is no inheritance of types in libFirm. */
723 clss->attr.ca.peculiarity = pec;
726 void set_class_dfn (type *clss, int dfn)
728 clss->attr.ca.dfn = dfn;
731 int get_class_dfn (type *clss)
733 return (clss->attr.ca.dfn);
737 bool is_class_type(type *clss) {
739 if (clss->type_op == type_class) return 1; else return 0;
742 bool is_subclass_of(type *low, type *high) {
744 assert(is_class_type(low) && is_class_type(high));
745 if (low == high) return true;
746 /* depth first search from high downwards. */
747 for (i = 0; i < get_class_n_subtypes(high); i++) {
748 if (low == get_class_subtype(high, i))
750 if (is_subclass_of(low, get_class_subtype(high, i)))
756 /*******************************************************************/
758 /*******************************************************************/
760 /* create a new type struct */
761 INLINE type *new_type_struct (ident *name) {
763 res = new_type(type_struct, NULL, name);
764 res->attr.sa.members = NEW_ARR_F (entity *, 1);
767 type *new_d_type_struct (ident *name, dbg_info* db) {
768 type *res = new_type_struct (name);
769 set_type_dbg_info(res, db);
772 INLINE void free_struct_attrs (type *strct) {
773 assert(strct && (strct->type_op == type_struct));
774 DEL_ARR_F(strct->attr.sa.members);
777 /* manipulate private fields of struct */
778 int get_struct_n_members (type *strct) {
779 assert(strct && (strct->type_op == type_struct));
780 return (ARR_LEN (strct->attr.sa.members))-1;
782 void add_struct_member (type *strct, entity *member) {
783 assert(strct && (strct->type_op == type_struct));
784 assert(get_type_tpop(get_entity_type(member)) != type_method);
785 /* @@@ lowerfirm geht nicht durch */
786 ARR_APP1 (entity *, strct->attr.sa.members, member);
788 entity *get_struct_member (type *strct, int pos) {
789 assert(strct && (strct->type_op == type_struct));
790 assert(pos >= 0 && pos < get_struct_n_members(strct));
791 return strct->attr.sa.members[pos+1];
793 void set_struct_member (type *strct, int pos, entity *member) {
794 assert(strct && (strct->type_op == type_struct));
795 assert(pos >= 0 && pos < get_struct_n_members(strct));
796 assert(get_entity_type(member)->type_op != type_method);/* @@@ lowerfirm !!*/
797 strct->attr.sa.members[pos+1] = member;
799 void remove_struct_member(type *strct, entity *member) {
801 assert(strct && (strct->type_op == type_struct));
802 for (i = 1; i < (ARR_LEN (strct->attr.sa.members)); i++)
803 if (strct->attr.sa.members[i] == member) {
804 for(; i < (ARR_LEN (strct->attr.sa.members))-1; i++)
805 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
806 ARR_SETLEN(entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
812 bool is_struct_type(type *strct) {
814 if (strct->type_op == type_struct) return 1; else return 0;
817 /*******************************************************************/
819 /*******************************************************************/
821 /* Lazy construction of value argument / result representation. */
823 build_value_type(ident *name, int len, type **tps) {
825 type *res = new_type_struct(name);
826 /* Remove type from type list. Must be treated differently than other types. */
827 remove_irp_type_from_list(res);
828 for (i = 0; i < len; i++) {
829 type *elt_type = res; /* use res as default if corresponding type is not yet set. */
830 if (tps[i]) elt_type = tps[i];
831 new_entity(res, mangle_u(name, get_type_ident(elt_type)), elt_type);
836 /* Create a new method type.
837 N_param is the number of parameters, n_res the number of results. */
838 INLINE type *new_type_method (ident *name, int n_param, int n_res) {
840 res = new_type(type_method, mode_P_mach, name);
841 res->state = layout_fixed;
842 assert((get_mode_size_bytes(mode_P_mach) != -1) && "unorthodox modes not implemented");
843 res->size = get_mode_size_bytes(mode_P_mach);
844 res->attr.ma.n_params = n_param;
845 res->attr.ma.param_type = (type **) xmalloc (sizeof (type *) * n_param);
846 res->attr.ma.value_params = NULL;
847 res->attr.ma.n_res = n_res;
848 res->attr.ma.res_type = (type **) xmalloc (sizeof (type *) * n_res);
849 res->attr.ma.value_ress = NULL;
850 res->attr.ma.variadicity = non_variadic;
855 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db) {
856 type *res = new_type_method (name, n_param, n_res);
857 set_type_dbg_info(res, db);
861 INLINE void free_method_attrs(type *method) {
862 assert(method && (method->type_op == type_method));
863 free(method->attr.ma.param_type);
864 free(method->attr.ma.res_type);
867 /* manipulate private fields of method. */
868 int get_method_n_params (type *method) {
869 assert(method && (method->type_op == type_method));
870 return method->attr.ma.n_params;
872 type *get_method_param_type(type *method, int pos) {
873 assert(method && (method->type_op == type_method));
874 assert(pos >= 0 && pos < get_method_n_params(method));
875 return method->attr.ma.param_type[pos] = skip_tid(method->attr.ma.param_type[pos]);
877 void set_method_param_type(type *method, int pos, type* tp) {
878 assert(method && (method->type_op == type_method));
879 assert(pos >= 0 && pos < get_method_n_params(method));
880 method->attr.ma.param_type[pos] = tp;
881 /* If information constructed set pass-by-value representation. */
882 if (method->attr.ma.value_params) {
883 assert(get_method_n_params(method) == get_struct_n_members(method->attr.ma.value_params));
884 set_entity_type(get_struct_member(method->attr.ma.value_params, pos), tp);
887 /* Returns an entity that represents the copied value argument. Only necessary
888 for compounds passed by value. */
889 entity *get_method_value_param_ent(type *method, int pos) {
890 assert(method && (method->type_op == type_method));
891 assert(pos >= 0 && pos < get_method_n_params(method));
892 if (!method->attr.ma.value_params)
893 method->attr.ma.value_params
894 = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
895 get_method_n_params(method), method->attr.ma.param_type);
896 assert((get_entity_type(get_struct_member(method->attr.ma.value_params, pos)) != method->attr.ma.value_params)
897 && "param type not yet set");
898 return get_struct_member(method->attr.ma.value_params, pos);
901 int get_method_n_ress (type *method) {
902 assert(method && (method->type_op == type_method));
903 return method->attr.ma.n_res;
905 type *get_method_res_type(type *method, int pos) {
906 assert(method && (method->type_op == type_method));
907 assert(pos >= 0 && pos < get_method_n_ress(method));
908 return method->attr.ma.res_type[pos] = skip_tid(method->attr.ma.res_type[pos]);
910 void set_method_res_type(type *method, int pos, type* tp) {
911 assert(method && (method->type_op == type_method));
912 assert(pos >= 0 && pos < get_method_n_ress(method));
913 /* set the result type */
914 method->attr.ma.res_type[pos] = tp;
915 /* If information constructed set pass-by-value representation. */
916 if (method->attr.ma.value_ress) {
917 assert(get_method_n_ress(method) == get_struct_n_members(method->attr.ma.value_ress));
918 set_entity_type(get_struct_member(method->attr.ma.value_ress, pos), tp);
921 /* Returns an entity that represents the copied value result. Only necessary
922 for compounds passed by value. */
923 entity *get_method_value_res_ent(type *method, int pos) {
924 assert(method && (method->type_op == type_method));
925 assert(pos >= 0 && pos < get_method_n_ress(method));
926 if (!method->attr.ma.value_ress)
927 method->attr.ma.value_ress
928 = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
929 get_method_n_ress(method), method->attr.ma.res_type);
930 assert((get_entity_type(get_struct_member(method->attr.ma.value_ress, pos)) != method->attr.ma.value_ress)
931 && "result type not yet set");
932 return get_struct_member(method->attr.ma.value_ress, pos);
935 variadicity get_method_variadicity(type *method)
937 assert(method && (method->type_op == type_method));
938 return method->attr.ma.variadicity;
941 void set_method_variadicity(type *method, variadicity vari)
943 assert(method && (method->type_op == type_method));
944 method->attr.ma.variadicity = vari;
948 bool is_method_type (type *method) {
950 if (method->type_op == type_method) return 1; else return 0;
953 /*******************************************************************/
955 /*******************************************************************/
957 /* create a new type uni */
958 INLINE type *new_type_union (ident *name) {
960 res = new_type(type_union, NULL, name);
961 /*res->attr.ua.unioned_type = (type **) xmalloc (sizeof (type *) * n_types);
962 res->attr.ua.delim_names = (ident **) xmalloc (sizeof (ident *) * n_types); */
963 res->attr.ua.members = NEW_ARR_F (entity *, 1);
966 type *new_d_type_union (ident *name, dbg_info* db) {
967 type *res = new_type_union (name);
968 set_type_dbg_info(res, db);
971 INLINE void free_union_attrs (type *uni) {
972 assert(uni && (uni->type_op == type_union));
973 DEL_ARR_F(uni->attr.ua.members);
975 /* manipulate private fields of union */
977 int get_union_n_types (type *uni) {
978 assert(uni && (uni->type_op == type_union));
979 return uni->attr.ua.n_types;
981 type *get_union_unioned_type (type *uni, int pos) {
982 assert(uni && (uni->type_op == type_union));
983 assert(pos >= 0 && pos < get_union_n_types(uni));
984 return uni->attr.ua.unioned_type[pos] = skip_tid(uni->attr.ua.unioned_type[pos]);
986 void set_union_unioned_type (type *uni, int pos, type *tp) {
987 assert(uni && (uni->type_op == type_union));
988 assert(pos >= 0 && pos < get_union_n_types(uni));
989 uni->attr.ua.unioned_type[pos] = tp;
991 ident *get_union_delim_nameid (type *uni, int pos) {
992 assert(uni && (uni->type_op == type_union));
993 assert(pos >= 0 && pos < get_union_n_types(uni));
994 return uni->attr.ua.delim_names[pos];
996 const char *get_union_delim_name (type *uni, int pos) {
997 assert(uni && (uni->type_op == type_union));
998 assert(pos >= 0 && pos < get_union_n_types(uni));
999 return get_id_str(uni->attr.ua.delim_names[pos]);
1001 void set_union_delim_nameid (type *uni, int pos, ident *id) {
1002 assert(uni && (uni->type_op == type_union));
1003 assert(pos >= 0 && pos < get_union_n_types(uni));
1004 uni->attr.ua.delim_names[pos] = id;
1007 int get_union_n_members (type *uni) {
1008 assert(uni && (uni->type_op == type_union));
1009 return (ARR_LEN (uni->attr.ua.members))-1;
1011 void add_union_member (type *uni, entity *member) {
1012 assert(uni && (uni->type_op == type_union));
1013 ARR_APP1 (entity *, uni->attr.ua.members, member);
1015 entity *get_union_member (type *uni, int pos) {
1016 assert(uni && (uni->type_op == type_union));
1017 assert(pos >= 0 && pos < get_union_n_members(uni));
1018 return uni->attr.ua.members[pos+1];
1020 void set_union_member (type *uni, int pos, entity *member) {
1021 assert(uni && (uni->type_op == type_union));
1022 assert(pos >= 0 && pos < get_union_n_members(uni));
1023 uni->attr.ua.members[pos+1] = member;
1025 void remove_union_member(type *uni, entity *member) {
1027 assert(uni && (uni->type_op == type_union));
1028 for (i = 1; i < (ARR_LEN (uni->attr.ua.members)); i++)
1029 if (uni->attr.ua.members[i] == member) {
1030 for(; i < (ARR_LEN (uni->attr.ua.members))-1; i++)
1031 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1032 ARR_SETLEN(entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1038 bool is_union_type (type *uni) {
1040 if (uni->type_op == type_union) return 1; else return 0;
1043 /*******************************************************************/
1045 /*******************************************************************/
1048 /* create a new type array -- set dimension sizes independently */
1049 INLINE type *new_type_array (ident *name, int n_dimensions,
1050 type *element_type) {
1053 ir_graph *rem = current_ir_graph;
1054 assert(!is_method_type(element_type));
1056 res = new_type(type_array, NULL, name);
1057 res->attr.aa.n_dimensions = n_dimensions;
1058 res->attr.aa.lower_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1059 res->attr.aa.upper_bound = (ir_node **) xmalloc (sizeof (ir_node *) * n_dimensions);
1060 res->attr.aa.order = (int *) xmalloc (sizeof (int) * n_dimensions);
1062 current_ir_graph = get_const_code_irg();
1063 for (i = 0; i < n_dimensions; i++) {
1064 res->attr.aa.lower_bound[i] = new_Unknown();
1065 res->attr.aa.upper_bound[i] = new_Unknown();
1066 res->attr.aa.order[i] = i;
1068 current_ir_graph = rem;
1070 res->attr.aa.element_type = element_type;
1071 new_entity(res, mangle_u(name, id_from_str("elem_ent", 8)), element_type);
1075 type *new_d_type_array (ident *name, int n_dimensions,
1076 type *element_type, dbg_info* db) {
1077 type *res = new_type_array (name, n_dimensions, element_type);
1078 set_type_dbg_info(res, db);
1082 INLINE void free_array_attrs (type *array) {
1083 assert(array && (array->type_op == type_array));
1084 free(array->attr.aa.lower_bound);
1085 free(array->attr.aa.upper_bound);
1088 /* manipulate private fields of array type */
1089 int get_array_n_dimensions (type *array) {
1090 assert(array && (array->type_op == type_array));
1091 return array->attr.aa.n_dimensions;
1095 set_array_bounds (type *array, int dimension, ir_node * lower_bound,
1096 ir_node * upper_bound) {
1097 assert(array && (array->type_op == type_array));
1098 assert(lower_bound && "lower_bound node may not be NULL.");
1099 assert(upper_bound && "upper_bound node may not be NULL.");
1100 assert(dimension < array->attr.aa.n_dimensions && dimension >= 0);
1101 array->attr.aa.lower_bound[dimension] = lower_bound;
1102 array->attr.aa.upper_bound[dimension] = upper_bound;
1105 set_array_bounds_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_bounds (array, dimension,
1110 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
1111 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
1112 current_ir_graph = rem;
1115 set_array_lower_bound (type *array, int dimension, ir_node * lower_bound) {
1116 assert(array && (array->type_op == type_array));
1117 assert(lower_bound && "lower_bound node may not be NULL.");
1118 array->attr.aa.lower_bound[dimension] = lower_bound;
1120 void set_array_lower_bound_int (type *array, int dimension, int lower_bound) {
1121 ir_graph *rem = current_ir_graph;
1122 current_ir_graph = get_const_code_irg();
1123 set_array_lower_bound (array, dimension,
1124 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
1125 current_ir_graph = rem;
1128 set_array_upper_bound (type *array, int dimension, ir_node * upper_bound) {
1129 assert(array && (array->type_op == type_array));
1130 assert(upper_bound && "upper_bound node may not be NULL.");
1131 array->attr.aa.upper_bound[dimension] = upper_bound;
1133 void set_array_upper_bound_int (type *array, int dimension, int upper_bound) {
1134 ir_graph *rem = current_ir_graph;
1135 current_ir_graph = get_const_code_irg();
1136 set_array_upper_bound (array, dimension,
1137 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
1138 current_ir_graph = rem;
1140 int has_array_lower_bound (type *array, int dimension) {
1141 assert(array && (array->type_op == type_array));
1142 return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
1144 ir_node * get_array_lower_bound (type *array, int dimension) {
1145 assert(array && (array->type_op == type_array));
1146 return array->attr.aa.lower_bound[dimension];
1148 int has_array_upper_bound (type *array, int dimension) {
1149 assert(array && (array->type_op == type_array));
1150 return (get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown);
1152 ir_node * get_array_upper_bound (type *array, int dimension) {
1153 assert(array && (array->type_op == type_array));
1154 return array->attr.aa.upper_bound[dimension];
1157 void set_array_order (type *array, int dimension, int order) {
1158 assert(array && (array->type_op == type_array));
1159 array->attr.aa.order[dimension] = order;
1161 int get_array_order (type *array, int dimension) {
1162 assert(array && (array->type_op == type_array));
1163 return array->attr.aa.order[dimension];
1166 void set_array_element_type (type *array, type *tp) {
1167 assert(array && (array->type_op == type_array));
1168 assert(!is_method_type(tp));
1169 array->attr.aa.element_type = tp;
1171 type *get_array_element_type (type *array) {
1172 assert(array && (array->type_op == type_array));
1173 return array->attr.aa.element_type = skip_tid(array->attr.aa.element_type);
1176 void set_array_element_entity (type *array, entity *ent) {
1177 assert(array && (array->type_op == type_array));
1178 assert((get_entity_type(ent)->type_op != type_method));
1179 array->attr.aa.element_ent = ent;
1180 array->attr.aa.element_type = get_entity_type(ent);
1182 entity *get_array_element_entity (type *array) {
1183 assert(array && (array->type_op == type_array));
1184 return array->attr.aa.element_ent;
1188 bool is_array_type (type *array) {
1190 if (array->type_op == type_array) return 1; else return 0;
1193 /*******************************************************************/
1194 /** TYPE_ENUMERATION **/
1195 /*******************************************************************/
1197 /* create a new type enumeration -- set the enumerators independently */
1198 INLINE type *new_type_enumeration (ident *name, int n_enums) {
1201 res = new_type(type_enumeration, NULL, name);
1202 res->attr.ea.n_enums = n_enums;
1203 res->attr.ea.enumer = (tarval **) xmalloc (sizeof (tarval *) * n_enums);
1204 res->attr.ea.enum_nameid = (ident **) xmalloc (sizeof (ident *) * n_enums);
1205 for (i = 0; i < n_enums; i++) {
1206 res->attr.ea.enumer[i] = NULL;
1207 res->attr.ea.enum_nameid = NULL;
1211 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db) {
1212 type *res = new_type_enumeration (name, n_enums);
1213 set_type_dbg_info(res, db);
1217 INLINE void free_enumeration_attrs(type *enumeration) {
1218 assert(enumeration && (enumeration->type_op == type_enumeration));
1219 free(enumeration->attr.ea.enumer);
1220 free(enumeration->attr.ea.enum_nameid);
1223 /* manipulate fields of enumeration type. */
1224 int get_enumeration_n_enums (type *enumeration) {
1225 assert(enumeration && (enumeration->type_op == type_enumeration));
1226 return enumeration->attr.ea.n_enums;
1228 void set_enumeration_enum (type *enumeration, int pos, tarval *con) {
1229 assert(enumeration && (enumeration->type_op == type_enumeration));
1230 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1231 enumeration->attr.ea.enumer[pos] = con;
1233 tarval *get_enumeration_enum (type *enumeration, int pos) {
1234 assert(enumeration && (enumeration->type_op == type_enumeration));
1235 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1236 return enumeration->attr.ea.enumer[pos];
1238 void set_enumeration_nameid (type *enumeration, int pos, ident *id) {
1239 assert(enumeration && (enumeration->type_op == type_enumeration));
1240 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1241 enumeration->attr.ea.enum_nameid[pos] = id;
1243 ident *get_enumeration_nameid (type *enumeration, int pos) {
1244 assert(enumeration && (enumeration->type_op == type_enumeration));
1245 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1246 return enumeration->attr.ea.enum_nameid[pos];
1248 const char *get_enumeration_name(type *enumeration, int pos) {
1249 assert(enumeration && (enumeration->type_op == type_enumeration));
1250 assert(pos >= 0 && pos < get_enumeration_n_enums(enumeration));
1251 return get_id_str(enumeration->attr.ea.enum_nameid[pos]);
1255 bool is_enumeration_type (type *enumeration) {
1256 assert(enumeration);
1257 if (enumeration->type_op == type_enumeration) return 1; else return 0;
1260 /*******************************************************************/
1261 /** TYPE_POINTER **/
1262 /*******************************************************************/
1264 /* Create a new type pointer */
1265 INLINE type *new_type_pointer_mode (ident *name, type *points_to, ir_mode *ptr_mode) {
1267 assert(mode_is_reference(ptr_mode));
1268 res = new_type(type_pointer, ptr_mode, name);
1269 res->attr.pa.points_to = points_to;
1270 assert((get_mode_size_bytes(res->mode) != -1) && "unorthodox modes not implemented");
1271 res->size = get_mode_size_bytes(res->mode);
1272 res->state = layout_fixed;
1275 type *new_d_type_pointer (ident *name, type *points_to, ir_mode *ptr_mode, dbg_info* db) {
1276 type *res = new_type_pointer_mode (name, points_to, ptr_mode);
1277 set_type_dbg_info(res, db);
1280 INLINE void free_pointer_attrs (type *pointer) {
1281 assert(pointer && (pointer->type_op == type_pointer));
1283 /* manipulate fields of type_pointer */
1284 void set_pointer_points_to_type (type *pointer, type *tp) {
1285 assert(pointer && (pointer->type_op == type_pointer));
1286 pointer->attr.pa.points_to = tp;
1288 type *get_pointer_points_to_type (type *pointer) {
1289 assert(pointer && (pointer->type_op == type_pointer));
1290 return pointer->attr.pa.points_to = skip_tid(pointer->attr.pa.points_to);
1294 bool is_pointer_type (type *pointer) {
1296 if (pointer->type_op == type_pointer) return 1; else return 0;
1300 /*******************************************************************/
1301 /** TYPE_PRIMITIVE **/
1302 /*******************************************************************/
1304 /* create a new type primitive */
1305 INLINE type *new_type_primitive (ident *name, ir_mode *mode) {
1307 /* @@@ assert( mode_is_data(mode) && (!mode_is_reference(mode))); */
1308 res = new_type(type_primitive, mode, name);
1309 assert((get_mode_size_bytes(mode) != -1) && "unorthodox modes not implemented");
1310 res->size = get_mode_size_bytes(mode);
1311 res->state = layout_fixed;
1314 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db) {
1315 type *res = new_type_primitive (name, mode);
1316 set_type_dbg_info(res, db);
1319 INLINE void free_primitive_attrs (type *primitive) {
1320 assert(primitive && (primitive->type_op == type_primitive));
1324 bool is_primitive_type (type *primitive) {
1325 assert(primitive && primitive->kind == k_type);
1326 if (primitive->type_op == type_primitive) return 1; else return 0;
1329 /*******************************************************************/
1330 /** common functionality **/
1331 /*******************************************************************/
1334 INLINE int is_atomic_type(type *tp) {
1335 assert(tp && tp->kind == k_type);
1336 return (is_primitive_type(tp) || is_pointer_type(tp) ||
1337 is_enumeration_type(tp));
1341 * Gets the number of elements in a firm compound type.
1343 int get_compound_n_members(type *tp)
1347 if (is_struct_type(tp))
1348 res = get_struct_n_members(tp);
1349 else if (is_class_type(tp))
1350 res = get_class_n_members(tp);
1351 else if (is_union_type(tp))
1352 res = get_union_n_members(tp);
1354 assert(0 && "need struct, union or class for member count");
1360 * Gets the member of a firm compound type at position pos.
1362 entity *get_compound_member(type *tp, int pos)
1366 if (is_struct_type(tp))
1367 res = get_struct_member(tp, pos);
1368 else if (is_class_type(tp))
1369 res = get_class_member(tp, pos);
1370 else if (is_union_type(tp))
1371 res = get_union_member(tp, pos);
1374 assert(0 && "need struct, union or class to get a member");
1382 INLINE int is_compound_type(type *tp) {
1383 assert(tp && tp->kind == k_type);
1384 return (is_class_type(tp) || is_struct_type(tp) ||
1385 is_array_type(tp) || is_union_type(tp));