2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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
27 #include <libfirm/firm.h>
28 #include <libfirm/adt/obst.h>
29 #include <libfirm/be.h>
33 #include "adt/error.h"
34 #include "adt/array.h"
42 #include "diagnostic.h"
43 #include "lang_features.h"
45 #include "type_hash.h"
47 #include "walk_statements.h"
50 #include "entitymap_t.h"
51 #include "driver/firm_opt.h"
52 #include "driver/firm_cmdline.h"
54 typedef struct trampoline_region trampoline_region;
55 struct trampoline_region {
56 ir_entity *function; /**< The function that is called by this trampoline */
57 ir_entity *region; /**< created region for the trampoline */
60 static const backend_params *be_params;
62 static ir_type *ir_type_char;
63 static ir_type *ir_type_const_char;
64 static ir_type *ir_type_wchar_t;
65 static ir_type *ir_type_void;
66 static ir_type *ir_type_int;
68 /* architecture specific floating point arithmetic mode (if any) */
69 static ir_mode *mode_float_arithmetic;
71 /* alignment of stack parameters */
72 static unsigned stack_param_align;
74 static int next_value_number_function;
75 static ir_node *continue_label;
76 static ir_node *break_label;
77 static ir_node *current_switch_cond;
78 static bool saw_default_label;
79 static label_t **all_labels;
80 static entity_t **inner_functions;
81 static ir_node *ijmp_list;
82 static bool constant_folding;
83 static bool initializer_use_bitfield_basetype;
85 extern bool have_const_functions;
87 static const entity_t *current_function_entity;
88 static ir_node *current_function_name;
89 static ir_node *current_funcsig;
90 static switch_statement_t *current_switch;
91 static ir_graph *current_function;
92 static translation_unit_t *current_translation_unit;
93 static trampoline_region *current_trampolines;
94 static ir_type *current_outer_frame;
95 static ir_type *current_outer_value_type;
96 static ir_node *current_static_link;
98 static entitymap_t entitymap;
100 static struct obstack asm_obst;
102 typedef enum declaration_kind_t {
103 DECLARATION_KIND_UNKNOWN,
104 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
105 DECLARATION_KIND_GLOBAL_VARIABLE,
106 DECLARATION_KIND_LOCAL_VARIABLE,
107 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
108 DECLARATION_KIND_PARAMETER,
109 DECLARATION_KIND_PARAMETER_ENTITY,
110 DECLARATION_KIND_FUNCTION,
111 DECLARATION_KIND_COMPOUND_MEMBER,
112 DECLARATION_KIND_INNER_FUNCTION
113 } declaration_kind_t;
115 static ir_mode *get_ir_mode_storage(type_t *type);
117 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
118 * int that it returns bigger modes for floating point on some platforms
119 * (x87 internally does arithemtic with 80bits)
121 static ir_mode *get_ir_mode_arithmetic(type_t *type);
123 static ir_type *get_ir_type_incomplete(type_t *type);
125 static void enqueue_inner_function(entity_t *entity)
127 if (inner_functions == NULL)
128 inner_functions = NEW_ARR_F(entity_t *, 0);
129 ARR_APP1(entity_t*, inner_functions, entity);
132 static ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
134 const entity_t *entity = get_irg_loc_description(irg, pos);
136 if (entity != NULL) {
137 warningf(&entity->base.source_position,
138 "%s '%#T' might be used uninitialized",
139 get_entity_kind_name(entity->kind),
140 entity->declaration.type, entity->base.symbol);
142 return new_r_Unknown(irg, mode);
145 static const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
147 const source_position_t *pos = (const source_position_t*) dbg;
152 return pos->input_name;
155 static dbg_info *get_dbg_info(const source_position_t *pos)
157 return (dbg_info*) pos;
160 static void dbg_print_type_dbg_info(char *buffer, size_t buffer_size,
161 const type_dbg_info *dbg)
164 print_to_buffer(buffer, buffer_size);
165 const type_t *type = (const type_t*) dbg;
167 finish_print_to_buffer();
170 static type_dbg_info *get_type_dbg_info_(const type_t *type)
172 return (type_dbg_info*) type;
175 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
177 static ir_mode *mode_int, *mode_uint;
179 static ir_node *_expression_to_firm(const expression_t *expression);
180 static ir_node *expression_to_firm(const expression_t *expression);
181 static void create_local_declaration(entity_t *entity);
183 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
185 unsigned flags = get_atomic_type_flags(kind);
186 unsigned size = get_atomic_type_size(kind);
187 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
188 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
191 unsigned bit_size = size * 8;
192 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
193 unsigned modulo_shift;
194 ir_mode_arithmetic arithmetic;
196 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
197 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
198 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
200 sort = irms_int_number;
201 arithmetic = irma_twos_complement;
202 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
204 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
205 snprintf(name, sizeof(name), "F%u", bit_size);
206 sort = irms_float_number;
207 arithmetic = irma_ieee754;
210 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
218 * Initialises the atomic modes depending on the machine size.
220 static void init_atomic_modes(void)
222 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
223 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
225 mode_int = atomic_modes[ATOMIC_TYPE_INT];
226 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
228 /* there's no real void type in firm */
229 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
231 /* initialize pointer modes */
233 ir_mode_sort sort = irms_reference;
234 unsigned bit_size = machine_size;
236 ir_mode_arithmetic arithmetic = irma_twos_complement;
237 unsigned modulo_shift
238 = bit_size < machine_size ? machine_size : bit_size;
240 snprintf(name, sizeof(name), "p%u", machine_size);
241 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
244 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
245 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
247 /* Hmm, pointers should be machine size */
248 set_modeP_data(ptr_mode);
249 set_modeP_code(ptr_mode);
252 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
254 assert(kind <= ATOMIC_TYPE_LAST);
255 return atomic_modes[kind];
258 static ir_node *get_vla_size(array_type_t *const type)
260 ir_node *size_node = type->size_node;
261 if (size_node == NULL) {
262 size_node = expression_to_firm(type->size_expression);
263 type->size_node = size_node;
269 * Return a node representing the size of a type.
271 static ir_node *get_type_size_node(type_t *type)
273 type = skip_typeref(type);
275 if (is_type_array(type) && type->array.is_vla) {
276 ir_node *size_node = get_vla_size(&type->array);
277 ir_node *elem_size = get_type_size_node(type->array.element_type);
278 ir_mode *mode = get_irn_mode(size_node);
279 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
283 ir_mode *mode = get_ir_mode_storage(type_size_t);
285 sym.type_p = get_ir_type(type);
286 return new_SymConst(mode, sym, symconst_type_size);
289 static unsigned count_parameters(const function_type_t *function_type)
293 function_parameter_t *parameter = function_type->parameters;
294 for ( ; parameter != NULL; parameter = parameter->next) {
302 * Creates a Firm type for an atomic type
304 static ir_type *create_atomic_type(atomic_type_kind_t akind, const type_t *type)
306 ir_mode *mode = atomic_modes[akind];
307 type_dbg_info *dbgi = get_type_dbg_info_(type);
308 ir_type *irtype = new_d_type_primitive(mode, dbgi);
309 il_alignment_t alignment = get_atomic_type_alignment(akind);
311 set_type_alignment_bytes(irtype, alignment);
317 * Creates a Firm type for a complex type
319 static ir_type *create_complex_type(const complex_type_t *type)
321 atomic_type_kind_t kind = type->akind;
322 ir_mode *mode = atomic_modes[kind];
323 ident *id = get_mode_ident(mode);
327 /* FIXME: finish the array */
332 * Creates a Firm type for an imaginary type
334 static ir_type *create_imaginary_type(imaginary_type_t *type)
336 return create_atomic_type(type->akind, (const type_t*) type);
340 * return type of a parameter (and take transparent union gnu extension into
343 static type_t *get_parameter_type(type_t *orig_type)
345 type_t *type = skip_typeref(orig_type);
346 if (is_type_union(type)
347 && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
348 compound_t *compound = type->compound.compound;
349 type = compound->members.entities->declaration.type;
355 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
357 type_t *return_type = skip_typeref(function_type->return_type);
359 int n_parameters = count_parameters(function_type)
360 + (for_closure ? 1 : 0);
361 int n_results = return_type == type_void ? 0 : 1;
362 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) function_type);
363 ir_type *irtype = new_d_type_method(n_parameters, n_results, dbgi);
365 if (return_type != type_void) {
366 ir_type *restype = get_ir_type(return_type);
367 set_method_res_type(irtype, 0, restype);
370 function_parameter_t *parameter = function_type->parameters;
373 ir_type *p_irtype = get_ir_type(type_void_ptr);
374 set_method_param_type(irtype, n, p_irtype);
377 for ( ; parameter != NULL; parameter = parameter->next) {
378 type_t *type = get_parameter_type(parameter->type);
379 ir_type *p_irtype = get_ir_type(type);
380 set_method_param_type(irtype, n, p_irtype);
384 bool is_variadic = function_type->variadic;
387 set_method_variadicity(irtype, variadicity_variadic);
389 unsigned cc = get_method_calling_convention(irtype);
390 switch (function_type->calling_convention) {
391 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
394 set_method_calling_convention(irtype, SET_CDECL(cc));
401 /* only non-variadic function can use stdcall, else use cdecl */
402 set_method_calling_convention(irtype, SET_STDCALL(cc));
408 /* only non-variadic function can use fastcall, else use cdecl */
409 set_method_calling_convention(irtype, SET_FASTCALL(cc));
413 /* Hmm, leave default, not accepted by the parser yet. */
418 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
423 static ir_type *create_pointer_type(pointer_type_t *type)
425 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
426 type_t *points_to = type->points_to;
427 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
428 ir_type *ir_type = new_d_type_pointer(ir_points_to, dbgi);
433 static ir_type *create_reference_type(reference_type_t *type)
435 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
436 type_t *refers_to = type->refers_to;
437 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
438 ir_type *ir_type = new_d_type_pointer(ir_refers_to, dbgi);
443 static ir_type *create_array_type(array_type_t *type)
445 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
446 type_t *element_type = type->element_type;
447 ir_type *ir_element_type = get_ir_type(element_type);
448 ir_type *ir_type = new_d_type_array(1, ir_element_type, dbgi);
450 const int align = get_type_alignment_bytes(ir_element_type);
451 set_type_alignment_bytes(ir_type, align);
453 if (type->size_constant) {
454 int n_elements = type->size;
456 set_array_bounds_int(ir_type, 0, 0, n_elements);
458 size_t elemsize = get_type_size_bytes(ir_element_type);
459 if (elemsize % align > 0) {
460 elemsize += align - (elemsize % align);
462 set_type_size_bytes(ir_type, n_elements * elemsize);
464 set_array_lower_bound_int(ir_type, 0, 0);
466 set_type_state(ir_type, layout_fixed);
472 * Return the signed integer type of size bits.
474 * @param size the size
476 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
480 static ir_mode *s_modes[64 + 1] = {NULL, };
484 if (size <= 0 || size > 64)
487 mode = s_modes[size];
491 snprintf(name, sizeof(name), "bf_I%u", size);
492 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
493 size <= 32 ? 32 : size );
494 s_modes[size] = mode;
497 type_dbg_info *dbgi = get_type_dbg_info_(type);
498 res = new_d_type_primitive(mode, dbgi);
499 set_primitive_base_type(res, base_tp);
505 * Return the unsigned integer type of size bits.
507 * @param size the size
509 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
513 static ir_mode *u_modes[64 + 1] = {NULL, };
517 if (size <= 0 || size > 64)
520 mode = u_modes[size];
524 snprintf(name, sizeof(name), "bf_U%u", size);
525 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
526 size <= 32 ? 32 : size );
527 u_modes[size] = mode;
530 type_dbg_info *dbgi = get_type_dbg_info_(type);
531 res = new_d_type_primitive(mode, dbgi);
532 set_primitive_base_type(res, base_tp);
537 static ir_type *create_bitfield_type(bitfield_type_t *const type)
539 type_t *base = skip_typeref(type->base_type);
540 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
541 ir_type *irbase = get_ir_type(base);
543 unsigned size = type->bit_size;
545 assert(!is_type_float(base));
546 if (is_type_signed(base)) {
547 return get_signed_int_type_for_bit_size(irbase, size,
548 (const type_t*) type);
550 return get_unsigned_int_type_for_bit_size(irbase, size,
551 (const type_t*) type);
555 #define INVALID_TYPE ((ir_type_ptr)-1)
558 COMPOUND_IS_STRUCT = false,
559 COMPOUND_IS_UNION = true
563 * Construct firm type from ast struct type.
565 static ir_type *create_compound_type(compound_type_t *type,
566 bool incomplete, bool is_union)
568 compound_t *compound = type->compound;
570 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
571 return compound->irtype;
574 symbol_t *symbol = compound->base.symbol;
576 if (symbol != NULL) {
577 id = new_id_from_str(symbol->string);
580 id = id_unique("__anonymous_union.%u");
582 id = id_unique("__anonymous_struct.%u");
588 irtype = new_type_union(id);
590 irtype = new_type_struct(id);
593 compound->irtype_complete = false;
594 compound->irtype = irtype;
600 layout_union_type(type);
602 layout_struct_type(type);
605 compound->irtype_complete = true;
607 entity_t *entry = compound->members.entities;
608 for ( ; entry != NULL; entry = entry->base.next) {
609 if (entry->kind != ENTITY_COMPOUND_MEMBER)
612 symbol_t *symbol = entry->base.symbol;
613 type_t *entry_type = entry->declaration.type;
615 if (symbol == NULL) {
616 /* anonymous bitfield member, skip */
617 if (entry_type->kind == TYPE_BITFIELD)
619 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
620 || entry_type->kind == TYPE_COMPOUND_UNION);
621 ident = id_unique("anon.%u");
623 ident = new_id_from_str(symbol->string);
626 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
628 ir_type *entry_irtype = get_ir_type(entry_type);
629 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
631 set_entity_offset(entity, entry->compound_member.offset);
632 set_entity_offset_bits_remainder(entity,
633 entry->compound_member.bit_offset);
635 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
636 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
637 entry->compound_member.entity = entity;
640 set_type_alignment_bytes(irtype, compound->alignment);
641 set_type_size_bytes(irtype, compound->size);
642 set_type_state(irtype, layout_fixed);
647 static ir_type *create_enum_type(enum_type_t *const type)
649 type->base.firm_type = ir_type_int;
651 ir_mode *const mode = mode_int;
652 ir_tarval *const one = get_mode_one(mode);
653 ir_tarval * tv_next = get_mode_null(mode);
655 bool constant_folding_old = constant_folding;
656 constant_folding = true;
658 enum_t *enume = type->enume;
659 entity_t *entry = enume->base.next;
660 for (; entry != NULL; entry = entry->base.next) {
661 if (entry->kind != ENTITY_ENUM_VALUE)
664 expression_t *const init = entry->enum_value.value;
666 ir_node *const cnst = expression_to_firm(init);
667 if (!is_Const(cnst)) {
668 panic("couldn't fold constant");
670 tv_next = get_Const_tarval(cnst);
672 entry->enum_value.tv = tv_next;
673 tv_next = tarval_add(tv_next, one);
676 constant_folding = constant_folding_old;
678 return create_atomic_type(type->akind, (const type_t*) type);
681 static ir_type *get_ir_type_incomplete(type_t *type)
683 assert(type != NULL);
684 type = skip_typeref(type);
686 if (type->base.firm_type != NULL) {
687 assert(type->base.firm_type != INVALID_TYPE);
688 return type->base.firm_type;
691 switch (type->kind) {
692 case TYPE_COMPOUND_STRUCT:
693 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
694 case TYPE_COMPOUND_UNION:
695 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
697 return get_ir_type(type);
701 ir_type *get_ir_type(type_t *type)
703 assert(type != NULL);
705 type = skip_typeref(type);
707 if (type->base.firm_type != NULL) {
708 assert(type->base.firm_type != INVALID_TYPE);
709 return type->base.firm_type;
712 ir_type *firm_type = NULL;
713 switch (type->kind) {
715 /* Happens while constant folding, when there was an error */
716 return create_atomic_type(ATOMIC_TYPE_VOID, NULL);
719 firm_type = create_atomic_type(type->atomic.akind, type);
722 firm_type = create_complex_type(&type->complex);
725 firm_type = create_imaginary_type(&type->imaginary);
728 firm_type = create_method_type(&type->function, false);
731 firm_type = create_pointer_type(&type->pointer);
734 firm_type = create_reference_type(&type->reference);
737 firm_type = create_array_type(&type->array);
739 case TYPE_COMPOUND_STRUCT:
740 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
742 case TYPE_COMPOUND_UNION:
743 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
746 firm_type = create_enum_type(&type->enumt);
749 firm_type = get_ir_type(type->builtin.real_type);
752 firm_type = create_bitfield_type(&type->bitfield);
760 if (firm_type == NULL)
761 panic("unknown type found");
763 type->base.firm_type = firm_type;
767 static ir_mode *get_ir_mode_storage(type_t *type)
769 ir_type *irtype = get_ir_type(type);
771 /* firm doesn't report a mode for arrays somehow... */
772 if (is_Array_type(irtype)) {
776 ir_mode *mode = get_type_mode(irtype);
777 assert(mode != NULL);
781 static ir_mode *get_ir_mode_arithmetic(type_t *type)
783 ir_mode *mode = get_ir_mode_storage(type);
784 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
785 return mode_float_arithmetic;
791 /** Names of the runtime functions. */
792 static const struct {
793 int id; /**< the rts id */
794 int n_res; /**< number of return values */
795 const char *name; /**< the name of the rts function */
796 int n_params; /**< number of parameters */
797 unsigned flags; /**< language flags */
799 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
800 { rts_abort, 0, "abort", 0, _C89 },
801 { rts_alloca, 1, "alloca", 1, _ALL },
802 { rts_abs, 1, "abs", 1, _C89 },
803 { rts_labs, 1, "labs", 1, _C89 },
804 { rts_llabs, 1, "llabs", 1, _C99 },
805 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
807 { rts_fabs, 1, "fabs", 1, _C89 },
808 { rts_sqrt, 1, "sqrt", 1, _C89 },
809 { rts_cbrt, 1, "cbrt", 1, _C99 },
810 { rts_exp, 1, "exp", 1, _C89 },
811 { rts_exp2, 1, "exp2", 1, _C89 },
812 { rts_exp10, 1, "exp10", 1, _GNUC },
813 { rts_log, 1, "log", 1, _C89 },
814 { rts_log2, 1, "log2", 1, _C89 },
815 { rts_log10, 1, "log10", 1, _C89 },
816 { rts_pow, 1, "pow", 2, _C89 },
817 { rts_sin, 1, "sin", 1, _C89 },
818 { rts_cos, 1, "cos", 1, _C89 },
819 { rts_tan, 1, "tan", 1, _C89 },
820 { rts_asin, 1, "asin", 1, _C89 },
821 { rts_acos, 1, "acos", 1, _C89 },
822 { rts_atan, 1, "atan", 1, _C89 },
823 { rts_sinh, 1, "sinh", 1, _C89 },
824 { rts_cosh, 1, "cosh", 1, _C89 },
825 { rts_tanh, 1, "tanh", 1, _C89 },
827 { rts_fabsf, 1, "fabsf", 1, _C99 },
828 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
829 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
830 { rts_expf, 1, "expf", 1, _C99 },
831 { rts_exp2f, 1, "exp2f", 1, _C99 },
832 { rts_exp10f, 1, "exp10f", 1, _GNUC },
833 { rts_logf, 1, "logf", 1, _C99 },
834 { rts_log2f, 1, "log2f", 1, _C99 },
835 { rts_log10f, 1, "log10f", 1, _C99 },
836 { rts_powf, 1, "powf", 2, _C99 },
837 { rts_sinf, 1, "sinf", 1, _C99 },
838 { rts_cosf, 1, "cosf", 1, _C99 },
839 { rts_tanf, 1, "tanf", 1, _C99 },
840 { rts_asinf, 1, "asinf", 1, _C99 },
841 { rts_acosf, 1, "acosf", 1, _C99 },
842 { rts_atanf, 1, "atanf", 1, _C99 },
843 { rts_sinhf, 1, "sinhf", 1, _C99 },
844 { rts_coshf, 1, "coshf", 1, _C99 },
845 { rts_tanhf, 1, "tanhf", 1, _C99 },
847 { rts_fabsl, 1, "fabsl", 1, _C99 },
848 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
849 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
850 { rts_expl, 1, "expl", 1, _C99 },
851 { rts_exp2l, 1, "exp2l", 1, _C99 },
852 { rts_exp10l, 1, "exp10l", 1, _GNUC },
853 { rts_logl, 1, "logl", 1, _C99 },
854 { rts_log2l, 1, "log2l", 1, _C99 },
855 { rts_log10l, 1, "log10l", 1, _C99 },
856 { rts_powl, 1, "powl", 2, _C99 },
857 { rts_sinl, 1, "sinl", 1, _C99 },
858 { rts_cosl, 1, "cosl", 1, _C99 },
859 { rts_tanl, 1, "tanl", 1, _C99 },
860 { rts_asinl, 1, "asinl", 1, _C99 },
861 { rts_acosl, 1, "acosl", 1, _C99 },
862 { rts_atanl, 1, "atanl", 1, _C99 },
863 { rts_sinhl, 1, "sinhl", 1, _C99 },
864 { rts_coshl, 1, "coshl", 1, _C99 },
865 { rts_tanhl, 1, "tanhl", 1, _C99 },
867 { rts_strcmp, 1, "strcmp", 2, _C89 },
868 { rts_strncmp, 1, "strncmp", 3, _C89 },
869 { rts_strcpy, 1, "strcpy", 2, _C89 },
870 { rts_strlen, 1, "strlen", 1, _C89 },
871 { rts_memcpy, 1, "memcpy", 3, _C89 },
872 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
873 { rts_memmove, 1, "memmove", 3, _C89 },
874 { rts_memset, 1, "memset", 3, _C89 },
875 { rts_memcmp, 1, "memcmp", 3, _C89 },
878 static ident *rts_idents[lengthof(rts_data)];
880 static create_ld_ident_func create_ld_ident = create_name_linux_elf;
882 void set_create_ld_ident(ident *(*func)(entity_t*))
884 create_ld_ident = func;
888 * Handle GNU attributes for entities
890 * @param ent the entity
891 * @param decl the routine declaration
893 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
895 assert(is_declaration(entity));
896 decl_modifiers_t modifiers = entity->declaration.modifiers;
898 if (is_method_entity(irentity)) {
899 if (modifiers & DM_PURE) {
900 set_entity_additional_properties(irentity, mtp_property_pure);
902 if (modifiers & DM_CONST) {
903 add_entity_additional_properties(irentity, mtp_property_const);
904 have_const_functions = true;
907 if (modifiers & DM_USED) {
908 add_entity_linkage(irentity, IR_LINKAGE_HIDDEN_USER);
910 if (modifiers & DM_WEAK) {
911 add_entity_linkage(irentity, IR_LINKAGE_WEAK);
915 static bool is_main(entity_t *entity)
917 static symbol_t *sym_main = NULL;
918 if (sym_main == NULL) {
919 sym_main = symbol_table_insert("main");
922 if (entity->base.symbol != sym_main)
924 /* must be in outermost scope */
925 if (entity->base.parent_scope != ¤t_translation_unit->scope)
932 * Creates an entity representing a function.
934 * @param entity the function declaration/definition
935 * @param owner_type the owner type of this function, NULL
936 * for global functions
938 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
940 assert(entity->kind == ENTITY_FUNCTION);
941 if (entity->function.irentity != NULL) {
942 return entity->function.irentity;
945 entity_t *original_entity = entity;
946 if (entity->function.btk != bk_none) {
947 entity = get_builtin_replacement(entity);
952 if (is_main(entity)) {
953 /* force main to C linkage */
954 type_t *type = entity->declaration.type;
955 assert(is_type_function(type));
956 if (type->function.linkage != LINKAGE_C) {
957 type_t *new_type = duplicate_type(type);
958 new_type->function.linkage = LINKAGE_C;
959 type = identify_new_type(new_type);
960 entity->declaration.type = type;
964 symbol_t *symbol = entity->base.symbol;
965 ident *id = new_id_from_str(symbol->string);
967 /* already an entity defined? */
968 ir_entity *irentity = entitymap_get(&entitymap, symbol);
969 bool const has_body = entity->function.statement != NULL;
970 if (irentity != NULL) {
971 if (get_entity_visibility(irentity) == ir_visibility_external
973 set_entity_visibility(irentity, ir_visibility_default);
978 ir_type *ir_type_method;
979 if (entity->function.need_closure)
980 ir_type_method = create_method_type(&entity->declaration.type->function, true);
982 ir_type_method = get_ir_type(entity->declaration.type);
984 bool nested_function = false;
985 if (owner_type == NULL)
986 owner_type = get_glob_type();
988 nested_function = true;
990 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
991 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
995 ld_id = id_unique("inner.%u");
997 ld_id = create_ld_ident(entity);
998 set_entity_ld_ident(irentity, ld_id);
1000 handle_decl_modifiers(irentity, entity);
1002 if (! nested_function) {
1003 /* static inline => local
1004 * extern inline => local
1005 * inline without definition => local
1006 * inline with definition => external_visible */
1007 storage_class_tag_t const storage_class
1008 = (storage_class_tag_t) entity->declaration.storage_class;
1009 bool const is_inline = entity->function.is_inline;
1011 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1012 set_entity_visibility(irentity, ir_visibility_default);
1013 } else if (storage_class == STORAGE_CLASS_STATIC ||
1014 (is_inline && has_body)) {
1015 set_entity_visibility(irentity, ir_visibility_local);
1016 } else if (has_body) {
1017 set_entity_visibility(irentity, ir_visibility_default);
1019 set_entity_visibility(irentity, ir_visibility_external);
1022 /* nested functions are always local */
1023 set_entity_visibility(irentity, ir_visibility_local);
1026 /* We should check for file scope here, but as long as we compile C only
1027 this is not needed. */
1028 if (!freestanding && !has_body) {
1029 /* check for a known runtime function */
1030 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1031 if (id != rts_idents[i])
1034 function_type_t *function_type
1035 = &entity->declaration.type->function;
1036 /* rts_entities code can't handle a "wrong" number of parameters */
1037 if (function_type->unspecified_parameters)
1040 /* check number of parameters */
1041 int n_params = count_parameters(function_type);
1042 if (n_params != rts_data[i].n_params)
1045 type_t *return_type = skip_typeref(function_type->return_type);
1046 int n_res = return_type != type_void ? 1 : 0;
1047 if (n_res != rts_data[i].n_res)
1050 /* ignore those rts functions not necessary needed for current mode */
1051 if ((c_mode & rts_data[i].flags) == 0)
1053 assert(rts_entities[rts_data[i].id] == NULL);
1054 rts_entities[rts_data[i].id] = irentity;
1058 entitymap_insert(&entitymap, symbol, irentity);
1061 original_entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1062 original_entity->function.irentity = irentity;
1068 * Creates a SymConst for a given entity.
1070 * @param dbgi debug info
1071 * @param mode the (reference) mode for the SymConst
1072 * @param entity the entity
1074 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1077 assert(entity != NULL);
1078 union symconst_symbol sym;
1079 sym.entity_p = entity;
1080 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1083 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1085 ir_mode *value_mode = get_irn_mode(value);
1087 if (value_mode == dest_mode || is_Bad(value))
1090 if (dest_mode == mode_b) {
1091 ir_node *zero = new_Const(get_mode_null(value_mode));
1092 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1093 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1097 return new_d_Conv(dbgi, value, dest_mode);
1101 * Creates a SymConst node representing a wide string literal.
1103 * @param literal the wide string literal
1105 static ir_node *wide_string_literal_to_firm(
1106 const string_literal_expression_t *literal)
1108 ir_type *const global_type = get_glob_type();
1109 ir_type *const elem_type = ir_type_wchar_t;
1110 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1111 ir_type *const type = new_type_array(1, elem_type);
1113 ident *const id = id_unique("str.%u");
1114 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1115 set_entity_ld_ident(entity, id);
1116 set_entity_visibility(entity, ir_visibility_private);
1117 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1119 ir_mode *const mode = get_type_mode(elem_type);
1120 const size_t slen = wstrlen(&literal->value);
1122 set_array_lower_bound_int(type, 0, 0);
1123 set_array_upper_bound_int(type, 0, slen);
1124 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1125 set_type_state(type, layout_fixed);
1127 ir_initializer_t *initializer = create_initializer_compound(slen);
1128 const char *p = literal->value.begin;
1129 for (size_t i = 0; i < slen; ++i) {
1130 assert(p < literal->value.begin + literal->value.size);
1131 utf32 v = read_utf8_char(&p);
1132 ir_tarval *tv = new_tarval_from_long(v, mode);
1133 ir_initializer_t *val = create_initializer_tarval(tv);
1134 set_initializer_compound_value(initializer, i, val);
1136 set_entity_initializer(entity, initializer);
1138 return create_symconst(dbgi, mode_P_data, entity);
1142 * Creates a SymConst node representing a string constant.
1144 * @param src_pos the source position of the string constant
1145 * @param id_prefix a prefix for the name of the generated string constant
1146 * @param value the value of the string constant
1148 static ir_node *string_to_firm(const source_position_t *const src_pos,
1149 const char *const id_prefix,
1150 const string_t *const value)
1152 ir_type *const global_type = get_glob_type();
1153 dbg_info *const dbgi = get_dbg_info(src_pos);
1154 ir_type *const type = new_type_array(1, ir_type_const_char);
1156 ident *const id = id_unique(id_prefix);
1157 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1158 set_entity_ld_ident(entity, id);
1159 set_entity_visibility(entity, ir_visibility_private);
1160 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1162 ir_type *const elem_type = ir_type_const_char;
1163 ir_mode *const mode = get_type_mode(elem_type);
1165 const char* const string = value->begin;
1166 const size_t slen = value->size;
1168 set_array_lower_bound_int(type, 0, 0);
1169 set_array_upper_bound_int(type, 0, slen);
1170 set_type_size_bytes(type, slen);
1171 set_type_state(type, layout_fixed);
1173 ir_initializer_t *initializer = create_initializer_compound(slen);
1174 for (size_t i = 0; i < slen; ++i) {
1175 ir_tarval *tv = new_tarval_from_long(string[i], mode);
1176 ir_initializer_t *val = create_initializer_tarval(tv);
1177 set_initializer_compound_value(initializer, i, val);
1179 set_entity_initializer(entity, initializer);
1181 return create_symconst(dbgi, mode_P_data, entity);
1184 static bool try_create_integer(literal_expression_t *literal,
1185 type_t *type, unsigned char base)
1187 const char *string = literal->value.begin;
1188 size_t size = literal->value.size;
1190 assert(type->kind == TYPE_ATOMIC);
1191 atomic_type_kind_t akind = type->atomic.akind;
1193 ir_mode *mode = atomic_modes[akind];
1194 ir_tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1195 if (tv == tarval_bad)
1198 literal->base.type = type;
1199 literal->target_value = tv;
1203 static void create_integer_tarval(literal_expression_t *literal)
1207 symbol_t *suffix = literal->suffix;
1209 if (suffix != NULL) {
1210 for (const char *c = suffix->string; *c != '\0'; ++c) {
1211 if (*c == 'u' || *c == 'U') { ++us; }
1212 if (*c == 'l' || *c == 'L') { ++ls; }
1217 switch (literal->base.kind) {
1218 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1219 case EXPR_LITERAL_INTEGER: base = 10; break;
1220 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1221 default: panic("invalid literal kind");
1224 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1226 /* now try if the constant is small enough for some types */
1227 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1229 if (us == 0 && try_create_integer(literal, type_int, base))
1231 if ((us == 1 || base != 10)
1232 && try_create_integer(literal, type_unsigned_int, base))
1236 if (us == 0 && try_create_integer(literal, type_long, base))
1238 if ((us == 1 || base != 10)
1239 && try_create_integer(literal, type_unsigned_long, base))
1242 /* last try? then we should not report tarval_bad */
1243 if (us != 1 && base == 10)
1244 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1245 if (us == 0 && try_create_integer(literal, type_long_long, base))
1249 assert(us == 1 || base != 10);
1250 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1251 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1253 panic("internal error when parsing number literal");
1256 tarval_set_integer_overflow_mode(old_mode);
1259 void determine_literal_type(literal_expression_t *literal)
1261 switch (literal->base.kind) {
1262 case EXPR_LITERAL_INTEGER:
1263 case EXPR_LITERAL_INTEGER_OCTAL:
1264 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1265 create_integer_tarval(literal);
1273 * Creates a Const node representing a constant.
1275 static ir_node *literal_to_firm(const literal_expression_t *literal)
1277 type_t *type = skip_typeref(literal->base.type);
1278 ir_mode *mode = get_ir_mode_storage(type);
1279 const char *string = literal->value.begin;
1280 size_t size = literal->value.size;
1283 switch (literal->base.kind) {
1284 case EXPR_LITERAL_WIDE_CHARACTER: {
1285 utf32 v = read_utf8_char(&string);
1287 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1289 tv = new_tarval_from_str(buf, len, mode);
1292 case EXPR_LITERAL_CHARACTER: {
1294 if (size == 1 && char_is_signed) {
1295 v = (signed char)string[0];
1298 for (size_t i = 0; i < size; ++i) {
1299 v = (v << 8) | ((unsigned char)string[i]);
1303 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1305 tv = new_tarval_from_str(buf, len, mode);
1308 case EXPR_LITERAL_INTEGER:
1309 case EXPR_LITERAL_INTEGER_OCTAL:
1310 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1311 assert(literal->target_value != NULL);
1312 tv = literal->target_value;
1314 case EXPR_LITERAL_FLOATINGPOINT:
1315 tv = new_tarval_from_str(string, size, mode);
1317 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1318 char buffer[size + 2];
1319 memcpy(buffer, "0x", 2);
1320 memcpy(buffer+2, string, size);
1321 tv = new_tarval_from_str(buffer, size+2, mode);
1324 case EXPR_LITERAL_BOOLEAN:
1325 if (string[0] == 't') {
1326 tv = get_mode_one(mode);
1328 assert(string[0] == 'f');
1329 tv = get_mode_null(mode);
1332 case EXPR_LITERAL_MS_NOOP:
1333 tv = get_mode_null(mode);
1338 panic("Invalid literal kind found");
1341 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1342 ir_node *res = new_d_Const(dbgi, tv);
1343 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1344 return create_conv(dbgi, res, mode_arith);
1348 * Allocate an area of size bytes aligned at alignment
1351 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1353 static unsigned area_cnt = 0;
1356 ir_type *tp = new_type_array(1, ir_type_char);
1357 set_array_bounds_int(tp, 0, 0, size);
1358 set_type_alignment_bytes(tp, alignment);
1360 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1361 ident *name = new_id_from_str(buf);
1362 ir_entity *area = new_entity(frame_type, name, tp);
1364 /* mark this entity as compiler generated */
1365 set_entity_compiler_generated(area, 1);
1370 * Return a node representing a trampoline region
1371 * for a given function entity.
1373 * @param dbgi debug info
1374 * @param entity the function entity
1376 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1378 ir_entity *region = NULL;
1381 if (current_trampolines != NULL) {
1382 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1383 if (current_trampolines[i].function == entity) {
1384 region = current_trampolines[i].region;
1389 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1391 ir_graph *irg = current_ir_graph;
1392 if (region == NULL) {
1393 /* create a new region */
1394 ir_type *frame_tp = get_irg_frame_type(irg);
1395 trampoline_region reg;
1396 reg.function = entity;
1398 reg.region = alloc_trampoline(frame_tp,
1399 be_params->trampoline_size,
1400 be_params->trampoline_align);
1401 ARR_APP1(trampoline_region, current_trampolines, reg);
1402 region = reg.region;
1404 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1409 * Creates a trampoline for a function represented by an entity.
1411 * @param dbgi debug info
1412 * @param mode the (reference) mode for the function address
1413 * @param entity the function entity
1415 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1418 assert(entity != NULL);
1420 in[0] = get_trampoline_region(dbgi, entity);
1421 in[1] = create_symconst(dbgi, mode, entity);
1422 in[2] = get_irg_frame(current_ir_graph);
1424 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1425 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1426 return new_Proj(irn, mode, pn_Builtin_1_result);
1430 * Dereference an address.
1432 * @param dbgi debug info
1433 * @param type the type of the dereferenced result (the points_to type)
1434 * @param addr the address to dereference
1436 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1437 ir_node *const addr)
1439 ir_type *irtype = get_ir_type(type);
1440 if (is_compound_type(irtype)
1441 || is_Method_type(irtype)
1442 || is_Array_type(irtype)) {
1446 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1447 ? cons_volatile : cons_none;
1448 ir_mode *const mode = get_type_mode(irtype);
1449 ir_node *const memory = get_store();
1450 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1451 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1452 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1454 set_store(load_mem);
1456 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1457 return create_conv(dbgi, load_res, mode_arithmetic);
1461 * Creates a strict Conv (to the node's mode) if necessary.
1463 * @param dbgi debug info
1464 * @param node the node to strict conv
1466 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1468 ir_mode *mode = get_irn_mode(node);
1470 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1472 if (!mode_is_float(mode))
1475 /* check if there is already a Conv */
1476 if (is_Conv(node)) {
1477 /* convert it into a strict Conv */
1478 set_Conv_strict(node, 1);
1482 /* otherwise create a new one */
1483 return new_d_strictConv(dbgi, node, mode);
1487 * Returns the address of a global variable.
1489 * @param dbgi debug info
1490 * @param variable the variable
1492 static ir_node *get_global_var_address(dbg_info *const dbgi,
1493 const variable_t *const variable)
1495 ir_entity *const irentity = variable->v.entity;
1496 if (variable->thread_local) {
1497 ir_node *const no_mem = new_NoMem();
1498 ir_node *const tls = get_irg_tls(current_ir_graph);
1499 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1501 return create_symconst(dbgi, mode_P_data, irentity);
1506 * Returns the correct base address depending on whether it is a parameter or a
1507 * normal local variable.
1509 static ir_node *get_local_frame(ir_entity *const ent)
1511 ir_graph *const irg = current_ir_graph;
1512 const ir_type *const owner = get_entity_owner(ent);
1513 if (owner == current_outer_frame || owner == current_outer_value_type) {
1514 assert(current_static_link != NULL);
1515 return current_static_link;
1517 return get_irg_frame(irg);
1522 * Keep all memory edges of the given block.
1524 static void keep_all_memory(ir_node *block)
1526 ir_node *old = get_cur_block();
1528 set_cur_block(block);
1529 keep_alive(get_store());
1530 /* TODO: keep all memory edges from restricted pointers */
1534 static ir_node *reference_expression_enum_value_to_firm(
1535 const reference_expression_t *ref)
1537 entity_t *entity = ref->entity;
1538 type_t *type = skip_typeref(entity->enum_value.enum_type);
1539 /* make sure the type is constructed */
1540 (void) get_ir_type(type);
1542 return new_Const(entity->enum_value.tv);
1545 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1547 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1548 entity_t *entity = ref->entity;
1549 assert(is_declaration(entity));
1550 type_t *type = skip_typeref(entity->declaration.type);
1552 /* make sure the type is constructed */
1553 (void) get_ir_type(type);
1555 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1556 ir_entity *irentity = get_function_entity(entity, NULL);
1557 /* for gcc compatibility we have to produce (dummy) addresses for some
1558 * builtins which don't have entities */
1559 if (irentity == NULL) {
1560 if (warning.other) {
1561 warningf(&ref->base.source_position,
1562 "taking address of builtin '%Y'",
1563 ref->entity->base.symbol);
1566 /* simply create a NULL pointer */
1567 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1568 ir_node *res = new_Const_long(mode, 0);
1574 switch ((declaration_kind_t) entity->declaration.kind) {
1575 case DECLARATION_KIND_UNKNOWN:
1578 case DECLARATION_KIND_LOCAL_VARIABLE: {
1579 ir_mode *const mode = get_ir_mode_storage(type);
1580 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1581 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1583 case DECLARATION_KIND_PARAMETER: {
1584 ir_mode *const mode = get_ir_mode_storage(type);
1585 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1586 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1588 case DECLARATION_KIND_FUNCTION: {
1589 ir_mode *const mode = get_ir_mode_storage(type);
1590 return create_symconst(dbgi, mode, entity->function.irentity);
1592 case DECLARATION_KIND_INNER_FUNCTION: {
1593 ir_mode *const mode = get_ir_mode_storage(type);
1594 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1595 /* inner function not using the closure */
1596 return create_symconst(dbgi, mode, entity->function.irentity);
1598 /* need trampoline here */
1599 return create_trampoline(dbgi, mode, entity->function.irentity);
1602 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1603 const variable_t *variable = &entity->variable;
1604 ir_node *const addr = get_global_var_address(dbgi, variable);
1605 return deref_address(dbgi, variable->base.type, addr);
1608 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1609 ir_entity *irentity = entity->variable.v.entity;
1610 ir_node *frame = get_local_frame(irentity);
1611 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1612 return deref_address(dbgi, entity->declaration.type, sel);
1614 case DECLARATION_KIND_PARAMETER_ENTITY: {
1615 ir_entity *irentity = entity->parameter.v.entity;
1616 ir_node *frame = get_local_frame(irentity);
1617 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1618 return deref_address(dbgi, entity->declaration.type, sel);
1621 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1622 return entity->variable.v.vla_base;
1624 case DECLARATION_KIND_COMPOUND_MEMBER:
1625 panic("not implemented reference type");
1628 panic("reference to declaration with unknown type found");
1631 static ir_node *reference_addr(const reference_expression_t *ref)
1633 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1634 entity_t *entity = ref->entity;
1635 assert(is_declaration(entity));
1637 switch((declaration_kind_t) entity->declaration.kind) {
1638 case DECLARATION_KIND_UNKNOWN:
1640 case DECLARATION_KIND_PARAMETER:
1641 case DECLARATION_KIND_LOCAL_VARIABLE:
1642 /* you can store to a local variable (so we don't panic but return NULL
1643 * as an indicator for no real address) */
1645 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1646 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1649 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1650 ir_entity *irentity = entity->variable.v.entity;
1651 ir_node *frame = get_local_frame(irentity);
1652 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1656 case DECLARATION_KIND_PARAMETER_ENTITY: {
1657 ir_entity *irentity = entity->parameter.v.entity;
1658 ir_node *frame = get_local_frame(irentity);
1659 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1664 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1665 return entity->variable.v.vla_base;
1667 case DECLARATION_KIND_FUNCTION: {
1668 type_t *const type = skip_typeref(entity->declaration.type);
1669 ir_mode *const mode = get_ir_mode_storage(type);
1670 return create_symconst(dbgi, mode, entity->function.irentity);
1673 case DECLARATION_KIND_INNER_FUNCTION: {
1674 type_t *const type = skip_typeref(entity->declaration.type);
1675 ir_mode *const mode = get_ir_mode_storage(type);
1676 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1677 /* inner function not using the closure */
1678 return create_symconst(dbgi, mode, entity->function.irentity);
1680 /* need trampoline here */
1681 return create_trampoline(dbgi, mode, entity->function.irentity);
1685 case DECLARATION_KIND_COMPOUND_MEMBER:
1686 panic("not implemented reference type");
1689 panic("reference to declaration with unknown type found");
1693 * Generate an unary builtin.
1695 * @param kind the builtin kind to generate
1696 * @param op the operand
1697 * @param function_type the function type for the GNU builtin routine
1698 * @param db debug info
1700 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1703 in[0] = expression_to_firm(op);
1705 ir_type *tp = get_ir_type(function_type);
1706 ir_type *res = get_method_res_type(tp, 0);
1707 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1708 set_irn_pinned(irn, op_pin_state_floats);
1709 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1713 * Generate a pinned unary builtin.
1715 * @param kind the builtin kind to generate
1716 * @param op the operand
1717 * @param function_type the function type for the GNU builtin routine
1718 * @param db debug info
1720 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1721 type_t *function_type, dbg_info *db)
1724 in[0] = expression_to_firm(op);
1726 ir_type *tp = get_ir_type(function_type);
1727 ir_type *res = get_method_res_type(tp, 0);
1728 ir_node *mem = get_store();
1729 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1730 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1731 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1735 * Generate an binary-void-return builtin.
1737 * @param kind the builtin kind to generate
1738 * @param op1 the first operand
1739 * @param op2 the second operand
1740 * @param function_type the function type for the GNU builtin routine
1741 * @param db debug info
1743 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1744 expression_t *op2, type_t *function_type,
1748 in[0] = expression_to_firm(op1);
1749 in[1] = expression_to_firm(op2);
1751 ir_type *tp = get_ir_type(function_type);
1752 ir_node *mem = get_store();
1753 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1754 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1759 * Transform calls to builtin functions.
1761 static ir_node *process_builtin_call(const call_expression_t *call)
1763 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1765 assert(call->function->kind == EXPR_REFERENCE);
1766 reference_expression_t *builtin = &call->function->reference;
1768 type_t *type = skip_typeref(builtin->base.type);
1769 assert(is_type_pointer(type));
1771 type_t *function_type = skip_typeref(type->pointer.points_to);
1773 switch (builtin->entity->function.btk) {
1774 case bk_gnu_builtin_alloca: {
1775 if (call->arguments == NULL || call->arguments->next != NULL) {
1776 panic("invalid number of parameters on __builtin_alloca");
1778 expression_t *argument = call->arguments->expression;
1779 ir_node *size = expression_to_firm(argument);
1781 ir_node *store = get_store();
1782 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1784 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1786 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1791 case bk_gnu_builtin_huge_val:
1792 case bk_gnu_builtin_huge_valf:
1793 case bk_gnu_builtin_huge_vall:
1794 case bk_gnu_builtin_inf:
1795 case bk_gnu_builtin_inff:
1796 case bk_gnu_builtin_infl: {
1797 type_t *type = function_type->function.return_type;
1798 ir_mode *mode = get_ir_mode_arithmetic(type);
1799 ir_tarval *tv = get_mode_infinite(mode);
1800 ir_node *res = new_d_Const(dbgi, tv);
1803 case bk_gnu_builtin_nan:
1804 case bk_gnu_builtin_nanf:
1805 case bk_gnu_builtin_nanl: {
1806 /* Ignore string for now... */
1807 assert(is_type_function(function_type));
1808 type_t *type = function_type->function.return_type;
1809 ir_mode *mode = get_ir_mode_arithmetic(type);
1810 ir_tarval *tv = get_mode_NAN(mode);
1811 ir_node *res = new_d_Const(dbgi, tv);
1814 case bk_gnu_builtin_expect: {
1815 expression_t *argument = call->arguments->expression;
1816 return _expression_to_firm(argument);
1818 case bk_gnu_builtin_va_end:
1819 /* evaluate the argument of va_end for its side effects */
1820 _expression_to_firm(call->arguments->expression);
1822 case bk_gnu_builtin_frame_address: {
1823 expression_t *const expression = call->arguments->expression;
1824 bool val = fold_constant_to_bool(expression);
1827 return get_irg_frame(current_ir_graph);
1829 /* get the argument */
1832 in[0] = expression_to_firm(expression);
1833 in[1] = get_irg_frame(current_ir_graph);
1834 ir_type *tp = get_ir_type(function_type);
1835 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1836 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1839 case bk_gnu_builtin_return_address: {
1840 expression_t *const expression = call->arguments->expression;
1843 in[0] = expression_to_firm(expression);
1844 in[1] = get_irg_frame(current_ir_graph);
1845 ir_type *tp = get_ir_type(function_type);
1846 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1847 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1849 case bk_gnu_builtin_ffs:
1850 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1851 case bk_gnu_builtin_clz:
1852 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1853 case bk_gnu_builtin_ctz:
1854 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1855 case bk_gnu_builtin_popcount:
1856 case bk_ms__popcount:
1857 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1858 case bk_gnu_builtin_parity:
1859 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1860 case bk_gnu_builtin_prefetch: {
1861 call_argument_t *const args = call->arguments;
1862 expression_t *const addr = args->expression;
1865 in[0] = _expression_to_firm(addr);
1866 if (args->next != NULL) {
1867 expression_t *const rw = args->next->expression;
1869 in[1] = _expression_to_firm(rw);
1871 if (args->next->next != NULL) {
1872 expression_t *const locality = args->next->next->expression;
1874 in[2] = expression_to_firm(locality);
1876 in[2] = new_Const_long(mode_int, 3);
1879 in[1] = new_Const_long(mode_int, 0);
1880 in[2] = new_Const_long(mode_int, 3);
1882 ir_type *tp = get_ir_type(function_type);
1883 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1884 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1887 case bk_gnu_builtin_object_size: {
1888 /* determine value of "type" */
1889 expression_t *type_expression = call->arguments->next->expression;
1890 long type_val = fold_constant_to_int(type_expression);
1891 type_t *type = function_type->function.return_type;
1892 ir_mode *mode = get_ir_mode_arithmetic(type);
1895 /* just produce a "I don't know" result */
1897 result = new_tarval_from_long(0, mode);
1899 result = new_tarval_from_long(-1, mode);
1901 return new_d_Const(dbgi, result);
1903 case bk_gnu_builtin_trap:
1906 ir_type *tp = get_ir_type(function_type);
1907 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1908 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1911 case bk_ms__debugbreak: {
1912 ir_type *tp = get_ir_type(function_type);
1913 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1914 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1917 case bk_ms_ReturnAddress: {
1920 in[0] = new_Const_long(mode_int, 0);
1921 in[1] = get_irg_frame(current_ir_graph);
1922 ir_type *tp = get_ir_type(function_type);
1923 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1924 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1927 case bk_ms_rotl64: {
1928 ir_node *val = expression_to_firm(call->arguments->expression);
1929 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1930 ir_mode *mode = get_irn_mode(val);
1931 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1934 case bk_ms_rotr64: {
1935 ir_node *val = expression_to_firm(call->arguments->expression);
1936 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1937 ir_mode *mode = get_irn_mode(val);
1938 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1939 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1940 return new_d_Rotl(dbgi, val, sub, mode);
1942 case bk_ms_byteswap_ushort:
1943 case bk_ms_byteswap_ulong:
1944 case bk_ms_byteswap_uint64:
1945 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1948 case bk_ms__indword:
1949 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1950 case bk_ms__outbyte:
1951 case bk_ms__outword:
1952 case bk_ms__outdword:
1953 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1954 call->arguments->next->expression, function_type, dbgi);
1956 panic("unsupported builtin found");
1961 * Transform a call expression.
1962 * Handles some special cases, like alloca() calls, which must be resolved
1963 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1964 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1967 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1969 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1970 assert(get_cur_block() != NULL);
1972 expression_t *function = call->function;
1973 if (function->kind == EXPR_REFERENCE) {
1974 const reference_expression_t *ref = &function->reference;
1975 entity_t *entity = ref->entity;
1977 if (entity->kind == ENTITY_FUNCTION) {
1978 ir_entity *irentity = entity->function.irentity;
1979 if (irentity == NULL)
1980 irentity = get_function_entity(entity, NULL);
1982 if (irentity == NULL && entity->function.btk != bk_none) {
1983 return process_builtin_call(call);
1987 if (irentity == rts_entities[rts_alloca]) {
1988 /* handle alloca() call */
1989 expression_t *argument = call->arguments->expression;
1990 ir_node *size = expression_to_firm(argument);
1991 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1993 size = create_conv(dbgi, size, mode);
1995 ir_node *store = get_store();
1996 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1997 firm_unknown_type, stack_alloc);
1998 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
2000 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
2007 ir_node *callee = expression_to_firm(function);
2009 type_t *type = skip_typeref(function->base.type);
2010 assert(is_type_pointer(type));
2011 pointer_type_t *pointer_type = &type->pointer;
2012 type_t *points_to = skip_typeref(pointer_type->points_to);
2013 assert(is_type_function(points_to));
2014 function_type_t *function_type = &points_to->function;
2016 int n_parameters = 0;
2017 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
2018 ir_type *new_method_type = NULL;
2019 if (function_type->variadic || function_type->unspecified_parameters) {
2020 const call_argument_t *argument = call->arguments;
2021 for ( ; argument != NULL; argument = argument->next) {
2025 /* we need to construct a new method type matching the call
2027 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2028 int n_res = get_method_n_ress(ir_method_type);
2029 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2030 set_method_calling_convention(new_method_type,
2031 get_method_calling_convention(ir_method_type));
2032 set_method_additional_properties(new_method_type,
2033 get_method_additional_properties(ir_method_type));
2034 set_method_variadicity(new_method_type,
2035 get_method_variadicity(ir_method_type));
2037 for (int i = 0; i < n_res; ++i) {
2038 set_method_res_type(new_method_type, i,
2039 get_method_res_type(ir_method_type, i));
2041 argument = call->arguments;
2042 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2043 expression_t *expression = argument->expression;
2044 ir_type *irtype = get_ir_type(expression->base.type);
2045 set_method_param_type(new_method_type, i, irtype);
2047 ir_method_type = new_method_type;
2049 n_parameters = get_method_n_params(ir_method_type);
2052 ir_node *in[n_parameters];
2054 const call_argument_t *argument = call->arguments;
2055 for (int n = 0; n < n_parameters; ++n) {
2056 expression_t *expression = argument->expression;
2057 ir_node *arg_node = expression_to_firm(expression);
2059 type_t *type = skip_typeref(expression->base.type);
2060 if (!is_type_compound(type)) {
2061 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2062 arg_node = create_conv(dbgi, arg_node, mode);
2063 arg_node = do_strict_conv(dbgi, arg_node);
2068 argument = argument->next;
2071 ir_node *store = get_store();
2072 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2074 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2077 type_t *return_type = skip_typeref(function_type->return_type);
2078 ir_node *result = NULL;
2080 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2081 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2083 if (is_type_scalar(return_type)) {
2084 ir_mode *mode = get_ir_mode_storage(return_type);
2085 result = new_d_Proj(dbgi, resproj, mode, 0);
2086 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2087 result = create_conv(NULL, result, mode_arith);
2089 ir_mode *mode = mode_P_data;
2090 result = new_d_Proj(dbgi, resproj, mode, 0);
2094 if (function->kind == EXPR_REFERENCE &&
2095 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2096 /* A dead end: Keep the Call and the Block. Also place all further
2097 * nodes into a new and unreachable block. */
2099 keep_alive(get_cur_block());
2100 ir_node *block = new_Block(0, NULL);
2101 set_cur_block(block);
2107 static void statement_to_firm(statement_t *statement);
2108 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2110 static ir_node *expression_to_addr(const expression_t *expression);
2111 static ir_node *create_condition_evaluation(const expression_t *expression,
2112 ir_node *true_block,
2113 ir_node *false_block);
2115 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2118 if (!is_type_compound(type)) {
2119 ir_mode *mode = get_ir_mode_storage(type);
2120 value = create_conv(dbgi, value, mode);
2121 value = do_strict_conv(dbgi, value);
2124 ir_node *memory = get_store();
2126 if (is_type_scalar(type)) {
2127 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2128 ? cons_volatile : cons_none;
2129 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2130 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2131 set_store(store_mem);
2133 ir_type *irtype = get_ir_type(type);
2134 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2135 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2136 set_store(copyb_mem);
2140 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2142 ir_tarval *all_one = get_mode_all_one(mode);
2143 int mode_size = get_mode_size_bits(mode);
2145 assert(offset >= 0);
2147 assert(offset + size <= mode_size);
2148 if (size == mode_size) {
2152 long shiftr = get_mode_size_bits(mode) - size;
2153 long shiftl = offset;
2154 ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2155 ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2156 ir_tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2157 ir_tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2162 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2163 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2165 ir_type *entity_type = get_entity_type(entity);
2166 ir_type *base_type = get_primitive_base_type(entity_type);
2167 assert(base_type != NULL);
2168 ir_mode *mode = get_type_mode(base_type);
2170 value = create_conv(dbgi, value, mode);
2172 /* kill upper bits of value and shift to right position */
2173 int bitoffset = get_entity_offset_bits_remainder(entity);
2174 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2176 ir_tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2177 ir_node *mask_node = new_d_Const(dbgi, mask);
2178 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2179 ir_tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2180 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2181 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2183 /* load current value */
2184 ir_node *mem = get_store();
2185 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2186 set_volatile ? cons_volatile : cons_none);
2187 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2188 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2189 ir_tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2190 ir_tarval *inv_mask = tarval_not(shift_mask);
2191 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2192 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2194 /* construct new value and store */
2195 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2196 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2197 set_volatile ? cons_volatile : cons_none);
2198 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2199 set_store(store_mem);
2201 return value_masked;
2204 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2207 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2208 type_t *type = expression->base.type;
2209 ir_mode *mode = get_ir_mode_storage(type);
2210 ir_node *mem = get_store();
2211 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2212 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2213 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2215 load_res = create_conv(dbgi, load_res, mode_int);
2217 set_store(load_mem);
2219 /* kill upper bits */
2220 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2221 ir_entity *entity = expression->compound_entry->compound_member.entity;
2222 int bitoffset = get_entity_offset_bits_remainder(entity);
2223 ir_type *entity_type = get_entity_type(entity);
2224 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2225 long shift_bitsl = machine_size - bitoffset - bitsize;
2226 assert(shift_bitsl >= 0);
2227 ir_tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2228 ir_node *countl = new_d_Const(dbgi, tvl);
2229 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2231 long shift_bitsr = bitoffset + shift_bitsl;
2232 assert(shift_bitsr <= (long) machine_size);
2233 ir_tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2234 ir_node *countr = new_d_Const(dbgi, tvr);
2236 if (mode_is_signed(mode)) {
2237 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2239 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2242 return create_conv(dbgi, shiftr, mode);
2245 /* make sure the selected compound type is constructed */
2246 static void construct_select_compound(const select_expression_t *expression)
2248 type_t *type = skip_typeref(expression->compound->base.type);
2249 if (is_type_pointer(type)) {
2250 type = type->pointer.points_to;
2252 (void) get_ir_type(type);
2255 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2256 ir_node *value, ir_node *addr)
2258 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2259 type_t *type = skip_typeref(expression->base.type);
2261 if (!is_type_compound(type)) {
2262 ir_mode *mode = get_ir_mode_storage(type);
2263 value = create_conv(dbgi, value, mode);
2264 value = do_strict_conv(dbgi, value);
2267 if (expression->kind == EXPR_REFERENCE) {
2268 const reference_expression_t *ref = &expression->reference;
2270 entity_t *entity = ref->entity;
2271 assert(is_declaration(entity));
2272 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2273 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2274 set_value(entity->variable.v.value_number, value);
2276 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2277 set_value(entity->parameter.v.value_number, value);
2283 addr = expression_to_addr(expression);
2284 assert(addr != NULL);
2286 if (expression->kind == EXPR_SELECT) {
2287 const select_expression_t *select = &expression->select;
2289 construct_select_compound(select);
2291 entity_t *entity = select->compound_entry;
2292 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2293 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2294 ir_entity *irentity = entity->compound_member.entity;
2296 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2297 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2303 assign_value(dbgi, addr, type, value);
2307 static void set_value_for_expression(const expression_t *expression,
2310 set_value_for_expression_addr(expression, value, NULL);
2313 static ir_node *get_value_from_lvalue(const expression_t *expression,
2316 if (expression->kind == EXPR_REFERENCE) {
2317 const reference_expression_t *ref = &expression->reference;
2319 entity_t *entity = ref->entity;
2320 assert(entity->kind == ENTITY_VARIABLE
2321 || entity->kind == ENTITY_PARAMETER);
2322 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2324 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2325 value_number = entity->variable.v.value_number;
2326 assert(addr == NULL);
2327 type_t *type = skip_typeref(expression->base.type);
2328 ir_mode *mode = get_ir_mode_storage(type);
2329 ir_node *res = get_value(value_number, mode);
2330 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2331 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2332 value_number = entity->parameter.v.value_number;
2333 assert(addr == NULL);
2334 type_t *type = skip_typeref(expression->base.type);
2335 ir_mode *mode = get_ir_mode_storage(type);
2336 ir_node *res = get_value(value_number, mode);
2337 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2341 assert(addr != NULL);
2342 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2345 if (expression->kind == EXPR_SELECT &&
2346 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2347 construct_select_compound(&expression->select);
2348 value = bitfield_extract_to_firm(&expression->select, addr);
2350 value = deref_address(dbgi, expression->base.type, addr);
2357 static ir_node *create_incdec(const unary_expression_t *expression)
2359 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2360 const expression_t *value_expr = expression->value;
2361 ir_node *addr = expression_to_addr(value_expr);
2362 ir_node *value = get_value_from_lvalue(value_expr, addr);
2364 type_t *type = skip_typeref(expression->base.type);
2365 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2368 if (is_type_pointer(type)) {
2369 pointer_type_t *pointer_type = &type->pointer;
2370 offset = get_type_size_node(pointer_type->points_to);
2372 assert(is_type_arithmetic(type));
2373 offset = new_Const(get_mode_one(mode));
2377 ir_node *store_value;
2378 switch(expression->base.kind) {
2379 case EXPR_UNARY_POSTFIX_INCREMENT:
2381 store_value = new_d_Add(dbgi, value, offset, mode);
2383 case EXPR_UNARY_POSTFIX_DECREMENT:
2385 store_value = new_d_Sub(dbgi, value, offset, mode);
2387 case EXPR_UNARY_PREFIX_INCREMENT:
2388 result = new_d_Add(dbgi, value, offset, mode);
2389 store_value = result;
2391 case EXPR_UNARY_PREFIX_DECREMENT:
2392 result = new_d_Sub(dbgi, value, offset, mode);
2393 store_value = result;
2396 panic("no incdec expr in create_incdec");
2399 set_value_for_expression_addr(value_expr, store_value, addr);
2404 static bool is_local_variable(expression_t *expression)
2406 if (expression->kind != EXPR_REFERENCE)
2408 reference_expression_t *ref_expr = &expression->reference;
2409 entity_t *entity = ref_expr->entity;
2410 if (entity->kind != ENTITY_VARIABLE)
2412 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2413 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2416 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2419 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2420 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2421 case EXPR_BINARY_NOTEQUAL:
2422 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2423 case EXPR_BINARY_ISLESS:
2424 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2425 case EXPR_BINARY_ISLESSEQUAL:
2426 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2427 case EXPR_BINARY_ISGREATER:
2428 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2429 case EXPR_BINARY_ISGREATEREQUAL:
2430 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2431 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2436 panic("trying to get pn_Cmp from non-comparison binexpr type");
2440 * Handle the assume optimizer hint: check if a Confirm
2441 * node can be created.
2443 * @param dbi debug info
2444 * @param expr the IL assume expression
2446 * we support here only some simple cases:
2451 static ir_node *handle_assume_compare(dbg_info *dbi,
2452 const binary_expression_t *expression)
2454 expression_t *op1 = expression->left;
2455 expression_t *op2 = expression->right;
2456 entity_t *var2, *var = NULL;
2457 ir_node *res = NULL;
2460 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2462 if (is_local_variable(op1) && is_local_variable(op2)) {
2463 var = op1->reference.entity;
2464 var2 = op2->reference.entity;
2466 type_t *const type = skip_typeref(var->declaration.type);
2467 ir_mode *const mode = get_ir_mode_storage(type);
2469 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2470 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2472 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2473 set_value(var2->variable.v.value_number, res);
2475 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2476 set_value(var->variable.v.value_number, res);
2482 if (is_local_variable(op1) && is_constant_expression(op2)) {
2483 var = op1->reference.entity;
2485 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2486 cmp_val = get_inversed_pnc(cmp_val);
2487 var = op2->reference.entity;
2492 type_t *const type = skip_typeref(var->declaration.type);
2493 ir_mode *const mode = get_ir_mode_storage(type);
2495 res = get_value(var->variable.v.value_number, mode);
2496 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2497 set_value(var->variable.v.value_number, res);
2503 * Handle the assume optimizer hint.
2505 * @param dbi debug info
2506 * @param expr the IL assume expression
2508 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2510 switch(expression->kind) {
2511 case EXPR_BINARY_EQUAL:
2512 case EXPR_BINARY_NOTEQUAL:
2513 case EXPR_BINARY_LESS:
2514 case EXPR_BINARY_LESSEQUAL:
2515 case EXPR_BINARY_GREATER:
2516 case EXPR_BINARY_GREATEREQUAL:
2517 return handle_assume_compare(dbi, &expression->binary);
2523 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2524 type_t *from_type, type_t *type)
2526 type = skip_typeref(type);
2527 if (type == type_void) {
2528 /* make sure firm type is constructed */
2529 (void) get_ir_type(type);
2532 if (!is_type_scalar(type)) {
2533 /* make sure firm type is constructed */
2534 (void) get_ir_type(type);
2538 from_type = skip_typeref(from_type);
2539 ir_mode *mode = get_ir_mode_storage(type);
2540 /* check for conversion from / to __based types */
2541 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2542 const variable_t *from_var = from_type->pointer.base_variable;
2543 const variable_t *to_var = type->pointer.base_variable;
2544 if (from_var != to_var) {
2545 if (from_var != NULL) {
2546 ir_node *const addr = get_global_var_address(dbgi, from_var);
2547 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2548 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2550 if (to_var != NULL) {
2551 ir_node *const addr = get_global_var_address(dbgi, to_var);
2552 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2553 value_node = new_d_Sub(dbgi, value_node, base, mode);
2558 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2559 /* bool adjustments (we save a mode_Bu, but have to temporarily
2560 * convert to mode_b so we only get a 0/1 value */
2561 value_node = create_conv(dbgi, value_node, mode_b);
2564 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2565 ir_node *node = create_conv(dbgi, value_node, mode);
2566 node = do_strict_conv(dbgi, node);
2567 node = create_conv(dbgi, node, mode_arith);
2572 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2574 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2575 type_t *type = skip_typeref(expression->base.type);
2577 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2578 return expression_to_addr(expression->value);
2580 const expression_t *value = expression->value;
2582 switch(expression->base.kind) {
2583 case EXPR_UNARY_NEGATE: {
2584 ir_node *value_node = expression_to_firm(value);
2585 ir_mode *mode = get_ir_mode_arithmetic(type);
2586 return new_d_Minus(dbgi, value_node, mode);
2588 case EXPR_UNARY_PLUS:
2589 return expression_to_firm(value);
2590 case EXPR_UNARY_BITWISE_NEGATE: {
2591 ir_node *value_node = expression_to_firm(value);
2592 ir_mode *mode = get_ir_mode_arithmetic(type);
2593 return new_d_Not(dbgi, value_node, mode);
2595 case EXPR_UNARY_NOT: {
2596 ir_node *value_node = _expression_to_firm(value);
2597 value_node = create_conv(dbgi, value_node, mode_b);
2598 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2601 case EXPR_UNARY_DEREFERENCE: {
2602 ir_node *value_node = expression_to_firm(value);
2603 type_t *value_type = skip_typeref(value->base.type);
2604 assert(is_type_pointer(value_type));
2606 /* check for __based */
2607 const variable_t *const base_var = value_type->pointer.base_variable;
2608 if (base_var != NULL) {
2609 ir_node *const addr = get_global_var_address(dbgi, base_var);
2610 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2611 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2613 type_t *points_to = value_type->pointer.points_to;
2614 return deref_address(dbgi, points_to, value_node);
2616 case EXPR_UNARY_POSTFIX_INCREMENT:
2617 case EXPR_UNARY_POSTFIX_DECREMENT:
2618 case EXPR_UNARY_PREFIX_INCREMENT:
2619 case EXPR_UNARY_PREFIX_DECREMENT:
2620 return create_incdec(expression);
2621 case EXPR_UNARY_CAST_IMPLICIT:
2622 case EXPR_UNARY_CAST: {
2623 ir_node *value_node = expression_to_firm(value);
2624 type_t *from_type = value->base.type;
2625 return create_cast(dbgi, value_node, from_type, type);
2627 case EXPR_UNARY_ASSUME:
2628 return handle_assume(dbgi, value);
2633 panic("invalid UNEXPR type found");
2637 * produces a 0/1 depending of the value of a mode_b node
2639 static ir_node *produce_condition_result(const expression_t *expression,
2640 ir_mode *mode, dbg_info *dbgi)
2642 ir_node *cur_block = get_cur_block();
2644 ir_node *one_block = new_immBlock();
2645 set_cur_block(one_block);
2646 ir_node *one = new_Const(get_mode_one(mode));
2647 ir_node *jmp_one = new_d_Jmp(dbgi);
2649 ir_node *zero_block = new_immBlock();
2650 set_cur_block(zero_block);
2651 ir_node *zero = new_Const(get_mode_null(mode));
2652 ir_node *jmp_zero = new_d_Jmp(dbgi);
2654 set_cur_block(cur_block);
2655 create_condition_evaluation(expression, one_block, zero_block);
2656 mature_immBlock(one_block);
2657 mature_immBlock(zero_block);
2659 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2660 ir_node *block = new_Block(2, in_cf);
2661 set_cur_block(block);
2663 ir_node *in[2] = { one, zero };
2664 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2669 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2670 ir_node *value, type_t *type)
2672 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2673 assert(is_type_pointer(type));
2674 pointer_type_t *const pointer_type = &type->pointer;
2675 type_t *const points_to = skip_typeref(pointer_type->points_to);
2676 ir_node * elem_size = get_type_size_node(points_to);
2677 elem_size = create_conv(dbgi, elem_size, mode);
2678 value = create_conv(dbgi, value, mode);
2679 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2683 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2684 ir_node *left, ir_node *right)
2687 type_t *type_left = skip_typeref(expression->left->base.type);
2688 type_t *type_right = skip_typeref(expression->right->base.type);
2690 expression_kind_t kind = expression->base.kind;
2693 case EXPR_BINARY_SHIFTLEFT:
2694 case EXPR_BINARY_SHIFTRIGHT:
2695 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2696 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2697 mode = get_irn_mode(left);
2698 right = create_conv(dbgi, right, mode_uint);
2701 case EXPR_BINARY_SUB:
2702 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2703 const pointer_type_t *const ptr_type = &type_left->pointer;
2705 mode = get_ir_mode_arithmetic(expression->base.type);
2706 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2707 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2708 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2709 ir_node *const no_mem = new_NoMem();
2710 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2711 mode, op_pin_state_floats);
2712 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2715 case EXPR_BINARY_SUB_ASSIGN:
2716 if (is_type_pointer(type_left)) {
2717 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2718 mode = get_ir_mode_arithmetic(type_left);
2723 case EXPR_BINARY_ADD:
2724 case EXPR_BINARY_ADD_ASSIGN:
2725 if (is_type_pointer(type_left)) {
2726 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2727 mode = get_ir_mode_arithmetic(type_left);
2729 } else if (is_type_pointer(type_right)) {
2730 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2731 mode = get_ir_mode_arithmetic(type_right);
2738 mode = get_ir_mode_arithmetic(type_right);
2739 left = create_conv(dbgi, left, mode);
2744 case EXPR_BINARY_ADD_ASSIGN:
2745 case EXPR_BINARY_ADD:
2746 return new_d_Add(dbgi, left, right, mode);
2747 case EXPR_BINARY_SUB_ASSIGN:
2748 case EXPR_BINARY_SUB:
2749 return new_d_Sub(dbgi, left, right, mode);
2750 case EXPR_BINARY_MUL_ASSIGN:
2751 case EXPR_BINARY_MUL:
2752 return new_d_Mul(dbgi, left, right, mode);
2753 case EXPR_BINARY_BITWISE_AND:
2754 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2755 return new_d_And(dbgi, left, right, mode);
2756 case EXPR_BINARY_BITWISE_OR:
2757 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2758 return new_d_Or(dbgi, left, right, mode);
2759 case EXPR_BINARY_BITWISE_XOR:
2760 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2761 return new_d_Eor(dbgi, left, right, mode);
2762 case EXPR_BINARY_SHIFTLEFT:
2763 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2764 return new_d_Shl(dbgi, left, right, mode);
2765 case EXPR_BINARY_SHIFTRIGHT:
2766 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2767 if (mode_is_signed(mode)) {
2768 return new_d_Shrs(dbgi, left, right, mode);
2770 return new_d_Shr(dbgi, left, right, mode);
2772 case EXPR_BINARY_DIV:
2773 case EXPR_BINARY_DIV_ASSIGN: {
2774 ir_node *pin = new_Pin(new_NoMem());
2777 if (mode_is_float(mode)) {
2778 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2779 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2781 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2782 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2786 case EXPR_BINARY_MOD:
2787 case EXPR_BINARY_MOD_ASSIGN: {
2788 ir_node *pin = new_Pin(new_NoMem());
2789 assert(!mode_is_float(mode));
2790 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2791 op_pin_state_floats);
2792 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2796 panic("unexpected expression kind");
2800 static ir_node *create_lazy_op(const binary_expression_t *expression)
2802 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2803 type_t *type = skip_typeref(expression->base.type);
2804 ir_mode *mode = get_ir_mode_arithmetic(type);
2806 if (is_constant_expression(expression->left)) {
2807 bool val = fold_constant_to_bool(expression->left);
2808 expression_kind_t ekind = expression->base.kind;
2809 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2810 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2812 return new_Const(get_mode_null(mode));
2816 return new_Const(get_mode_one(mode));
2820 if (is_constant_expression(expression->right)) {
2821 bool valr = fold_constant_to_bool(expression->right);
2823 new_Const(get_mode_one(mode)) :
2824 new_Const(get_mode_null(mode));
2827 return produce_condition_result(expression->right, mode, dbgi);
2830 return produce_condition_result((const expression_t*) expression, mode,
2834 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2835 ir_node *right, ir_mode *mode);
2837 static ir_node *create_assign_binop(const binary_expression_t *expression)
2839 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2840 const expression_t *left_expr = expression->left;
2841 type_t *type = skip_typeref(left_expr->base.type);
2842 ir_node *right = expression_to_firm(expression->right);
2843 ir_node *left_addr = expression_to_addr(left_expr);
2844 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2845 ir_node *result = create_op(dbgi, expression, left, right);
2847 result = create_cast(dbgi, result, expression->right->base.type, type);
2848 result = do_strict_conv(dbgi, result);
2850 result = set_value_for_expression_addr(left_expr, result, left_addr);
2852 if (!is_type_compound(type)) {
2853 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2854 result = create_conv(dbgi, result, mode_arithmetic);
2859 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2861 expression_kind_t kind = expression->base.kind;
2864 case EXPR_BINARY_EQUAL:
2865 case EXPR_BINARY_NOTEQUAL:
2866 case EXPR_BINARY_LESS:
2867 case EXPR_BINARY_LESSEQUAL:
2868 case EXPR_BINARY_GREATER:
2869 case EXPR_BINARY_GREATEREQUAL:
2870 case EXPR_BINARY_ISGREATER:
2871 case EXPR_BINARY_ISGREATEREQUAL:
2872 case EXPR_BINARY_ISLESS:
2873 case EXPR_BINARY_ISLESSEQUAL:
2874 case EXPR_BINARY_ISLESSGREATER:
2875 case EXPR_BINARY_ISUNORDERED: {
2876 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2877 ir_node *left = expression_to_firm(expression->left);
2878 ir_node *right = expression_to_firm(expression->right);
2879 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2880 long pnc = get_pnc(kind, expression->left->base.type);
2881 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2884 case EXPR_BINARY_ASSIGN: {
2885 ir_node *addr = expression_to_addr(expression->left);
2886 ir_node *right = expression_to_firm(expression->right);
2888 = set_value_for_expression_addr(expression->left, right, addr);
2890 type_t *type = skip_typeref(expression->base.type);
2891 if (!is_type_compound(type)) {
2892 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2893 res = create_conv(NULL, res, mode_arithmetic);
2897 case EXPR_BINARY_ADD:
2898 case EXPR_BINARY_SUB:
2899 case EXPR_BINARY_MUL:
2900 case EXPR_BINARY_DIV:
2901 case EXPR_BINARY_MOD:
2902 case EXPR_BINARY_BITWISE_AND:
2903 case EXPR_BINARY_BITWISE_OR:
2904 case EXPR_BINARY_BITWISE_XOR:
2905 case EXPR_BINARY_SHIFTLEFT:
2906 case EXPR_BINARY_SHIFTRIGHT:
2908 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2909 ir_node *left = expression_to_firm(expression->left);
2910 ir_node *right = expression_to_firm(expression->right);
2911 return create_op(dbgi, expression, left, right);
2913 case EXPR_BINARY_LOGICAL_AND:
2914 case EXPR_BINARY_LOGICAL_OR:
2915 return create_lazy_op(expression);
2916 case EXPR_BINARY_COMMA:
2917 /* create side effects of left side */
2918 (void) expression_to_firm(expression->left);
2919 return _expression_to_firm(expression->right);
2921 case EXPR_BINARY_ADD_ASSIGN:
2922 case EXPR_BINARY_SUB_ASSIGN:
2923 case EXPR_BINARY_MUL_ASSIGN:
2924 case EXPR_BINARY_MOD_ASSIGN:
2925 case EXPR_BINARY_DIV_ASSIGN:
2926 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2927 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2928 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2929 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2930 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2931 return create_assign_binop(expression);
2933 panic("TODO binexpr type");
2937 static ir_node *array_access_addr(const array_access_expression_t *expression)
2939 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2940 ir_node *base_addr = expression_to_firm(expression->array_ref);
2941 ir_node *offset = expression_to_firm(expression->index);
2942 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2943 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2944 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2949 static ir_node *array_access_to_firm(
2950 const array_access_expression_t *expression)
2952 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2953 ir_node *addr = array_access_addr(expression);
2954 type_t *type = revert_automatic_type_conversion(
2955 (const expression_t*) expression);
2956 type = skip_typeref(type);
2958 return deref_address(dbgi, type, addr);
2961 static long get_offsetof_offset(const offsetof_expression_t *expression)
2963 type_t *orig_type = expression->type;
2966 designator_t *designator = expression->designator;
2967 for ( ; designator != NULL; designator = designator->next) {
2968 type_t *type = skip_typeref(orig_type);
2969 /* be sure the type is constructed */
2970 (void) get_ir_type(type);
2972 if (designator->symbol != NULL) {
2973 assert(is_type_compound(type));
2974 symbol_t *symbol = designator->symbol;
2976 compound_t *compound = type->compound.compound;
2977 entity_t *iter = compound->members.entities;
2978 for ( ; iter != NULL; iter = iter->base.next) {
2979 if (iter->base.symbol == symbol) {
2983 assert(iter != NULL);
2985 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2986 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2987 offset += get_entity_offset(iter->compound_member.entity);
2989 orig_type = iter->declaration.type;
2991 expression_t *array_index = designator->array_index;
2992 assert(designator->array_index != NULL);
2993 assert(is_type_array(type));
2995 long index = fold_constant_to_int(array_index);
2996 ir_type *arr_type = get_ir_type(type);
2997 ir_type *elem_type = get_array_element_type(arr_type);
2998 long elem_size = get_type_size_bytes(elem_type);
3000 offset += index * elem_size;
3002 orig_type = type->array.element_type;
3009 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
3011 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3012 long offset = get_offsetof_offset(expression);
3013 ir_tarval *tv = new_tarval_from_long(offset, mode);
3014 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3016 return new_d_Const(dbgi, tv);
3019 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3020 ir_entity *entity, type_t *type);
3022 static ir_node *compound_literal_to_firm(
3023 const compound_literal_expression_t *expression)
3025 type_t *type = expression->type;
3027 /* create an entity on the stack */
3028 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3030 ident *const id = id_unique("CompLit.%u");
3031 ir_type *const irtype = get_ir_type(type);
3032 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3033 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
3034 set_entity_ld_ident(entity, id);
3036 /* create initialisation code */
3037 initializer_t *initializer = expression->initializer;
3038 create_local_initializer(initializer, dbgi, entity, type);
3040 /* create a sel for the compound literal address */
3041 ir_node *frame = get_irg_frame(current_ir_graph);
3042 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3047 * Transform a sizeof expression into Firm code.
3049 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3051 type_t *const type = skip_typeref(expression->type);
3052 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3053 if (is_type_array(type) && type->array.is_vla
3054 && expression->tp_expression != NULL) {
3055 expression_to_firm(expression->tp_expression);
3058 return get_type_size_node(type);
3061 static entity_t *get_expression_entity(const expression_t *expression)
3063 if (expression->kind != EXPR_REFERENCE)
3066 return expression->reference.entity;
3069 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3071 switch(entity->kind) {
3072 DECLARATION_KIND_CASES
3073 return entity->declaration.alignment;
3076 return entity->compound.alignment;
3077 case ENTITY_TYPEDEF:
3078 return entity->typedefe.alignment;
3086 * Transform an alignof expression into Firm code.
3088 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3090 unsigned alignment = 0;
3092 const expression_t *tp_expression = expression->tp_expression;
3093 if (tp_expression != NULL) {
3094 entity_t *entity = get_expression_entity(tp_expression);
3095 if (entity != NULL) {
3096 alignment = get_cparser_entity_alignment(entity);
3100 if (alignment == 0) {
3101 type_t *type = expression->type;
3102 alignment = get_type_alignment(type);
3105 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3106 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3107 ir_tarval *tv = new_tarval_from_long(alignment, mode);
3108 return new_d_Const(dbgi, tv);
3111 static void init_ir_types(void);
3113 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3115 assert(is_type_valid(skip_typeref(expression->base.type)));
3117 bool constant_folding_old = constant_folding;
3118 constant_folding = true;
3122 assert(is_constant_expression(expression));
3124 ir_graph *old_current_ir_graph = current_ir_graph;
3125 current_ir_graph = get_const_code_irg();
3127 ir_node *cnst = expression_to_firm(expression);
3128 current_ir_graph = old_current_ir_graph;
3130 if (!is_Const(cnst)) {
3131 panic("couldn't fold constant");
3134 constant_folding = constant_folding_old;
3136 return get_Const_tarval(cnst);
3139 long fold_constant_to_int(const expression_t *expression)
3141 if (expression->kind == EXPR_INVALID)
3144 ir_tarval *tv = fold_constant_to_tarval(expression);
3145 if (!tarval_is_long(tv)) {
3146 panic("result of constant folding is not integer");
3149 return get_tarval_long(tv);
3152 bool fold_constant_to_bool(const expression_t *expression)
3154 if (expression->kind == EXPR_INVALID)
3156 ir_tarval *tv = fold_constant_to_tarval(expression);
3157 return !tarval_is_null(tv);
3160 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3162 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3164 /* first try to fold a constant condition */
3165 if (is_constant_expression(expression->condition)) {
3166 bool val = fold_constant_to_bool(expression->condition);
3168 expression_t *true_expression = expression->true_expression;
3169 if (true_expression == NULL)
3170 true_expression = expression->condition;
3171 return expression_to_firm(true_expression);
3173 return expression_to_firm(expression->false_expression);
3177 ir_node *cur_block = get_cur_block();
3179 /* create the true block */
3180 ir_node *true_block = new_immBlock();
3181 set_cur_block(true_block);
3183 ir_node *true_val = expression->true_expression != NULL ?
3184 expression_to_firm(expression->true_expression) : NULL;
3185 ir_node *true_jmp = new_Jmp();
3187 /* create the false block */
3188 ir_node *false_block = new_immBlock();
3189 set_cur_block(false_block);
3191 ir_node *false_val = expression_to_firm(expression->false_expression);
3192 ir_node *false_jmp = new_Jmp();
3194 /* create the condition evaluation */
3195 set_cur_block(cur_block);
3196 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3197 if (expression->true_expression == NULL) {
3198 if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3199 true_val = cond_expr;
3201 /* Condition ended with a short circuit (&&, ||, !) operation or a
3202 * comparison. Generate a "1" as value for the true branch. */
3203 true_val = new_Const(get_mode_one(mode_Is));
3206 mature_immBlock(true_block);
3207 mature_immBlock(false_block);
3209 /* create the common block */
3210 ir_node *in_cf[2] = { true_jmp, false_jmp };
3211 ir_node *block = new_Block(2, in_cf);
3212 set_cur_block(block);
3214 /* TODO improve static semantics, so either both or no values are NULL */
3215 if (true_val == NULL || false_val == NULL)
3218 ir_node *in[2] = { true_val, false_val };
3219 ir_mode *mode = get_irn_mode(true_val);
3220 assert(get_irn_mode(false_val) == mode);
3221 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
3227 * Returns an IR-node representing the address of a field.
3229 static ir_node *select_addr(const select_expression_t *expression)
3231 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3233 construct_select_compound(expression);
3235 ir_node *compound_addr = expression_to_firm(expression->compound);
3237 entity_t *entry = expression->compound_entry;
3238 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3239 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3241 if (constant_folding) {
3242 ir_mode *mode = get_irn_mode(compound_addr);
3243 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3244 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3245 return new_d_Add(dbgi, compound_addr, ofs, mode);
3247 ir_entity *irentity = entry->compound_member.entity;
3248 assert(irentity != NULL);
3249 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3253 static ir_node *select_to_firm(const select_expression_t *expression)
3255 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3256 ir_node *addr = select_addr(expression);
3257 type_t *type = revert_automatic_type_conversion(
3258 (const expression_t*) expression);
3259 type = skip_typeref(type);
3261 entity_t *entry = expression->compound_entry;
3262 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3263 type_t *entry_type = skip_typeref(entry->declaration.type);
3265 if (entry_type->kind == TYPE_BITFIELD) {
3266 return bitfield_extract_to_firm(expression, addr);
3269 return deref_address(dbgi, type, addr);
3272 /* Values returned by __builtin_classify_type. */
3273 typedef enum gcc_type_class
3279 enumeral_type_class,
3282 reference_type_class,
3286 function_type_class,
3297 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3299 type_t *type = expr->type_expression->base.type;
3301 /* FIXME gcc returns different values depending on whether compiling C or C++
3302 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3305 type = skip_typeref(type);
3306 switch (type->kind) {
3308 const atomic_type_t *const atomic_type = &type->atomic;
3309 switch (atomic_type->akind) {
3310 /* should not be reached */
3311 case ATOMIC_TYPE_INVALID:
3315 /* gcc cannot do that */
3316 case ATOMIC_TYPE_VOID:
3317 tc = void_type_class;
3320 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3321 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3322 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3323 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3324 case ATOMIC_TYPE_SHORT:
3325 case ATOMIC_TYPE_USHORT:
3326 case ATOMIC_TYPE_INT:
3327 case ATOMIC_TYPE_UINT:
3328 case ATOMIC_TYPE_LONG:
3329 case ATOMIC_TYPE_ULONG:
3330 case ATOMIC_TYPE_LONGLONG:
3331 case ATOMIC_TYPE_ULONGLONG:
3332 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3333 tc = integer_type_class;
3336 case ATOMIC_TYPE_FLOAT:
3337 case ATOMIC_TYPE_DOUBLE:
3338 case ATOMIC_TYPE_LONG_DOUBLE:
3339 tc = real_type_class;
3342 panic("Unexpected atomic type in classify_type_to_firm().");
3345 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3346 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3347 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3348 case TYPE_ARRAY: /* gcc handles this as pointer */
3349 case TYPE_FUNCTION: /* gcc handles this as pointer */
3350 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3351 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3352 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3354 /* gcc handles this as integer */
3355 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3357 /* gcc classifies the referenced type */
3358 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3361 /* typedef/typeof should be skipped already */
3368 panic("unexpected TYPE classify_type_to_firm().");
3372 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3373 ir_tarval *const tv = new_tarval_from_long(tc, mode_int);
3374 return new_d_Const(dbgi, tv);
3377 static ir_node *function_name_to_firm(
3378 const funcname_expression_t *const expr)
3380 switch(expr->kind) {
3381 case FUNCNAME_FUNCTION:
3382 case FUNCNAME_PRETTY_FUNCTION:
3383 case FUNCNAME_FUNCDNAME:
3384 if (current_function_name == NULL) {
3385 const source_position_t *const src_pos = &expr->base.source_position;
3386 const char *name = current_function_entity->base.symbol->string;
3387 const string_t string = { name, strlen(name) + 1 };
3388 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3390 return current_function_name;
3391 case FUNCNAME_FUNCSIG:
3392 if (current_funcsig == NULL) {
3393 const source_position_t *const src_pos = &expr->base.source_position;
3394 ir_entity *ent = get_irg_entity(current_ir_graph);
3395 const char *const name = get_entity_ld_name(ent);
3396 const string_t string = { name, strlen(name) + 1 };
3397 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3399 return current_funcsig;
3401 panic("Unsupported function name");
3404 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3406 statement_t *statement = expr->statement;
3408 assert(statement->kind == STATEMENT_COMPOUND);
3409 return compound_statement_to_firm(&statement->compound);
3412 static ir_node *va_start_expression_to_firm(
3413 const va_start_expression_t *const expr)
3415 type_t *const type = current_function_entity->declaration.type;
3416 ir_type *const method_type = get_ir_type(type);
3417 int const n = get_method_n_params(method_type) - 1;
3418 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3419 ir_node *const frame = get_irg_frame(current_ir_graph);
3420 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3421 ir_node *const no_mem = new_NoMem();
3422 ir_node *const arg_sel =
3423 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3425 type_t *const param_type = expr->parameter->base.type;
3426 ir_node *const cnst = get_type_size_node(param_type);
3427 ir_mode *const mode = get_irn_mode(cnst);
3428 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3429 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3430 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3431 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3432 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3433 set_value_for_expression(expr->ap, add);
3438 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3440 type_t *const type = expr->base.type;
3441 expression_t *const ap_expr = expr->ap;
3442 ir_node *const ap_addr = expression_to_addr(ap_expr);
3443 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3444 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3445 ir_node *const res = deref_address(dbgi, type, ap);
3447 ir_node *const cnst = get_type_size_node(expr->base.type);
3448 ir_mode *const mode = get_irn_mode(cnst);
3449 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3450 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3451 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3452 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3453 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3455 set_value_for_expression_addr(ap_expr, add, ap_addr);
3461 * Generate Firm for a va_copy expression.
3463 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3465 ir_node *const src = expression_to_firm(expr->src);
3466 set_value_for_expression(expr->dst, src);
3470 static ir_node *dereference_addr(const unary_expression_t *const expression)
3472 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3473 return expression_to_firm(expression->value);
3477 * Returns a IR-node representing an lvalue of the given expression.
3479 static ir_node *expression_to_addr(const expression_t *expression)
3481 switch(expression->kind) {
3482 case EXPR_ARRAY_ACCESS:
3483 return array_access_addr(&expression->array_access);
3485 return call_expression_to_firm(&expression->call);
3486 case EXPR_COMPOUND_LITERAL:
3487 return compound_literal_to_firm(&expression->compound_literal);
3488 case EXPR_REFERENCE:
3489 return reference_addr(&expression->reference);
3491 return select_addr(&expression->select);
3492 case EXPR_UNARY_DEREFERENCE:
3493 return dereference_addr(&expression->unary);
3497 panic("trying to get address of non-lvalue");
3500 static ir_node *builtin_constant_to_firm(
3501 const builtin_constant_expression_t *expression)
3503 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3506 if (is_constant_expression(expression->value)) {
3511 return new_Const_long(mode, v);
3514 static ir_node *builtin_types_compatible_to_firm(
3515 const builtin_types_compatible_expression_t *expression)
3517 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3518 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3519 long const value = types_compatible(left, right) ? 1 : 0;
3520 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3521 return new_Const_long(mode, value);
3524 static ir_node *get_label_block(label_t *label)
3526 if (label->block != NULL)
3527 return label->block;
3529 /* beware: might be called from create initializer with current_ir_graph
3530 * set to const_code_irg. */
3531 ir_graph *rem = current_ir_graph;
3532 current_ir_graph = current_function;
3534 ir_node *block = new_immBlock();
3536 label->block = block;
3538 ARR_APP1(label_t *, all_labels, label);
3540 current_ir_graph = rem;
3545 * Pointer to a label. This is used for the
3546 * GNU address-of-label extension.
3548 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3550 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3551 ir_node *block = get_label_block(label->label);
3552 ir_entity *entity = create_Block_entity(block);
3554 symconst_symbol value;
3555 value.entity_p = entity;
3556 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3560 * creates firm nodes for an expression. The difference between this function
3561 * and expression_to_firm is, that this version might produce mode_b nodes
3562 * instead of mode_Is.
3564 static ir_node *_expression_to_firm(const expression_t *expression)
3567 if (!constant_folding) {
3568 assert(!expression->base.transformed);
3569 ((expression_t*) expression)->base.transformed = true;
3573 switch (expression->kind) {
3575 return literal_to_firm(&expression->literal);
3576 case EXPR_STRING_LITERAL:
3577 return string_to_firm(&expression->base.source_position, "str.%u",
3578 &expression->literal.value);
3579 case EXPR_WIDE_STRING_LITERAL:
3580 return wide_string_literal_to_firm(&expression->string_literal);
3581 case EXPR_REFERENCE:
3582 return reference_expression_to_firm(&expression->reference);
3583 case EXPR_REFERENCE_ENUM_VALUE:
3584 return reference_expression_enum_value_to_firm(&expression->reference);
3586 return call_expression_to_firm(&expression->call);
3588 return unary_expression_to_firm(&expression->unary);
3590 return binary_expression_to_firm(&expression->binary);
3591 case EXPR_ARRAY_ACCESS:
3592 return array_access_to_firm(&expression->array_access);
3594 return sizeof_to_firm(&expression->typeprop);
3596 return alignof_to_firm(&expression->typeprop);
3597 case EXPR_CONDITIONAL:
3598 return conditional_to_firm(&expression->conditional);
3600 return select_to_firm(&expression->select);
3601 case EXPR_CLASSIFY_TYPE:
3602 return classify_type_to_firm(&expression->classify_type);
3604 return function_name_to_firm(&expression->funcname);
3605 case EXPR_STATEMENT:
3606 return statement_expression_to_firm(&expression->statement);
3608 return va_start_expression_to_firm(&expression->va_starte);
3610 return va_arg_expression_to_firm(&expression->va_arge);
3612 return va_copy_expression_to_firm(&expression->va_copye);
3613 case EXPR_BUILTIN_CONSTANT_P:
3614 return builtin_constant_to_firm(&expression->builtin_constant);
3615 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3616 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3618 return offsetof_to_firm(&expression->offsetofe);
3619 case EXPR_COMPOUND_LITERAL:
3620 return compound_literal_to_firm(&expression->compound_literal);
3621 case EXPR_LABEL_ADDRESS:
3622 return label_address_to_firm(&expression->label_address);
3628 panic("invalid expression found");
3632 * Check if a given expression is a GNU __builtin_expect() call.
3634 static bool is_builtin_expect(const expression_t *expression)
3636 if (expression->kind != EXPR_CALL)
3639 expression_t *function = expression->call.function;
3640 if (function->kind != EXPR_REFERENCE)
3642 reference_expression_t *ref = &function->reference;
3643 if (ref->entity->kind != ENTITY_FUNCTION ||
3644 ref->entity->function.btk != bk_gnu_builtin_expect)
3650 static bool produces_mode_b(const expression_t *expression)
3652 switch (expression->kind) {
3653 case EXPR_BINARY_EQUAL:
3654 case EXPR_BINARY_NOTEQUAL:
3655 case EXPR_BINARY_LESS:
3656 case EXPR_BINARY_LESSEQUAL:
3657 case EXPR_BINARY_GREATER:
3658 case EXPR_BINARY_GREATEREQUAL:
3659 case EXPR_BINARY_ISGREATER:
3660 case EXPR_BINARY_ISGREATEREQUAL:
3661 case EXPR_BINARY_ISLESS:
3662 case EXPR_BINARY_ISLESSEQUAL:
3663 case EXPR_BINARY_ISLESSGREATER:
3664 case EXPR_BINARY_ISUNORDERED:
3665 case EXPR_UNARY_NOT:
3669 if (is_builtin_expect(expression)) {
3670 expression_t *argument = expression->call.arguments->expression;
3671 return produces_mode_b(argument);
3674 case EXPR_BINARY_COMMA:
3675 return produces_mode_b(expression->binary.right);
3682 static ir_node *expression_to_firm(const expression_t *expression)
3684 if (!produces_mode_b(expression)) {
3685 ir_node *res = _expression_to_firm(expression);
3686 assert(res == NULL || get_irn_mode(res) != mode_b);
3690 if (is_constant_expression(expression)) {
3691 ir_node *res = _expression_to_firm(expression);
3692 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3693 assert(is_Const(res));
3694 if (is_Const_null(res)) {
3695 return new_Const_long(mode, 0);
3697 return new_Const_long(mode, 1);
3701 /* we have to produce a 0/1 from the mode_b expression */
3702 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3703 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3704 return produce_condition_result(expression, mode, dbgi);
3708 * create a short-circuit expression evaluation that tries to construct
3709 * efficient control flow structures for &&, || and ! expressions
3711 static ir_node *create_condition_evaluation(const expression_t *expression,
3712 ir_node *true_block,
3713 ir_node *false_block)
3715 switch(expression->kind) {
3716 case EXPR_UNARY_NOT: {
3717 const unary_expression_t *unary_expression = &expression->unary;
3718 create_condition_evaluation(unary_expression->value, false_block,
3722 case EXPR_BINARY_LOGICAL_AND: {
3723 const binary_expression_t *binary_expression = &expression->binary;
3725 ir_node *extra_block = new_immBlock();
3726 create_condition_evaluation(binary_expression->left, extra_block,
3728 mature_immBlock(extra_block);
3729 set_cur_block(extra_block);
3730 create_condition_evaluation(binary_expression->right, true_block,
3734 case EXPR_BINARY_LOGICAL_OR: {
3735 const binary_expression_t *binary_expression = &expression->binary;
3737 ir_node *extra_block = new_immBlock();
3738 create_condition_evaluation(binary_expression->left, true_block,
3740 mature_immBlock(extra_block);
3741 set_cur_block(extra_block);
3742 create_condition_evaluation(binary_expression->right, true_block,
3750 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3751 ir_node *cond_expr = _expression_to_firm(expression);
3752 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3753 ir_node *cond = new_d_Cond(dbgi, condition);
3754 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3755 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3757 /* set branch prediction info based on __builtin_expect */
3758 if (is_builtin_expect(expression) && is_Cond(cond)) {
3759 call_argument_t *argument = expression->call.arguments->next;
3760 if (is_constant_expression(argument->expression)) {
3761 bool cnst = fold_constant_to_bool(argument->expression);
3762 cond_jmp_predicate pred;
3764 if (cnst == false) {
3765 pred = COND_JMP_PRED_FALSE;
3767 pred = COND_JMP_PRED_TRUE;
3769 set_Cond_jmp_pred(cond, pred);
3773 add_immBlock_pred(true_block, true_proj);
3774 add_immBlock_pred(false_block, false_proj);
3776 set_cur_block(NULL);
3780 static void create_variable_entity(entity_t *variable,
3781 declaration_kind_t declaration_kind,
3782 ir_type *parent_type)
3784 assert(variable->kind == ENTITY_VARIABLE);
3785 type_t *type = skip_typeref(variable->declaration.type);
3787 ident *const id = new_id_from_str(variable->base.symbol->string);
3788 ir_type *const irtype = get_ir_type(type);
3789 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3790 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3791 unsigned alignment = variable->declaration.alignment;
3793 set_entity_alignment(irentity, alignment);
3795 handle_decl_modifiers(irentity, variable);
3797 variable->declaration.kind = (unsigned char) declaration_kind;
3798 variable->variable.v.entity = irentity;
3799 set_entity_ld_ident(irentity, create_ld_ident(variable));
3801 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3802 set_entity_volatility(irentity, volatility_is_volatile);
3807 typedef struct type_path_entry_t type_path_entry_t;
3808 struct type_path_entry_t {
3810 ir_initializer_t *initializer;
3812 entity_t *compound_entry;
3815 typedef struct type_path_t type_path_t;
3816 struct type_path_t {
3817 type_path_entry_t *path;
3822 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3824 size_t len = ARR_LEN(path->path);
3826 for (size_t i = 0; i < len; ++i) {
3827 const type_path_entry_t *entry = & path->path[i];
3829 type_t *type = skip_typeref(entry->type);
3830 if (is_type_compound(type)) {
3831 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3832 } else if (is_type_array(type)) {
3833 fprintf(stderr, "[%u]", (unsigned) entry->index);
3835 fprintf(stderr, "-INVALID-");
3838 fprintf(stderr, " (");
3839 print_type(path->top_type);
3840 fprintf(stderr, ")");
3843 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3845 size_t len = ARR_LEN(path->path);
3847 return & path->path[len-1];
3850 static type_path_entry_t *append_to_type_path(type_path_t *path)
3852 size_t len = ARR_LEN(path->path);
3853 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3855 type_path_entry_t *result = & path->path[len];
3856 memset(result, 0, sizeof(result[0]));
3860 static size_t get_compound_member_count(const compound_type_t *type)
3862 compound_t *compound = type->compound;
3863 size_t n_members = 0;
3864 entity_t *member = compound->members.entities;
3865 for ( ; member != NULL; member = member->base.next) {
3872 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3874 type_t *orig_top_type = path->top_type;
3875 type_t *top_type = skip_typeref(orig_top_type);
3877 assert(is_type_compound(top_type) || is_type_array(top_type));
3879 if (ARR_LEN(path->path) == 0) {
3882 type_path_entry_t *top = get_type_path_top(path);
3883 ir_initializer_t *initializer = top->initializer;
3884 return get_initializer_compound_value(initializer, top->index);
3888 static void descend_into_subtype(type_path_t *path)
3890 type_t *orig_top_type = path->top_type;
3891 type_t *top_type = skip_typeref(orig_top_type);
3893 assert(is_type_compound(top_type) || is_type_array(top_type));
3895 ir_initializer_t *initializer = get_initializer_entry(path);
3897 type_path_entry_t *top = append_to_type_path(path);
3898 top->type = top_type;
3902 if (is_type_compound(top_type)) {
3903 compound_t *compound = top_type->compound.compound;
3904 entity_t *entry = compound->members.entities;
3906 top->compound_entry = entry;
3908 len = get_compound_member_count(&top_type->compound);
3909 if (entry != NULL) {
3910 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3911 path->top_type = entry->declaration.type;
3914 assert(is_type_array(top_type));
3915 assert(top_type->array.size > 0);
3918 path->top_type = top_type->array.element_type;
3919 len = top_type->array.size;
3921 if (initializer == NULL
3922 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3923 initializer = create_initializer_compound(len);
3924 /* we have to set the entry at the 2nd latest path entry... */
3925 size_t path_len = ARR_LEN(path->path);
3926 assert(path_len >= 1);
3928 type_path_entry_t *entry = & path->path[path_len-2];
3929 ir_initializer_t *tinitializer = entry->initializer;
3930 set_initializer_compound_value(tinitializer, entry->index,
3934 top->initializer = initializer;
3937 static void ascend_from_subtype(type_path_t *path)
3939 type_path_entry_t *top = get_type_path_top(path);
3941 path->top_type = top->type;
3943 size_t len = ARR_LEN(path->path);
3944 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3947 static void walk_designator(type_path_t *path, const designator_t *designator)
3949 /* designators start at current object type */
3950 ARR_RESIZE(type_path_entry_t, path->path, 1);
3952 for ( ; designator != NULL; designator = designator->next) {
3953 type_path_entry_t *top = get_type_path_top(path);
3954 type_t *orig_type = top->type;
3955 type_t *type = skip_typeref(orig_type);
3957 if (designator->symbol != NULL) {
3958 assert(is_type_compound(type));
3960 symbol_t *symbol = designator->symbol;
3962 compound_t *compound = type->compound.compound;
3963 entity_t *iter = compound->members.entities;
3964 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3965 if (iter->base.symbol == symbol) {
3966 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3970 assert(iter != NULL);
3972 /* revert previous initialisations of other union elements */
3973 if (type->kind == TYPE_COMPOUND_UNION) {
3974 ir_initializer_t *initializer = top->initializer;
3975 if (initializer != NULL
3976 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3977 /* are we writing to a new element? */
3978 ir_initializer_t *oldi
3979 = get_initializer_compound_value(initializer, index);
3980 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3981 /* clear initializer */
3983 = get_initializer_compound_n_entries(initializer);
3984 ir_initializer_t *nulli = get_initializer_null();
3985 for (size_t i = 0; i < len; ++i) {
3986 set_initializer_compound_value(initializer, i,
3993 top->type = orig_type;
3994 top->compound_entry = iter;
3996 orig_type = iter->declaration.type;
3998 expression_t *array_index = designator->array_index;
3999 assert(designator->array_index != NULL);
4000 assert(is_type_array(type));
4002 long index = fold_constant_to_int(array_index);
4005 if (type->array.size_constant) {
4006 long array_size = type->array.size;
4007 assert(index < array_size);
4011 top->type = orig_type;
4012 top->index = (size_t) index;
4013 orig_type = type->array.element_type;
4015 path->top_type = orig_type;
4017 if (designator->next != NULL) {
4018 descend_into_subtype(path);
4022 path->invalid = false;
4025 static void advance_current_object(type_path_t *path)
4027 if (path->invalid) {
4028 /* TODO: handle this... */
4029 panic("invalid initializer in ast2firm (excessive elements)");
4032 type_path_entry_t *top = get_type_path_top(path);
4034 type_t *type = skip_typeref(top->type);
4035 if (is_type_union(type)) {
4036 /* only the first element is initialized in unions */
4037 top->compound_entry = NULL;
4038 } else if (is_type_struct(type)) {
4039 entity_t *entry = top->compound_entry;
4042 entry = entry->base.next;
4043 top->compound_entry = entry;
4044 if (entry != NULL) {
4045 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
4046 path->top_type = entry->declaration.type;
4050 assert(is_type_array(type));
4053 if (!type->array.size_constant || top->index < type->array.size) {
4058 /* we're past the last member of the current sub-aggregate, try if we
4059 * can ascend in the type hierarchy and continue with another subobject */
4060 size_t len = ARR_LEN(path->path);
4063 ascend_from_subtype(path);
4064 advance_current_object(path);
4066 path->invalid = true;
4071 static ir_initializer_t *create_ir_initializer(
4072 const initializer_t *initializer, type_t *type);
4074 static ir_initializer_t *create_ir_initializer_value(
4075 const initializer_value_t *initializer)
4077 if (is_type_compound(initializer->value->base.type)) {
4078 panic("initializer creation for compounds not implemented yet");
4080 type_t *type = initializer->value->base.type;
4081 expression_t *expr = initializer->value;
4082 if (initializer_use_bitfield_basetype) {
4083 type_t *skipped = skip_typeref(type);
4084 if (skipped->kind == TYPE_BITFIELD) {
4085 /* remove the bitfield cast... */
4086 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4087 expr = expr->unary.value;
4088 type = skipped->bitfield.base_type;
4091 ir_node *value = expression_to_firm(expr);
4092 ir_mode *mode = get_ir_mode_storage(type);
4093 value = create_conv(NULL, value, mode);
4094 return create_initializer_const(value);
4097 /** test wether type can be initialized by a string constant */
4098 static bool is_string_type(type_t *type)
4101 if (is_type_pointer(type)) {
4102 inner = skip_typeref(type->pointer.points_to);
4103 } else if(is_type_array(type)) {
4104 inner = skip_typeref(type->array.element_type);
4109 return is_type_integer(inner);
4112 static ir_initializer_t *create_ir_initializer_list(
4113 const initializer_list_t *initializer, type_t *type)
4116 memset(&path, 0, sizeof(path));
4117 path.top_type = type;
4118 path.path = NEW_ARR_F(type_path_entry_t, 0);
4120 descend_into_subtype(&path);
4122 for (size_t i = 0; i < initializer->len; ++i) {
4123 const initializer_t *sub_initializer = initializer->initializers[i];
4125 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4126 walk_designator(&path, sub_initializer->designator.designator);
4130 if (sub_initializer->kind == INITIALIZER_VALUE) {
4131 /* we might have to descend into types until we're at a scalar
4134 type_t *orig_top_type = path.top_type;
4135 type_t *top_type = skip_typeref(orig_top_type);
4137 if (is_type_scalar(top_type))
4139 descend_into_subtype(&path);
4141 } else if (sub_initializer->kind == INITIALIZER_STRING
4142 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4143 /* we might have to descend into types until we're at a scalar
4146 type_t *orig_top_type = path.top_type;
4147 type_t *top_type = skip_typeref(orig_top_type);
4149 if (is_string_type(top_type))
4151 descend_into_subtype(&path);
4155 ir_initializer_t *sub_irinitializer
4156 = create_ir_initializer(sub_initializer, path.top_type);
4158 size_t path_len = ARR_LEN(path.path);
4159 assert(path_len >= 1);
4160 type_path_entry_t *entry = & path.path[path_len-1];
4161 ir_initializer_t *tinitializer = entry->initializer;
4162 set_initializer_compound_value(tinitializer, entry->index,
4165 advance_current_object(&path);
4168 assert(ARR_LEN(path.path) >= 1);
4169 ir_initializer_t *result = path.path[0].initializer;
4170 DEL_ARR_F(path.path);
4175 static ir_initializer_t *create_ir_initializer_string(
4176 const initializer_string_t *initializer, type_t *type)
4178 type = skip_typeref(type);
4180 size_t string_len = initializer->string.size;
4181 assert(type->kind == TYPE_ARRAY);
4182 assert(type->array.size_constant);
4183 size_t len = type->array.size;
4184 ir_initializer_t *irinitializer = create_initializer_compound(len);
4186 const char *string = initializer->string.begin;
4187 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4189 for (size_t i = 0; i < len; ++i) {
4194 ir_tarval *tv = new_tarval_from_long(c, mode);
4195 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4197 set_initializer_compound_value(irinitializer, i, char_initializer);
4200 return irinitializer;
4203 static ir_initializer_t *create_ir_initializer_wide_string(
4204 const initializer_wide_string_t *initializer, type_t *type)
4206 assert(type->kind == TYPE_ARRAY);
4207 assert(type->array.size_constant);
4208 size_t len = type->array.size;
4209 size_t string_len = wstrlen(&initializer->string);
4210 ir_initializer_t *irinitializer = create_initializer_compound(len);
4212 const char *p = initializer->string.begin;
4213 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4215 for (size_t i = 0; i < len; ++i) {
4217 if (i < string_len) {
4218 c = read_utf8_char(&p);
4220 ir_tarval *tv = new_tarval_from_long(c, mode);
4221 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4223 set_initializer_compound_value(irinitializer, i, char_initializer);
4226 return irinitializer;
4229 static ir_initializer_t *create_ir_initializer(
4230 const initializer_t *initializer, type_t *type)
4232 switch(initializer->kind) {
4233 case INITIALIZER_STRING:
4234 return create_ir_initializer_string(&initializer->string, type);
4236 case INITIALIZER_WIDE_STRING:
4237 return create_ir_initializer_wide_string(&initializer->wide_string,
4240 case INITIALIZER_LIST:
4241 return create_ir_initializer_list(&initializer->list, type);
4243 case INITIALIZER_VALUE:
4244 return create_ir_initializer_value(&initializer->value);
4246 case INITIALIZER_DESIGNATOR:
4247 panic("unexpected designator initializer found");
4249 panic("unknown initializer");
4252 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4253 * are elements [...] the remainder of the aggregate shall be initialized
4254 * implicitly the same as objects that have static storage duration. */
4255 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4258 /* for unions we must NOT do anything for null initializers */
4259 ir_type *owner = get_entity_owner(entity);
4260 if (is_Union_type(owner)) {
4264 ir_type *ent_type = get_entity_type(entity);
4265 /* create sub-initializers for a compound type */
4266 if (is_compound_type(ent_type)) {
4267 unsigned n_members = get_compound_n_members(ent_type);
4268 for (unsigned n = 0; n < n_members; ++n) {
4269 ir_entity *member = get_compound_member(ent_type, n);
4270 ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4272 create_dynamic_null_initializer(member, dbgi, addr);
4276 if (is_Array_type(ent_type)) {
4277 assert(has_array_upper_bound(ent_type, 0));
4278 long n = get_array_upper_bound_int(ent_type, 0);
4279 for (long i = 0; i < n; ++i) {
4280 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4281 ir_node *cnst = new_d_Const(dbgi, index_tv);
4282 ir_node *in[1] = { cnst };
4283 ir_entity *arrent = get_array_element_entity(ent_type);
4284 ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4286 create_dynamic_null_initializer(arrent, dbgi, addr);
4291 ir_mode *value_mode = get_type_mode(ent_type);
4292 ir_node *node = new_Const_long(value_mode, 0);
4294 /* is it a bitfield type? */
4295 if (is_Primitive_type(ent_type) &&
4296 get_primitive_base_type(ent_type) != NULL) {
4297 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4301 ir_node *mem = get_store();
4302 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4303 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4307 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4308 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4310 switch(get_initializer_kind(initializer)) {
4311 case IR_INITIALIZER_NULL:
4312 create_dynamic_null_initializer(entity, dbgi, base_addr);
4314 case IR_INITIALIZER_CONST: {
4315 ir_node *node = get_initializer_const_value(initializer);
4316 ir_type *ent_type = get_entity_type(entity);
4318 /* is it a bitfield type? */
4319 if (is_Primitive_type(ent_type) &&
4320 get_primitive_base_type(ent_type) != NULL) {
4321 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4325 assert(get_type_mode(type) == get_irn_mode(node));
4326 ir_node *mem = get_store();
4327 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4328 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4332 case IR_INITIALIZER_TARVAL: {
4333 ir_tarval *tv = get_initializer_tarval_value(initializer);
4334 ir_node *cnst = new_d_Const(dbgi, tv);
4335 ir_type *ent_type = get_entity_type(entity);
4337 /* is it a bitfield type? */
4338 if (is_Primitive_type(ent_type) &&
4339 get_primitive_base_type(ent_type) != NULL) {
4340 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4344 assert(get_type_mode(type) == get_tarval_mode(tv));
4345 ir_node *mem = get_store();
4346 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4347 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4351 case IR_INITIALIZER_COMPOUND: {
4352 assert(is_compound_type(type) || is_Array_type(type));
4354 if (is_Array_type(type)) {
4355 assert(has_array_upper_bound(type, 0));
4356 n_members = get_array_upper_bound_int(type, 0);
4358 n_members = get_compound_n_members(type);
4361 if (get_initializer_compound_n_entries(initializer)
4362 != (unsigned) n_members)
4363 panic("initializer doesn't match compound type");
4365 for (int i = 0; i < n_members; ++i) {
4368 ir_entity *sub_entity;
4369 if (is_Array_type(type)) {
4370 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4371 ir_node *cnst = new_d_Const(dbgi, index_tv);
4372 ir_node *in[1] = { cnst };
4373 irtype = get_array_element_type(type);
4374 sub_entity = get_array_element_entity(type);
4375 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4378 sub_entity = get_compound_member(type, i);
4379 irtype = get_entity_type(sub_entity);
4380 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4384 ir_initializer_t *sub_init
4385 = get_initializer_compound_value(initializer, i);
4387 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4394 panic("invalid IR_INITIALIZER found");
4397 static void create_dynamic_initializer(ir_initializer_t *initializer,
4398 dbg_info *dbgi, ir_entity *entity)
4400 ir_node *frame = get_irg_frame(current_ir_graph);
4401 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4402 ir_type *type = get_entity_type(entity);
4404 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4407 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4408 ir_entity *entity, type_t *type)
4410 ir_node *memory = get_store();
4411 ir_node *nomem = new_NoMem();
4412 ir_node *frame = get_irg_frame(current_ir_graph);
4413 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4415 if (initializer->kind == INITIALIZER_VALUE) {
4416 initializer_value_t *initializer_value = &initializer->value;
4418 ir_node *value = expression_to_firm(initializer_value->value);
4419 type = skip_typeref(type);
4420 assign_value(dbgi, addr, type, value);
4424 if (!is_constant_initializer(initializer)) {
4425 bool old_initializer_use_bitfield_basetype
4426 = initializer_use_bitfield_basetype;
4427 initializer_use_bitfield_basetype = true;
4428 ir_initializer_t *irinitializer
4429 = create_ir_initializer(initializer, type);
4430 initializer_use_bitfield_basetype
4431 = old_initializer_use_bitfield_basetype;
4433 create_dynamic_initializer(irinitializer, dbgi, entity);
4437 /* create the ir_initializer */
4438 ir_graph *const old_current_ir_graph = current_ir_graph;
4439 current_ir_graph = get_const_code_irg();
4441 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4443 assert(current_ir_graph == get_const_code_irg());
4444 current_ir_graph = old_current_ir_graph;
4446 /* create a "template" entity which is copied to the entity on the stack */
4447 ident *const id = id_unique("initializer.%u");
4448 ir_type *const irtype = get_ir_type(type);
4449 ir_type *const global_type = get_glob_type();
4450 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4451 set_entity_ld_ident(init_entity, id);
4453 set_entity_visibility(init_entity, ir_visibility_private);
4454 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4456 set_entity_initializer(init_entity, irinitializer);
4458 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4459 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4461 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4462 set_store(copyb_mem);
4465 static void create_initializer_local_variable_entity(entity_t *entity)
4467 assert(entity->kind == ENTITY_VARIABLE);
4468 initializer_t *initializer = entity->variable.initializer;
4469 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4470 ir_entity *irentity = entity->variable.v.entity;
4471 type_t *type = entity->declaration.type;
4473 create_local_initializer(initializer, dbgi, irentity, type);
4476 static void create_variable_initializer(entity_t *entity)
4478 assert(entity->kind == ENTITY_VARIABLE);
4479 initializer_t *initializer = entity->variable.initializer;
4480 if (initializer == NULL)
4483 declaration_kind_t declaration_kind
4484 = (declaration_kind_t) entity->declaration.kind;
4485 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4486 create_initializer_local_variable_entity(entity);
4490 type_t *type = entity->declaration.type;
4491 type_qualifiers_t tq = get_type_qualifier(type, true);
4493 if (initializer->kind == INITIALIZER_VALUE) {
4494 initializer_value_t *initializer_value = &initializer->value;
4495 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4497 ir_node *value = expression_to_firm(initializer_value->value);
4499 type_t *type = initializer_value->value->base.type;
4500 ir_mode *mode = get_ir_mode_storage(type);
4501 value = create_conv(dbgi, value, mode);
4502 value = do_strict_conv(dbgi, value);
4504 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4505 set_value(entity->variable.v.value_number, value);
4507 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4509 ir_entity *irentity = entity->variable.v.entity;
4511 if (tq & TYPE_QUALIFIER_CONST
4512 && get_entity_owner(irentity) != get_tls_type()) {
4513 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4515 set_atomic_ent_value(irentity, value);
4518 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4519 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4521 ir_entity *irentity = entity->variable.v.entity;
4522 ir_initializer_t *irinitializer
4523 = create_ir_initializer(initializer, type);
4525 if (tq & TYPE_QUALIFIER_CONST) {
4526 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4528 set_entity_initializer(irentity, irinitializer);
4532 static void create_variable_length_array(entity_t *entity)
4534 assert(entity->kind == ENTITY_VARIABLE);
4535 assert(entity->variable.initializer == NULL);
4537 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4538 entity->variable.v.vla_base = NULL;
4540 /* TODO: record VLA somewhere so we create the free node when we leave
4544 static void allocate_variable_length_array(entity_t *entity)
4546 assert(entity->kind == ENTITY_VARIABLE);
4547 assert(entity->variable.initializer == NULL);
4548 assert(get_cur_block() != NULL);
4550 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4551 type_t *type = entity->declaration.type;
4552 ir_type *el_type = get_ir_type(type->array.element_type);
4554 /* make sure size_node is calculated */
4555 get_type_size_node(type);
4556 ir_node *elems = type->array.size_node;
4557 ir_node *mem = get_store();
4558 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4560 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4561 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4564 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4565 entity->variable.v.vla_base = addr;
4569 * Creates a Firm local variable from a declaration.
4571 static void create_local_variable(entity_t *entity)
4573 assert(entity->kind == ENTITY_VARIABLE);
4574 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4576 bool needs_entity = entity->variable.address_taken;
4577 type_t *type = skip_typeref(entity->declaration.type);
4579 /* is it a variable length array? */
4580 if (is_type_array(type) && !type->array.size_constant) {
4581 create_variable_length_array(entity);
4583 } else if (is_type_array(type) || is_type_compound(type)) {
4584 needs_entity = true;
4585 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4586 needs_entity = true;
4590 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4591 create_variable_entity(entity,
4592 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4595 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4596 entity->variable.v.value_number = next_value_number_function;
4597 set_irg_loc_description(current_ir_graph, next_value_number_function,
4599 ++next_value_number_function;
4603 static void create_local_static_variable(entity_t *entity)
4605 assert(entity->kind == ENTITY_VARIABLE);
4606 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4608 type_t *type = skip_typeref(entity->declaration.type);
4609 ir_type *const var_type = entity->variable.thread_local ?
4610 get_tls_type() : get_glob_type();
4611 ir_type *const irtype = get_ir_type(type);
4612 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4614 size_t l = strlen(entity->base.symbol->string);
4615 char buf[l + sizeof(".%u")];
4616 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4617 ident *const id = id_unique(buf);
4618 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4620 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4621 set_entity_volatility(irentity, volatility_is_volatile);
4624 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4625 entity->variable.v.entity = irentity;
4627 set_entity_ld_ident(irentity, id);
4628 set_entity_visibility(irentity, ir_visibility_local);
4630 ir_graph *const old_current_ir_graph = current_ir_graph;
4631 current_ir_graph = get_const_code_irg();
4633 create_variable_initializer(entity);
4635 assert(current_ir_graph == get_const_code_irg());
4636 current_ir_graph = old_current_ir_graph;
4641 static void return_statement_to_firm(return_statement_t *statement)
4643 if (get_cur_block() == NULL)
4646 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4647 type_t *type = current_function_entity->declaration.type;
4648 ir_type *func_irtype = get_ir_type(type);
4653 if (get_method_n_ress(func_irtype) > 0) {
4654 ir_type *res_type = get_method_res_type(func_irtype, 0);
4656 if (statement->value != NULL) {
4657 ir_node *node = expression_to_firm(statement->value);
4658 if (!is_compound_type(res_type)) {
4659 type_t *type = statement->value->base.type;
4660 ir_mode *mode = get_ir_mode_storage(type);
4661 node = create_conv(dbgi, node, mode);
4662 node = do_strict_conv(dbgi, node);
4667 if (is_compound_type(res_type)) {
4670 mode = get_type_mode(res_type);
4672 in[0] = new_Unknown(mode);
4676 /* build return_value for its side effects */
4677 if (statement->value != NULL) {
4678 expression_to_firm(statement->value);
4683 ir_node *store = get_store();
4684 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4686 ir_node *end_block = get_irg_end_block(current_ir_graph);
4687 add_immBlock_pred(end_block, ret);
4689 set_cur_block(NULL);
4692 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4694 if (get_cur_block() == NULL)
4697 return expression_to_firm(statement->expression);
4700 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4702 entity_t *entity = compound->scope.entities;
4703 for ( ; entity != NULL; entity = entity->base.next) {
4704 if (!is_declaration(entity))
4707 create_local_declaration(entity);
4710 ir_node *result = NULL;
4711 statement_t *statement = compound->statements;
4712 for ( ; statement != NULL; statement = statement->base.next) {
4713 if (statement->base.next == NULL
4714 && statement->kind == STATEMENT_EXPRESSION) {
4715 result = expression_statement_to_firm(
4716 &statement->expression);
4719 statement_to_firm(statement);
4725 static void create_global_variable(entity_t *entity)
4727 ir_linkage linkage = IR_LINKAGE_DEFAULT;
4728 ir_visibility visibility = ir_visibility_default;
4729 ir_entity *irentity;
4730 assert(entity->kind == ENTITY_VARIABLE);
4732 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4733 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4734 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4735 case STORAGE_CLASS_NONE:
4736 visibility = ir_visibility_default;
4737 /* uninitialized globals get merged in C */
4738 if (entity->variable.initializer == NULL)
4739 linkage |= IR_LINKAGE_MERGE;
4741 case STORAGE_CLASS_TYPEDEF:
4742 case STORAGE_CLASS_AUTO:
4743 case STORAGE_CLASS_REGISTER:
4744 panic("invalid storage class for global var");
4747 ir_type *var_type = get_glob_type();
4748 if (entity->variable.thread_local) {
4749 var_type = get_tls_type();
4750 /* LINKAGE_MERGE not supported by current linkers */
4751 linkage &= ~IR_LINKAGE_MERGE;
4753 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4754 irentity = entity->variable.v.entity;
4755 add_entity_linkage(irentity, linkage);
4756 set_entity_visibility(irentity, visibility);
4759 static void create_local_declaration(entity_t *entity)
4761 assert(is_declaration(entity));
4763 /* construct type */
4764 (void) get_ir_type(entity->declaration.type);
4765 if (entity->base.symbol == NULL) {
4769 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4770 case STORAGE_CLASS_STATIC:
4771 if (entity->kind == ENTITY_FUNCTION) {
4772 (void)get_function_entity(entity, NULL);
4774 create_local_static_variable(entity);
4777 case STORAGE_CLASS_EXTERN:
4778 if (entity->kind == ENTITY_FUNCTION) {
4779 assert(entity->function.statement == NULL);
4780 (void)get_function_entity(entity, NULL);
4782 create_global_variable(entity);
4783 create_variable_initializer(entity);
4786 case STORAGE_CLASS_NONE:
4787 case STORAGE_CLASS_AUTO:
4788 case STORAGE_CLASS_REGISTER:
4789 if (entity->kind == ENTITY_FUNCTION) {
4790 if (entity->function.statement != NULL) {
4791 ir_type *owner = get_irg_frame_type(current_ir_graph);
4792 (void)get_function_entity(entity, owner);
4793 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4794 enqueue_inner_function(entity);
4796 (void)get_function_entity(entity, NULL);
4799 create_local_variable(entity);
4802 case STORAGE_CLASS_TYPEDEF:
4805 panic("invalid storage class found");
4808 static void initialize_local_declaration(entity_t *entity)
4810 if (entity->base.symbol == NULL)
4813 // no need to emit code in dead blocks
4814 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4815 && get_cur_block() == NULL)
4818 switch ((declaration_kind_t) entity->declaration.kind) {
4819 case DECLARATION_KIND_LOCAL_VARIABLE:
4820 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4821 create_variable_initializer(entity);
4824 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4825 allocate_variable_length_array(entity);
4828 case DECLARATION_KIND_COMPOUND_MEMBER:
4829 case DECLARATION_KIND_GLOBAL_VARIABLE:
4830 case DECLARATION_KIND_FUNCTION:
4831 case DECLARATION_KIND_INNER_FUNCTION:
4834 case DECLARATION_KIND_PARAMETER:
4835 case DECLARATION_KIND_PARAMETER_ENTITY:
4836 panic("can't initialize parameters");
4838 case DECLARATION_KIND_UNKNOWN:
4839 panic("can't initialize unknown declaration");
4841 panic("invalid declaration kind");
4844 static void declaration_statement_to_firm(declaration_statement_t *statement)
4846 entity_t *entity = statement->declarations_begin;
4850 entity_t *const last = statement->declarations_end;
4851 for ( ;; entity = entity->base.next) {
4852 if (is_declaration(entity)) {
4853 initialize_local_declaration(entity);
4854 } else if (entity->kind == ENTITY_TYPEDEF) {
4855 /* ยง6.7.7:3 Any array size expressions associated with variable length
4856 * array declarators are evaluated each time the declaration of the
4857 * typedef name is reached in the order of execution. */
4858 type_t *const type = skip_typeref(entity->typedefe.type);
4859 if (is_type_array(type) && type->array.is_vla)
4860 get_vla_size(&type->array);
4867 static void if_statement_to_firm(if_statement_t *statement)
4869 /* Create the condition. */
4870 ir_node *true_block = NULL;
4871 ir_node *false_block = NULL;
4872 if (get_cur_block() != NULL) {
4873 true_block = new_immBlock();
4874 false_block = new_immBlock();
4875 create_condition_evaluation(statement->condition, true_block, false_block);
4876 mature_immBlock(true_block);
4879 /* Create the false statement.
4880 * Hadle false before true, so if no false statement is present, then the
4881 * empty false block is reused as fallthrough block. */
4882 ir_node *fallthrough_block = NULL;
4883 if (statement->false_statement != NULL) {
4884 if (false_block != NULL) {
4885 mature_immBlock(false_block);
4887 set_cur_block(false_block);
4888 statement_to_firm(statement->false_statement);
4889 if (get_cur_block() != NULL) {
4890 fallthrough_block = new_immBlock();
4891 add_immBlock_pred(fallthrough_block, new_Jmp());
4894 fallthrough_block = false_block;
4897 /* Create the true statement. */
4898 set_cur_block(true_block);
4899 statement_to_firm(statement->true_statement);
4900 if (get_cur_block() != NULL) {
4901 if (fallthrough_block == NULL) {
4902 fallthrough_block = new_immBlock();
4904 add_immBlock_pred(fallthrough_block, new_Jmp());
4907 /* Handle the block after the if-statement. */
4908 if (fallthrough_block != NULL) {
4909 mature_immBlock(fallthrough_block);
4911 set_cur_block(fallthrough_block);
4914 static void while_statement_to_firm(while_statement_t *statement)
4916 /* Create the header block */
4917 ir_node *const header_block = new_immBlock();
4918 if (get_cur_block() != NULL) {
4919 add_immBlock_pred(header_block, new_Jmp());
4922 /* Create the condition. */
4923 ir_node * body_block;
4924 ir_node * false_block;
4925 expression_t *const cond = statement->condition;
4926 if (is_constant_expression(cond) && fold_constant_to_bool(cond)) {
4927 /* Shortcut for while (true). */
4928 body_block = header_block;
4931 keep_alive(header_block);
4932 keep_all_memory(header_block);
4934 body_block = new_immBlock();
4935 false_block = new_immBlock();
4937 set_cur_block(header_block);
4938 create_condition_evaluation(cond, body_block, false_block);
4939 mature_immBlock(body_block);
4942 ir_node *const old_continue_label = continue_label;
4943 ir_node *const old_break_label = break_label;
4944 continue_label = header_block;
4945 break_label = false_block;
4947 /* Create the loop body. */
4948 set_cur_block(body_block);
4949 statement_to_firm(statement->body);
4950 if (get_cur_block() != NULL) {
4951 add_immBlock_pred(header_block, new_Jmp());
4954 mature_immBlock(header_block);
4955 assert(false_block == NULL || false_block == break_label);
4956 false_block = break_label;
4957 if (false_block != NULL) {
4958 mature_immBlock(false_block);
4960 set_cur_block(false_block);
4962 assert(continue_label == header_block);
4963 continue_label = old_continue_label;
4964 break_label = old_break_label;
4967 static void do_while_statement_to_firm(do_while_statement_t *statement)
4969 /* create the header block */
4970 ir_node *header_block = new_immBlock();
4973 ir_node *body_block = new_immBlock();
4974 if (get_cur_block() != NULL) {
4975 add_immBlock_pred(body_block, new_Jmp());
4978 ir_node *old_continue_label = continue_label;
4979 ir_node *old_break_label = break_label;
4980 continue_label = header_block;
4983 set_cur_block(body_block);
4984 statement_to_firm(statement->body);
4985 ir_node *false_block = break_label;
4987 assert(continue_label == header_block);
4988 continue_label = old_continue_label;
4989 break_label = old_break_label;
4991 if (get_cur_block() != NULL) {
4992 ir_node *body_jmp = new_Jmp();
4993 add_immBlock_pred(header_block, body_jmp);
4994 mature_immBlock(header_block);
4997 if (false_block == NULL) {
4998 false_block = new_immBlock();
5001 /* create the condition */
5002 set_cur_block(header_block);
5004 create_condition_evaluation(statement->condition, body_block, false_block);
5005 mature_immBlock(body_block);
5006 mature_immBlock(header_block);
5007 mature_immBlock(false_block);
5009 set_cur_block(false_block);
5012 static void for_statement_to_firm(for_statement_t *statement)
5014 /* create declarations */
5015 entity_t *entity = statement->scope.entities;
5016 for ( ; entity != NULL; entity = entity->base.next) {
5017 if (!is_declaration(entity))
5020 create_local_declaration(entity);
5023 ir_node *jmp = NULL;
5024 if (get_cur_block() != NULL) {
5025 entity = statement->scope.entities;
5026 for ( ; entity != NULL; entity = entity->base.next) {
5027 if (!is_declaration(entity))
5030 initialize_local_declaration(entity);
5033 if (statement->initialisation != NULL) {
5034 expression_to_firm(statement->initialisation);
5041 /* create the step block */
5042 ir_node *const step_block = new_immBlock();
5043 set_cur_block(step_block);
5044 if (statement->step != NULL) {
5045 expression_to_firm(statement->step);
5047 ir_node *const step_jmp = new_Jmp();
5049 /* create the header block */
5050 ir_node *const header_block = new_immBlock();
5051 set_cur_block(header_block);
5053 add_immBlock_pred(header_block, jmp);
5055 add_immBlock_pred(header_block, step_jmp);
5057 /* the false block */
5058 ir_node *const false_block = new_immBlock();
5061 ir_node *const old_continue_label = continue_label;
5062 ir_node *const old_break_label = break_label;
5063 continue_label = step_block;
5064 break_label = false_block;
5066 ir_node *const body_block = new_immBlock();
5067 set_cur_block(body_block);
5068 statement_to_firm(statement->body);
5070 assert(continue_label == step_block);
5071 assert(break_label == false_block);
5072 continue_label = old_continue_label;
5073 break_label = old_break_label;
5075 if (get_cur_block() != NULL) {
5076 add_immBlock_pred(step_block, new_Jmp());
5079 /* create the condition */
5080 set_cur_block(header_block);
5081 if (statement->condition != NULL) {
5082 create_condition_evaluation(statement->condition, body_block,
5085 keep_alive(header_block);
5086 keep_all_memory(header_block);
5087 add_immBlock_pred(body_block, new_Jmp());
5090 mature_immBlock(body_block);
5091 mature_immBlock(false_block);
5092 mature_immBlock(step_block);
5093 mature_immBlock(header_block);
5094 mature_immBlock(false_block);
5096 set_cur_block(false_block);
5099 static void create_jump_statement(const statement_t *statement,
5100 ir_node *target_block)
5102 if (get_cur_block() == NULL)
5105 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5106 ir_node *jump = new_d_Jmp(dbgi);
5107 add_immBlock_pred(target_block, jump);
5109 set_cur_block(NULL);
5112 static ir_node *get_break_label(void)
5114 if (break_label == NULL) {
5115 break_label = new_immBlock();
5120 static void switch_statement_to_firm(switch_statement_t *statement)
5122 ir_node *first_block = NULL;
5123 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5124 ir_node *cond = NULL;
5126 if (get_cur_block() != NULL) {
5127 ir_node *expression = expression_to_firm(statement->expression);
5128 cond = new_d_Cond(dbgi, expression);
5129 first_block = get_cur_block();
5132 set_cur_block(NULL);
5134 ir_node *const old_switch_cond = current_switch_cond;
5135 ir_node *const old_break_label = break_label;
5136 const bool old_saw_default_label = saw_default_label;
5137 saw_default_label = false;
5138 current_switch_cond = cond;
5140 switch_statement_t *const old_switch = current_switch;
5141 current_switch = statement;
5143 /* determine a free number for the default label */
5144 unsigned long num_cases = 0;
5145 long default_proj_nr = 0;
5146 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5147 if (l->expression == NULL) {
5151 if (l->last_case >= l->first_case)
5152 num_cases += l->last_case - l->first_case + 1;
5153 if (l->last_case > default_proj_nr)
5154 default_proj_nr = l->last_case;
5157 if (default_proj_nr == INT_MAX) {
5158 /* Bad: an overflow will occur, we cannot be sure that the
5159 * maximum + 1 is a free number. Scan the values a second
5160 * time to find a free number.
5162 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5164 memset(bits, 0, (num_cases + 7) >> 3);
5165 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5166 if (l->expression == NULL) {
5170 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5171 if (start < num_cases && l->last_case >= 0) {
5172 unsigned long end = (unsigned long)l->last_case < num_cases ?
5173 (unsigned long)l->last_case : num_cases - 1;
5174 for (unsigned long cns = start; cns <= end; ++cns) {
5175 bits[cns >> 3] |= (1 << (cns & 7));
5179 /* We look at the first num_cases constants:
5180 * Either they are dense, so we took the last (num_cases)
5181 * one, or they are not dense, so we will find one free
5185 for (i = 0; i < num_cases; ++i)
5186 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5190 default_proj_nr = i;
5194 statement->default_proj_nr = default_proj_nr;
5195 /* safety check: cond might already be folded to a Bad */
5196 if (cond != NULL && is_Cond(cond)) {
5197 set_Cond_default_proj(cond, default_proj_nr);
5200 statement_to_firm(statement->body);
5202 if (get_cur_block() != NULL) {
5203 ir_node *jmp = new_Jmp();
5204 add_immBlock_pred(get_break_label(), jmp);
5207 if (!saw_default_label && first_block != NULL) {
5208 set_cur_block(first_block);
5209 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5210 add_immBlock_pred(get_break_label(), proj);
5213 if (break_label != NULL) {
5214 mature_immBlock(break_label);
5216 set_cur_block(break_label);
5218 assert(current_switch_cond == cond);
5219 current_switch = old_switch;
5220 current_switch_cond = old_switch_cond;
5221 break_label = old_break_label;
5222 saw_default_label = old_saw_default_label;
5225 static void case_label_to_firm(const case_label_statement_t *statement)
5227 if (statement->is_empty_range)
5230 ir_node *block = new_immBlock();
5232 if (get_cur_block() != NULL) {
5233 /* Fallthrough from previous case */
5234 add_immBlock_pred(block, new_Jmp());
5237 if (current_switch_cond != NULL) {
5238 set_cur_block(get_nodes_block(current_switch_cond));
5239 dbg_info *const dbgi = get_dbg_info(&statement->base.source_position);
5240 if (statement->expression != NULL) {
5241 long pn = statement->first_case;
5242 long end_pn = statement->last_case;
5243 assert(pn <= end_pn);
5244 /* create jumps for all cases in the given range */
5246 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5247 add_immBlock_pred(block, proj);
5248 } while (pn++ < end_pn);
5250 saw_default_label = true;
5251 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5252 current_switch->default_proj_nr);
5253 add_immBlock_pred(block, proj);
5257 mature_immBlock(block);
5258 set_cur_block(block);
5260 statement_to_firm(statement->statement);
5263 static void label_to_firm(const label_statement_t *statement)
5265 ir_node *block = get_label_block(statement->label);
5267 if (get_cur_block() != NULL) {
5268 ir_node *jmp = new_Jmp();
5269 add_immBlock_pred(block, jmp);
5272 set_cur_block(block);
5274 keep_all_memory(block);
5276 statement_to_firm(statement->statement);
5279 static void goto_to_firm(const goto_statement_t *statement)
5281 if (get_cur_block() == NULL)
5284 if (statement->expression) {
5285 ir_node *irn = expression_to_firm(statement->expression);
5286 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5287 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5289 set_irn_link(ijmp, ijmp_list);
5292 ir_node *block = get_label_block(statement->label);
5293 ir_node *jmp = new_Jmp();
5294 add_immBlock_pred(block, jmp);
5296 set_cur_block(NULL);
5299 static void asm_statement_to_firm(const asm_statement_t *statement)
5301 bool needs_memory = false;
5303 if (statement->is_volatile) {
5304 needs_memory = true;
5307 size_t n_clobbers = 0;
5308 asm_clobber_t *clobber = statement->clobbers;
5309 for ( ; clobber != NULL; clobber = clobber->next) {
5310 const char *clobber_str = clobber->clobber.begin;
5312 if (!be_is_valid_clobber(clobber_str)) {
5313 errorf(&statement->base.source_position,
5314 "invalid clobber '%s' specified", clobber->clobber);
5318 if (strcmp(clobber_str, "memory") == 0) {
5319 needs_memory = true;
5323 ident *id = new_id_from_str(clobber_str);
5324 obstack_ptr_grow(&asm_obst, id);
5327 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5328 ident **clobbers = NULL;
5329 if (n_clobbers > 0) {
5330 clobbers = obstack_finish(&asm_obst);
5333 size_t n_inputs = 0;
5334 asm_argument_t *argument = statement->inputs;
5335 for ( ; argument != NULL; argument = argument->next)
5337 size_t n_outputs = 0;
5338 argument = statement->outputs;
5339 for ( ; argument != NULL; argument = argument->next)
5342 unsigned next_pos = 0;
5344 ir_node *ins[n_inputs + n_outputs + 1];
5347 ir_asm_constraint tmp_in_constraints[n_outputs];
5349 const expression_t *out_exprs[n_outputs];
5350 ir_node *out_addrs[n_outputs];
5351 size_t out_size = 0;
5353 argument = statement->outputs;
5354 for ( ; argument != NULL; argument = argument->next) {
5355 const char *constraints = argument->constraints.begin;
5356 asm_constraint_flags_t asm_flags
5357 = be_parse_asm_constraints(constraints);
5359 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5360 warningf(&statement->base.source_position,
5361 "some constraints in '%s' are not supported", constraints);
5363 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5364 errorf(&statement->base.source_position,
5365 "some constraints in '%s' are invalid", constraints);
5368 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5369 errorf(&statement->base.source_position,
5370 "no write flag specified for output constraints '%s'",
5375 unsigned pos = next_pos++;
5376 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5377 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5378 expression_t *expr = argument->expression;
5379 ir_node *addr = expression_to_addr(expr);
5380 /* in+output, construct an artifical same_as constraint on the
5382 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5384 ir_node *value = get_value_from_lvalue(expr, addr);
5386 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5388 ir_asm_constraint constraint;
5389 constraint.pos = pos;
5390 constraint.constraint = new_id_from_str(buf);
5391 constraint.mode = get_ir_mode_storage(expr->base.type);
5392 tmp_in_constraints[in_size] = constraint;
5393 ins[in_size] = value;
5398 out_exprs[out_size] = expr;
5399 out_addrs[out_size] = addr;
5401 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5402 /* pure memory ops need no input (but we have to make sure we
5403 * attach to the memory) */
5404 assert(! (asm_flags &
5405 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5406 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5407 needs_memory = true;
5409 /* we need to attach the address to the inputs */
5410 expression_t *expr = argument->expression;
5412 ir_asm_constraint constraint;
5413 constraint.pos = pos;
5414 constraint.constraint = new_id_from_str(constraints);
5415 constraint.mode = NULL;
5416 tmp_in_constraints[in_size] = constraint;
5418 ins[in_size] = expression_to_addr(expr);
5422 errorf(&statement->base.source_position,
5423 "only modifiers but no place set in constraints '%s'",
5428 ir_asm_constraint constraint;
5429 constraint.pos = pos;
5430 constraint.constraint = new_id_from_str(constraints);
5431 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5433 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5435 assert(obstack_object_size(&asm_obst)
5436 == out_size * sizeof(ir_asm_constraint));
5437 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5440 obstack_grow(&asm_obst, tmp_in_constraints,
5441 in_size * sizeof(tmp_in_constraints[0]));
5442 /* find and count input and output arguments */
5443 argument = statement->inputs;
5444 for ( ; argument != NULL; argument = argument->next) {
5445 const char *constraints = argument->constraints.begin;
5446 asm_constraint_flags_t asm_flags
5447 = be_parse_asm_constraints(constraints);
5449 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5450 errorf(&statement->base.source_position,
5451 "some constraints in '%s' are not supported", constraints);
5454 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5455 errorf(&statement->base.source_position,
5456 "some constraints in '%s' are invalid", constraints);
5459 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5460 errorf(&statement->base.source_position,
5461 "write flag specified for input constraints '%s'",
5467 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5468 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5469 /* we can treat this as "normal" input */
5470 input = expression_to_firm(argument->expression);
5471 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5472 /* pure memory ops need no input (but we have to make sure we
5473 * attach to the memory) */
5474 assert(! (asm_flags &
5475 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5476 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5477 needs_memory = true;
5478 input = expression_to_addr(argument->expression);
5480 errorf(&statement->base.source_position,
5481 "only modifiers but no place set in constraints '%s'",
5486 ir_asm_constraint constraint;
5487 constraint.pos = next_pos++;
5488 constraint.constraint = new_id_from_str(constraints);
5489 constraint.mode = get_irn_mode(input);
5491 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5492 ins[in_size++] = input;
5496 ir_asm_constraint constraint;
5497 constraint.pos = next_pos++;
5498 constraint.constraint = new_id_from_str("");
5499 constraint.mode = mode_M;
5501 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5502 ins[in_size++] = get_store();
5505 assert(obstack_object_size(&asm_obst)
5506 == in_size * sizeof(ir_asm_constraint));
5507 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5509 /* create asm node */
5510 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5512 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5514 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5515 out_size, output_constraints,
5516 n_clobbers, clobbers, asm_text);
5518 if (statement->is_volatile) {
5519 set_irn_pinned(node, op_pin_state_pinned);
5521 set_irn_pinned(node, op_pin_state_floats);
5524 /* create output projs & connect them */
5526 ir_node *projm = new_Proj(node, mode_M, out_size);
5531 for (i = 0; i < out_size; ++i) {
5532 const expression_t *out_expr = out_exprs[i];
5534 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5535 ir_node *proj = new_Proj(node, mode, pn);
5536 ir_node *addr = out_addrs[i];
5538 set_value_for_expression_addr(out_expr, proj, addr);
5542 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5544 statement_to_firm(statement->try_statement);
5545 warningf(&statement->base.source_position, "structured exception handling ignored");
5548 static void leave_statement_to_firm(leave_statement_t *statement)
5550 errorf(&statement->base.source_position, "__leave not supported yet");
5554 * Transform a statement.
5556 static void statement_to_firm(statement_t *statement)
5559 assert(!statement->base.transformed);
5560 statement->base.transformed = true;
5563 switch (statement->kind) {
5564 case STATEMENT_INVALID:
5565 panic("invalid statement found");
5566 case STATEMENT_EMPTY:
5569 case STATEMENT_COMPOUND:
5570 compound_statement_to_firm(&statement->compound);
5572 case STATEMENT_RETURN:
5573 return_statement_to_firm(&statement->returns);
5575 case STATEMENT_EXPRESSION:
5576 expression_statement_to_firm(&statement->expression);
5579 if_statement_to_firm(&statement->ifs);
5581 case STATEMENT_WHILE:
5582 while_statement_to_firm(&statement->whiles);
5584 case STATEMENT_DO_WHILE:
5585 do_while_statement_to_firm(&statement->do_while);
5587 case STATEMENT_DECLARATION:
5588 declaration_statement_to_firm(&statement->declaration);
5590 case STATEMENT_BREAK:
5591 create_jump_statement(statement, get_break_label());
5593 case STATEMENT_CONTINUE:
5594 create_jump_statement(statement, continue_label);
5596 case STATEMENT_SWITCH:
5597 switch_statement_to_firm(&statement->switchs);
5599 case STATEMENT_CASE_LABEL:
5600 case_label_to_firm(&statement->case_label);
5603 for_statement_to_firm(&statement->fors);
5605 case STATEMENT_LABEL:
5606 label_to_firm(&statement->label);
5608 case STATEMENT_GOTO:
5609 goto_to_firm(&statement->gotos);
5612 asm_statement_to_firm(&statement->asms);
5614 case STATEMENT_MS_TRY:
5615 ms_try_statement_to_firm(&statement->ms_try);
5617 case STATEMENT_LEAVE:
5618 leave_statement_to_firm(&statement->leave);
5621 panic("statement not implemented");
5624 static int count_local_variables(const entity_t *entity,
5625 const entity_t *const last)
5628 entity_t const *const end = last != NULL ? last->base.next : NULL;
5629 for (; entity != end; entity = entity->base.next) {
5633 if (entity->kind == ENTITY_VARIABLE) {
5634 type = skip_typeref(entity->declaration.type);
5635 address_taken = entity->variable.address_taken;
5636 } else if (entity->kind == ENTITY_PARAMETER) {
5637 type = skip_typeref(entity->declaration.type);
5638 address_taken = entity->parameter.address_taken;
5643 if (!address_taken && is_type_scalar(type))
5649 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5651 int *const count = env;
5653 switch (stmt->kind) {
5654 case STATEMENT_DECLARATION: {
5655 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5656 *count += count_local_variables(decl_stmt->declarations_begin,
5657 decl_stmt->declarations_end);
5662 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5671 * Return the number of local (alias free) variables used by a function.
5673 static int get_function_n_local_vars(entity_t *entity)
5675 const function_t *function = &entity->function;
5678 /* count parameters */
5679 count += count_local_variables(function->parameters.entities, NULL);
5681 /* count local variables declared in body */
5682 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5687 * Build Firm code for the parameters of a function.
5689 static void initialize_function_parameters(entity_t *entity)
5691 assert(entity->kind == ENTITY_FUNCTION);
5692 ir_graph *irg = current_ir_graph;
5693 ir_node *args = get_irg_args(irg);
5694 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5695 int first_param_nr = 0;
5697 if (entity->function.need_closure) {
5698 /* add an extra parameter for the static link */
5699 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5704 entity_t *parameter = entity->function.parameters.entities;
5705 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5706 if (parameter->kind != ENTITY_PARAMETER)
5709 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5710 type_t *type = skip_typeref(parameter->declaration.type);
5712 bool needs_entity = parameter->parameter.address_taken;
5713 assert(!is_type_array(type));
5714 if (is_type_compound(type)) {
5715 needs_entity = true;
5719 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5720 ident *id = new_id_from_str(parameter->base.symbol->string);
5721 set_entity_ident(entity, id);
5723 parameter->declaration.kind
5724 = DECLARATION_KIND_PARAMETER_ENTITY;
5725 parameter->parameter.v.entity = entity;
5729 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5730 ir_mode *param_mode = get_type_mode(param_irtype);
5732 long pn = n + first_param_nr;
5733 ir_node *value = new_r_Proj(args, param_mode, pn);
5735 ir_mode *mode = get_ir_mode_storage(type);
5736 value = create_conv(NULL, value, mode);
5737 value = do_strict_conv(NULL, value);
5739 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5740 parameter->parameter.v.value_number = next_value_number_function;
5741 set_irg_loc_description(current_ir_graph, next_value_number_function,
5743 ++next_value_number_function;
5745 set_value(parameter->parameter.v.value_number, value);
5750 * Handle additional decl modifiers for IR-graphs
5752 * @param irg the IR-graph
5753 * @param dec_modifiers additional modifiers
5755 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5756 decl_modifiers_t decl_modifiers)
5758 if (decl_modifiers & DM_RETURNS_TWICE) {
5759 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5760 add_irg_additional_properties(irg, mtp_property_returns_twice);
5762 if (decl_modifiers & DM_NORETURN) {
5763 /* TRUE if the declaration includes the Microsoft
5764 __declspec(noreturn) specifier. */
5765 add_irg_additional_properties(irg, mtp_property_noreturn);
5767 if (decl_modifiers & DM_NOTHROW) {
5768 /* TRUE if the declaration includes the Microsoft
5769 __declspec(nothrow) specifier. */
5770 add_irg_additional_properties(irg, mtp_property_nothrow);
5772 if (decl_modifiers & DM_NAKED) {
5773 /* TRUE if the declaration includes the Microsoft
5774 __declspec(naked) specifier. */
5775 add_irg_additional_properties(irg, mtp_property_naked);
5777 if (decl_modifiers & DM_FORCEINLINE) {
5778 /* TRUE if the declaration includes the
5779 Microsoft __forceinline specifier. */
5780 set_irg_inline_property(irg, irg_inline_forced);
5782 if (decl_modifiers & DM_NOINLINE) {
5783 /* TRUE if the declaration includes the Microsoft
5784 __declspec(noinline) specifier. */
5785 set_irg_inline_property(irg, irg_inline_forbidden);
5789 static void add_function_pointer(ir_type *segment, ir_entity *method,
5790 const char *unique_template)
5792 ir_type *method_type = get_entity_type(method);
5793 ir_type *ptr_type = new_type_pointer(method_type);
5795 /* these entities don't really have a name but firm only allows
5797 * Note that we mustn't give these entities a name since for example
5798 * Mach-O doesn't allow them. */
5799 ident *ide = id_unique(unique_template);
5800 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5801 ir_graph *irg = get_const_code_irg();
5802 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5805 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5806 set_entity_compiler_generated(ptr, 1);
5807 set_entity_visibility(ptr, ir_visibility_private);
5808 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5809 set_atomic_ent_value(ptr, val);
5813 * Generate possible IJmp branches to a given label block.
5815 static void gen_ijmp_branches(ir_node *block)
5818 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5819 add_immBlock_pred(block, ijmp);
5824 * Create code for a function and all inner functions.
5826 * @param entity the function entity
5828 static void create_function(entity_t *entity)
5830 assert(entity->kind == ENTITY_FUNCTION);
5831 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5833 if (entity->function.statement == NULL)
5836 if (is_main(entity) && enable_main_collect2_hack) {
5837 prepare_main_collect2(entity);
5840 inner_functions = NULL;
5841 current_trampolines = NULL;
5843 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5844 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5845 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5847 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5848 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5849 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5852 current_function_entity = entity;
5853 current_function_name = NULL;
5854 current_funcsig = NULL;
5856 assert(all_labels == NULL);
5857 all_labels = NEW_ARR_F(label_t *, 0);
5860 int n_local_vars = get_function_n_local_vars(entity);
5861 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5862 current_ir_graph = irg;
5864 ir_graph *old_current_function = current_function;
5865 current_function = irg;
5867 set_irg_fp_model(irg, firm_opt.fp_model);
5868 tarval_enable_fp_ops(1);
5869 set_irn_dbg_info(get_irg_start_block(irg),
5870 get_entity_dbg_info(function_entity));
5872 ir_node *first_block = get_cur_block();
5874 /* set inline flags */
5875 if (entity->function.is_inline)
5876 set_irg_inline_property(irg, irg_inline_recomended);
5877 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5879 next_value_number_function = 0;
5880 initialize_function_parameters(entity);
5881 current_static_link = entity->function.static_link;
5883 statement_to_firm(entity->function.statement);
5885 ir_node *end_block = get_irg_end_block(irg);
5887 /* do we have a return statement yet? */
5888 if (get_cur_block() != NULL) {
5889 type_t *type = skip_typeref(entity->declaration.type);
5890 assert(is_type_function(type));
5891 const function_type_t *func_type = &type->function;
5892 const type_t *return_type
5893 = skip_typeref(func_type->return_type);
5896 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5897 ret = new_Return(get_store(), 0, NULL);
5900 if (is_type_scalar(return_type)) {
5901 mode = get_ir_mode_storage(func_type->return_type);
5907 /* ยง5.1.2.2.3 main implicitly returns 0 */
5908 if (is_main(entity)) {
5909 in[0] = new_Const(get_mode_null(mode));
5911 in[0] = new_Unknown(mode);
5913 ret = new_Return(get_store(), 1, in);
5915 add_immBlock_pred(end_block, ret);
5918 bool has_computed_gotos = false;
5919 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5920 label_t *label = all_labels[i];
5921 if (label->address_taken) {
5922 gen_ijmp_branches(label->block);
5923 has_computed_gotos = true;
5925 mature_immBlock(label->block);
5927 if (has_computed_gotos) {
5928 /* if we have computed goto's in the function, we cannot inline it */
5929 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5930 warningf(&entity->base.source_position,
5931 "function '%Y' can never be inlined because it contains a computed goto",
5932 entity->base.symbol);
5934 set_irg_inline_property(irg, irg_inline_forbidden);
5937 DEL_ARR_F(all_labels);
5940 mature_immBlock(first_block);
5941 mature_immBlock(end_block);
5943 irg_finalize_cons(irg);
5945 /* finalize the frame type */
5946 ir_type *frame_type = get_irg_frame_type(irg);
5947 int n = get_compound_n_members(frame_type);
5950 for (int i = 0; i < n; ++i) {
5951 ir_entity *entity = get_compound_member(frame_type, i);
5952 ir_type *entity_type = get_entity_type(entity);
5954 int align = get_type_alignment_bytes(entity_type);
5955 if (align > align_all)
5959 misalign = offset % align;
5961 offset += align - misalign;
5965 set_entity_offset(entity, offset);
5966 offset += get_type_size_bytes(entity_type);
5968 set_type_size_bytes(frame_type, offset);
5969 set_type_alignment_bytes(frame_type, align_all);
5971 irg_verify(irg, VERIFY_ENFORCE_SSA);
5972 current_function = old_current_function;
5974 if (current_trampolines != NULL) {
5975 DEL_ARR_F(current_trampolines);
5976 current_trampolines = NULL;
5979 /* create inner functions if any */
5980 entity_t **inner = inner_functions;
5981 if (inner != NULL) {
5982 ir_type *rem_outer_frame = current_outer_frame;
5983 current_outer_frame = get_irg_frame_type(current_ir_graph);
5984 ir_type *rem_outer_value_type = current_outer_value_type;
5985 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5986 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5987 create_function(inner[i]);
5991 current_outer_value_type = rem_outer_value_type;
5992 current_outer_frame = rem_outer_frame;
5996 static void scope_to_firm(scope_t *scope)
5998 /* first pass: create declarations */
5999 entity_t *entity = scope->entities;
6000 for ( ; entity != NULL; entity = entity->base.next) {
6001 if (entity->base.symbol == NULL)
6004 if (entity->kind == ENTITY_FUNCTION) {
6005 if (entity->function.btk != bk_none) {
6006 /* builtins have no representation */
6009 (void)get_function_entity(entity, NULL);
6010 } else if (entity->kind == ENTITY_VARIABLE) {
6011 create_global_variable(entity);
6012 } else if (entity->kind == ENTITY_NAMESPACE) {
6013 scope_to_firm(&entity->namespacee.members);
6017 /* second pass: create code/initializers */
6018 entity = scope->entities;
6019 for ( ; entity != NULL; entity = entity->base.next) {
6020 if (entity->base.symbol == NULL)
6023 if (entity->kind == ENTITY_FUNCTION) {
6024 if (entity->function.btk != bk_none) {
6025 /* builtins have no representation */
6028 create_function(entity);
6029 } else if (entity->kind == ENTITY_VARIABLE) {
6030 assert(entity->declaration.kind
6031 == DECLARATION_KIND_GLOBAL_VARIABLE);
6032 current_ir_graph = get_const_code_irg();
6033 create_variable_initializer(entity);
6038 void init_ast2firm(void)
6040 obstack_init(&asm_obst);
6041 init_atomic_modes();
6043 ir_set_debug_retrieve(dbg_retrieve);
6044 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
6046 /* create idents for all known runtime functions */
6047 for (size_t i = 0; i < lengthof(rts_data); ++i) {
6048 rts_idents[i] = new_id_from_str(rts_data[i].name);
6051 entitymap_init(&entitymap);
6054 static void init_ir_types(void)
6056 static int ir_types_initialized = 0;
6057 if (ir_types_initialized)
6059 ir_types_initialized = 1;
6061 ir_type_int = get_ir_type(type_int);
6062 ir_type_char = get_ir_type(type_char);
6063 ir_type_const_char = get_ir_type(type_const_char);
6064 ir_type_wchar_t = get_ir_type(type_wchar_t);
6065 ir_type_void = get_ir_type(type_void);
6067 be_params = be_get_backend_param();
6068 mode_float_arithmetic = be_params->mode_float_arithmetic;
6070 stack_param_align = be_params->stack_param_align;
6073 void exit_ast2firm(void)
6075 entitymap_destroy(&entitymap);
6076 obstack_free(&asm_obst, NULL);
6079 static void global_asm_to_firm(statement_t *s)
6081 for (; s != NULL; s = s->base.next) {
6082 assert(s->kind == STATEMENT_ASM);
6084 char const *const text = s->asms.asm_text.begin;
6085 size_t size = s->asms.asm_text.size;
6087 /* skip the last \0 */
6088 if (text[size - 1] == '\0')
6091 ident *const id = new_id_from_chars(text, size);
6096 void translation_unit_to_firm(translation_unit_t *unit)
6098 /* initialize firm arithmetic */
6099 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6100 ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6102 /* just to be sure */
6103 continue_label = NULL;
6105 current_switch_cond = NULL;
6106 current_translation_unit = unit;
6110 scope_to_firm(&unit->scope);
6111 global_asm_to_firm(unit->global_asm);
6113 current_ir_graph = NULL;
6114 current_translation_unit = NULL;