2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <libfirm/firm.h>
27 #include <libfirm/adt/obst.h>
28 #include <libfirm/be.h>
32 #include "adt/error.h"
33 #include "adt/array.h"
39 #include "diagnostic.h"
40 #include "lang_features.h"
43 #include "entitymap_t.h"
44 #include "driver/firm_opt.h"
45 #include "driver/firm_cmdline.h"
47 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
49 /* some idents needed for name mangling */
50 static ident *id_underscore;
53 static ir_type *ir_type_const_char;
54 static ir_type *ir_type_wchar_t;
55 static ir_type *ir_type_void;
56 static ir_type *ir_type_int;
58 static type_t *type_const_char;
60 static int next_value_number_function;
61 static ir_node *continue_label;
62 static ir_node *break_label;
63 static ir_node *current_switch_cond;
64 static bool saw_default_label;
65 static ir_node **imature_blocks;
66 static bool constant_folding;
68 static const declaration_t *current_function_decl;
69 static ir_node *current_function_name;
70 static ir_node *current_funcsig;
72 static entitymap_t entitymap;
74 static struct obstack asm_obst;
76 typedef enum declaration_kind_t {
77 DECLARATION_KIND_UNKNOWN,
78 DECLARATION_KIND_FUNCTION,
79 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
80 DECLARATION_KIND_GLOBAL_VARIABLE,
81 DECLARATION_KIND_LOCAL_VARIABLE,
82 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
83 DECLARATION_KIND_COMPOUND_MEMBER,
84 DECLARATION_KIND_LABEL_BLOCK,
85 DECLARATION_KIND_ENUM_ENTRY,
86 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
87 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
91 static ir_type *get_ir_type(type_t *type);
92 static ir_type *get_ir_type_incomplete(type_t *type);
93 static int count_decls_in_stmts(const statement_t *stmt);
95 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
97 const declaration_t *declaration = get_irg_loc_description(irg, pos);
99 if (declaration != NULL) {
100 warningf(&declaration->source_position,
101 "variable '%#T' might be used uninitialized",
102 declaration->type, declaration->symbol);
104 return new_r_Unknown(irg, mode);
107 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
109 const source_position_t *pos = (const source_position_t*) dbg;
112 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
116 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
118 const source_position_t *pos = (const source_position_t*) dbg;
123 return pos->input_name;
126 static dbg_info *get_dbg_info(const source_position_t *pos)
128 return (dbg_info*) pos;
131 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
133 static ir_mode *mode_int, *mode_uint;
135 static ir_node *_expression_to_firm(const expression_t *expression);
136 static ir_node *expression_to_firm(const expression_t *expression);
137 static inline ir_mode *get_ir_mode(type_t *type);
138 static void create_local_declaration(declaration_t *declaration);
140 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
142 unsigned flags = get_atomic_type_flags(kind);
143 unsigned size = get_atomic_type_size(kind);
144 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
145 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
148 unsigned bit_size = size * 8;
149 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
150 ir_mode_arithmetic arithmetic;
151 unsigned modulo_shift;
153 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
154 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
155 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
156 sort = irms_int_number;
157 arithmetic = irma_twos_complement;
158 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
160 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
161 snprintf(name, sizeof(name), "f%d", bit_size);
162 sort = irms_float_number;
163 arithmetic = irma_ieee754;
166 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
174 * Initialises the atomic modes depending on the machine size.
176 static void init_atomic_modes(void)
178 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
179 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
181 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
182 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
184 /* there's no real void type in firm */
185 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
187 /* initialize pointer modes */
189 ir_mode_sort sort = irms_reference;
190 unsigned bit_size = machine_size;
192 ir_mode_arithmetic arithmetic = irma_twos_complement;
193 unsigned modulo_shift
194 = bit_size < machine_size ? machine_size : bit_size;
196 snprintf(name, sizeof(name), "p%d", machine_size);
197 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
200 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
201 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
203 /* Hmm, pointers should be machine size */
204 set_modeP_data(ptr_mode);
205 set_modeP_code(ptr_mode);
208 static unsigned get_compound_type_size(compound_type_t *type)
210 ir_type *irtype = get_ir_type((type_t*) type);
211 return get_type_size_bytes(irtype);
214 static unsigned get_array_type_size(array_type_t *type)
216 assert(!type->is_vla);
217 ir_type *irtype = get_ir_type((type_t*) type);
218 return get_type_size_bytes(irtype);
222 static unsigned get_type_size_const(type_t *type)
226 panic("error type occurred");
228 return get_atomic_type_size(type->atomic.akind);
230 return 2 * get_atomic_type_size(type->complex.akind);
232 return get_atomic_type_size(type->imaginary.akind);
234 return get_mode_size_bytes(mode_int);
235 case TYPE_COMPOUND_UNION:
236 case TYPE_COMPOUND_STRUCT:
237 return get_compound_type_size(&type->compound);
239 /* just a pointer to the function */
240 return get_mode_size_bytes(mode_P_code);
242 return get_mode_size_bytes(mode_P_data);
244 return get_array_type_size(&type->array);
246 return get_type_size_const(type->builtin.real_type);
248 panic("type size of bitfield request");
254 panic("Trying to determine size of invalid type");
257 static ir_node *get_type_size(type_t *type)
259 type = skip_typeref(type);
261 if (is_type_array(type) && type->array.is_vla) {
262 ir_node *size_node = type->array.size_node;
263 if (size_node == NULL) {
264 size_node = expression_to_firm(type->array.size_expression);
265 assert(!is_Const(size_node));
266 type->array.size_node = size_node;
269 ir_node *elem_size = get_type_size(type->array.element_type);
270 ir_mode *mode = get_irn_mode(size_node);
271 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
275 ir_mode *mode = get_ir_mode(type_size_t);
277 sym.type_p = get_ir_type(type);
278 return new_SymConst(mode, sym, symconst_type_size);
281 static unsigned count_parameters(const function_type_t *function_type)
285 function_parameter_t *parameter = function_type->parameters;
286 for ( ; parameter != NULL; parameter = parameter->next) {
294 * Creates a Firm type for an atomic type
296 static ir_type *create_atomic_type(const atomic_type_t *type)
298 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
299 atomic_type_kind_t kind = type->akind;
300 ir_mode *mode = _atomic_modes[kind];
301 ident *id = get_mode_ident(mode);
302 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
304 set_type_alignment_bytes(irtype, type->base.alignment);
310 * Creates a Firm type for a complex type
312 static ir_type *create_complex_type(const complex_type_t *type)
314 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
315 atomic_type_kind_t kind = type->akind;
316 ir_mode *mode = _atomic_modes[kind];
317 ident *id = get_mode_ident(mode);
322 /* FIXME: finish the array */
327 * Creates a Firm type for an imaginary type
329 static ir_type *create_imaginary_type(const imaginary_type_t *type)
331 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
332 atomic_type_kind_t kind = type->akind;
333 ir_mode *mode = _atomic_modes[kind];
334 ident *id = get_mode_ident(mode);
335 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
337 set_type_alignment_bytes(irtype, type->base.alignment);
343 * return type of a parameter (and take transparent union gnu extension into
346 static type_t *get_parameter_type(type_t *type)
348 type = skip_typeref(type);
349 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
350 declaration_t *decl = type->compound.declaration;
351 type = decl->scope.declarations->type;
357 static ir_type *create_method_type(const function_type_t *function_type)
359 type_t *return_type = function_type->return_type;
361 ident *id = id_unique("functiontype.%u");
362 int n_parameters = count_parameters(function_type);
363 int n_results = return_type == type_void ? 0 : 1;
364 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
365 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
367 if (return_type != type_void) {
368 ir_type *restype = get_ir_type(return_type);
369 set_method_res_type(irtype, 0, restype);
372 function_parameter_t *parameter = function_type->parameters;
374 for ( ; parameter != NULL; parameter = parameter->next) {
375 type_t *type = get_parameter_type(parameter->type);
376 ir_type *p_irtype = get_ir_type(type);
377 set_method_param_type(irtype, n, p_irtype);
381 if (function_type->variadic || function_type->unspecified_parameters) {
382 set_method_variadicity(irtype, variadicity_variadic);
385 unsigned cc = get_method_calling_convention(irtype);
386 switch (function_type->calling_convention) {
387 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typical cdecl */
389 set_method_calling_convention(irtype, SET_CDECL(cc));
393 set_method_calling_convention(irtype, cc_fixed | SET_CDECL(cc));
397 if (function_type->variadic || function_type->unspecified_parameters)
400 /* only non-variadic function can use stdcall, else use cdecl */
401 set_method_calling_convention(irtype, cc_fixed | SET_STDCALL(cc));
405 if (function_type->variadic || function_type->unspecified_parameters)
407 /* only non-variadic function can use fastcall, else use cdecl */
408 set_method_calling_convention(irtype, cc_fixed | SET_FASTCALL(cc));
412 /* Hmm, leave default, not accepted by the parser yet. */
413 warningf(&function_type->base.source_position, "THISCALL calling convention not supported yet");
419 static ir_type *create_pointer_type(pointer_type_t *type)
421 type_t *points_to = type->points_to;
422 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
423 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
424 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
425 ir_points_to, mode_P_data, dbgi);
430 static ir_type *create_array_type(array_type_t *type)
432 type_t *element_type = type->element_type;
433 ir_type *ir_element_type = get_ir_type(element_type);
435 ident *id = id_unique("array.%u");
436 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
437 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
439 const int align = get_type_alignment_bytes(ir_element_type);
440 set_type_alignment_bytes(ir_type, align);
442 if (type->size_constant) {
443 int n_elements = type->size;
445 set_array_bounds_int(ir_type, 0, 0, n_elements);
447 size_t elemsize = get_type_size_bytes(ir_element_type);
448 if (elemsize % align > 0) {
449 elemsize += align - (elemsize % align);
451 set_type_size_bytes(ir_type, n_elements * elemsize);
453 set_array_lower_bound_int(ir_type, 0, 0);
455 set_type_state(ir_type, layout_fixed);
461 * Return the signed integer type of size bits.
463 * @param size the size
465 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
468 static ir_mode *s_modes[64 + 1] = {NULL, };
472 if (size <= 0 || size > 64)
475 mode = s_modes[size];
479 snprintf(name, sizeof(name), "bf_I%u", size);
480 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
481 size <= 32 ? 32 : size );
482 s_modes[size] = mode;
486 snprintf(name, sizeof(name), "I%u", size);
487 ident *id = new_id_from_str(name);
488 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
489 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
490 set_primitive_base_type(res, base_tp);
496 * Return the unsigned integer type of size bits.
498 * @param size the size
500 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
503 static ir_mode *u_modes[64 + 1] = {NULL, };
507 if (size <= 0 || size > 64)
510 mode = u_modes[size];
514 snprintf(name, sizeof(name), "bf_U%u", size);
515 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
516 size <= 32 ? 32 : size );
517 u_modes[size] = mode;
522 snprintf(name, sizeof(name), "U%u", size);
523 ident *id = new_id_from_str(name);
524 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
525 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
526 set_primitive_base_type(res, base_tp);
531 static ir_type *create_bitfield_type(bitfield_type_t *const type)
533 type_t *base = skip_typeref(type->base_type);
534 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
535 ir_type *irbase = get_ir_type(base);
537 unsigned size = fold_constant(type->size);
539 assert(!is_type_float(base));
540 if (is_type_signed(base)) {
541 return get_signed_int_type_for_bit_size(irbase, size);
543 return get_unsigned_int_type_for_bit_size(irbase, size);
547 #define INVALID_TYPE ((ir_type_ptr)-1)
550 COMPOUND_IS_STRUCT = false,
551 COMPOUND_IS_UNION = true
555 * Construct firm type from ast struct type.
557 * As anonymous inner structs get flattened to a single firm type, we might get
558 * irtype, outer_offset and out_align passed (they represent the position of
559 * the anonymous inner struct inside the resulting firm struct)
561 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
562 size_t *outer_offset, size_t *outer_align,
563 bool incomplete, bool is_union)
565 declaration_t *declaration = type->declaration;
566 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
568 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
569 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
571 return declaration->v.irtype;
573 size_t align_all = 1;
575 size_t bit_offset = 0;
578 if (irtype == NULL) {
579 symbol_t *symbol = declaration->symbol;
581 if (symbol != NULL) {
582 id = new_id_from_str(symbol->string);
585 id = id_unique("__anonymous_union.%u");
587 id = id_unique("__anonymous_struct.%u");
590 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
593 irtype = new_d_type_union(id, dbgi);
595 irtype = new_d_type_struct(id, dbgi);
598 declaration->declaration_kind
599 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
600 declaration->v.irtype = irtype;
601 //type->base.firm_type = irtype;
603 offset = *outer_offset;
604 align_all = *outer_align;
610 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
612 declaration_t *entry = declaration->scope.declarations;
613 for( ; entry != NULL; entry = entry->next) {
614 if (entry->namespc != NAMESPACE_NORMAL)
617 size_t prev_offset = offset;
619 symbol_t *symbol = entry->symbol;
620 type_t *entry_type = skip_typeref(entry->type);
621 dbg_info *dbgi = get_dbg_info(&entry->source_position);
624 if (symbol != NULL) {
625 ident = new_id_from_str(symbol->string);
627 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
628 create_compound_type(&entry_type->compound, irtype, &offset,
629 &align_all, false, COMPOUND_IS_STRUCT);
630 goto finished_member;
631 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
632 create_compound_type(&entry_type->compound, irtype, &offset,
633 &align_all, false, COMPOUND_IS_UNION);
634 goto finished_member;
636 assert(entry_type->kind == TYPE_BITFIELD);
638 ident = id_unique("anon.%u");
641 ir_type *base_irtype;
642 if (entry_type->kind == TYPE_BITFIELD) {
643 base_irtype = get_ir_type(entry_type->bitfield.base_type);
645 base_irtype = get_ir_type(entry_type);
648 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
649 size_t misalign = offset % entry_alignment;
651 ir_type *entry_irtype = get_ir_type(entry_type);
652 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
655 size_t bits_remainder;
656 if (entry_type->kind == TYPE_BITFIELD) {
657 size_t size_bits = fold_constant(entry_type->bitfield.size);
658 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
660 if (size_bits > rest_size_bits) {
661 /* start a new bucket */
662 offset += entry_alignment - misalign;
668 /* put into current bucket */
669 base = offset - misalign;
670 bits_remainder = misalign * 8 + bit_offset;
673 offset += size_bits / 8;
674 bit_offset = bit_offset + (size_bits % 8);
676 size_t entry_size = get_type_size_bytes(base_irtype);
677 if (misalign > 0 || bit_offset > 0)
678 offset += entry_alignment - misalign;
682 offset += entry_size;
686 if (entry_alignment > align_all) {
687 if (entry_alignment % align_all != 0) {
688 panic("uneven alignments not supported yet");
690 align_all = entry_alignment;
693 set_entity_offset(entity, base);
694 set_entity_offset_bits_remainder(entity,
695 (unsigned char) bits_remainder);
696 //add_struct_member(irtype, entity);
697 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
698 assert(entry->v.entity == NULL);
699 entry->v.entity = entity;
703 size_t entry_size = offset - prev_offset;
704 if (entry_size > size) {
716 size_t misalign = offset % align_all;
717 if (misalign > 0 || bit_offset > 0) {
718 size += align_all - misalign;
721 if (outer_offset != NULL) {
723 *outer_offset = offset;
725 *outer_offset += size;
728 if (align_all > *outer_align) {
729 if (align_all % *outer_align != 0) {
730 panic("uneven alignments not supported yet");
732 *outer_align = align_all;
735 set_type_alignment_bytes(irtype, align_all);
736 set_type_size_bytes(irtype, size);
737 set_type_state(irtype, layout_fixed);
743 static ir_type *create_enum_type(enum_type_t *const type)
745 type->base.firm_type = ir_type_int;
747 ir_mode *const mode = mode_int;
748 tarval *const one = get_mode_one(mode);
749 tarval * tv_next = get_tarval_null(mode);
751 bool constant_folding_old = constant_folding;
752 constant_folding = true;
754 declaration_t *declaration = type->declaration->next;
755 for (; declaration != NULL; declaration = declaration->next) {
756 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
759 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
761 expression_t *const init = declaration->init.enum_value;
763 ir_node *const cnst = expression_to_firm(init);
764 if (!is_Const(cnst)) {
765 panic("couldn't fold constant");
767 tv_next = get_Const_tarval(cnst);
769 declaration->v.enum_val = tv_next;
770 tv_next = tarval_add(tv_next, one);
773 constant_folding = constant_folding_old;
778 static ir_type *get_ir_type_incomplete(type_t *type)
780 assert(type != NULL);
781 type = skip_typeref(type);
783 if (type->base.firm_type != NULL) {
784 assert(type->base.firm_type != INVALID_TYPE);
785 return type->base.firm_type;
788 switch (type->kind) {
789 case TYPE_COMPOUND_STRUCT:
790 return create_compound_type(&type->compound, NULL, NULL, NULL,
791 true, COMPOUND_IS_STRUCT);
792 case TYPE_COMPOUND_UNION:
793 return create_compound_type(&type->compound, NULL, NULL, NULL,
794 true, COMPOUND_IS_UNION);
796 return get_ir_type(type);
800 static ir_type *get_ir_type(type_t *type)
802 assert(type != NULL);
804 type = skip_typeref(type);
806 if (type->base.firm_type != NULL) {
807 assert(type->base.firm_type != INVALID_TYPE);
808 return type->base.firm_type;
811 ir_type *firm_type = NULL;
812 switch (type->kind) {
814 panic("error type occurred");
816 firm_type = create_atomic_type(&type->atomic);
819 firm_type = create_complex_type(&type->complex);
822 firm_type = create_imaginary_type(&type->imaginary);
825 firm_type = create_method_type(&type->function);
828 firm_type = create_pointer_type(&type->pointer);
831 firm_type = create_array_type(&type->array);
833 case TYPE_COMPOUND_STRUCT:
834 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
835 false, COMPOUND_IS_STRUCT);
837 case TYPE_COMPOUND_UNION:
838 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
839 false, COMPOUND_IS_UNION);
842 firm_type = create_enum_type(&type->enumt);
845 firm_type = get_ir_type(type->builtin.real_type);
848 firm_type = create_bitfield_type(&type->bitfield);
856 if (firm_type == NULL)
857 panic("unknown type found");
859 type->base.firm_type = firm_type;
863 static inline ir_mode *get_ir_mode(type_t *type)
865 ir_type *irtype = get_ir_type(type);
867 /* firm doesn't report a mode for arrays somehow... */
868 if (is_Array_type(irtype)) {
872 ir_mode *mode = get_type_mode(irtype);
873 assert(mode != NULL);
877 /** Names of the runtime functions. */
878 static const struct {
879 int id; /**< the rts id */
880 int n_res; /**< number of return values */
881 const char *name; /**< the name of the rts function */
882 int n_params; /**< number of parameters */
883 unsigned flags; /**< language flags */
885 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
886 { rts_abort, 0, "abort", 0, _C89 },
887 { rts_alloca, 1, "alloca", 1, _ALL },
888 { rts_abs, 1, "abs", 1, _C89 },
889 { rts_labs, 1, "labs", 1, _C89 },
890 { rts_llabs, 1, "llabs", 1, _C99 },
891 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
893 { rts_fabs, 1, "fabs", 1, _C89 },
894 { rts_sqrt, 1, "sqrt", 1, _C89 },
895 { rts_cbrt, 1, "cbrt", 1, _C99 },
896 { rts_exp, 1, "exp", 1, _C89 },
897 { rts_exp2, 1, "exp2", 1, _C89 },
898 { rts_exp10, 1, "exp10", 1, _GNUC },
899 { rts_log, 1, "log", 1, _C89 },
900 { rts_log2, 1, "log2", 1, _C89 },
901 { rts_log10, 1, "log10", 1, _C89 },
902 { rts_pow, 1, "pow", 2, _C89 },
903 { rts_sin, 1, "sin", 1, _C89 },
904 { rts_cos, 1, "cos", 1, _C89 },
905 { rts_tan, 1, "tan", 1, _C89 },
906 { rts_asin, 1, "asin", 1, _C89 },
907 { rts_acos, 1, "acos", 1, _C89 },
908 { rts_atan, 1, "atan", 1, _C89 },
909 { rts_sinh, 1, "sinh", 1, _C89 },
910 { rts_cosh, 1, "cosh", 1, _C89 },
911 { rts_tanh, 1, "tanh", 1, _C89 },
913 { rts_fabsf, 1, "fabsf", 1, _C99 },
914 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
915 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
916 { rts_expf, 1, "expf", 1, _C99 },
917 { rts_exp2f, 1, "exp2f", 1, _C99 },
918 { rts_exp10f, 1, "exp10f", 1, _GNUC },
919 { rts_logf, 1, "logf", 1, _C99 },
920 { rts_log2f, 1, "log2f", 1, _C99 },
921 { rts_log10f, 1, "log10f", 1, _C99 },
922 { rts_powf, 1, "powf", 2, _C99 },
923 { rts_sinf, 1, "sinf", 1, _C99 },
924 { rts_cosf, 1, "cosf", 1, _C99 },
925 { rts_tanf, 1, "tanf", 1, _C99 },
926 { rts_asinf, 1, "asinf", 1, _C99 },
927 { rts_acosf, 1, "acosf", 1, _C99 },
928 { rts_atanf, 1, "atanf", 1, _C99 },
929 { rts_sinhf, 1, "sinhf", 1, _C99 },
930 { rts_coshf, 1, "coshf", 1, _C99 },
931 { rts_tanhf, 1, "tanhf", 1, _C99 },
933 { rts_fabsl, 1, "fabsl", 1, _C99 },
934 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
935 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
936 { rts_expl, 1, "expl", 1, _C99 },
937 { rts_exp2l, 1, "exp2l", 1, _C99 },
938 { rts_exp10l, 1, "exp10l", 1, _GNUC },
939 { rts_logl, 1, "logl", 1, _C99 },
940 { rts_log2l, 1, "log2l", 1, _C99 },
941 { rts_log10l, 1, "log10l", 1, _C99 },
942 { rts_powl, 1, "powl", 2, _C99 },
943 { rts_sinl, 1, "sinl", 1, _C99 },
944 { rts_cosl, 1, "cosl", 1, _C99 },
945 { rts_tanl, 1, "tanl", 1, _C99 },
946 { rts_asinl, 1, "asinl", 1, _C99 },
947 { rts_acosl, 1, "acosl", 1, _C99 },
948 { rts_atanl, 1, "atanl", 1, _C99 },
949 { rts_sinhl, 1, "sinhl", 1, _C99 },
950 { rts_coshl, 1, "coshl", 1, _C99 },
951 { rts_tanhl, 1, "tanhl", 1, _C99 },
953 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
954 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
955 { rts_strcmp, 1, "strcmp", 2, _C89 },
956 { rts_strncmp, 1, "strncmp", 3, _C89 }
959 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
962 * Mangles an entity linker (ld) name for win32 usage.
964 * @param ent the entity to be mangled
965 * @param declaration the declaration
967 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
971 if (is_Method_type(get_entity_type(ent)))
972 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
974 /* always add an underscore in win32 */
975 id = mangle(id_underscore, get_entity_ident(ent));
978 decl_modifiers_t decl_modifiers = declaration->modifiers;
979 if (decl_modifiers & DM_DLLIMPORT) {
980 /* add prefix for imported symbols */
981 id = mangle(id_imp, id);
987 * Mangles an entity linker (ld) name for Linux ELF usage.
989 * @param ent the entity to be mangled
990 * @param declaration the declaration
992 static ident *create_ld_ident_linux_elf(ir_entity *entity,
993 declaration_t *declaration)
996 return get_entity_ident(entity);
1000 * Mangles an entity linker (ld) name for Mach-O usage.
1002 * @param ent the entity to be mangled
1003 * @param declaration the declaration
1005 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
1008 ident *id = mangle(id_underscore, get_entity_ident(ent));
1012 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
1013 declaration_t *declaration);
1014 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
1017 * Handle GNU attributes for entities
1019 * @param ent the entity
1020 * @param decl the routine declaration
1022 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
1024 if (decl->modifiers & DM_PURE) {
1025 /* TRUE if the declaration includes the GNU
1026 __attribute__((pure)) specifier. */
1027 set_entity_additional_property(ent, mtp_property_pure);
1029 if (decl->modifiers & DM_USED) {
1030 /* TRUE if the declaration includes the GNU
1031 __attribute__((used)) specifier. */
1032 set_entity_stickyness(ent, stickyness_sticky);
1037 * Creates an entity representing a function.
1039 * @param declaration the function declaration
1041 static ir_entity *get_function_entity(declaration_t *declaration)
1043 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
1044 return declaration->v.entity;
1045 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1047 symbol_t *symbol = declaration->symbol;
1048 ident *id = new_id_from_str(symbol->string);
1050 ir_type *global_type = get_glob_type();
1051 ir_type *ir_type_method = get_ir_type(declaration->type);
1052 assert(is_Method_type(ir_type_method));
1054 /* already an entity defined? */
1055 ir_entity *entity = entitymap_get(&entitymap, symbol);
1056 if (entity != NULL) {
1057 if (get_entity_visibility(entity) == visibility_external_allocated
1058 && declaration->init.statement != NULL) {
1059 set_entity_visibility(entity, visibility_external_visible);
1061 goto entity_created;
1064 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1065 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1066 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1068 handle_gnu_attributes_ent(entity, declaration);
1070 /* static inline => local
1071 * extern inline => local
1072 * inline without definition => local
1073 * inline with definition => external_visible */
1074 storage_class_tag_t const storage_class = declaration->storage_class;
1075 bool const is_inline = declaration->is_inline;
1076 bool const has_body = declaration->init.statement != NULL;
1077 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1078 set_entity_visibility(entity, visibility_external_visible);
1079 } else if (storage_class == STORAGE_CLASS_STATIC ||
1080 (is_inline && has_body)) {
1082 /* this entity was declared, but is defined nowhere */
1083 set_entity_peculiarity(entity, peculiarity_description);
1085 set_entity_visibility(entity, visibility_local);
1086 } else if (has_body) {
1087 set_entity_visibility(entity, visibility_external_visible);
1089 set_entity_visibility(entity, visibility_external_allocated);
1091 set_entity_allocation(entity, allocation_static);
1093 /* We should check for file scope here, but as long as we compile C only
1094 this is not needed. */
1095 if (! firm_opt.freestanding) {
1096 /* check for a known runtime function */
1097 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1098 if (id != rts_idents[i])
1101 /* ignore those rts functions not necessary needed for current mode */
1102 if ((c_mode & rts_data[i].flags) == 0)
1104 assert(rts_entities[rts_data[i].id] == NULL);
1105 rts_entities[rts_data[i].id] = entity;
1109 entitymap_insert(&entitymap, symbol, entity);
1112 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1113 declaration->v.entity = entity;
1119 * Creates a Const node representing a constant.
1121 static ir_node *const_to_firm(const const_expression_t *cnst)
1123 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1124 ir_mode *mode = get_ir_mode(cnst->base.type);
1129 if (mode_is_float(mode)) {
1130 tv = new_tarval_from_double(cnst->v.float_value, mode);
1132 if (mode_is_signed(mode)) {
1133 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1135 len = snprintf(buf, sizeof(buf), "%llu",
1136 (unsigned long long) cnst->v.int_value);
1138 tv = new_tarval_from_str(buf, len, mode);
1141 return new_d_Const(dbgi, mode, tv);
1145 * Creates a Const node representing a character constant.
1147 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1149 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1150 ir_mode *mode = get_ir_mode(cnst->base.type);
1152 long long int v = 0;
1153 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1154 if (char_is_signed) {
1155 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1157 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1161 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1162 tarval *tv = new_tarval_from_str(buf, len, mode);
1164 return new_d_Const(dbgi, mode, tv);
1168 * Creates a Const node representing a wide character constant.
1170 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1172 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1173 ir_mode *mode = get_ir_mode(cnst->base.type);
1175 long long int v = cnst->v.wide_character.begin[0];
1178 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1179 tarval *tv = new_tarval_from_str(buf, len, mode);
1181 return new_d_Const(dbgi, mode, tv);
1185 * Creates a SymConst for a given entity.
1187 * @param dbgi debug info
1188 * @param mode the (reference) mode for the SymConst
1189 * @param entity the entity
1191 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1194 assert(entity != NULL);
1195 union symconst_symbol sym;
1196 sym.entity_p = entity;
1197 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1201 * Creates a SymConst node representing a string constant.
1203 * @param src_pos the source position of the string constant
1204 * @param id_prefix a prefix for the name of the generated string constant
1205 * @param value the value of the string constant
1207 static ir_node *string_to_firm(const source_position_t *const src_pos,
1208 const char *const id_prefix,
1209 const string_t *const value)
1211 ir_type *const global_type = get_glob_type();
1212 dbg_info *const dbgi = get_dbg_info(src_pos);
1213 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1214 ir_type_const_char, dbgi);
1216 ident *const id = id_unique(id_prefix);
1217 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1218 set_entity_ld_ident(entity, id);
1219 set_entity_variability(entity, variability_constant);
1220 set_entity_allocation(entity, allocation_static);
1222 ir_type *const elem_type = ir_type_const_char;
1223 ir_mode *const mode = get_type_mode(elem_type);
1225 const char* const string = value->begin;
1226 const size_t slen = value->size;
1228 set_array_lower_bound_int(type, 0, 0);
1229 set_array_upper_bound_int(type, 0, slen);
1230 set_type_size_bytes(type, slen);
1231 set_type_state(type, layout_fixed);
1233 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1234 for(size_t i = 0; i < slen; ++i) {
1235 tvs[i] = new_tarval_from_long(string[i], mode);
1238 set_array_entity_values(entity, tvs, slen);
1241 return create_symconst(dbgi, mode_P_data, entity);
1245 * Creates a SymConst node representing a string literal.
1247 * @param literal the string literal
1249 static ir_node *string_literal_to_firm(
1250 const string_literal_expression_t* literal)
1252 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1257 * Creates a SymConst node representing a wide string literal.
1259 * @param literal the wide string literal
1261 static ir_node *wide_string_literal_to_firm(
1262 const wide_string_literal_expression_t* const literal)
1264 ir_type *const global_type = get_glob_type();
1265 ir_type *const elem_type = ir_type_wchar_t;
1266 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1267 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1270 ident *const id = id_unique("Lstr.%u");
1271 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1272 set_entity_ld_ident(entity, id);
1273 set_entity_variability(entity, variability_constant);
1274 set_entity_allocation(entity, allocation_static);
1276 ir_mode *const mode = get_type_mode(elem_type);
1278 const wchar_rep_t *const string = literal->value.begin;
1279 const size_t slen = literal->value.size;
1281 set_array_lower_bound_int(type, 0, 0);
1282 set_array_upper_bound_int(type, 0, slen);
1283 set_type_size_bytes(type, slen);
1284 set_type_state(type, layout_fixed);
1286 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1287 for(size_t i = 0; i < slen; ++i) {
1288 tvs[i] = new_tarval_from_long(string[i], mode);
1291 set_array_entity_values(entity, tvs, slen);
1294 return create_symconst(dbgi, mode_P_data, entity);
1297 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1298 ir_node *const addr)
1300 ir_type *irtype = get_ir_type(type);
1301 if (is_compound_type(irtype)
1302 || is_Method_type(irtype)
1303 || is_Array_type(irtype)) {
1307 ir_mode *const mode = get_type_mode(irtype);
1308 ir_node *const memory = get_store();
1309 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1310 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1311 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1313 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1314 set_Load_volatility(load, volatility_is_volatile);
1317 set_store(load_mem);
1322 * Creates a strict Conv if neccessary.
1324 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1326 ir_mode *mode = get_irn_mode(node);
1328 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1330 if (!mode_is_float(mode))
1333 /* check if there is already a Conv */
1334 if (is_Conv(node)) {
1335 /* convert it into a strict Conv */
1336 set_Conv_strict(node, 1);
1340 /* otherwise create a new one */
1341 return new_d_strictConv(dbgi, node, mode);
1344 static ir_node *get_global_var_address(dbg_info *const dbgi,
1345 const declaration_t *const decl)
1347 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1349 ir_entity *const entity = decl->v.entity;
1350 switch ((storage_class_tag_t)decl->storage_class) {
1351 case STORAGE_CLASS_THREAD:
1352 case STORAGE_CLASS_THREAD_EXTERN:
1353 case STORAGE_CLASS_THREAD_STATIC: {
1354 ir_node *const no_mem = new_NoMem();
1355 ir_node *const tls = get_irg_tls(current_ir_graph);
1356 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1360 return create_symconst(dbgi, mode_P_data, entity);
1365 * Returns the correct base address depending on whether it is a parameter or a
1366 * normal local variable.
1368 static ir_node *get_local_frame(ir_entity *const ent)
1370 ir_graph *const irg = current_ir_graph;
1371 const ir_type *const owner = get_entity_owner(ent);
1372 if (owner == get_irg_frame_type(irg)) {
1373 return get_irg_frame(irg);
1375 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1376 return get_irg_value_param_base(irg);
1380 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1382 ir_mode *value_mode = get_irn_mode(value);
1384 if (value_mode == dest_mode || is_Bad(value))
1387 if (dest_mode == mode_b) {
1388 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1389 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1390 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1394 return new_d_Conv(dbgi, value, dest_mode);
1397 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1399 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1400 declaration_t *declaration = ref->declaration;
1401 type_t *type = skip_typeref(declaration->type);
1403 /* make sure the type is constructed */
1404 (void) get_ir_type(type);
1406 switch((declaration_kind_t) declaration->declaration_kind) {
1407 case DECLARATION_KIND_TYPE:
1408 case DECLARATION_KIND_UNKNOWN:
1411 case DECLARATION_KIND_ENUM_ENTRY: {
1412 ir_mode *const mode = get_ir_mode(type);
1413 return new_Const(mode, declaration->v.enum_val);
1416 case DECLARATION_KIND_LOCAL_VARIABLE: {
1417 ir_mode *const mode = get_ir_mode(type);
1418 return get_value(declaration->v.value_number, mode);
1420 case DECLARATION_KIND_FUNCTION: {
1421 ir_mode *const mode = get_ir_mode(type);
1422 return create_symconst(dbgi, mode, declaration->v.entity);
1424 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1425 ir_node *const addr = get_global_var_address(dbgi, declaration);
1426 return deref_address(dbgi, declaration->type, addr);
1429 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1430 ir_entity *entity = declaration->v.entity;
1431 ir_node *frame = get_local_frame(entity);
1432 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1433 return deref_address(dbgi, declaration->type, sel);
1436 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1437 return declaration->v.vla_base;
1439 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1440 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1441 case DECLARATION_KIND_COMPOUND_MEMBER:
1442 case DECLARATION_KIND_LABEL_BLOCK:
1443 panic("not implemented reference type");
1446 panic("reference to declaration with unknown type found");
1449 static ir_node *reference_addr(const reference_expression_t *ref)
1451 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1452 declaration_t *declaration = ref->declaration;
1454 switch((declaration_kind_t) declaration->declaration_kind) {
1455 case DECLARATION_KIND_TYPE:
1456 case DECLARATION_KIND_UNKNOWN:
1458 case DECLARATION_KIND_LOCAL_VARIABLE:
1459 /* you can store to a local variable (so we don't panic but return NULL
1460 * as an indicator for no real address) */
1462 case DECLARATION_KIND_FUNCTION: {
1463 type_t *const type = skip_typeref(ref->base.type);
1464 ir_mode *const mode = get_ir_mode(type);
1465 return create_symconst(dbgi, mode, declaration->v.entity);
1467 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1468 ir_node *const addr = get_global_var_address(dbgi, declaration);
1471 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1472 ir_entity *entity = declaration->v.entity;
1473 ir_node *frame = get_local_frame(entity);
1474 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1479 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1480 return declaration->v.vla_base;
1482 case DECLARATION_KIND_ENUM_ENTRY:
1483 panic("trying to reference enum entry");
1485 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1486 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1487 case DECLARATION_KIND_COMPOUND_MEMBER:
1488 case DECLARATION_KIND_LABEL_BLOCK:
1489 panic("not implemented reference type");
1492 panic("reference to declaration with unknown type found");
1496 * Transform calls to builtin functions.
1498 static ir_node *process_builtin_call(const call_expression_t *call)
1500 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1502 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1503 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1505 type_t *type = skip_typeref(builtin->base.type);
1506 assert(is_type_pointer(type));
1508 type_t *function_type = skip_typeref(type->pointer.points_to);
1509 symbol_t *symbol = builtin->symbol;
1511 switch(symbol->ID) {
1512 case T___builtin_alloca: {
1513 if (call->arguments == NULL || call->arguments->next != NULL) {
1514 panic("invalid number of parameters on __builtin_alloca");
1516 expression_t *argument = call->arguments->expression;
1517 ir_node *size = expression_to_firm(argument);
1519 ir_node *store = get_store();
1520 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1522 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1524 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1528 case T___builtin_huge_val: {
1529 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1530 tarval *tv = get_mode_infinite(mode);
1531 ir_node *res = new_d_Const(dbgi, mode, tv);
1534 case T___builtin_nan:
1535 case T___builtin_nanf:
1536 case T___builtin_nand: {
1537 /* Ignore string for now... */
1538 assert(is_type_function(function_type));
1539 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1540 tarval *tv = get_mode_NAN(mode);
1541 ir_node *res = new_d_Const(dbgi, mode, tv);
1544 case T___builtin_va_end:
1547 panic("Unsupported builtin found\n");
1552 * Transform a call expression.
1553 * Handles some special cases, like alloca() calls, which must be resolved
1554 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1555 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1558 static ir_node *call_expression_to_firm(const call_expression_t *call)
1560 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1561 assert(get_cur_block() != NULL);
1563 expression_t *function = call->function;
1564 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1565 return process_builtin_call(call);
1567 if (function->kind == EXPR_REFERENCE) {
1568 const reference_expression_t *ref = &function->reference;
1569 declaration_t *declaration = ref->declaration;
1571 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1572 if (declaration->v.entity == rts_entities[rts_alloca]) {
1573 /* handle alloca() call */
1574 expression_t *argument = call->arguments->expression;
1575 ir_node *size = expression_to_firm(argument);
1577 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1579 ir_node *store = get_store();
1580 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1581 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1583 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1585 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1591 ir_node *callee = expression_to_firm(function);
1593 type_t *type = skip_typeref(function->base.type);
1594 assert(is_type_pointer(type));
1595 pointer_type_t *pointer_type = &type->pointer;
1596 type_t *points_to = skip_typeref(pointer_type->points_to);
1597 assert(is_type_function(points_to));
1598 function_type_t *function_type = &points_to->function;
1600 int n_parameters = 0;
1601 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1602 ir_type *new_method_type = NULL;
1603 if (function_type->variadic || function_type->unspecified_parameters) {
1604 const call_argument_t *argument = call->arguments;
1605 for( ; argument != NULL; argument = argument->next) {
1609 /* we need to construct a new method type matching the call
1611 int n_res = get_method_n_ress(ir_method_type);
1612 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1613 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1614 n_parameters, n_res, dbgi);
1615 set_method_calling_convention(new_method_type,
1616 get_method_calling_convention(ir_method_type));
1617 set_method_additional_properties(new_method_type,
1618 get_method_additional_properties(ir_method_type));
1619 set_method_variadicity(new_method_type,
1620 get_method_variadicity(ir_method_type));
1622 for(int i = 0; i < n_res; ++i) {
1623 set_method_res_type(new_method_type, i,
1624 get_method_res_type(ir_method_type, i));
1626 argument = call->arguments;
1627 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1628 expression_t *expression = argument->expression;
1629 ir_type *irtype = get_ir_type(expression->base.type);
1630 set_method_param_type(new_method_type, i, irtype);
1632 ir_method_type = new_method_type;
1634 n_parameters = get_method_n_params(ir_method_type);
1637 ir_node *in[n_parameters];
1639 const call_argument_t *argument = call->arguments;
1640 for(int n = 0; n < n_parameters; ++n) {
1641 expression_t *expression = argument->expression;
1642 ir_node *arg_node = expression_to_firm(expression);
1644 arg_node = do_strict_conv(dbgi, arg_node);
1648 argument = argument->next;
1651 ir_node *store = get_store();
1652 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1654 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1657 type_t *return_type = skip_typeref(function_type->return_type);
1658 ir_node *result = NULL;
1660 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1662 if (is_type_scalar(return_type)) {
1663 mode = get_ir_mode(return_type);
1667 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1668 result = new_d_Proj(dbgi, resproj, mode, 0);
1674 static void statement_to_firm(statement_t *statement);
1675 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1677 static ir_node *expression_to_addr(const expression_t *expression);
1678 static void create_condition_evaluation(const expression_t *expression,
1679 ir_node *true_block,
1680 ir_node *false_block);
1682 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1685 value = do_strict_conv(dbgi, value);
1687 ir_node *memory = get_store();
1689 if (is_type_scalar(type)) {
1690 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1691 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1692 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1693 set_Store_volatility(store, volatility_is_volatile);
1694 set_store(store_mem);
1696 ir_type *irtype = get_ir_type(type);
1697 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1698 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1699 set_store(copyb_mem);
1703 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1705 tarval *all_one = get_mode_all_one(mode);
1706 int mode_size = get_mode_size_bits(mode);
1708 assert(offset >= 0 && size >= 0);
1709 assert(offset + size <= mode_size);
1710 if (size == mode_size) {
1714 long shiftr = get_mode_size_bits(mode) - size;
1715 long shiftl = offset;
1716 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1717 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1718 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1719 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1724 static void bitfield_store_to_firm(const select_expression_t *expression,
1725 ir_node *addr, ir_node *value)
1727 type_t *type = expression->base.type;
1728 ir_mode *mode = get_ir_mode(type);
1730 assert(get_irn_mode(value) == mode || is_Bad(value));
1732 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1734 /* kill upper bits of value and shift to right position */
1735 ir_entity *entity = expression->compound_entry->v.entity;
1736 int bitoffset = get_entity_offset_bits_remainder(entity);
1737 ir_type *entity_type = get_entity_type(entity);
1738 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1740 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1741 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1742 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1743 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1744 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1745 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1747 /* load current value */
1748 ir_node *mem = get_store();
1749 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1750 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1751 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1752 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1753 tarval *inv_mask = tarval_not(shift_mask);
1754 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1755 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1757 /* construct new value and store */
1758 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1759 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1760 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1761 set_store(store_mem);
1763 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1764 set_Load_volatility(load, volatility_is_volatile);
1765 set_Store_volatility(store, volatility_is_volatile);
1769 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1772 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1773 type_t *type = expression->base.type;
1774 ir_mode *mode = get_ir_mode(type);
1775 ir_node *mem = get_store();
1776 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1777 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1778 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1780 load_res = create_conv(dbgi, load_res, mode_int);
1782 set_store(load_mem);
1784 /* kill upper bits */
1785 ir_entity *entity = expression->compound_entry->v.entity;
1786 int bitoffset = get_entity_offset_bits_remainder(entity);
1787 ir_type *entity_type = get_entity_type(entity);
1788 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1789 long shift_bitsl = machine_size - bitoffset - bitsize;
1790 assert(shift_bitsl >= 0);
1791 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1792 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1793 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1795 long shift_bitsr = bitoffset + shift_bitsl;
1796 assert(shift_bitsr <= (long) machine_size);
1797 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1798 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1800 if (mode_is_signed(mode)) {
1801 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1803 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1806 return create_conv(dbgi, shiftr, mode);
1809 static void set_value_for_expression_addr(const expression_t *expression,
1810 ir_node *value, ir_node *addr)
1812 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1813 value = do_strict_conv(dbgi, value);
1815 if (expression->kind == EXPR_REFERENCE) {
1816 const reference_expression_t *ref = &expression->reference;
1818 declaration_t *declaration = ref->declaration;
1819 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1820 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1821 set_value(declaration->v.value_number, value);
1827 addr = expression_to_addr(expression);
1829 type_t *type = skip_typeref(expression->base.type);
1831 if (expression->kind == EXPR_SELECT) {
1832 const select_expression_t *select = &expression->select;
1834 declaration_t *declaration = select->compound_entry;
1835 if (declaration->type->kind == TYPE_BITFIELD) {
1836 bitfield_store_to_firm(select, addr, value);
1841 assign_value(dbgi, addr, type, value);
1844 static void set_value_for_expression(const expression_t *expression,
1847 set_value_for_expression_addr(expression, value, NULL);
1850 static ir_node *get_value_from_lvalue(const expression_t *expression,
1853 if (expression->kind == EXPR_REFERENCE) {
1854 const reference_expression_t *ref = &expression->reference;
1856 declaration_t *declaration = ref->declaration;
1857 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1858 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1859 assert(addr == NULL);
1860 ir_mode *mode = get_ir_mode(expression->base.type);
1861 return get_value(declaration->v.value_number, mode);
1865 assert(addr != NULL);
1866 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1869 if (expression->kind == EXPR_SELECT &&
1870 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1871 value = bitfield_extract_to_firm(&expression->select, addr);
1873 value = deref_address(dbgi, expression->base.type, addr);
1880 static ir_node *create_incdec(const unary_expression_t *expression)
1882 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1883 const expression_t *value_expr = expression->value;
1884 ir_node *addr = expression_to_addr(value_expr);
1885 ir_node *value = get_value_from_lvalue(value_expr, addr);
1887 type_t *type = skip_typeref(expression->base.type);
1888 ir_mode *mode = get_ir_mode(expression->base.type);
1891 if (is_type_pointer(type)) {
1892 pointer_type_t *pointer_type = &type->pointer;
1893 offset = get_type_size(pointer_type->points_to);
1895 assert(is_type_arithmetic(type));
1896 offset = new_Const(mode, get_mode_one(mode));
1900 ir_node *store_value;
1901 switch(expression->base.kind) {
1902 case EXPR_UNARY_POSTFIX_INCREMENT:
1904 store_value = new_d_Add(dbgi, value, offset, mode);
1906 case EXPR_UNARY_POSTFIX_DECREMENT:
1908 store_value = new_d_Sub(dbgi, value, offset, mode);
1910 case EXPR_UNARY_PREFIX_INCREMENT:
1911 result = new_d_Add(dbgi, value, offset, mode);
1912 store_value = result;
1914 case EXPR_UNARY_PREFIX_DECREMENT:
1915 result = new_d_Sub(dbgi, value, offset, mode);
1916 store_value = result;
1919 panic("no incdec expr in create_incdec");
1922 set_value_for_expression_addr(value_expr, store_value, addr);
1927 static bool is_local_variable(expression_t *expression)
1929 if (expression->kind != EXPR_REFERENCE)
1931 reference_expression_t *ref_expr = &expression->reference;
1932 declaration_t *declaration = ref_expr->declaration;
1933 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1936 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1939 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1940 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1941 case EXPR_BINARY_NOTEQUAL:
1942 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1943 case EXPR_BINARY_ISLESS:
1944 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1945 case EXPR_BINARY_ISLESSEQUAL:
1946 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1947 case EXPR_BINARY_ISGREATER:
1948 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1949 case EXPR_BINARY_ISGREATEREQUAL:
1950 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1951 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1956 panic("trying to get pn_Cmp from non-comparison binexpr type");
1960 * Handle the assume optimizer hint: check if a Confirm
1961 * node can be created.
1963 * @param dbi debug info
1964 * @param expr the IL assume expression
1966 * we support here only some simple cases:
1971 static ir_node *handle_assume_compare(dbg_info *dbi,
1972 const binary_expression_t *expression)
1974 expression_t *op1 = expression->left;
1975 expression_t *op2 = expression->right;
1976 declaration_t *var2, *var = NULL;
1977 ir_node *res = NULL;
1980 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1982 if (is_local_variable(op1) && is_local_variable(op2)) {
1983 var = op1->reference.declaration;
1984 var2 = op2->reference.declaration;
1986 type_t *const type = skip_typeref(var->type);
1987 ir_mode *const mode = get_ir_mode(type);
1989 ir_node *const irn1 = get_value(var->v.value_number, mode);
1990 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1992 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1993 set_value(var2->v.value_number, res);
1995 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1996 set_value(var->v.value_number, res);
2002 if (is_local_variable(op1) && is_constant_expression(op2)) {
2003 var = op1->reference.declaration;
2005 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2006 cmp_val = get_inversed_pnc(cmp_val);
2007 var = op2->reference.declaration;
2012 type_t *const type = skip_typeref(var->type);
2013 ir_mode *const mode = get_ir_mode(type);
2015 res = get_value(var->v.value_number, mode);
2016 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2017 set_value(var->v.value_number, res);
2023 * Handle the assume optimizer hint.
2025 * @param dbi debug info
2026 * @param expr the IL assume expression
2028 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2029 switch(expression->kind) {
2030 case EXPR_BINARY_EQUAL:
2031 case EXPR_BINARY_NOTEQUAL:
2032 case EXPR_BINARY_LESS:
2033 case EXPR_BINARY_LESSEQUAL:
2034 case EXPR_BINARY_GREATER:
2035 case EXPR_BINARY_GREATEREQUAL:
2036 return handle_assume_compare(dbi, &expression->binary);
2042 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2044 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2045 type_t *type = skip_typeref(expression->base.type);
2047 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2048 return expression_to_addr(expression->value);
2050 const expression_t *value = expression->value;
2052 switch(expression->base.kind) {
2053 case EXPR_UNARY_NEGATE: {
2054 ir_node *value_node = expression_to_firm(value);
2055 ir_mode *mode = get_ir_mode(type);
2056 return new_d_Minus(dbgi, value_node, mode);
2058 case EXPR_UNARY_PLUS:
2059 return expression_to_firm(value);
2060 case EXPR_UNARY_BITWISE_NEGATE: {
2061 ir_node *value_node = expression_to_firm(value);
2062 ir_mode *mode = get_ir_mode(type);
2063 return new_d_Not(dbgi, value_node, mode);
2065 case EXPR_UNARY_NOT: {
2066 ir_node *value_node = _expression_to_firm(value);
2067 value_node = create_conv(dbgi, value_node, mode_b);
2068 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2071 case EXPR_UNARY_DEREFERENCE: {
2072 ir_node *value_node = expression_to_firm(value);
2073 type_t *value_type = skip_typeref(value->base.type);
2074 assert(is_type_pointer(value_type));
2075 type_t *points_to = value_type->pointer.points_to;
2076 return deref_address(dbgi, points_to, value_node);
2078 case EXPR_UNARY_POSTFIX_INCREMENT:
2079 case EXPR_UNARY_POSTFIX_DECREMENT:
2080 case EXPR_UNARY_PREFIX_INCREMENT:
2081 case EXPR_UNARY_PREFIX_DECREMENT:
2082 return create_incdec(expression);
2083 case EXPR_UNARY_CAST: {
2084 ir_node *value_node = expression_to_firm(value);
2085 if (is_type_scalar(type)) {
2086 ir_mode *mode = get_ir_mode(type);
2087 ir_node *node = create_conv(dbgi, value_node, mode);
2088 node = do_strict_conv(dbgi, node);
2091 /* make sure firm type is constructed */
2092 (void) get_ir_type(type);
2096 case EXPR_UNARY_CAST_IMPLICIT: {
2097 ir_node *value_node = expression_to_firm(value);
2098 if (is_type_scalar(type)) {
2099 ir_mode *mode = get_ir_mode(type);
2100 return create_conv(dbgi, value_node, mode);
2105 case EXPR_UNARY_ASSUME:
2106 if (firm_opt.confirm)
2107 return handle_assume(dbgi, value);
2114 panic("invalid UNEXPR type found");
2118 * produces a 0/1 depending of the value of a mode_b node
2120 static ir_node *produce_condition_result(const expression_t *expression,
2123 ir_mode *mode = get_ir_mode(expression->base.type);
2124 ir_node *cur_block = get_cur_block();
2126 ir_node *one_block = new_immBlock();
2127 ir_node *one = new_Const(mode, get_mode_one(mode));
2128 ir_node *jmp_one = new_d_Jmp(dbgi);
2130 ir_node *zero_block = new_immBlock();
2131 ir_node *zero = new_Const(mode, get_mode_null(mode));
2132 ir_node *jmp_zero = new_d_Jmp(dbgi);
2134 set_cur_block(cur_block);
2135 create_condition_evaluation(expression, one_block, zero_block);
2136 mature_immBlock(one_block);
2137 mature_immBlock(zero_block);
2139 ir_node *common_block = new_immBlock();
2140 add_immBlock_pred(common_block, jmp_one);
2141 add_immBlock_pred(common_block, jmp_zero);
2142 mature_immBlock(common_block);
2144 ir_node *in[2] = { one, zero };
2145 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2150 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2151 ir_node *value, type_t *type)
2153 pointer_type_t *const pointer_type = &type->pointer;
2154 type_t *const points_to = skip_typeref(pointer_type->points_to);
2155 unsigned elem_size = get_type_size_const(points_to);
2158 if (elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
2162 assert(elem_size >= 1);
2166 value = create_conv(dbgi, value, mode_int);
2167 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2168 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2172 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2173 ir_node *left, ir_node *right)
2176 type_t *type_left = skip_typeref(expression->left->base.type);
2177 type_t *type_right = skip_typeref(expression->right->base.type);
2179 expression_kind_t kind = expression->base.kind;
2182 case EXPR_BINARY_SHIFTLEFT:
2183 case EXPR_BINARY_SHIFTRIGHT:
2184 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2185 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2186 mode = get_irn_mode(left);
2187 right = create_conv(dbgi, right, mode_uint);
2190 case EXPR_BINARY_SUB:
2191 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2192 const pointer_type_t *const ptr_type = &type_left->pointer;
2194 mode = get_ir_mode(expression->base.type);
2195 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2196 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2197 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2198 ir_node *const no_mem = new_NoMem();
2199 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2200 mode, op_pin_state_floats);
2201 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2204 case EXPR_BINARY_SUB_ASSIGN:
2205 if (is_type_pointer(type_left)) {
2206 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2207 mode = get_ir_mode(type_left);
2212 case EXPR_BINARY_ADD:
2213 case EXPR_BINARY_ADD_ASSIGN:
2214 if (is_type_pointer(type_left)) {
2215 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2216 mode = get_ir_mode(type_left);
2218 } else if (is_type_pointer(type_right)) {
2219 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2220 mode = get_ir_mode(type_right);
2227 mode = get_irn_mode(right);
2228 left = create_conv(dbgi, left, mode);
2233 case EXPR_BINARY_ADD_ASSIGN:
2234 case EXPR_BINARY_ADD:
2235 return new_d_Add(dbgi, left, right, mode);
2236 case EXPR_BINARY_SUB_ASSIGN:
2237 case EXPR_BINARY_SUB:
2238 return new_d_Sub(dbgi, left, right, mode);
2239 case EXPR_BINARY_MUL_ASSIGN:
2240 case EXPR_BINARY_MUL:
2241 return new_d_Mul(dbgi, left, right, mode);
2242 case EXPR_BINARY_BITWISE_AND:
2243 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2244 return new_d_And(dbgi, left, right, mode);
2245 case EXPR_BINARY_BITWISE_OR:
2246 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2247 return new_d_Or(dbgi, left, right, mode);
2248 case EXPR_BINARY_BITWISE_XOR:
2249 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2250 return new_d_Eor(dbgi, left, right, mode);
2251 case EXPR_BINARY_SHIFTLEFT:
2252 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2253 return new_d_Shl(dbgi, left, right, mode);
2254 case EXPR_BINARY_SHIFTRIGHT:
2255 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2256 if (mode_is_signed(mode)) {
2257 return new_d_Shrs(dbgi, left, right, mode);
2259 return new_d_Shr(dbgi, left, right, mode);
2261 case EXPR_BINARY_DIV:
2262 case EXPR_BINARY_DIV_ASSIGN: {
2263 ir_node *pin = new_Pin(new_NoMem());
2266 if (mode_is_float(mode)) {
2267 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2268 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2270 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2271 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2275 case EXPR_BINARY_MOD:
2276 case EXPR_BINARY_MOD_ASSIGN: {
2277 ir_node *pin = new_Pin(new_NoMem());
2278 assert(!mode_is_float(mode));
2279 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2280 op_pin_state_floats);
2281 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2285 panic("unexpected expression kind");
2289 static ir_node *create_lazy_op(const binary_expression_t *expression)
2291 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2292 type_t *type = expression->base.type;
2293 ir_mode *mode = get_ir_mode(type);
2295 if (is_constant_expression(expression->left)) {
2296 long val = fold_constant(expression->left);
2297 expression_kind_t ekind = expression->base.kind;
2298 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
2299 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2300 return expression_to_firm(expression->right);
2302 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
2303 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
2304 return new_Const(mode, get_mode_one(mode));
2308 return produce_condition_result((const expression_t*) expression, dbgi);
2311 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2312 ir_node *right, ir_mode *mode);
2314 static ir_node *create_assign_binop(const binary_expression_t *expression)
2316 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2317 const expression_t *left_expr = expression->left;
2318 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2319 ir_node *left_addr = expression_to_addr(left_expr);
2320 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2321 ir_node *right = expression_to_firm(expression->right);
2322 ir_node *result = create_op(dbgi, expression, left, right);
2324 result = create_conv(dbgi, result, left_mode);
2325 result = do_strict_conv(dbgi, result);
2327 set_value_for_expression_addr(left_expr, result, left_addr);
2332 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2334 expression_kind_t kind = expression->base.kind;
2337 case EXPR_BINARY_EQUAL:
2338 case EXPR_BINARY_NOTEQUAL:
2339 case EXPR_BINARY_LESS:
2340 case EXPR_BINARY_LESSEQUAL:
2341 case EXPR_BINARY_GREATER:
2342 case EXPR_BINARY_GREATEREQUAL:
2343 case EXPR_BINARY_ISGREATER:
2344 case EXPR_BINARY_ISGREATEREQUAL:
2345 case EXPR_BINARY_ISLESS:
2346 case EXPR_BINARY_ISLESSEQUAL:
2347 case EXPR_BINARY_ISLESSGREATER:
2348 case EXPR_BINARY_ISUNORDERED: {
2349 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2350 ir_node *left = expression_to_firm(expression->left);
2351 ir_node *right = expression_to_firm(expression->right);
2352 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2353 long pnc = get_pnc(kind, expression->left->base.type);
2354 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2357 case EXPR_BINARY_ASSIGN: {
2358 ir_node *addr = expression_to_addr(expression->left);
2359 ir_node *right = expression_to_firm(expression->right);
2360 set_value_for_expression_addr(expression->left, right, addr);
2364 case EXPR_BINARY_ADD:
2365 case EXPR_BINARY_SUB:
2366 case EXPR_BINARY_MUL:
2367 case EXPR_BINARY_DIV:
2368 case EXPR_BINARY_MOD:
2369 case EXPR_BINARY_BITWISE_AND:
2370 case EXPR_BINARY_BITWISE_OR:
2371 case EXPR_BINARY_BITWISE_XOR:
2372 case EXPR_BINARY_SHIFTLEFT:
2373 case EXPR_BINARY_SHIFTRIGHT:
2375 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2376 ir_node *left = expression_to_firm(expression->left);
2377 ir_node *right = expression_to_firm(expression->right);
2378 return create_op(dbgi, expression, left, right);
2380 case EXPR_BINARY_LOGICAL_AND:
2381 case EXPR_BINARY_LOGICAL_OR:
2382 return create_lazy_op(expression);
2383 case EXPR_BINARY_COMMA:
2384 /* create side effects of left side */
2385 (void) expression_to_firm(expression->left);
2386 return _expression_to_firm(expression->right);
2388 case EXPR_BINARY_ADD_ASSIGN:
2389 case EXPR_BINARY_SUB_ASSIGN:
2390 case EXPR_BINARY_MUL_ASSIGN:
2391 case EXPR_BINARY_MOD_ASSIGN:
2392 case EXPR_BINARY_DIV_ASSIGN:
2393 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2394 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2395 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2396 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2397 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2398 return create_assign_binop(expression);
2399 case EXPR_BINARY_BUILTIN_EXPECT:
2400 return _expression_to_firm(expression->left);
2402 panic("TODO binexpr type");
2406 static ir_node *array_access_addr(const array_access_expression_t *expression)
2408 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2409 ir_node *base_addr = expression_to_firm(expression->array_ref);
2410 ir_node *offset = expression_to_firm(expression->index);
2412 type_t *offset_type = skip_typeref(expression->index->base.type);
2414 if (is_type_signed(offset_type)) {
2415 mode = get_ir_mode(type_ssize_t);
2417 mode = get_ir_mode(type_size_t);
2419 offset = create_conv(dbgi, offset, mode);
2421 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2422 assert(is_type_pointer(ref_type));
2423 pointer_type_t *pointer_type = &ref_type->pointer;
2425 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2426 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2427 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2429 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2434 static ir_node *array_access_to_firm(
2435 const array_access_expression_t *expression)
2437 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2438 ir_node *addr = array_access_addr(expression);
2439 type_t *type = revert_automatic_type_conversion(
2440 (const expression_t*) expression);
2441 type = skip_typeref(type);
2443 return deref_address(dbgi, type, addr);
2446 static long get_offsetof_offset(const offsetof_expression_t *expression)
2448 type_t *orig_type = expression->type;
2451 designator_t *designator = expression->designator;
2452 for( ; designator != NULL; designator = designator->next) {
2453 type_t *type = skip_typeref(orig_type);
2454 /* be sure the type is constructed */
2455 (void) get_ir_type(type);
2457 if (designator->symbol != NULL) {
2458 assert(is_type_compound(type));
2459 symbol_t *symbol = designator->symbol;
2461 declaration_t *declaration = type->compound.declaration;
2462 declaration_t *iter = declaration->scope.declarations;
2463 for( ; iter != NULL; iter = iter->next) {
2464 if (iter->symbol == symbol) {
2468 assert(iter != NULL);
2470 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2471 offset += get_entity_offset(iter->v.entity);
2473 orig_type = iter->type;
2475 expression_t *array_index = designator->array_index;
2476 assert(designator->array_index != NULL);
2477 assert(is_type_array(type));
2478 assert(is_type_valid(array_index->base.type));
2480 long index = fold_constant(array_index);
2481 ir_type *arr_type = get_ir_type(type);
2482 ir_type *elem_type = get_array_element_type(arr_type);
2483 long elem_size = get_type_size_bytes(elem_type);
2485 offset += index * elem_size;
2487 orig_type = type->array.element_type;
2494 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2496 ir_mode *mode = get_ir_mode(expression->base.type);
2497 long offset = get_offsetof_offset(expression);
2498 tarval *tv = new_tarval_from_long(offset, mode);
2499 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2501 return new_d_Const(dbgi, mode, tv);
2504 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2505 ir_entity *entity, type_t *type);
2507 static ir_node *compound_literal_to_firm(
2508 const compound_literal_expression_t *expression)
2510 type_t *type = expression->type;
2512 /* create an entity on the stack */
2513 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2515 ident *const id = id_unique("CompLit.%u");
2516 ir_type *const irtype = get_ir_type(type);
2517 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2518 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2519 set_entity_ld_ident(entity, id);
2521 set_entity_variability(entity, variability_uninitialized);
2523 /* create initialisation code */
2524 initializer_t *initializer = expression->initializer;
2525 create_local_initializer(initializer, dbgi, entity, type);
2527 /* create a sel for the compound literal address */
2528 ir_node *frame = get_local_frame(entity);
2529 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2534 * Transform a sizeof expression into Firm code.
2536 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2538 type_t *type = expression->type;
2540 type = expression->tp_expression->base.type;
2541 assert(type != NULL);
2544 type = skip_typeref(type);
2545 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2546 if (is_type_array(type) && type->array.is_vla
2547 && expression->tp_expression != NULL) {
2548 expression_to_firm(expression->tp_expression);
2551 return get_type_size(type);
2555 * Transform an alignof expression into Firm code.
2557 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2559 type_t *type = expression->type;
2561 /* beware: if expression is a variable reference, return the
2562 alignment of the variable. */
2563 const expression_t *tp_expression = expression->tp_expression;
2564 const declaration_t *declaration = expr_is_variable(tp_expression);
2565 if (declaration != NULL) {
2566 /* TODO: get the alignment of this variable. */
2568 type = tp_expression->base.type;
2569 assert(type != NULL);
2572 ir_mode *const mode = get_ir_mode(expression->base.type);
2573 symconst_symbol sym;
2574 sym.type_p = get_ir_type(type);
2575 return new_SymConst(mode, sym, symconst_type_align);
2578 static void init_ir_types(void);
2579 long fold_constant(const expression_t *expression)
2581 bool constant_folding_old = constant_folding;
2582 constant_folding = true;
2586 assert(is_constant_expression(expression));
2588 ir_graph *old_current_ir_graph = current_ir_graph;
2589 if (current_ir_graph == NULL) {
2590 current_ir_graph = get_const_code_irg();
2593 ir_node *cnst = expression_to_firm(expression);
2594 current_ir_graph = old_current_ir_graph;
2596 if (!is_Const(cnst)) {
2597 panic("couldn't fold constant\n");
2600 tarval *tv = get_Const_tarval(cnst);
2601 if (!tarval_is_long(tv)) {
2602 panic("result of constant folding is not integer\n");
2605 constant_folding = constant_folding_old;
2607 return get_tarval_long(tv);
2610 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2612 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2614 /* first try to fold a constant condition */
2615 if (is_constant_expression(expression->condition)) {
2616 long val = fold_constant(expression->condition);
2618 return expression_to_firm(expression->true_expression);
2620 return expression_to_firm(expression->false_expression);
2624 ir_node *cur_block = get_cur_block();
2626 /* create the true block */
2627 ir_node *true_block = new_immBlock();
2629 ir_node *true_val = expression_to_firm(expression->true_expression);
2630 ir_node *true_jmp = new_Jmp();
2632 /* create the false block */
2633 ir_node *false_block = new_immBlock();
2635 ir_node *false_val = expression_to_firm(expression->false_expression);
2636 ir_node *false_jmp = new_Jmp();
2638 /* create the condition evaluation */
2639 set_cur_block(cur_block);
2640 create_condition_evaluation(expression->condition, true_block, false_block);
2641 mature_immBlock(true_block);
2642 mature_immBlock(false_block);
2644 /* create the common block */
2645 ir_node *common_block = new_immBlock();
2646 add_immBlock_pred(common_block, true_jmp);
2647 add_immBlock_pred(common_block, false_jmp);
2648 mature_immBlock(common_block);
2650 /* TODO improve static semantics, so either both or no values are NULL */
2651 if (true_val == NULL || false_val == NULL)
2654 ir_node *in[2] = { true_val, false_val };
2655 ir_mode *mode = get_irn_mode(true_val);
2656 assert(get_irn_mode(false_val) == mode);
2657 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2663 * Returns an IR-node representing the address of a field.
2665 static ir_node *select_addr(const select_expression_t *expression)
2667 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2669 ir_node *compound_addr = expression_to_firm(expression->compound);
2671 /* make sure the type is constructed */
2672 type_t *type = skip_typeref(expression->compound->base.type);
2673 if (is_type_pointer(type)) {
2674 type = type->pointer.points_to;
2676 (void) get_ir_type(type);
2678 declaration_t *entry = expression->compound_entry;
2679 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2680 ir_entity *entity = entry->v.entity;
2682 assert(entity != NULL);
2684 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2689 static ir_node *select_to_firm(const select_expression_t *expression)
2691 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2692 ir_node *addr = select_addr(expression);
2693 type_t *type = revert_automatic_type_conversion(
2694 (const expression_t*) expression);
2695 type = skip_typeref(type);
2697 declaration_t *entry = expression->compound_entry;
2698 type_t *entry_type = skip_typeref(entry->type);
2700 if (entry_type->kind == TYPE_BITFIELD) {
2701 return bitfield_extract_to_firm(expression, addr);
2704 return deref_address(dbgi, type, addr);
2707 /* Values returned by __builtin_classify_type. */
2708 typedef enum gcc_type_class
2714 enumeral_type_class,
2717 reference_type_class,
2721 function_type_class,
2732 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2734 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2740 const atomic_type_t *const atomic_type = &type->atomic;
2741 switch (atomic_type->akind) {
2742 /* should not be reached */
2743 case ATOMIC_TYPE_INVALID:
2747 /* gcc cannot do that */
2748 case ATOMIC_TYPE_VOID:
2749 tc = void_type_class;
2752 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2753 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2754 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2755 case ATOMIC_TYPE_SHORT:
2756 case ATOMIC_TYPE_USHORT:
2757 case ATOMIC_TYPE_INT:
2758 case ATOMIC_TYPE_UINT:
2759 case ATOMIC_TYPE_LONG:
2760 case ATOMIC_TYPE_ULONG:
2761 case ATOMIC_TYPE_LONGLONG:
2762 case ATOMIC_TYPE_ULONGLONG:
2763 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2764 tc = integer_type_class;
2767 case ATOMIC_TYPE_FLOAT:
2768 case ATOMIC_TYPE_DOUBLE:
2769 case ATOMIC_TYPE_LONG_DOUBLE:
2770 tc = real_type_class;
2773 panic("Unexpected atomic type in classify_type_to_firm().");
2776 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2777 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2778 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2779 case TYPE_ARRAY: /* gcc handles this as pointer */
2780 case TYPE_FUNCTION: /* gcc handles this as pointer */
2781 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2782 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2783 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2785 /* gcc handles this as integer */
2786 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2789 /* typedef/typeof should be skipped already */
2796 panic("unexpected TYPE classify_type_to_firm().");
2799 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2800 ir_mode *const mode = mode_int;
2801 tarval *const tv = new_tarval_from_long(tc, mode);
2802 return new_d_Const(dbgi, mode, tv);
2805 static ir_node *function_name_to_firm(
2806 const funcname_expression_t *const expr)
2808 switch(expr->kind) {
2809 case FUNCNAME_FUNCTION:
2810 case FUNCNAME_PRETTY_FUNCTION:
2811 case FUNCNAME_FUNCDNAME:
2812 if (current_function_name == NULL) {
2813 const source_position_t *const src_pos = &expr->base.source_position;
2814 const char *const name = current_function_decl->symbol->string;
2815 const string_t string = { name, strlen(name) + 1 };
2816 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2818 return current_function_name;
2819 case FUNCNAME_FUNCSIG:
2820 if (current_funcsig == NULL) {
2821 const source_position_t *const src_pos = &expr->base.source_position;
2822 ir_entity *ent = get_irg_entity(current_ir_graph);
2823 const char *const name = get_entity_ld_name(ent);
2824 const string_t string = { name, strlen(name) + 1 };
2825 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2827 return current_funcsig;
2829 panic("Unsupported function name");
2832 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2834 statement_t *statement = expr->statement;
2836 assert(statement->kind == STATEMENT_COMPOUND);
2837 return compound_statement_to_firm(&statement->compound);
2840 static ir_node *va_start_expression_to_firm(
2841 const va_start_expression_t *const expr)
2843 ir_type *const method_type = get_ir_type(current_function_decl->type);
2844 int const n = get_method_n_params(method_type) - 1;
2845 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2846 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2847 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2848 ir_node *const no_mem = new_NoMem();
2849 ir_node *const arg_sel =
2850 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2852 ir_node *const cnst = get_type_size(expr->parameter->type);
2853 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2854 set_value_for_expression(expr->ap, add);
2859 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2861 type_t *const type = expr->base.type;
2862 expression_t *const ap_expr = expr->ap;
2863 ir_node *const ap_addr = expression_to_addr(ap_expr);
2864 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2865 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2866 ir_node *const res = deref_address(dbgi, type, ap);
2868 ir_node *const cnst = get_type_size(expr->base.type);
2869 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2871 set_value_for_expression_addr(ap_expr, add, ap_addr);
2876 static ir_node *dereference_addr(const unary_expression_t *const expression)
2878 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2879 return expression_to_firm(expression->value);
2883 * Returns a IR-node representing an lvalue of the given expression.
2885 static ir_node *expression_to_addr(const expression_t *expression)
2887 switch(expression->kind) {
2888 case EXPR_REFERENCE:
2889 return reference_addr(&expression->reference);
2890 case EXPR_ARRAY_ACCESS:
2891 return array_access_addr(&expression->array_access);
2893 return select_addr(&expression->select);
2895 return call_expression_to_firm(&expression->call);
2896 case EXPR_UNARY_DEREFERENCE: {
2897 return dereference_addr(&expression->unary);
2902 panic("trying to get address of non-lvalue");
2905 static ir_node *builtin_constant_to_firm(
2906 const builtin_constant_expression_t *expression)
2908 ir_mode *mode = get_ir_mode(expression->base.type);
2911 if (is_constant_expression(expression->value)) {
2916 return new_Const_long(mode, v);
2919 static ir_node *builtin_prefetch_to_firm(
2920 const builtin_prefetch_expression_t *expression)
2922 ir_node *adr = expression_to_firm(expression->adr);
2923 /* no Firm support for prefetch yet */
2929 * creates firm nodes for an expression. The difference between this function
2930 * and expression_to_firm is, that this version might produce mode_b nodes
2931 * instead of mode_Is.
2933 static ir_node *_expression_to_firm(const expression_t *expression)
2936 if (!constant_folding) {
2937 assert(!expression->base.transformed);
2938 ((expression_t*) expression)->base.transformed = true;
2942 switch(expression->kind) {
2943 case EXPR_CHARACTER_CONSTANT:
2944 return character_constant_to_firm(&expression->conste);
2945 case EXPR_WIDE_CHARACTER_CONSTANT:
2946 return wide_character_constant_to_firm(&expression->conste);
2948 return const_to_firm(&expression->conste);
2949 case EXPR_STRING_LITERAL:
2950 return string_literal_to_firm(&expression->string);
2951 case EXPR_WIDE_STRING_LITERAL:
2952 return wide_string_literal_to_firm(&expression->wide_string);
2953 case EXPR_REFERENCE:
2954 return reference_expression_to_firm(&expression->reference);
2956 return call_expression_to_firm(&expression->call);
2958 return unary_expression_to_firm(&expression->unary);
2960 return binary_expression_to_firm(&expression->binary);
2961 case EXPR_ARRAY_ACCESS:
2962 return array_access_to_firm(&expression->array_access);
2964 return sizeof_to_firm(&expression->typeprop);
2966 return alignof_to_firm(&expression->typeprop);
2967 case EXPR_CONDITIONAL:
2968 return conditional_to_firm(&expression->conditional);
2970 return select_to_firm(&expression->select);
2971 case EXPR_CLASSIFY_TYPE:
2972 return classify_type_to_firm(&expression->classify_type);
2974 return function_name_to_firm(&expression->funcname);
2975 case EXPR_STATEMENT:
2976 return statement_expression_to_firm(&expression->statement);
2978 return va_start_expression_to_firm(&expression->va_starte);
2980 return va_arg_expression_to_firm(&expression->va_arge);
2981 case EXPR_BUILTIN_SYMBOL:
2982 panic("unimplemented expression found");
2983 case EXPR_BUILTIN_CONSTANT_P:
2984 return builtin_constant_to_firm(&expression->builtin_constant);
2985 case EXPR_BUILTIN_PREFETCH:
2986 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2988 return offsetof_to_firm(&expression->offsetofe);
2989 case EXPR_COMPOUND_LITERAL:
2990 return compound_literal_to_firm(&expression->compound_literal);
2996 panic("invalid expression found");
2999 static bool produces_mode_b(const expression_t *expression)
3001 switch (expression->kind) {
3002 case EXPR_BINARY_EQUAL:
3003 case EXPR_BINARY_NOTEQUAL:
3004 case EXPR_BINARY_LESS:
3005 case EXPR_BINARY_LESSEQUAL:
3006 case EXPR_BINARY_GREATER:
3007 case EXPR_BINARY_GREATEREQUAL:
3008 case EXPR_BINARY_ISGREATER:
3009 case EXPR_BINARY_ISGREATEREQUAL:
3010 case EXPR_BINARY_ISLESS:
3011 case EXPR_BINARY_ISLESSEQUAL:
3012 case EXPR_BINARY_ISLESSGREATER:
3013 case EXPR_BINARY_ISUNORDERED:
3014 case EXPR_UNARY_NOT:
3017 case EXPR_BINARY_BUILTIN_EXPECT:
3018 return produces_mode_b(expression->binary.left);
3019 case EXPR_BINARY_COMMA:
3020 return produces_mode_b(expression->binary.right);
3027 static ir_node *expression_to_firm(const expression_t *expression)
3029 if (!produces_mode_b(expression)) {
3030 ir_node *res = _expression_to_firm(expression);
3031 assert(res == NULL || get_irn_mode(res) != mode_b);
3035 if (is_constant_expression(expression)) {
3036 ir_node *res = _expression_to_firm(expression);
3037 ir_mode *mode = get_ir_mode(expression->base.type);
3038 assert(is_Const(res));
3039 if (is_Const_null(res)) {
3040 return new_Const_long(mode, 0);
3042 return new_Const_long(mode, 1);
3046 /* we have to produce a 0/1 from the mode_b expression */
3047 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3048 return produce_condition_result(expression, dbgi);
3051 static ir_node *expression_to_modeb(const expression_t *expression)
3053 ir_node *res = _expression_to_firm(expression);
3054 res = create_conv(NULL, res, mode_b);
3060 * create a short-circuit expression evaluation that tries to construct
3061 * efficient control flow structures for &&, || and ! expressions
3063 static void create_condition_evaluation(const expression_t *expression,
3064 ir_node *true_block,
3065 ir_node *false_block)
3067 switch(expression->kind) {
3068 case EXPR_UNARY_NOT: {
3069 const unary_expression_t *unary_expression = &expression->unary;
3070 create_condition_evaluation(unary_expression->value, false_block,
3074 case EXPR_BINARY_LOGICAL_AND: {
3075 const binary_expression_t *binary_expression = &expression->binary;
3077 ir_node *cur_block = get_cur_block();
3078 ir_node *extra_block = new_immBlock();
3079 set_cur_block(cur_block);
3080 create_condition_evaluation(binary_expression->left, extra_block,
3082 mature_immBlock(extra_block);
3083 set_cur_block(extra_block);
3084 create_condition_evaluation(binary_expression->right, true_block,
3088 case EXPR_BINARY_LOGICAL_OR: {
3089 const binary_expression_t *binary_expression = &expression->binary;
3091 ir_node *cur_block = get_cur_block();
3092 ir_node *extra_block = new_immBlock();
3093 set_cur_block(cur_block);
3094 create_condition_evaluation(binary_expression->left, true_block,
3096 mature_immBlock(extra_block);
3097 set_cur_block(extra_block);
3098 create_condition_evaluation(binary_expression->right, true_block,
3106 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3107 ir_node *condition = expression_to_modeb(expression);
3108 ir_node *cond = new_d_Cond(dbgi, condition);
3109 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3110 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3112 /* set branch prediction info based on __builtin_expect */
3113 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3114 long cnst = fold_constant(expression->binary.right);
3115 cond_jmp_predicate pred;
3118 pred = COND_JMP_PRED_FALSE;
3120 pred = COND_JMP_PRED_TRUE;
3122 set_Cond_jmp_pred(cond, pred);
3125 add_immBlock_pred(true_block, true_proj);
3126 if (false_block != NULL) {
3127 add_immBlock_pred(false_block, false_proj);
3130 set_cur_block(NULL);
3135 static void create_declaration_entity(declaration_t *declaration,
3136 declaration_kind_t declaration_kind,
3137 ir_type *parent_type)
3139 type_t *const type = skip_typeref(declaration->type);
3140 if (is_type_function(type)) {
3141 (void) get_function_entity(declaration);
3145 ident *const id = new_id_from_str(declaration->symbol->string);
3146 ir_type *const irtype = get_ir_type(type);
3147 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3148 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3150 declaration->declaration_kind = (unsigned char) declaration_kind;
3151 declaration->v.entity = entity;
3152 set_entity_variability(entity, variability_uninitialized);
3153 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3154 if (parent_type == get_tls_type())
3155 set_entity_allocation(entity, allocation_automatic);
3156 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3157 set_entity_allocation(entity, allocation_static);
3159 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3160 set_entity_volatility(entity, volatility_is_volatile);
3165 typedef struct type_path_entry_t type_path_entry_t;
3166 struct type_path_entry_t {
3168 ir_initializer_t *initializer;
3170 declaration_t *compound_entry;
3173 typedef struct type_path_t type_path_t;
3174 struct type_path_t {
3175 type_path_entry_t *path;
3180 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3182 size_t len = ARR_LEN(path->path);
3184 for(size_t i = 0; i < len; ++i) {
3185 const type_path_entry_t *entry = & path->path[i];
3187 type_t *type = skip_typeref(entry->type);
3188 if (is_type_compound(type)) {
3189 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3190 } else if (is_type_array(type)) {
3191 fprintf(stderr, "[%zd]", entry->index);
3193 fprintf(stderr, "-INVALID-");
3196 fprintf(stderr, " (");
3197 print_type(path->top_type);
3198 fprintf(stderr, ")");
3201 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3203 size_t len = ARR_LEN(path->path);
3205 return & path->path[len-1];
3208 static type_path_entry_t *append_to_type_path(type_path_t *path)
3210 size_t len = ARR_LEN(path->path);
3211 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3213 type_path_entry_t *result = & path->path[len];
3214 memset(result, 0, sizeof(result[0]));
3218 static size_t get_compound_size(const compound_type_t *type)
3220 declaration_t *declaration = type->declaration;
3221 declaration_t *member = declaration->scope.declarations;
3223 for( ; member != NULL; member = member->next) {
3226 /* TODO: cache results? */
3231 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3233 type_t *orig_top_type = path->top_type;
3234 type_t *top_type = skip_typeref(orig_top_type);
3236 assert(is_type_compound(top_type) || is_type_array(top_type));
3238 if (ARR_LEN(path->path) == 0) {
3241 type_path_entry_t *top = get_type_path_top(path);
3242 ir_initializer_t *initializer = top->initializer;
3243 return get_initializer_compound_value(initializer, top->index);
3247 static void descend_into_subtype(type_path_t *path)
3249 type_t *orig_top_type = path->top_type;
3250 type_t *top_type = skip_typeref(orig_top_type);
3252 assert(is_type_compound(top_type) || is_type_array(top_type));
3254 ir_initializer_t *initializer = get_initializer_entry(path);
3256 type_path_entry_t *top = append_to_type_path(path);
3257 top->type = top_type;
3261 if (is_type_compound(top_type)) {
3262 declaration_t *declaration = top_type->compound.declaration;
3263 declaration_t *entry = declaration->scope.declarations;
3265 top->compound_entry = entry;
3267 len = get_compound_size(&top_type->compound);
3269 path->top_type = entry->type;
3271 assert(is_type_array(top_type));
3272 assert(top_type->array.size > 0);
3275 path->top_type = top_type->array.element_type;
3276 len = top_type->array.size;
3278 if (initializer == NULL
3279 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3280 initializer = create_initializer_compound(len);
3281 /* we have to set the entry at the 2nd latest path entry... */
3282 size_t path_len = ARR_LEN(path->path);
3283 assert(path_len >= 1);
3285 type_path_entry_t *entry = & path->path[path_len-2];
3286 ir_initializer_t *tinitializer = entry->initializer;
3287 set_initializer_compound_value(tinitializer, entry->index,
3291 top->initializer = initializer;
3294 static void ascend_from_subtype(type_path_t *path)
3296 type_path_entry_t *top = get_type_path_top(path);
3298 path->top_type = top->type;
3300 size_t len = ARR_LEN(path->path);
3301 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3304 static void walk_designator(type_path_t *path, const designator_t *designator)
3306 /* designators start at current object type */
3307 ARR_RESIZE(type_path_entry_t, path->path, 1);
3309 for( ; designator != NULL; designator = designator->next) {
3310 type_path_entry_t *top = get_type_path_top(path);
3311 type_t *orig_type = top->type;
3312 type_t *type = skip_typeref(orig_type);
3314 if (designator->symbol != NULL) {
3315 assert(is_type_compound(type));
3317 symbol_t *symbol = designator->symbol;
3319 declaration_t *declaration = type->compound.declaration;
3320 declaration_t *iter = declaration->scope.declarations;
3321 for( ; iter != NULL; iter = iter->next, ++index) {
3322 if (iter->symbol == symbol) {
3326 assert(iter != NULL);
3328 top->type = orig_type;
3329 top->compound_entry = iter;
3331 orig_type = iter->type;
3333 expression_t *array_index = designator->array_index;
3334 assert(designator->array_index != NULL);
3335 assert(is_type_array(type));
3336 assert(is_type_valid(array_index->base.type));
3338 long index = fold_constant(array_index);
3341 if (type->array.size_constant == 1) {
3342 long array_size = type->array.size;
3343 assert(index < array_size);
3347 top->type = orig_type;
3348 top->index = (size_t) index;
3349 orig_type = type->array.element_type;
3351 path->top_type = orig_type;
3353 if (designator->next != NULL) {
3354 descend_into_subtype(path);
3358 path->invalid = false;
3361 static void advance_current_object(type_path_t *path)
3363 if (path->invalid) {
3364 /* TODO: handle this... */
3365 panic("invalid initializer in ast2firm (excessive elements)");
3369 type_path_entry_t *top = get_type_path_top(path);
3371 type_t *type = skip_typeref(top->type);
3372 if (is_type_union(type)) {
3373 top->compound_entry = NULL;
3374 } else if (is_type_struct(type)) {
3375 declaration_t *entry = top->compound_entry;
3378 entry = entry->next;
3379 top->compound_entry = entry;
3380 if (entry != NULL) {
3381 path->top_type = entry->type;
3385 assert(is_type_array(type));
3388 if (!type->array.size_constant || top->index < type->array.size) {
3393 /* we're past the last member of the current sub-aggregate, try if we
3394 * can ascend in the type hierarchy and continue with another subobject */
3395 size_t len = ARR_LEN(path->path);
3398 ascend_from_subtype(path);
3399 advance_current_object(path);
3401 path->invalid = true;
3406 static ir_initializer_t *create_ir_initializer(
3407 const initializer_t *initializer, type_t *type);
3409 static ir_initializer_t *create_ir_initializer_value(
3410 const initializer_value_t *initializer)
3412 if (is_type_compound(initializer->value->base.type)) {
3413 panic("initializer creation for compounds not implemented yet");
3415 ir_node *value = expression_to_firm(initializer->value);
3416 return create_initializer_const(value);
3419 static ir_initializer_t *create_ir_initializer_list(
3420 const initializer_list_t *initializer, type_t *type)
3423 memset(&path, 0, sizeof(path));
3424 path.top_type = type;
3425 path.path = NEW_ARR_F(type_path_entry_t, 0);
3427 descend_into_subtype(&path);
3429 for(size_t i = 0; i < initializer->len; ++i) {
3430 const initializer_t *sub_initializer = initializer->initializers[i];
3432 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3433 walk_designator(&path, sub_initializer->designator.designator);
3437 if (sub_initializer->kind == INITIALIZER_VALUE) {
3438 /* we might have to descend into types until we're at a scalar
3441 type_t *orig_top_type = path.top_type;
3442 type_t *top_type = skip_typeref(orig_top_type);
3444 if (is_type_scalar(top_type))
3446 descend_into_subtype(&path);
3450 ir_initializer_t *sub_irinitializer
3451 = create_ir_initializer(sub_initializer, path.top_type);
3453 size_t path_len = ARR_LEN(path.path);
3454 assert(path_len >= 1);
3455 type_path_entry_t *entry = & path.path[path_len-1];
3456 ir_initializer_t *tinitializer = entry->initializer;
3457 set_initializer_compound_value(tinitializer, entry->index,
3460 advance_current_object(&path);
3463 assert(ARR_LEN(path.path) >= 1);
3464 ir_initializer_t *result = path.path[0].initializer;
3465 DEL_ARR_F(path.path);
3470 static ir_initializer_t *create_ir_initializer_string(
3471 const initializer_string_t *initializer, type_t *type)
3473 type = skip_typeref(type);
3475 size_t string_len = initializer->string.size;
3476 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3477 size_t len = type->array.size;
3478 ir_initializer_t *irinitializer = create_initializer_compound(len);
3480 const char *string = initializer->string.begin;
3481 ir_mode *mode = get_ir_mode(type->array.element_type);
3483 for(size_t i = 0; i < len; ++i) {
3488 tarval *tv = new_tarval_from_long(c, mode);
3489 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3491 set_initializer_compound_value(irinitializer, i, char_initializer);
3494 return irinitializer;
3497 static ir_initializer_t *create_ir_initializer_wide_string(
3498 const initializer_wide_string_t *initializer, type_t *type)
3500 size_t string_len = initializer->string.size;
3501 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3502 size_t len = type->array.size;
3503 ir_initializer_t *irinitializer = create_initializer_compound(len);
3505 const wchar_rep_t *string = initializer->string.begin;
3506 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3508 for(size_t i = 0; i < len; ++i) {
3510 if (i < string_len) {
3513 tarval *tv = new_tarval_from_long(c, mode);
3514 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3516 set_initializer_compound_value(irinitializer, i, char_initializer);
3519 return irinitializer;
3522 static ir_initializer_t *create_ir_initializer(
3523 const initializer_t *initializer, type_t *type)
3525 switch(initializer->kind) {
3526 case INITIALIZER_STRING:
3527 return create_ir_initializer_string(&initializer->string, type);
3529 case INITIALIZER_WIDE_STRING:
3530 return create_ir_initializer_wide_string(&initializer->wide_string,
3533 case INITIALIZER_LIST:
3534 return create_ir_initializer_list(&initializer->list, type);
3536 case INITIALIZER_VALUE:
3537 return create_ir_initializer_value(&initializer->value);
3539 case INITIALIZER_DESIGNATOR:
3540 panic("unexpected designator initializer found");
3542 panic("unknown initializer");
3545 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3548 if (is_atomic_type(type)) {
3549 ir_mode *mode = get_type_mode(type);
3550 tarval *zero = get_mode_null(mode);
3551 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3553 /* TODO: bitfields */
3554 ir_node *mem = get_store();
3555 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3556 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3559 assert(is_compound_type(type));
3562 if (is_Array_type(type)) {
3563 assert(has_array_upper_bound(type, 0));
3564 n_members = get_array_upper_bound_int(type, 0);
3566 n_members = get_compound_n_members(type);
3569 for(int i = 0; i < n_members; ++i) {
3572 if (is_Array_type(type)) {
3573 ir_entity *entity = get_array_element_entity(type);
3574 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3575 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3576 ir_node *in[1] = { cnst };
3577 irtype = get_array_element_type(type);
3578 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3580 ir_entity *member = get_compound_member(type, i);
3582 irtype = get_entity_type(member);
3583 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3586 create_dynamic_null_initializer(irtype, dbgi, addr);
3591 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3592 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3594 switch(get_initializer_kind(initializer)) {
3595 case IR_INITIALIZER_NULL: {
3596 create_dynamic_null_initializer(type, dbgi, base_addr);
3599 case IR_INITIALIZER_CONST: {
3600 ir_node *node = get_initializer_const_value(initializer);
3601 ir_mode *mode = get_irn_mode(node);
3602 assert(get_type_mode(type) == mode);
3604 /* TODO: bitfields... */
3605 ir_node *mem = get_store();
3606 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3607 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3611 case IR_INITIALIZER_TARVAL: {
3612 tarval *tv = get_initializer_tarval_value(initializer);
3613 ir_mode *mode = get_tarval_mode(tv);
3614 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3615 assert(get_type_mode(type) == mode);
3617 /* TODO: bitfields... */
3618 ir_node *mem = get_store();
3619 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3620 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3624 case IR_INITIALIZER_COMPOUND: {
3625 assert(is_compound_type(type));
3627 if (is_Array_type(type)) {
3628 assert(has_array_upper_bound(type, 0));
3629 n_members = get_array_upper_bound_int(type, 0);
3631 n_members = get_compound_n_members(type);
3634 if (get_initializer_compound_n_entries(initializer)
3635 != (unsigned) n_members)
3636 panic("initializer doesn't match compound type");
3638 for(int i = 0; i < n_members; ++i) {
3641 if (is_Array_type(type)) {
3642 ir_entity *entity = get_array_element_entity(type);
3643 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3644 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3645 ir_node *in[1] = { cnst };
3646 irtype = get_array_element_type(type);
3647 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3649 ir_entity *member = get_compound_member(type, i);
3651 irtype = get_entity_type(member);
3652 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3655 ir_initializer_t *sub_init
3656 = get_initializer_compound_value(initializer, i);
3658 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3664 panic("invalid IR_INITIALIZER found");
3667 static void create_dynamic_initializer(ir_initializer_t *initializer,
3668 dbg_info *dbgi, ir_entity *entity)
3670 ir_node *frame = get_local_frame(entity);
3671 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3672 ir_type *type = get_entity_type(entity);
3674 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3677 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3678 ir_entity *entity, type_t *type)
3680 ir_node *memory = get_store();
3681 ir_node *nomem = new_NoMem();
3682 ir_node *frame = get_irg_frame(current_ir_graph);
3683 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3685 if (initializer->kind == INITIALIZER_VALUE) {
3686 initializer_value_t *initializer_value = &initializer->value;
3688 ir_node *value = expression_to_firm(initializer_value->value);
3689 type = skip_typeref(type);
3690 assign_value(dbgi, addr, type, value);
3694 if (!is_constant_initializer(initializer)) {
3695 ir_initializer_t *irinitializer
3696 = create_ir_initializer(initializer, type);
3698 create_dynamic_initializer(irinitializer, dbgi, entity);
3702 /* create the ir_initializer */
3703 ir_graph *const old_current_ir_graph = current_ir_graph;
3704 current_ir_graph = get_const_code_irg();
3706 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3708 assert(current_ir_graph == get_const_code_irg());
3709 current_ir_graph = old_current_ir_graph;
3711 /* create a "template" entity which is copied to the entity on the stack */
3712 ident *const id = id_unique("initializer.%u");
3713 ir_type *const irtype = get_ir_type(type);
3714 ir_type *const global_type = get_glob_type();
3715 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3716 set_entity_ld_ident(init_entity, id);
3718 set_entity_variability(init_entity, variability_initialized);
3719 set_entity_visibility(init_entity, visibility_local);
3720 set_entity_allocation(init_entity, allocation_static);
3722 set_entity_initializer(init_entity, irinitializer);
3724 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3725 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3727 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3728 set_store(copyb_mem);
3731 static void create_initializer_local_variable_entity(declaration_t *declaration)
3733 initializer_t *initializer = declaration->init.initializer;
3734 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3735 ir_entity *entity = declaration->v.entity;
3736 type_t *type = declaration->type;
3737 create_local_initializer(initializer, dbgi, entity, type);
3740 static void create_declaration_initializer(declaration_t *declaration)
3742 initializer_t *initializer = declaration->init.initializer;
3743 if (initializer == NULL)
3746 declaration_kind_t declaration_kind
3747 = (declaration_kind_t) declaration->declaration_kind;
3748 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3749 create_initializer_local_variable_entity(declaration);
3753 type_t *type = skip_typeref(declaration->type);
3755 if (initializer->kind == INITIALIZER_VALUE) {
3756 initializer_value_t *initializer_value = &initializer->value;
3758 = get_dbg_info(&declaration->source_position);
3760 ir_node *value = expression_to_firm(initializer_value->value);
3761 value = do_strict_conv(dbgi, value);
3763 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3764 set_value(declaration->v.value_number, value);
3766 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3768 ir_entity *entity = declaration->v.entity;
3770 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3771 set_entity_variability(entity, variability_constant);
3773 set_entity_variability(entity, variability_initialized);
3775 set_atomic_ent_value(entity, value);
3778 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3779 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3781 ir_entity *entity = declaration->v.entity;
3782 ir_initializer_t *irinitializer
3783 = create_ir_initializer(initializer, type);
3785 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3786 set_entity_variability(entity, variability_constant);
3788 set_entity_variability(entity, variability_initialized);
3790 set_entity_initializer(entity, irinitializer);
3794 static void create_variable_length_array(declaration_t *declaration)
3796 /* initializers are not allowed for VLAs */
3797 assert(declaration->init.initializer == NULL);
3799 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3800 declaration->v.vla_base = NULL;
3802 /* TODO: record VLA somewhere so we create the free node when we leave
3806 static void allocate_variable_length_array(declaration_t *declaration)
3808 /* initializers are not allowed for VLAs */
3809 assert(declaration->init.initializer == NULL);
3810 assert(get_cur_block() != NULL);
3812 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3813 type_t *type = declaration->type;
3814 ir_type *el_type = get_ir_type(type->array.element_type);
3816 /* make sure size_node is calculated */
3817 get_type_size(type);
3818 ir_node *elems = type->array.size_node;
3819 ir_node *mem = get_store();
3820 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3822 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3823 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3826 assert(declaration->declaration_kind
3827 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3828 declaration->v.vla_base = addr;
3832 * Creates a Firm local variable from a declaration.
3834 static void create_local_variable(declaration_t *declaration)
3836 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3838 bool needs_entity = declaration->address_taken;
3839 type_t *type = skip_typeref(declaration->type);
3841 /* is it a variable length array? */
3842 if (is_type_array(type) && !type->array.size_constant) {
3843 create_variable_length_array(declaration);
3845 } else if (is_type_array(type) || is_type_compound(type)) {
3846 needs_entity = true;
3847 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3848 needs_entity = true;
3852 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3853 create_declaration_entity(declaration,
3854 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3857 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3858 declaration->v.value_number = next_value_number_function;
3859 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3860 ++next_value_number_function;
3864 static void create_local_static_variable(declaration_t *declaration)
3866 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3868 type_t *const type = skip_typeref(declaration->type);
3869 ir_type *const global_type = get_glob_type();
3870 ir_type *const irtype = get_ir_type(type);
3871 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3873 size_t l = strlen(declaration->symbol->string);
3874 char buf[l + sizeof(".%u")];
3875 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3876 ident *const id = id_unique(buf);
3878 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3880 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3881 set_entity_volatility(entity, volatility_is_volatile);
3884 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3885 declaration->v.entity = entity;
3886 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3887 set_entity_variability(entity, variability_uninitialized);
3888 set_entity_visibility(entity, visibility_local);
3889 set_entity_allocation(entity, allocation_static);
3891 ir_graph *const old_current_ir_graph = current_ir_graph;
3892 current_ir_graph = get_const_code_irg();
3894 create_declaration_initializer(declaration);
3896 assert(current_ir_graph == get_const_code_irg());
3897 current_ir_graph = old_current_ir_graph;
3902 static void return_statement_to_firm(return_statement_t *statement)
3904 if (get_cur_block() == NULL)
3907 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3908 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3913 if (get_method_n_ress(func_irtype) > 0) {
3914 ir_type *res_type = get_method_res_type(func_irtype, 0);
3916 if (statement->value != NULL) {
3917 ir_node *node = expression_to_firm(statement->value);
3918 node = do_strict_conv(dbgi, node);
3922 if (is_compound_type(res_type)) {
3925 mode = get_type_mode(res_type);
3927 in[0] = new_Unknown(mode);
3931 /* build return_value for its side effects */
3932 if (statement->value != NULL) {
3933 expression_to_firm(statement->value);
3938 ir_node *store = get_store();
3939 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3941 ir_node *end_block = get_irg_end_block(current_ir_graph);
3942 add_immBlock_pred(end_block, ret);
3944 set_cur_block(NULL);
3947 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3949 if (get_cur_block() == NULL)
3952 return expression_to_firm(statement->expression);
3955 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3957 declaration_t *declaration = compound->scope.declarations;
3958 for( ; declaration != NULL; declaration = declaration->next) {
3959 create_local_declaration(declaration);
3962 ir_node *result = NULL;
3963 statement_t *statement = compound->statements;
3964 for( ; statement != NULL; statement = statement->base.next) {
3965 if (statement->base.next == NULL
3966 && statement->kind == STATEMENT_EXPRESSION) {
3967 result = expression_statement_to_firm(
3968 &statement->expression);
3971 statement_to_firm(statement);
3977 static void create_global_variable(declaration_t *declaration)
3981 switch ((storage_class_tag_t)declaration->storage_class) {
3982 case STORAGE_CLASS_STATIC:
3983 vis = visibility_local;
3986 case STORAGE_CLASS_EXTERN:
3987 vis = visibility_external_allocated;
3990 case STORAGE_CLASS_NONE:
3991 vis = visibility_external_visible;
3994 case STORAGE_CLASS_THREAD:
3995 vis = visibility_external_visible;
3998 case STORAGE_CLASS_THREAD_EXTERN:
3999 vis = visibility_external_allocated;
4002 case STORAGE_CLASS_THREAD_STATIC:
4003 vis = visibility_local;
4007 var_type = get_tls_type();
4011 var_type = get_glob_type();
4015 create_declaration_entity(declaration,
4016 DECLARATION_KIND_GLOBAL_VARIABLE,
4018 if (!is_type_function(skip_typeref(declaration->type))) {
4019 set_entity_visibility(declaration->v.entity, vis);
4024 case STORAGE_CLASS_TYPEDEF:
4025 case STORAGE_CLASS_AUTO:
4026 case STORAGE_CLASS_REGISTER:
4027 case STORAGE_CLASS_ENUM_ENTRY:
4030 panic("Invalid storage class for global variable");
4033 static void create_local_declaration(declaration_t *declaration)
4035 if (declaration->namespc != NAMESPACE_NORMAL)
4037 /* construct type */
4038 (void) get_ir_type(declaration->type);
4039 if (declaration->symbol == NULL) {
4043 type_t *type = skip_typeref(declaration->type);
4045 switch ((storage_class_tag_t) declaration->storage_class) {
4046 case STORAGE_CLASS_STATIC:
4047 create_local_static_variable(declaration);
4049 case STORAGE_CLASS_EXTERN:
4050 create_global_variable(declaration);
4051 create_declaration_initializer(declaration);
4053 case STORAGE_CLASS_NONE:
4054 case STORAGE_CLASS_AUTO:
4055 case STORAGE_CLASS_REGISTER:
4056 if (is_type_function(type)) {
4057 if (declaration->init.statement != NULL) {
4058 panic("nested functions not supported yet");
4060 get_function_entity(declaration);
4063 create_local_variable(declaration);
4066 case STORAGE_CLASS_ENUM_ENTRY:
4067 /* should already be handled */
4068 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4070 case STORAGE_CLASS_TYPEDEF:
4071 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4073 case STORAGE_CLASS_THREAD:
4074 case STORAGE_CLASS_THREAD_EXTERN:
4075 case STORAGE_CLASS_THREAD_STATIC:
4078 panic("invalid storage class found");
4081 static void initialize_local_declaration(declaration_t *declaration)
4083 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4086 switch ((declaration_kind_t) declaration->declaration_kind) {
4087 case DECLARATION_KIND_LOCAL_VARIABLE:
4088 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4089 create_declaration_initializer(declaration);
4092 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4093 allocate_variable_length_array(declaration);
4096 case DECLARATION_KIND_LABEL_BLOCK:
4097 case DECLARATION_KIND_COMPOUND_MEMBER:
4098 case DECLARATION_KIND_GLOBAL_VARIABLE:
4099 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4100 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4101 case DECLARATION_KIND_FUNCTION:
4102 case DECLARATION_KIND_TYPE:
4103 case DECLARATION_KIND_ENUM_ENTRY:
4106 case DECLARATION_KIND_UNKNOWN:
4107 panic("can't initialize unknwon declaration");
4109 panic("invalid declaration kind");
4112 static void declaration_statement_to_firm(declaration_statement_t *statement)
4114 declaration_t *declaration = statement->declarations_begin;
4115 declaration_t *end = statement->declarations_end->next;
4116 for( ; declaration != end; declaration = declaration->next) {
4117 if (declaration->namespc != NAMESPACE_NORMAL)
4119 initialize_local_declaration(declaration);
4123 static void if_statement_to_firm(if_statement_t *statement)
4125 ir_node *cur_block = get_cur_block();
4127 ir_node *fallthrough_block = NULL;
4129 /* the true (blocks) */
4130 ir_node *true_block = NULL;
4131 if (statement->true_statement != NULL) {
4132 true_block = new_immBlock();
4133 statement_to_firm(statement->true_statement);
4134 if (get_cur_block() != NULL) {
4135 ir_node *jmp = new_Jmp();
4136 if (fallthrough_block == NULL)
4137 fallthrough_block = new_immBlock();
4138 add_immBlock_pred(fallthrough_block, jmp);
4142 /* the false (blocks) */
4143 ir_node *false_block = NULL;
4144 if (statement->false_statement != NULL) {
4145 false_block = new_immBlock();
4147 statement_to_firm(statement->false_statement);
4148 if (get_cur_block() != NULL) {
4149 ir_node *jmp = new_Jmp();
4150 if (fallthrough_block == NULL)
4151 fallthrough_block = new_immBlock();
4152 add_immBlock_pred(fallthrough_block, jmp);
4156 /* create the condition */
4157 if (cur_block != NULL) {
4158 if (true_block == NULL || false_block == NULL) {
4159 if (fallthrough_block == NULL)
4160 fallthrough_block = new_immBlock();
4161 if (true_block == NULL)
4162 true_block = fallthrough_block;
4163 if (false_block == NULL)
4164 false_block = fallthrough_block;
4167 set_cur_block(cur_block);
4168 create_condition_evaluation(statement->condition, true_block,
4172 mature_immBlock(true_block);
4173 if (false_block != fallthrough_block) {
4174 mature_immBlock(false_block);
4176 if (fallthrough_block != NULL) {
4177 mature_immBlock(fallthrough_block);
4180 set_cur_block(fallthrough_block);
4183 static void while_statement_to_firm(while_statement_t *statement)
4185 ir_node *jmp = NULL;
4186 if (get_cur_block() != NULL) {
4190 /* create the header block */
4191 ir_node *header_block = new_immBlock();
4193 add_immBlock_pred(header_block, jmp);
4197 ir_node *old_continue_label = continue_label;
4198 ir_node *old_break_label = break_label;
4199 continue_label = header_block;
4202 ir_node *body_block = new_immBlock();
4203 statement_to_firm(statement->body);
4204 ir_node *false_block = break_label;
4206 assert(continue_label == header_block);
4207 continue_label = old_continue_label;
4208 break_label = old_break_label;
4210 if (get_cur_block() != NULL) {
4212 add_immBlock_pred(header_block, jmp);
4215 /* shortcut for while(true) */
4216 if (is_constant_expression(statement->condition)
4217 && fold_constant(statement->condition) != 0) {
4218 set_cur_block(header_block);
4219 ir_node *header_jmp = new_Jmp();
4220 add_immBlock_pred(body_block, header_jmp);
4222 keep_alive(body_block);
4224 if (false_block == NULL) {
4225 false_block = new_immBlock();
4228 /* create the condition */
4229 set_cur_block(header_block);
4231 create_condition_evaluation(statement->condition, body_block,
4235 mature_immBlock(body_block);
4236 mature_immBlock(header_block);
4237 if (false_block != NULL) {
4238 mature_immBlock(false_block);
4241 set_cur_block(false_block);
4244 static void do_while_statement_to_firm(do_while_statement_t *statement)
4246 ir_node *jmp = NULL;
4247 if (get_cur_block() != NULL) {
4251 /* create the header block */
4252 ir_node *header_block = new_immBlock();
4255 ir_node *body_block = new_immBlock();
4257 add_immBlock_pred(body_block, jmp);
4260 ir_node *old_continue_label = continue_label;
4261 ir_node *old_break_label = break_label;
4262 continue_label = header_block;
4265 statement_to_firm(statement->body);
4266 ir_node *false_block = break_label;
4268 assert(continue_label == header_block);
4269 continue_label = old_continue_label;
4270 break_label = old_break_label;
4272 if (get_cur_block() != NULL) {
4273 ir_node *body_jmp = new_Jmp();
4274 add_immBlock_pred(header_block, body_jmp);
4275 mature_immBlock(header_block);
4278 if (false_block == NULL) {
4279 false_block = new_immBlock();
4282 /* create the condition */
4283 set_cur_block(header_block);
4285 create_condition_evaluation(statement->condition, body_block, false_block);
4286 mature_immBlock(body_block);
4287 mature_immBlock(header_block);
4288 if (false_block != NULL) {
4289 mature_immBlock(false_block);
4292 set_cur_block(false_block);
4295 static void for_statement_to_firm(for_statement_t *statement)
4297 ir_node *jmp = NULL;
4299 /* create declarations */
4300 declaration_t *declaration = statement->scope.declarations;
4301 for( ; declaration != NULL; declaration = declaration->next) {
4302 create_local_declaration(declaration);
4304 declaration = statement->scope.declarations;
4305 for( ; declaration != NULL; declaration = declaration->next) {
4306 initialize_local_declaration(declaration);
4309 if (get_cur_block() != NULL) {
4310 if (statement->initialisation != NULL) {
4311 expression_to_firm(statement->initialisation);
4318 /* create the step block */
4319 ir_node *const step_block = new_immBlock();
4320 if (statement->step != NULL) {
4321 expression_to_firm(statement->step);
4323 ir_node *const step_jmp = new_Jmp();
4325 /* create the header block */
4326 ir_node *const header_block = new_immBlock();
4328 add_immBlock_pred(header_block, jmp);
4330 add_immBlock_pred(header_block, step_jmp);
4332 /* the false block */
4333 ir_node *const false_block = new_immBlock();
4336 ir_node * body_block;
4337 if (statement->body != NULL) {
4338 ir_node *const old_continue_label = continue_label;
4339 ir_node *const old_break_label = break_label;
4340 continue_label = step_block;
4341 break_label = false_block;
4343 body_block = new_immBlock();
4344 statement_to_firm(statement->body);
4346 assert(continue_label == step_block);
4347 assert(break_label == false_block);
4348 continue_label = old_continue_label;
4349 break_label = old_break_label;
4351 if (get_cur_block() != NULL) {
4353 add_immBlock_pred(step_block, jmp);
4356 body_block = step_block;
4359 /* create the condition */
4360 set_cur_block(header_block);
4361 if (statement->condition != NULL) {
4362 create_condition_evaluation(statement->condition, body_block,
4365 keep_alive(header_block);
4367 add_immBlock_pred(body_block, jmp);
4370 mature_immBlock(body_block);
4371 mature_immBlock(false_block);
4372 mature_immBlock(step_block);
4373 mature_immBlock(header_block);
4374 mature_immBlock(false_block);
4376 set_cur_block(false_block);
4379 static void create_jump_statement(const statement_t *statement,
4380 ir_node *target_block)
4382 if (get_cur_block() == NULL)
4385 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4386 ir_node *jump = new_d_Jmp(dbgi);
4387 add_immBlock_pred(target_block, jump);
4389 set_cur_block(NULL);
4392 static ir_node *get_break_label(void)
4394 if (break_label == NULL) {
4395 ir_node *cur_block = get_cur_block();
4396 break_label = new_immBlock();
4397 set_cur_block(cur_block);
4402 static void switch_statement_to_firm(const switch_statement_t *statement)
4404 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4406 ir_node *expression = expression_to_firm(statement->expression);
4407 ir_node *cond = new_d_Cond(dbgi, expression);
4409 set_cur_block(NULL);
4411 ir_node *const old_switch_cond = current_switch_cond;
4412 ir_node *const old_break_label = break_label;
4413 const bool old_saw_default_label = saw_default_label;
4414 saw_default_label = false;
4415 current_switch_cond = cond;
4418 if (statement->body != NULL) {
4419 statement_to_firm(statement->body);
4422 if (get_cur_block() != NULL) {
4423 ir_node *jmp = new_Jmp();
4424 add_immBlock_pred(get_break_label(), jmp);
4427 if (!saw_default_label) {
4428 set_cur_block(get_nodes_block(cond));
4429 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4430 MAGIC_DEFAULT_PN_NUMBER);
4431 add_immBlock_pred(get_break_label(), proj);
4434 if (break_label != NULL) {
4435 mature_immBlock(break_label);
4437 set_cur_block(break_label);
4439 assert(current_switch_cond == cond);
4440 current_switch_cond = old_switch_cond;
4441 break_label = old_break_label;
4442 saw_default_label = old_saw_default_label;
4445 static void case_label_to_firm(const case_label_statement_t *statement)
4447 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4449 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4451 /* let's create a node and hope firm constant folding creates a Const
4454 ir_node *old_block = get_nodes_block(current_switch_cond);
4455 ir_node *block = new_immBlock();
4457 set_cur_block(old_block);
4458 if (statement->expression != NULL) {
4459 long start_pn = fold_constant(statement->expression);
4460 long end_pn = start_pn;
4461 if (statement->end_range != NULL) {
4462 end_pn = fold_constant(statement->end_range);
4464 assert(start_pn <= end_pn);
4465 /* create jumps for all cases in the given range */
4466 for (long pn = start_pn; pn <= end_pn; ++pn) {
4467 if (pn == MAGIC_DEFAULT_PN_NUMBER) {
4468 /* oops someone detected our cheating... */
4469 panic("magic default pn used");
4471 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4472 add_immBlock_pred(block, proj);
4475 saw_default_label = true;
4476 proj = new_d_defaultProj(dbgi, current_switch_cond,
4477 MAGIC_DEFAULT_PN_NUMBER);
4479 add_immBlock_pred(block, proj);
4482 if (fallthrough != NULL) {
4483 add_immBlock_pred(block, fallthrough);
4485 mature_immBlock(block);
4486 set_cur_block(block);
4488 if (statement->statement != NULL) {
4489 statement_to_firm(statement->statement);
4493 static ir_node *get_label_block(declaration_t *label)
4495 assert(label->namespc == NAMESPACE_LABEL);
4497 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4498 return label->v.block;
4500 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4502 ir_node *old_cur_block = get_cur_block();
4503 ir_node *block = new_immBlock();
4504 set_cur_block(old_cur_block);
4506 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4507 label->v.block = block;
4509 ARR_APP1(ir_node *, imature_blocks, block);
4514 static void label_to_firm(const label_statement_t *statement)
4516 ir_node *block = get_label_block(statement->label);
4518 if (get_cur_block() != NULL) {
4519 ir_node *jmp = new_Jmp();
4520 add_immBlock_pred(block, jmp);
4523 set_cur_block(block);
4526 if (statement->statement != NULL) {
4527 statement_to_firm(statement->statement);
4531 static void goto_to_firm(const goto_statement_t *statement)
4533 if (get_cur_block() == NULL)
4536 ir_node *block = get_label_block(statement->label);
4537 ir_node *jmp = new_Jmp();
4538 add_immBlock_pred(block, jmp);
4540 set_cur_block(NULL);
4543 static void asm_statement_to_firm(const asm_statement_t *statement)
4545 bool needs_memory = false;
4547 if (statement->is_volatile) {
4548 needs_memory = true;
4551 size_t n_clobbers = 0;
4552 asm_clobber_t *clobber = statement->clobbers;
4553 for( ; clobber != NULL; clobber = clobber->next) {
4554 const char *clobber_str = clobber->clobber.begin;
4556 if (!be_is_valid_clobber(clobber_str)) {
4557 errorf(&statement->base.source_position,
4558 "invalid clobber '%s' specified", clobber->clobber);
4562 if (strcmp(clobber_str, "memory") == 0) {
4563 needs_memory = true;
4567 ident *id = new_id_from_str(clobber_str);
4568 obstack_ptr_grow(&asm_obst, id);
4571 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4572 ident **clobbers = NULL;
4573 if (n_clobbers > 0) {
4574 clobbers = obstack_finish(&asm_obst);
4577 size_t n_inputs = 0;
4578 asm_argument_t *argument = statement->inputs;
4579 for ( ; argument != NULL; argument = argument->next)
4581 size_t n_outputs = 0;
4582 argument = statement->outputs;
4583 for ( ; argument != NULL; argument = argument->next)
4586 unsigned next_pos = 0;
4588 ir_node *ins[n_inputs + n_outputs + 1];
4591 ir_asm_constraint *tmp_in_constraints
4592 = xmalloc(n_outputs * sizeof(tmp_in_constraints[0]));
4594 const expression_t *out_exprs[n_outputs];
4595 ir_node *out_addrs[n_outputs];
4596 size_t out_size = 0;
4598 argument = statement->outputs;
4599 for ( ; argument != NULL; argument = argument->next) {
4600 const char *constraints = argument->constraints.begin;
4601 asm_constraint_flags_t asm_flags
4602 = be_parse_asm_constraints(constraints);
4604 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4605 errorf(&statement->base.source_position,
4606 "some constraints in '%s' are not supported", constraints);
4609 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4610 errorf(&statement->base.source_position,
4611 "some constraints in '%s' are invalid", constraints);
4614 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4615 errorf(&statement->base.source_position,
4616 "no write flag specified for output constraints '%s'",
4621 unsigned pos = next_pos++;
4622 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4623 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4624 expression_t *expr = argument->expression;
4625 ir_node *addr = expression_to_addr(expr);
4626 /* in+output, construct an artifical same_as constraint on the
4628 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4630 ir_node *value = get_value_from_lvalue(expr, addr);
4632 snprintf(buf, sizeof(buf), "%d", pos);
4634 ir_asm_constraint constraint;
4635 constraint.pos = pos;
4636 constraint.constraint = new_id_from_str(buf);
4637 constraint.mode = get_ir_mode(expr->base.type);
4638 tmp_in_constraints[in_size] = constraint;
4639 ins[in_size] = value;
4644 out_exprs[out_size] = expr;
4645 out_addrs[out_size] = addr;
4647 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4648 /* pure memory ops need no input (but we have to make sure we
4649 * attach to the memory) */
4650 assert(! (asm_flags &
4651 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4652 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4653 needs_memory = true;
4655 /* we need to attach the address to the inputs */
4656 expression_t *expr = argument->expression;
4658 ir_asm_constraint constraint;
4659 constraint.pos = pos;
4660 constraint.constraint = new_id_from_str(constraints);
4661 constraint.mode = NULL;
4662 tmp_in_constraints[in_size] = constraint;
4664 ins[in_size] = expression_to_addr(expr);
4668 errorf(&statement->base.source_position,
4669 "only modifiers but no place set in constraints '%s'",
4674 ir_asm_constraint constraint;
4675 constraint.pos = pos;
4676 constraint.constraint = new_id_from_str(constraints);
4677 constraint.mode = get_ir_mode(argument->expression->base.type);
4679 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4681 assert(obstack_object_size(&asm_obst)
4682 == out_size * sizeof(ir_asm_constraint));
4683 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4686 obstack_grow(&asm_obst, tmp_in_constraints,
4687 in_size * sizeof(tmp_in_constraints[0]));
4688 free(tmp_in_constraints);
4689 /* find and count input and output arguments */
4690 argument = statement->inputs;
4691 for( ; argument != NULL; argument = argument->next) {
4692 const char *constraints = argument->constraints.begin;
4693 asm_constraint_flags_t asm_flags
4694 = be_parse_asm_constraints(constraints);
4696 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4697 errorf(&statement->base.source_position,
4698 "some constraints in '%s' are not supported", constraints);
4701 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4702 errorf(&statement->base.source_position,
4703 "some constraints in '%s' are invalid", constraints);
4706 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4707 errorf(&statement->base.source_position,
4708 "write flag specified for input constraints '%s'",
4714 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4715 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4716 /* we can treat this as "normal" input */
4717 input = expression_to_firm(argument->expression);
4718 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4719 /* pure memory ops need no input (but we have to make sure we
4720 * attach to the memory) */
4721 assert(! (asm_flags &
4722 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4723 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4724 needs_memory = true;
4725 input = expression_to_addr(argument->expression);
4727 errorf(&statement->base.source_position,
4728 "only modifiers but no place set in constraints '%s'",
4733 ir_asm_constraint constraint;
4734 constraint.pos = next_pos++;
4735 constraint.constraint = new_id_from_str(constraints);
4736 constraint.mode = get_irn_mode(input);
4738 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4739 ins[in_size++] = input;
4743 ir_asm_constraint constraint;
4744 constraint.pos = next_pos++;
4745 constraint.constraint = new_id_from_str("");
4746 constraint.mode = mode_M;
4748 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4749 ins[in_size++] = get_store();
4752 assert(obstack_object_size(&asm_obst)
4753 == in_size * sizeof(ir_asm_constraint));
4754 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4756 /* create asm node */
4757 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4759 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4761 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4762 out_size, output_constraints,
4763 n_clobbers, clobbers, asm_text);
4765 if (statement->is_volatile) {
4766 set_irn_pinned(node, op_pin_state_pinned);
4768 set_irn_pinned(node, op_pin_state_floats);
4771 /* create output projs & connect them */
4773 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4778 for (i = 0; i < out_size; ++i) {
4779 const expression_t *out_expr = out_exprs[i];
4781 ir_mode *mode = get_ir_mode(out_expr->base.type);
4782 ir_node *proj = new_Proj(node, mode, pn);
4783 ir_node *addr = out_addrs[i];
4785 set_value_for_expression_addr(out_expr, proj, addr);
4789 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4790 statement_to_firm(statement->try_statement);
4791 warningf(&statement->base.source_position, "structured exception handling ignored");
4794 static void leave_statement_to_firm(leave_statement_t *statement) {
4795 errorf(&statement->base.source_position, "__leave not supported yet");
4799 * Transform a statement.
4801 static void statement_to_firm(statement_t *statement)
4804 assert(!statement->base.transformed);
4805 statement->base.transformed = true;
4808 switch(statement->kind) {
4809 case STATEMENT_INVALID:
4810 panic("invalid statement found");
4812 case STATEMENT_EMPTY:
4815 case STATEMENT_COMPOUND:
4816 compound_statement_to_firm(&statement->compound);
4818 case STATEMENT_RETURN:
4819 return_statement_to_firm(&statement->returns);
4821 case STATEMENT_EXPRESSION:
4822 expression_statement_to_firm(&statement->expression);
4825 if_statement_to_firm(&statement->ifs);
4827 case STATEMENT_WHILE:
4828 while_statement_to_firm(&statement->whiles);
4830 case STATEMENT_DO_WHILE:
4831 do_while_statement_to_firm(&statement->do_while);
4833 case STATEMENT_DECLARATION:
4834 declaration_statement_to_firm(&statement->declaration);
4836 case STATEMENT_BREAK:
4837 create_jump_statement(statement, get_break_label());
4839 case STATEMENT_CONTINUE:
4840 create_jump_statement(statement, continue_label);
4842 case STATEMENT_SWITCH:
4843 switch_statement_to_firm(&statement->switchs);
4845 case STATEMENT_CASE_LABEL:
4846 case_label_to_firm(&statement->case_label);
4849 for_statement_to_firm(&statement->fors);
4851 case STATEMENT_LABEL:
4852 label_to_firm(&statement->label);
4854 case STATEMENT_GOTO:
4855 goto_to_firm(&statement->gotos);
4858 asm_statement_to_firm(&statement->asms);
4860 case STATEMENT_MS_TRY:
4861 ms_try_statement_to_firm(&statement->ms_try);
4863 case STATEMENT_LEAVE:
4864 leave_statement_to_firm(&statement->leave);
4867 panic("Statement not implemented\n");
4870 static int count_decls_in_expression(const expression_t *expression);
4872 static int count_local_declarations(const declaration_t * decl,
4873 const declaration_t *const end)
4876 for (; decl != end; decl = decl->next) {
4877 if (decl->namespc != NAMESPACE_NORMAL)
4879 type_t *type = skip_typeref(decl->type);
4881 if (!decl->address_taken && is_type_scalar(type))
4883 const initializer_t *initializer = decl->init.initializer;
4884 /* FIXME: should walk initializer hierarchies... */
4885 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4886 count += count_decls_in_expression(initializer->value.value);
4892 static int count_decls_in_expression(const expression_t *expression) {
4895 if (expression == NULL)
4898 switch((expression_kind_t) expression->base.kind) {
4899 case EXPR_STATEMENT:
4900 return count_decls_in_stmts(expression->statement.statement);
4902 int count_left = count_decls_in_expression(expression->binary.left);
4903 int count_right = count_decls_in_expression(expression->binary.right);
4904 return count_left + count_right;
4907 return count_decls_in_expression(expression->unary.value);
4909 call_argument_t *argument = expression->call.arguments;
4910 for( ; argument != NULL; argument = argument->next) {
4911 count += count_decls_in_expression(argument->expression);
4918 panic("unexpected expression kind");
4920 case EXPR_COMPOUND_LITERAL:
4924 case EXPR_CONDITIONAL:
4925 count += count_decls_in_expression(expression->conditional.condition);
4926 count += count_decls_in_expression(expression->conditional.true_expression);
4927 count += count_decls_in_expression(expression->conditional.false_expression);
4930 case EXPR_BUILTIN_PREFETCH:
4931 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4932 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4933 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4936 case EXPR_BUILTIN_CONSTANT_P:
4937 count += count_decls_in_expression(expression->builtin_constant.value);
4941 count += count_decls_in_expression(expression->select.compound);
4944 case EXPR_ARRAY_ACCESS:
4945 count += count_decls_in_expression(expression->array_access.array_ref);
4946 count += count_decls_in_expression(expression->array_access.index);
4949 case EXPR_CLASSIFY_TYPE:
4950 count += count_decls_in_expression(expression->classify_type.type_expression);
4954 case EXPR_ALIGNOF: {
4955 expression_t *tp_expression = expression->typeprop.tp_expression;
4956 if (tp_expression != NULL) {
4957 count += count_decls_in_expression(tp_expression);
4963 case EXPR_REFERENCE:
4965 case EXPR_CHARACTER_CONSTANT:
4966 case EXPR_WIDE_CHARACTER_CONSTANT:
4967 case EXPR_STRING_LITERAL:
4968 case EXPR_WIDE_STRING_LITERAL:
4970 case EXPR_BUILTIN_SYMBOL:
4976 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4977 * (or implement all the missing expressions here/implement a walker)
4983 static int count_decls_in_stmts(const statement_t *stmt)
4986 for (; stmt != NULL; stmt = stmt->base.next) {
4987 switch (stmt->kind) {
4988 case STATEMENT_EMPTY:
4991 case STATEMENT_DECLARATION: {
4992 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4993 count += count_local_declarations(decl_stmt->declarations_begin,
4994 decl_stmt->declarations_end->next);
4998 case STATEMENT_COMPOUND: {
4999 const compound_statement_t *const comp =
5001 count += count_decls_in_stmts(comp->statements);
5005 case STATEMENT_IF: {
5006 const if_statement_t *const if_stmt = &stmt->ifs;
5007 count += count_decls_in_expression(if_stmt->condition);
5008 count += count_decls_in_stmts(if_stmt->true_statement);
5009 count += count_decls_in_stmts(if_stmt->false_statement);
5013 case STATEMENT_SWITCH: {
5014 const switch_statement_t *const switch_stmt = &stmt->switchs;
5015 count += count_decls_in_expression(switch_stmt->expression);
5016 count += count_decls_in_stmts(switch_stmt->body);
5020 case STATEMENT_LABEL: {
5021 const label_statement_t *const label_stmt = &stmt->label;
5022 if (label_stmt->statement != NULL) {
5023 count += count_decls_in_stmts(label_stmt->statement);
5028 case STATEMENT_WHILE: {
5029 const while_statement_t *const while_stmt = &stmt->whiles;
5030 count += count_decls_in_expression(while_stmt->condition);
5031 count += count_decls_in_stmts(while_stmt->body);
5035 case STATEMENT_DO_WHILE: {
5036 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5037 count += count_decls_in_expression(do_while_stmt->condition);
5038 count += count_decls_in_stmts(do_while_stmt->body);
5042 case STATEMENT_FOR: {
5043 const for_statement_t *const for_stmt = &stmt->fors;
5044 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5045 count += count_decls_in_expression(for_stmt->initialisation);
5046 count += count_decls_in_expression(for_stmt->condition);
5047 count += count_decls_in_expression(for_stmt->step);
5048 count += count_decls_in_stmts(for_stmt->body);
5052 case STATEMENT_CASE_LABEL: {
5053 const case_label_statement_t *label = &stmt->case_label;
5054 count += count_decls_in_expression(label->expression);
5055 if (label->statement != NULL) {
5056 count += count_decls_in_stmts(label->statement);
5062 case STATEMENT_BREAK:
5063 case STATEMENT_CONTINUE:
5066 case STATEMENT_EXPRESSION: {
5067 const expression_statement_t *expr_stmt = &stmt->expression;
5068 count += count_decls_in_expression(expr_stmt->expression);
5072 case STATEMENT_GOTO:
5073 case STATEMENT_LEAVE:
5074 case STATEMENT_INVALID:
5077 case STATEMENT_RETURN: {
5078 const return_statement_t *ret_stmt = &stmt->returns;
5079 count += count_decls_in_expression(ret_stmt->value);
5083 case STATEMENT_MS_TRY: {
5084 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5085 count += count_decls_in_stmts(try_stmt->try_statement);
5086 if (try_stmt->except_expression != NULL)
5087 count += count_decls_in_expression(try_stmt->except_expression);
5088 count += count_decls_in_stmts(try_stmt->final_statement);
5096 static int get_function_n_local_vars(declaration_t *declaration)
5100 /* count parameters */
5101 count += count_local_declarations(declaration->scope.declarations, NULL);
5103 /* count local variables declared in body */
5104 count += count_decls_in_stmts(declaration->init.statement);
5109 static void initialize_function_parameters(declaration_t *declaration)
5111 ir_graph *irg = current_ir_graph;
5112 ir_node *args = get_irg_args(irg);
5113 ir_node *start_block = get_irg_start_block(irg);
5114 ir_type *function_irtype = get_ir_type(declaration->type);
5117 declaration_t *parameter = declaration->scope.declarations;
5118 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5119 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5120 type_t *type = skip_typeref(parameter->type);
5122 bool needs_entity = parameter->address_taken;
5123 assert(!is_type_array(type));
5124 if (is_type_compound(type)) {
5125 needs_entity = true;
5129 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5130 ident *id = new_id_from_str(parameter->symbol->string);
5131 set_entity_ident(entity, id);
5133 parameter->declaration_kind
5134 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5135 parameter->v.entity = entity;
5139 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5140 ir_mode *param_mode = get_type_mode(param_irtype);
5143 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5145 ir_mode *mode = get_ir_mode(type);
5146 value = create_conv(NULL, value, mode);
5147 value = do_strict_conv(NULL, value);
5149 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5150 parameter->v.value_number = next_value_number_function;
5151 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5152 ++next_value_number_function;
5154 set_value(parameter->v.value_number, value);
5159 * Handle additional decl modifiers for IR-graphs
5161 * @param irg the IR-graph
5162 * @param dec_modifiers additional modifiers
5164 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5166 if (decl_modifiers & DM_NORETURN) {
5167 /* TRUE if the declaration includes the Microsoft
5168 __declspec(noreturn) specifier. */
5169 set_irg_additional_property(irg, mtp_property_noreturn);
5171 if (decl_modifiers & DM_NOTHROW) {
5172 /* TRUE if the declaration includes the Microsoft
5173 __declspec(nothrow) specifier. */
5174 set_irg_additional_property(irg, mtp_property_nothrow);
5176 if (decl_modifiers & DM_NAKED) {
5177 /* TRUE if the declaration includes the Microsoft
5178 __declspec(naked) specifier. */
5179 set_irg_additional_property(irg, mtp_property_naked);
5181 if (decl_modifiers & DM_FORCEINLINE) {
5182 /* TRUE if the declaration includes the
5183 Microsoft __forceinline specifier. */
5184 set_irg_inline_property(irg, irg_inline_forced);
5186 if (decl_modifiers & DM_NOINLINE) {
5187 /* TRUE if the declaration includes the Microsoft
5188 __declspec(noinline) specifier. */
5189 set_irg_inline_property(irg, irg_inline_forbidden);
5193 static void add_function_pointer(ir_type *segment, ir_entity *method,
5194 const char *unique_template)
5196 ir_type *method_type = get_entity_type(method);
5197 ident *id = id_unique(unique_template);
5198 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5200 ident *ide = id_unique(unique_template);
5201 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5202 ir_graph *irg = get_const_code_irg();
5203 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5206 set_entity_compiler_generated(ptr, 1);
5207 set_entity_variability(ptr, variability_constant);
5208 set_atomic_ent_value(ptr, val);
5212 * Create code for a function.
5214 static void create_function(declaration_t *declaration)
5216 ir_entity *function_entity = get_function_entity(declaration);
5218 if (declaration->init.statement == NULL)
5221 if (declaration->modifiers & DM_CONSTRUCTOR) {
5222 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5223 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5225 if (declaration->modifiers & DM_DESTRUCTOR) {
5226 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5227 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5230 current_function_decl = declaration;
5231 current_function_name = NULL;
5232 current_funcsig = NULL;
5234 assert(imature_blocks == NULL);
5235 imature_blocks = NEW_ARR_F(ir_node*, 0);
5237 int n_local_vars = get_function_n_local_vars(declaration);
5238 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5240 set_irg_fp_model(irg, firm_opt.fp_model);
5241 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5242 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5244 ir_node *first_block = get_cur_block();
5246 /* set inline flags */
5247 if (declaration->is_inline)
5248 set_irg_inline_property(irg, irg_inline_recomended);
5249 handle_decl_modifier_irg(irg, declaration->modifiers);
5251 next_value_number_function = 0;
5252 initialize_function_parameters(declaration);
5254 statement_to_firm(declaration->init.statement);
5256 ir_node *end_block = get_irg_end_block(irg);
5258 /* do we have a return statement yet? */
5259 if (get_cur_block() != NULL) {
5260 type_t *type = skip_typeref(declaration->type);
5261 assert(is_type_function(type));
5262 const function_type_t *func_type = &type->function;
5263 const type_t *return_type
5264 = skip_typeref(func_type->return_type);
5267 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5268 ret = new_Return(get_store(), 0, NULL);
5271 if (is_type_scalar(return_type)) {
5272 mode = get_ir_mode(func_type->return_type);
5278 /* ยง5.1.2.2.3 main implicitly returns 0 */
5279 if (strcmp(declaration->symbol->string, "main") == 0) {
5280 in[0] = new_Const(mode, get_mode_null(mode));
5282 in[0] = new_Unknown(mode);
5283 if (warning.return_type) {
5284 warningf(&declaration->source_position,
5285 "missing return statement at end of non-void function '%Y'",
5286 declaration->symbol);
5289 ret = new_Return(get_store(), 1, in);
5291 add_immBlock_pred(end_block, ret);
5294 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
5295 mature_immBlock(imature_blocks[i]);
5297 DEL_ARR_F(imature_blocks);
5298 imature_blocks = NULL;
5300 mature_immBlock(first_block);
5301 mature_immBlock(end_block);
5303 irg_finalize_cons(irg);
5305 /* finalize the frame type */
5306 ir_type *frame_type = get_irg_frame_type(irg);
5307 int n = get_compound_n_members(frame_type);
5310 for(int i = 0; i < n; ++i) {
5311 ir_entity *entity = get_compound_member(frame_type, i);
5312 ir_type *entity_type = get_entity_type(entity);
5314 int align = get_type_alignment_bytes(entity_type);
5315 if (align > align_all)
5319 misalign = offset % align;
5321 offset += align - misalign;
5325 set_entity_offset(entity, offset);
5326 offset += get_type_size_bytes(entity_type);
5328 set_type_size_bytes(frame_type, offset);
5329 set_type_alignment_bytes(frame_type, align_all);
5334 static void scope_to_firm(scope_t *scope)
5336 /* first pass: create declarations */
5337 declaration_t *declaration = scope->declarations;
5338 for( ; declaration != NULL; declaration = declaration->next) {
5339 if (declaration->namespc != NAMESPACE_NORMAL)
5341 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5342 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5344 if (declaration->symbol == NULL)
5347 type_t *type = skip_typeref(declaration->type);
5348 if (is_type_function(type)) {
5349 get_function_entity(declaration);
5351 create_global_variable(declaration);
5355 /* second pass: create code/initializers */
5356 declaration = scope->declarations;
5357 for( ; declaration != NULL; declaration = declaration->next) {
5358 if (declaration->namespc != NAMESPACE_NORMAL)
5360 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5361 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5363 if (declaration->symbol == NULL)
5366 type_t *type = declaration->type;
5367 if (type->kind == TYPE_FUNCTION) {
5368 create_function(declaration);
5370 assert(declaration->declaration_kind
5371 == DECLARATION_KIND_GLOBAL_VARIABLE);
5372 current_ir_graph = get_const_code_irg();
5373 create_declaration_initializer(declaration);
5378 void init_ast2firm(void)
5380 obstack_init(&asm_obst);
5381 init_atomic_modes();
5383 id_underscore = new_id_from_chars("_", 1);
5384 id_imp = new_id_from_chars("__imp_", 6);
5386 /* OS option must be set to the backend */
5387 const char *s = "ia32-gasmode=linux";
5388 switch (firm_opt.os_support) {
5389 case OS_SUPPORT_MINGW:
5390 create_ld_ident = create_ld_ident_win32;
5391 s = "ia32-gasmode=mingw";
5393 case OS_SUPPORT_LINUX:
5394 create_ld_ident = create_ld_ident_linux_elf;
5395 s = "ia32-gasmode=elf";
5397 case OS_SUPPORT_MACHO:
5398 create_ld_ident = create_ld_ident_macho;
5399 s = "ia32-gasmode=macho";
5402 int res = firm_be_option(s);
5406 /* create idents for all known runtime functions */
5407 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5408 rts_idents[i] = new_id_from_str(rts_data[i].name);
5411 entitymap_init(&entitymap);
5414 static void init_ir_types(void)
5416 static int ir_types_initialized = 0;
5417 if (ir_types_initialized)
5419 ir_types_initialized = 1;
5421 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
5422 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5423 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5425 ir_type_int = get_ir_type(type_int);
5426 ir_type_const_char = get_ir_type(type_const_char);
5427 ir_type_wchar_t = get_ir_type(type_wchar_t);
5428 ir_type_void = get_ir_type(type_void);
5431 void exit_ast2firm(void)
5433 entitymap_destroy(&entitymap);
5434 obstack_free(&asm_obst, NULL);
5437 static void global_asm_to_firm(statement_t *s)
5439 for (; s != NULL; s = s->base.next) {
5440 assert(s->kind == STATEMENT_ASM);
5442 char const *const text = s->asms.asm_text.begin;
5443 size_t size = s->asms.asm_text.size;
5445 /* skip the last \0 */
5446 if (text[size - 1] == '\0')
5449 ident *const id = new_id_from_chars(text, size);
5454 void translation_unit_to_firm(translation_unit_t *unit)
5456 /* just to be sure */
5457 continue_label = NULL;
5459 current_switch_cond = NULL;
5463 scope_to_firm(&unit->scope);
5464 global_asm_to_firm(unit->global_asm);