1 /*
2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
3 *
4 * This file is part of libFirm.
5 *
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.
10 *
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.
14 *
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
17 * PURPOSE.
18 */
19
20 /**
21 * @file type.c
22 * @brief Representation of types.
23 * @author Goetz Lindenmaier, Michael Beck
24 * @brief
25 *
26 * Implementation of the datastructure to hold
27 * type information.
28 *
29 * This module supplies a datastructure to represent all types
30 * known in the compiled program. This includes types specified
31 * in the program as well as types defined by the language. In the
32 * view of the intermediate representation there is no difference
33 * between these types.
34 *
35 * There exist several kinds of types, arranged by the structure of
36 * the type. A type is described by a set of attributes. Some of
37 * these attributes are common to all types, others depend on the
38 * kind of the type.
39 *
40 * Types are different from the modes defined in irmode: Types are
41 * on the level of the programming language, modes at the level of
42 * the target processor.
43 */
44 #include "config.h"
45
46 #include <string.h>
47 #include <stdlib.h>
48 #include <stddef.h>
49 #include <stdbool.h>
50
51 #include "type_t.h"
52
53 #include "xmalloc.h"
54 #include "irprog_t.h"
55 #include "ircons.h"
56 #include "tpop_t.h"
57 #include "tv_t.h"
58 #include "irhooks.h"
59 #include "util.h"
60 #include "entity_t.h"
61 #include "error.h"
62 #include "dbginfo.h"
63 #include "irprog_t.h"
64
65 #include "array.h"
66
67 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db);
68
get_none_type(void)69 ir_type *get_none_type(void)
70 {
71 return irp->none_type;
72 }
73
get_code_type(void)74 ir_type *get_code_type(void)
75 {
76 return irp->code_type;
77 }
78
get_unknown_type(void)79 ir_type *get_unknown_type(void)
80 {
81 return irp->unknown_type;
82 }
83
ir_init_type(ir_prog * irp)84 void ir_init_type(ir_prog *irp)
85 {
86 /* construct none and unknown type. */
87 irp->none_type = new_type(tpop_none, mode_BAD, NULL);
88 set_type_size_bytes(irp->none_type, 0);
89 set_type_state (irp->none_type, layout_fixed);
90
91 irp->code_type = new_type(tpop_code, mode_ANY, NULL);
92 set_type_state(irp->code_type, layout_fixed);
93
94 irp->unknown_type = new_type(tpop_unknown, mode_ANY, NULL);
95 set_type_size_bytes(irp->unknown_type, 0);
96 set_type_state (irp->unknown_type, layout_fixed);
97 }
98
ir_finish_type(ir_prog * irp)99 void ir_finish_type(ir_prog *irp)
100 {
101 /** nothing todo. (The none, code, unknown types are in the global type list
102 * and freed there */
103 (void)irp;
104 }
105
106 ir_visited_t firm_type_visited;
107
108 void (set_master_type_visited)(ir_visited_t val)
109 {
110 _set_master_type_visited(val);
111 }
112
ir_visited_t(get_master_type_visited)113 ir_visited_t (get_master_type_visited)(void)
114 {
115 return _get_master_type_visited();
116 }
117
118 void (inc_master_type_visited)(void)
119 {
120 _inc_master_type_visited();
121 }
122
123 /**
124 * Creates a new type representation:
125 *
126 * @param type_op the kind of this type. May not be type_id.
127 * @param mode the mode to be used for this type, may be NULL
128 * @param db debug info
129 *
130 * @return A new type of the given type. The remaining private attributes are
131 * not initialized. The type is in state layout_undefined.
132 */
new_type(tp_op const * type_op,ir_mode * mode,type_dbg_info * db)133 static ir_type *new_type(tp_op const *type_op, ir_mode *mode, type_dbg_info *db)
134 {
135 ir_type *res;
136 size_t node_size;
137
138 node_size = offsetof(ir_type, attr) + type_op->attr_size;
139 res = (ir_type*)xmalloc(node_size);
140 memset(res, 0, node_size);
141
142 res->kind = k_type;
143 res->type_op = type_op;
144 res->mode = mode;
145 res->visibility = ir_visibility_external;
146 res->flags = tf_none;
147 res->size = 0;
148 res->align = 0;
149 res->visit = 0;
150 res->link = NULL;
151 res->dbi = db;
152 #ifdef DEBUG_libfirm
153 res->nr = get_irp_new_node_nr();
154 #endif /* defined DEBUG_libfirm */
155
156 add_irp_type(res); /* Remember the new type global. */
157
158 return res;
159 }
160
free_type_entities(ir_type * tp)161 void free_type_entities(ir_type *tp)
162 {
163 const tp_op *op = get_type_tpop(tp);
164 if (op->ops.free_entities != NULL)
165 op->ops.free_entities(tp);
166 }
167
free_type_attrs(ir_type * tp)168 static void free_type_attrs(ir_type *tp)
169 {
170 const tp_op *tpop = get_type_tpop(tp);
171
172 if (tpop->ops.free_attrs)
173 tpop->ops.free_attrs(tp);
174 }
175
free_type(ir_type * tp)176 void free_type(ir_type *tp)
177 {
178 const tp_op *op = get_type_tpop(tp);
179
180 free_type_entities(tp);
181 /* Remove from list of all types */
182 remove_irp_type(tp);
183 /* Free the attributes of the type. */
184 free_type_attrs(tp);
185 /* Free entities automatically allocated with the ir_type */
186 if (op->ops.free_auto_entities)
187 op->ops.free_auto_entities(tp);
188 /* And now the type itself... */
189 #ifdef DEBUG_libfirm
190 tp->kind = k_BAD;
191 #endif
192 free(tp);
193 }
194
195 void *(get_type_link)(const ir_type *tp)
196 {
197 return _get_type_link(tp);
198 }
199
200 void (set_type_link)(ir_type *tp, void *l)
201 {
202 _set_type_link(tp, l);
203 }
204
205 const tp_op *(get_type_tpop)(const ir_type *tp)
206 {
207 return _get_type_tpop(tp);
208 }
209
210 ident *(get_type_tpop_nameid)(const ir_type *tp)
211 {
212 return _get_type_tpop_nameid(tp);
213 }
214
get_type_tpop_name(const ir_type * tp)215 const char* get_type_tpop_name(const ir_type *tp)
216 {
217 assert(tp && tp->kind == k_type);
218 return get_id_str(tp->type_op->name);
219 }
220
tp_opcode(get_type_tpop_code)221 tp_opcode (get_type_tpop_code)(const ir_type *tp)
222 {
223 return _get_type_tpop_code(tp);
224 }
225
226 ir_mode *(get_type_mode)(const ir_type *tp)
227 {
228 return _get_type_mode(tp);
229 }
230
set_type_mode(ir_type * tp,ir_mode * mode)231 void set_type_mode(ir_type *tp, ir_mode *mode)
232 {
233 const tp_op *tpop = get_type_tpop(tp);
234
235 if (tpop->ops.set_type_mode)
236 tpop->ops.set_type_mode(tp, mode);
237 else
238 assert(0 && "setting a mode is NOT allowed for this type");
239 }
240
get_type_nr(const ir_type * tp)241 long get_type_nr(const ir_type *tp)
242 {
243 assert(tp);
244 #ifdef DEBUG_libfirm
245 return tp->nr;
246 #else
247 return (long)PTR_TO_INT(tp);
248 #endif
249 }
250
251 unsigned (get_type_size_bytes)(const ir_type *tp)
252 {
253 return _get_type_size_bytes(tp);
254 }
255
get_type_visibility(const ir_type * tp)256 ir_visibility get_type_visibility(const ir_type *tp)
257 {
258 assert(is_type(tp));
259 return tp->visibility;
260 }
261
set_type_visibility(ir_type * tp,ir_visibility v)262 void set_type_visibility(ir_type *tp, ir_visibility v)
263 {
264 assert(is_type(tp));
265 tp->visibility = v;
266 }
267
set_type_size_bytes(ir_type * tp,unsigned size)268 void set_type_size_bytes(ir_type *tp, unsigned size)
269 {
270 const tp_op *tpop = get_type_tpop(tp);
271
272 if (tpop->ops.set_type_size)
273 tpop->ops.set_type_size(tp, size);
274 else
275 assert(0 && "Cannot set size for this type");
276 }
277
get_type_alignment_bytes(ir_type * tp)278 unsigned get_type_alignment_bytes(ir_type *tp)
279 {
280 unsigned align = 1;
281
282 if (tp->align > 0)
283 return tp->align;
284
285 /* alignment NOT set calculate it "on demand" */
286 if (tp->mode)
287 align = (get_mode_size_bits(tp->mode) + 7) >> 3;
288 else if (is_Array_type(tp))
289 align = get_type_alignment_bytes(get_array_element_type(tp));
290 else if (is_compound_type(tp)) {
291 size_t i, n = get_compound_n_members(tp);
292
293 align = 0;
294 for (i = 0; i < n; ++i) {
295 ir_type *t = get_entity_type(get_compound_member(tp, i));
296 unsigned a = get_type_alignment_bytes(t);
297
298 if (a > align)
299 align = a;
300 }
301 } else if (is_Method_type(tp)) {
302 align = 0;
303 }
304
305 /* write back */
306 tp->align = align;
307
308 return align;
309 }
310
set_type_alignment_bytes(ir_type * tp,unsigned align)311 void set_type_alignment_bytes(ir_type *tp, unsigned align)
312 {
313 assert(tp && tp->kind == k_type);
314 /* Methods don't have an alignment. */
315 if (tp->type_op != type_method) {
316 tp->align = align;
317 }
318 }
319
get_type_state_name(ir_type_state s)320 const char *get_type_state_name(ir_type_state s)
321 {
322 #define X(a) case a: return #a
323 switch (s) {
324 X(layout_undefined);
325 X(layout_fixed);
326 }
327 return "<unknown>";
328 #undef X
329 }
330
ir_type_state(get_type_state)331 ir_type_state (get_type_state)(const ir_type *tp)
332 {
333 return _get_type_state(tp);
334 }
335
set_type_state(ir_type * tp,ir_type_state state)336 void set_type_state(ir_type *tp, ir_type_state state)
337 {
338 assert(tp && tp->kind == k_type);
339
340 if ((tp->type_op == type_pointer) || (tp->type_op == type_primitive) ||
341 (tp->type_op == type_method))
342 return;
343
344 /* Just a correctness check: */
345 if (state == layout_fixed) {
346 size_t i;
347 switch (get_type_tpop_code(tp)) {
348 case tpo_class:
349 if (tp != get_glob_type()) {
350 size_t n_mem = get_class_n_members(tp);
351 for (i = 0; i < n_mem; i++) {
352 ir_entity *entity = get_class_member(tp, i);
353 if (is_Method_type(get_entity_type(entity)))
354 continue;
355 assert(get_entity_offset(entity) > -1);
356 }
357 }
358 break;
359 case tpo_struct:
360 for (i = 0; i < get_struct_n_members(tp); i++) {
361 assert(get_entity_offset(get_struct_member(tp, i)) > -1);
362 }
363 break;
364 case tpo_union:
365 break;
366 case tpo_array:
367 break;
368 case tpo_enumeration: {
369 #ifndef NDEBUG
370 size_t n_enums = get_enumeration_n_enums(tp);
371 assert(get_type_mode(tp) != NULL);
372 for (i = 0; i < n_enums; ++i) {
373 ir_enum_const *ec = get_enumeration_const(tp, i);
374 ir_tarval *tv = get_enumeration_value(ec);
375 assert(tv != NULL && tv != tarval_bad);
376 }
377 #endif
378 break;
379 }
380 default: break;
381 }
382 }
383 if (state == layout_fixed)
384 tp->flags |= tf_layout_fixed;
385 else
386 tp->flags &= ~tf_layout_fixed;
387 }
388
ir_visited_t(get_type_visited)389 ir_visited_t (get_type_visited)(const ir_type *tp)
390 {
391 return _get_type_visited(tp);
392 }
393
394 void (set_type_visited)(ir_type *tp, ir_visited_t num)
395 {
396 _set_type_visited(tp, num);
397 }
398
399 void (mark_type_visited)(ir_type *tp)
400 {
401 _mark_type_visited(tp);
402 }
403
404 int (type_visited)(const ir_type *tp)
405 {
406 return _type_visited(tp);
407 }
408
409 int (type_not_visited)(const ir_type *tp)
410 {
411 return _type_not_visited(tp);
412 }
413
414 type_dbg_info *(get_type_dbg_info)(const ir_type *tp)
415 {
416 return _get_type_dbg_info(tp);
417 }
418
419 void (set_type_dbg_info)(ir_type *tp, type_dbg_info *db)
420 {
421 _set_type_dbg_info(tp, db);
422 }
423
424 int (is_type)(const void *thing)
425 {
426 return _is_type(thing);
427 }
428
equal_type(ir_type * typ1,ir_type * typ2)429 int equal_type(ir_type *typ1, ir_type *typ2)
430 {
431 ir_entity **m;
432 ir_type **t;
433 size_t i;
434 size_t j;
435
436 if (typ1 == typ2) return 1;
437
438 if ((get_type_tpop_code(typ1) != get_type_tpop_code(typ2)) ||
439 typ1->name != typ2->name ||
440 (get_type_mode(typ1) != get_type_mode(typ2)) ||
441 (get_type_state(typ1) != get_type_state(typ2)))
442 return 0;
443 if ((get_type_state(typ1) == layout_fixed) &&
444 (get_type_size_bytes(typ1) != get_type_size_bytes(typ2)))
445 return 0;
446
447 switch (get_type_tpop_code(typ1)) {
448 case tpo_class:
449 if (get_class_n_members(typ1) != get_class_n_members(typ2)) return 0;
450 if (get_class_n_subtypes(typ1) != get_class_n_subtypes(typ2)) return 0;
451 if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
452 if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
453 /** Compare the members **/
454 m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
455 /* First sort the members of typ2 */
456 for (i = 0; i < get_class_n_members(typ1); i++) {
457 ir_entity *e1 = get_class_member(typ1, i);
458 for (j = 0; j < get_class_n_members(typ2); j++) {
459 ir_entity *e2 = get_class_member(typ2, j);
460 if (get_entity_name(e1) == get_entity_name(e2))
461 m[i] = e2;
462 }
463 }
464 for (i = 0; i < get_class_n_members(typ1); i++) {
465 if (!m[i] || get_class_member(typ1, i) != m[i])
466 return 0;
467 }
468 /** Compare the supertypes **/
469 t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
470 /* First sort the supertypes of typ2 */
471 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
472 ir_type *t1 = get_class_supertype(typ1, i);
473 for (j = 0; j < get_class_n_supertypes(typ2); j++) {
474 ir_type *t2 = get_class_supertype(typ2, j);
475 if (t2->name == t1->name)
476 t[i] = t2;
477 }
478 }
479 for (i = 0; i < get_class_n_supertypes(typ1); i++) {
480 if (!t[i] || /* Found no counterpart */
481 get_class_supertype(typ1, i) != t[i])
482 return 0;
483 }
484 break;
485
486 case tpo_struct:
487 if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
488 m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
489 /* First sort the members of lt */
490 for (i = 0; i < get_struct_n_members(typ1); i++) {
491 ir_entity *e1 = get_struct_member(typ1, i);
492 for (j = 0; j < get_struct_n_members(typ2); j++) {
493 ir_entity *e2 = get_struct_member(typ2, j);
494 if (get_entity_name(e1) == get_entity_name(e2))
495 m[i] = e2;
496 }
497 }
498 for (i = 0; i < get_struct_n_members(typ1); i++) {
499 if (!m[i] || get_struct_member(typ1, i) != m[i])
500 return 0;
501 }
502 break;
503
504 case tpo_method: {
505 size_t n_param1;
506 size_t n_param2;
507
508 if (get_method_variadicity(typ1) != get_method_variadicity(typ2)) return 0;
509 if (get_method_n_ress(typ1) != get_method_n_ress(typ2)) return 0;
510 if (get_method_calling_convention(typ1) !=
511 get_method_calling_convention(typ2)) return 0;
512
513 n_param1 = get_method_n_params(typ1);
514 n_param2 = get_method_n_params(typ2);
515
516 if (n_param1 != n_param2) return 0;
517
518 for (i = 0; i < n_param1; i++) {
519 if (!equal_type(get_method_param_type(typ1, i), get_method_param_type(typ2, i)))
520 return 0;
521 }
522 for (i = 0; i < get_method_n_ress(typ1); i++) {
523 if (!equal_type(get_method_res_type(typ1, i), get_method_res_type(typ2, i)))
524 return 0;
525 }
526 } break;
527
528 case tpo_union:
529 if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
530 m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
531 /* First sort the members of lt */
532 for (i = 0; i < get_union_n_members(typ1); i++) {
533 ir_entity *e1 = get_union_member(typ1, i);
534 for (j = 0; j < get_union_n_members(typ2); j++) {
535 ir_entity *e2 = get_union_member(typ2, j);
536 if (get_entity_name(e1) == get_entity_name(e2))
537 m[i] = e2;
538 }
539 }
540 for (i = 0; i < get_union_n_members(typ1); i++) {
541 if (!m[i] || get_union_member(typ1, i) != m[i])
542 return 0;
543 }
544 break;
545
546 case tpo_array:
547 if (get_array_n_dimensions(typ1) != get_array_n_dimensions(typ2))
548 return 0;
549 if (!equal_type(get_array_element_type(typ1), get_array_element_type(typ2)))
550 return 0;
551 for (i = 0; i < get_array_n_dimensions(typ1); i++) {
552 if (get_array_lower_bound(typ1, i) != get_array_lower_bound(typ2, i) ||
553 get_array_upper_bound(typ1, i) != get_array_upper_bound(typ2, i))
554 return 0;
555 if (get_array_order(typ1, i) != get_array_order(typ2, i))
556 assert(0 && "type compare with different dimension orders not implemented");
557 }
558 break;
559
560 case tpo_enumeration:
561 assert(0 && "enumerations not implemented");
562 break;
563
564 case tpo_pointer:
565 if (get_pointer_points_to_type(typ1) != get_pointer_points_to_type(typ2))
566 return 0;
567 break;
568
569 case tpo_primitive:
570 break;
571
572 default: break;
573 }
574 return 1;
575 }
576
smaller_type(ir_type * st,ir_type * lt)577 int smaller_type(ir_type *st, ir_type *lt)
578 {
579 ir_entity **m;
580 size_t i;
581 size_t j;
582 size_t n_st_members;
583
584 if (st == lt) return 1;
585
586 if (get_type_tpop_code(st) != get_type_tpop_code(lt))
587 return 0;
588
589 switch (get_type_tpop_code(st)) {
590 case tpo_class:
591 return is_SubClass_of(st, lt);
592
593 case tpo_struct:
594 n_st_members = get_struct_n_members(st);
595 if (n_st_members != get_struct_n_members(lt))
596 return 0;
597
598 m = ALLOCANZ(ir_entity*, n_st_members);
599 /* First sort the members of lt */
600 for (i = 0; i < n_st_members; ++i) {
601 ir_entity *se = get_struct_member(st, i);
602 size_t n = get_struct_n_members(lt);
603 for (j = 0; j < n; ++j) {
604 ir_entity *le = get_struct_member(lt, j);
605 if (get_entity_name(le) == get_entity_name(se))
606 m[i] = le;
607 }
608 }
609 for (i = 0; i < n_st_members; i++) {
610 if (!m[i] || /* Found no counterpart */
611 !smaller_type(get_entity_type(get_struct_member(st, i)), get_entity_type(m[i])))
612 return 0;
613 }
614 break;
615
616 case tpo_method: {
617 size_t n_param1, n_param2;
618
619 /** FIXME: is this still 1? */
620 if (get_method_variadicity(st) != get_method_variadicity(lt)) return 0;
621 if (get_method_n_ress(st) != get_method_n_ress(lt)) return 0;
622 if (get_method_calling_convention(st) !=
623 get_method_calling_convention(lt)) return 0;
624
625 n_param1 = get_method_n_params(st);
626 n_param2 = get_method_n_params(lt);
627
628 if (n_param1 != n_param2) return 0;
629
630 for (i = 0; i < get_method_n_params(st); i++) {
631 if (!smaller_type(get_method_param_type(st, i), get_method_param_type(lt, i)))
632 return 0;
633 }
634 for (i = 0; i < get_method_n_ress(st); i++) {
635 if (!smaller_type(get_method_res_type(st, i), get_method_res_type(lt, i)))
636 return 0;
637 }
638 } break;
639
640 case tpo_union:
641 n_st_members = get_union_n_members(st);
642 if (n_st_members != get_union_n_members(lt)) return 0;
643 m = ALLOCANZ(ir_entity*, n_st_members);
644 /* First sort the members of lt */
645 for (i = 0; i < n_st_members; ++i) {
646 ir_entity *se = get_union_member(st, i);
647 size_t n = get_union_n_members(lt);
648 for (j = 0; j < n; ++j) {
649 ir_entity *le = get_union_member(lt, j);
650 if (get_entity_name(le) == get_entity_name(se))
651 m[i] = le;
652 }
653 }
654 for (i = 0; i < n_st_members; ++i) {
655 if (!m[i] || /* Found no counterpart */
656 !smaller_type(get_entity_type(get_union_member(st, i)), get_entity_type(m[i])))
657 return 0;
658 }
659 break;
660
661 case tpo_array: {
662 ir_type *set, *let; /* small/large elt. ir_type */
663 if (get_array_n_dimensions(st) != get_array_n_dimensions(lt))
664 return 0;
665 set = get_array_element_type(st);
666 let = get_array_element_type(lt);
667 if (set != let) {
668 /* If the element types are different, set must be convertible
669 to let, and they must have the same size so that address
670 computations work out. To have a size the layout must
671 be fixed. */
672 if ((get_type_state(set) != layout_fixed) ||
673 (get_type_state(let) != layout_fixed))
674 return 0;
675 if (!smaller_type(set, let) ||
676 get_type_size_bytes(set) != get_type_size_bytes(let))
677 return 0;
678 }
679 for (i = 0; i < get_array_n_dimensions(st); i++) {
680 if (get_array_lower_bound(lt, i))
681 if (get_array_lower_bound(st, i) != get_array_lower_bound(lt, i))
682 return 0;
683 if (get_array_upper_bound(lt, i))
684 if (get_array_upper_bound(st, i) != get_array_upper_bound(lt, i))
685 return 0;
686 }
687 } break;
688
689 case tpo_enumeration:
690 assert(0 && "enumerations not implemented");
691 break;
692
693 case tpo_pointer:
694 if (!smaller_type(get_pointer_points_to_type(st), get_pointer_points_to_type(lt)))
695 return 0;
696 break;
697
698 case tpo_primitive:
699 if (!smaller_mode(get_type_mode(st), get_type_mode(lt)))
700 return 0;
701 break;
702
703 default: break;
704 }
705 return 1;
706 }
707
708
new_d_type_class(ident * name,type_dbg_info * db)709 ir_type *new_d_type_class(ident *name, type_dbg_info *db)
710 {
711 ir_type *res;
712
713 res = new_type(type_class, NULL, db);
714 res->name = name;
715
716 res->attr.ca.members = NEW_ARR_F (ir_entity *, 0);
717 res->attr.ca.subtypes = NEW_ARR_F (ir_type *, 0);
718 res->attr.ca.supertypes = NEW_ARR_F (ir_type *, 0);
719 res->attr.ca.peculiarity = peculiarity_existent;
720 res->attr.ca.type_info = NULL;
721 res->attr.ca.vtable_size = 0;
722 res->attr.ca.clss_flags = cf_none;
723 res->attr.ca.dfn = 0;
724 hook_new_type(res);
725 return res;
726 }
727
new_type_class(ident * name)728 ir_type *new_type_class(ident *name)
729 {
730 return new_d_type_class(name, NULL);
731 }
732
free_class_entities(ir_type * clss)733 void free_class_entities(ir_type *clss)
734 {
735 size_t i;
736 assert(clss && (clss->type_op == type_class));
737 /* we must iterate backward here */
738 for (i = get_class_n_members(clss); i > 0;)
739 free_entity(get_class_member(clss, --i));
740 /* do NOT free the type info here. It belongs to another class */
741 }
742
free_class_attrs(ir_type * clss)743 void free_class_attrs(ir_type *clss)
744 {
745 assert(clss && (clss->type_op == type_class));
746 DEL_ARR_F(clss->attr.ca.members);
747 DEL_ARR_F(clss->attr.ca.subtypes);
748 DEL_ARR_F(clss->attr.ca.supertypes);
749 }
750
get_class_ident(const ir_type * clss)751 ident *get_class_ident(const ir_type *clss)
752 {
753 assert(clss->type_op == type_class);
754 return clss->name;
755 }
756
get_class_name(const ir_type * clss)757 const char *get_class_name(const ir_type *clss)
758 {
759 if (get_class_ident(clss) == NULL)
760 return NULL;
761 return get_id_str(get_class_ident(clss));
762 }
763
add_class_member(ir_type * clss,ir_entity * member)764 static void add_class_member(ir_type *clss, ir_entity *member)
765 {
766 assert(clss && (clss->type_op == type_class));
767 assert(clss != get_entity_type(member) && "recursive type");
768 ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
769 }
770
size_t(get_class_n_members)771 size_t (get_class_n_members)(const ir_type *clss)
772 {
773 return _get_class_n_members(clss);
774 }
775
get_class_member_index(const ir_type * clss,ir_entity * mem)776 size_t get_class_member_index(const ir_type *clss, ir_entity *mem)
777 {
778 size_t i, n;
779 assert(clss && (clss->type_op == type_class));
780 for (i = 0, n = get_class_n_members(clss); i < n; ++i) {
781 if (get_class_member(clss, i) == mem)
782 return i;
783 }
784 return INVALID_MEMBER_INDEX;
785 }
786
787 ir_entity *(get_class_member)(const ir_type *clss, size_t pos)
788 {
789 return _get_class_member(clss, pos);
790 }
791
get_class_member_by_name(ir_type * clss,ident * name)792 ir_entity *get_class_member_by_name(ir_type *clss, ident *name)
793 {
794 size_t i, n_mem;
795 assert(clss && (clss->type_op == type_class));
796 n_mem = get_class_n_members(clss);
797 for (i = 0; i < n_mem; ++i) {
798 ir_entity *mem = get_class_member(clss, i);
799 if (get_entity_ident(mem) == name)
800 return mem;
801 }
802 return NULL;
803 }
804
remove_class_member(ir_type * clss,ir_entity * member)805 static void remove_class_member(ir_type *clss, ir_entity *member)
806 {
807 size_t i;
808 assert(clss && (clss->type_op == type_class));
809 for (i = 0; i < ARR_LEN(clss->attr.ca.members); ++i) {
810 if (clss->attr.ca.members[i] == member) {
811 for (; i < ARR_LEN(clss->attr.ca.members) - 1; ++i)
812 clss->attr.ca.members[i] = clss->attr.ca.members[i + 1];
813 ARR_SETLEN(ir_entity*, clss->attr.ca.members, ARR_LEN(clss->attr.ca.members) - 1);
814 break;
815 }
816 }
817 }
818
add_class_subtype(ir_type * clss,ir_type * subtype)819 void add_class_subtype(ir_type *clss, ir_type *subtype)
820 {
821 size_t i;
822 assert(clss->type_op == type_class);
823 ARR_APP1 (ir_type *, clss->attr.ca.subtypes, subtype);
824 for (i = 0; i < get_class_n_supertypes(subtype); i++) {
825 if (get_class_supertype(subtype, i) == clss)
826 /* Class already registered */
827 return;
828 }
829 ARR_APP1(ir_type *, subtype->attr.ca.supertypes, clss);
830 }
831
get_class_n_subtypes(const ir_type * clss)832 size_t get_class_n_subtypes(const ir_type *clss)
833 {
834 assert(clss->type_op == type_class);
835 return ARR_LEN (clss->attr.ca.subtypes);
836 }
837
get_class_subtype(ir_type * clss,size_t pos)838 ir_type *get_class_subtype(ir_type *clss, size_t pos)
839 {
840 assert(clss->type_op == type_class);
841 assert(pos < get_class_n_subtypes(clss));
842 return clss->attr.ca.subtypes[pos];
843 }
844
get_class_subtype_index(ir_type * clss,const ir_type * subclass)845 size_t get_class_subtype_index(ir_type *clss, const ir_type *subclass)
846 {
847 size_t i, n_subtypes = get_class_n_subtypes(clss);
848 assert(is_Class_type(subclass));
849 for (i = 0; i < n_subtypes; ++i) {
850 if (get_class_subtype(clss, i) == subclass)
851 return i;
852 }
853 return (size_t)-1;
854 }
855
set_class_subtype(ir_type * clss,ir_type * subtype,size_t pos)856 void set_class_subtype(ir_type *clss, ir_type *subtype, size_t pos)
857 {
858 assert(clss->type_op == type_class);
859 assert(pos < get_class_n_subtypes(clss));
860 clss->attr.ca.subtypes[pos] = subtype;
861 }
862
remove_class_subtype(ir_type * clss,ir_type * subtype)863 void remove_class_subtype(ir_type *clss, ir_type *subtype)
864 {
865 size_t i;
866 assert(clss && (clss->type_op == type_class));
867 for (i = 0; i < ARR_LEN(clss->attr.ca.subtypes); ++i) {
868 if (clss->attr.ca.subtypes[i] == subtype) {
869 for (; i < ARR_LEN(clss->attr.ca.subtypes) - 1; ++i)
870 clss->attr.ca.subtypes[i] = clss->attr.ca.subtypes[i+1];
871 ARR_SETLEN(ir_type*, clss->attr.ca.subtypes, ARR_LEN(clss->attr.ca.subtypes) - 1);
872 break;
873 }
874 }
875 }
876
add_class_supertype(ir_type * clss,ir_type * supertype)877 void add_class_supertype(ir_type *clss, ir_type *supertype)
878 {
879 size_t i;
880 size_t n;
881 assert(clss && (clss->type_op == type_class));
882 assert(supertype && (supertype -> type_op == type_class));
883 ARR_APP1 (ir_type *, clss->attr.ca.supertypes, supertype);
884 for (i = 0, n = get_class_n_subtypes(supertype); i < n; ++i) {
885 if (get_class_subtype(supertype, i) == clss)
886 /* Class already registered */
887 return;
888 }
889 ARR_APP1(ir_type *, supertype->attr.ca.subtypes, clss);
890 }
891
get_class_n_supertypes(const ir_type * clss)892 size_t get_class_n_supertypes(const ir_type *clss)
893 {
894 assert(clss->type_op == type_class);
895 return ARR_LEN(clss->attr.ca.supertypes);
896 }
897
get_class_supertype_index(ir_type * clss,ir_type * super_clss)898 size_t get_class_supertype_index(ir_type *clss, ir_type *super_clss)
899 {
900 size_t i, n_supertypes = get_class_n_supertypes(clss);
901 assert(super_clss && (super_clss->type_op == type_class));
902 for (i = 0; i < n_supertypes; i++) {
903 if (get_class_supertype(clss, i) == super_clss)
904 return i;
905 }
906 return (size_t)-1;
907 }
908
get_class_supertype(ir_type * clss,size_t pos)909 ir_type *get_class_supertype(ir_type *clss, size_t pos)
910 {
911 assert(clss->type_op == type_class);
912 assert(pos < get_class_n_supertypes(clss));
913 return clss->attr.ca.supertypes[pos];
914 }
915
set_class_supertype(ir_type * clss,ir_type * supertype,size_t pos)916 void set_class_supertype(ir_type *clss, ir_type *supertype, size_t pos)
917 {
918 assert(clss->type_op == type_class);
919 assert(pos < get_class_n_supertypes(clss));
920 clss->attr.ca.supertypes[pos] = supertype;
921 }
922
remove_class_supertype(ir_type * clss,ir_type * supertype)923 void remove_class_supertype(ir_type *clss, ir_type *supertype)
924 {
925 size_t i;
926 assert(clss && (clss->type_op == type_class));
927 for (i = 0; i < ARR_LEN(clss->attr.ca.supertypes); ++i) {
928 if (clss->attr.ca.supertypes[i] == supertype) {
929 for (; i < ARR_LEN(clss->attr.ca.supertypes) - 1; ++i)
930 clss->attr.ca.supertypes[i] = clss->attr.ca.supertypes[i+1];
931 ARR_SETLEN(ir_type*, clss->attr.ca.supertypes, ARR_LEN(clss->attr.ca.supertypes) - 1);
932 break;
933 }
934 }
935 }
936
get_class_type_info(const ir_type * clss)937 ir_entity *get_class_type_info(const ir_type *clss)
938 {
939 return clss->attr.ca.type_info;
940 }
941
set_class_type_info(ir_type * clss,ir_entity * ent)942 void set_class_type_info(ir_type *clss, ir_entity *ent)
943 {
944 clss->attr.ca.type_info = ent;
945 if (ent)
946 ent->repr_class = clss;
947 }
948
get_class_peculiarity(const ir_type * clss)949 ir_peculiarity get_class_peculiarity(const ir_type *clss)
950 {
951 assert(clss && (clss->type_op == type_class));
952 return clss->attr.ca.peculiarity;
953 }
954
set_class_peculiarity(ir_type * clss,ir_peculiarity pec)955 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec)
956 {
957 assert(clss && (clss->type_op == type_class));
958 assert(pec != peculiarity_inherited); /* There is no inheritance of types in libFirm. */
959 clss->attr.ca.peculiarity = pec;
960 }
961
962 unsigned (get_class_vtable_size)(const ir_type *clss)
963 {
964 return _get_class_vtable_size(clss);
965 }
966
967 void (set_class_vtable_size)(ir_type *clss, unsigned size)
968 {
969 _set_class_vtable_size(clss, size);
970 }
971
972 int (is_class_final)(const ir_type *clss)
973 {
974 return _is_class_final(clss);
975 }
976
977 void (set_class_final)(ir_type *clss, int flag)
978 {
979 _set_class_final(clss, flag);
980 }
981
982 int (is_class_interface)(const ir_type *clss)
983 {
984 return _is_class_interface(clss);
985 }
986
987 void (set_class_interface)(ir_type *clss, int flag)
988 {
989 _set_class_interface(clss, flag);
990 }
991
992 int (is_class_abstract)(const ir_type *clss)
993 {
994 return _is_class_abstract(clss);
995 }
996
997 void (set_class_abstract)(ir_type *clss, int final)
998 {
999 _set_class_abstract(clss, final);
1000 }
1001
set_class_dfn(ir_type * clss,int dfn)1002 void set_class_dfn(ir_type *clss, int dfn)
1003 {
1004 clss->attr.ca.dfn = dfn;
1005 }
1006
get_class_dfn(const ir_type * clss)1007 int get_class_dfn(const ir_type *clss)
1008 {
1009 return (clss->attr.ca.dfn);
1010 }
1011
1012 int (is_Class_type)(const ir_type *clss)
1013 {
1014 return _is_class_type(clss);
1015 }
1016
set_class_mode(ir_type * tp,ir_mode * mode)1017 void set_class_mode(ir_type *tp, ir_mode *mode)
1018 {
1019 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1020 assert(get_type_state(tp) == layout_fixed &&
1021 tp->size == get_mode_size_bytes(mode) && "mode don't match class layout");
1022 tp->mode = mode;
1023 }
1024
set_class_size(ir_type * tp,unsigned size)1025 void set_class_size(ir_type *tp, unsigned size)
1026 {
1027 tp->size = size;
1028 }
1029
1030
new_d_type_struct(ident * name,type_dbg_info * db)1031 ir_type *new_d_type_struct(ident *name, type_dbg_info *db)
1032 {
1033 ir_type *res = new_type(type_struct, NULL, db);
1034 res->name = name;
1035
1036 res->attr.sa.members = NEW_ARR_F(ir_entity *, 0);
1037 hook_new_type(res);
1038 return res;
1039 }
1040
new_type_struct(ident * name)1041 ir_type *new_type_struct(ident *name)
1042 {
1043 return new_d_type_struct (name, NULL);
1044 }
1045
free_struct_entities(ir_type * strct)1046 void free_struct_entities(ir_type *strct)
1047 {
1048 size_t i;
1049 assert(strct && (strct->type_op == type_struct));
1050 /* we must iterate backward here */
1051 for (i = get_struct_n_members(strct); i > 0;)
1052 free_entity(get_struct_member(strct, --i));
1053 }
1054
free_struct_attrs(ir_type * strct)1055 void free_struct_attrs(ir_type *strct)
1056 {
1057 assert(strct && (strct->type_op == type_struct));
1058 DEL_ARR_F(strct->attr.sa.members);
1059 }
1060
get_struct_ident(const ir_type * strct)1061 ident *get_struct_ident(const ir_type *strct)
1062 {
1063 assert(strct->type_op == type_struct);
1064 return strct->name;
1065 }
1066
get_struct_name(const ir_type * strct)1067 const char *get_struct_name(const ir_type *strct)
1068 {
1069 if (get_struct_ident(strct) == NULL)
1070 return NULL;
1071 return get_id_str(get_struct_ident(strct));
1072 }
1073
get_struct_n_members(const ir_type * strct)1074 size_t get_struct_n_members(const ir_type *strct)
1075 {
1076 assert(strct->type_op == type_struct);
1077 return ARR_LEN(strct->attr.sa.members);
1078 }
1079
add_struct_member(ir_type * strct,ir_entity * member)1080 static void add_struct_member(ir_type *strct, ir_entity *member)
1081 {
1082 assert(strct && (strct->type_op == type_struct));
1083 assert(get_type_tpop(get_entity_type(member)) != type_method);
1084 assert(strct != get_entity_type(member) && "recursive type");
1085 ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
1086 }
1087
get_struct_member(const ir_type * strct,size_t pos)1088 ir_entity *get_struct_member(const ir_type *strct, size_t pos)
1089 {
1090 assert(strct && (strct->type_op == type_struct));
1091 assert(pos < get_struct_n_members(strct));
1092 return strct->attr.sa.members[pos];
1093 }
1094
get_struct_member_index(const ir_type * strct,ir_entity * mem)1095 size_t get_struct_member_index(const ir_type *strct, ir_entity *mem)
1096 {
1097 size_t i, n;
1098 assert(strct && (strct->type_op == type_struct));
1099 for (i = 0, n = get_struct_n_members(strct); i < n; ++i) {
1100 if (get_struct_member(strct, i) == mem)
1101 return i;
1102 }
1103 return (size_t)-1;
1104 }
1105
remove_struct_member(ir_type * strct,ir_entity * member)1106 static void remove_struct_member(ir_type *strct, ir_entity *member)
1107 {
1108 size_t i;
1109 assert(strct && (strct->type_op == type_struct));
1110 for (i = 0; i < ARR_LEN(strct->attr.sa.members); ++i) {
1111 if (strct->attr.sa.members[i] == member) {
1112 for (; i < ARR_LEN(strct->attr.sa.members) - 1; ++i)
1113 strct->attr.sa.members[i] = strct->attr.sa.members[i+1];
1114 ARR_SETLEN(ir_entity*, strct->attr.sa.members, ARR_LEN(strct->attr.sa.members) - 1);
1115 break;
1116 }
1117 }
1118 }
1119
1120 int (is_Struct_type)(const ir_type *strct)
1121 {
1122 return _is_struct_type(strct);
1123 }
1124
set_struct_mode(ir_type * tp,ir_mode * mode)1125 void set_struct_mode(ir_type *tp, ir_mode *mode)
1126 {
1127 /* for classes and structs we allow to set a mode if the layout is fixed AND the size matches */
1128 assert(get_type_state(tp) == layout_fixed &&
1129 tp->size == get_mode_size_bytes(mode) && "mode don't match struct layout");
1130 tp->mode = mode;
1131 }
1132
set_struct_size(ir_type * tp,unsigned size)1133 void set_struct_size(ir_type *tp, unsigned size)
1134 {
1135 tp->size = size;
1136 }
1137
new_d_type_method(size_t n_param,size_t n_res,type_dbg_info * db)1138 ir_type *new_d_type_method(size_t n_param, size_t n_res, type_dbg_info *db)
1139 {
1140 ir_type *res;
1141
1142 assert((get_mode_size_bits(mode_P_code) % 8 == 0) && "unorthodox modes not implemented");
1143 res = new_type(type_method, mode_P_code, db);
1144 res->flags |= tf_layout_fixed;
1145 res->size = get_mode_size_bytes(mode_P_code);
1146 res->attr.ma.n_params = n_param;
1147 res->attr.ma.params = XMALLOCNZ(tp_ent_pair, n_param);
1148 res->attr.ma.n_res = n_res;
1149 res->attr.ma.res_type = XMALLOCNZ(tp_ent_pair, n_res);
1150 res->attr.ma.variadicity = variadicity_non_variadic;
1151 res->attr.ma.properties = mtp_no_property;
1152 hook_new_type(res);
1153 return res;
1154 }
1155
new_type_method(size_t n_param,size_t n_res)1156 ir_type *new_type_method(size_t n_param, size_t n_res)
1157 {
1158 return new_d_type_method(n_param, n_res, NULL);
1159 }
1160
clone_type_method(ir_type * tp)1161 ir_type *clone_type_method(ir_type *tp)
1162 {
1163 ir_type *res;
1164 ir_mode *mode;
1165 size_t n_params;
1166 size_t n_res;
1167 type_dbg_info *db;
1168
1169 assert(is_Method_type(tp));
1170
1171 mode = tp->mode;
1172 n_params = tp->attr.ma.n_params;
1173 n_res = tp->attr.ma.n_res;
1174 db = tp->dbi;
1175
1176 res = new_type(type_method, mode, db);
1177
1178 res->flags = tp->flags;
1179 res->higher_type = tp->higher_type;
1180 res->size = tp->size;
1181 res->attr.ma.n_params = n_params;
1182 res->attr.ma.params = XMALLOCN(tp_ent_pair, n_params);
1183 memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
1184 res->attr.ma.n_res = n_res;
1185 res->attr.ma.res_type = XMALLOCN(tp_ent_pair, n_res);
1186 memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
1187 res->attr.ma.variadicity = tp->attr.ma.variadicity;
1188 res->attr.ma.properties = tp->attr.ma.properties;
1189 res->attr.ma.irg_calling_conv = tp->attr.ma.irg_calling_conv;
1190 hook_new_type(res);
1191 return res;
1192 }
1193
free_method_entities(ir_type * method)1194 void free_method_entities(ir_type *method)
1195 {
1196 (void) method;
1197 assert(method && (method->type_op == type_method));
1198 }
1199
free_method_attrs(ir_type * method)1200 void free_method_attrs(ir_type *method)
1201 {
1202 assert(method && (method->type_op == type_method));
1203 free(method->attr.ma.params);
1204 free(method->attr.ma.res_type);
1205 }
1206
size_t(get_method_n_params)1207 size_t (get_method_n_params)(const ir_type *method)
1208 {
1209 return _get_method_n_params(method);
1210 }
1211
get_method_param_type(const ir_type * method,size_t pos)1212 ir_type *get_method_param_type(const ir_type *method, size_t pos)
1213 {
1214 ir_type *res;
1215 assert(method->type_op == type_method);
1216 assert(pos < get_method_n_params(method));
1217 res = method->attr.ma.params[pos].tp;
1218 assert(res != NULL && "empty method param type");
1219 return res;
1220 }
1221
set_method_param_type(ir_type * method,size_t pos,ir_type * tp)1222 void set_method_param_type(ir_type *method, size_t pos, ir_type *tp)
1223 {
1224 assert(method->type_op == type_method);
1225 assert(pos < get_method_n_params(method));
1226 method->attr.ma.params[pos].tp = tp;
1227 }
1228
size_t(get_method_n_ress)1229 size_t (get_method_n_ress)(const ir_type *method)
1230 {
1231 return _get_method_n_ress(method);
1232 }
1233
get_method_res_type(const ir_type * method,size_t pos)1234 ir_type *get_method_res_type(const ir_type *method, size_t pos)
1235 {
1236 ir_type *res;
1237 assert(method->type_op == type_method);
1238 assert(pos < get_method_n_ress(method));
1239 res = method->attr.ma.res_type[pos].tp;
1240 assert(res != NULL && "empty method return type");
1241 return res;
1242 }
1243
set_method_res_type(ir_type * method,size_t pos,ir_type * tp)1244 void set_method_res_type(ir_type *method, size_t pos, ir_type *tp)
1245 {
1246 assert(method->type_op == type_method);
1247 assert(pos < get_method_n_ress(method));
1248 /* set the result ir_type */
1249 method->attr.ma.res_type[pos].tp = tp;
1250 /* If information constructed set pass-by-value representation. */
1251 }
1252
get_variadicity_name(ir_variadicity vari)1253 const char *get_variadicity_name(ir_variadicity vari)
1254 {
1255 #define X(a) case a: return #a
1256 switch (vari) {
1257 X(variadicity_non_variadic);
1258 X(variadicity_variadic);
1259 default:
1260 return "BAD VALUE";
1261 }
1262 #undef X
1263 }
1264
get_method_variadicity(const ir_type * method)1265 ir_variadicity get_method_variadicity(const ir_type *method)
1266 {
1267 assert(method && (method->type_op == type_method));
1268 return method->attr.ma.variadicity;
1269 }
1270
set_method_variadicity(ir_type * method,ir_variadicity vari)1271 void set_method_variadicity(ir_type *method, ir_variadicity vari)
1272 {
1273 assert(method && (method->type_op == type_method));
1274 method->attr.ma.variadicity = vari;
1275 }
1276
mtp_additional_properties(get_method_additional_properties)1277 mtp_additional_properties (get_method_additional_properties)(const ir_type *method)
1278 {
1279 return _get_method_additional_properties(method);
1280 }
1281
1282 void (set_method_additional_properties)(ir_type *method, mtp_additional_properties mask)
1283 {
1284 _set_method_additional_properties(method, mask);
1285 }
1286
1287 void (add_method_additional_properties)(ir_type *method,
1288 mtp_additional_properties flag)
1289 {
1290 _add_method_additional_properties(method, flag);
1291 }
1292
1293 unsigned (get_method_calling_convention)(const ir_type *method)
1294 {
1295 return _get_method_calling_convention(method);
1296 }
1297
1298 void (set_method_calling_convention)(ir_type *method, unsigned cc_mask)
1299 {
1300 _set_method_calling_convention(method, cc_mask);
1301 }
1302
get_method_n_regparams(ir_type * method)1303 unsigned get_method_n_regparams(ir_type *method)
1304 {
1305 unsigned cc = get_method_calling_convention(method);
1306 assert(IS_FASTCALL(cc));
1307
1308 return cc & ~cc_bits;
1309 }
1310
set_method_n_regparams(ir_type * method,unsigned n_regs)1311 void set_method_n_regparams(ir_type *method, unsigned n_regs)
1312 {
1313 unsigned cc = get_method_calling_convention(method);
1314 assert(IS_FASTCALL(cc));
1315
1316 set_method_calling_convention(method, (cc & cc_bits) | (n_regs & ~cc_bits));
1317 }
1318
1319 int (is_Method_type)(const ir_type *method)
1320 {
1321 return _is_method_type(method);
1322 }
1323
1324
new_d_type_union(ident * name,type_dbg_info * db)1325 ir_type *new_d_type_union(ident *name, type_dbg_info *db)
1326 {
1327 ir_type *res = new_type(type_union, NULL, db);
1328 res->name = name;
1329
1330 res->attr.ua.members = NEW_ARR_F(ir_entity *, 0);
1331 hook_new_type(res);
1332 return res;
1333 }
1334
new_type_union(ident * name)1335 ir_type *new_type_union(ident *name)
1336 {
1337 return new_d_type_union(name, NULL);
1338 }
1339
free_union_entities(ir_type * uni)1340 void free_union_entities(ir_type *uni)
1341 {
1342 size_t i;
1343 assert(uni && (uni->type_op == type_union));
1344 /* we must iterate backward here */
1345 for (i = get_union_n_members(uni); i > 0;)
1346 free_entity(get_union_member(uni, --i));
1347 }
1348
free_union_attrs(ir_type * uni)1349 void free_union_attrs(ir_type *uni)
1350 {
1351 assert(uni && (uni->type_op == type_union));
1352 DEL_ARR_F(uni->attr.ua.members);
1353 }
1354
get_union_ident(const ir_type * uni)1355 ident *get_union_ident(const ir_type *uni)
1356 {
1357 assert(uni->type_op == type_union);
1358 return uni->name;
1359 }
1360
get_union_name(const ir_type * uni)1361 const char *get_union_name(const ir_type *uni)
1362 {
1363 if (get_union_ident(uni) == NULL)
1364 return NULL;
1365 return get_id_str(get_union_ident(uni));
1366 }
1367
get_union_n_members(const ir_type * uni)1368 size_t get_union_n_members(const ir_type *uni)
1369 {
1370 assert(uni->type_op == type_union);
1371 return ARR_LEN(uni->attr.ua.members);
1372 }
1373
add_union_member(ir_type * uni,ir_entity * member)1374 static void add_union_member(ir_type *uni, ir_entity *member)
1375 {
1376 assert(uni->type_op == type_union);
1377 assert(uni != get_entity_type(member) && "recursive type");
1378 ARR_APP1(ir_entity *, uni->attr.ua.members, member);
1379 }
1380
get_union_member(const ir_type * uni,size_t pos)1381 ir_entity *get_union_member(const ir_type *uni, size_t pos)
1382 {
1383 assert(uni->type_op == type_union);
1384 assert(pos < get_union_n_members(uni));
1385 return uni->attr.ua.members[pos];
1386 }
1387
get_union_member_index(const ir_type * uni,ir_entity * mem)1388 size_t get_union_member_index(const ir_type *uni, ir_entity *mem)
1389 {
1390 size_t i, n;
1391 assert(uni && (uni->type_op == type_union));
1392 for (i = 0, n = get_union_n_members(uni); i < n; ++i) {
1393 if (get_union_member(uni, i) == mem)
1394 return i;
1395 }
1396 return (size_t)-1;
1397 }
1398
remove_union_member(ir_type * uni,ir_entity * member)1399 static void remove_union_member(ir_type *uni, ir_entity *member)
1400 {
1401 size_t i;
1402 assert(uni && (uni->type_op == type_union));
1403 for (i = 0; i < ARR_LEN(uni->attr.ua.members); ++i) {
1404 if (uni->attr.ua.members[i] == member) {
1405 for (; i < ARR_LEN(uni->attr.ua.members) - 1; i++)
1406 uni->attr.ua.members[i] = uni->attr.ua.members[i+1];
1407 ARR_SETLEN(ir_entity*, uni->attr.ua.members, ARR_LEN(uni->attr.ua.members) - 1);
1408 break;
1409 }
1410 }
1411 }
1412
1413 int (is_Union_type)(const ir_type *uni)
1414 {
1415 return _is_union_type(uni);
1416 }
1417
set_union_size(ir_type * tp,unsigned size)1418 void set_union_size(ir_type *tp, unsigned size)
1419 {
1420 tp->size = size;
1421 }
1422
1423
1424
new_d_type_array(size_t n_dimensions,ir_type * element_type,type_dbg_info * db)1425 ir_type *new_d_type_array(size_t n_dimensions, ir_type *element_type,
1426 type_dbg_info *db)
1427 {
1428 ir_type *res;
1429 size_t i;
1430 ir_node *unk;
1431 ir_graph *irg = get_const_code_irg();
1432
1433 assert(!is_Method_type(element_type));
1434
1435 res = new_type(type_array, NULL, db);
1436 res->attr.aa.n_dimensions = n_dimensions;
1437 res->attr.aa.lower_bound = XMALLOCNZ(ir_node*, n_dimensions);
1438 res->attr.aa.upper_bound = XMALLOCNZ(ir_node*, n_dimensions);
1439 res->attr.aa.order = XMALLOCNZ(size_t, n_dimensions);
1440
1441 unk = new_r_Unknown(irg, mode_Iu);
1442 for (i = 0; i < n_dimensions; i++) {
1443 res->attr.aa.lower_bound[i] =
1444 res->attr.aa.upper_bound[i] = unk;
1445 res->attr.aa.order[i] = i;
1446 }
1447
1448 res->attr.aa.element_type = element_type;
1449 res->attr.aa.element_ent
1450 = new_entity(NULL, new_id_from_chars("elem_ent", 8), element_type);
1451 res->attr.aa.element_ent->owner = res;
1452
1453 hook_new_type(res);
1454 return res;
1455 }
1456
new_type_array(size_t n_dimensions,ir_type * element_type)1457 ir_type *new_type_array(size_t n_dimensions, ir_type *element_type)
1458 {
1459 return new_d_type_array(n_dimensions, element_type, NULL);
1460 }
1461
free_array_automatic_entities(ir_type * array)1462 void free_array_automatic_entities(ir_type *array)
1463 {
1464 assert(array->type_op == type_array);
1465 free_entity(get_array_element_entity(array));
1466 }
1467
free_array_entities(ir_type * array)1468 void free_array_entities(ir_type *array)
1469 {
1470 (void) array;
1471 assert(array->type_op == type_array);
1472 }
1473
free_array_attrs(ir_type * array)1474 void free_array_attrs(ir_type *array)
1475 {
1476 assert(array->type_op == type_array);
1477 free(array->attr.aa.lower_bound);
1478 free(array->attr.aa.upper_bound);
1479 free(array->attr.aa.order);
1480 }
1481
get_array_n_dimensions(const ir_type * array)1482 size_t get_array_n_dimensions(const ir_type *array)
1483 {
1484 assert(array->type_op == type_array);
1485 return array->attr.aa.n_dimensions;
1486 }
1487
set_array_bounds(ir_type * array,size_t dimension,ir_node * lower_bound,ir_node * upper_bound)1488 void set_array_bounds(ir_type *array, size_t dimension, ir_node *lower_bound,
1489 ir_node *upper_bound)
1490 {
1491 assert(array->type_op == type_array);
1492 assert(lower_bound && "lower_bound node may not be NULL.");
1493 assert(upper_bound && "upper_bound node may not be NULL.");
1494 assert(dimension < array->attr.aa.n_dimensions);
1495 array->attr.aa.lower_bound[dimension] = lower_bound;
1496 array->attr.aa.upper_bound[dimension] = upper_bound;
1497 }
1498
set_array_bounds_int(ir_type * array,size_t dimension,int lower_bound,int upper_bound)1499 void set_array_bounds_int(ir_type *array, size_t dimension, int lower_bound,
1500 int upper_bound)
1501 {
1502 ir_graph *irg = get_const_code_irg();
1503 set_array_bounds(array, dimension,
1504 new_r_Const_long(irg, mode_Iu, lower_bound),
1505 new_r_Const_long(irg, mode_Iu, upper_bound));
1506 }
1507
set_array_lower_bound(ir_type * array,size_t dimension,ir_node * lower_bound)1508 void set_array_lower_bound(ir_type *array, size_t dimension,
1509 ir_node *lower_bound)
1510 {
1511 assert(array->type_op == type_array);
1512 assert(lower_bound && "lower_bound node may not be NULL.");
1513 array->attr.aa.lower_bound[dimension] = lower_bound;
1514 }
1515
set_array_lower_bound_int(ir_type * array,size_t dimension,int lower_bound)1516 void set_array_lower_bound_int(ir_type *array, size_t dimension, int lower_bound)
1517 {
1518 ir_graph *irg = get_const_code_irg();
1519 set_array_lower_bound(array, dimension,
1520 new_r_Const_long(irg, mode_Iu, lower_bound));
1521 }
1522
set_array_upper_bound(ir_type * array,size_t dimension,ir_node * upper_bound)1523 void set_array_upper_bound(ir_type *array, size_t dimension, ir_node *upper_bound)
1524 {
1525 assert(array->type_op == type_array);
1526 assert(upper_bound && "upper_bound node may not be NULL.");
1527 array->attr.aa.upper_bound[dimension] = upper_bound;
1528 }
1529
set_array_upper_bound_int(ir_type * array,size_t dimension,int upper_bound)1530 void set_array_upper_bound_int(ir_type *array, size_t dimension, int upper_bound)
1531 {
1532 ir_graph *irg = get_const_code_irg();
1533 set_array_upper_bound(array, dimension,
1534 new_r_Const_long(irg, mode_Iu, upper_bound));
1535 }
1536
has_array_lower_bound(const ir_type * array,size_t dimension)1537 int has_array_lower_bound(const ir_type *array, size_t dimension)
1538 {
1539 assert(array->type_op == type_array);
1540 return !is_Unknown(array->attr.aa.lower_bound[dimension]);
1541 }
1542
get_array_lower_bound(const ir_type * array,size_t dimension)1543 ir_node *get_array_lower_bound(const ir_type *array, size_t dimension)
1544 {
1545 assert(array->type_op == type_array);
1546 return array->attr.aa.lower_bound[dimension];
1547 }
1548
get_array_lower_bound_int(const ir_type * array,size_t dimension)1549 long get_array_lower_bound_int(const ir_type *array, size_t dimension)
1550 {
1551 ir_node *node;
1552 assert(array->type_op == type_array);
1553 node = array->attr.aa.lower_bound[dimension];
1554 assert(is_Const(node));
1555 return get_tarval_long(get_Const_tarval(node));
1556 }
1557
has_array_upper_bound(const ir_type * array,size_t dimension)1558 int has_array_upper_bound(const ir_type *array, size_t dimension)
1559 {
1560 assert(array->type_op == type_array);
1561 return !is_Unknown(array->attr.aa.upper_bound[dimension]);
1562 }
1563
get_array_upper_bound(const ir_type * array,size_t dimension)1564 ir_node *get_array_upper_bound(const ir_type *array, size_t dimension)
1565 {
1566 assert(array->type_op == type_array);
1567 return array->attr.aa.upper_bound[dimension];
1568 }
1569
get_array_upper_bound_int(const ir_type * array,size_t dimension)1570 long get_array_upper_bound_int(const ir_type *array, size_t dimension)
1571 {
1572 ir_node *node;
1573 assert(array->type_op == type_array);
1574 node = array->attr.aa.upper_bound[dimension];
1575 assert(is_Const(node));
1576 return get_tarval_long(get_Const_tarval(node));
1577 }
1578
set_array_order(ir_type * array,size_t dimension,size_t order)1579 void set_array_order(ir_type *array, size_t dimension, size_t order)
1580 {
1581 assert(array->type_op == type_array);
1582 array->attr.aa.order[dimension] = order;
1583 }
1584
get_array_order(const ir_type * array,size_t dimension)1585 size_t get_array_order(const ir_type *array, size_t dimension)
1586 {
1587 assert(array->type_op == type_array);
1588 return array->attr.aa.order[dimension];
1589 }
1590
find_array_dimension(const ir_type * array,size_t order)1591 size_t find_array_dimension(const ir_type *array, size_t order)
1592 {
1593 size_t dim;
1594
1595 assert(array->type_op == type_array);
1596
1597 for (dim = 0; dim < array->attr.aa.n_dimensions; ++dim) {
1598 if (array->attr.aa.order[dim] == order)
1599 return dim;
1600 }
1601 return (size_t)-1;
1602 }
1603
set_array_element_type(ir_type * array,ir_type * tp)1604 void set_array_element_type(ir_type *array, ir_type *tp)
1605 {
1606 assert(array->type_op == type_array);
1607 assert(!is_Method_type(tp));
1608 array->attr.aa.element_type = tp;
1609 }
1610
get_array_element_type(const ir_type * array)1611 ir_type *get_array_element_type(const ir_type *array)
1612 {
1613 assert(array->type_op == type_array);
1614 return array->attr.aa.element_type;
1615 }
1616
set_array_element_entity(ir_type * array,ir_entity * ent)1617 void set_array_element_entity(ir_type *array, ir_entity *ent)
1618 {
1619 assert(array->type_op == type_array);
1620 assert((get_entity_type(ent)->type_op != type_method));
1621 array->attr.aa.element_ent = ent;
1622 array->attr.aa.element_type = get_entity_type(ent);
1623 }
1624
get_array_element_entity(const ir_type * array)1625 ir_entity *get_array_element_entity(const ir_type *array)
1626 {
1627 assert(array->type_op == type_array);
1628 return array->attr.aa.element_ent;
1629 }
1630
is_array_variable_size(const ir_type * array)1631 int is_array_variable_size(const ir_type *array)
1632 {
1633 assert(array->type_op == type_array);
1634 return (array->flags & tf_variable_size) != 0;
1635 }
1636
set_array_variable_size(ir_type * array,int flag)1637 void set_array_variable_size(ir_type *array, int flag)
1638 {
1639 assert(array->type_op == type_array);
1640 array->flags = (array->flags & ~tf_variable_size)
1641 | (flag != 0 ? tf_variable_size : 0);
1642 }
1643
1644 int (is_Array_type)(const ir_type *array)
1645 {
1646 return _is_array_type(array);
1647 }
1648
set_array_size(ir_type * tp,unsigned size)1649 void set_array_size(ir_type *tp, unsigned size)
1650 {
1651 /* FIXME: Here we should make some checks with the element type size */
1652 tp->size = size;
1653 }
1654
1655
new_d_type_enumeration(ident * name,size_t n_enums,type_dbg_info * db)1656 ir_type *new_d_type_enumeration(ident *name, size_t n_enums, type_dbg_info *db)
1657 {
1658 ir_type *res;
1659
1660 res = new_type(type_enumeration, NULL, db);
1661 res->name = name;
1662 res->attr.ea.enumer = NEW_ARR_F(ir_enum_const, n_enums);
1663 hook_new_type(res);
1664 return res;
1665 }
1666
new_type_enumeration(ident * name,size_t n_enums)1667 ir_type *new_type_enumeration(ident *name, size_t n_enums)
1668 {
1669 return new_d_type_enumeration(name, n_enums, NULL);
1670 }
1671
free_enumeration_entities(ir_type * enumeration)1672 void free_enumeration_entities(ir_type *enumeration)
1673 {
1674 (void) enumeration;
1675 assert(enumeration->type_op == type_enumeration);
1676 }
1677
free_enumeration_attrs(ir_type * enumeration)1678 void free_enumeration_attrs(ir_type *enumeration)
1679 {
1680 assert(enumeration->type_op == type_enumeration);
1681 DEL_ARR_F(enumeration->attr.ea.enumer);
1682 }
1683
get_enumeration_ident(const ir_type * enumeration)1684 ident *get_enumeration_ident(const ir_type *enumeration)
1685 {
1686 assert(enumeration->type_op == type_enumeration);
1687 return enumeration->name;
1688 }
1689
get_enumeration_name(const ir_type * enumeration)1690 const char *get_enumeration_name(const ir_type *enumeration)
1691 {
1692 if (get_enumeration_ident(enumeration) == NULL)
1693 return NULL;
1694 return get_id_str(get_enumeration_ident(enumeration));
1695 }
1696
get_enumeration_n_enums(const ir_type * enumeration)1697 size_t get_enumeration_n_enums(const ir_type *enumeration)
1698 {
1699 assert(enumeration->type_op == type_enumeration);
1700 return ARR_LEN(enumeration->attr.ea.enumer);
1701 }
1702
set_enumeration_const(ir_type * enumeration,size_t pos,ident * nameid,ir_tarval * con)1703 void set_enumeration_const(ir_type *enumeration, size_t pos, ident *nameid,
1704 ir_tarval *con)
1705 {
1706 assert(pos < ARR_LEN(enumeration->attr.ea.enumer));
1707 enumeration->attr.ea.enumer[pos].nameid = nameid;
1708 enumeration->attr.ea.enumer[pos].value = con;
1709 enumeration->attr.ea.enumer[pos].owner = enumeration;
1710 }
1711
get_enumeration_const(const ir_type * enumeration,size_t pos)1712 ir_enum_const *get_enumeration_const(const ir_type *enumeration, size_t pos)
1713 {
1714 assert(enumeration->type_op == type_enumeration);
1715 assert(pos < get_enumeration_n_enums(enumeration));
1716 return &enumeration->attr.ea.enumer[pos];
1717 }
1718
get_enumeration_owner(const ir_enum_const * enum_cnst)1719 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst)
1720 {
1721 return enum_cnst->owner;
1722 }
1723
set_enumeration_value(ir_enum_const * enum_cnst,ir_tarval * con)1724 void set_enumeration_value(ir_enum_const *enum_cnst, ir_tarval *con)
1725 {
1726 enum_cnst->value = con;
1727 }
1728
get_enumeration_value(const ir_enum_const * enum_cnst)1729 ir_tarval *get_enumeration_value(const ir_enum_const *enum_cnst)
1730 {
1731 return enum_cnst->value;
1732 }
1733
set_enumeration_nameid(ir_enum_const * enum_cnst,ident * id)1734 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id)
1735 {
1736 enum_cnst->nameid = id;
1737 }
1738
get_enumeration_const_nameid(const ir_enum_const * enum_cnst)1739 ident *get_enumeration_const_nameid(const ir_enum_const *enum_cnst)
1740 {
1741 return enum_cnst->nameid;
1742 }
1743
get_enumeration_const_name(const ir_enum_const * enum_cnst)1744 const char *get_enumeration_const_name(const ir_enum_const *enum_cnst)
1745 {
1746 return get_id_str(enum_cnst->nameid);
1747 }
1748
1749 int (is_Enumeration_type)(const ir_type *enumeration)
1750 {
1751 return _is_enumeration_type(enumeration);
1752 }
1753
set_enumeration_mode(ir_type * tp,ir_mode * mode)1754 void set_enumeration_mode(ir_type *tp, ir_mode *mode)
1755 {
1756 assert(mode_is_int(mode) && "Modes of enumerations must be integers");
1757 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1758 assert((get_mode_size_bits(mode) % 8) == 0 && "unorthodox modes not implemented");
1759
1760 tp->size = get_mode_size_bytes(mode);
1761 tp->mode = mode;
1762 }
1763
1764
1765
new_d_type_pointer(ir_type * points_to,type_dbg_info * db)1766 ir_type *new_d_type_pointer(ir_type *points_to, type_dbg_info *db)
1767 {
1768 ir_type *res;
1769 ir_mode *mode;
1770
1771 if (is_Method_type(points_to) || is_code_type(points_to)) {
1772 mode = mode_P_code;
1773 } else {
1774 mode = mode_P_data;
1775 }
1776
1777 res = new_type(type_pointer, mode, db);
1778 res->attr.pa.points_to = points_to;
1779 assert((get_mode_size_bits(res->mode) % 8 == 0) && "unorthodox modes not implemented");
1780 res->size = get_mode_size_bytes(res->mode);
1781 res->flags |= tf_layout_fixed;
1782 hook_new_type(res);
1783 return res;
1784 }
1785
new_type_pointer(ir_type * points_to)1786 ir_type *new_type_pointer(ir_type *points_to)
1787 {
1788 return new_d_type_pointer(points_to, NULL);
1789 }
1790
free_pointer_entities(ir_type * pointer)1791 void free_pointer_entities(ir_type *pointer)
1792 {
1793 (void) pointer;
1794 assert(pointer && (pointer->type_op == type_pointer));
1795 }
1796
free_pointer_attrs(ir_type * pointer)1797 void free_pointer_attrs(ir_type *pointer)
1798 {
1799 (void) pointer;
1800 assert(pointer && (pointer->type_op == type_pointer));
1801 }
1802
set_pointer_points_to_type(ir_type * pointer,ir_type * tp)1803 void set_pointer_points_to_type(ir_type *pointer, ir_type *tp)
1804 {
1805 assert(pointer && (pointer->type_op == type_pointer));
1806 pointer->attr.pa.points_to = tp;
1807 }
1808
get_pointer_points_to_type(const ir_type * pointer)1809 ir_type *get_pointer_points_to_type(const ir_type *pointer)
1810 {
1811 assert(pointer && (pointer->type_op == type_pointer));
1812 return pointer->attr.pa.points_to;
1813 }
1814
1815 int (is_Pointer_type)(const ir_type *pointer)
1816 {
1817 return _is_pointer_type(pointer);
1818 }
1819
set_pointer_mode(ir_type * tp,ir_mode * mode)1820 void set_pointer_mode(ir_type *tp, ir_mode *mode)
1821 {
1822 assert(mode_is_reference(mode) && "Modes of pointers must be references");
1823 /* For pointer and enumeration size depends on the mode, but only byte size allowed. */
1824 assert((get_mode_size_bits(mode) & 7) == 0 && "unorthodox modes not implemented");
1825
1826 tp->size = get_mode_size_bytes(mode);
1827 tp->mode = mode;
1828 }
1829
find_pointer_type_to_type(ir_type * tp)1830 ir_type *find_pointer_type_to_type(ir_type *tp)
1831 {
1832 size_t i, n = get_irp_n_types();
1833 for (i = 0; i < n; ++i) {
1834 ir_type *found = get_irp_type(i);
1835 if (is_Pointer_type(found) && get_pointer_points_to_type(found) == tp)
1836 return (found);
1837 }
1838 return get_unknown_type();
1839 }
1840
1841
new_d_type_primitive(ir_mode * mode,type_dbg_info * db)1842 ir_type *new_d_type_primitive(ir_mode *mode, type_dbg_info *db)
1843 {
1844 ir_type *res = new_type(type_primitive, mode, db);
1845 res->size = get_mode_size_bytes(mode);
1846 res->flags |= tf_layout_fixed;
1847 res->attr.ba.base_type = NULL;
1848 hook_new_type(res);
1849 return res;
1850 }
1851
new_type_primitive(ir_mode * mode)1852 ir_type *new_type_primitive(ir_mode *mode)
1853 {
1854 return new_d_type_primitive(mode, NULL);
1855 }
1856
1857 int (is_Primitive_type)(const ir_type *primitive)
1858 {
1859 return _is_primitive_type(primitive);
1860 }
1861
set_primitive_mode(ir_type * tp,ir_mode * mode)1862 void set_primitive_mode(ir_type *tp, ir_mode *mode)
1863 {
1864 /* Modes of primitives must be data */
1865 assert(mode_is_data(mode));
1866
1867 /* For primitive size depends on the mode. */
1868 tp->size = get_mode_size_bytes(mode);
1869 tp->mode = mode;
1870 }
1871
get_primitive_base_type(const ir_type * tp)1872 ir_type *get_primitive_base_type(const ir_type *tp)
1873 {
1874 assert(is_Primitive_type(tp));
1875 return tp->attr.ba.base_type;
1876 }
1877
set_primitive_base_type(ir_type * tp,ir_type * base_tp)1878 void set_primitive_base_type(ir_type *tp, ir_type *base_tp)
1879 {
1880 assert(is_Primitive_type(tp));
1881 tp->attr.ba.base_type = base_tp;
1882 }
1883
1884
1885
1886 int (is_atomic_type)(const ir_type *tp)
1887 {
1888 return _is_atomic_type(tp);
1889 }
1890
get_compound_n_members(const ir_type * tp)1891 size_t get_compound_n_members(const ir_type *tp)
1892 {
1893 const tp_op *op = get_type_tpop(tp);
1894 return op->ops.get_n_members(tp);
1895 }
1896
get_compound_member(const ir_type * tp,size_t pos)1897 ir_entity *get_compound_member(const ir_type *tp, size_t pos)
1898 {
1899 const tp_op *op = get_type_tpop(tp);
1900 return op->ops.get_member(tp, pos);
1901 }
1902
get_compound_member_index(const ir_type * tp,ir_entity * member)1903 size_t get_compound_member_index(const ir_type *tp, ir_entity *member)
1904 {
1905 const tp_op *op = get_type_tpop(tp);
1906 return op->ops.get_member_index(tp, member);
1907 }
1908
set_compound_variable_size(ir_type * tp,int variable_size_flag)1909 void set_compound_variable_size(ir_type *tp, int variable_size_flag)
1910 {
1911 assert(is_compound_type(tp));
1912 tp->flags = (tp->flags & ~tf_variable_size)
1913 | (variable_size_flag != 0 ? tf_variable_size : 0);
1914 }
1915
is_compound_variable_size(const ir_type * tp)1916 int is_compound_variable_size(const ir_type *tp)
1917 {
1918 assert(is_compound_type(tp));
1919 return (tp->flags & tf_variable_size) != 0;
1920 }
1921
is_compound_type(const ir_type * tp)1922 int is_compound_type(const ir_type *tp)
1923 {
1924 assert(tp->kind == k_type);
1925 return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
1926 }
1927
get_compound_ident(const ir_type * tp)1928 ident *get_compound_ident(const ir_type *tp)
1929 {
1930 assert(is_compound_type(tp));
1931 return tp->name;
1932 }
1933
get_compound_name(const ir_type * tp)1934 const char *get_compound_name(const ir_type *tp)
1935 {
1936 if (get_compound_ident(tp) == NULL)
1937 return NULL;
1938 return get_id_str(get_compound_ident(tp));
1939 }
1940
remove_compound_member(ir_type * compound,ir_entity * entity)1941 void remove_compound_member(ir_type *compound, ir_entity *entity)
1942 {
1943 switch (get_type_tpop_code(compound)) {
1944 case tpo_class: remove_class_member(compound, entity); break;
1945 case tpo_struct: remove_struct_member(compound, entity); break;
1946 case tpo_union: remove_union_member(compound, entity); break;
1947 default:
1948 panic("argument for remove_compound_member not a compound type");
1949 }
1950 }
1951
add_compound_member(ir_type * compound,ir_entity * entity)1952 void add_compound_member(ir_type *compound, ir_entity *entity)
1953 {
1954 switch (get_type_tpop_code(compound)) {
1955 case tpo_class: add_class_member(compound, entity); break;
1956 case tpo_struct: add_struct_member(compound, entity); break;
1957 case tpo_union: add_union_member(compound, entity); break;
1958 default:
1959 panic("argument for add_compound_member not a compound type");
1960 }
1961 }
1962
is_code_type(const ir_type * tp)1963 int is_code_type(const ir_type *tp)
1964 {
1965 assert(tp->kind == k_type);
1966 return tp->type_op == tpop_code;
1967 }
1968
is_unknown_type(const ir_type * tp)1969 int is_unknown_type(const ir_type *tp)
1970 {
1971 assert(tp->kind == k_type);
1972 return tp->type_op == tpop_unknown;
1973 }
1974
is_none_type(const ir_type * tp)1975 int is_none_type(const ir_type *tp)
1976 {
1977 assert(tp->kind == k_type);
1978 return tp->type_op == tpop_none;
1979 }
1980
is_frame_type(const ir_type * tp)1981 int is_frame_type(const ir_type *tp)
1982 {
1983 return tp->flags & tf_frame_type;
1984 }
1985
new_type_frame(void)1986 ir_type *new_type_frame(void)
1987 {
1988 ir_type *res = new_type_class(new_id_from_str("<frame_type>"));
1989
1990 res->flags |= tf_frame_type;
1991
1992 /* It is not possible to derive from the frame type. Set the final flag. */
1993 set_class_final(res, 1);
1994
1995 return res;
1996 }
1997
clone_frame_type(ir_type * type)1998 ir_type *clone_frame_type(ir_type *type)
1999 {
2000 ir_type *res;
2001 size_t i, n;
2002
2003 assert(is_frame_type(type));
2004 /* the entity link resource should be allocated if this function is called */
2005 assert(irp_resources_reserved(irp) & IRP_RESOURCE_ENTITY_LINK);
2006
2007 res = new_type_frame();
2008 for (i = 0, n = get_class_n_members(type); i < n; ++i) {
2009 ir_entity *ent = get_class_member(type, i);
2010 ir_entity *nent = copy_entity_own(ent, res);
2011 set_entity_link(ent, nent);
2012 set_entity_link(nent, ent);
2013 }
2014 return res;
2015 }
2016
set_default_size(ir_type * tp,unsigned size)2017 void set_default_size(ir_type *tp, unsigned size)
2018 {
2019 tp->size = size;
2020 }
2021
default_layout_compound_type(ir_type * type)2022 void default_layout_compound_type(ir_type *type)
2023 {
2024 size_t i;
2025 size_t n = get_compound_n_members(type);
2026 int size = 0;
2027 unsigned align_all = 1;
2028 bool var_size = is_compound_variable_size(type);
2029
2030 for (i = 0; i < n; ++i) {
2031 ir_entity *entity = get_compound_member(type, i);
2032 ir_type *entity_type = get_entity_type(entity);
2033 unsigned align;
2034 unsigned misalign;
2035 unsigned entity_size;
2036
2037 if (is_Method_type(entity_type))
2038 continue;
2039
2040 if (i+1 < n || !var_size) {
2041 assert(get_type_state(entity_type) == layout_fixed);
2042 entity_size = get_type_size_bytes(entity_type);
2043 } else {
2044 entity_size = 0;
2045 }
2046
2047 align = get_type_alignment_bytes(entity_type);
2048 align_all = align > align_all ? align : align_all;
2049 misalign = (align ? size % align : 0);
2050 size += (misalign ? align - misalign : 0);
2051
2052 set_entity_offset(entity, size);
2053 if (!is_Union_type(type)) {
2054 size += entity_size;
2055 }
2056 }
2057 if (align_all > 0 && size % align_all) {
2058 size += align_all - (size % align_all);
2059 }
2060 if (align_all > get_type_alignment_bytes(type)) {
2061 set_type_alignment_bytes(type, align_all);
2062 }
2063 set_type_size_bytes(type, size);
2064 set_type_state(type, layout_fixed);
2065 }
2066
frame_alloc_area(ir_type * frame_type,int size,unsigned alignment,int at_start)2067 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment,
2068 int at_start)
2069 {
2070 ir_entity *area;
2071 ir_type *tp;
2072 ident *name;
2073 char buf[32];
2074 int offset;
2075 unsigned frame_size = get_type_size_bytes(frame_type);
2076 unsigned frame_align = get_type_alignment_bytes(frame_type);
2077 static unsigned area_cnt = 0;
2078
2079 assert(is_frame_type(frame_type));
2080 assert(get_type_state(frame_type) == layout_fixed);
2081 assert(get_type_alignment_bytes(frame_type) > 0);
2082 set_type_state(frame_type, layout_undefined);
2083
2084 if (irp->byte_type == NULL)
2085 irp->byte_type = new_type_primitive(mode_Bu);
2086
2087 snprintf(buf, sizeof(buf), "area%u", area_cnt++);
2088 name = new_id_from_str(buf);
2089
2090 tp = new_type_array(1, irp->byte_type);
2091 set_array_bounds_int(tp, 0, 0, size);
2092 set_type_alignment_bytes(tp, alignment);
2093 set_type_size_bytes(tp, size);
2094
2095 if (at_start) {
2096 size_t i, n;
2097 unsigned delta = (size + frame_align - 1) & ~(frame_align - 1);
2098 /* fix all offsets so far */
2099 for (i = 0, n = get_class_n_members(frame_type); i < n; ++i) {
2100 ir_entity *ent = get_class_member(frame_type, i);
2101
2102 set_entity_offset(ent, get_entity_offset(ent) + delta);
2103 }
2104 /* calculate offset and new type size */
2105 offset = 0;
2106 frame_size += delta;
2107 } else {
2108 /* calculate offset and new type size */
2109 offset = (frame_size + alignment - 1) & ~(alignment - 1);
2110 frame_size = offset + size;
2111 }
2112
2113 area = new_entity(frame_type, name, tp);
2114 set_entity_offset(area, offset);
2115 set_type_size_bytes(frame_type, frame_size);
2116 if (alignment > frame_align) {
2117 set_type_alignment_bytes(frame_type, alignment);
2118 }
2119
2120 /* mark this entity as compiler generated */
2121 set_entity_compiler_generated(area, 1);
2122
2123 set_type_state(frame_type, layout_fixed);
2124 return area;
2125 }
2126
ir_print_type(char * buffer,size_t buffer_size,const ir_type * type)2127 void ir_print_type(char *buffer, size_t buffer_size, const ir_type *type)
2128 {
2129 ident *id;
2130 int p;
2131 type_dbg_info *tdbgi = get_type_dbg_info(type);
2132 if (tdbgi != NULL) {
2133 ir_retrieve_type_dbg_info(buffer, buffer_size, tdbgi);
2134 return;
2135 }
2136
2137 /* we have to construct some name... */
2138 switch (get_type_tpop_code(type)) {
2139 case tpo_uninitialized:
2140 break;
2141 case tpo_code:
2142 snprintf(buffer, buffer_size, "code");
2143 return;
2144
2145 case tpo_class:
2146 id = get_class_ident(type);
2147 snprintf(buffer, buffer_size, "class '%s'", get_id_str(id));
2148 return;
2149
2150 case tpo_struct:
2151 id = get_struct_ident(type);
2152 snprintf(buffer, buffer_size, "struct '%s'", get_id_str(id));
2153 return;
2154
2155 case tpo_union:
2156 id = get_union_ident(type);
2157 snprintf(buffer, buffer_size, "union '%s'", get_id_str(id));
2158 return;
2159
2160 case tpo_enumeration:
2161 id = get_enumeration_ident(type);
2162 snprintf(buffer, buffer_size, "enumeration '%s'", get_id_str(id));
2163 return;
2164
2165 case tpo_unknown:
2166 snprintf(buffer, buffer_size, "unknown type");
2167 return;
2168
2169 case tpo_pointer:
2170 p = snprintf(buffer, buffer_size, "pointer to ");
2171 buffer += p;
2172 buffer_size -= p;
2173 ir_print_type(buffer, buffer_size, get_pointer_points_to_type(type));
2174 return;
2175
2176 case tpo_array:
2177 p = snprintf(buffer, buffer_size, "array of ");
2178 buffer += p;
2179 buffer_size -= p;
2180 ir_print_type(buffer, buffer_size, get_array_element_type(type));
2181 return;
2182
2183 case tpo_primitive:
2184 id = get_mode_ident(get_type_mode(type));
2185 snprintf(buffer, buffer_size, "%s", get_id_str(id));
2186 return;
2187
2188 case tpo_none:
2189 snprintf(buffer, buffer_size, "none");
2190 return;
2191 case tpo_method:
2192 /* TODO: we should print argument and return types here... */
2193 snprintf(buffer, buffer_size, "method type");
2194 return;
2195 }
2196 snprintf(buffer, buffer_size, "invalid type");
2197 }
2198