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 && warning.uninitialized) {
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, ir_relation_less_greater);
1096 return new_d_Conv(dbgi, value, dest_mode);
1099 static ir_node *create_Const_from_bool(ir_mode *const mode, bool const v)
1101 return new_Const((v ? get_mode_one : get_mode_null)(mode));
1105 * Creates a SymConst node representing a wide string literal.
1107 * @param literal the wide string literal
1109 static ir_node *wide_string_literal_to_firm(
1110 const string_literal_expression_t *literal)
1112 ir_type *const global_type = get_glob_type();
1113 ir_type *const elem_type = ir_type_wchar_t;
1114 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1115 ir_type *const type = new_type_array(1, elem_type);
1117 ident *const id = id_unique("str.%u");
1118 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1119 set_entity_ld_ident(entity, id);
1120 set_entity_visibility(entity, ir_visibility_private);
1121 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1123 ir_mode *const mode = get_type_mode(elem_type);
1124 const size_t slen = wstrlen(&literal->value);
1126 set_array_lower_bound_int(type, 0, 0);
1127 set_array_upper_bound_int(type, 0, slen);
1128 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1129 set_type_state(type, layout_fixed);
1131 ir_initializer_t *initializer = create_initializer_compound(slen);
1132 const char *p = literal->value.begin;
1133 for (size_t i = 0; i < slen; ++i) {
1134 assert(p < literal->value.begin + literal->value.size);
1135 utf32 v = read_utf8_char(&p);
1136 ir_tarval *tv = new_tarval_from_long(v, mode);
1137 ir_initializer_t *val = create_initializer_tarval(tv);
1138 set_initializer_compound_value(initializer, i, val);
1140 set_entity_initializer(entity, initializer);
1142 return create_symconst(dbgi, mode_P_data, entity);
1146 * Creates a SymConst node representing a string constant.
1148 * @param src_pos the source position of the string constant
1149 * @param id_prefix a prefix for the name of the generated string constant
1150 * @param value the value of the string constant
1152 static ir_node *string_to_firm(const source_position_t *const src_pos,
1153 const char *const id_prefix,
1154 const string_t *const value)
1156 ir_type *const global_type = get_glob_type();
1157 dbg_info *const dbgi = get_dbg_info(src_pos);
1158 ir_type *const type = new_type_array(1, ir_type_const_char);
1160 ident *const id = id_unique(id_prefix);
1161 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1162 set_entity_ld_ident(entity, id);
1163 set_entity_visibility(entity, ir_visibility_private);
1164 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1166 ir_type *const elem_type = ir_type_const_char;
1167 ir_mode *const mode = get_type_mode(elem_type);
1169 const char* const string = value->begin;
1170 const size_t slen = value->size;
1172 set_array_lower_bound_int(type, 0, 0);
1173 set_array_upper_bound_int(type, 0, slen);
1174 set_type_size_bytes(type, slen);
1175 set_type_state(type, layout_fixed);
1177 ir_initializer_t *initializer = create_initializer_compound(slen);
1178 for (size_t i = 0; i < slen; ++i) {
1179 ir_tarval *tv = new_tarval_from_long(string[i], mode);
1180 ir_initializer_t *val = create_initializer_tarval(tv);
1181 set_initializer_compound_value(initializer, i, val);
1183 set_entity_initializer(entity, initializer);
1185 return create_symconst(dbgi, mode_P_data, entity);
1188 static bool try_create_integer(literal_expression_t *literal,
1189 type_t *type, unsigned char base)
1191 const char *string = literal->value.begin;
1192 size_t size = literal->value.size;
1194 assert(type->kind == TYPE_ATOMIC);
1195 atomic_type_kind_t akind = type->atomic.akind;
1197 ir_mode *mode = atomic_modes[akind];
1198 ir_tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1199 if (tv == tarval_bad)
1202 literal->base.type = type;
1203 literal->target_value = tv;
1207 static void create_integer_tarval(literal_expression_t *literal)
1211 symbol_t *suffix = literal->suffix;
1213 if (suffix != NULL) {
1214 for (const char *c = suffix->string; *c != '\0'; ++c) {
1215 if (*c == 'u' || *c == 'U') { ++us; }
1216 if (*c == 'l' || *c == 'L') { ++ls; }
1221 switch (literal->base.kind) {
1222 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1223 case EXPR_LITERAL_INTEGER: base = 10; break;
1224 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1225 default: panic("invalid literal kind");
1228 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1230 /* now try if the constant is small enough for some types */
1231 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1233 if (us == 0 && try_create_integer(literal, type_int, base))
1235 if ((us == 1 || base != 10)
1236 && try_create_integer(literal, type_unsigned_int, base))
1240 if (us == 0 && try_create_integer(literal, type_long, base))
1242 if ((us == 1 || base != 10)
1243 && try_create_integer(literal, type_unsigned_long, base))
1246 /* last try? then we should not report tarval_bad */
1247 if (us != 1 && base == 10)
1248 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1249 if (us == 0 && try_create_integer(literal, type_long_long, base))
1253 assert(us == 1 || base != 10);
1254 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1255 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1257 panic("internal error when parsing number literal");
1260 tarval_set_integer_overflow_mode(old_mode);
1263 void determine_literal_type(literal_expression_t *literal)
1265 switch (literal->base.kind) {
1266 case EXPR_LITERAL_INTEGER:
1267 case EXPR_LITERAL_INTEGER_OCTAL:
1268 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1269 create_integer_tarval(literal);
1277 * Creates a Const node representing a constant.
1279 static ir_node *literal_to_firm(const literal_expression_t *literal)
1281 type_t *type = skip_typeref(literal->base.type);
1282 ir_mode *mode = get_ir_mode_storage(type);
1283 const char *string = literal->value.begin;
1284 size_t size = literal->value.size;
1287 switch (literal->base.kind) {
1288 case EXPR_LITERAL_WIDE_CHARACTER: {
1289 utf32 v = read_utf8_char(&string);
1291 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1293 tv = new_tarval_from_str(buf, len, mode);
1296 case EXPR_LITERAL_CHARACTER: {
1298 if (size == 1 && char_is_signed) {
1299 v = (signed char)string[0];
1302 for (size_t i = 0; i < size; ++i) {
1303 v = (v << 8) | ((unsigned char)string[i]);
1307 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1309 tv = new_tarval_from_str(buf, len, mode);
1312 case EXPR_LITERAL_INTEGER:
1313 case EXPR_LITERAL_INTEGER_OCTAL:
1314 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1315 assert(literal->target_value != NULL);
1316 tv = literal->target_value;
1318 case EXPR_LITERAL_FLOATINGPOINT:
1319 tv = new_tarval_from_str(string, size, mode);
1321 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1322 char buffer[size + 2];
1323 memcpy(buffer, "0x", 2);
1324 memcpy(buffer+2, string, size);
1325 tv = new_tarval_from_str(buffer, size+2, mode);
1328 case EXPR_LITERAL_BOOLEAN:
1329 if (string[0] == 't') {
1330 tv = get_mode_one(mode);
1332 assert(string[0] == 'f');
1333 tv = get_mode_null(mode);
1336 case EXPR_LITERAL_MS_NOOP:
1337 tv = get_mode_null(mode);
1342 panic("Invalid literal kind found");
1345 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1346 ir_node *res = new_d_Const(dbgi, tv);
1347 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1348 return create_conv(dbgi, res, mode_arith);
1352 * Allocate an area of size bytes aligned at alignment
1355 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1357 static unsigned area_cnt = 0;
1360 ir_type *tp = new_type_array(1, ir_type_char);
1361 set_array_bounds_int(tp, 0, 0, size);
1362 set_type_alignment_bytes(tp, alignment);
1364 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1365 ident *name = new_id_from_str(buf);
1366 ir_entity *area = new_entity(frame_type, name, tp);
1368 /* mark this entity as compiler generated */
1369 set_entity_compiler_generated(area, 1);
1374 * Return a node representing a trampoline region
1375 * for a given function entity.
1377 * @param dbgi debug info
1378 * @param entity the function entity
1380 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1382 ir_entity *region = NULL;
1385 if (current_trampolines != NULL) {
1386 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1387 if (current_trampolines[i].function == entity) {
1388 region = current_trampolines[i].region;
1393 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1395 ir_graph *irg = current_ir_graph;
1396 if (region == NULL) {
1397 /* create a new region */
1398 ir_type *frame_tp = get_irg_frame_type(irg);
1399 trampoline_region reg;
1400 reg.function = entity;
1402 reg.region = alloc_trampoline(frame_tp,
1403 be_params->trampoline_size,
1404 be_params->trampoline_align);
1405 ARR_APP1(trampoline_region, current_trampolines, reg);
1406 region = reg.region;
1408 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1413 * Creates a trampoline for a function represented by an entity.
1415 * @param dbgi debug info
1416 * @param mode the (reference) mode for the function address
1417 * @param entity the function entity
1419 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1422 assert(entity != NULL);
1424 in[0] = get_trampoline_region(dbgi, entity);
1425 in[1] = create_symconst(dbgi, mode, entity);
1426 in[2] = get_irg_frame(current_ir_graph);
1428 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1429 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1430 return new_Proj(irn, mode, pn_Builtin_1_result);
1434 * Dereference an address.
1436 * @param dbgi debug info
1437 * @param type the type of the dereferenced result (the points_to type)
1438 * @param addr the address to dereference
1440 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1441 ir_node *const addr)
1443 ir_type *irtype = get_ir_type(type);
1444 if (is_compound_type(irtype)
1445 || is_Method_type(irtype)
1446 || is_Array_type(irtype)) {
1450 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1451 ? cons_volatile : cons_none;
1452 ir_mode *const mode = get_type_mode(irtype);
1453 ir_node *const memory = get_store();
1454 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1455 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1456 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1458 set_store(load_mem);
1460 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1461 return create_conv(dbgi, load_res, mode_arithmetic);
1465 * Creates a strict Conv (to the node's mode) if necessary.
1467 * @param dbgi debug info
1468 * @param node the node to strict conv
1470 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1472 ir_mode *mode = get_irn_mode(node);
1474 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1476 if (!mode_is_float(mode))
1479 /* check if there is already a Conv */
1480 if (is_Conv(node)) {
1481 /* convert it into a strict Conv */
1482 set_Conv_strict(node, 1);
1486 /* otherwise create a new one */
1487 return new_d_strictConv(dbgi, node, mode);
1491 * Returns the address of a global variable.
1493 * @param dbgi debug info
1494 * @param variable the variable
1496 static ir_node *get_global_var_address(dbg_info *const dbgi,
1497 const variable_t *const variable)
1499 ir_entity *const irentity = variable->v.entity;
1500 if (variable->thread_local) {
1501 ir_node *const no_mem = new_NoMem();
1502 ir_node *const tls = get_irg_tls(current_ir_graph);
1503 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1505 return create_symconst(dbgi, mode_P_data, irentity);
1510 * Returns the correct base address depending on whether it is a parameter or a
1511 * normal local variable.
1513 static ir_node *get_local_frame(ir_entity *const ent)
1515 ir_graph *const irg = current_ir_graph;
1516 const ir_type *const owner = get_entity_owner(ent);
1517 if (owner == current_outer_frame || owner == current_outer_value_type) {
1518 assert(current_static_link != NULL);
1519 return current_static_link;
1521 return get_irg_frame(irg);
1526 * Keep all memory edges of the given block.
1528 static void keep_all_memory(ir_node *block)
1530 ir_node *old = get_cur_block();
1532 set_cur_block(block);
1533 keep_alive(get_store());
1534 /* TODO: keep all memory edges from restricted pointers */
1538 static ir_node *reference_expression_enum_value_to_firm(
1539 const reference_expression_t *ref)
1541 entity_t *entity = ref->entity;
1542 type_t *type = skip_typeref(entity->enum_value.enum_type);
1543 /* make sure the type is constructed */
1544 (void) get_ir_type(type);
1546 return new_Const(entity->enum_value.tv);
1549 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1551 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1552 entity_t *entity = ref->entity;
1553 assert(is_declaration(entity));
1554 type_t *type = skip_typeref(entity->declaration.type);
1556 /* make sure the type is constructed */
1557 (void) get_ir_type(type);
1559 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1560 ir_entity *irentity = get_function_entity(entity, NULL);
1561 /* for gcc compatibility we have to produce (dummy) addresses for some
1562 * builtins which don't have entities */
1563 if (irentity == NULL) {
1564 if (warning.other) {
1565 warningf(&ref->base.source_position,
1566 "taking address of builtin '%Y'",
1567 ref->entity->base.symbol);
1570 /* simply create a NULL pointer */
1571 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1572 ir_node *res = new_Const(get_mode_null(mode));
1578 switch ((declaration_kind_t) entity->declaration.kind) {
1579 case DECLARATION_KIND_UNKNOWN:
1582 case DECLARATION_KIND_LOCAL_VARIABLE: {
1583 ir_mode *const mode = get_ir_mode_storage(type);
1584 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1585 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1587 case DECLARATION_KIND_PARAMETER: {
1588 ir_mode *const mode = get_ir_mode_storage(type);
1589 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1590 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1592 case DECLARATION_KIND_FUNCTION: {
1593 ir_mode *const mode = get_ir_mode_storage(type);
1594 return create_symconst(dbgi, mode, entity->function.irentity);
1596 case DECLARATION_KIND_INNER_FUNCTION: {
1597 ir_mode *const mode = get_ir_mode_storage(type);
1598 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1599 /* inner function not using the closure */
1600 return create_symconst(dbgi, mode, entity->function.irentity);
1602 /* need trampoline here */
1603 return create_trampoline(dbgi, mode, entity->function.irentity);
1606 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1607 const variable_t *variable = &entity->variable;
1608 ir_node *const addr = get_global_var_address(dbgi, variable);
1609 return deref_address(dbgi, variable->base.type, addr);
1612 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1613 ir_entity *irentity = entity->variable.v.entity;
1614 ir_node *frame = get_local_frame(irentity);
1615 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1616 return deref_address(dbgi, entity->declaration.type, sel);
1618 case DECLARATION_KIND_PARAMETER_ENTITY: {
1619 ir_entity *irentity = entity->parameter.v.entity;
1620 ir_node *frame = get_local_frame(irentity);
1621 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1622 return deref_address(dbgi, entity->declaration.type, sel);
1625 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1626 return entity->variable.v.vla_base;
1628 case DECLARATION_KIND_COMPOUND_MEMBER:
1629 panic("not implemented reference type");
1632 panic("reference to declaration with unknown type found");
1635 static ir_node *reference_addr(const reference_expression_t *ref)
1637 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1638 entity_t *entity = ref->entity;
1639 assert(is_declaration(entity));
1641 switch((declaration_kind_t) entity->declaration.kind) {
1642 case DECLARATION_KIND_UNKNOWN:
1644 case DECLARATION_KIND_PARAMETER:
1645 case DECLARATION_KIND_LOCAL_VARIABLE:
1646 /* you can store to a local variable (so we don't panic but return NULL
1647 * as an indicator for no real address) */
1649 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1650 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1653 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1654 ir_entity *irentity = entity->variable.v.entity;
1655 ir_node *frame = get_local_frame(irentity);
1656 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1660 case DECLARATION_KIND_PARAMETER_ENTITY: {
1661 ir_entity *irentity = entity->parameter.v.entity;
1662 ir_node *frame = get_local_frame(irentity);
1663 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1668 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1669 return entity->variable.v.vla_base;
1671 case DECLARATION_KIND_FUNCTION: {
1672 type_t *const type = skip_typeref(entity->declaration.type);
1673 ir_mode *const mode = get_ir_mode_storage(type);
1674 return create_symconst(dbgi, mode, entity->function.irentity);
1677 case DECLARATION_KIND_INNER_FUNCTION: {
1678 type_t *const type = skip_typeref(entity->declaration.type);
1679 ir_mode *const mode = get_ir_mode_storage(type);
1680 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1681 /* inner function not using the closure */
1682 return create_symconst(dbgi, mode, entity->function.irentity);
1684 /* need trampoline here */
1685 return create_trampoline(dbgi, mode, entity->function.irentity);
1689 case DECLARATION_KIND_COMPOUND_MEMBER:
1690 panic("not implemented reference type");
1693 panic("reference to declaration with unknown type found");
1697 * Generate an unary builtin.
1699 * @param kind the builtin kind to generate
1700 * @param op the operand
1701 * @param function_type the function type for the GNU builtin routine
1702 * @param db debug info
1704 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1707 in[0] = expression_to_firm(op);
1709 ir_type *tp = get_ir_type(function_type);
1710 ir_type *res = get_method_res_type(tp, 0);
1711 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1712 set_irn_pinned(irn, op_pin_state_floats);
1713 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1717 * Generate a pinned unary builtin.
1719 * @param kind the builtin kind to generate
1720 * @param op the operand
1721 * @param function_type the function type for the GNU builtin routine
1722 * @param db debug info
1724 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1725 type_t *function_type, dbg_info *db)
1728 in[0] = expression_to_firm(op);
1730 ir_type *tp = get_ir_type(function_type);
1731 ir_type *res = get_method_res_type(tp, 0);
1732 ir_node *mem = get_store();
1733 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1734 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1735 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1739 * Generate an binary-void-return builtin.
1741 * @param kind the builtin kind to generate
1742 * @param op1 the first operand
1743 * @param op2 the second operand
1744 * @param function_type the function type for the GNU builtin routine
1745 * @param db debug info
1747 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1748 expression_t *op2, type_t *function_type,
1752 in[0] = expression_to_firm(op1);
1753 in[1] = expression_to_firm(op2);
1755 ir_type *tp = get_ir_type(function_type);
1756 ir_node *mem = get_store();
1757 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1758 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1763 * Transform calls to builtin functions.
1765 static ir_node *process_builtin_call(const call_expression_t *call)
1767 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1769 assert(call->function->kind == EXPR_REFERENCE);
1770 reference_expression_t *builtin = &call->function->reference;
1772 type_t *type = skip_typeref(builtin->base.type);
1773 assert(is_type_pointer(type));
1775 type_t *function_type = skip_typeref(type->pointer.points_to);
1777 switch (builtin->entity->function.btk) {
1778 case bk_gnu_builtin_alloca: {
1779 if (call->arguments == NULL || call->arguments->next != NULL) {
1780 panic("invalid number of parameters on __builtin_alloca");
1782 expression_t *argument = call->arguments->expression;
1783 ir_node *size = expression_to_firm(argument);
1785 ir_node *store = get_store();
1786 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1788 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1790 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1795 case bk_gnu_builtin_huge_val:
1796 case bk_gnu_builtin_huge_valf:
1797 case bk_gnu_builtin_huge_vall:
1798 case bk_gnu_builtin_inf:
1799 case bk_gnu_builtin_inff:
1800 case bk_gnu_builtin_infl: {
1801 type_t *type = function_type->function.return_type;
1802 ir_mode *mode = get_ir_mode_arithmetic(type);
1803 ir_tarval *tv = get_mode_infinite(mode);
1804 ir_node *res = new_d_Const(dbgi, tv);
1807 case bk_gnu_builtin_nan:
1808 case bk_gnu_builtin_nanf:
1809 case bk_gnu_builtin_nanl: {
1810 /* Ignore string for now... */
1811 assert(is_type_function(function_type));
1812 type_t *type = function_type->function.return_type;
1813 ir_mode *mode = get_ir_mode_arithmetic(type);
1814 ir_tarval *tv = get_mode_NAN(mode);
1815 ir_node *res = new_d_Const(dbgi, tv);
1818 case bk_gnu_builtin_expect: {
1819 expression_t *argument = call->arguments->expression;
1820 return _expression_to_firm(argument);
1822 case bk_gnu_builtin_va_end:
1823 /* evaluate the argument of va_end for its side effects */
1824 _expression_to_firm(call->arguments->expression);
1826 case bk_gnu_builtin_frame_address: {
1827 expression_t *const expression = call->arguments->expression;
1828 bool val = fold_constant_to_bool(expression);
1831 return get_irg_frame(current_ir_graph);
1833 /* get the argument */
1836 in[0] = expression_to_firm(expression);
1837 in[1] = get_irg_frame(current_ir_graph);
1838 ir_type *tp = get_ir_type(function_type);
1839 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1840 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1843 case bk_gnu_builtin_return_address: {
1844 expression_t *const expression = call->arguments->expression;
1847 in[0] = expression_to_firm(expression);
1848 in[1] = get_irg_frame(current_ir_graph);
1849 ir_type *tp = get_ir_type(function_type);
1850 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1851 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1853 case bk_gnu_builtin_ffs:
1854 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1855 case bk_gnu_builtin_clz:
1856 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1857 case bk_gnu_builtin_ctz:
1858 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1859 case bk_gnu_builtin_popcount:
1860 case bk_ms__popcount:
1861 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1862 case bk_gnu_builtin_parity:
1863 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1864 case bk_gnu_builtin_prefetch: {
1865 call_argument_t *const args = call->arguments;
1866 expression_t *const addr = args->expression;
1869 in[0] = _expression_to_firm(addr);
1870 if (args->next != NULL) {
1871 expression_t *const rw = args->next->expression;
1873 in[1] = _expression_to_firm(rw);
1875 if (args->next->next != NULL) {
1876 expression_t *const locality = args->next->next->expression;
1878 in[2] = expression_to_firm(locality);
1880 in[2] = new_Const_long(mode_int, 3);
1883 in[1] = new_Const_long(mode_int, 0);
1884 in[2] = new_Const_long(mode_int, 3);
1886 ir_type *tp = get_ir_type(function_type);
1887 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1888 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1891 case bk_gnu_builtin_object_size: {
1892 /* determine value of "type" */
1893 expression_t *type_expression = call->arguments->next->expression;
1894 long type_val = fold_constant_to_int(type_expression);
1895 type_t *type = function_type->function.return_type;
1896 ir_mode *mode = get_ir_mode_arithmetic(type);
1897 /* just produce a "I don't know" result */
1898 ir_tarval *result = type_val & 2 ? get_mode_null(mode) :
1899 get_mode_minus_one(mode);
1901 return new_d_Const(dbgi, result);
1903 case bk_gnu_builtin_trap:
1906 ir_type *tp = get_ir_type(function_type);
1907 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1908 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1911 case bk_ms__debugbreak: {
1912 ir_type *tp = get_ir_type(function_type);
1913 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1914 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1917 case bk_ms_ReturnAddress: {
1920 in[0] = new_Const(get_mode_null(mode_int));
1921 in[1] = get_irg_frame(current_ir_graph);
1922 ir_type *tp = get_ir_type(function_type);
1923 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1924 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1927 case bk_ms_rotl64: {
1928 ir_node *val = expression_to_firm(call->arguments->expression);
1929 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1930 ir_mode *mode = get_irn_mode(val);
1931 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1934 case bk_ms_rotr64: {
1935 ir_node *val = expression_to_firm(call->arguments->expression);
1936 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1937 ir_mode *mode = get_irn_mode(val);
1938 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1939 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1940 return new_d_Rotl(dbgi, val, sub, mode);
1942 case bk_ms_byteswap_ushort:
1943 case bk_ms_byteswap_ulong:
1944 case bk_ms_byteswap_uint64:
1945 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1948 case bk_ms__indword:
1949 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1950 case bk_ms__outbyte:
1951 case bk_ms__outword:
1952 case bk_ms__outdword:
1953 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1954 call->arguments->next->expression, function_type, dbgi);
1956 panic("unsupported builtin found");
1961 * Transform a call expression.
1962 * Handles some special cases, like alloca() calls, which must be resolved
1963 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1964 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1967 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1969 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1970 assert(get_cur_block() != NULL);
1972 expression_t *function = call->function;
1973 if (function->kind == EXPR_REFERENCE) {
1974 const reference_expression_t *ref = &function->reference;
1975 entity_t *entity = ref->entity;
1977 if (entity->kind == ENTITY_FUNCTION) {
1978 ir_entity *irentity = entity->function.irentity;
1979 if (irentity == NULL)
1980 irentity = get_function_entity(entity, NULL);
1982 if (irentity == NULL && entity->function.btk != bk_none) {
1983 return process_builtin_call(call);
1987 if (irentity == rts_entities[rts_alloca]) {
1988 /* handle alloca() call */
1989 expression_t *argument = call->arguments->expression;
1990 ir_node *size = expression_to_firm(argument);
1991 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1993 size = create_conv(dbgi, size, mode);
1995 ir_node *store = get_store();
1996 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1997 firm_unknown_type, stack_alloc);
1998 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
2000 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
2007 ir_node *callee = expression_to_firm(function);
2009 type_t *type = skip_typeref(function->base.type);
2010 assert(is_type_pointer(type));
2011 pointer_type_t *pointer_type = &type->pointer;
2012 type_t *points_to = skip_typeref(pointer_type->points_to);
2013 assert(is_type_function(points_to));
2014 function_type_t *function_type = &points_to->function;
2016 int n_parameters = 0;
2017 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
2018 ir_type *new_method_type = NULL;
2019 if (function_type->variadic || function_type->unspecified_parameters) {
2020 const call_argument_t *argument = call->arguments;
2021 for ( ; argument != NULL; argument = argument->next) {
2025 /* we need to construct a new method type matching the call
2027 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2028 int n_res = get_method_n_ress(ir_method_type);
2029 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2030 set_method_calling_convention(new_method_type,
2031 get_method_calling_convention(ir_method_type));
2032 set_method_additional_properties(new_method_type,
2033 get_method_additional_properties(ir_method_type));
2034 set_method_variadicity(new_method_type,
2035 get_method_variadicity(ir_method_type));
2037 for (int i = 0; i < n_res; ++i) {
2038 set_method_res_type(new_method_type, i,
2039 get_method_res_type(ir_method_type, i));
2041 argument = call->arguments;
2042 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2043 expression_t *expression = argument->expression;
2044 ir_type *irtype = get_ir_type(expression->base.type);
2045 set_method_param_type(new_method_type, i, irtype);
2047 ir_method_type = new_method_type;
2049 n_parameters = get_method_n_params(ir_method_type);
2052 ir_node *in[n_parameters];
2054 const call_argument_t *argument = call->arguments;
2055 for (int n = 0; n < n_parameters; ++n) {
2056 expression_t *expression = argument->expression;
2057 ir_node *arg_node = expression_to_firm(expression);
2059 type_t *type = skip_typeref(expression->base.type);
2060 if (!is_type_compound(type)) {
2061 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2062 arg_node = create_conv(dbgi, arg_node, mode);
2063 arg_node = do_strict_conv(dbgi, arg_node);
2068 argument = argument->next;
2071 ir_node *store = get_store();
2072 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2074 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2077 type_t *return_type = skip_typeref(function_type->return_type);
2078 ir_node *result = NULL;
2080 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2081 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2083 if (is_type_scalar(return_type)) {
2084 ir_mode *mode = get_ir_mode_storage(return_type);
2085 result = new_d_Proj(dbgi, resproj, mode, 0);
2086 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2087 result = create_conv(NULL, result, mode_arith);
2089 ir_mode *mode = mode_P_data;
2090 result = new_d_Proj(dbgi, resproj, mode, 0);
2094 if (function->kind == EXPR_REFERENCE &&
2095 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2096 /* A dead end: Keep the Call and the Block. Also place all further
2097 * nodes into a new and unreachable block. */
2099 keep_alive(get_cur_block());
2100 ir_node *block = new_Block(0, NULL);
2101 set_cur_block(block);
2107 static void statement_to_firm(statement_t *statement);
2108 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2110 static ir_node *expression_to_addr(const expression_t *expression);
2111 static ir_node *create_condition_evaluation(const expression_t *expression,
2112 ir_node *true_block,
2113 ir_node *false_block);
2115 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2118 if (!is_type_compound(type)) {
2119 ir_mode *mode = get_ir_mode_storage(type);
2120 value = create_conv(dbgi, value, mode);
2121 value = do_strict_conv(dbgi, value);
2124 ir_node *memory = get_store();
2126 if (is_type_scalar(type)) {
2127 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2128 ? cons_volatile : cons_none;
2129 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2130 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2131 set_store(store_mem);
2133 ir_type *irtype = get_ir_type(type);
2134 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2135 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2136 set_store(copyb_mem);
2140 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2142 ir_tarval *all_one = get_mode_all_one(mode);
2143 int mode_size = get_mode_size_bits(mode);
2145 assert(offset >= 0);
2147 assert(offset + size <= mode_size);
2148 if (size == mode_size) {
2152 long shiftr = get_mode_size_bits(mode) - size;
2153 long shiftl = offset;
2154 ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2155 ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2156 ir_tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2157 ir_tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2162 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2163 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2165 ir_type *entity_type = get_entity_type(entity);
2166 ir_type *base_type = get_primitive_base_type(entity_type);
2167 assert(base_type != NULL);
2168 ir_mode *mode = get_type_mode(base_type);
2170 value = create_conv(dbgi, value, mode);
2172 /* kill upper bits of value and shift to right position */
2173 int bitoffset = get_entity_offset_bits_remainder(entity);
2174 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2176 ir_tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2177 ir_node *mask_node = new_d_Const(dbgi, mask);
2178 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2179 ir_tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2180 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2181 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2183 /* load current value */
2184 ir_node *mem = get_store();
2185 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2186 set_volatile ? cons_volatile : cons_none);
2187 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2188 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2189 ir_tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2190 ir_tarval *inv_mask = tarval_not(shift_mask);
2191 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2192 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2194 /* construct new value and store */
2195 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2196 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2197 set_volatile ? cons_volatile : cons_none);
2198 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2199 set_store(store_mem);
2201 return value_masked;
2204 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2207 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2208 type_t *type = expression->base.type;
2209 ir_mode *mode = get_ir_mode_storage(type);
2210 ir_node *mem = get_store();
2211 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2212 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2213 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2215 load_res = create_conv(dbgi, load_res, mode_int);
2217 set_store(load_mem);
2219 /* kill upper bits */
2220 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2221 ir_entity *entity = expression->compound_entry->compound_member.entity;
2222 int bitoffset = get_entity_offset_bits_remainder(entity);
2223 ir_type *entity_type = get_entity_type(entity);
2224 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2225 long shift_bitsl = machine_size - bitoffset - bitsize;
2226 assert(shift_bitsl >= 0);
2227 ir_tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2228 ir_node *countl = new_d_Const(dbgi, tvl);
2229 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2231 long shift_bitsr = bitoffset + shift_bitsl;
2232 assert(shift_bitsr <= (long) machine_size);
2233 ir_tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2234 ir_node *countr = new_d_Const(dbgi, tvr);
2236 if (mode_is_signed(mode)) {
2237 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2239 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2242 return create_conv(dbgi, shiftr, mode);
2245 /* make sure the selected compound type is constructed */
2246 static void construct_select_compound(const select_expression_t *expression)
2248 type_t *type = skip_typeref(expression->compound->base.type);
2249 if (is_type_pointer(type)) {
2250 type = type->pointer.points_to;
2252 (void) get_ir_type(type);
2255 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2256 ir_node *value, ir_node *addr)
2258 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2259 type_t *type = skip_typeref(expression->base.type);
2261 if (!is_type_compound(type)) {
2262 ir_mode *mode = get_ir_mode_storage(type);
2263 value = create_conv(dbgi, value, mode);
2264 value = do_strict_conv(dbgi, value);
2267 if (expression->kind == EXPR_REFERENCE) {
2268 const reference_expression_t *ref = &expression->reference;
2270 entity_t *entity = ref->entity;
2271 assert(is_declaration(entity));
2272 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2273 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2274 set_value(entity->variable.v.value_number, value);
2276 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2277 set_value(entity->parameter.v.value_number, value);
2283 addr = expression_to_addr(expression);
2284 assert(addr != NULL);
2286 if (expression->kind == EXPR_SELECT) {
2287 const select_expression_t *select = &expression->select;
2289 construct_select_compound(select);
2291 entity_t *entity = select->compound_entry;
2292 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2293 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2294 ir_entity *irentity = entity->compound_member.entity;
2296 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2297 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2303 assign_value(dbgi, addr, type, value);
2307 static void set_value_for_expression(const expression_t *expression,
2310 set_value_for_expression_addr(expression, value, NULL);
2313 static ir_node *get_value_from_lvalue(const expression_t *expression,
2316 if (expression->kind == EXPR_REFERENCE) {
2317 const reference_expression_t *ref = &expression->reference;
2319 entity_t *entity = ref->entity;
2320 assert(entity->kind == ENTITY_VARIABLE
2321 || entity->kind == ENTITY_PARAMETER);
2322 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2324 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2325 value_number = entity->variable.v.value_number;
2326 assert(addr == NULL);
2327 type_t *type = skip_typeref(expression->base.type);
2328 ir_mode *mode = get_ir_mode_storage(type);
2329 ir_node *res = get_value(value_number, mode);
2330 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2331 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2332 value_number = entity->parameter.v.value_number;
2333 assert(addr == NULL);
2334 type_t *type = skip_typeref(expression->base.type);
2335 ir_mode *mode = get_ir_mode_storage(type);
2336 ir_node *res = get_value(value_number, mode);
2337 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2341 assert(addr != NULL);
2342 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2345 if (expression->kind == EXPR_SELECT &&
2346 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2347 construct_select_compound(&expression->select);
2348 value = bitfield_extract_to_firm(&expression->select, addr);
2350 value = deref_address(dbgi, expression->base.type, addr);
2357 static ir_node *create_incdec(const unary_expression_t *expression)
2359 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2360 const expression_t *value_expr = expression->value;
2361 ir_node *addr = expression_to_addr(value_expr);
2362 ir_node *value = get_value_from_lvalue(value_expr, addr);
2364 type_t *type = skip_typeref(expression->base.type);
2365 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2368 if (is_type_pointer(type)) {
2369 pointer_type_t *pointer_type = &type->pointer;
2370 offset = get_type_size_node(pointer_type->points_to);
2372 assert(is_type_arithmetic(type));
2373 offset = new_Const(get_mode_one(mode));
2377 ir_node *store_value;
2378 switch(expression->base.kind) {
2379 case EXPR_UNARY_POSTFIX_INCREMENT:
2381 store_value = new_d_Add(dbgi, value, offset, mode);
2383 case EXPR_UNARY_POSTFIX_DECREMENT:
2385 store_value = new_d_Sub(dbgi, value, offset, mode);
2387 case EXPR_UNARY_PREFIX_INCREMENT:
2388 result = new_d_Add(dbgi, value, offset, mode);
2389 store_value = result;
2391 case EXPR_UNARY_PREFIX_DECREMENT:
2392 result = new_d_Sub(dbgi, value, offset, mode);
2393 store_value = result;
2396 panic("no incdec expr in create_incdec");
2399 set_value_for_expression_addr(value_expr, store_value, addr);
2404 static bool is_local_variable(expression_t *expression)
2406 if (expression->kind != EXPR_REFERENCE)
2408 reference_expression_t *ref_expr = &expression->reference;
2409 entity_t *entity = ref_expr->entity;
2410 if (entity->kind != ENTITY_VARIABLE)
2412 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2413 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2416 static ir_relation get_relation(const expression_kind_t kind)
2419 case EXPR_BINARY_EQUAL: return ir_relation_equal;
2420 case EXPR_BINARY_ISLESSGREATER: return ir_relation_less_greater;
2421 case EXPR_BINARY_NOTEQUAL: return ir_relation_unordered_less_greater;
2422 case EXPR_BINARY_ISLESS:
2423 case EXPR_BINARY_LESS: return ir_relation_less;
2424 case EXPR_BINARY_ISLESSEQUAL:
2425 case EXPR_BINARY_LESSEQUAL: return ir_relation_less_equal;
2426 case EXPR_BINARY_ISGREATER:
2427 case EXPR_BINARY_GREATER: return ir_relation_greater;
2428 case EXPR_BINARY_ISGREATEREQUAL:
2429 case EXPR_BINARY_GREATEREQUAL: return ir_relation_greater_equal;
2430 case EXPR_BINARY_ISUNORDERED: return ir_relation_unordered;
2435 panic("trying to get pn_Cmp from non-comparison binexpr type");
2439 * Handle the assume optimizer hint: check if a Confirm
2440 * node can be created.
2442 * @param dbi debug info
2443 * @param expr the IL assume expression
2445 * we support here only some simple cases:
2450 static ir_node *handle_assume_compare(dbg_info *dbi,
2451 const binary_expression_t *expression)
2453 expression_t *op1 = expression->left;
2454 expression_t *op2 = expression->right;
2455 entity_t *var2, *var = NULL;
2456 ir_node *res = NULL;
2457 ir_relation relation = get_relation(expression->base.kind);
2459 if (is_local_variable(op1) && is_local_variable(op2)) {
2460 var = op1->reference.entity;
2461 var2 = op2->reference.entity;
2463 type_t *const type = skip_typeref(var->declaration.type);
2464 ir_mode *const mode = get_ir_mode_storage(type);
2466 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2467 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2469 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_relation(relation));
2470 set_value(var2->variable.v.value_number, res);
2472 res = new_d_Confirm(dbi, irn1, irn2, relation);
2473 set_value(var->variable.v.value_number, res);
2479 if (is_local_variable(op1) && is_constant_expression(op2) == EXPR_CLASS_CONSTANT) {
2480 var = op1->reference.entity;
2482 } else if (is_constant_expression(op1) == EXPR_CLASS_CONSTANT && is_local_variable(op2)) {
2483 relation = get_inversed_relation(relation);
2484 var = op2->reference.entity;
2489 type_t *const type = skip_typeref(var->declaration.type);
2490 ir_mode *const mode = get_ir_mode_storage(type);
2492 res = get_value(var->variable.v.value_number, mode);
2493 res = new_d_Confirm(dbi, res, expression_to_firm(con), relation);
2494 set_value(var->variable.v.value_number, res);
2500 * Handle the assume optimizer hint.
2502 * @param dbi debug info
2503 * @param expr the IL assume expression
2505 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2507 switch(expression->kind) {
2508 case EXPR_BINARY_EQUAL:
2509 case EXPR_BINARY_NOTEQUAL:
2510 case EXPR_BINARY_LESS:
2511 case EXPR_BINARY_LESSEQUAL:
2512 case EXPR_BINARY_GREATER:
2513 case EXPR_BINARY_GREATEREQUAL:
2514 return handle_assume_compare(dbi, &expression->binary);
2520 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2521 type_t *from_type, type_t *type)
2523 type = skip_typeref(type);
2524 if (type == type_void) {
2525 /* make sure firm type is constructed */
2526 (void) get_ir_type(type);
2529 if (!is_type_scalar(type)) {
2530 /* make sure firm type is constructed */
2531 (void) get_ir_type(type);
2535 from_type = skip_typeref(from_type);
2536 ir_mode *mode = get_ir_mode_storage(type);
2537 /* check for conversion from / to __based types */
2538 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2539 const variable_t *from_var = from_type->pointer.base_variable;
2540 const variable_t *to_var = type->pointer.base_variable;
2541 if (from_var != to_var) {
2542 if (from_var != NULL) {
2543 ir_node *const addr = get_global_var_address(dbgi, from_var);
2544 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2545 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2547 if (to_var != NULL) {
2548 ir_node *const addr = get_global_var_address(dbgi, to_var);
2549 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2550 value_node = new_d_Sub(dbgi, value_node, base, mode);
2555 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2556 /* bool adjustments (we save a mode_Bu, but have to temporarily
2557 * convert to mode_b so we only get a 0/1 value */
2558 value_node = create_conv(dbgi, value_node, mode_b);
2561 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2562 ir_node *node = create_conv(dbgi, value_node, mode);
2563 node = do_strict_conv(dbgi, node);
2564 node = create_conv(dbgi, node, mode_arith);
2569 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2571 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2572 type_t *type = skip_typeref(expression->base.type);
2574 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2575 return expression_to_addr(expression->value);
2577 const expression_t *value = expression->value;
2579 switch(expression->base.kind) {
2580 case EXPR_UNARY_NEGATE: {
2581 ir_node *value_node = expression_to_firm(value);
2582 ir_mode *mode = get_ir_mode_arithmetic(type);
2583 return new_d_Minus(dbgi, value_node, mode);
2585 case EXPR_UNARY_PLUS:
2586 return expression_to_firm(value);
2587 case EXPR_UNARY_BITWISE_NEGATE: {
2588 ir_node *value_node = expression_to_firm(value);
2589 ir_mode *mode = get_ir_mode_arithmetic(type);
2590 return new_d_Not(dbgi, value_node, mode);
2592 case EXPR_UNARY_NOT: {
2593 ir_node *value_node = _expression_to_firm(value);
2594 value_node = create_conv(dbgi, value_node, mode_b);
2595 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2598 case EXPR_UNARY_DEREFERENCE: {
2599 ir_node *value_node = expression_to_firm(value);
2600 type_t *value_type = skip_typeref(value->base.type);
2601 assert(is_type_pointer(value_type));
2603 /* check for __based */
2604 const variable_t *const base_var = value_type->pointer.base_variable;
2605 if (base_var != NULL) {
2606 ir_node *const addr = get_global_var_address(dbgi, base_var);
2607 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2608 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2610 type_t *points_to = value_type->pointer.points_to;
2611 return deref_address(dbgi, points_to, value_node);
2613 case EXPR_UNARY_POSTFIX_INCREMENT:
2614 case EXPR_UNARY_POSTFIX_DECREMENT:
2615 case EXPR_UNARY_PREFIX_INCREMENT:
2616 case EXPR_UNARY_PREFIX_DECREMENT:
2617 return create_incdec(expression);
2618 case EXPR_UNARY_CAST_IMPLICIT:
2619 case EXPR_UNARY_CAST: {
2620 ir_node *value_node = expression_to_firm(value);
2621 type_t *from_type = value->base.type;
2622 return create_cast(dbgi, value_node, from_type, type);
2624 case EXPR_UNARY_ASSUME:
2625 return handle_assume(dbgi, value);
2630 panic("invalid UNEXPR type found");
2634 * produces a 0/1 depending of the value of a mode_b node
2636 static ir_node *produce_condition_result(const expression_t *expression,
2637 ir_mode *mode, dbg_info *dbgi)
2639 ir_node *const one_block = new_immBlock();
2640 ir_node *const zero_block = new_immBlock();
2641 create_condition_evaluation(expression, one_block, zero_block);
2642 mature_immBlock(one_block);
2643 mature_immBlock(zero_block);
2645 ir_node *const jmp_one = new_rd_Jmp(dbgi, one_block);
2646 ir_node *const jmp_zero = new_rd_Jmp(dbgi, zero_block);
2647 ir_node *const in_cf[2] = { jmp_one, jmp_zero };
2648 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
2649 set_cur_block(block);
2651 ir_node *const one = new_Const(get_mode_one(mode));
2652 ir_node *const zero = new_Const(get_mode_null(mode));
2653 ir_node *const in[2] = { one, zero };
2654 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
2659 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2660 ir_node *value, type_t *type)
2662 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2663 assert(is_type_pointer(type));
2664 pointer_type_t *const pointer_type = &type->pointer;
2665 type_t *const points_to = skip_typeref(pointer_type->points_to);
2666 ir_node * elem_size = get_type_size_node(points_to);
2667 elem_size = create_conv(dbgi, elem_size, mode);
2668 value = create_conv(dbgi, value, mode);
2669 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2673 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2674 ir_node *left, ir_node *right)
2677 type_t *type_left = skip_typeref(expression->left->base.type);
2678 type_t *type_right = skip_typeref(expression->right->base.type);
2680 expression_kind_t kind = expression->base.kind;
2683 case EXPR_BINARY_SHIFTLEFT:
2684 case EXPR_BINARY_SHIFTRIGHT:
2685 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2686 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2687 mode = get_irn_mode(left);
2688 right = create_conv(dbgi, right, mode_uint);
2691 case EXPR_BINARY_SUB:
2692 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2693 const pointer_type_t *const ptr_type = &type_left->pointer;
2695 mode = get_ir_mode_arithmetic(expression->base.type);
2696 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2697 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2698 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2699 ir_node *const no_mem = new_NoMem();
2700 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2701 mode, op_pin_state_floats);
2702 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2705 case EXPR_BINARY_SUB_ASSIGN:
2706 if (is_type_pointer(type_left)) {
2707 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2708 mode = get_ir_mode_arithmetic(type_left);
2713 case EXPR_BINARY_ADD:
2714 case EXPR_BINARY_ADD_ASSIGN:
2715 if (is_type_pointer(type_left)) {
2716 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2717 mode = get_ir_mode_arithmetic(type_left);
2719 } else if (is_type_pointer(type_right)) {
2720 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2721 mode = get_ir_mode_arithmetic(type_right);
2728 mode = get_ir_mode_arithmetic(type_right);
2729 left = create_conv(dbgi, left, mode);
2734 case EXPR_BINARY_ADD_ASSIGN:
2735 case EXPR_BINARY_ADD:
2736 return new_d_Add(dbgi, left, right, mode);
2737 case EXPR_BINARY_SUB_ASSIGN:
2738 case EXPR_BINARY_SUB:
2739 return new_d_Sub(dbgi, left, right, mode);
2740 case EXPR_BINARY_MUL_ASSIGN:
2741 case EXPR_BINARY_MUL:
2742 return new_d_Mul(dbgi, left, right, mode);
2743 case EXPR_BINARY_BITWISE_AND:
2744 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2745 return new_d_And(dbgi, left, right, mode);
2746 case EXPR_BINARY_BITWISE_OR:
2747 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2748 return new_d_Or(dbgi, left, right, mode);
2749 case EXPR_BINARY_BITWISE_XOR:
2750 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2751 return new_d_Eor(dbgi, left, right, mode);
2752 case EXPR_BINARY_SHIFTLEFT:
2753 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2754 return new_d_Shl(dbgi, left, right, mode);
2755 case EXPR_BINARY_SHIFTRIGHT:
2756 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2757 if (mode_is_signed(mode)) {
2758 return new_d_Shrs(dbgi, left, right, mode);
2760 return new_d_Shr(dbgi, left, right, mode);
2762 case EXPR_BINARY_DIV:
2763 case EXPR_BINARY_DIV_ASSIGN: {
2764 ir_node *pin = new_Pin(new_NoMem());
2765 ir_node *op = new_d_Div(dbgi, pin, left, right, mode,
2766 op_pin_state_floats);
2767 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2770 case EXPR_BINARY_MOD:
2771 case EXPR_BINARY_MOD_ASSIGN: {
2772 ir_node *pin = new_Pin(new_NoMem());
2773 assert(!mode_is_float(mode));
2774 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2775 op_pin_state_floats);
2776 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2780 panic("unexpected expression kind");
2784 static ir_node *create_lazy_op(const binary_expression_t *expression)
2786 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2787 type_t *type = skip_typeref(expression->base.type);
2788 ir_mode *mode = get_ir_mode_arithmetic(type);
2790 if (is_constant_expression(expression->left) == EXPR_CLASS_CONSTANT) {
2791 bool val = fold_constant_to_bool(expression->left);
2792 expression_kind_t ekind = expression->base.kind;
2793 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2794 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2796 return new_Const(get_mode_null(mode));
2800 return new_Const(get_mode_one(mode));
2804 if (is_constant_expression(expression->right) == EXPR_CLASS_CONSTANT) {
2805 bool valr = fold_constant_to_bool(expression->right);
2806 return create_Const_from_bool(mode, valr);
2809 return produce_condition_result(expression->right, mode, dbgi);
2812 return produce_condition_result((const expression_t*) expression, mode,
2816 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2817 ir_node *right, ir_mode *mode);
2819 static ir_node *create_assign_binop(const binary_expression_t *expression)
2821 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2822 const expression_t *left_expr = expression->left;
2823 type_t *type = skip_typeref(left_expr->base.type);
2824 ir_node *right = expression_to_firm(expression->right);
2825 ir_node *left_addr = expression_to_addr(left_expr);
2826 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2827 ir_node *result = create_op(dbgi, expression, left, right);
2829 result = create_cast(dbgi, result, expression->right->base.type, type);
2830 result = do_strict_conv(dbgi, result);
2832 result = set_value_for_expression_addr(left_expr, result, left_addr);
2834 if (!is_type_compound(type)) {
2835 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2836 result = create_conv(dbgi, result, mode_arithmetic);
2841 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2843 expression_kind_t kind = expression->base.kind;
2846 case EXPR_BINARY_EQUAL:
2847 case EXPR_BINARY_NOTEQUAL:
2848 case EXPR_BINARY_LESS:
2849 case EXPR_BINARY_LESSEQUAL:
2850 case EXPR_BINARY_GREATER:
2851 case EXPR_BINARY_GREATEREQUAL:
2852 case EXPR_BINARY_ISGREATER:
2853 case EXPR_BINARY_ISGREATEREQUAL:
2854 case EXPR_BINARY_ISLESS:
2855 case EXPR_BINARY_ISLESSEQUAL:
2856 case EXPR_BINARY_ISLESSGREATER:
2857 case EXPR_BINARY_ISUNORDERED: {
2858 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2859 ir_node *left = expression_to_firm(expression->left);
2860 ir_node *right = expression_to_firm(expression->right);
2861 ir_relation relation = get_relation(kind);
2862 ir_node *cmp = new_d_Cmp(dbgi, left, right, relation);
2865 case EXPR_BINARY_ASSIGN: {
2866 ir_node *addr = expression_to_addr(expression->left);
2867 ir_node *right = expression_to_firm(expression->right);
2869 = set_value_for_expression_addr(expression->left, right, addr);
2871 type_t *type = skip_typeref(expression->base.type);
2872 if (!is_type_compound(type)) {
2873 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2874 res = create_conv(NULL, res, mode_arithmetic);
2878 case EXPR_BINARY_ADD:
2879 case EXPR_BINARY_SUB:
2880 case EXPR_BINARY_MUL:
2881 case EXPR_BINARY_DIV:
2882 case EXPR_BINARY_MOD:
2883 case EXPR_BINARY_BITWISE_AND:
2884 case EXPR_BINARY_BITWISE_OR:
2885 case EXPR_BINARY_BITWISE_XOR:
2886 case EXPR_BINARY_SHIFTLEFT:
2887 case EXPR_BINARY_SHIFTRIGHT:
2889 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2890 ir_node *left = expression_to_firm(expression->left);
2891 ir_node *right = expression_to_firm(expression->right);
2892 return create_op(dbgi, expression, left, right);
2894 case EXPR_BINARY_LOGICAL_AND:
2895 case EXPR_BINARY_LOGICAL_OR:
2896 return create_lazy_op(expression);
2897 case EXPR_BINARY_COMMA:
2898 /* create side effects of left side */
2899 (void) expression_to_firm(expression->left);
2900 return _expression_to_firm(expression->right);
2902 case EXPR_BINARY_ADD_ASSIGN:
2903 case EXPR_BINARY_SUB_ASSIGN:
2904 case EXPR_BINARY_MUL_ASSIGN:
2905 case EXPR_BINARY_MOD_ASSIGN:
2906 case EXPR_BINARY_DIV_ASSIGN:
2907 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2908 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2909 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2910 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2911 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2912 return create_assign_binop(expression);
2914 panic("TODO binexpr type");
2918 static ir_node *array_access_addr(const array_access_expression_t *expression)
2920 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2921 ir_node *base_addr = expression_to_firm(expression->array_ref);
2922 ir_node *offset = expression_to_firm(expression->index);
2923 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2924 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2925 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2930 static ir_node *array_access_to_firm(
2931 const array_access_expression_t *expression)
2933 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2934 ir_node *addr = array_access_addr(expression);
2935 type_t *type = revert_automatic_type_conversion(
2936 (const expression_t*) expression);
2937 type = skip_typeref(type);
2939 return deref_address(dbgi, type, addr);
2942 static long get_offsetof_offset(const offsetof_expression_t *expression)
2944 type_t *orig_type = expression->type;
2947 designator_t *designator = expression->designator;
2948 for ( ; designator != NULL; designator = designator->next) {
2949 type_t *type = skip_typeref(orig_type);
2950 /* be sure the type is constructed */
2951 (void) get_ir_type(type);
2953 if (designator->symbol != NULL) {
2954 assert(is_type_compound(type));
2955 symbol_t *symbol = designator->symbol;
2957 compound_t *compound = type->compound.compound;
2958 entity_t *iter = compound->members.entities;
2959 for ( ; iter != NULL; iter = iter->base.next) {
2960 if (iter->base.symbol == symbol) {
2964 assert(iter != NULL);
2966 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2967 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2968 offset += get_entity_offset(iter->compound_member.entity);
2970 orig_type = iter->declaration.type;
2972 expression_t *array_index = designator->array_index;
2973 assert(designator->array_index != NULL);
2974 assert(is_type_array(type));
2976 long index = fold_constant_to_int(array_index);
2977 ir_type *arr_type = get_ir_type(type);
2978 ir_type *elem_type = get_array_element_type(arr_type);
2979 long elem_size = get_type_size_bytes(elem_type);
2981 offset += index * elem_size;
2983 orig_type = type->array.element_type;
2990 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2992 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2993 long offset = get_offsetof_offset(expression);
2994 ir_tarval *tv = new_tarval_from_long(offset, mode);
2995 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2997 return new_d_Const(dbgi, tv);
3000 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3001 ir_entity *entity, type_t *type);
3003 static ir_node *compound_literal_to_firm(
3004 const compound_literal_expression_t *expression)
3006 type_t *type = expression->type;
3008 /* create an entity on the stack */
3009 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3011 ident *const id = id_unique("CompLit.%u");
3012 ir_type *const irtype = get_ir_type(type);
3013 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3014 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
3015 set_entity_ld_ident(entity, id);
3017 /* create initialisation code */
3018 initializer_t *initializer = expression->initializer;
3019 create_local_initializer(initializer, dbgi, entity, type);
3021 /* create a sel for the compound literal address */
3022 ir_node *frame = get_irg_frame(current_ir_graph);
3023 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3028 * Transform a sizeof expression into Firm code.
3030 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3032 type_t *const type = skip_typeref(expression->type);
3033 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3034 if (is_type_array(type) && type->array.is_vla
3035 && expression->tp_expression != NULL) {
3036 expression_to_firm(expression->tp_expression);
3038 /* strange gnu extensions: sizeof(function) == 1 */
3039 if (is_type_function(type)) {
3040 ir_mode *mode = get_ir_mode_storage(type_size_t);
3041 return new_Const(get_mode_one(mode));
3044 return get_type_size_node(type);
3047 static entity_t *get_expression_entity(const expression_t *expression)
3049 if (expression->kind != EXPR_REFERENCE)
3052 return expression->reference.entity;
3055 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3057 switch(entity->kind) {
3058 DECLARATION_KIND_CASES
3059 return entity->declaration.alignment;
3062 return entity->compound.alignment;
3063 case ENTITY_TYPEDEF:
3064 return entity->typedefe.alignment;
3072 * Transform an alignof expression into Firm code.
3074 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3076 unsigned alignment = 0;
3078 const expression_t *tp_expression = expression->tp_expression;
3079 if (tp_expression != NULL) {
3080 entity_t *entity = get_expression_entity(tp_expression);
3081 if (entity != NULL) {
3082 if (entity->kind == ENTITY_FUNCTION) {
3083 /* a gnu-extension */
3086 alignment = get_cparser_entity_alignment(entity);
3091 if (alignment == 0) {
3092 type_t *type = expression->type;
3093 alignment = get_type_alignment(type);
3096 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3097 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3098 ir_tarval *tv = new_tarval_from_long(alignment, mode);
3099 return new_d_Const(dbgi, tv);
3102 static void init_ir_types(void);
3104 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3106 assert(is_type_valid(skip_typeref(expression->base.type)));
3108 bool constant_folding_old = constant_folding;
3109 constant_folding = true;
3113 assert(is_constant_expression(expression) == EXPR_CLASS_CONSTANT);
3115 ir_graph *old_current_ir_graph = current_ir_graph;
3116 current_ir_graph = get_const_code_irg();
3118 ir_node *cnst = expression_to_firm(expression);
3119 current_ir_graph = old_current_ir_graph;
3121 if (!is_Const(cnst)) {
3122 panic("couldn't fold constant");
3125 constant_folding = constant_folding_old;
3127 return get_Const_tarval(cnst);
3130 long fold_constant_to_int(const expression_t *expression)
3132 if (expression->kind == EXPR_INVALID)
3135 ir_tarval *tv = fold_constant_to_tarval(expression);
3136 if (!tarval_is_long(tv)) {
3137 panic("result of constant folding is not integer");
3140 return get_tarval_long(tv);
3143 bool fold_constant_to_bool(const expression_t *expression)
3145 if (expression->kind == EXPR_INVALID)
3147 ir_tarval *tv = fold_constant_to_tarval(expression);
3148 return !tarval_is_null(tv);
3151 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3153 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3155 /* first try to fold a constant condition */
3156 if (is_constant_expression(expression->condition) == EXPR_CLASS_CONSTANT) {
3157 bool val = fold_constant_to_bool(expression->condition);
3159 expression_t *true_expression = expression->true_expression;
3160 if (true_expression == NULL)
3161 true_expression = expression->condition;
3162 return expression_to_firm(true_expression);
3164 return expression_to_firm(expression->false_expression);
3168 ir_node *const true_block = new_immBlock();
3169 ir_node *const false_block = new_immBlock();
3170 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3171 mature_immBlock(true_block);
3172 mature_immBlock(false_block);
3174 set_cur_block(true_block);
3176 if (expression->true_expression != NULL) {
3177 true_val = expression_to_firm(expression->true_expression);
3178 } else if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3179 true_val = cond_expr;
3181 /* Condition ended with a short circuit (&&, ||, !) operation or a
3182 * comparison. Generate a "1" as value for the true branch. */
3183 true_val = new_Const(get_mode_one(mode_Is));
3185 ir_node *const true_jmp = new_d_Jmp(dbgi);
3187 set_cur_block(false_block);
3188 ir_node *const false_val = expression_to_firm(expression->false_expression);
3189 ir_node *const false_jmp = new_d_Jmp(dbgi);
3191 /* create the common block */
3192 ir_node *const in_cf[2] = { true_jmp, false_jmp };
3193 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
3194 set_cur_block(block);
3196 /* TODO improve static semantics, so either both or no values are NULL */
3197 if (true_val == NULL || false_val == NULL)
3200 ir_node *const in[2] = { true_val, false_val };
3201 type_t *const type = skip_typeref(expression->base.type);
3203 if (is_type_compound(type)) {
3206 mode = get_ir_mode_arithmetic(type);
3208 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
3214 * Returns an IR-node representing the address of a field.
3216 static ir_node *select_addr(const select_expression_t *expression)
3218 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3220 construct_select_compound(expression);
3222 ir_node *compound_addr = expression_to_firm(expression->compound);
3224 entity_t *entry = expression->compound_entry;
3225 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3226 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3228 if (constant_folding) {
3229 ir_mode *mode = get_irn_mode(compound_addr);
3230 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3231 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3232 return new_d_Add(dbgi, compound_addr, ofs, mode);
3234 ir_entity *irentity = entry->compound_member.entity;
3235 assert(irentity != NULL);
3236 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3240 static ir_node *select_to_firm(const select_expression_t *expression)
3242 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3243 ir_node *addr = select_addr(expression);
3244 type_t *type = revert_automatic_type_conversion(
3245 (const expression_t*) expression);
3246 type = skip_typeref(type);
3248 entity_t *entry = expression->compound_entry;
3249 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3250 type_t *entry_type = skip_typeref(entry->declaration.type);
3252 if (entry_type->kind == TYPE_BITFIELD) {
3253 return bitfield_extract_to_firm(expression, addr);
3256 return deref_address(dbgi, type, addr);
3259 /* Values returned by __builtin_classify_type. */
3260 typedef enum gcc_type_class
3266 enumeral_type_class,
3269 reference_type_class,
3273 function_type_class,
3284 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3286 type_t *type = expr->type_expression->base.type;
3288 /* FIXME gcc returns different values depending on whether compiling C or C++
3289 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3292 type = skip_typeref(type);
3293 switch (type->kind) {
3295 const atomic_type_t *const atomic_type = &type->atomic;
3296 switch (atomic_type->akind) {
3297 /* should not be reached */
3298 case ATOMIC_TYPE_INVALID:
3302 /* gcc cannot do that */
3303 case ATOMIC_TYPE_VOID:
3304 tc = void_type_class;
3307 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3308 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3309 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3310 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3311 case ATOMIC_TYPE_SHORT:
3312 case ATOMIC_TYPE_USHORT:
3313 case ATOMIC_TYPE_INT:
3314 case ATOMIC_TYPE_UINT:
3315 case ATOMIC_TYPE_LONG:
3316 case ATOMIC_TYPE_ULONG:
3317 case ATOMIC_TYPE_LONGLONG:
3318 case ATOMIC_TYPE_ULONGLONG:
3319 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3320 tc = integer_type_class;
3323 case ATOMIC_TYPE_FLOAT:
3324 case ATOMIC_TYPE_DOUBLE:
3325 case ATOMIC_TYPE_LONG_DOUBLE:
3326 tc = real_type_class;
3329 panic("Unexpected atomic type in classify_type_to_firm().");
3332 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3333 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3334 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3335 case TYPE_ARRAY: /* gcc handles this as pointer */
3336 case TYPE_FUNCTION: /* gcc handles this as pointer */
3337 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3338 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3339 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3341 /* gcc handles this as integer */
3342 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3344 /* gcc classifies the referenced type */
3345 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3348 /* typedef/typeof should be skipped already */
3355 panic("unexpected TYPE classify_type_to_firm().");
3359 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3360 ir_tarval *const tv = new_tarval_from_long(tc, mode_int);
3361 return new_d_Const(dbgi, tv);
3364 static ir_node *function_name_to_firm(
3365 const funcname_expression_t *const expr)
3367 switch(expr->kind) {
3368 case FUNCNAME_FUNCTION:
3369 case FUNCNAME_PRETTY_FUNCTION:
3370 case FUNCNAME_FUNCDNAME:
3371 if (current_function_name == NULL) {
3372 const source_position_t *const src_pos = &expr->base.source_position;
3373 const char *name = current_function_entity->base.symbol->string;
3374 const string_t string = { name, strlen(name) + 1 };
3375 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3377 return current_function_name;
3378 case FUNCNAME_FUNCSIG:
3379 if (current_funcsig == NULL) {
3380 const source_position_t *const src_pos = &expr->base.source_position;
3381 ir_entity *ent = get_irg_entity(current_ir_graph);
3382 const char *const name = get_entity_ld_name(ent);
3383 const string_t string = { name, strlen(name) + 1 };
3384 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3386 return current_funcsig;
3388 panic("Unsupported function name");
3391 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3393 statement_t *statement = expr->statement;
3395 assert(statement->kind == STATEMENT_COMPOUND);
3396 return compound_statement_to_firm(&statement->compound);
3399 static ir_node *va_start_expression_to_firm(
3400 const va_start_expression_t *const expr)
3402 type_t *const type = current_function_entity->declaration.type;
3403 ir_type *const method_type = get_ir_type(type);
3404 int const n = get_method_n_params(method_type) - 1;
3405 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3406 ir_node *const frame = get_irg_frame(current_ir_graph);
3407 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3408 ir_node *const no_mem = new_NoMem();
3409 ir_node *const arg_sel =
3410 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3412 type_t *const param_type = expr->parameter->base.type;
3413 ir_node *const cnst = get_type_size_node(param_type);
3414 ir_mode *const mode = get_irn_mode(cnst);
3415 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3416 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3417 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3418 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3419 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3420 set_value_for_expression(expr->ap, add);
3425 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3427 type_t *const type = expr->base.type;
3428 expression_t *const ap_expr = expr->ap;
3429 ir_node *const ap_addr = expression_to_addr(ap_expr);
3430 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3431 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3432 ir_node *const res = deref_address(dbgi, type, ap);
3434 ir_node *const cnst = get_type_size_node(expr->base.type);
3435 ir_mode *const mode = get_irn_mode(cnst);
3436 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3437 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3438 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3439 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3440 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3442 set_value_for_expression_addr(ap_expr, add, ap_addr);
3448 * Generate Firm for a va_copy expression.
3450 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3452 ir_node *const src = expression_to_firm(expr->src);
3453 set_value_for_expression(expr->dst, src);
3457 static ir_node *dereference_addr(const unary_expression_t *const expression)
3459 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3460 return expression_to_firm(expression->value);
3464 * Returns a IR-node representing an lvalue of the given expression.
3466 static ir_node *expression_to_addr(const expression_t *expression)
3468 switch(expression->kind) {
3469 case EXPR_ARRAY_ACCESS:
3470 return array_access_addr(&expression->array_access);
3472 return call_expression_to_firm(&expression->call);
3473 case EXPR_COMPOUND_LITERAL:
3474 return compound_literal_to_firm(&expression->compound_literal);
3475 case EXPR_REFERENCE:
3476 return reference_addr(&expression->reference);
3478 return select_addr(&expression->select);
3479 case EXPR_UNARY_DEREFERENCE:
3480 return dereference_addr(&expression->unary);
3484 panic("trying to get address of non-lvalue");
3487 static ir_node *builtin_constant_to_firm(
3488 const builtin_constant_expression_t *expression)
3490 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3491 bool const v = is_constant_expression(expression->value) == EXPR_CLASS_CONSTANT;
3492 return create_Const_from_bool(mode, v);
3495 static ir_node *builtin_types_compatible_to_firm(
3496 const builtin_types_compatible_expression_t *expression)
3498 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3499 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3500 bool const value = types_compatible(left, right);
3501 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3502 return create_Const_from_bool(mode, value);
3505 static ir_node *get_label_block(label_t *label)
3507 if (label->block != NULL)
3508 return label->block;
3510 /* beware: might be called from create initializer with current_ir_graph
3511 * set to const_code_irg. */
3512 ir_graph *rem = current_ir_graph;
3513 current_ir_graph = current_function;
3515 ir_node *block = new_immBlock();
3517 label->block = block;
3519 ARR_APP1(label_t *, all_labels, label);
3521 current_ir_graph = rem;
3526 * Pointer to a label. This is used for the
3527 * GNU address-of-label extension.
3529 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3531 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3532 ir_node *block = get_label_block(label->label);
3533 ir_entity *entity = create_Block_entity(block);
3535 symconst_symbol value;
3536 value.entity_p = entity;
3537 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3541 * creates firm nodes for an expression. The difference between this function
3542 * and expression_to_firm is, that this version might produce mode_b nodes
3543 * instead of mode_Is.
3545 static ir_node *_expression_to_firm(const expression_t *expression)
3548 if (!constant_folding) {
3549 assert(!expression->base.transformed);
3550 ((expression_t*) expression)->base.transformed = true;
3554 switch (expression->kind) {
3556 return literal_to_firm(&expression->literal);
3557 case EXPR_STRING_LITERAL:
3558 return string_to_firm(&expression->base.source_position, "str.%u",
3559 &expression->literal.value);
3560 case EXPR_WIDE_STRING_LITERAL:
3561 return wide_string_literal_to_firm(&expression->string_literal);
3562 case EXPR_REFERENCE:
3563 return reference_expression_to_firm(&expression->reference);
3564 case EXPR_REFERENCE_ENUM_VALUE:
3565 return reference_expression_enum_value_to_firm(&expression->reference);
3567 return call_expression_to_firm(&expression->call);
3569 return unary_expression_to_firm(&expression->unary);
3571 return binary_expression_to_firm(&expression->binary);
3572 case EXPR_ARRAY_ACCESS:
3573 return array_access_to_firm(&expression->array_access);
3575 return sizeof_to_firm(&expression->typeprop);
3577 return alignof_to_firm(&expression->typeprop);
3578 case EXPR_CONDITIONAL:
3579 return conditional_to_firm(&expression->conditional);
3581 return select_to_firm(&expression->select);
3582 case EXPR_CLASSIFY_TYPE:
3583 return classify_type_to_firm(&expression->classify_type);
3585 return function_name_to_firm(&expression->funcname);
3586 case EXPR_STATEMENT:
3587 return statement_expression_to_firm(&expression->statement);
3589 return va_start_expression_to_firm(&expression->va_starte);
3591 return va_arg_expression_to_firm(&expression->va_arge);
3593 return va_copy_expression_to_firm(&expression->va_copye);
3594 case EXPR_BUILTIN_CONSTANT_P:
3595 return builtin_constant_to_firm(&expression->builtin_constant);
3596 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3597 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3599 return offsetof_to_firm(&expression->offsetofe);
3600 case EXPR_COMPOUND_LITERAL:
3601 return compound_literal_to_firm(&expression->compound_literal);
3602 case EXPR_LABEL_ADDRESS:
3603 return label_address_to_firm(&expression->label_address);
3609 panic("invalid expression found");
3613 * Check if a given expression is a GNU __builtin_expect() call.
3615 static bool is_builtin_expect(const expression_t *expression)
3617 if (expression->kind != EXPR_CALL)
3620 expression_t *function = expression->call.function;
3621 if (function->kind != EXPR_REFERENCE)
3623 reference_expression_t *ref = &function->reference;
3624 if (ref->entity->kind != ENTITY_FUNCTION ||
3625 ref->entity->function.btk != bk_gnu_builtin_expect)
3631 static bool produces_mode_b(const expression_t *expression)
3633 switch (expression->kind) {
3634 case EXPR_BINARY_EQUAL:
3635 case EXPR_BINARY_NOTEQUAL:
3636 case EXPR_BINARY_LESS:
3637 case EXPR_BINARY_LESSEQUAL:
3638 case EXPR_BINARY_GREATER:
3639 case EXPR_BINARY_GREATEREQUAL:
3640 case EXPR_BINARY_ISGREATER:
3641 case EXPR_BINARY_ISGREATEREQUAL:
3642 case EXPR_BINARY_ISLESS:
3643 case EXPR_BINARY_ISLESSEQUAL:
3644 case EXPR_BINARY_ISLESSGREATER:
3645 case EXPR_BINARY_ISUNORDERED:
3646 case EXPR_UNARY_NOT:
3650 if (is_builtin_expect(expression)) {
3651 expression_t *argument = expression->call.arguments->expression;
3652 return produces_mode_b(argument);
3655 case EXPR_BINARY_COMMA:
3656 return produces_mode_b(expression->binary.right);
3663 static ir_node *expression_to_firm(const expression_t *expression)
3665 if (!produces_mode_b(expression)) {
3666 ir_node *res = _expression_to_firm(expression);
3667 assert(res == NULL || get_irn_mode(res) != mode_b);
3671 if (is_constant_expression(expression) == EXPR_CLASS_CONSTANT) {
3672 bool const constant_folding_old = constant_folding;
3673 constant_folding = true;
3674 ir_node *res = _expression_to_firm(expression);
3675 constant_folding = constant_folding_old;
3676 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3677 assert(is_Const(res));
3678 return create_Const_from_bool(mode, !is_Const_null(res));
3681 /* we have to produce a 0/1 from the mode_b expression */
3682 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3683 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3684 return produce_condition_result(expression, mode, dbgi);
3688 * create a short-circuit expression evaluation that tries to construct
3689 * efficient control flow structures for &&, || and ! expressions
3691 static ir_node *create_condition_evaluation(const expression_t *expression,
3692 ir_node *true_block,
3693 ir_node *false_block)
3695 switch(expression->kind) {
3696 case EXPR_UNARY_NOT: {
3697 const unary_expression_t *unary_expression = &expression->unary;
3698 create_condition_evaluation(unary_expression->value, false_block,
3702 case EXPR_BINARY_LOGICAL_AND: {
3703 const binary_expression_t *binary_expression = &expression->binary;
3705 ir_node *extra_block = new_immBlock();
3706 create_condition_evaluation(binary_expression->left, extra_block,
3708 mature_immBlock(extra_block);
3709 set_cur_block(extra_block);
3710 create_condition_evaluation(binary_expression->right, true_block,
3714 case EXPR_BINARY_LOGICAL_OR: {
3715 const binary_expression_t *binary_expression = &expression->binary;
3717 ir_node *extra_block = new_immBlock();
3718 create_condition_evaluation(binary_expression->left, true_block,
3720 mature_immBlock(extra_block);
3721 set_cur_block(extra_block);
3722 create_condition_evaluation(binary_expression->right, true_block,
3730 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3731 ir_node *cond_expr = _expression_to_firm(expression);
3732 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3733 ir_node *cond = new_d_Cond(dbgi, condition);
3734 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3735 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3737 /* set branch prediction info based on __builtin_expect */
3738 if (is_builtin_expect(expression) && is_Cond(cond)) {
3739 call_argument_t *argument = expression->call.arguments->next;
3740 if (is_constant_expression(argument->expression) == EXPR_CLASS_CONSTANT) {
3741 bool cnst = fold_constant_to_bool(argument->expression);
3742 cond_jmp_predicate pred;
3744 if (cnst == false) {
3745 pred = COND_JMP_PRED_FALSE;
3747 pred = COND_JMP_PRED_TRUE;
3749 set_Cond_jmp_pred(cond, pred);
3753 add_immBlock_pred(true_block, true_proj);
3754 add_immBlock_pred(false_block, false_proj);
3756 set_cur_block(NULL);
3760 static void create_variable_entity(entity_t *variable,
3761 declaration_kind_t declaration_kind,
3762 ir_type *parent_type)
3764 assert(variable->kind == ENTITY_VARIABLE);
3765 type_t *type = skip_typeref(variable->declaration.type);
3767 ident *const id = new_id_from_str(variable->base.symbol->string);
3768 ir_type *const irtype = get_ir_type(type);
3769 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3770 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3771 unsigned alignment = variable->declaration.alignment;
3773 set_entity_alignment(irentity, alignment);
3775 handle_decl_modifiers(irentity, variable);
3777 variable->declaration.kind = (unsigned char) declaration_kind;
3778 variable->variable.v.entity = irentity;
3779 set_entity_ld_ident(irentity, create_ld_ident(variable));
3781 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3782 set_entity_volatility(irentity, volatility_is_volatile);
3787 typedef struct type_path_entry_t type_path_entry_t;
3788 struct type_path_entry_t {
3790 ir_initializer_t *initializer;
3792 entity_t *compound_entry;
3795 typedef struct type_path_t type_path_t;
3796 struct type_path_t {
3797 type_path_entry_t *path;
3802 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3804 size_t len = ARR_LEN(path->path);
3806 for (size_t i = 0; i < len; ++i) {
3807 const type_path_entry_t *entry = & path->path[i];
3809 type_t *type = skip_typeref(entry->type);
3810 if (is_type_compound(type)) {
3811 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3812 } else if (is_type_array(type)) {
3813 fprintf(stderr, "[%u]", (unsigned) entry->index);
3815 fprintf(stderr, "-INVALID-");
3818 fprintf(stderr, " (");
3819 print_type(path->top_type);
3820 fprintf(stderr, ")");
3823 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3825 size_t len = ARR_LEN(path->path);
3827 return & path->path[len-1];
3830 static type_path_entry_t *append_to_type_path(type_path_t *path)
3832 size_t len = ARR_LEN(path->path);
3833 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3835 type_path_entry_t *result = & path->path[len];
3836 memset(result, 0, sizeof(result[0]));
3840 static size_t get_compound_member_count(const compound_type_t *type)
3842 compound_t *compound = type->compound;
3843 size_t n_members = 0;
3844 entity_t *member = compound->members.entities;
3845 for ( ; member != NULL; member = member->base.next) {
3852 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3854 type_t *orig_top_type = path->top_type;
3855 type_t *top_type = skip_typeref(orig_top_type);
3857 assert(is_type_compound(top_type) || is_type_array(top_type));
3859 if (ARR_LEN(path->path) == 0) {
3862 type_path_entry_t *top = get_type_path_top(path);
3863 ir_initializer_t *initializer = top->initializer;
3864 return get_initializer_compound_value(initializer, top->index);
3868 static void descend_into_subtype(type_path_t *path)
3870 type_t *orig_top_type = path->top_type;
3871 type_t *top_type = skip_typeref(orig_top_type);
3873 assert(is_type_compound(top_type) || is_type_array(top_type));
3875 ir_initializer_t *initializer = get_initializer_entry(path);
3877 type_path_entry_t *top = append_to_type_path(path);
3878 top->type = top_type;
3882 if (is_type_compound(top_type)) {
3883 compound_t *compound = top_type->compound.compound;
3884 entity_t *entry = compound->members.entities;
3886 top->compound_entry = entry;
3888 len = get_compound_member_count(&top_type->compound);
3889 if (entry != NULL) {
3890 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3891 path->top_type = entry->declaration.type;
3894 assert(is_type_array(top_type));
3895 assert(top_type->array.size > 0);
3898 path->top_type = top_type->array.element_type;
3899 len = top_type->array.size;
3901 if (initializer == NULL
3902 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3903 initializer = create_initializer_compound(len);
3904 /* we have to set the entry at the 2nd latest path entry... */
3905 size_t path_len = ARR_LEN(path->path);
3906 assert(path_len >= 1);
3908 type_path_entry_t *entry = & path->path[path_len-2];
3909 ir_initializer_t *tinitializer = entry->initializer;
3910 set_initializer_compound_value(tinitializer, entry->index,
3914 top->initializer = initializer;
3917 static void ascend_from_subtype(type_path_t *path)
3919 type_path_entry_t *top = get_type_path_top(path);
3921 path->top_type = top->type;
3923 size_t len = ARR_LEN(path->path);
3924 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3927 static void walk_designator(type_path_t *path, const designator_t *designator)
3929 /* designators start at current object type */
3930 ARR_RESIZE(type_path_entry_t, path->path, 1);
3932 for ( ; designator != NULL; designator = designator->next) {
3933 type_path_entry_t *top = get_type_path_top(path);
3934 type_t *orig_type = top->type;
3935 type_t *type = skip_typeref(orig_type);
3937 if (designator->symbol != NULL) {
3938 assert(is_type_compound(type));
3940 symbol_t *symbol = designator->symbol;
3942 compound_t *compound = type->compound.compound;
3943 entity_t *iter = compound->members.entities;
3944 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3945 if (iter->base.symbol == symbol) {
3946 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3950 assert(iter != NULL);
3952 /* revert previous initialisations of other union elements */
3953 if (type->kind == TYPE_COMPOUND_UNION) {
3954 ir_initializer_t *initializer = top->initializer;
3955 if (initializer != NULL
3956 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3957 /* are we writing to a new element? */
3958 ir_initializer_t *oldi
3959 = get_initializer_compound_value(initializer, index);
3960 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3961 /* clear initializer */
3963 = get_initializer_compound_n_entries(initializer);
3964 ir_initializer_t *nulli = get_initializer_null();
3965 for (size_t i = 0; i < len; ++i) {
3966 set_initializer_compound_value(initializer, i,
3973 top->type = orig_type;
3974 top->compound_entry = iter;
3976 orig_type = iter->declaration.type;
3978 expression_t *array_index = designator->array_index;
3979 assert(designator->array_index != NULL);
3980 assert(is_type_array(type));
3982 long index = fold_constant_to_int(array_index);
3985 if (type->array.size_constant) {
3986 long array_size = type->array.size;
3987 assert(index < array_size);
3991 top->type = orig_type;
3992 top->index = (size_t) index;
3993 orig_type = type->array.element_type;
3995 path->top_type = orig_type;
3997 if (designator->next != NULL) {
3998 descend_into_subtype(path);
4002 path->invalid = false;
4005 static void advance_current_object(type_path_t *path)
4007 if (path->invalid) {
4008 /* TODO: handle this... */
4009 panic("invalid initializer in ast2firm (excessive elements)");
4012 type_path_entry_t *top = get_type_path_top(path);
4014 type_t *type = skip_typeref(top->type);
4015 if (is_type_union(type)) {
4016 /* only the first element is initialized in unions */
4017 top->compound_entry = NULL;
4018 } else if (is_type_struct(type)) {
4019 entity_t *entry = top->compound_entry;
4022 entry = entry->base.next;
4023 top->compound_entry = entry;
4024 if (entry != NULL) {
4025 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
4026 path->top_type = entry->declaration.type;
4030 assert(is_type_array(type));
4033 if (!type->array.size_constant || top->index < type->array.size) {
4038 /* we're past the last member of the current sub-aggregate, try if we
4039 * can ascend in the type hierarchy and continue with another subobject */
4040 size_t len = ARR_LEN(path->path);
4043 ascend_from_subtype(path);
4044 advance_current_object(path);
4046 path->invalid = true;
4051 static ir_initializer_t *create_ir_initializer(
4052 const initializer_t *initializer, type_t *type);
4054 static ir_initializer_t *create_ir_initializer_value(
4055 const initializer_value_t *initializer)
4057 if (is_type_compound(initializer->value->base.type)) {
4058 panic("initializer creation for compounds not implemented yet");
4060 type_t *type = initializer->value->base.type;
4061 expression_t *expr = initializer->value;
4062 if (initializer_use_bitfield_basetype) {
4063 type_t *skipped = skip_typeref(type);
4064 if (skipped->kind == TYPE_BITFIELD) {
4065 /* remove the bitfield cast... */
4066 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4067 expr = expr->unary.value;
4068 type = skipped->bitfield.base_type;
4071 ir_node *value = expression_to_firm(expr);
4072 ir_mode *mode = get_ir_mode_storage(type);
4073 value = create_conv(NULL, value, mode);
4074 return create_initializer_const(value);
4077 /** test wether type can be initialized by a string constant */
4078 static bool is_string_type(type_t *type)
4081 if (is_type_pointer(type)) {
4082 inner = skip_typeref(type->pointer.points_to);
4083 } else if(is_type_array(type)) {
4084 inner = skip_typeref(type->array.element_type);
4089 return is_type_integer(inner);
4092 static ir_initializer_t *create_ir_initializer_list(
4093 const initializer_list_t *initializer, type_t *type)
4096 memset(&path, 0, sizeof(path));
4097 path.top_type = type;
4098 path.path = NEW_ARR_F(type_path_entry_t, 0);
4100 descend_into_subtype(&path);
4102 for (size_t i = 0; i < initializer->len; ++i) {
4103 const initializer_t *sub_initializer = initializer->initializers[i];
4105 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4106 walk_designator(&path, sub_initializer->designator.designator);
4110 if (sub_initializer->kind == INITIALIZER_VALUE) {
4111 /* we might have to descend into types until we're at a scalar
4114 type_t *orig_top_type = path.top_type;
4115 type_t *top_type = skip_typeref(orig_top_type);
4117 if (is_type_scalar(top_type))
4119 descend_into_subtype(&path);
4121 } else if (sub_initializer->kind == INITIALIZER_STRING
4122 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4123 /* we might have to descend into types until we're at a scalar
4126 type_t *orig_top_type = path.top_type;
4127 type_t *top_type = skip_typeref(orig_top_type);
4129 if (is_string_type(top_type))
4131 descend_into_subtype(&path);
4135 ir_initializer_t *sub_irinitializer
4136 = create_ir_initializer(sub_initializer, path.top_type);
4138 size_t path_len = ARR_LEN(path.path);
4139 assert(path_len >= 1);
4140 type_path_entry_t *entry = & path.path[path_len-1];
4141 ir_initializer_t *tinitializer = entry->initializer;
4142 set_initializer_compound_value(tinitializer, entry->index,
4145 advance_current_object(&path);
4148 assert(ARR_LEN(path.path) >= 1);
4149 ir_initializer_t *result = path.path[0].initializer;
4150 DEL_ARR_F(path.path);
4155 static ir_initializer_t *create_ir_initializer_string(
4156 const initializer_string_t *initializer, type_t *type)
4158 type = skip_typeref(type);
4160 size_t string_len = initializer->string.size;
4161 assert(type->kind == TYPE_ARRAY);
4162 assert(type->array.size_constant);
4163 size_t len = type->array.size;
4164 ir_initializer_t *irinitializer = create_initializer_compound(len);
4166 const char *string = initializer->string.begin;
4167 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4169 for (size_t i = 0; i < len; ++i) {
4174 ir_tarval *tv = new_tarval_from_long(c, mode);
4175 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4177 set_initializer_compound_value(irinitializer, i, char_initializer);
4180 return irinitializer;
4183 static ir_initializer_t *create_ir_initializer_wide_string(
4184 const initializer_wide_string_t *initializer, type_t *type)
4186 assert(type->kind == TYPE_ARRAY);
4187 assert(type->array.size_constant);
4188 size_t len = type->array.size;
4189 size_t string_len = wstrlen(&initializer->string);
4190 ir_initializer_t *irinitializer = create_initializer_compound(len);
4192 const char *p = initializer->string.begin;
4193 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4195 for (size_t i = 0; i < len; ++i) {
4197 if (i < string_len) {
4198 c = read_utf8_char(&p);
4200 ir_tarval *tv = new_tarval_from_long(c, mode);
4201 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4203 set_initializer_compound_value(irinitializer, i, char_initializer);
4206 return irinitializer;
4209 static ir_initializer_t *create_ir_initializer(
4210 const initializer_t *initializer, type_t *type)
4212 switch(initializer->kind) {
4213 case INITIALIZER_STRING:
4214 return create_ir_initializer_string(&initializer->string, type);
4216 case INITIALIZER_WIDE_STRING:
4217 return create_ir_initializer_wide_string(&initializer->wide_string,
4220 case INITIALIZER_LIST:
4221 return create_ir_initializer_list(&initializer->list, type);
4223 case INITIALIZER_VALUE:
4224 return create_ir_initializer_value(&initializer->value);
4226 case INITIALIZER_DESIGNATOR:
4227 panic("unexpected designator initializer found");
4229 panic("unknown initializer");
4232 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4233 * are elements [...] the remainder of the aggregate shall be initialized
4234 * implicitly the same as objects that have static storage duration. */
4235 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4238 /* for unions we must NOT do anything for null initializers */
4239 ir_type *owner = get_entity_owner(entity);
4240 if (is_Union_type(owner)) {
4244 ir_type *ent_type = get_entity_type(entity);
4245 /* create sub-initializers for a compound type */
4246 if (is_compound_type(ent_type)) {
4247 unsigned n_members = get_compound_n_members(ent_type);
4248 for (unsigned n = 0; n < n_members; ++n) {
4249 ir_entity *member = get_compound_member(ent_type, n);
4250 ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4252 create_dynamic_null_initializer(member, dbgi, addr);
4256 if (is_Array_type(ent_type)) {
4257 assert(has_array_upper_bound(ent_type, 0));
4258 long n = get_array_upper_bound_int(ent_type, 0);
4259 for (long i = 0; i < n; ++i) {
4260 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4261 ir_node *cnst = new_d_Const(dbgi, index_tv);
4262 ir_node *in[1] = { cnst };
4263 ir_entity *arrent = get_array_element_entity(ent_type);
4264 ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4266 create_dynamic_null_initializer(arrent, dbgi, addr);
4271 ir_mode *value_mode = get_type_mode(ent_type);
4272 ir_node *node = new_Const(get_mode_null(value_mode));
4274 /* is it a bitfield type? */
4275 if (is_Primitive_type(ent_type) &&
4276 get_primitive_base_type(ent_type) != NULL) {
4277 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4281 ir_node *mem = get_store();
4282 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4283 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4287 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4288 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4290 switch(get_initializer_kind(initializer)) {
4291 case IR_INITIALIZER_NULL:
4292 create_dynamic_null_initializer(entity, dbgi, base_addr);
4294 case IR_INITIALIZER_CONST: {
4295 ir_node *node = get_initializer_const_value(initializer);
4296 ir_type *ent_type = get_entity_type(entity);
4298 /* is it a bitfield type? */
4299 if (is_Primitive_type(ent_type) &&
4300 get_primitive_base_type(ent_type) != NULL) {
4301 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4305 assert(get_type_mode(type) == get_irn_mode(node));
4306 ir_node *mem = get_store();
4307 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4308 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4312 case IR_INITIALIZER_TARVAL: {
4313 ir_tarval *tv = get_initializer_tarval_value(initializer);
4314 ir_node *cnst = new_d_Const(dbgi, tv);
4315 ir_type *ent_type = get_entity_type(entity);
4317 /* is it a bitfield type? */
4318 if (is_Primitive_type(ent_type) &&
4319 get_primitive_base_type(ent_type) != NULL) {
4320 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4324 assert(get_type_mode(type) == get_tarval_mode(tv));
4325 ir_node *mem = get_store();
4326 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4327 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4331 case IR_INITIALIZER_COMPOUND: {
4332 assert(is_compound_type(type) || is_Array_type(type));
4334 if (is_Array_type(type)) {
4335 assert(has_array_upper_bound(type, 0));
4336 n_members = get_array_upper_bound_int(type, 0);
4338 n_members = get_compound_n_members(type);
4341 if (get_initializer_compound_n_entries(initializer)
4342 != (unsigned) n_members)
4343 panic("initializer doesn't match compound type");
4345 for (int i = 0; i < n_members; ++i) {
4348 ir_entity *sub_entity;
4349 if (is_Array_type(type)) {
4350 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4351 ir_node *cnst = new_d_Const(dbgi, index_tv);
4352 ir_node *in[1] = { cnst };
4353 irtype = get_array_element_type(type);
4354 sub_entity = get_array_element_entity(type);
4355 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4358 sub_entity = get_compound_member(type, i);
4359 irtype = get_entity_type(sub_entity);
4360 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4364 ir_initializer_t *sub_init
4365 = get_initializer_compound_value(initializer, i);
4367 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4374 panic("invalid IR_INITIALIZER found");
4377 static void create_dynamic_initializer(ir_initializer_t *initializer,
4378 dbg_info *dbgi, ir_entity *entity)
4380 ir_node *frame = get_irg_frame(current_ir_graph);
4381 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4382 ir_type *type = get_entity_type(entity);
4384 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4387 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4388 ir_entity *entity, type_t *type)
4390 ir_node *memory = get_store();
4391 ir_node *nomem = new_NoMem();
4392 ir_node *frame = get_irg_frame(current_ir_graph);
4393 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4395 if (initializer->kind == INITIALIZER_VALUE) {
4396 initializer_value_t *initializer_value = &initializer->value;
4398 ir_node *value = expression_to_firm(initializer_value->value);
4399 type = skip_typeref(type);
4400 assign_value(dbgi, addr, type, value);
4404 if (is_constant_initializer(initializer) == EXPR_CLASS_VARIABLE) {
4405 bool old_initializer_use_bitfield_basetype
4406 = initializer_use_bitfield_basetype;
4407 initializer_use_bitfield_basetype = true;
4408 ir_initializer_t *irinitializer
4409 = create_ir_initializer(initializer, type);
4410 initializer_use_bitfield_basetype
4411 = old_initializer_use_bitfield_basetype;
4413 create_dynamic_initializer(irinitializer, dbgi, entity);
4417 /* create the ir_initializer */
4418 ir_graph *const old_current_ir_graph = current_ir_graph;
4419 current_ir_graph = get_const_code_irg();
4421 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4423 assert(current_ir_graph == get_const_code_irg());
4424 current_ir_graph = old_current_ir_graph;
4426 /* create a "template" entity which is copied to the entity on the stack */
4427 ident *const id = id_unique("initializer.%u");
4428 ir_type *const irtype = get_ir_type(type);
4429 ir_type *const global_type = get_glob_type();
4430 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4431 set_entity_ld_ident(init_entity, id);
4433 set_entity_visibility(init_entity, ir_visibility_private);
4434 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4436 set_entity_initializer(init_entity, irinitializer);
4438 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4439 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4441 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4442 set_store(copyb_mem);
4445 static void create_initializer_local_variable_entity(entity_t *entity)
4447 assert(entity->kind == ENTITY_VARIABLE);
4448 initializer_t *initializer = entity->variable.initializer;
4449 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4450 ir_entity *irentity = entity->variable.v.entity;
4451 type_t *type = entity->declaration.type;
4453 create_local_initializer(initializer, dbgi, irentity, type);
4456 static void create_variable_initializer(entity_t *entity)
4458 assert(entity->kind == ENTITY_VARIABLE);
4459 initializer_t *initializer = entity->variable.initializer;
4460 if (initializer == NULL)
4463 declaration_kind_t declaration_kind
4464 = (declaration_kind_t) entity->declaration.kind;
4465 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4466 create_initializer_local_variable_entity(entity);
4470 type_t *type = entity->declaration.type;
4471 type_qualifiers_t tq = get_type_qualifier(type, true);
4473 if (initializer->kind == INITIALIZER_VALUE) {
4474 initializer_value_t *initializer_value = &initializer->value;
4475 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4477 ir_node *value = expression_to_firm(initializer_value->value);
4479 type_t *type = initializer_value->value->base.type;
4480 ir_mode *mode = get_ir_mode_storage(type);
4481 value = create_conv(dbgi, value, mode);
4482 value = do_strict_conv(dbgi, value);
4484 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4485 set_value(entity->variable.v.value_number, value);
4487 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4489 ir_entity *irentity = entity->variable.v.entity;
4491 if (tq & TYPE_QUALIFIER_CONST
4492 && get_entity_owner(irentity) != get_tls_type()) {
4493 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4495 set_atomic_ent_value(irentity, value);
4498 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4499 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4501 ir_entity *irentity = entity->variable.v.entity;
4502 ir_initializer_t *irinitializer
4503 = create_ir_initializer(initializer, type);
4505 if (tq & TYPE_QUALIFIER_CONST) {
4506 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4508 set_entity_initializer(irentity, irinitializer);
4512 static void create_variable_length_array(entity_t *entity)
4514 assert(entity->kind == ENTITY_VARIABLE);
4515 assert(entity->variable.initializer == NULL);
4517 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4518 entity->variable.v.vla_base = NULL;
4520 /* TODO: record VLA somewhere so we create the free node when we leave
4524 static void allocate_variable_length_array(entity_t *entity)
4526 assert(entity->kind == ENTITY_VARIABLE);
4527 assert(entity->variable.initializer == NULL);
4528 assert(get_cur_block() != NULL);
4530 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4531 type_t *type = entity->declaration.type;
4532 ir_type *el_type = get_ir_type(type->array.element_type);
4534 /* make sure size_node is calculated */
4535 get_type_size_node(type);
4536 ir_node *elems = type->array.size_node;
4537 ir_node *mem = get_store();
4538 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4540 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4541 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4544 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4545 entity->variable.v.vla_base = addr;
4549 * Creates a Firm local variable from a declaration.
4551 static void create_local_variable(entity_t *entity)
4553 assert(entity->kind == ENTITY_VARIABLE);
4554 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4556 bool needs_entity = entity->variable.address_taken;
4557 type_t *type = skip_typeref(entity->declaration.type);
4559 /* is it a variable length array? */
4560 if (is_type_array(type) && !type->array.size_constant) {
4561 create_variable_length_array(entity);
4563 } else if (is_type_array(type) || is_type_compound(type)) {
4564 needs_entity = true;
4565 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4566 needs_entity = true;
4570 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4571 create_variable_entity(entity,
4572 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4575 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4576 entity->variable.v.value_number = next_value_number_function;
4577 set_irg_loc_description(current_ir_graph, next_value_number_function,
4579 ++next_value_number_function;
4583 static void create_local_static_variable(entity_t *entity)
4585 assert(entity->kind == ENTITY_VARIABLE);
4586 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4588 type_t *type = skip_typeref(entity->declaration.type);
4589 ir_type *const var_type = entity->variable.thread_local ?
4590 get_tls_type() : get_glob_type();
4591 ir_type *const irtype = get_ir_type(type);
4592 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4594 size_t l = strlen(entity->base.symbol->string);
4595 char buf[l + sizeof(".%u")];
4596 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4597 ident *const id = id_unique(buf);
4598 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4600 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4601 set_entity_volatility(irentity, volatility_is_volatile);
4604 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4605 entity->variable.v.entity = irentity;
4607 set_entity_ld_ident(irentity, id);
4608 set_entity_visibility(irentity, ir_visibility_local);
4610 ir_graph *const old_current_ir_graph = current_ir_graph;
4611 current_ir_graph = get_const_code_irg();
4613 create_variable_initializer(entity);
4615 assert(current_ir_graph == get_const_code_irg());
4616 current_ir_graph = old_current_ir_graph;
4621 static void return_statement_to_firm(return_statement_t *statement)
4623 if (get_cur_block() == NULL)
4626 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4627 type_t *type = current_function_entity->declaration.type;
4628 ir_type *func_irtype = get_ir_type(type);
4633 if (get_method_n_ress(func_irtype) > 0) {
4634 ir_type *res_type = get_method_res_type(func_irtype, 0);
4636 if (statement->value != NULL) {
4637 ir_node *node = expression_to_firm(statement->value);
4638 if (!is_compound_type(res_type)) {
4639 type_t *type = statement->value->base.type;
4640 ir_mode *mode = get_ir_mode_storage(type);
4641 node = create_conv(dbgi, node, mode);
4642 node = do_strict_conv(dbgi, node);
4647 if (is_compound_type(res_type)) {
4650 mode = get_type_mode(res_type);
4652 in[0] = new_Unknown(mode);
4656 /* build return_value for its side effects */
4657 if (statement->value != NULL) {
4658 expression_to_firm(statement->value);
4663 ir_node *store = get_store();
4664 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4666 ir_node *end_block = get_irg_end_block(current_ir_graph);
4667 add_immBlock_pred(end_block, ret);
4669 set_cur_block(NULL);
4672 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4674 if (get_cur_block() == NULL)
4677 return expression_to_firm(statement->expression);
4680 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4682 entity_t *entity = compound->scope.entities;
4683 for ( ; entity != NULL; entity = entity->base.next) {
4684 if (!is_declaration(entity))
4687 create_local_declaration(entity);
4690 ir_node *result = NULL;
4691 statement_t *statement = compound->statements;
4692 for ( ; statement != NULL; statement = statement->base.next) {
4693 if (statement->base.next == NULL
4694 && statement->kind == STATEMENT_EXPRESSION) {
4695 result = expression_statement_to_firm(
4696 &statement->expression);
4699 statement_to_firm(statement);
4705 static void create_global_variable(entity_t *entity)
4707 ir_linkage linkage = IR_LINKAGE_DEFAULT;
4708 ir_visibility visibility = ir_visibility_default;
4709 ir_entity *irentity;
4710 assert(entity->kind == ENTITY_VARIABLE);
4712 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4713 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4714 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4715 case STORAGE_CLASS_NONE:
4716 visibility = ir_visibility_default;
4717 /* uninitialized globals get merged in C */
4718 if (entity->variable.initializer == NULL)
4719 linkage |= IR_LINKAGE_MERGE;
4721 case STORAGE_CLASS_TYPEDEF:
4722 case STORAGE_CLASS_AUTO:
4723 case STORAGE_CLASS_REGISTER:
4724 panic("invalid storage class for global var");
4727 ir_type *var_type = get_glob_type();
4728 if (entity->variable.thread_local) {
4729 var_type = get_tls_type();
4730 /* LINKAGE_MERGE not supported by current linkers */
4731 linkage &= ~IR_LINKAGE_MERGE;
4733 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4734 irentity = entity->variable.v.entity;
4735 add_entity_linkage(irentity, linkage);
4736 set_entity_visibility(irentity, visibility);
4739 static void create_local_declaration(entity_t *entity)
4741 assert(is_declaration(entity));
4743 /* construct type */
4744 (void) get_ir_type(entity->declaration.type);
4745 if (entity->base.symbol == NULL) {
4749 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4750 case STORAGE_CLASS_STATIC:
4751 if (entity->kind == ENTITY_FUNCTION) {
4752 (void)get_function_entity(entity, NULL);
4754 create_local_static_variable(entity);
4757 case STORAGE_CLASS_EXTERN:
4758 if (entity->kind == ENTITY_FUNCTION) {
4759 assert(entity->function.statement == NULL);
4760 (void)get_function_entity(entity, NULL);
4762 create_global_variable(entity);
4763 create_variable_initializer(entity);
4766 case STORAGE_CLASS_NONE:
4767 case STORAGE_CLASS_AUTO:
4768 case STORAGE_CLASS_REGISTER:
4769 if (entity->kind == ENTITY_FUNCTION) {
4770 if (entity->function.statement != NULL) {
4771 ir_type *owner = get_irg_frame_type(current_ir_graph);
4772 (void)get_function_entity(entity, owner);
4773 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4774 enqueue_inner_function(entity);
4776 (void)get_function_entity(entity, NULL);
4779 create_local_variable(entity);
4782 case STORAGE_CLASS_TYPEDEF:
4785 panic("invalid storage class found");
4788 static void initialize_local_declaration(entity_t *entity)
4790 if (entity->base.symbol == NULL)
4793 // no need to emit code in dead blocks
4794 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4795 && get_cur_block() == NULL)
4798 switch ((declaration_kind_t) entity->declaration.kind) {
4799 case DECLARATION_KIND_LOCAL_VARIABLE:
4800 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4801 create_variable_initializer(entity);
4804 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4805 allocate_variable_length_array(entity);
4808 case DECLARATION_KIND_COMPOUND_MEMBER:
4809 case DECLARATION_KIND_GLOBAL_VARIABLE:
4810 case DECLARATION_KIND_FUNCTION:
4811 case DECLARATION_KIND_INNER_FUNCTION:
4814 case DECLARATION_KIND_PARAMETER:
4815 case DECLARATION_KIND_PARAMETER_ENTITY:
4816 panic("can't initialize parameters");
4818 case DECLARATION_KIND_UNKNOWN:
4819 panic("can't initialize unknown declaration");
4821 panic("invalid declaration kind");
4824 static void declaration_statement_to_firm(declaration_statement_t *statement)
4826 entity_t *entity = statement->declarations_begin;
4830 entity_t *const last = statement->declarations_end;
4831 for ( ;; entity = entity->base.next) {
4832 if (is_declaration(entity)) {
4833 initialize_local_declaration(entity);
4834 } else if (entity->kind == ENTITY_TYPEDEF) {
4835 /* ยง6.7.7:3 Any array size expressions associated with variable length
4836 * array declarators are evaluated each time the declaration of the
4837 * typedef name is reached in the order of execution. */
4838 type_t *const type = skip_typeref(entity->typedefe.type);
4839 if (is_type_array(type) && type->array.is_vla)
4840 get_vla_size(&type->array);
4847 static void if_statement_to_firm(if_statement_t *statement)
4849 /* Create the condition. */
4850 ir_node *true_block = NULL;
4851 ir_node *false_block = NULL;
4852 if (get_cur_block() != NULL) {
4853 true_block = new_immBlock();
4854 false_block = new_immBlock();
4855 create_condition_evaluation(statement->condition, true_block, false_block);
4856 mature_immBlock(true_block);
4859 /* Create the false statement.
4860 * Handle false before true, so if no false statement is present, then the
4861 * empty false block is reused as fallthrough block. */
4862 ir_node *fallthrough_block = NULL;
4863 if (statement->false_statement != NULL) {
4864 if (false_block != NULL) {
4865 mature_immBlock(false_block);
4867 set_cur_block(false_block);
4868 statement_to_firm(statement->false_statement);
4869 if (get_cur_block() != NULL) {
4870 fallthrough_block = new_immBlock();
4871 add_immBlock_pred(fallthrough_block, new_Jmp());
4874 fallthrough_block = false_block;
4877 /* Create the true statement. */
4878 set_cur_block(true_block);
4879 statement_to_firm(statement->true_statement);
4880 if (get_cur_block() != NULL) {
4881 if (fallthrough_block == NULL) {
4882 fallthrough_block = new_immBlock();
4884 add_immBlock_pred(fallthrough_block, new_Jmp());
4887 /* Handle the block after the if-statement. */
4888 if (fallthrough_block != NULL) {
4889 mature_immBlock(fallthrough_block);
4891 set_cur_block(fallthrough_block);
4894 /* Create a jump node which jumps into target_block, if the current block is
4896 static void jump_if_reachable(ir_node *const target_block)
4898 if (get_cur_block() != NULL) {
4899 add_immBlock_pred(target_block, new_Jmp());
4903 static void while_statement_to_firm(while_statement_t *statement)
4905 /* Create the header block */
4906 ir_node *const header_block = new_immBlock();
4907 jump_if_reachable(header_block);
4909 /* Create the condition. */
4910 ir_node * body_block;
4911 ir_node * false_block;
4912 expression_t *const cond = statement->condition;
4913 if (is_constant_expression(cond) == EXPR_CLASS_CONSTANT &&
4914 fold_constant_to_bool(cond)) {
4915 /* Shortcut for while (true). */
4916 body_block = header_block;
4919 keep_alive(header_block);
4920 keep_all_memory(header_block);
4922 body_block = new_immBlock();
4923 false_block = new_immBlock();
4925 set_cur_block(header_block);
4926 create_condition_evaluation(cond, body_block, false_block);
4927 mature_immBlock(body_block);
4930 ir_node *const old_continue_label = continue_label;
4931 ir_node *const old_break_label = break_label;
4932 continue_label = header_block;
4933 break_label = false_block;
4935 /* Create the loop body. */
4936 set_cur_block(body_block);
4937 statement_to_firm(statement->body);
4938 jump_if_reachable(header_block);
4940 mature_immBlock(header_block);
4941 assert(false_block == NULL || false_block == break_label);
4942 false_block = break_label;
4943 if (false_block != NULL) {
4944 mature_immBlock(false_block);
4946 set_cur_block(false_block);
4948 assert(continue_label == header_block);
4949 continue_label = old_continue_label;
4950 break_label = old_break_label;
4953 static ir_node *get_break_label(void)
4955 if (break_label == NULL) {
4956 break_label = new_immBlock();
4961 static void do_while_statement_to_firm(do_while_statement_t *statement)
4963 /* create the header block */
4964 ir_node *header_block = new_immBlock();
4967 ir_node *body_block = new_immBlock();
4968 jump_if_reachable(body_block);
4970 ir_node *old_continue_label = continue_label;
4971 ir_node *old_break_label = break_label;
4972 continue_label = header_block;
4975 set_cur_block(body_block);
4976 statement_to_firm(statement->body);
4977 ir_node *const false_block = get_break_label();
4979 assert(continue_label == header_block);
4980 continue_label = old_continue_label;
4981 break_label = old_break_label;
4983 jump_if_reachable(header_block);
4985 /* create the condition */
4986 mature_immBlock(header_block);
4987 set_cur_block(header_block);
4989 create_condition_evaluation(statement->condition, body_block, false_block);
4990 mature_immBlock(body_block);
4991 mature_immBlock(false_block);
4993 set_cur_block(false_block);
4996 static void for_statement_to_firm(for_statement_t *statement)
4998 /* create declarations */
4999 entity_t *entity = statement->scope.entities;
5000 for ( ; entity != NULL; entity = entity->base.next) {
5001 if (!is_declaration(entity))
5004 create_local_declaration(entity);
5007 if (get_cur_block() != NULL) {
5008 entity = statement->scope.entities;
5009 for ( ; entity != NULL; entity = entity->base.next) {
5010 if (!is_declaration(entity))
5013 initialize_local_declaration(entity);
5016 if (statement->initialisation != NULL) {
5017 expression_to_firm(statement->initialisation);
5021 /* Create the header block */
5022 ir_node *const header_block = new_immBlock();
5023 jump_if_reachable(header_block);
5025 /* Create the condition. */
5026 ir_node *body_block;
5027 ir_node *false_block;
5028 if (statement->condition != NULL) {
5029 body_block = new_immBlock();
5030 false_block = new_immBlock();
5032 set_cur_block(header_block);
5033 create_condition_evaluation(statement->condition, body_block, false_block);
5034 mature_immBlock(body_block);
5037 body_block = header_block;
5040 keep_alive(header_block);
5041 keep_all_memory(header_block);
5044 /* Create the step block, if necessary. */
5045 ir_node * step_block = header_block;
5046 expression_t *const step = statement->step;
5048 step_block = new_immBlock();
5051 ir_node *const old_continue_label = continue_label;
5052 ir_node *const old_break_label = break_label;
5053 continue_label = step_block;
5054 break_label = false_block;
5056 /* Create the loop body. */
5057 set_cur_block(body_block);
5058 statement_to_firm(statement->body);
5059 jump_if_reachable(step_block);
5061 /* Create the step code. */
5063 mature_immBlock(step_block);
5064 set_cur_block(step_block);
5065 expression_to_firm(step);
5066 jump_if_reachable(header_block);
5069 mature_immBlock(header_block);
5070 assert(false_block == NULL || false_block == break_label);
5071 false_block = break_label;
5072 if (false_block != NULL) {
5073 mature_immBlock(false_block);
5075 set_cur_block(false_block);
5077 assert(continue_label == step_block);
5078 continue_label = old_continue_label;
5079 break_label = old_break_label;
5082 static void create_jump_statement(const statement_t *statement,
5083 ir_node *target_block)
5085 if (get_cur_block() == NULL)
5088 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5089 ir_node *jump = new_d_Jmp(dbgi);
5090 add_immBlock_pred(target_block, jump);
5092 set_cur_block(NULL);
5095 static void switch_statement_to_firm(switch_statement_t *statement)
5097 ir_node *first_block = NULL;
5098 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5099 ir_node *cond = NULL;
5101 if (get_cur_block() != NULL) {
5102 ir_node *expression = expression_to_firm(statement->expression);
5103 cond = new_d_Cond(dbgi, expression);
5104 first_block = get_cur_block();
5107 set_cur_block(NULL);
5109 ir_node *const old_switch_cond = current_switch_cond;
5110 ir_node *const old_break_label = break_label;
5111 const bool old_saw_default_label = saw_default_label;
5112 saw_default_label = false;
5113 current_switch_cond = cond;
5115 switch_statement_t *const old_switch = current_switch;
5116 current_switch = statement;
5118 /* determine a free number for the default label */
5119 unsigned long num_cases = 0;
5120 long default_proj_nr = 0;
5121 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5122 if (l->expression == NULL) {
5126 if (l->last_case >= l->first_case)
5127 num_cases += l->last_case - l->first_case + 1;
5128 if (l->last_case > default_proj_nr)
5129 default_proj_nr = l->last_case;
5132 if (default_proj_nr == INT_MAX) {
5133 /* Bad: an overflow will occur, we cannot be sure that the
5134 * maximum + 1 is a free number. Scan the values a second
5135 * time to find a free number.
5137 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5139 memset(bits, 0, (num_cases + 7) >> 3);
5140 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5141 if (l->expression == NULL) {
5145 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5146 if (start < num_cases && l->last_case >= 0) {
5147 unsigned long end = (unsigned long)l->last_case < num_cases ?
5148 (unsigned long)l->last_case : num_cases - 1;
5149 for (unsigned long cns = start; cns <= end; ++cns) {
5150 bits[cns >> 3] |= (1 << (cns & 7));
5154 /* We look at the first num_cases constants:
5155 * Either they are dense, so we took the last (num_cases)
5156 * one, or they are not dense, so we will find one free
5160 for (i = 0; i < num_cases; ++i)
5161 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5165 default_proj_nr = i;
5169 statement->default_proj_nr = default_proj_nr;
5170 /* safety check: cond might already be folded to a Bad */
5171 if (cond != NULL && is_Cond(cond)) {
5172 set_Cond_default_proj(cond, default_proj_nr);
5175 statement_to_firm(statement->body);
5177 jump_if_reachable(get_break_label());
5179 if (!saw_default_label && first_block != NULL) {
5180 set_cur_block(first_block);
5181 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5182 add_immBlock_pred(get_break_label(), proj);
5185 if (break_label != NULL) {
5186 mature_immBlock(break_label);
5188 set_cur_block(break_label);
5190 assert(current_switch_cond == cond);
5191 current_switch = old_switch;
5192 current_switch_cond = old_switch_cond;
5193 break_label = old_break_label;
5194 saw_default_label = old_saw_default_label;
5197 static void case_label_to_firm(const case_label_statement_t *statement)
5199 if (statement->is_empty_range)
5202 ir_node *block = new_immBlock();
5203 /* Fallthrough from previous case */
5204 jump_if_reachable(block);
5206 if (current_switch_cond != NULL) {
5207 set_cur_block(get_nodes_block(current_switch_cond));
5208 dbg_info *const dbgi = get_dbg_info(&statement->base.source_position);
5209 if (statement->expression != NULL) {
5210 long pn = statement->first_case;
5211 long end_pn = statement->last_case;
5212 assert(pn <= end_pn);
5213 /* create jumps for all cases in the given range */
5215 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5216 add_immBlock_pred(block, proj);
5217 } while (pn++ < end_pn);
5219 saw_default_label = true;
5220 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5221 current_switch->default_proj_nr);
5222 add_immBlock_pred(block, proj);
5226 mature_immBlock(block);
5227 set_cur_block(block);
5229 statement_to_firm(statement->statement);
5232 static void label_to_firm(const label_statement_t *statement)
5234 ir_node *block = get_label_block(statement->label);
5235 jump_if_reachable(block);
5237 set_cur_block(block);
5239 keep_all_memory(block);
5241 statement_to_firm(statement->statement);
5244 static void goto_to_firm(const goto_statement_t *statement)
5246 if (get_cur_block() == NULL)
5249 if (statement->expression) {
5250 ir_node *irn = expression_to_firm(statement->expression);
5251 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5252 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5254 set_irn_link(ijmp, ijmp_list);
5257 ir_node *block = get_label_block(statement->label);
5258 ir_node *jmp = new_Jmp();
5259 add_immBlock_pred(block, jmp);
5261 set_cur_block(NULL);
5264 static void asm_statement_to_firm(const asm_statement_t *statement)
5266 bool needs_memory = false;
5268 if (statement->is_volatile) {
5269 needs_memory = true;
5272 size_t n_clobbers = 0;
5273 asm_clobber_t *clobber = statement->clobbers;
5274 for ( ; clobber != NULL; clobber = clobber->next) {
5275 const char *clobber_str = clobber->clobber.begin;
5277 if (!be_is_valid_clobber(clobber_str)) {
5278 errorf(&statement->base.source_position,
5279 "invalid clobber '%s' specified", clobber->clobber);
5283 if (strcmp(clobber_str, "memory") == 0) {
5284 needs_memory = true;
5288 ident *id = new_id_from_str(clobber_str);
5289 obstack_ptr_grow(&asm_obst, id);
5292 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5293 ident **clobbers = NULL;
5294 if (n_clobbers > 0) {
5295 clobbers = obstack_finish(&asm_obst);
5298 size_t n_inputs = 0;
5299 asm_argument_t *argument = statement->inputs;
5300 for ( ; argument != NULL; argument = argument->next)
5302 size_t n_outputs = 0;
5303 argument = statement->outputs;
5304 for ( ; argument != NULL; argument = argument->next)
5307 unsigned next_pos = 0;
5309 ir_node *ins[n_inputs + n_outputs + 1];
5312 ir_asm_constraint tmp_in_constraints[n_outputs];
5314 const expression_t *out_exprs[n_outputs];
5315 ir_node *out_addrs[n_outputs];
5316 size_t out_size = 0;
5318 argument = statement->outputs;
5319 for ( ; argument != NULL; argument = argument->next) {
5320 const char *constraints = argument->constraints.begin;
5321 asm_constraint_flags_t asm_flags
5322 = be_parse_asm_constraints(constraints);
5324 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5325 warningf(&statement->base.source_position,
5326 "some constraints in '%s' are not supported", constraints);
5328 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5329 errorf(&statement->base.source_position,
5330 "some constraints in '%s' are invalid", constraints);
5333 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5334 errorf(&statement->base.source_position,
5335 "no write flag specified for output constraints '%s'",
5340 unsigned pos = next_pos++;
5341 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5342 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5343 expression_t *expr = argument->expression;
5344 ir_node *addr = expression_to_addr(expr);
5345 /* in+output, construct an artifical same_as constraint on the
5347 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5349 ir_node *value = get_value_from_lvalue(expr, addr);
5351 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5353 ir_asm_constraint constraint;
5354 constraint.pos = pos;
5355 constraint.constraint = new_id_from_str(buf);
5356 constraint.mode = get_ir_mode_storage(expr->base.type);
5357 tmp_in_constraints[in_size] = constraint;
5358 ins[in_size] = value;
5363 out_exprs[out_size] = expr;
5364 out_addrs[out_size] = addr;
5366 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5367 /* pure memory ops need no input (but we have to make sure we
5368 * attach to the memory) */
5369 assert(! (asm_flags &
5370 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5371 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5372 needs_memory = true;
5374 /* we need to attach the address to the inputs */
5375 expression_t *expr = argument->expression;
5377 ir_asm_constraint constraint;
5378 constraint.pos = pos;
5379 constraint.constraint = new_id_from_str(constraints);
5380 constraint.mode = NULL;
5381 tmp_in_constraints[in_size] = constraint;
5383 ins[in_size] = expression_to_addr(expr);
5387 errorf(&statement->base.source_position,
5388 "only modifiers but no place set in constraints '%s'",
5393 ir_asm_constraint constraint;
5394 constraint.pos = pos;
5395 constraint.constraint = new_id_from_str(constraints);
5396 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5398 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5400 assert(obstack_object_size(&asm_obst)
5401 == out_size * sizeof(ir_asm_constraint));
5402 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5405 obstack_grow(&asm_obst, tmp_in_constraints,
5406 in_size * sizeof(tmp_in_constraints[0]));
5407 /* find and count input and output arguments */
5408 argument = statement->inputs;
5409 for ( ; argument != NULL; argument = argument->next) {
5410 const char *constraints = argument->constraints.begin;
5411 asm_constraint_flags_t asm_flags
5412 = be_parse_asm_constraints(constraints);
5414 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5415 errorf(&statement->base.source_position,
5416 "some constraints in '%s' are not supported", constraints);
5419 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5420 errorf(&statement->base.source_position,
5421 "some constraints in '%s' are invalid", constraints);
5424 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5425 errorf(&statement->base.source_position,
5426 "write flag specified for input constraints '%s'",
5432 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5433 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5434 /* we can treat this as "normal" input */
5435 input = expression_to_firm(argument->expression);
5436 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5437 /* pure memory ops need no input (but we have to make sure we
5438 * attach to the memory) */
5439 assert(! (asm_flags &
5440 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5441 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5442 needs_memory = true;
5443 input = expression_to_addr(argument->expression);
5445 errorf(&statement->base.source_position,
5446 "only modifiers but no place set in constraints '%s'",
5451 ir_asm_constraint constraint;
5452 constraint.pos = next_pos++;
5453 constraint.constraint = new_id_from_str(constraints);
5454 constraint.mode = get_irn_mode(input);
5456 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5457 ins[in_size++] = input;
5461 ir_asm_constraint constraint;
5462 constraint.pos = next_pos++;
5463 constraint.constraint = new_id_from_str("");
5464 constraint.mode = mode_M;
5466 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5467 ins[in_size++] = get_store();
5470 assert(obstack_object_size(&asm_obst)
5471 == in_size * sizeof(ir_asm_constraint));
5472 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5474 /* create asm node */
5475 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5477 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5479 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5480 out_size, output_constraints,
5481 n_clobbers, clobbers, asm_text);
5483 if (statement->is_volatile) {
5484 set_irn_pinned(node, op_pin_state_pinned);
5486 set_irn_pinned(node, op_pin_state_floats);
5489 /* create output projs & connect them */
5491 ir_node *projm = new_Proj(node, mode_M, out_size);
5496 for (i = 0; i < out_size; ++i) {
5497 const expression_t *out_expr = out_exprs[i];
5499 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5500 ir_node *proj = new_Proj(node, mode, pn);
5501 ir_node *addr = out_addrs[i];
5503 set_value_for_expression_addr(out_expr, proj, addr);
5507 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5509 statement_to_firm(statement->try_statement);
5510 warningf(&statement->base.source_position, "structured exception handling ignored");
5513 static void leave_statement_to_firm(leave_statement_t *statement)
5515 errorf(&statement->base.source_position, "__leave not supported yet");
5519 * Transform a statement.
5521 static void statement_to_firm(statement_t *statement)
5524 assert(!statement->base.transformed);
5525 statement->base.transformed = true;
5528 switch (statement->kind) {
5529 case STATEMENT_INVALID:
5530 panic("invalid statement found");
5531 case STATEMENT_EMPTY:
5534 case STATEMENT_COMPOUND:
5535 compound_statement_to_firm(&statement->compound);
5537 case STATEMENT_RETURN:
5538 return_statement_to_firm(&statement->returns);
5540 case STATEMENT_EXPRESSION:
5541 expression_statement_to_firm(&statement->expression);
5544 if_statement_to_firm(&statement->ifs);
5546 case STATEMENT_WHILE:
5547 while_statement_to_firm(&statement->whiles);
5549 case STATEMENT_DO_WHILE:
5550 do_while_statement_to_firm(&statement->do_while);
5552 case STATEMENT_DECLARATION:
5553 declaration_statement_to_firm(&statement->declaration);
5555 case STATEMENT_BREAK:
5556 create_jump_statement(statement, get_break_label());
5558 case STATEMENT_CONTINUE:
5559 create_jump_statement(statement, continue_label);
5561 case STATEMENT_SWITCH:
5562 switch_statement_to_firm(&statement->switchs);
5564 case STATEMENT_CASE_LABEL:
5565 case_label_to_firm(&statement->case_label);
5568 for_statement_to_firm(&statement->fors);
5570 case STATEMENT_LABEL:
5571 label_to_firm(&statement->label);
5573 case STATEMENT_GOTO:
5574 goto_to_firm(&statement->gotos);
5577 asm_statement_to_firm(&statement->asms);
5579 case STATEMENT_MS_TRY:
5580 ms_try_statement_to_firm(&statement->ms_try);
5582 case STATEMENT_LEAVE:
5583 leave_statement_to_firm(&statement->leave);
5586 panic("statement not implemented");
5589 static int count_local_variables(const entity_t *entity,
5590 const entity_t *const last)
5593 entity_t const *const end = last != NULL ? last->base.next : NULL;
5594 for (; entity != end; entity = entity->base.next) {
5598 if (entity->kind == ENTITY_VARIABLE) {
5599 type = skip_typeref(entity->declaration.type);
5600 address_taken = entity->variable.address_taken;
5601 } else if (entity->kind == ENTITY_PARAMETER) {
5602 type = skip_typeref(entity->declaration.type);
5603 address_taken = entity->parameter.address_taken;
5608 if (!address_taken && is_type_scalar(type))
5614 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5616 int *const count = env;
5618 switch (stmt->kind) {
5619 case STATEMENT_DECLARATION: {
5620 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5621 *count += count_local_variables(decl_stmt->declarations_begin,
5622 decl_stmt->declarations_end);
5627 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5636 * Return the number of local (alias free) variables used by a function.
5638 static int get_function_n_local_vars(entity_t *entity)
5640 const function_t *function = &entity->function;
5643 /* count parameters */
5644 count += count_local_variables(function->parameters.entities, NULL);
5646 /* count local variables declared in body */
5647 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5652 * Build Firm code for the parameters of a function.
5654 static void initialize_function_parameters(entity_t *entity)
5656 assert(entity->kind == ENTITY_FUNCTION);
5657 ir_graph *irg = current_ir_graph;
5658 ir_node *args = get_irg_args(irg);
5659 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5660 int first_param_nr = 0;
5662 if (entity->function.need_closure) {
5663 /* add an extra parameter for the static link */
5664 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5669 entity_t *parameter = entity->function.parameters.entities;
5670 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5671 if (parameter->kind != ENTITY_PARAMETER)
5674 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5675 type_t *type = skip_typeref(parameter->declaration.type);
5677 bool needs_entity = parameter->parameter.address_taken;
5678 assert(!is_type_array(type));
5679 if (is_type_compound(type)) {
5680 needs_entity = true;
5684 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5685 ident *id = new_id_from_str(parameter->base.symbol->string);
5686 set_entity_ident(entity, id);
5688 parameter->declaration.kind
5689 = DECLARATION_KIND_PARAMETER_ENTITY;
5690 parameter->parameter.v.entity = entity;
5694 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5695 ir_mode *param_mode = get_type_mode(param_irtype);
5697 long pn = n + first_param_nr;
5698 ir_node *value = new_r_Proj(args, param_mode, pn);
5700 ir_mode *mode = get_ir_mode_storage(type);
5701 value = create_conv(NULL, value, mode);
5702 value = do_strict_conv(NULL, value);
5704 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5705 parameter->parameter.v.value_number = next_value_number_function;
5706 set_irg_loc_description(current_ir_graph, next_value_number_function,
5708 ++next_value_number_function;
5710 set_value(parameter->parameter.v.value_number, value);
5715 * Handle additional decl modifiers for IR-graphs
5717 * @param irg the IR-graph
5718 * @param dec_modifiers additional modifiers
5720 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5721 decl_modifiers_t decl_modifiers)
5723 if (decl_modifiers & DM_RETURNS_TWICE) {
5724 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5725 add_irg_additional_properties(irg, mtp_property_returns_twice);
5727 if (decl_modifiers & DM_NORETURN) {
5728 /* TRUE if the declaration includes the Microsoft
5729 __declspec(noreturn) specifier. */
5730 add_irg_additional_properties(irg, mtp_property_noreturn);
5732 if (decl_modifiers & DM_NOTHROW) {
5733 /* TRUE if the declaration includes the Microsoft
5734 __declspec(nothrow) specifier. */
5735 add_irg_additional_properties(irg, mtp_property_nothrow);
5737 if (decl_modifiers & DM_NAKED) {
5738 /* TRUE if the declaration includes the Microsoft
5739 __declspec(naked) specifier. */
5740 add_irg_additional_properties(irg, mtp_property_naked);
5742 if (decl_modifiers & DM_FORCEINLINE) {
5743 /* TRUE if the declaration includes the
5744 Microsoft __forceinline specifier. */
5745 set_irg_inline_property(irg, irg_inline_forced);
5747 if (decl_modifiers & DM_NOINLINE) {
5748 /* TRUE if the declaration includes the Microsoft
5749 __declspec(noinline) specifier. */
5750 set_irg_inline_property(irg, irg_inline_forbidden);
5754 static void add_function_pointer(ir_type *segment, ir_entity *method,
5755 const char *unique_template)
5757 ir_type *method_type = get_entity_type(method);
5758 ir_type *ptr_type = new_type_pointer(method_type);
5760 /* these entities don't really have a name but firm only allows
5762 * Note that we mustn't give these entities a name since for example
5763 * Mach-O doesn't allow them. */
5764 ident *ide = id_unique(unique_template);
5765 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5766 ir_graph *irg = get_const_code_irg();
5767 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5770 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5771 set_entity_compiler_generated(ptr, 1);
5772 set_entity_visibility(ptr, ir_visibility_private);
5773 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5774 set_atomic_ent_value(ptr, val);
5778 * Generate possible IJmp branches to a given label block.
5780 static void gen_ijmp_branches(ir_node *block)
5783 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5784 add_immBlock_pred(block, ijmp);
5789 * Create code for a function and all inner functions.
5791 * @param entity the function entity
5793 static void create_function(entity_t *entity)
5795 assert(entity->kind == ENTITY_FUNCTION);
5796 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5798 if (entity->function.statement == NULL)
5801 if (is_main(entity) && enable_main_collect2_hack) {
5802 prepare_main_collect2(entity);
5805 inner_functions = NULL;
5806 current_trampolines = NULL;
5808 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5809 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5810 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5812 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5813 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5814 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5817 current_function_entity = entity;
5818 current_function_name = NULL;
5819 current_funcsig = NULL;
5821 assert(all_labels == NULL);
5822 all_labels = NEW_ARR_F(label_t *, 0);
5825 int n_local_vars = get_function_n_local_vars(entity);
5826 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5827 current_ir_graph = irg;
5829 ir_graph *old_current_function = current_function;
5830 current_function = irg;
5832 set_irg_fp_model(irg, firm_opt.fp_model);
5833 tarval_enable_fp_ops(1);
5834 set_irn_dbg_info(get_irg_start_block(irg),
5835 get_entity_dbg_info(function_entity));
5837 ir_node *first_block = get_cur_block();
5839 /* set inline flags */
5840 if (entity->function.is_inline)
5841 set_irg_inline_property(irg, irg_inline_recomended);
5842 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5844 next_value_number_function = 0;
5845 initialize_function_parameters(entity);
5846 current_static_link = entity->function.static_link;
5848 statement_to_firm(entity->function.statement);
5850 ir_node *end_block = get_irg_end_block(irg);
5852 /* do we have a return statement yet? */
5853 if (get_cur_block() != NULL) {
5854 type_t *type = skip_typeref(entity->declaration.type);
5855 assert(is_type_function(type));
5856 const function_type_t *func_type = &type->function;
5857 const type_t *return_type
5858 = skip_typeref(func_type->return_type);
5861 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5862 ret = new_Return(get_store(), 0, NULL);
5865 if (is_type_scalar(return_type)) {
5866 mode = get_ir_mode_storage(func_type->return_type);
5872 /* ยง5.1.2.2.3 main implicitly returns 0 */
5873 if (is_main(entity)) {
5874 in[0] = new_Const(get_mode_null(mode));
5876 in[0] = new_Unknown(mode);
5878 ret = new_Return(get_store(), 1, in);
5880 add_immBlock_pred(end_block, ret);
5883 bool has_computed_gotos = false;
5884 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5885 label_t *label = all_labels[i];
5886 if (label->address_taken) {
5887 gen_ijmp_branches(label->block);
5888 has_computed_gotos = true;
5890 mature_immBlock(label->block);
5892 if (has_computed_gotos) {
5893 /* if we have computed goto's in the function, we cannot inline it */
5894 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5895 warningf(&entity->base.source_position,
5896 "function '%Y' can never be inlined because it contains a computed goto",
5897 entity->base.symbol);
5899 set_irg_inline_property(irg, irg_inline_forbidden);
5902 DEL_ARR_F(all_labels);
5905 mature_immBlock(first_block);
5906 mature_immBlock(end_block);
5908 irg_finalize_cons(irg);
5910 /* finalize the frame type */
5911 ir_type *frame_type = get_irg_frame_type(irg);
5912 int n = get_compound_n_members(frame_type);
5915 for (int i = 0; i < n; ++i) {
5916 ir_entity *entity = get_compound_member(frame_type, i);
5917 ir_type *entity_type = get_entity_type(entity);
5919 int align = get_type_alignment_bytes(entity_type);
5920 if (align > align_all)
5924 misalign = offset % align;
5926 offset += align - misalign;
5930 set_entity_offset(entity, offset);
5931 offset += get_type_size_bytes(entity_type);
5933 set_type_size_bytes(frame_type, offset);
5934 set_type_alignment_bytes(frame_type, align_all);
5936 irg_verify(irg, VERIFY_ENFORCE_SSA);
5937 current_function = old_current_function;
5939 if (current_trampolines != NULL) {
5940 DEL_ARR_F(current_trampolines);
5941 current_trampolines = NULL;
5944 /* create inner functions if any */
5945 entity_t **inner = inner_functions;
5946 if (inner != NULL) {
5947 ir_type *rem_outer_frame = current_outer_frame;
5948 current_outer_frame = get_irg_frame_type(current_ir_graph);
5949 ir_type *rem_outer_value_type = current_outer_value_type;
5950 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5951 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5952 create_function(inner[i]);
5956 current_outer_value_type = rem_outer_value_type;
5957 current_outer_frame = rem_outer_frame;
5961 static void scope_to_firm(scope_t *scope)
5963 /* first pass: create declarations */
5964 entity_t *entity = scope->entities;
5965 for ( ; entity != NULL; entity = entity->base.next) {
5966 if (entity->base.symbol == NULL)
5969 if (entity->kind == ENTITY_FUNCTION) {
5970 if (entity->function.btk != bk_none) {
5971 /* builtins have no representation */
5974 (void)get_function_entity(entity, NULL);
5975 } else if (entity->kind == ENTITY_VARIABLE) {
5976 create_global_variable(entity);
5977 } else if (entity->kind == ENTITY_NAMESPACE) {
5978 scope_to_firm(&entity->namespacee.members);
5982 /* second pass: create code/initializers */
5983 entity = scope->entities;
5984 for ( ; entity != NULL; entity = entity->base.next) {
5985 if (entity->base.symbol == NULL)
5988 if (entity->kind == ENTITY_FUNCTION) {
5989 if (entity->function.btk != bk_none) {
5990 /* builtins have no representation */
5993 create_function(entity);
5994 } else if (entity->kind == ENTITY_VARIABLE) {
5995 assert(entity->declaration.kind
5996 == DECLARATION_KIND_GLOBAL_VARIABLE);
5997 current_ir_graph = get_const_code_irg();
5998 create_variable_initializer(entity);
6003 void init_ast2firm(void)
6005 obstack_init(&asm_obst);
6006 init_atomic_modes();
6008 ir_set_debug_retrieve(dbg_retrieve);
6009 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
6011 /* create idents for all known runtime functions */
6012 for (size_t i = 0; i < lengthof(rts_data); ++i) {
6013 rts_idents[i] = new_id_from_str(rts_data[i].name);
6016 entitymap_init(&entitymap);
6019 static void init_ir_types(void)
6021 static int ir_types_initialized = 0;
6022 if (ir_types_initialized)
6024 ir_types_initialized = 1;
6026 ir_type_int = get_ir_type(type_int);
6027 ir_type_char = get_ir_type(type_char);
6028 ir_type_const_char = get_ir_type(type_const_char);
6029 ir_type_wchar_t = get_ir_type(type_wchar_t);
6030 ir_type_void = get_ir_type(type_void);
6032 be_params = be_get_backend_param();
6033 mode_float_arithmetic = be_params->mode_float_arithmetic;
6035 stack_param_align = be_params->stack_param_align;
6038 void exit_ast2firm(void)
6040 entitymap_destroy(&entitymap);
6041 obstack_free(&asm_obst, NULL);
6044 static void global_asm_to_firm(statement_t *s)
6046 for (; s != NULL; s = s->base.next) {
6047 assert(s->kind == STATEMENT_ASM);
6049 char const *const text = s->asms.asm_text.begin;
6050 size_t size = s->asms.asm_text.size;
6052 /* skip the last \0 */
6053 if (text[size - 1] == '\0')
6056 ident *const id = new_id_from_chars(text, size);
6061 void translation_unit_to_firm(translation_unit_t *unit)
6063 /* initialize firm arithmetic */
6064 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6065 ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6067 /* just to be sure */
6068 continue_label = NULL;
6070 current_switch_cond = NULL;
6071 current_translation_unit = unit;
6075 scope_to_firm(&unit->scope);
6076 global_asm_to_firm(unit->global_asm);
6078 current_ir_graph = NULL;
6079 current_translation_unit = NULL;