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);
1100 static ir_node *create_Const_from_bool(ir_mode *const mode, bool const v)
1102 return new_Const((v ? get_mode_one : get_mode_null)(mode));
1106 * Creates a SymConst node representing a wide string literal.
1108 * @param literal the wide string literal
1110 static ir_node *wide_string_literal_to_firm(
1111 const string_literal_expression_t *literal)
1113 ir_type *const global_type = get_glob_type();
1114 ir_type *const elem_type = ir_type_wchar_t;
1115 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1116 ir_type *const type = new_type_array(1, elem_type);
1118 ident *const id = id_unique("str.%u");
1119 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1120 set_entity_ld_ident(entity, id);
1121 set_entity_visibility(entity, ir_visibility_private);
1122 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1124 ir_mode *const mode = get_type_mode(elem_type);
1125 const size_t slen = wstrlen(&literal->value);
1127 set_array_lower_bound_int(type, 0, 0);
1128 set_array_upper_bound_int(type, 0, slen);
1129 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1130 set_type_state(type, layout_fixed);
1132 ir_initializer_t *initializer = create_initializer_compound(slen);
1133 const char *p = literal->value.begin;
1134 for (size_t i = 0; i < slen; ++i) {
1135 assert(p < literal->value.begin + literal->value.size);
1136 utf32 v = read_utf8_char(&p);
1137 ir_tarval *tv = new_tarval_from_long(v, mode);
1138 ir_initializer_t *val = create_initializer_tarval(tv);
1139 set_initializer_compound_value(initializer, i, val);
1141 set_entity_initializer(entity, initializer);
1143 return create_symconst(dbgi, mode_P_data, entity);
1147 * Creates a SymConst node representing a string constant.
1149 * @param src_pos the source position of the string constant
1150 * @param id_prefix a prefix for the name of the generated string constant
1151 * @param value the value of the string constant
1153 static ir_node *string_to_firm(const source_position_t *const src_pos,
1154 const char *const id_prefix,
1155 const string_t *const value)
1157 ir_type *const global_type = get_glob_type();
1158 dbg_info *const dbgi = get_dbg_info(src_pos);
1159 ir_type *const type = new_type_array(1, ir_type_const_char);
1161 ident *const id = id_unique(id_prefix);
1162 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1163 set_entity_ld_ident(entity, id);
1164 set_entity_visibility(entity, ir_visibility_private);
1165 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1167 ir_type *const elem_type = ir_type_const_char;
1168 ir_mode *const mode = get_type_mode(elem_type);
1170 const char* const string = value->begin;
1171 const size_t slen = value->size;
1173 set_array_lower_bound_int(type, 0, 0);
1174 set_array_upper_bound_int(type, 0, slen);
1175 set_type_size_bytes(type, slen);
1176 set_type_state(type, layout_fixed);
1178 ir_initializer_t *initializer = create_initializer_compound(slen);
1179 for (size_t i = 0; i < slen; ++i) {
1180 ir_tarval *tv = new_tarval_from_long(string[i], mode);
1181 ir_initializer_t *val = create_initializer_tarval(tv);
1182 set_initializer_compound_value(initializer, i, val);
1184 set_entity_initializer(entity, initializer);
1186 return create_symconst(dbgi, mode_P_data, entity);
1189 static bool try_create_integer(literal_expression_t *literal,
1190 type_t *type, unsigned char base)
1192 const char *string = literal->value.begin;
1193 size_t size = literal->value.size;
1195 assert(type->kind == TYPE_ATOMIC);
1196 atomic_type_kind_t akind = type->atomic.akind;
1198 ir_mode *mode = atomic_modes[akind];
1199 ir_tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1200 if (tv == tarval_bad)
1203 literal->base.type = type;
1204 literal->target_value = tv;
1208 static void create_integer_tarval(literal_expression_t *literal)
1212 symbol_t *suffix = literal->suffix;
1214 if (suffix != NULL) {
1215 for (const char *c = suffix->string; *c != '\0'; ++c) {
1216 if (*c == 'u' || *c == 'U') { ++us; }
1217 if (*c == 'l' || *c == 'L') { ++ls; }
1222 switch (literal->base.kind) {
1223 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1224 case EXPR_LITERAL_INTEGER: base = 10; break;
1225 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1226 default: panic("invalid literal kind");
1229 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1231 /* now try if the constant is small enough for some types */
1232 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1234 if (us == 0 && try_create_integer(literal, type_int, base))
1236 if ((us == 1 || base != 10)
1237 && try_create_integer(literal, type_unsigned_int, base))
1241 if (us == 0 && try_create_integer(literal, type_long, base))
1243 if ((us == 1 || base != 10)
1244 && try_create_integer(literal, type_unsigned_long, base))
1247 /* last try? then we should not report tarval_bad */
1248 if (us != 1 && base == 10)
1249 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1250 if (us == 0 && try_create_integer(literal, type_long_long, base))
1254 assert(us == 1 || base != 10);
1255 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1256 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1258 panic("internal error when parsing number literal");
1261 tarval_set_integer_overflow_mode(old_mode);
1264 void determine_literal_type(literal_expression_t *literal)
1266 switch (literal->base.kind) {
1267 case EXPR_LITERAL_INTEGER:
1268 case EXPR_LITERAL_INTEGER_OCTAL:
1269 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1270 create_integer_tarval(literal);
1278 * Creates a Const node representing a constant.
1280 static ir_node *literal_to_firm(const literal_expression_t *literal)
1282 type_t *type = skip_typeref(literal->base.type);
1283 ir_mode *mode = get_ir_mode_storage(type);
1284 const char *string = literal->value.begin;
1285 size_t size = literal->value.size;
1288 switch (literal->base.kind) {
1289 case EXPR_LITERAL_WIDE_CHARACTER: {
1290 utf32 v = read_utf8_char(&string);
1292 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1294 tv = new_tarval_from_str(buf, len, mode);
1297 case EXPR_LITERAL_CHARACTER: {
1299 if (size == 1 && char_is_signed) {
1300 v = (signed char)string[0];
1303 for (size_t i = 0; i < size; ++i) {
1304 v = (v << 8) | ((unsigned char)string[i]);
1308 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1310 tv = new_tarval_from_str(buf, len, mode);
1313 case EXPR_LITERAL_INTEGER:
1314 case EXPR_LITERAL_INTEGER_OCTAL:
1315 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1316 assert(literal->target_value != NULL);
1317 tv = literal->target_value;
1319 case EXPR_LITERAL_FLOATINGPOINT:
1320 tv = new_tarval_from_str(string, size, mode);
1322 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1323 char buffer[size + 2];
1324 memcpy(buffer, "0x", 2);
1325 memcpy(buffer+2, string, size);
1326 tv = new_tarval_from_str(buffer, size+2, mode);
1329 case EXPR_LITERAL_BOOLEAN:
1330 if (string[0] == 't') {
1331 tv = get_mode_one(mode);
1333 assert(string[0] == 'f');
1334 tv = get_mode_null(mode);
1337 case EXPR_LITERAL_MS_NOOP:
1338 tv = get_mode_null(mode);
1343 panic("Invalid literal kind found");
1346 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1347 ir_node *res = new_d_Const(dbgi, tv);
1348 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1349 return create_conv(dbgi, res, mode_arith);
1353 * Allocate an area of size bytes aligned at alignment
1356 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1358 static unsigned area_cnt = 0;
1361 ir_type *tp = new_type_array(1, ir_type_char);
1362 set_array_bounds_int(tp, 0, 0, size);
1363 set_type_alignment_bytes(tp, alignment);
1365 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1366 ident *name = new_id_from_str(buf);
1367 ir_entity *area = new_entity(frame_type, name, tp);
1369 /* mark this entity as compiler generated */
1370 set_entity_compiler_generated(area, 1);
1375 * Return a node representing a trampoline region
1376 * for a given function entity.
1378 * @param dbgi debug info
1379 * @param entity the function entity
1381 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1383 ir_entity *region = NULL;
1386 if (current_trampolines != NULL) {
1387 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1388 if (current_trampolines[i].function == entity) {
1389 region = current_trampolines[i].region;
1394 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1396 ir_graph *irg = current_ir_graph;
1397 if (region == NULL) {
1398 /* create a new region */
1399 ir_type *frame_tp = get_irg_frame_type(irg);
1400 trampoline_region reg;
1401 reg.function = entity;
1403 reg.region = alloc_trampoline(frame_tp,
1404 be_params->trampoline_size,
1405 be_params->trampoline_align);
1406 ARR_APP1(trampoline_region, current_trampolines, reg);
1407 region = reg.region;
1409 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1414 * Creates a trampoline for a function represented by an entity.
1416 * @param dbgi debug info
1417 * @param mode the (reference) mode for the function address
1418 * @param entity the function entity
1420 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1423 assert(entity != NULL);
1425 in[0] = get_trampoline_region(dbgi, entity);
1426 in[1] = create_symconst(dbgi, mode, entity);
1427 in[2] = get_irg_frame(current_ir_graph);
1429 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1430 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1431 return new_Proj(irn, mode, pn_Builtin_1_result);
1435 * Dereference an address.
1437 * @param dbgi debug info
1438 * @param type the type of the dereferenced result (the points_to type)
1439 * @param addr the address to dereference
1441 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1442 ir_node *const addr)
1444 ir_type *irtype = get_ir_type(type);
1445 if (is_compound_type(irtype)
1446 || is_Method_type(irtype)
1447 || is_Array_type(irtype)) {
1451 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1452 ? cons_volatile : cons_none;
1453 ir_mode *const mode = get_type_mode(irtype);
1454 ir_node *const memory = get_store();
1455 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1456 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1457 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1459 set_store(load_mem);
1461 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1462 return create_conv(dbgi, load_res, mode_arithmetic);
1466 * Creates a strict Conv (to the node's mode) if necessary.
1468 * @param dbgi debug info
1469 * @param node the node to strict conv
1471 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1473 ir_mode *mode = get_irn_mode(node);
1475 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1477 if (!mode_is_float(mode))
1480 /* check if there is already a Conv */
1481 if (is_Conv(node)) {
1482 /* convert it into a strict Conv */
1483 set_Conv_strict(node, 1);
1487 /* otherwise create a new one */
1488 return new_d_strictConv(dbgi, node, mode);
1492 * Returns the address of a global variable.
1494 * @param dbgi debug info
1495 * @param variable the variable
1497 static ir_node *get_global_var_address(dbg_info *const dbgi,
1498 const variable_t *const variable)
1500 ir_entity *const irentity = variable->v.entity;
1501 if (variable->thread_local) {
1502 ir_node *const no_mem = new_NoMem();
1503 ir_node *const tls = get_irg_tls(current_ir_graph);
1504 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1506 return create_symconst(dbgi, mode_P_data, irentity);
1511 * Returns the correct base address depending on whether it is a parameter or a
1512 * normal local variable.
1514 static ir_node *get_local_frame(ir_entity *const ent)
1516 ir_graph *const irg = current_ir_graph;
1517 const ir_type *const owner = get_entity_owner(ent);
1518 if (owner == current_outer_frame || owner == current_outer_value_type) {
1519 assert(current_static_link != NULL);
1520 return current_static_link;
1522 return get_irg_frame(irg);
1527 * Keep all memory edges of the given block.
1529 static void keep_all_memory(ir_node *block)
1531 ir_node *old = get_cur_block();
1533 set_cur_block(block);
1534 keep_alive(get_store());
1535 /* TODO: keep all memory edges from restricted pointers */
1539 static ir_node *reference_expression_enum_value_to_firm(
1540 const reference_expression_t *ref)
1542 entity_t *entity = ref->entity;
1543 type_t *type = skip_typeref(entity->enum_value.enum_type);
1544 /* make sure the type is constructed */
1545 (void) get_ir_type(type);
1547 return new_Const(entity->enum_value.tv);
1550 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1552 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1553 entity_t *entity = ref->entity;
1554 assert(is_declaration(entity));
1555 type_t *type = skip_typeref(entity->declaration.type);
1557 /* make sure the type is constructed */
1558 (void) get_ir_type(type);
1560 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1561 ir_entity *irentity = get_function_entity(entity, NULL);
1562 /* for gcc compatibility we have to produce (dummy) addresses for some
1563 * builtins which don't have entities */
1564 if (irentity == NULL) {
1565 if (warning.other) {
1566 warningf(&ref->base.source_position,
1567 "taking address of builtin '%Y'",
1568 ref->entity->base.symbol);
1571 /* simply create a NULL pointer */
1572 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1573 ir_node *res = new_Const(get_mode_null(mode));
1579 switch ((declaration_kind_t) entity->declaration.kind) {
1580 case DECLARATION_KIND_UNKNOWN:
1583 case DECLARATION_KIND_LOCAL_VARIABLE: {
1584 ir_mode *const mode = get_ir_mode_storage(type);
1585 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1586 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1588 case DECLARATION_KIND_PARAMETER: {
1589 ir_mode *const mode = get_ir_mode_storage(type);
1590 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1591 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1593 case DECLARATION_KIND_FUNCTION: {
1594 ir_mode *const mode = get_ir_mode_storage(type);
1595 return create_symconst(dbgi, mode, entity->function.irentity);
1597 case DECLARATION_KIND_INNER_FUNCTION: {
1598 ir_mode *const mode = get_ir_mode_storage(type);
1599 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1600 /* inner function not using the closure */
1601 return create_symconst(dbgi, mode, entity->function.irentity);
1603 /* need trampoline here */
1604 return create_trampoline(dbgi, mode, entity->function.irentity);
1607 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1608 const variable_t *variable = &entity->variable;
1609 ir_node *const addr = get_global_var_address(dbgi, variable);
1610 return deref_address(dbgi, variable->base.type, addr);
1613 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1614 ir_entity *irentity = entity->variable.v.entity;
1615 ir_node *frame = get_local_frame(irentity);
1616 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1617 return deref_address(dbgi, entity->declaration.type, sel);
1619 case DECLARATION_KIND_PARAMETER_ENTITY: {
1620 ir_entity *irentity = entity->parameter.v.entity;
1621 ir_node *frame = get_local_frame(irentity);
1622 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1623 return deref_address(dbgi, entity->declaration.type, sel);
1626 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1627 return entity->variable.v.vla_base;
1629 case DECLARATION_KIND_COMPOUND_MEMBER:
1630 panic("not implemented reference type");
1633 panic("reference to declaration with unknown type found");
1636 static ir_node *reference_addr(const reference_expression_t *ref)
1638 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1639 entity_t *entity = ref->entity;
1640 assert(is_declaration(entity));
1642 switch((declaration_kind_t) entity->declaration.kind) {
1643 case DECLARATION_KIND_UNKNOWN:
1645 case DECLARATION_KIND_PARAMETER:
1646 case DECLARATION_KIND_LOCAL_VARIABLE:
1647 /* you can store to a local variable (so we don't panic but return NULL
1648 * as an indicator for no real address) */
1650 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1651 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1654 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1655 ir_entity *irentity = entity->variable.v.entity;
1656 ir_node *frame = get_local_frame(irentity);
1657 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1661 case DECLARATION_KIND_PARAMETER_ENTITY: {
1662 ir_entity *irentity = entity->parameter.v.entity;
1663 ir_node *frame = get_local_frame(irentity);
1664 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1669 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1670 return entity->variable.v.vla_base;
1672 case DECLARATION_KIND_FUNCTION: {
1673 type_t *const type = skip_typeref(entity->declaration.type);
1674 ir_mode *const mode = get_ir_mode_storage(type);
1675 return create_symconst(dbgi, mode, entity->function.irentity);
1678 case DECLARATION_KIND_INNER_FUNCTION: {
1679 type_t *const type = skip_typeref(entity->declaration.type);
1680 ir_mode *const mode = get_ir_mode_storage(type);
1681 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1682 /* inner function not using the closure */
1683 return create_symconst(dbgi, mode, entity->function.irentity);
1685 /* need trampoline here */
1686 return create_trampoline(dbgi, mode, entity->function.irentity);
1690 case DECLARATION_KIND_COMPOUND_MEMBER:
1691 panic("not implemented reference type");
1694 panic("reference to declaration with unknown type found");
1698 * Generate an unary builtin.
1700 * @param kind the builtin kind to generate
1701 * @param op the operand
1702 * @param function_type the function type for the GNU builtin routine
1703 * @param db debug info
1705 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1708 in[0] = expression_to_firm(op);
1710 ir_type *tp = get_ir_type(function_type);
1711 ir_type *res = get_method_res_type(tp, 0);
1712 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1713 set_irn_pinned(irn, op_pin_state_floats);
1714 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1718 * Generate a pinned unary builtin.
1720 * @param kind the builtin kind to generate
1721 * @param op the operand
1722 * @param function_type the function type for the GNU builtin routine
1723 * @param db debug info
1725 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1726 type_t *function_type, dbg_info *db)
1729 in[0] = expression_to_firm(op);
1731 ir_type *tp = get_ir_type(function_type);
1732 ir_type *res = get_method_res_type(tp, 0);
1733 ir_node *mem = get_store();
1734 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1735 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1736 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1740 * Generate an binary-void-return builtin.
1742 * @param kind the builtin kind to generate
1743 * @param op1 the first operand
1744 * @param op2 the second operand
1745 * @param function_type the function type for the GNU builtin routine
1746 * @param db debug info
1748 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1749 expression_t *op2, type_t *function_type,
1753 in[0] = expression_to_firm(op1);
1754 in[1] = expression_to_firm(op2);
1756 ir_type *tp = get_ir_type(function_type);
1757 ir_node *mem = get_store();
1758 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1759 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1764 * Transform calls to builtin functions.
1766 static ir_node *process_builtin_call(const call_expression_t *call)
1768 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1770 assert(call->function->kind == EXPR_REFERENCE);
1771 reference_expression_t *builtin = &call->function->reference;
1773 type_t *type = skip_typeref(builtin->base.type);
1774 assert(is_type_pointer(type));
1776 type_t *function_type = skip_typeref(type->pointer.points_to);
1778 switch (builtin->entity->function.btk) {
1779 case bk_gnu_builtin_alloca: {
1780 if (call->arguments == NULL || call->arguments->next != NULL) {
1781 panic("invalid number of parameters on __builtin_alloca");
1783 expression_t *argument = call->arguments->expression;
1784 ir_node *size = expression_to_firm(argument);
1786 ir_node *store = get_store();
1787 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1789 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1791 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1796 case bk_gnu_builtin_huge_val:
1797 case bk_gnu_builtin_huge_valf:
1798 case bk_gnu_builtin_huge_vall:
1799 case bk_gnu_builtin_inf:
1800 case bk_gnu_builtin_inff:
1801 case bk_gnu_builtin_infl: {
1802 type_t *type = function_type->function.return_type;
1803 ir_mode *mode = get_ir_mode_arithmetic(type);
1804 ir_tarval *tv = get_mode_infinite(mode);
1805 ir_node *res = new_d_Const(dbgi, tv);
1808 case bk_gnu_builtin_nan:
1809 case bk_gnu_builtin_nanf:
1810 case bk_gnu_builtin_nanl: {
1811 /* Ignore string for now... */
1812 assert(is_type_function(function_type));
1813 type_t *type = function_type->function.return_type;
1814 ir_mode *mode = get_ir_mode_arithmetic(type);
1815 ir_tarval *tv = get_mode_NAN(mode);
1816 ir_node *res = new_d_Const(dbgi, tv);
1819 case bk_gnu_builtin_expect: {
1820 expression_t *argument = call->arguments->expression;
1821 return _expression_to_firm(argument);
1823 case bk_gnu_builtin_va_end:
1824 /* evaluate the argument of va_end for its side effects */
1825 _expression_to_firm(call->arguments->expression);
1827 case bk_gnu_builtin_frame_address: {
1828 expression_t *const expression = call->arguments->expression;
1829 bool val = fold_constant_to_bool(expression);
1832 return get_irg_frame(current_ir_graph);
1834 /* get the argument */
1837 in[0] = expression_to_firm(expression);
1838 in[1] = get_irg_frame(current_ir_graph);
1839 ir_type *tp = get_ir_type(function_type);
1840 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1841 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1844 case bk_gnu_builtin_return_address: {
1845 expression_t *const expression = call->arguments->expression;
1848 in[0] = expression_to_firm(expression);
1849 in[1] = get_irg_frame(current_ir_graph);
1850 ir_type *tp = get_ir_type(function_type);
1851 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1852 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1854 case bk_gnu_builtin_ffs:
1855 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1856 case bk_gnu_builtin_clz:
1857 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1858 case bk_gnu_builtin_ctz:
1859 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1860 case bk_gnu_builtin_popcount:
1861 case bk_ms__popcount:
1862 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1863 case bk_gnu_builtin_parity:
1864 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1865 case bk_gnu_builtin_prefetch: {
1866 call_argument_t *const args = call->arguments;
1867 expression_t *const addr = args->expression;
1870 in[0] = _expression_to_firm(addr);
1871 if (args->next != NULL) {
1872 expression_t *const rw = args->next->expression;
1874 in[1] = _expression_to_firm(rw);
1876 if (args->next->next != NULL) {
1877 expression_t *const locality = args->next->next->expression;
1879 in[2] = expression_to_firm(locality);
1881 in[2] = new_Const_long(mode_int, 3);
1884 in[1] = new_Const_long(mode_int, 0);
1885 in[2] = new_Const_long(mode_int, 3);
1887 ir_type *tp = get_ir_type(function_type);
1888 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1889 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1892 case bk_gnu_builtin_object_size: {
1893 /* determine value of "type" */
1894 expression_t *type_expression = call->arguments->next->expression;
1895 long type_val = fold_constant_to_int(type_expression);
1896 type_t *type = function_type->function.return_type;
1897 ir_mode *mode = get_ir_mode_arithmetic(type);
1900 /* just produce a "I don't know" result */
1902 result = new_tarval_from_long(0, mode);
1904 result = new_tarval_from_long(-1, mode);
1906 return new_d_Const(dbgi, result);
1908 case bk_gnu_builtin_trap:
1911 ir_type *tp = get_ir_type(function_type);
1912 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1913 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1916 case bk_ms__debugbreak: {
1917 ir_type *tp = get_ir_type(function_type);
1918 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1919 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1922 case bk_ms_ReturnAddress: {
1925 in[0] = new_Const(get_mode_null(mode_int));
1926 in[1] = get_irg_frame(current_ir_graph);
1927 ir_type *tp = get_ir_type(function_type);
1928 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1929 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1932 case bk_ms_rotl64: {
1933 ir_node *val = expression_to_firm(call->arguments->expression);
1934 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1935 ir_mode *mode = get_irn_mode(val);
1936 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1939 case bk_ms_rotr64: {
1940 ir_node *val = expression_to_firm(call->arguments->expression);
1941 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1942 ir_mode *mode = get_irn_mode(val);
1943 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1944 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1945 return new_d_Rotl(dbgi, val, sub, mode);
1947 case bk_ms_byteswap_ushort:
1948 case bk_ms_byteswap_ulong:
1949 case bk_ms_byteswap_uint64:
1950 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1953 case bk_ms__indword:
1954 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1955 case bk_ms__outbyte:
1956 case bk_ms__outword:
1957 case bk_ms__outdword:
1958 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1959 call->arguments->next->expression, function_type, dbgi);
1961 panic("unsupported builtin found");
1966 * Transform a call expression.
1967 * Handles some special cases, like alloca() calls, which must be resolved
1968 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1969 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1972 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1974 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1975 assert(get_cur_block() != NULL);
1977 expression_t *function = call->function;
1978 if (function->kind == EXPR_REFERENCE) {
1979 const reference_expression_t *ref = &function->reference;
1980 entity_t *entity = ref->entity;
1982 if (entity->kind == ENTITY_FUNCTION) {
1983 ir_entity *irentity = entity->function.irentity;
1984 if (irentity == NULL)
1985 irentity = get_function_entity(entity, NULL);
1987 if (irentity == NULL && entity->function.btk != bk_none) {
1988 return process_builtin_call(call);
1992 if (irentity == rts_entities[rts_alloca]) {
1993 /* handle alloca() call */
1994 expression_t *argument = call->arguments->expression;
1995 ir_node *size = expression_to_firm(argument);
1996 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1998 size = create_conv(dbgi, size, mode);
2000 ir_node *store = get_store();
2001 ir_node *alloca = new_d_Alloc(dbgi, store, size,
2002 firm_unknown_type, stack_alloc);
2003 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
2005 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
2012 ir_node *callee = expression_to_firm(function);
2014 type_t *type = skip_typeref(function->base.type);
2015 assert(is_type_pointer(type));
2016 pointer_type_t *pointer_type = &type->pointer;
2017 type_t *points_to = skip_typeref(pointer_type->points_to);
2018 assert(is_type_function(points_to));
2019 function_type_t *function_type = &points_to->function;
2021 int n_parameters = 0;
2022 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
2023 ir_type *new_method_type = NULL;
2024 if (function_type->variadic || function_type->unspecified_parameters) {
2025 const call_argument_t *argument = call->arguments;
2026 for ( ; argument != NULL; argument = argument->next) {
2030 /* we need to construct a new method type matching the call
2032 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2033 int n_res = get_method_n_ress(ir_method_type);
2034 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2035 set_method_calling_convention(new_method_type,
2036 get_method_calling_convention(ir_method_type));
2037 set_method_additional_properties(new_method_type,
2038 get_method_additional_properties(ir_method_type));
2039 set_method_variadicity(new_method_type,
2040 get_method_variadicity(ir_method_type));
2042 for (int i = 0; i < n_res; ++i) {
2043 set_method_res_type(new_method_type, i,
2044 get_method_res_type(ir_method_type, i));
2046 argument = call->arguments;
2047 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2048 expression_t *expression = argument->expression;
2049 ir_type *irtype = get_ir_type(expression->base.type);
2050 set_method_param_type(new_method_type, i, irtype);
2052 ir_method_type = new_method_type;
2054 n_parameters = get_method_n_params(ir_method_type);
2057 ir_node *in[n_parameters];
2059 const call_argument_t *argument = call->arguments;
2060 for (int n = 0; n < n_parameters; ++n) {
2061 expression_t *expression = argument->expression;
2062 ir_node *arg_node = expression_to_firm(expression);
2064 type_t *type = skip_typeref(expression->base.type);
2065 if (!is_type_compound(type)) {
2066 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2067 arg_node = create_conv(dbgi, arg_node, mode);
2068 arg_node = do_strict_conv(dbgi, arg_node);
2073 argument = argument->next;
2076 ir_node *store = get_store();
2077 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2079 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2082 type_t *return_type = skip_typeref(function_type->return_type);
2083 ir_node *result = NULL;
2085 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2086 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2088 if (is_type_scalar(return_type)) {
2089 ir_mode *mode = get_ir_mode_storage(return_type);
2090 result = new_d_Proj(dbgi, resproj, mode, 0);
2091 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2092 result = create_conv(NULL, result, mode_arith);
2094 ir_mode *mode = mode_P_data;
2095 result = new_d_Proj(dbgi, resproj, mode, 0);
2099 if (function->kind == EXPR_REFERENCE &&
2100 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2101 /* A dead end: Keep the Call and the Block. Also place all further
2102 * nodes into a new and unreachable block. */
2104 keep_alive(get_cur_block());
2105 ir_node *block = new_Block(0, NULL);
2106 set_cur_block(block);
2112 static void statement_to_firm(statement_t *statement);
2113 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2115 static ir_node *expression_to_addr(const expression_t *expression);
2116 static ir_node *create_condition_evaluation(const expression_t *expression,
2117 ir_node *true_block,
2118 ir_node *false_block);
2120 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2123 if (!is_type_compound(type)) {
2124 ir_mode *mode = get_ir_mode_storage(type);
2125 value = create_conv(dbgi, value, mode);
2126 value = do_strict_conv(dbgi, value);
2129 ir_node *memory = get_store();
2131 if (is_type_scalar(type)) {
2132 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2133 ? cons_volatile : cons_none;
2134 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2135 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2136 set_store(store_mem);
2138 ir_type *irtype = get_ir_type(type);
2139 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2140 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2141 set_store(copyb_mem);
2145 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2147 ir_tarval *all_one = get_mode_all_one(mode);
2148 int mode_size = get_mode_size_bits(mode);
2150 assert(offset >= 0);
2152 assert(offset + size <= mode_size);
2153 if (size == mode_size) {
2157 long shiftr = get_mode_size_bits(mode) - size;
2158 long shiftl = offset;
2159 ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2160 ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2161 ir_tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2162 ir_tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2167 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2168 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2170 ir_type *entity_type = get_entity_type(entity);
2171 ir_type *base_type = get_primitive_base_type(entity_type);
2172 assert(base_type != NULL);
2173 ir_mode *mode = get_type_mode(base_type);
2175 value = create_conv(dbgi, value, mode);
2177 /* kill upper bits of value and shift to right position */
2178 int bitoffset = get_entity_offset_bits_remainder(entity);
2179 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2181 ir_tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2182 ir_node *mask_node = new_d_Const(dbgi, mask);
2183 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2184 ir_tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2185 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2186 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2188 /* load current value */
2189 ir_node *mem = get_store();
2190 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2191 set_volatile ? cons_volatile : cons_none);
2192 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2193 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2194 ir_tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2195 ir_tarval *inv_mask = tarval_not(shift_mask);
2196 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2197 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2199 /* construct new value and store */
2200 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2201 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2202 set_volatile ? cons_volatile : cons_none);
2203 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2204 set_store(store_mem);
2206 return value_masked;
2209 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2212 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2213 type_t *type = expression->base.type;
2214 ir_mode *mode = get_ir_mode_storage(type);
2215 ir_node *mem = get_store();
2216 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2217 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2218 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2220 load_res = create_conv(dbgi, load_res, mode_int);
2222 set_store(load_mem);
2224 /* kill upper bits */
2225 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2226 ir_entity *entity = expression->compound_entry->compound_member.entity;
2227 int bitoffset = get_entity_offset_bits_remainder(entity);
2228 ir_type *entity_type = get_entity_type(entity);
2229 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2230 long shift_bitsl = machine_size - bitoffset - bitsize;
2231 assert(shift_bitsl >= 0);
2232 ir_tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2233 ir_node *countl = new_d_Const(dbgi, tvl);
2234 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2236 long shift_bitsr = bitoffset + shift_bitsl;
2237 assert(shift_bitsr <= (long) machine_size);
2238 ir_tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2239 ir_node *countr = new_d_Const(dbgi, tvr);
2241 if (mode_is_signed(mode)) {
2242 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2244 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2247 return create_conv(dbgi, shiftr, mode);
2250 /* make sure the selected compound type is constructed */
2251 static void construct_select_compound(const select_expression_t *expression)
2253 type_t *type = skip_typeref(expression->compound->base.type);
2254 if (is_type_pointer(type)) {
2255 type = type->pointer.points_to;
2257 (void) get_ir_type(type);
2260 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2261 ir_node *value, ir_node *addr)
2263 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2264 type_t *type = skip_typeref(expression->base.type);
2266 if (!is_type_compound(type)) {
2267 ir_mode *mode = get_ir_mode_storage(type);
2268 value = create_conv(dbgi, value, mode);
2269 value = do_strict_conv(dbgi, value);
2272 if (expression->kind == EXPR_REFERENCE) {
2273 const reference_expression_t *ref = &expression->reference;
2275 entity_t *entity = ref->entity;
2276 assert(is_declaration(entity));
2277 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2278 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2279 set_value(entity->variable.v.value_number, value);
2281 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2282 set_value(entity->parameter.v.value_number, value);
2288 addr = expression_to_addr(expression);
2289 assert(addr != NULL);
2291 if (expression->kind == EXPR_SELECT) {
2292 const select_expression_t *select = &expression->select;
2294 construct_select_compound(select);
2296 entity_t *entity = select->compound_entry;
2297 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2298 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2299 ir_entity *irentity = entity->compound_member.entity;
2301 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2302 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2308 assign_value(dbgi, addr, type, value);
2312 static void set_value_for_expression(const expression_t *expression,
2315 set_value_for_expression_addr(expression, value, NULL);
2318 static ir_node *get_value_from_lvalue(const expression_t *expression,
2321 if (expression->kind == EXPR_REFERENCE) {
2322 const reference_expression_t *ref = &expression->reference;
2324 entity_t *entity = ref->entity;
2325 assert(entity->kind == ENTITY_VARIABLE
2326 || entity->kind == ENTITY_PARAMETER);
2327 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2329 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2330 value_number = entity->variable.v.value_number;
2331 assert(addr == NULL);
2332 type_t *type = skip_typeref(expression->base.type);
2333 ir_mode *mode = get_ir_mode_storage(type);
2334 ir_node *res = get_value(value_number, mode);
2335 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2336 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2337 value_number = entity->parameter.v.value_number;
2338 assert(addr == NULL);
2339 type_t *type = skip_typeref(expression->base.type);
2340 ir_mode *mode = get_ir_mode_storage(type);
2341 ir_node *res = get_value(value_number, mode);
2342 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2346 assert(addr != NULL);
2347 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2350 if (expression->kind == EXPR_SELECT &&
2351 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2352 construct_select_compound(&expression->select);
2353 value = bitfield_extract_to_firm(&expression->select, addr);
2355 value = deref_address(dbgi, expression->base.type, addr);
2362 static ir_node *create_incdec(const unary_expression_t *expression)
2364 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2365 const expression_t *value_expr = expression->value;
2366 ir_node *addr = expression_to_addr(value_expr);
2367 ir_node *value = get_value_from_lvalue(value_expr, addr);
2369 type_t *type = skip_typeref(expression->base.type);
2370 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2373 if (is_type_pointer(type)) {
2374 pointer_type_t *pointer_type = &type->pointer;
2375 offset = get_type_size_node(pointer_type->points_to);
2377 assert(is_type_arithmetic(type));
2378 offset = new_Const(get_mode_one(mode));
2382 ir_node *store_value;
2383 switch(expression->base.kind) {
2384 case EXPR_UNARY_POSTFIX_INCREMENT:
2386 store_value = new_d_Add(dbgi, value, offset, mode);
2388 case EXPR_UNARY_POSTFIX_DECREMENT:
2390 store_value = new_d_Sub(dbgi, value, offset, mode);
2392 case EXPR_UNARY_PREFIX_INCREMENT:
2393 result = new_d_Add(dbgi, value, offset, mode);
2394 store_value = result;
2396 case EXPR_UNARY_PREFIX_DECREMENT:
2397 result = new_d_Sub(dbgi, value, offset, mode);
2398 store_value = result;
2401 panic("no incdec expr in create_incdec");
2404 set_value_for_expression_addr(value_expr, store_value, addr);
2409 static bool is_local_variable(expression_t *expression)
2411 if (expression->kind != EXPR_REFERENCE)
2413 reference_expression_t *ref_expr = &expression->reference;
2414 entity_t *entity = ref_expr->entity;
2415 if (entity->kind != ENTITY_VARIABLE)
2417 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2418 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2421 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2424 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2425 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2426 case EXPR_BINARY_NOTEQUAL:
2427 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2428 case EXPR_BINARY_ISLESS:
2429 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2430 case EXPR_BINARY_ISLESSEQUAL:
2431 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2432 case EXPR_BINARY_ISGREATER:
2433 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2434 case EXPR_BINARY_ISGREATEREQUAL:
2435 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2436 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2441 panic("trying to get pn_Cmp from non-comparison binexpr type");
2445 * Handle the assume optimizer hint: check if a Confirm
2446 * node can be created.
2448 * @param dbi debug info
2449 * @param expr the IL assume expression
2451 * we support here only some simple cases:
2456 static ir_node *handle_assume_compare(dbg_info *dbi,
2457 const binary_expression_t *expression)
2459 expression_t *op1 = expression->left;
2460 expression_t *op2 = expression->right;
2461 entity_t *var2, *var = NULL;
2462 ir_node *res = NULL;
2465 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2467 if (is_local_variable(op1) && is_local_variable(op2)) {
2468 var = op1->reference.entity;
2469 var2 = op2->reference.entity;
2471 type_t *const type = skip_typeref(var->declaration.type);
2472 ir_mode *const mode = get_ir_mode_storage(type);
2474 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2475 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2477 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2478 set_value(var2->variable.v.value_number, res);
2480 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2481 set_value(var->variable.v.value_number, res);
2487 if (is_local_variable(op1) && is_constant_expression(op2) == EXPR_CLASS_CONSTANT) {
2488 var = op1->reference.entity;
2490 } else if (is_constant_expression(op1) == EXPR_CLASS_CONSTANT && is_local_variable(op2)) {
2491 cmp_val = get_inversed_pnc(cmp_val);
2492 var = op2->reference.entity;
2497 type_t *const type = skip_typeref(var->declaration.type);
2498 ir_mode *const mode = get_ir_mode_storage(type);
2500 res = get_value(var->variable.v.value_number, mode);
2501 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2502 set_value(var->variable.v.value_number, res);
2508 * Handle the assume optimizer hint.
2510 * @param dbi debug info
2511 * @param expr the IL assume expression
2513 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2515 switch(expression->kind) {
2516 case EXPR_BINARY_EQUAL:
2517 case EXPR_BINARY_NOTEQUAL:
2518 case EXPR_BINARY_LESS:
2519 case EXPR_BINARY_LESSEQUAL:
2520 case EXPR_BINARY_GREATER:
2521 case EXPR_BINARY_GREATEREQUAL:
2522 return handle_assume_compare(dbi, &expression->binary);
2528 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2529 type_t *from_type, type_t *type)
2531 type = skip_typeref(type);
2532 if (type == type_void) {
2533 /* make sure firm type is constructed */
2534 (void) get_ir_type(type);
2537 if (!is_type_scalar(type)) {
2538 /* make sure firm type is constructed */
2539 (void) get_ir_type(type);
2543 from_type = skip_typeref(from_type);
2544 ir_mode *mode = get_ir_mode_storage(type);
2545 /* check for conversion from / to __based types */
2546 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2547 const variable_t *from_var = from_type->pointer.base_variable;
2548 const variable_t *to_var = type->pointer.base_variable;
2549 if (from_var != to_var) {
2550 if (from_var != NULL) {
2551 ir_node *const addr = get_global_var_address(dbgi, from_var);
2552 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2553 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2555 if (to_var != NULL) {
2556 ir_node *const addr = get_global_var_address(dbgi, to_var);
2557 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2558 value_node = new_d_Sub(dbgi, value_node, base, mode);
2563 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2564 /* bool adjustments (we save a mode_Bu, but have to temporarily
2565 * convert to mode_b so we only get a 0/1 value */
2566 value_node = create_conv(dbgi, value_node, mode_b);
2569 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2570 ir_node *node = create_conv(dbgi, value_node, mode);
2571 node = do_strict_conv(dbgi, node);
2572 node = create_conv(dbgi, node, mode_arith);
2577 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2579 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2580 type_t *type = skip_typeref(expression->base.type);
2582 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2583 return expression_to_addr(expression->value);
2585 const expression_t *value = expression->value;
2587 switch(expression->base.kind) {
2588 case EXPR_UNARY_NEGATE: {
2589 ir_node *value_node = expression_to_firm(value);
2590 ir_mode *mode = get_ir_mode_arithmetic(type);
2591 return new_d_Minus(dbgi, value_node, mode);
2593 case EXPR_UNARY_PLUS:
2594 return expression_to_firm(value);
2595 case EXPR_UNARY_BITWISE_NEGATE: {
2596 ir_node *value_node = expression_to_firm(value);
2597 ir_mode *mode = get_ir_mode_arithmetic(type);
2598 return new_d_Not(dbgi, value_node, mode);
2600 case EXPR_UNARY_NOT: {
2601 ir_node *value_node = _expression_to_firm(value);
2602 value_node = create_conv(dbgi, value_node, mode_b);
2603 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2606 case EXPR_UNARY_DEREFERENCE: {
2607 ir_node *value_node = expression_to_firm(value);
2608 type_t *value_type = skip_typeref(value->base.type);
2609 assert(is_type_pointer(value_type));
2611 /* check for __based */
2612 const variable_t *const base_var = value_type->pointer.base_variable;
2613 if (base_var != NULL) {
2614 ir_node *const addr = get_global_var_address(dbgi, base_var);
2615 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2616 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2618 type_t *points_to = value_type->pointer.points_to;
2619 return deref_address(dbgi, points_to, value_node);
2621 case EXPR_UNARY_POSTFIX_INCREMENT:
2622 case EXPR_UNARY_POSTFIX_DECREMENT:
2623 case EXPR_UNARY_PREFIX_INCREMENT:
2624 case EXPR_UNARY_PREFIX_DECREMENT:
2625 return create_incdec(expression);
2626 case EXPR_UNARY_CAST_IMPLICIT:
2627 case EXPR_UNARY_CAST: {
2628 ir_node *value_node = expression_to_firm(value);
2629 type_t *from_type = value->base.type;
2630 return create_cast(dbgi, value_node, from_type, type);
2632 case EXPR_UNARY_ASSUME:
2633 return handle_assume(dbgi, value);
2638 panic("invalid UNEXPR type found");
2642 * produces a 0/1 depending of the value of a mode_b node
2644 static ir_node *produce_condition_result(const expression_t *expression,
2645 ir_mode *mode, dbg_info *dbgi)
2647 ir_node *const one_block = new_immBlock();
2648 ir_node *const zero_block = new_immBlock();
2649 create_condition_evaluation(expression, one_block, zero_block);
2650 mature_immBlock(one_block);
2651 mature_immBlock(zero_block);
2653 ir_node *const jmp_one = new_rd_Jmp(dbgi, one_block);
2654 ir_node *const jmp_zero = new_rd_Jmp(dbgi, zero_block);
2655 ir_node *const in_cf[2] = { jmp_one, jmp_zero };
2656 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
2657 set_cur_block(block);
2659 ir_node *const one = new_Const(get_mode_one(mode));
2660 ir_node *const zero = new_Const(get_mode_null(mode));
2661 ir_node *const in[2] = { one, zero };
2662 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
2667 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2668 ir_node *value, type_t *type)
2670 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2671 assert(is_type_pointer(type));
2672 pointer_type_t *const pointer_type = &type->pointer;
2673 type_t *const points_to = skip_typeref(pointer_type->points_to);
2674 ir_node * elem_size = get_type_size_node(points_to);
2675 elem_size = create_conv(dbgi, elem_size, mode);
2676 value = create_conv(dbgi, value, mode);
2677 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2681 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2682 ir_node *left, ir_node *right)
2685 type_t *type_left = skip_typeref(expression->left->base.type);
2686 type_t *type_right = skip_typeref(expression->right->base.type);
2688 expression_kind_t kind = expression->base.kind;
2691 case EXPR_BINARY_SHIFTLEFT:
2692 case EXPR_BINARY_SHIFTRIGHT:
2693 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2694 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2695 mode = get_irn_mode(left);
2696 right = create_conv(dbgi, right, mode_uint);
2699 case EXPR_BINARY_SUB:
2700 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2701 const pointer_type_t *const ptr_type = &type_left->pointer;
2703 mode = get_ir_mode_arithmetic(expression->base.type);
2704 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2705 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2706 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2707 ir_node *const no_mem = new_NoMem();
2708 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2709 mode, op_pin_state_floats);
2710 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2713 case EXPR_BINARY_SUB_ASSIGN:
2714 if (is_type_pointer(type_left)) {
2715 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2716 mode = get_ir_mode_arithmetic(type_left);
2721 case EXPR_BINARY_ADD:
2722 case EXPR_BINARY_ADD_ASSIGN:
2723 if (is_type_pointer(type_left)) {
2724 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2725 mode = get_ir_mode_arithmetic(type_left);
2727 } else if (is_type_pointer(type_right)) {
2728 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2729 mode = get_ir_mode_arithmetic(type_right);
2736 mode = get_ir_mode_arithmetic(type_right);
2737 left = create_conv(dbgi, left, mode);
2742 case EXPR_BINARY_ADD_ASSIGN:
2743 case EXPR_BINARY_ADD:
2744 return new_d_Add(dbgi, left, right, mode);
2745 case EXPR_BINARY_SUB_ASSIGN:
2746 case EXPR_BINARY_SUB:
2747 return new_d_Sub(dbgi, left, right, mode);
2748 case EXPR_BINARY_MUL_ASSIGN:
2749 case EXPR_BINARY_MUL:
2750 return new_d_Mul(dbgi, left, right, mode);
2751 case EXPR_BINARY_BITWISE_AND:
2752 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2753 return new_d_And(dbgi, left, right, mode);
2754 case EXPR_BINARY_BITWISE_OR:
2755 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2756 return new_d_Or(dbgi, left, right, mode);
2757 case EXPR_BINARY_BITWISE_XOR:
2758 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2759 return new_d_Eor(dbgi, left, right, mode);
2760 case EXPR_BINARY_SHIFTLEFT:
2761 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2762 return new_d_Shl(dbgi, left, right, mode);
2763 case EXPR_BINARY_SHIFTRIGHT:
2764 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2765 if (mode_is_signed(mode)) {
2766 return new_d_Shrs(dbgi, left, right, mode);
2768 return new_d_Shr(dbgi, left, right, mode);
2770 case EXPR_BINARY_DIV:
2771 case EXPR_BINARY_DIV_ASSIGN: {
2772 ir_node *pin = new_Pin(new_NoMem());
2773 ir_node *op = new_d_Div(dbgi, pin, left, right, mode,
2774 op_pin_state_floats);
2775 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2778 case EXPR_BINARY_MOD:
2779 case EXPR_BINARY_MOD_ASSIGN: {
2780 ir_node *pin = new_Pin(new_NoMem());
2781 assert(!mode_is_float(mode));
2782 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2783 op_pin_state_floats);
2784 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2788 panic("unexpected expression kind");
2792 static ir_node *create_lazy_op(const binary_expression_t *expression)
2794 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2795 type_t *type = skip_typeref(expression->base.type);
2796 ir_mode *mode = get_ir_mode_arithmetic(type);
2798 if (is_constant_expression(expression->left) == EXPR_CLASS_CONSTANT) {
2799 bool val = fold_constant_to_bool(expression->left);
2800 expression_kind_t ekind = expression->base.kind;
2801 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2802 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2804 return new_Const(get_mode_null(mode));
2808 return new_Const(get_mode_one(mode));
2812 if (is_constant_expression(expression->right) == EXPR_CLASS_CONSTANT) {
2813 bool valr = fold_constant_to_bool(expression->right);
2814 return create_Const_from_bool(mode, valr);
2817 return produce_condition_result(expression->right, mode, dbgi);
2820 return produce_condition_result((const expression_t*) expression, mode,
2824 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2825 ir_node *right, ir_mode *mode);
2827 static ir_node *create_assign_binop(const binary_expression_t *expression)
2829 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2830 const expression_t *left_expr = expression->left;
2831 type_t *type = skip_typeref(left_expr->base.type);
2832 ir_node *right = expression_to_firm(expression->right);
2833 ir_node *left_addr = expression_to_addr(left_expr);
2834 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2835 ir_node *result = create_op(dbgi, expression, left, right);
2837 result = create_cast(dbgi, result, expression->right->base.type, type);
2838 result = do_strict_conv(dbgi, result);
2840 result = set_value_for_expression_addr(left_expr, result, left_addr);
2842 if (!is_type_compound(type)) {
2843 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2844 result = create_conv(dbgi, result, mode_arithmetic);
2849 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2851 expression_kind_t kind = expression->base.kind;
2854 case EXPR_BINARY_EQUAL:
2855 case EXPR_BINARY_NOTEQUAL:
2856 case EXPR_BINARY_LESS:
2857 case EXPR_BINARY_LESSEQUAL:
2858 case EXPR_BINARY_GREATER:
2859 case EXPR_BINARY_GREATEREQUAL:
2860 case EXPR_BINARY_ISGREATER:
2861 case EXPR_BINARY_ISGREATEREQUAL:
2862 case EXPR_BINARY_ISLESS:
2863 case EXPR_BINARY_ISLESSEQUAL:
2864 case EXPR_BINARY_ISLESSGREATER:
2865 case EXPR_BINARY_ISUNORDERED: {
2866 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2867 ir_node *left = expression_to_firm(expression->left);
2868 ir_node *right = expression_to_firm(expression->right);
2869 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2870 long pnc = get_pnc(kind, expression->left->base.type);
2871 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2874 case EXPR_BINARY_ASSIGN: {
2875 ir_node *addr = expression_to_addr(expression->left);
2876 ir_node *right = expression_to_firm(expression->right);
2878 = set_value_for_expression_addr(expression->left, right, addr);
2880 type_t *type = skip_typeref(expression->base.type);
2881 if (!is_type_compound(type)) {
2882 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2883 res = create_conv(NULL, res, mode_arithmetic);
2887 case EXPR_BINARY_ADD:
2888 case EXPR_BINARY_SUB:
2889 case EXPR_BINARY_MUL:
2890 case EXPR_BINARY_DIV:
2891 case EXPR_BINARY_MOD:
2892 case EXPR_BINARY_BITWISE_AND:
2893 case EXPR_BINARY_BITWISE_OR:
2894 case EXPR_BINARY_BITWISE_XOR:
2895 case EXPR_BINARY_SHIFTLEFT:
2896 case EXPR_BINARY_SHIFTRIGHT:
2898 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2899 ir_node *left = expression_to_firm(expression->left);
2900 ir_node *right = expression_to_firm(expression->right);
2901 return create_op(dbgi, expression, left, right);
2903 case EXPR_BINARY_LOGICAL_AND:
2904 case EXPR_BINARY_LOGICAL_OR:
2905 return create_lazy_op(expression);
2906 case EXPR_BINARY_COMMA:
2907 /* create side effects of left side */
2908 (void) expression_to_firm(expression->left);
2909 return _expression_to_firm(expression->right);
2911 case EXPR_BINARY_ADD_ASSIGN:
2912 case EXPR_BINARY_SUB_ASSIGN:
2913 case EXPR_BINARY_MUL_ASSIGN:
2914 case EXPR_BINARY_MOD_ASSIGN:
2915 case EXPR_BINARY_DIV_ASSIGN:
2916 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2917 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2918 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2919 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2920 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2921 return create_assign_binop(expression);
2923 panic("TODO binexpr type");
2927 static ir_node *array_access_addr(const array_access_expression_t *expression)
2929 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2930 ir_node *base_addr = expression_to_firm(expression->array_ref);
2931 ir_node *offset = expression_to_firm(expression->index);
2932 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2933 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2934 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2939 static ir_node *array_access_to_firm(
2940 const array_access_expression_t *expression)
2942 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2943 ir_node *addr = array_access_addr(expression);
2944 type_t *type = revert_automatic_type_conversion(
2945 (const expression_t*) expression);
2946 type = skip_typeref(type);
2948 return deref_address(dbgi, type, addr);
2951 static long get_offsetof_offset(const offsetof_expression_t *expression)
2953 type_t *orig_type = expression->type;
2956 designator_t *designator = expression->designator;
2957 for ( ; designator != NULL; designator = designator->next) {
2958 type_t *type = skip_typeref(orig_type);
2959 /* be sure the type is constructed */
2960 (void) get_ir_type(type);
2962 if (designator->symbol != NULL) {
2963 assert(is_type_compound(type));
2964 symbol_t *symbol = designator->symbol;
2966 compound_t *compound = type->compound.compound;
2967 entity_t *iter = compound->members.entities;
2968 for ( ; iter != NULL; iter = iter->base.next) {
2969 if (iter->base.symbol == symbol) {
2973 assert(iter != NULL);
2975 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2976 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2977 offset += get_entity_offset(iter->compound_member.entity);
2979 orig_type = iter->declaration.type;
2981 expression_t *array_index = designator->array_index;
2982 assert(designator->array_index != NULL);
2983 assert(is_type_array(type));
2985 long index = fold_constant_to_int(array_index);
2986 ir_type *arr_type = get_ir_type(type);
2987 ir_type *elem_type = get_array_element_type(arr_type);
2988 long elem_size = get_type_size_bytes(elem_type);
2990 offset += index * elem_size;
2992 orig_type = type->array.element_type;
2999 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
3001 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3002 long offset = get_offsetof_offset(expression);
3003 ir_tarval *tv = new_tarval_from_long(offset, mode);
3004 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3006 return new_d_Const(dbgi, tv);
3009 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3010 ir_entity *entity, type_t *type);
3012 static ir_node *compound_literal_to_firm(
3013 const compound_literal_expression_t *expression)
3015 type_t *type = expression->type;
3017 /* create an entity on the stack */
3018 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3020 ident *const id = id_unique("CompLit.%u");
3021 ir_type *const irtype = get_ir_type(type);
3022 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3023 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
3024 set_entity_ld_ident(entity, id);
3026 /* create initialisation code */
3027 initializer_t *initializer = expression->initializer;
3028 create_local_initializer(initializer, dbgi, entity, type);
3030 /* create a sel for the compound literal address */
3031 ir_node *frame = get_irg_frame(current_ir_graph);
3032 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3037 * Transform a sizeof expression into Firm code.
3039 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3041 type_t *const type = skip_typeref(expression->type);
3042 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3043 if (is_type_array(type) && type->array.is_vla
3044 && expression->tp_expression != NULL) {
3045 expression_to_firm(expression->tp_expression);
3048 return get_type_size_node(type);
3051 static entity_t *get_expression_entity(const expression_t *expression)
3053 if (expression->kind != EXPR_REFERENCE)
3056 return expression->reference.entity;
3059 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3061 switch(entity->kind) {
3062 DECLARATION_KIND_CASES
3063 return entity->declaration.alignment;
3066 return entity->compound.alignment;
3067 case ENTITY_TYPEDEF:
3068 return entity->typedefe.alignment;
3076 * Transform an alignof expression into Firm code.
3078 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3080 unsigned alignment = 0;
3082 const expression_t *tp_expression = expression->tp_expression;
3083 if (tp_expression != NULL) {
3084 entity_t *entity = get_expression_entity(tp_expression);
3085 if (entity != NULL) {
3086 alignment = get_cparser_entity_alignment(entity);
3090 if (alignment == 0) {
3091 type_t *type = expression->type;
3092 alignment = get_type_alignment(type);
3095 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3096 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3097 ir_tarval *tv = new_tarval_from_long(alignment, mode);
3098 return new_d_Const(dbgi, tv);
3101 static void init_ir_types(void);
3103 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3105 assert(is_type_valid(skip_typeref(expression->base.type)));
3107 bool constant_folding_old = constant_folding;
3108 constant_folding = true;
3112 assert(is_constant_expression(expression) == EXPR_CLASS_CONSTANT);
3114 ir_graph *old_current_ir_graph = current_ir_graph;
3115 current_ir_graph = get_const_code_irg();
3117 ir_node *cnst = expression_to_firm(expression);
3118 current_ir_graph = old_current_ir_graph;
3120 if (!is_Const(cnst)) {
3121 panic("couldn't fold constant");
3124 constant_folding = constant_folding_old;
3126 return get_Const_tarval(cnst);
3129 long fold_constant_to_int(const expression_t *expression)
3131 if (expression->kind == EXPR_INVALID)
3134 ir_tarval *tv = fold_constant_to_tarval(expression);
3135 if (!tarval_is_long(tv)) {
3136 panic("result of constant folding is not integer");
3139 return get_tarval_long(tv);
3142 bool fold_constant_to_bool(const expression_t *expression)
3144 if (expression->kind == EXPR_INVALID)
3146 ir_tarval *tv = fold_constant_to_tarval(expression);
3147 return !tarval_is_null(tv);
3150 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3152 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3154 /* first try to fold a constant condition */
3155 if (is_constant_expression(expression->condition) == EXPR_CLASS_CONSTANT) {
3156 bool val = fold_constant_to_bool(expression->condition);
3158 expression_t *true_expression = expression->true_expression;
3159 if (true_expression == NULL)
3160 true_expression = expression->condition;
3161 return expression_to_firm(true_expression);
3163 return expression_to_firm(expression->false_expression);
3167 ir_node *const true_block = new_immBlock();
3168 ir_node *const false_block = new_immBlock();
3169 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3170 mature_immBlock(true_block);
3171 mature_immBlock(false_block);
3173 set_cur_block(true_block);
3175 if (expression->true_expression != NULL) {
3176 true_val = expression_to_firm(expression->true_expression);
3177 } else if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3178 true_val = cond_expr;
3180 /* Condition ended with a short circuit (&&, ||, !) operation or a
3181 * comparison. Generate a "1" as value for the true branch. */
3182 true_val = new_Const(get_mode_one(mode_Is));
3184 ir_node *const true_jmp = new_d_Jmp(dbgi);
3186 set_cur_block(false_block);
3187 ir_node *const false_val = expression_to_firm(expression->false_expression);
3188 ir_node *const false_jmp = new_d_Jmp(dbgi);
3190 /* create the common block */
3191 ir_node *const in_cf[2] = { true_jmp, false_jmp };
3192 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
3193 set_cur_block(block);
3195 /* TODO improve static semantics, so either both or no values are NULL */
3196 if (true_val == NULL || false_val == NULL)
3199 ir_node *const in[2] = { true_val, false_val };
3200 type_t *const type = skip_typeref(expression->base.type);
3202 if (is_type_compound(type)) {
3205 mode = get_ir_mode_arithmetic(type);
3207 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
3213 * Returns an IR-node representing the address of a field.
3215 static ir_node *select_addr(const select_expression_t *expression)
3217 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3219 construct_select_compound(expression);
3221 ir_node *compound_addr = expression_to_firm(expression->compound);
3223 entity_t *entry = expression->compound_entry;
3224 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3225 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3227 if (constant_folding) {
3228 ir_mode *mode = get_irn_mode(compound_addr);
3229 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3230 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3231 return new_d_Add(dbgi, compound_addr, ofs, mode);
3233 ir_entity *irentity = entry->compound_member.entity;
3234 assert(irentity != NULL);
3235 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3239 static ir_node *select_to_firm(const select_expression_t *expression)
3241 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3242 ir_node *addr = select_addr(expression);
3243 type_t *type = revert_automatic_type_conversion(
3244 (const expression_t*) expression);
3245 type = skip_typeref(type);
3247 entity_t *entry = expression->compound_entry;
3248 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3249 type_t *entry_type = skip_typeref(entry->declaration.type);
3251 if (entry_type->kind == TYPE_BITFIELD) {
3252 return bitfield_extract_to_firm(expression, addr);
3255 return deref_address(dbgi, type, addr);
3258 /* Values returned by __builtin_classify_type. */
3259 typedef enum gcc_type_class
3265 enumeral_type_class,
3268 reference_type_class,
3272 function_type_class,
3283 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3285 type_t *type = expr->type_expression->base.type;
3287 /* FIXME gcc returns different values depending on whether compiling C or C++
3288 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3291 type = skip_typeref(type);
3292 switch (type->kind) {
3294 const atomic_type_t *const atomic_type = &type->atomic;
3295 switch (atomic_type->akind) {
3296 /* should not be reached */
3297 case ATOMIC_TYPE_INVALID:
3301 /* gcc cannot do that */
3302 case ATOMIC_TYPE_VOID:
3303 tc = void_type_class;
3306 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3307 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3308 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3309 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3310 case ATOMIC_TYPE_SHORT:
3311 case ATOMIC_TYPE_USHORT:
3312 case ATOMIC_TYPE_INT:
3313 case ATOMIC_TYPE_UINT:
3314 case ATOMIC_TYPE_LONG:
3315 case ATOMIC_TYPE_ULONG:
3316 case ATOMIC_TYPE_LONGLONG:
3317 case ATOMIC_TYPE_ULONGLONG:
3318 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3319 tc = integer_type_class;
3322 case ATOMIC_TYPE_FLOAT:
3323 case ATOMIC_TYPE_DOUBLE:
3324 case ATOMIC_TYPE_LONG_DOUBLE:
3325 tc = real_type_class;
3328 panic("Unexpected atomic type in classify_type_to_firm().");
3331 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3332 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3333 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3334 case TYPE_ARRAY: /* gcc handles this as pointer */
3335 case TYPE_FUNCTION: /* gcc handles this as pointer */
3336 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3337 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3338 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3340 /* gcc handles this as integer */
3341 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3343 /* gcc classifies the referenced type */
3344 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3347 /* typedef/typeof should be skipped already */
3354 panic("unexpected TYPE classify_type_to_firm().");
3358 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3359 ir_tarval *const tv = new_tarval_from_long(tc, mode_int);
3360 return new_d_Const(dbgi, tv);
3363 static ir_node *function_name_to_firm(
3364 const funcname_expression_t *const expr)
3366 switch(expr->kind) {
3367 case FUNCNAME_FUNCTION:
3368 case FUNCNAME_PRETTY_FUNCTION:
3369 case FUNCNAME_FUNCDNAME:
3370 if (current_function_name == NULL) {
3371 const source_position_t *const src_pos = &expr->base.source_position;
3372 const char *name = current_function_entity->base.symbol->string;
3373 const string_t string = { name, strlen(name) + 1 };
3374 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3376 return current_function_name;
3377 case FUNCNAME_FUNCSIG:
3378 if (current_funcsig == NULL) {
3379 const source_position_t *const src_pos = &expr->base.source_position;
3380 ir_entity *ent = get_irg_entity(current_ir_graph);
3381 const char *const name = get_entity_ld_name(ent);
3382 const string_t string = { name, strlen(name) + 1 };
3383 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3385 return current_funcsig;
3387 panic("Unsupported function name");
3390 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3392 statement_t *statement = expr->statement;
3394 assert(statement->kind == STATEMENT_COMPOUND);
3395 return compound_statement_to_firm(&statement->compound);
3398 static ir_node *va_start_expression_to_firm(
3399 const va_start_expression_t *const expr)
3401 type_t *const type = current_function_entity->declaration.type;
3402 ir_type *const method_type = get_ir_type(type);
3403 int const n = get_method_n_params(method_type) - 1;
3404 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3405 ir_node *const frame = get_irg_frame(current_ir_graph);
3406 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3407 ir_node *const no_mem = new_NoMem();
3408 ir_node *const arg_sel =
3409 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3411 type_t *const param_type = expr->parameter->base.type;
3412 ir_node *const cnst = get_type_size_node(param_type);
3413 ir_mode *const mode = get_irn_mode(cnst);
3414 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3415 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3416 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3417 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3418 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3419 set_value_for_expression(expr->ap, add);
3424 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3426 type_t *const type = expr->base.type;
3427 expression_t *const ap_expr = expr->ap;
3428 ir_node *const ap_addr = expression_to_addr(ap_expr);
3429 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3430 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3431 ir_node *const res = deref_address(dbgi, type, ap);
3433 ir_node *const cnst = get_type_size_node(expr->base.type);
3434 ir_mode *const mode = get_irn_mode(cnst);
3435 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3436 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3437 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3438 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3439 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3441 set_value_for_expression_addr(ap_expr, add, ap_addr);
3447 * Generate Firm for a va_copy expression.
3449 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3451 ir_node *const src = expression_to_firm(expr->src);
3452 set_value_for_expression(expr->dst, src);
3456 static ir_node *dereference_addr(const unary_expression_t *const expression)
3458 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3459 return expression_to_firm(expression->value);
3463 * Returns a IR-node representing an lvalue of the given expression.
3465 static ir_node *expression_to_addr(const expression_t *expression)
3467 switch(expression->kind) {
3468 case EXPR_ARRAY_ACCESS:
3469 return array_access_addr(&expression->array_access);
3471 return call_expression_to_firm(&expression->call);
3472 case EXPR_COMPOUND_LITERAL:
3473 return compound_literal_to_firm(&expression->compound_literal);
3474 case EXPR_REFERENCE:
3475 return reference_addr(&expression->reference);
3477 return select_addr(&expression->select);
3478 case EXPR_UNARY_DEREFERENCE:
3479 return dereference_addr(&expression->unary);
3483 panic("trying to get address of non-lvalue");
3486 static ir_node *builtin_constant_to_firm(
3487 const builtin_constant_expression_t *expression)
3489 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3490 bool const v = is_constant_expression(expression->value) == EXPR_CLASS_CONSTANT;
3491 return create_Const_from_bool(mode, v);
3494 static ir_node *builtin_types_compatible_to_firm(
3495 const builtin_types_compatible_expression_t *expression)
3497 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3498 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3499 bool const value = types_compatible(left, right);
3500 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3501 return create_Const_from_bool(mode, value);
3504 static ir_node *get_label_block(label_t *label)
3506 if (label->block != NULL)
3507 return label->block;
3509 /* beware: might be called from create initializer with current_ir_graph
3510 * set to const_code_irg. */
3511 ir_graph *rem = current_ir_graph;
3512 current_ir_graph = current_function;
3514 ir_node *block = new_immBlock();
3516 label->block = block;
3518 ARR_APP1(label_t *, all_labels, label);
3520 current_ir_graph = rem;
3525 * Pointer to a label. This is used for the
3526 * GNU address-of-label extension.
3528 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3530 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3531 ir_node *block = get_label_block(label->label);
3532 ir_entity *entity = create_Block_entity(block);
3534 symconst_symbol value;
3535 value.entity_p = entity;
3536 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3540 * creates firm nodes for an expression. The difference between this function
3541 * and expression_to_firm is, that this version might produce mode_b nodes
3542 * instead of mode_Is.
3544 static ir_node *_expression_to_firm(const expression_t *expression)
3547 if (!constant_folding) {
3548 assert(!expression->base.transformed);
3549 ((expression_t*) expression)->base.transformed = true;
3553 switch (expression->kind) {
3555 return literal_to_firm(&expression->literal);
3556 case EXPR_STRING_LITERAL:
3557 return string_to_firm(&expression->base.source_position, "str.%u",
3558 &expression->literal.value);
3559 case EXPR_WIDE_STRING_LITERAL:
3560 return wide_string_literal_to_firm(&expression->string_literal);
3561 case EXPR_REFERENCE:
3562 return reference_expression_to_firm(&expression->reference);
3563 case EXPR_REFERENCE_ENUM_VALUE:
3564 return reference_expression_enum_value_to_firm(&expression->reference);
3566 return call_expression_to_firm(&expression->call);
3568 return unary_expression_to_firm(&expression->unary);
3570 return binary_expression_to_firm(&expression->binary);
3571 case EXPR_ARRAY_ACCESS:
3572 return array_access_to_firm(&expression->array_access);
3574 return sizeof_to_firm(&expression->typeprop);
3576 return alignof_to_firm(&expression->typeprop);
3577 case EXPR_CONDITIONAL:
3578 return conditional_to_firm(&expression->conditional);
3580 return select_to_firm(&expression->select);
3581 case EXPR_CLASSIFY_TYPE:
3582 return classify_type_to_firm(&expression->classify_type);
3584 return function_name_to_firm(&expression->funcname);
3585 case EXPR_STATEMENT:
3586 return statement_expression_to_firm(&expression->statement);
3588 return va_start_expression_to_firm(&expression->va_starte);
3590 return va_arg_expression_to_firm(&expression->va_arge);
3592 return va_copy_expression_to_firm(&expression->va_copye);
3593 case EXPR_BUILTIN_CONSTANT_P:
3594 return builtin_constant_to_firm(&expression->builtin_constant);
3595 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3596 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3598 return offsetof_to_firm(&expression->offsetofe);
3599 case EXPR_COMPOUND_LITERAL:
3600 return compound_literal_to_firm(&expression->compound_literal);
3601 case EXPR_LABEL_ADDRESS:
3602 return label_address_to_firm(&expression->label_address);
3608 panic("invalid expression found");
3612 * Check if a given expression is a GNU __builtin_expect() call.
3614 static bool is_builtin_expect(const expression_t *expression)
3616 if (expression->kind != EXPR_CALL)
3619 expression_t *function = expression->call.function;
3620 if (function->kind != EXPR_REFERENCE)
3622 reference_expression_t *ref = &function->reference;
3623 if (ref->entity->kind != ENTITY_FUNCTION ||
3624 ref->entity->function.btk != bk_gnu_builtin_expect)
3630 static bool produces_mode_b(const expression_t *expression)
3632 switch (expression->kind) {
3633 case EXPR_BINARY_EQUAL:
3634 case EXPR_BINARY_NOTEQUAL:
3635 case EXPR_BINARY_LESS:
3636 case EXPR_BINARY_LESSEQUAL:
3637 case EXPR_BINARY_GREATER:
3638 case EXPR_BINARY_GREATEREQUAL:
3639 case EXPR_BINARY_ISGREATER:
3640 case EXPR_BINARY_ISGREATEREQUAL:
3641 case EXPR_BINARY_ISLESS:
3642 case EXPR_BINARY_ISLESSEQUAL:
3643 case EXPR_BINARY_ISLESSGREATER:
3644 case EXPR_BINARY_ISUNORDERED:
3645 case EXPR_UNARY_NOT:
3649 if (is_builtin_expect(expression)) {
3650 expression_t *argument = expression->call.arguments->expression;
3651 return produces_mode_b(argument);
3654 case EXPR_BINARY_COMMA:
3655 return produces_mode_b(expression->binary.right);
3662 static ir_node *expression_to_firm(const expression_t *expression)
3664 if (!produces_mode_b(expression)) {
3665 ir_node *res = _expression_to_firm(expression);
3666 assert(res == NULL || get_irn_mode(res) != mode_b);
3670 if (is_constant_expression(expression) == EXPR_CLASS_CONSTANT) {
3671 bool const constant_folding_old = constant_folding;
3672 constant_folding = true;
3673 ir_node *res = _expression_to_firm(expression);
3674 constant_folding = constant_folding_old;
3675 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3676 assert(is_Const(res));
3677 return create_Const_from_bool(mode, !is_Const_null(res));
3680 /* we have to produce a 0/1 from the mode_b expression */
3681 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3682 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3683 return produce_condition_result(expression, mode, dbgi);
3687 * create a short-circuit expression evaluation that tries to construct
3688 * efficient control flow structures for &&, || and ! expressions
3690 static ir_node *create_condition_evaluation(const expression_t *expression,
3691 ir_node *true_block,
3692 ir_node *false_block)
3694 switch(expression->kind) {
3695 case EXPR_UNARY_NOT: {
3696 const unary_expression_t *unary_expression = &expression->unary;
3697 create_condition_evaluation(unary_expression->value, false_block,
3701 case EXPR_BINARY_LOGICAL_AND: {
3702 const binary_expression_t *binary_expression = &expression->binary;
3704 ir_node *extra_block = new_immBlock();
3705 create_condition_evaluation(binary_expression->left, extra_block,
3707 mature_immBlock(extra_block);
3708 set_cur_block(extra_block);
3709 create_condition_evaluation(binary_expression->right, true_block,
3713 case EXPR_BINARY_LOGICAL_OR: {
3714 const binary_expression_t *binary_expression = &expression->binary;
3716 ir_node *extra_block = new_immBlock();
3717 create_condition_evaluation(binary_expression->left, true_block,
3719 mature_immBlock(extra_block);
3720 set_cur_block(extra_block);
3721 create_condition_evaluation(binary_expression->right, true_block,
3729 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3730 ir_node *cond_expr = _expression_to_firm(expression);
3731 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3732 ir_node *cond = new_d_Cond(dbgi, condition);
3733 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3734 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3736 /* set branch prediction info based on __builtin_expect */
3737 if (is_builtin_expect(expression) && is_Cond(cond)) {
3738 call_argument_t *argument = expression->call.arguments->next;
3739 if (is_constant_expression(argument->expression) == EXPR_CLASS_CONSTANT) {
3740 bool cnst = fold_constant_to_bool(argument->expression);
3741 cond_jmp_predicate pred;
3743 if (cnst == false) {
3744 pred = COND_JMP_PRED_FALSE;
3746 pred = COND_JMP_PRED_TRUE;
3748 set_Cond_jmp_pred(cond, pred);
3752 add_immBlock_pred(true_block, true_proj);
3753 add_immBlock_pred(false_block, false_proj);
3755 set_cur_block(NULL);
3759 static void create_variable_entity(entity_t *variable,
3760 declaration_kind_t declaration_kind,
3761 ir_type *parent_type)
3763 assert(variable->kind == ENTITY_VARIABLE);
3764 type_t *type = skip_typeref(variable->declaration.type);
3766 ident *const id = new_id_from_str(variable->base.symbol->string);
3767 ir_type *const irtype = get_ir_type(type);
3768 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3769 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3770 unsigned alignment = variable->declaration.alignment;
3772 set_entity_alignment(irentity, alignment);
3774 handle_decl_modifiers(irentity, variable);
3776 variable->declaration.kind = (unsigned char) declaration_kind;
3777 variable->variable.v.entity = irentity;
3778 set_entity_ld_ident(irentity, create_ld_ident(variable));
3780 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3781 set_entity_volatility(irentity, volatility_is_volatile);
3786 typedef struct type_path_entry_t type_path_entry_t;
3787 struct type_path_entry_t {
3789 ir_initializer_t *initializer;
3791 entity_t *compound_entry;
3794 typedef struct type_path_t type_path_t;
3795 struct type_path_t {
3796 type_path_entry_t *path;
3801 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3803 size_t len = ARR_LEN(path->path);
3805 for (size_t i = 0; i < len; ++i) {
3806 const type_path_entry_t *entry = & path->path[i];
3808 type_t *type = skip_typeref(entry->type);
3809 if (is_type_compound(type)) {
3810 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3811 } else if (is_type_array(type)) {
3812 fprintf(stderr, "[%u]", (unsigned) entry->index);
3814 fprintf(stderr, "-INVALID-");
3817 fprintf(stderr, " (");
3818 print_type(path->top_type);
3819 fprintf(stderr, ")");
3822 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3824 size_t len = ARR_LEN(path->path);
3826 return & path->path[len-1];
3829 static type_path_entry_t *append_to_type_path(type_path_t *path)
3831 size_t len = ARR_LEN(path->path);
3832 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3834 type_path_entry_t *result = & path->path[len];
3835 memset(result, 0, sizeof(result[0]));
3839 static size_t get_compound_member_count(const compound_type_t *type)
3841 compound_t *compound = type->compound;
3842 size_t n_members = 0;
3843 entity_t *member = compound->members.entities;
3844 for ( ; member != NULL; member = member->base.next) {
3851 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3853 type_t *orig_top_type = path->top_type;
3854 type_t *top_type = skip_typeref(orig_top_type);
3856 assert(is_type_compound(top_type) || is_type_array(top_type));
3858 if (ARR_LEN(path->path) == 0) {
3861 type_path_entry_t *top = get_type_path_top(path);
3862 ir_initializer_t *initializer = top->initializer;
3863 return get_initializer_compound_value(initializer, top->index);
3867 static void descend_into_subtype(type_path_t *path)
3869 type_t *orig_top_type = path->top_type;
3870 type_t *top_type = skip_typeref(orig_top_type);
3872 assert(is_type_compound(top_type) || is_type_array(top_type));
3874 ir_initializer_t *initializer = get_initializer_entry(path);
3876 type_path_entry_t *top = append_to_type_path(path);
3877 top->type = top_type;
3881 if (is_type_compound(top_type)) {
3882 compound_t *compound = top_type->compound.compound;
3883 entity_t *entry = compound->members.entities;
3885 top->compound_entry = entry;
3887 len = get_compound_member_count(&top_type->compound);
3888 if (entry != NULL) {
3889 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3890 path->top_type = entry->declaration.type;
3893 assert(is_type_array(top_type));
3894 assert(top_type->array.size > 0);
3897 path->top_type = top_type->array.element_type;
3898 len = top_type->array.size;
3900 if (initializer == NULL
3901 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3902 initializer = create_initializer_compound(len);
3903 /* we have to set the entry at the 2nd latest path entry... */
3904 size_t path_len = ARR_LEN(path->path);
3905 assert(path_len >= 1);
3907 type_path_entry_t *entry = & path->path[path_len-2];
3908 ir_initializer_t *tinitializer = entry->initializer;
3909 set_initializer_compound_value(tinitializer, entry->index,
3913 top->initializer = initializer;
3916 static void ascend_from_subtype(type_path_t *path)
3918 type_path_entry_t *top = get_type_path_top(path);
3920 path->top_type = top->type;
3922 size_t len = ARR_LEN(path->path);
3923 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3926 static void walk_designator(type_path_t *path, const designator_t *designator)
3928 /* designators start at current object type */
3929 ARR_RESIZE(type_path_entry_t, path->path, 1);
3931 for ( ; designator != NULL; designator = designator->next) {
3932 type_path_entry_t *top = get_type_path_top(path);
3933 type_t *orig_type = top->type;
3934 type_t *type = skip_typeref(orig_type);
3936 if (designator->symbol != NULL) {
3937 assert(is_type_compound(type));
3939 symbol_t *symbol = designator->symbol;
3941 compound_t *compound = type->compound.compound;
3942 entity_t *iter = compound->members.entities;
3943 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3944 if (iter->base.symbol == symbol) {
3945 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3949 assert(iter != NULL);
3951 /* revert previous initialisations of other union elements */
3952 if (type->kind == TYPE_COMPOUND_UNION) {
3953 ir_initializer_t *initializer = top->initializer;
3954 if (initializer != NULL
3955 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3956 /* are we writing to a new element? */
3957 ir_initializer_t *oldi
3958 = get_initializer_compound_value(initializer, index);
3959 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3960 /* clear initializer */
3962 = get_initializer_compound_n_entries(initializer);
3963 ir_initializer_t *nulli = get_initializer_null();
3964 for (size_t i = 0; i < len; ++i) {
3965 set_initializer_compound_value(initializer, i,
3972 top->type = orig_type;
3973 top->compound_entry = iter;
3975 orig_type = iter->declaration.type;
3977 expression_t *array_index = designator->array_index;
3978 assert(designator->array_index != NULL);
3979 assert(is_type_array(type));
3981 long index = fold_constant_to_int(array_index);
3984 if (type->array.size_constant) {
3985 long array_size = type->array.size;
3986 assert(index < array_size);
3990 top->type = orig_type;
3991 top->index = (size_t) index;
3992 orig_type = type->array.element_type;
3994 path->top_type = orig_type;
3996 if (designator->next != NULL) {
3997 descend_into_subtype(path);
4001 path->invalid = false;
4004 static void advance_current_object(type_path_t *path)
4006 if (path->invalid) {
4007 /* TODO: handle this... */
4008 panic("invalid initializer in ast2firm (excessive elements)");
4011 type_path_entry_t *top = get_type_path_top(path);
4013 type_t *type = skip_typeref(top->type);
4014 if (is_type_union(type)) {
4015 /* only the first element is initialized in unions */
4016 top->compound_entry = NULL;
4017 } else if (is_type_struct(type)) {
4018 entity_t *entry = top->compound_entry;
4021 entry = entry->base.next;
4022 top->compound_entry = entry;
4023 if (entry != NULL) {
4024 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
4025 path->top_type = entry->declaration.type;
4029 assert(is_type_array(type));
4032 if (!type->array.size_constant || top->index < type->array.size) {
4037 /* we're past the last member of the current sub-aggregate, try if we
4038 * can ascend in the type hierarchy and continue with another subobject */
4039 size_t len = ARR_LEN(path->path);
4042 ascend_from_subtype(path);
4043 advance_current_object(path);
4045 path->invalid = true;
4050 static ir_initializer_t *create_ir_initializer(
4051 const initializer_t *initializer, type_t *type);
4053 static ir_initializer_t *create_ir_initializer_value(
4054 const initializer_value_t *initializer)
4056 if (is_type_compound(initializer->value->base.type)) {
4057 panic("initializer creation for compounds not implemented yet");
4059 type_t *type = initializer->value->base.type;
4060 expression_t *expr = initializer->value;
4061 if (initializer_use_bitfield_basetype) {
4062 type_t *skipped = skip_typeref(type);
4063 if (skipped->kind == TYPE_BITFIELD) {
4064 /* remove the bitfield cast... */
4065 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4066 expr = expr->unary.value;
4067 type = skipped->bitfield.base_type;
4070 ir_node *value = expression_to_firm(expr);
4071 ir_mode *mode = get_ir_mode_storage(type);
4072 value = create_conv(NULL, value, mode);
4073 return create_initializer_const(value);
4076 /** test wether type can be initialized by a string constant */
4077 static bool is_string_type(type_t *type)
4080 if (is_type_pointer(type)) {
4081 inner = skip_typeref(type->pointer.points_to);
4082 } else if(is_type_array(type)) {
4083 inner = skip_typeref(type->array.element_type);
4088 return is_type_integer(inner);
4091 static ir_initializer_t *create_ir_initializer_list(
4092 const initializer_list_t *initializer, type_t *type)
4095 memset(&path, 0, sizeof(path));
4096 path.top_type = type;
4097 path.path = NEW_ARR_F(type_path_entry_t, 0);
4099 descend_into_subtype(&path);
4101 for (size_t i = 0; i < initializer->len; ++i) {
4102 const initializer_t *sub_initializer = initializer->initializers[i];
4104 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4105 walk_designator(&path, sub_initializer->designator.designator);
4109 if (sub_initializer->kind == INITIALIZER_VALUE) {
4110 /* we might have to descend into types until we're at a scalar
4113 type_t *orig_top_type = path.top_type;
4114 type_t *top_type = skip_typeref(orig_top_type);
4116 if (is_type_scalar(top_type))
4118 descend_into_subtype(&path);
4120 } else if (sub_initializer->kind == INITIALIZER_STRING
4121 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4122 /* we might have to descend into types until we're at a scalar
4125 type_t *orig_top_type = path.top_type;
4126 type_t *top_type = skip_typeref(orig_top_type);
4128 if (is_string_type(top_type))
4130 descend_into_subtype(&path);
4134 ir_initializer_t *sub_irinitializer
4135 = create_ir_initializer(sub_initializer, path.top_type);
4137 size_t path_len = ARR_LEN(path.path);
4138 assert(path_len >= 1);
4139 type_path_entry_t *entry = & path.path[path_len-1];
4140 ir_initializer_t *tinitializer = entry->initializer;
4141 set_initializer_compound_value(tinitializer, entry->index,
4144 advance_current_object(&path);
4147 assert(ARR_LEN(path.path) >= 1);
4148 ir_initializer_t *result = path.path[0].initializer;
4149 DEL_ARR_F(path.path);
4154 static ir_initializer_t *create_ir_initializer_string(
4155 const initializer_string_t *initializer, type_t *type)
4157 type = skip_typeref(type);
4159 size_t string_len = initializer->string.size;
4160 assert(type->kind == TYPE_ARRAY);
4161 assert(type->array.size_constant);
4162 size_t len = type->array.size;
4163 ir_initializer_t *irinitializer = create_initializer_compound(len);
4165 const char *string = initializer->string.begin;
4166 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4168 for (size_t i = 0; i < len; ++i) {
4173 ir_tarval *tv = new_tarval_from_long(c, mode);
4174 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4176 set_initializer_compound_value(irinitializer, i, char_initializer);
4179 return irinitializer;
4182 static ir_initializer_t *create_ir_initializer_wide_string(
4183 const initializer_wide_string_t *initializer, type_t *type)
4185 assert(type->kind == TYPE_ARRAY);
4186 assert(type->array.size_constant);
4187 size_t len = type->array.size;
4188 size_t string_len = wstrlen(&initializer->string);
4189 ir_initializer_t *irinitializer = create_initializer_compound(len);
4191 const char *p = initializer->string.begin;
4192 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4194 for (size_t i = 0; i < len; ++i) {
4196 if (i < string_len) {
4197 c = read_utf8_char(&p);
4199 ir_tarval *tv = new_tarval_from_long(c, mode);
4200 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4202 set_initializer_compound_value(irinitializer, i, char_initializer);
4205 return irinitializer;
4208 static ir_initializer_t *create_ir_initializer(
4209 const initializer_t *initializer, type_t *type)
4211 switch(initializer->kind) {
4212 case INITIALIZER_STRING:
4213 return create_ir_initializer_string(&initializer->string, type);
4215 case INITIALIZER_WIDE_STRING:
4216 return create_ir_initializer_wide_string(&initializer->wide_string,
4219 case INITIALIZER_LIST:
4220 return create_ir_initializer_list(&initializer->list, type);
4222 case INITIALIZER_VALUE:
4223 return create_ir_initializer_value(&initializer->value);
4225 case INITIALIZER_DESIGNATOR:
4226 panic("unexpected designator initializer found");
4228 panic("unknown initializer");
4231 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4232 * are elements [...] the remainder of the aggregate shall be initialized
4233 * implicitly the same as objects that have static storage duration. */
4234 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4237 /* for unions we must NOT do anything for null initializers */
4238 ir_type *owner = get_entity_owner(entity);
4239 if (is_Union_type(owner)) {
4243 ir_type *ent_type = get_entity_type(entity);
4244 /* create sub-initializers for a compound type */
4245 if (is_compound_type(ent_type)) {
4246 unsigned n_members = get_compound_n_members(ent_type);
4247 for (unsigned n = 0; n < n_members; ++n) {
4248 ir_entity *member = get_compound_member(ent_type, n);
4249 ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4251 create_dynamic_null_initializer(member, dbgi, addr);
4255 if (is_Array_type(ent_type)) {
4256 assert(has_array_upper_bound(ent_type, 0));
4257 long n = get_array_upper_bound_int(ent_type, 0);
4258 for (long i = 0; i < n; ++i) {
4259 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4260 ir_node *cnst = new_d_Const(dbgi, index_tv);
4261 ir_node *in[1] = { cnst };
4262 ir_entity *arrent = get_array_element_entity(ent_type);
4263 ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4265 create_dynamic_null_initializer(arrent, dbgi, addr);
4270 ir_mode *value_mode = get_type_mode(ent_type);
4271 ir_node *node = new_Const(get_mode_null(value_mode));
4273 /* is it a bitfield type? */
4274 if (is_Primitive_type(ent_type) &&
4275 get_primitive_base_type(ent_type) != NULL) {
4276 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4280 ir_node *mem = get_store();
4281 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4282 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4286 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4287 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4289 switch(get_initializer_kind(initializer)) {
4290 case IR_INITIALIZER_NULL:
4291 create_dynamic_null_initializer(entity, dbgi, base_addr);
4293 case IR_INITIALIZER_CONST: {
4294 ir_node *node = get_initializer_const_value(initializer);
4295 ir_type *ent_type = get_entity_type(entity);
4297 /* is it a bitfield type? */
4298 if (is_Primitive_type(ent_type) &&
4299 get_primitive_base_type(ent_type) != NULL) {
4300 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4304 assert(get_type_mode(type) == get_irn_mode(node));
4305 ir_node *mem = get_store();
4306 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4307 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4311 case IR_INITIALIZER_TARVAL: {
4312 ir_tarval *tv = get_initializer_tarval_value(initializer);
4313 ir_node *cnst = new_d_Const(dbgi, tv);
4314 ir_type *ent_type = get_entity_type(entity);
4316 /* is it a bitfield type? */
4317 if (is_Primitive_type(ent_type) &&
4318 get_primitive_base_type(ent_type) != NULL) {
4319 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4323 assert(get_type_mode(type) == get_tarval_mode(tv));
4324 ir_node *mem = get_store();
4325 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4326 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4330 case IR_INITIALIZER_COMPOUND: {
4331 assert(is_compound_type(type) || is_Array_type(type));
4333 if (is_Array_type(type)) {
4334 assert(has_array_upper_bound(type, 0));
4335 n_members = get_array_upper_bound_int(type, 0);
4337 n_members = get_compound_n_members(type);
4340 if (get_initializer_compound_n_entries(initializer)
4341 != (unsigned) n_members)
4342 panic("initializer doesn't match compound type");
4344 for (int i = 0; i < n_members; ++i) {
4347 ir_entity *sub_entity;
4348 if (is_Array_type(type)) {
4349 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4350 ir_node *cnst = new_d_Const(dbgi, index_tv);
4351 ir_node *in[1] = { cnst };
4352 irtype = get_array_element_type(type);
4353 sub_entity = get_array_element_entity(type);
4354 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4357 sub_entity = get_compound_member(type, i);
4358 irtype = get_entity_type(sub_entity);
4359 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4363 ir_initializer_t *sub_init
4364 = get_initializer_compound_value(initializer, i);
4366 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4373 panic("invalid IR_INITIALIZER found");
4376 static void create_dynamic_initializer(ir_initializer_t *initializer,
4377 dbg_info *dbgi, ir_entity *entity)
4379 ir_node *frame = get_irg_frame(current_ir_graph);
4380 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4381 ir_type *type = get_entity_type(entity);
4383 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4386 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4387 ir_entity *entity, type_t *type)
4389 ir_node *memory = get_store();
4390 ir_node *nomem = new_NoMem();
4391 ir_node *frame = get_irg_frame(current_ir_graph);
4392 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4394 if (initializer->kind == INITIALIZER_VALUE) {
4395 initializer_value_t *initializer_value = &initializer->value;
4397 ir_node *value = expression_to_firm(initializer_value->value);
4398 type = skip_typeref(type);
4399 assign_value(dbgi, addr, type, value);
4403 if (is_constant_initializer(initializer) == EXPR_CLASS_VARIABLE) {
4404 bool old_initializer_use_bitfield_basetype
4405 = initializer_use_bitfield_basetype;
4406 initializer_use_bitfield_basetype = true;
4407 ir_initializer_t *irinitializer
4408 = create_ir_initializer(initializer, type);
4409 initializer_use_bitfield_basetype
4410 = old_initializer_use_bitfield_basetype;
4412 create_dynamic_initializer(irinitializer, dbgi, entity);
4416 /* create the ir_initializer */
4417 ir_graph *const old_current_ir_graph = current_ir_graph;
4418 current_ir_graph = get_const_code_irg();
4420 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4422 assert(current_ir_graph == get_const_code_irg());
4423 current_ir_graph = old_current_ir_graph;
4425 /* create a "template" entity which is copied to the entity on the stack */
4426 ident *const id = id_unique("initializer.%u");
4427 ir_type *const irtype = get_ir_type(type);
4428 ir_type *const global_type = get_glob_type();
4429 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4430 set_entity_ld_ident(init_entity, id);
4432 set_entity_visibility(init_entity, ir_visibility_private);
4433 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4435 set_entity_initializer(init_entity, irinitializer);
4437 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4438 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4440 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4441 set_store(copyb_mem);
4444 static void create_initializer_local_variable_entity(entity_t *entity)
4446 assert(entity->kind == ENTITY_VARIABLE);
4447 initializer_t *initializer = entity->variable.initializer;
4448 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4449 ir_entity *irentity = entity->variable.v.entity;
4450 type_t *type = entity->declaration.type;
4452 create_local_initializer(initializer, dbgi, irentity, type);
4455 static void create_variable_initializer(entity_t *entity)
4457 assert(entity->kind == ENTITY_VARIABLE);
4458 initializer_t *initializer = entity->variable.initializer;
4459 if (initializer == NULL)
4462 declaration_kind_t declaration_kind
4463 = (declaration_kind_t) entity->declaration.kind;
4464 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4465 create_initializer_local_variable_entity(entity);
4469 type_t *type = entity->declaration.type;
4470 type_qualifiers_t tq = get_type_qualifier(type, true);
4472 if (initializer->kind == INITIALIZER_VALUE) {
4473 initializer_value_t *initializer_value = &initializer->value;
4474 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4476 ir_node *value = expression_to_firm(initializer_value->value);
4478 type_t *type = initializer_value->value->base.type;
4479 ir_mode *mode = get_ir_mode_storage(type);
4480 value = create_conv(dbgi, value, mode);
4481 value = do_strict_conv(dbgi, value);
4483 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4484 set_value(entity->variable.v.value_number, value);
4486 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4488 ir_entity *irentity = entity->variable.v.entity;
4490 if (tq & TYPE_QUALIFIER_CONST
4491 && get_entity_owner(irentity) != get_tls_type()) {
4492 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4494 set_atomic_ent_value(irentity, value);
4497 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4498 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4500 ir_entity *irentity = entity->variable.v.entity;
4501 ir_initializer_t *irinitializer
4502 = create_ir_initializer(initializer, type);
4504 if (tq & TYPE_QUALIFIER_CONST) {
4505 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4507 set_entity_initializer(irentity, irinitializer);
4511 static void create_variable_length_array(entity_t *entity)
4513 assert(entity->kind == ENTITY_VARIABLE);
4514 assert(entity->variable.initializer == NULL);
4516 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4517 entity->variable.v.vla_base = NULL;
4519 /* TODO: record VLA somewhere so we create the free node when we leave
4523 static void allocate_variable_length_array(entity_t *entity)
4525 assert(entity->kind == ENTITY_VARIABLE);
4526 assert(entity->variable.initializer == NULL);
4527 assert(get_cur_block() != NULL);
4529 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4530 type_t *type = entity->declaration.type;
4531 ir_type *el_type = get_ir_type(type->array.element_type);
4533 /* make sure size_node is calculated */
4534 get_type_size_node(type);
4535 ir_node *elems = type->array.size_node;
4536 ir_node *mem = get_store();
4537 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4539 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4540 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4543 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4544 entity->variable.v.vla_base = addr;
4548 * Creates a Firm local variable from a declaration.
4550 static void create_local_variable(entity_t *entity)
4552 assert(entity->kind == ENTITY_VARIABLE);
4553 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4555 bool needs_entity = entity->variable.address_taken;
4556 type_t *type = skip_typeref(entity->declaration.type);
4558 /* is it a variable length array? */
4559 if (is_type_array(type) && !type->array.size_constant) {
4560 create_variable_length_array(entity);
4562 } else if (is_type_array(type) || is_type_compound(type)) {
4563 needs_entity = true;
4564 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4565 needs_entity = true;
4569 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4570 create_variable_entity(entity,
4571 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4574 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4575 entity->variable.v.value_number = next_value_number_function;
4576 set_irg_loc_description(current_ir_graph, next_value_number_function,
4578 ++next_value_number_function;
4582 static void create_local_static_variable(entity_t *entity)
4584 assert(entity->kind == ENTITY_VARIABLE);
4585 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4587 type_t *type = skip_typeref(entity->declaration.type);
4588 ir_type *const var_type = entity->variable.thread_local ?
4589 get_tls_type() : get_glob_type();
4590 ir_type *const irtype = get_ir_type(type);
4591 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4593 size_t l = strlen(entity->base.symbol->string);
4594 char buf[l + sizeof(".%u")];
4595 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4596 ident *const id = id_unique(buf);
4597 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4599 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4600 set_entity_volatility(irentity, volatility_is_volatile);
4603 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4604 entity->variable.v.entity = irentity;
4606 set_entity_ld_ident(irentity, id);
4607 set_entity_visibility(irentity, ir_visibility_local);
4609 ir_graph *const old_current_ir_graph = current_ir_graph;
4610 current_ir_graph = get_const_code_irg();
4612 create_variable_initializer(entity);
4614 assert(current_ir_graph == get_const_code_irg());
4615 current_ir_graph = old_current_ir_graph;
4620 static void return_statement_to_firm(return_statement_t *statement)
4622 if (get_cur_block() == NULL)
4625 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4626 type_t *type = current_function_entity->declaration.type;
4627 ir_type *func_irtype = get_ir_type(type);
4632 if (get_method_n_ress(func_irtype) > 0) {
4633 ir_type *res_type = get_method_res_type(func_irtype, 0);
4635 if (statement->value != NULL) {
4636 ir_node *node = expression_to_firm(statement->value);
4637 if (!is_compound_type(res_type)) {
4638 type_t *type = statement->value->base.type;
4639 ir_mode *mode = get_ir_mode_storage(type);
4640 node = create_conv(dbgi, node, mode);
4641 node = do_strict_conv(dbgi, node);
4646 if (is_compound_type(res_type)) {
4649 mode = get_type_mode(res_type);
4651 in[0] = new_Unknown(mode);
4655 /* build return_value for its side effects */
4656 if (statement->value != NULL) {
4657 expression_to_firm(statement->value);
4662 ir_node *store = get_store();
4663 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4665 ir_node *end_block = get_irg_end_block(current_ir_graph);
4666 add_immBlock_pred(end_block, ret);
4668 set_cur_block(NULL);
4671 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4673 if (get_cur_block() == NULL)
4676 return expression_to_firm(statement->expression);
4679 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4681 entity_t *entity = compound->scope.entities;
4682 for ( ; entity != NULL; entity = entity->base.next) {
4683 if (!is_declaration(entity))
4686 create_local_declaration(entity);
4689 ir_node *result = NULL;
4690 statement_t *statement = compound->statements;
4691 for ( ; statement != NULL; statement = statement->base.next) {
4692 if (statement->base.next == NULL
4693 && statement->kind == STATEMENT_EXPRESSION) {
4694 result = expression_statement_to_firm(
4695 &statement->expression);
4698 statement_to_firm(statement);
4704 static void create_global_variable(entity_t *entity)
4706 ir_linkage linkage = IR_LINKAGE_DEFAULT;
4707 ir_visibility visibility = ir_visibility_default;
4708 ir_entity *irentity;
4709 assert(entity->kind == ENTITY_VARIABLE);
4711 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4712 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4713 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4714 case STORAGE_CLASS_NONE:
4715 visibility = ir_visibility_default;
4716 /* uninitialized globals get merged in C */
4717 if (entity->variable.initializer == NULL)
4718 linkage |= IR_LINKAGE_MERGE;
4720 case STORAGE_CLASS_TYPEDEF:
4721 case STORAGE_CLASS_AUTO:
4722 case STORAGE_CLASS_REGISTER:
4723 panic("invalid storage class for global var");
4726 ir_type *var_type = get_glob_type();
4727 if (entity->variable.thread_local) {
4728 var_type = get_tls_type();
4729 /* LINKAGE_MERGE not supported by current linkers */
4730 linkage &= ~IR_LINKAGE_MERGE;
4732 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4733 irentity = entity->variable.v.entity;
4734 add_entity_linkage(irentity, linkage);
4735 set_entity_visibility(irentity, visibility);
4738 static void create_local_declaration(entity_t *entity)
4740 assert(is_declaration(entity));
4742 /* construct type */
4743 (void) get_ir_type(entity->declaration.type);
4744 if (entity->base.symbol == NULL) {
4748 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4749 case STORAGE_CLASS_STATIC:
4750 if (entity->kind == ENTITY_FUNCTION) {
4751 (void)get_function_entity(entity, NULL);
4753 create_local_static_variable(entity);
4756 case STORAGE_CLASS_EXTERN:
4757 if (entity->kind == ENTITY_FUNCTION) {
4758 assert(entity->function.statement == NULL);
4759 (void)get_function_entity(entity, NULL);
4761 create_global_variable(entity);
4762 create_variable_initializer(entity);
4765 case STORAGE_CLASS_NONE:
4766 case STORAGE_CLASS_AUTO:
4767 case STORAGE_CLASS_REGISTER:
4768 if (entity->kind == ENTITY_FUNCTION) {
4769 if (entity->function.statement != NULL) {
4770 ir_type *owner = get_irg_frame_type(current_ir_graph);
4771 (void)get_function_entity(entity, owner);
4772 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4773 enqueue_inner_function(entity);
4775 (void)get_function_entity(entity, NULL);
4778 create_local_variable(entity);
4781 case STORAGE_CLASS_TYPEDEF:
4784 panic("invalid storage class found");
4787 static void initialize_local_declaration(entity_t *entity)
4789 if (entity->base.symbol == NULL)
4792 // no need to emit code in dead blocks
4793 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4794 && get_cur_block() == NULL)
4797 switch ((declaration_kind_t) entity->declaration.kind) {
4798 case DECLARATION_KIND_LOCAL_VARIABLE:
4799 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4800 create_variable_initializer(entity);
4803 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4804 allocate_variable_length_array(entity);
4807 case DECLARATION_KIND_COMPOUND_MEMBER:
4808 case DECLARATION_KIND_GLOBAL_VARIABLE:
4809 case DECLARATION_KIND_FUNCTION:
4810 case DECLARATION_KIND_INNER_FUNCTION:
4813 case DECLARATION_KIND_PARAMETER:
4814 case DECLARATION_KIND_PARAMETER_ENTITY:
4815 panic("can't initialize parameters");
4817 case DECLARATION_KIND_UNKNOWN:
4818 panic("can't initialize unknown declaration");
4820 panic("invalid declaration kind");
4823 static void declaration_statement_to_firm(declaration_statement_t *statement)
4825 entity_t *entity = statement->declarations_begin;
4829 entity_t *const last = statement->declarations_end;
4830 for ( ;; entity = entity->base.next) {
4831 if (is_declaration(entity)) {
4832 initialize_local_declaration(entity);
4833 } else if (entity->kind == ENTITY_TYPEDEF) {
4834 /* ยง6.7.7:3 Any array size expressions associated with variable length
4835 * array declarators are evaluated each time the declaration of the
4836 * typedef name is reached in the order of execution. */
4837 type_t *const type = skip_typeref(entity->typedefe.type);
4838 if (is_type_array(type) && type->array.is_vla)
4839 get_vla_size(&type->array);
4846 static void if_statement_to_firm(if_statement_t *statement)
4848 /* Create the condition. */
4849 ir_node *true_block = NULL;
4850 ir_node *false_block = NULL;
4851 if (get_cur_block() != NULL) {
4852 true_block = new_immBlock();
4853 false_block = new_immBlock();
4854 create_condition_evaluation(statement->condition, true_block, false_block);
4855 mature_immBlock(true_block);
4858 /* Create the false statement.
4859 * Hadle false before true, so if no false statement is present, then the
4860 * empty false block is reused as fallthrough block. */
4861 ir_node *fallthrough_block = NULL;
4862 if (statement->false_statement != NULL) {
4863 if (false_block != NULL) {
4864 mature_immBlock(false_block);
4866 set_cur_block(false_block);
4867 statement_to_firm(statement->false_statement);
4868 if (get_cur_block() != NULL) {
4869 fallthrough_block = new_immBlock();
4870 add_immBlock_pred(fallthrough_block, new_Jmp());
4873 fallthrough_block = false_block;
4876 /* Create the true statement. */
4877 set_cur_block(true_block);
4878 statement_to_firm(statement->true_statement);
4879 if (get_cur_block() != NULL) {
4880 if (fallthrough_block == NULL) {
4881 fallthrough_block = new_immBlock();
4883 add_immBlock_pred(fallthrough_block, new_Jmp());
4886 /* Handle the block after the if-statement. */
4887 if (fallthrough_block != NULL) {
4888 mature_immBlock(fallthrough_block);
4890 set_cur_block(fallthrough_block);
4893 /* Create a jump node which jumps into target_block, if the current block is
4895 static void jump_if_reachable(ir_node *const target_block)
4897 if (get_cur_block() != NULL) {
4898 add_immBlock_pred(target_block, new_Jmp());
4902 static void while_statement_to_firm(while_statement_t *statement)
4904 /* Create the header block */
4905 ir_node *const header_block = new_immBlock();
4906 jump_if_reachable(header_block);
4908 /* Create the condition. */
4909 ir_node * body_block;
4910 ir_node * false_block;
4911 expression_t *const cond = statement->condition;
4912 if (is_constant_expression(cond) == EXPR_CLASS_CONSTANT &&
4913 fold_constant_to_bool(cond)) {
4914 /* Shortcut for while (true). */
4915 body_block = header_block;
4918 keep_alive(header_block);
4919 keep_all_memory(header_block);
4921 body_block = new_immBlock();
4922 false_block = new_immBlock();
4924 set_cur_block(header_block);
4925 create_condition_evaluation(cond, body_block, false_block);
4926 mature_immBlock(body_block);
4929 ir_node *const old_continue_label = continue_label;
4930 ir_node *const old_break_label = break_label;
4931 continue_label = header_block;
4932 break_label = false_block;
4934 /* Create the loop body. */
4935 set_cur_block(body_block);
4936 statement_to_firm(statement->body);
4937 jump_if_reachable(header_block);
4939 mature_immBlock(header_block);
4940 assert(false_block == NULL || false_block == break_label);
4941 false_block = break_label;
4942 if (false_block != NULL) {
4943 mature_immBlock(false_block);
4945 set_cur_block(false_block);
4947 assert(continue_label == header_block);
4948 continue_label = old_continue_label;
4949 break_label = old_break_label;
4952 static ir_node *get_break_label(void)
4954 if (break_label == NULL) {
4955 break_label = new_immBlock();
4960 static void do_while_statement_to_firm(do_while_statement_t *statement)
4962 /* create the header block */
4963 ir_node *header_block = new_immBlock();
4966 ir_node *body_block = new_immBlock();
4967 jump_if_reachable(body_block);
4969 ir_node *old_continue_label = continue_label;
4970 ir_node *old_break_label = break_label;
4971 continue_label = header_block;
4974 set_cur_block(body_block);
4975 statement_to_firm(statement->body);
4976 ir_node *const false_block = get_break_label();
4978 assert(continue_label == header_block);
4979 continue_label = old_continue_label;
4980 break_label = old_break_label;
4982 jump_if_reachable(header_block);
4984 /* create the condition */
4985 mature_immBlock(header_block);
4986 set_cur_block(header_block);
4988 create_condition_evaluation(statement->condition, body_block, false_block);
4989 mature_immBlock(body_block);
4990 mature_immBlock(false_block);
4992 set_cur_block(false_block);
4995 static void for_statement_to_firm(for_statement_t *statement)
4997 /* create declarations */
4998 entity_t *entity = statement->scope.entities;
4999 for ( ; entity != NULL; entity = entity->base.next) {
5000 if (!is_declaration(entity))
5003 create_local_declaration(entity);
5006 if (get_cur_block() != NULL) {
5007 entity = statement->scope.entities;
5008 for ( ; entity != NULL; entity = entity->base.next) {
5009 if (!is_declaration(entity))
5012 initialize_local_declaration(entity);
5015 if (statement->initialisation != NULL) {
5016 expression_to_firm(statement->initialisation);
5020 /* Create the header block */
5021 ir_node *const header_block = new_immBlock();
5022 jump_if_reachable(header_block);
5024 /* Create the condition. */
5025 ir_node *body_block;
5026 ir_node *false_block;
5027 if (statement->condition != NULL) {
5028 body_block = new_immBlock();
5029 false_block = new_immBlock();
5031 set_cur_block(header_block);
5032 create_condition_evaluation(statement->condition, body_block, false_block);
5033 mature_immBlock(body_block);
5036 body_block = header_block;
5039 keep_alive(header_block);
5040 keep_all_memory(header_block);
5043 /* Create the step block, if necessary. */
5044 ir_node * step_block = header_block;
5045 expression_t *const step = statement->step;
5047 step_block = new_immBlock();
5050 ir_node *const old_continue_label = continue_label;
5051 ir_node *const old_break_label = break_label;
5052 continue_label = step_block;
5053 break_label = false_block;
5055 /* Create the loop body. */
5056 set_cur_block(body_block);
5057 statement_to_firm(statement->body);
5058 jump_if_reachable(step_block);
5060 /* Create the step code. */
5062 mature_immBlock(step_block);
5063 set_cur_block(step_block);
5064 expression_to_firm(step);
5065 jump_if_reachable(header_block);
5068 mature_immBlock(header_block);
5069 assert(false_block == NULL || false_block == break_label);
5070 false_block = break_label;
5071 if (false_block != NULL) {
5072 mature_immBlock(false_block);
5074 set_cur_block(false_block);
5076 assert(continue_label == step_block);
5077 continue_label = old_continue_label;
5078 break_label = old_break_label;
5081 static void create_jump_statement(const statement_t *statement,
5082 ir_node *target_block)
5084 if (get_cur_block() == NULL)
5087 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5088 ir_node *jump = new_d_Jmp(dbgi);
5089 add_immBlock_pred(target_block, jump);
5091 set_cur_block(NULL);
5094 static void switch_statement_to_firm(switch_statement_t *statement)
5096 ir_node *first_block = NULL;
5097 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5098 ir_node *cond = NULL;
5100 if (get_cur_block() != NULL) {
5101 ir_node *expression = expression_to_firm(statement->expression);
5102 cond = new_d_Cond(dbgi, expression);
5103 first_block = get_cur_block();
5106 set_cur_block(NULL);
5108 ir_node *const old_switch_cond = current_switch_cond;
5109 ir_node *const old_break_label = break_label;
5110 const bool old_saw_default_label = saw_default_label;
5111 saw_default_label = false;
5112 current_switch_cond = cond;
5114 switch_statement_t *const old_switch = current_switch;
5115 current_switch = statement;
5117 /* determine a free number for the default label */
5118 unsigned long num_cases = 0;
5119 long default_proj_nr = 0;
5120 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5121 if (l->expression == NULL) {
5125 if (l->last_case >= l->first_case)
5126 num_cases += l->last_case - l->first_case + 1;
5127 if (l->last_case > default_proj_nr)
5128 default_proj_nr = l->last_case;
5131 if (default_proj_nr == INT_MAX) {
5132 /* Bad: an overflow will occur, we cannot be sure that the
5133 * maximum + 1 is a free number. Scan the values a second
5134 * time to find a free number.
5136 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5138 memset(bits, 0, (num_cases + 7) >> 3);
5139 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5140 if (l->expression == NULL) {
5144 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5145 if (start < num_cases && l->last_case >= 0) {
5146 unsigned long end = (unsigned long)l->last_case < num_cases ?
5147 (unsigned long)l->last_case : num_cases - 1;
5148 for (unsigned long cns = start; cns <= end; ++cns) {
5149 bits[cns >> 3] |= (1 << (cns & 7));
5153 /* We look at the first num_cases constants:
5154 * Either they are dense, so we took the last (num_cases)
5155 * one, or they are not dense, so we will find one free
5159 for (i = 0; i < num_cases; ++i)
5160 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5164 default_proj_nr = i;
5168 statement->default_proj_nr = default_proj_nr;
5169 /* safety check: cond might already be folded to a Bad */
5170 if (cond != NULL && is_Cond(cond)) {
5171 set_Cond_default_proj(cond, default_proj_nr);
5174 statement_to_firm(statement->body);
5176 jump_if_reachable(get_break_label());
5178 if (!saw_default_label && first_block != NULL) {
5179 set_cur_block(first_block);
5180 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5181 add_immBlock_pred(get_break_label(), proj);
5184 if (break_label != NULL) {
5185 mature_immBlock(break_label);
5187 set_cur_block(break_label);
5189 assert(current_switch_cond == cond);
5190 current_switch = old_switch;
5191 current_switch_cond = old_switch_cond;
5192 break_label = old_break_label;
5193 saw_default_label = old_saw_default_label;
5196 static void case_label_to_firm(const case_label_statement_t *statement)
5198 if (statement->is_empty_range)
5201 ir_node *block = new_immBlock();
5202 /* Fallthrough from previous case */
5203 jump_if_reachable(block);
5205 if (current_switch_cond != NULL) {
5206 set_cur_block(get_nodes_block(current_switch_cond));
5207 dbg_info *const dbgi = get_dbg_info(&statement->base.source_position);
5208 if (statement->expression != NULL) {
5209 long pn = statement->first_case;
5210 long end_pn = statement->last_case;
5211 assert(pn <= end_pn);
5212 /* create jumps for all cases in the given range */
5214 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5215 add_immBlock_pred(block, proj);
5216 } while (pn++ < end_pn);
5218 saw_default_label = true;
5219 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5220 current_switch->default_proj_nr);
5221 add_immBlock_pred(block, proj);
5225 mature_immBlock(block);
5226 set_cur_block(block);
5228 statement_to_firm(statement->statement);
5231 static void label_to_firm(const label_statement_t *statement)
5233 ir_node *block = get_label_block(statement->label);
5234 jump_if_reachable(block);
5236 set_cur_block(block);
5238 keep_all_memory(block);
5240 statement_to_firm(statement->statement);
5243 static void goto_to_firm(const goto_statement_t *statement)
5245 if (get_cur_block() == NULL)
5248 if (statement->expression) {
5249 ir_node *irn = expression_to_firm(statement->expression);
5250 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5251 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5253 set_irn_link(ijmp, ijmp_list);
5256 ir_node *block = get_label_block(statement->label);
5257 ir_node *jmp = new_Jmp();
5258 add_immBlock_pred(block, jmp);
5260 set_cur_block(NULL);
5263 static void asm_statement_to_firm(const asm_statement_t *statement)
5265 bool needs_memory = false;
5267 if (statement->is_volatile) {
5268 needs_memory = true;
5271 size_t n_clobbers = 0;
5272 asm_clobber_t *clobber = statement->clobbers;
5273 for ( ; clobber != NULL; clobber = clobber->next) {
5274 const char *clobber_str = clobber->clobber.begin;
5276 if (!be_is_valid_clobber(clobber_str)) {
5277 errorf(&statement->base.source_position,
5278 "invalid clobber '%s' specified", clobber->clobber);
5282 if (strcmp(clobber_str, "memory") == 0) {
5283 needs_memory = true;
5287 ident *id = new_id_from_str(clobber_str);
5288 obstack_ptr_grow(&asm_obst, id);
5291 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5292 ident **clobbers = NULL;
5293 if (n_clobbers > 0) {
5294 clobbers = obstack_finish(&asm_obst);
5297 size_t n_inputs = 0;
5298 asm_argument_t *argument = statement->inputs;
5299 for ( ; argument != NULL; argument = argument->next)
5301 size_t n_outputs = 0;
5302 argument = statement->outputs;
5303 for ( ; argument != NULL; argument = argument->next)
5306 unsigned next_pos = 0;
5308 ir_node *ins[n_inputs + n_outputs + 1];
5311 ir_asm_constraint tmp_in_constraints[n_outputs];
5313 const expression_t *out_exprs[n_outputs];
5314 ir_node *out_addrs[n_outputs];
5315 size_t out_size = 0;
5317 argument = statement->outputs;
5318 for ( ; argument != NULL; argument = argument->next) {
5319 const char *constraints = argument->constraints.begin;
5320 asm_constraint_flags_t asm_flags
5321 = be_parse_asm_constraints(constraints);
5323 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5324 warningf(&statement->base.source_position,
5325 "some constraints in '%s' are not supported", constraints);
5327 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5328 errorf(&statement->base.source_position,
5329 "some constraints in '%s' are invalid", constraints);
5332 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5333 errorf(&statement->base.source_position,
5334 "no write flag specified for output constraints '%s'",
5339 unsigned pos = next_pos++;
5340 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5341 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5342 expression_t *expr = argument->expression;
5343 ir_node *addr = expression_to_addr(expr);
5344 /* in+output, construct an artifical same_as constraint on the
5346 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5348 ir_node *value = get_value_from_lvalue(expr, addr);
5350 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5352 ir_asm_constraint constraint;
5353 constraint.pos = pos;
5354 constraint.constraint = new_id_from_str(buf);
5355 constraint.mode = get_ir_mode_storage(expr->base.type);
5356 tmp_in_constraints[in_size] = constraint;
5357 ins[in_size] = value;
5362 out_exprs[out_size] = expr;
5363 out_addrs[out_size] = addr;
5365 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5366 /* pure memory ops need no input (but we have to make sure we
5367 * attach to the memory) */
5368 assert(! (asm_flags &
5369 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5370 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5371 needs_memory = true;
5373 /* we need to attach the address to the inputs */
5374 expression_t *expr = argument->expression;
5376 ir_asm_constraint constraint;
5377 constraint.pos = pos;
5378 constraint.constraint = new_id_from_str(constraints);
5379 constraint.mode = NULL;
5380 tmp_in_constraints[in_size] = constraint;
5382 ins[in_size] = expression_to_addr(expr);
5386 errorf(&statement->base.source_position,
5387 "only modifiers but no place set in constraints '%s'",
5392 ir_asm_constraint constraint;
5393 constraint.pos = pos;
5394 constraint.constraint = new_id_from_str(constraints);
5395 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5397 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5399 assert(obstack_object_size(&asm_obst)
5400 == out_size * sizeof(ir_asm_constraint));
5401 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5404 obstack_grow(&asm_obst, tmp_in_constraints,
5405 in_size * sizeof(tmp_in_constraints[0]));
5406 /* find and count input and output arguments */
5407 argument = statement->inputs;
5408 for ( ; argument != NULL; argument = argument->next) {
5409 const char *constraints = argument->constraints.begin;
5410 asm_constraint_flags_t asm_flags
5411 = be_parse_asm_constraints(constraints);
5413 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5414 errorf(&statement->base.source_position,
5415 "some constraints in '%s' are not supported", constraints);
5418 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5419 errorf(&statement->base.source_position,
5420 "some constraints in '%s' are invalid", constraints);
5423 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5424 errorf(&statement->base.source_position,
5425 "write flag specified for input constraints '%s'",
5431 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5432 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5433 /* we can treat this as "normal" input */
5434 input = expression_to_firm(argument->expression);
5435 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5436 /* pure memory ops need no input (but we have to make sure we
5437 * attach to the memory) */
5438 assert(! (asm_flags &
5439 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5440 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5441 needs_memory = true;
5442 input = expression_to_addr(argument->expression);
5444 errorf(&statement->base.source_position,
5445 "only modifiers but no place set in constraints '%s'",
5450 ir_asm_constraint constraint;
5451 constraint.pos = next_pos++;
5452 constraint.constraint = new_id_from_str(constraints);
5453 constraint.mode = get_irn_mode(input);
5455 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5456 ins[in_size++] = input;
5460 ir_asm_constraint constraint;
5461 constraint.pos = next_pos++;
5462 constraint.constraint = new_id_from_str("");
5463 constraint.mode = mode_M;
5465 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5466 ins[in_size++] = get_store();
5469 assert(obstack_object_size(&asm_obst)
5470 == in_size * sizeof(ir_asm_constraint));
5471 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5473 /* create asm node */
5474 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5476 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5478 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5479 out_size, output_constraints,
5480 n_clobbers, clobbers, asm_text);
5482 if (statement->is_volatile) {
5483 set_irn_pinned(node, op_pin_state_pinned);
5485 set_irn_pinned(node, op_pin_state_floats);
5488 /* create output projs & connect them */
5490 ir_node *projm = new_Proj(node, mode_M, out_size);
5495 for (i = 0; i < out_size; ++i) {
5496 const expression_t *out_expr = out_exprs[i];
5498 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5499 ir_node *proj = new_Proj(node, mode, pn);
5500 ir_node *addr = out_addrs[i];
5502 set_value_for_expression_addr(out_expr, proj, addr);
5506 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5508 statement_to_firm(statement->try_statement);
5509 warningf(&statement->base.source_position, "structured exception handling ignored");
5512 static void leave_statement_to_firm(leave_statement_t *statement)
5514 errorf(&statement->base.source_position, "__leave not supported yet");
5518 * Transform a statement.
5520 static void statement_to_firm(statement_t *statement)
5523 assert(!statement->base.transformed);
5524 statement->base.transformed = true;
5527 switch (statement->kind) {
5528 case STATEMENT_INVALID:
5529 panic("invalid statement found");
5530 case STATEMENT_EMPTY:
5533 case STATEMENT_COMPOUND:
5534 compound_statement_to_firm(&statement->compound);
5536 case STATEMENT_RETURN:
5537 return_statement_to_firm(&statement->returns);
5539 case STATEMENT_EXPRESSION:
5540 expression_statement_to_firm(&statement->expression);
5543 if_statement_to_firm(&statement->ifs);
5545 case STATEMENT_WHILE:
5546 while_statement_to_firm(&statement->whiles);
5548 case STATEMENT_DO_WHILE:
5549 do_while_statement_to_firm(&statement->do_while);
5551 case STATEMENT_DECLARATION:
5552 declaration_statement_to_firm(&statement->declaration);
5554 case STATEMENT_BREAK:
5555 create_jump_statement(statement, get_break_label());
5557 case STATEMENT_CONTINUE:
5558 create_jump_statement(statement, continue_label);
5560 case STATEMENT_SWITCH:
5561 switch_statement_to_firm(&statement->switchs);
5563 case STATEMENT_CASE_LABEL:
5564 case_label_to_firm(&statement->case_label);
5567 for_statement_to_firm(&statement->fors);
5569 case STATEMENT_LABEL:
5570 label_to_firm(&statement->label);
5572 case STATEMENT_GOTO:
5573 goto_to_firm(&statement->gotos);
5576 asm_statement_to_firm(&statement->asms);
5578 case STATEMENT_MS_TRY:
5579 ms_try_statement_to_firm(&statement->ms_try);
5581 case STATEMENT_LEAVE:
5582 leave_statement_to_firm(&statement->leave);
5585 panic("statement not implemented");
5588 static int count_local_variables(const entity_t *entity,
5589 const entity_t *const last)
5592 entity_t const *const end = last != NULL ? last->base.next : NULL;
5593 for (; entity != end; entity = entity->base.next) {
5597 if (entity->kind == ENTITY_VARIABLE) {
5598 type = skip_typeref(entity->declaration.type);
5599 address_taken = entity->variable.address_taken;
5600 } else if (entity->kind == ENTITY_PARAMETER) {
5601 type = skip_typeref(entity->declaration.type);
5602 address_taken = entity->parameter.address_taken;
5607 if (!address_taken && is_type_scalar(type))
5613 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5615 int *const count = env;
5617 switch (stmt->kind) {
5618 case STATEMENT_DECLARATION: {
5619 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5620 *count += count_local_variables(decl_stmt->declarations_begin,
5621 decl_stmt->declarations_end);
5626 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5635 * Return the number of local (alias free) variables used by a function.
5637 static int get_function_n_local_vars(entity_t *entity)
5639 const function_t *function = &entity->function;
5642 /* count parameters */
5643 count += count_local_variables(function->parameters.entities, NULL);
5645 /* count local variables declared in body */
5646 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5651 * Build Firm code for the parameters of a function.
5653 static void initialize_function_parameters(entity_t *entity)
5655 assert(entity->kind == ENTITY_FUNCTION);
5656 ir_graph *irg = current_ir_graph;
5657 ir_node *args = get_irg_args(irg);
5658 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5659 int first_param_nr = 0;
5661 if (entity->function.need_closure) {
5662 /* add an extra parameter for the static link */
5663 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5668 entity_t *parameter = entity->function.parameters.entities;
5669 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5670 if (parameter->kind != ENTITY_PARAMETER)
5673 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5674 type_t *type = skip_typeref(parameter->declaration.type);
5676 bool needs_entity = parameter->parameter.address_taken;
5677 assert(!is_type_array(type));
5678 if (is_type_compound(type)) {
5679 needs_entity = true;
5683 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5684 ident *id = new_id_from_str(parameter->base.symbol->string);
5685 set_entity_ident(entity, id);
5687 parameter->declaration.kind
5688 = DECLARATION_KIND_PARAMETER_ENTITY;
5689 parameter->parameter.v.entity = entity;
5693 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5694 ir_mode *param_mode = get_type_mode(param_irtype);
5696 long pn = n + first_param_nr;
5697 ir_node *value = new_r_Proj(args, param_mode, pn);
5699 ir_mode *mode = get_ir_mode_storage(type);
5700 value = create_conv(NULL, value, mode);
5701 value = do_strict_conv(NULL, value);
5703 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5704 parameter->parameter.v.value_number = next_value_number_function;
5705 set_irg_loc_description(current_ir_graph, next_value_number_function,
5707 ++next_value_number_function;
5709 set_value(parameter->parameter.v.value_number, value);
5714 * Handle additional decl modifiers for IR-graphs
5716 * @param irg the IR-graph
5717 * @param dec_modifiers additional modifiers
5719 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5720 decl_modifiers_t decl_modifiers)
5722 if (decl_modifiers & DM_RETURNS_TWICE) {
5723 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5724 add_irg_additional_properties(irg, mtp_property_returns_twice);
5726 if (decl_modifiers & DM_NORETURN) {
5727 /* TRUE if the declaration includes the Microsoft
5728 __declspec(noreturn) specifier. */
5729 add_irg_additional_properties(irg, mtp_property_noreturn);
5731 if (decl_modifiers & DM_NOTHROW) {
5732 /* TRUE if the declaration includes the Microsoft
5733 __declspec(nothrow) specifier. */
5734 add_irg_additional_properties(irg, mtp_property_nothrow);
5736 if (decl_modifiers & DM_NAKED) {
5737 /* TRUE if the declaration includes the Microsoft
5738 __declspec(naked) specifier. */
5739 add_irg_additional_properties(irg, mtp_property_naked);
5741 if (decl_modifiers & DM_FORCEINLINE) {
5742 /* TRUE if the declaration includes the
5743 Microsoft __forceinline specifier. */
5744 set_irg_inline_property(irg, irg_inline_forced);
5746 if (decl_modifiers & DM_NOINLINE) {
5747 /* TRUE if the declaration includes the Microsoft
5748 __declspec(noinline) specifier. */
5749 set_irg_inline_property(irg, irg_inline_forbidden);
5753 static void add_function_pointer(ir_type *segment, ir_entity *method,
5754 const char *unique_template)
5756 ir_type *method_type = get_entity_type(method);
5757 ir_type *ptr_type = new_type_pointer(method_type);
5759 /* these entities don't really have a name but firm only allows
5761 * Note that we mustn't give these entities a name since for example
5762 * Mach-O doesn't allow them. */
5763 ident *ide = id_unique(unique_template);
5764 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5765 ir_graph *irg = get_const_code_irg();
5766 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5769 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5770 set_entity_compiler_generated(ptr, 1);
5771 set_entity_visibility(ptr, ir_visibility_private);
5772 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5773 set_atomic_ent_value(ptr, val);
5777 * Generate possible IJmp branches to a given label block.
5779 static void gen_ijmp_branches(ir_node *block)
5782 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5783 add_immBlock_pred(block, ijmp);
5788 * Create code for a function and all inner functions.
5790 * @param entity the function entity
5792 static void create_function(entity_t *entity)
5794 assert(entity->kind == ENTITY_FUNCTION);
5795 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5797 if (entity->function.statement == NULL)
5800 if (is_main(entity) && enable_main_collect2_hack) {
5801 prepare_main_collect2(entity);
5804 inner_functions = NULL;
5805 current_trampolines = NULL;
5807 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5808 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5809 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5811 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5812 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5813 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5816 current_function_entity = entity;
5817 current_function_name = NULL;
5818 current_funcsig = NULL;
5820 assert(all_labels == NULL);
5821 all_labels = NEW_ARR_F(label_t *, 0);
5824 int n_local_vars = get_function_n_local_vars(entity);
5825 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5826 current_ir_graph = irg;
5828 ir_graph *old_current_function = current_function;
5829 current_function = irg;
5831 set_irg_fp_model(irg, firm_opt.fp_model);
5832 tarval_enable_fp_ops(1);
5833 set_irn_dbg_info(get_irg_start_block(irg),
5834 get_entity_dbg_info(function_entity));
5836 ir_node *first_block = get_cur_block();
5838 /* set inline flags */
5839 if (entity->function.is_inline)
5840 set_irg_inline_property(irg, irg_inline_recomended);
5841 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5843 next_value_number_function = 0;
5844 initialize_function_parameters(entity);
5845 current_static_link = entity->function.static_link;
5847 statement_to_firm(entity->function.statement);
5849 ir_node *end_block = get_irg_end_block(irg);
5851 /* do we have a return statement yet? */
5852 if (get_cur_block() != NULL) {
5853 type_t *type = skip_typeref(entity->declaration.type);
5854 assert(is_type_function(type));
5855 const function_type_t *func_type = &type->function;
5856 const type_t *return_type
5857 = skip_typeref(func_type->return_type);
5860 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5861 ret = new_Return(get_store(), 0, NULL);
5864 if (is_type_scalar(return_type)) {
5865 mode = get_ir_mode_storage(func_type->return_type);
5871 /* ยง5.1.2.2.3 main implicitly returns 0 */
5872 if (is_main(entity)) {
5873 in[0] = new_Const(get_mode_null(mode));
5875 in[0] = new_Unknown(mode);
5877 ret = new_Return(get_store(), 1, in);
5879 add_immBlock_pred(end_block, ret);
5882 bool has_computed_gotos = false;
5883 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5884 label_t *label = all_labels[i];
5885 if (label->address_taken) {
5886 gen_ijmp_branches(label->block);
5887 has_computed_gotos = true;
5889 mature_immBlock(label->block);
5891 if (has_computed_gotos) {
5892 /* if we have computed goto's in the function, we cannot inline it */
5893 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5894 warningf(&entity->base.source_position,
5895 "function '%Y' can never be inlined because it contains a computed goto",
5896 entity->base.symbol);
5898 set_irg_inline_property(irg, irg_inline_forbidden);
5901 DEL_ARR_F(all_labels);
5904 mature_immBlock(first_block);
5905 mature_immBlock(end_block);
5907 irg_finalize_cons(irg);
5909 /* finalize the frame type */
5910 ir_type *frame_type = get_irg_frame_type(irg);
5911 int n = get_compound_n_members(frame_type);
5914 for (int i = 0; i < n; ++i) {
5915 ir_entity *entity = get_compound_member(frame_type, i);
5916 ir_type *entity_type = get_entity_type(entity);
5918 int align = get_type_alignment_bytes(entity_type);
5919 if (align > align_all)
5923 misalign = offset % align;
5925 offset += align - misalign;
5929 set_entity_offset(entity, offset);
5930 offset += get_type_size_bytes(entity_type);
5932 set_type_size_bytes(frame_type, offset);
5933 set_type_alignment_bytes(frame_type, align_all);
5935 irg_verify(irg, VERIFY_ENFORCE_SSA);
5936 current_function = old_current_function;
5938 if (current_trampolines != NULL) {
5939 DEL_ARR_F(current_trampolines);
5940 current_trampolines = NULL;
5943 /* create inner functions if any */
5944 entity_t **inner = inner_functions;
5945 if (inner != NULL) {
5946 ir_type *rem_outer_frame = current_outer_frame;
5947 current_outer_frame = get_irg_frame_type(current_ir_graph);
5948 ir_type *rem_outer_value_type = current_outer_value_type;
5949 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5950 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5951 create_function(inner[i]);
5955 current_outer_value_type = rem_outer_value_type;
5956 current_outer_frame = rem_outer_frame;
5960 static void scope_to_firm(scope_t *scope)
5962 /* first pass: create declarations */
5963 entity_t *entity = scope->entities;
5964 for ( ; entity != NULL; entity = entity->base.next) {
5965 if (entity->base.symbol == NULL)
5968 if (entity->kind == ENTITY_FUNCTION) {
5969 if (entity->function.btk != bk_none) {
5970 /* builtins have no representation */
5973 (void)get_function_entity(entity, NULL);
5974 } else if (entity->kind == ENTITY_VARIABLE) {
5975 create_global_variable(entity);
5976 } else if (entity->kind == ENTITY_NAMESPACE) {
5977 scope_to_firm(&entity->namespacee.members);
5981 /* second pass: create code/initializers */
5982 entity = scope->entities;
5983 for ( ; entity != NULL; entity = entity->base.next) {
5984 if (entity->base.symbol == NULL)
5987 if (entity->kind == ENTITY_FUNCTION) {
5988 if (entity->function.btk != bk_none) {
5989 /* builtins have no representation */
5992 create_function(entity);
5993 } else if (entity->kind == ENTITY_VARIABLE) {
5994 assert(entity->declaration.kind
5995 == DECLARATION_KIND_GLOBAL_VARIABLE);
5996 current_ir_graph = get_const_code_irg();
5997 create_variable_initializer(entity);
6002 void init_ast2firm(void)
6004 obstack_init(&asm_obst);
6005 init_atomic_modes();
6007 ir_set_debug_retrieve(dbg_retrieve);
6008 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
6010 /* create idents for all known runtime functions */
6011 for (size_t i = 0; i < lengthof(rts_data); ++i) {
6012 rts_idents[i] = new_id_from_str(rts_data[i].name);
6015 entitymap_init(&entitymap);
6018 static void init_ir_types(void)
6020 static int ir_types_initialized = 0;
6021 if (ir_types_initialized)
6023 ir_types_initialized = 1;
6025 ir_type_int = get_ir_type(type_int);
6026 ir_type_char = get_ir_type(type_char);
6027 ir_type_const_char = get_ir_type(type_const_char);
6028 ir_type_wchar_t = get_ir_type(type_wchar_t);
6029 ir_type_void = get_ir_type(type_void);
6031 be_params = be_get_backend_param();
6032 mode_float_arithmetic = be_params->mode_float_arithmetic;
6034 stack_param_align = be_params->stack_param_align;
6037 void exit_ast2firm(void)
6039 entitymap_destroy(&entitymap);
6040 obstack_free(&asm_obst, NULL);
6043 static void global_asm_to_firm(statement_t *s)
6045 for (; s != NULL; s = s->base.next) {
6046 assert(s->kind == STATEMENT_ASM);
6048 char const *const text = s->asms.asm_text.begin;
6049 size_t size = s->asms.asm_text.size;
6051 /* skip the last \0 */
6052 if (text[size - 1] == '\0')
6055 ident *const id = new_id_from_chars(text, size);
6060 void translation_unit_to_firm(translation_unit_t *unit)
6062 /* initialize firm arithmetic */
6063 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6064 ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6066 /* just to be sure */
6067 continue_label = NULL;
6069 current_switch_cond = NULL;
6070 current_translation_unit = unit;
6074 scope_to_firm(&unit->scope);
6075 global_asm_to_firm(unit->global_asm);
6077 current_ir_graph = NULL;
6078 current_translation_unit = NULL;