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"
53 typedef struct trampoline_region trampoline_region;
54 struct trampoline_region {
55 ir_entity *function; /**< The function that is called by this trampoline */
56 ir_entity *region; /**< created region for the trampoline */
59 fp_model_t firm_fp_model = fp_model_precise;
61 static const backend_params *be_params;
63 static ir_type *ir_type_char;
64 static ir_type *ir_type_const_char;
65 static ir_type *ir_type_wchar_t;
66 static ir_type *ir_type_void;
67 static ir_type *ir_type_int;
69 /* architecture specific floating point arithmetic mode (if any) */
70 static ir_mode *mode_float_arithmetic;
72 /* alignment of stack parameters */
73 static unsigned stack_param_align;
75 static int next_value_number_function;
76 static ir_node *continue_label;
77 static ir_node *break_label;
78 static ir_node *current_switch_cond;
79 static bool saw_default_label;
80 static label_t **all_labels;
81 static entity_t **inner_functions;
82 static ir_node *ijmp_list;
83 static bool constant_folding;
84 static bool initializer_use_bitfield_basetype;
86 static const entity_t *current_function_entity;
87 static ir_node *current_function_name;
88 static ir_node *current_funcsig;
89 static switch_statement_t *current_switch;
90 static ir_graph *current_function;
91 static translation_unit_t *current_translation_unit;
92 static trampoline_region *current_trampolines;
93 static ir_type *current_outer_frame;
94 static ir_type *current_outer_value_type;
95 static ir_node *current_static_link;
97 static entitymap_t entitymap;
99 static struct obstack asm_obst;
101 typedef enum declaration_kind_t {
102 DECLARATION_KIND_UNKNOWN,
103 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
104 DECLARATION_KIND_GLOBAL_VARIABLE,
105 DECLARATION_KIND_LOCAL_VARIABLE,
106 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
107 DECLARATION_KIND_PARAMETER,
108 DECLARATION_KIND_PARAMETER_ENTITY,
109 DECLARATION_KIND_FUNCTION,
110 DECLARATION_KIND_COMPOUND_MEMBER,
111 DECLARATION_KIND_INNER_FUNCTION
112 } declaration_kind_t;
114 static ir_mode *get_ir_mode_storage(type_t *type);
116 static ir_type *get_ir_type_incomplete(type_t *type);
118 static void enqueue_inner_function(entity_t *entity)
120 if (inner_functions == NULL)
121 inner_functions = NEW_ARR_F(entity_t *, 0);
122 ARR_APP1(entity_t*, inner_functions, entity);
125 static ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
127 const entity_t *entity = get_irg_loc_description(irg, pos);
129 if (entity != NULL && warning.uninitialized) {
130 source_position_t const *const pos = &entity->base.source_position;
131 warningf(pos, "'%N' might be used uninitialized", entity);
133 return new_r_Unknown(irg, mode);
136 static const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
138 const source_position_t *pos = (const source_position_t*) dbg;
143 return pos->input_name;
146 static dbg_info *get_dbg_info(const source_position_t *pos)
148 return (dbg_info*) pos;
151 static void dbg_print_type_dbg_info(char *buffer, size_t buffer_size,
152 const type_dbg_info *dbg)
155 print_to_buffer(buffer, buffer_size);
156 const type_t *type = (const type_t*) dbg;
158 finish_print_to_buffer();
161 static type_dbg_info *get_type_dbg_info_(const type_t *type)
163 return (type_dbg_info*) type;
166 /* is the current block a reachable one? */
167 static bool currently_reachable(void)
169 ir_node *const block = get_cur_block();
170 return block != NULL && !is_Bad(block);
173 static void set_unreachable_now(void)
178 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
180 static ir_mode *mode_int, *mode_uint;
182 static ir_node *_expression_to_firm(const expression_t *expression);
183 static ir_node *expression_to_firm(const expression_t *expression);
184 static void create_local_declaration(entity_t *entity);
186 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
188 unsigned flags = get_atomic_type_flags(kind);
189 unsigned size = get_atomic_type_size(kind);
190 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
191 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
194 unsigned bit_size = size * 8;
195 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
196 unsigned modulo_shift;
197 ir_mode_arithmetic arithmetic;
199 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
200 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
201 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
203 sort = irms_int_number;
204 arithmetic = irma_twos_complement;
205 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
207 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
208 snprintf(name, sizeof(name), "F%u", bit_size);
209 sort = irms_float_number;
210 arithmetic = irma_ieee754;
213 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
221 * Initialises the atomic modes depending on the machine size.
223 static void init_atomic_modes(void)
225 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
226 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
228 mode_int = atomic_modes[ATOMIC_TYPE_INT];
229 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
231 /* there's no real void type in firm */
232 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
234 /* initialize pointer modes */
236 ir_mode_sort sort = irms_reference;
237 unsigned bit_size = machine_size;
239 ir_mode_arithmetic arithmetic = irma_twos_complement;
240 unsigned modulo_shift
241 = bit_size < machine_size ? machine_size : bit_size;
243 snprintf(name, sizeof(name), "p%u", machine_size);
244 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
247 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
248 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
250 /* Hmm, pointers should be machine size */
251 set_modeP_data(ptr_mode);
252 set_modeP_code(ptr_mode);
255 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
257 assert(kind <= ATOMIC_TYPE_LAST);
258 return atomic_modes[kind];
261 static ir_node *get_vla_size(array_type_t *const type)
263 ir_node *size_node = type->size_node;
264 if (size_node == NULL) {
265 size_node = expression_to_firm(type->size_expression);
266 type->size_node = size_node;
272 * Return a node representing the size of a type.
274 static ir_node *get_type_size_node(type_t *type)
276 type = skip_typeref(type);
278 if (is_type_array(type) && type->array.is_vla) {
279 ir_node *size_node = get_vla_size(&type->array);
280 ir_node *elem_size = get_type_size_node(type->array.element_type);
281 ir_mode *mode = get_irn_mode(size_node);
282 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
286 ir_mode *mode = get_ir_mode_storage(type_size_t);
288 sym.type_p = get_ir_type(type);
289 return new_SymConst(mode, sym, symconst_type_size);
292 static unsigned count_parameters(const function_type_t *function_type)
296 function_parameter_t *parameter = function_type->parameters;
297 for ( ; parameter != NULL; parameter = parameter->next) {
305 * Creates a Firm type for an atomic type
307 static ir_type *create_atomic_type(atomic_type_kind_t akind, const type_t *type)
309 ir_mode *mode = atomic_modes[akind];
310 type_dbg_info *dbgi = get_type_dbg_info_(type);
311 ir_type *irtype = new_d_type_primitive(mode, dbgi);
312 il_alignment_t alignment = get_atomic_type_alignment(akind);
314 set_type_alignment_bytes(irtype, alignment);
320 * Creates a Firm type for a complex type
322 static ir_type *create_complex_type(const complex_type_t *type)
324 atomic_type_kind_t kind = type->akind;
325 ir_mode *mode = atomic_modes[kind];
326 ident *id = get_mode_ident(mode);
330 /* FIXME: finish the array */
335 * Creates a Firm type for an imaginary type
337 static ir_type *create_imaginary_type(imaginary_type_t *type)
339 return create_atomic_type(type->akind, (const type_t*) type);
343 * return type of a parameter (and take transparent union gnu extension into
346 static type_t *get_parameter_type(type_t *orig_type)
348 type_t *type = skip_typeref(orig_type);
349 if (is_type_union(type)
350 && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
351 compound_t *compound = type->compound.compound;
352 type = compound->members.entities->declaration.type;
358 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
360 type_t *return_type = skip_typeref(function_type->return_type);
362 int n_parameters = count_parameters(function_type)
363 + (for_closure ? 1 : 0);
364 int n_results = return_type == type_void ? 0 : 1;
365 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) function_type);
366 ir_type *irtype = new_d_type_method(n_parameters, n_results, dbgi);
368 if (return_type != type_void) {
369 ir_type *restype = get_ir_type(return_type);
370 set_method_res_type(irtype, 0, restype);
373 function_parameter_t *parameter = function_type->parameters;
376 ir_type *p_irtype = get_ir_type(type_void_ptr);
377 set_method_param_type(irtype, n, p_irtype);
380 for ( ; parameter != NULL; parameter = parameter->next) {
381 type_t *type = get_parameter_type(parameter->type);
382 ir_type *p_irtype = get_ir_type(type);
383 set_method_param_type(irtype, n, p_irtype);
387 bool is_variadic = function_type->variadic;
390 set_method_variadicity(irtype, variadicity_variadic);
392 unsigned cc = get_method_calling_convention(irtype);
393 switch (function_type->calling_convention) {
394 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
397 set_method_calling_convention(irtype, SET_CDECL(cc));
404 /* only non-variadic function can use stdcall, else use cdecl */
405 set_method_calling_convention(irtype, SET_STDCALL(cc));
411 /* only non-variadic function can use fastcall, else use cdecl */
412 set_method_calling_convention(irtype, SET_FASTCALL(cc));
416 /* Hmm, leave default, not accepted by the parser yet. */
421 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
426 static ir_type *create_pointer_type(pointer_type_t *type)
428 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
429 type_t *points_to = type->points_to;
430 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
431 ir_type *irtype = new_d_type_pointer(ir_points_to, dbgi);
436 static ir_type *create_reference_type(reference_type_t *type)
438 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
439 type_t *refers_to = type->refers_to;
440 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
441 ir_type *irtype = new_d_type_pointer(ir_refers_to, dbgi);
446 static ir_type *create_array_type(array_type_t *type)
448 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
449 type_t *element_type = type->element_type;
450 ir_type *ir_element_type = get_ir_type(element_type);
451 ir_type *irtype = new_d_type_array(1, ir_element_type, dbgi);
453 const int align = get_type_alignment_bytes(ir_element_type);
454 set_type_alignment_bytes(irtype, align);
456 if (type->size_constant) {
457 int n_elements = type->size;
459 set_array_bounds_int(irtype, 0, 0, n_elements);
461 size_t elemsize = get_type_size_bytes(ir_element_type);
462 if (elemsize % align > 0) {
463 elemsize += align - (elemsize % align);
465 set_type_size_bytes(irtype, n_elements * elemsize);
467 set_array_lower_bound_int(irtype, 0, 0);
469 set_type_state(irtype, layout_fixed);
475 * Return the signed integer type of size bits.
477 * @param size the size
479 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
483 static ir_mode *s_modes[64 + 1] = {NULL, };
487 if (size <= 0 || size > 64)
490 mode = s_modes[size];
494 snprintf(name, sizeof(name), "bf_I%u", size);
495 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
496 size <= 32 ? 32 : size );
497 s_modes[size] = mode;
500 type_dbg_info *dbgi = get_type_dbg_info_(type);
501 res = new_d_type_primitive(mode, dbgi);
502 set_primitive_base_type(res, base_tp);
508 * Return the unsigned integer type of size bits.
510 * @param size the size
512 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
516 static ir_mode *u_modes[64 + 1] = {NULL, };
520 if (size <= 0 || size > 64)
523 mode = u_modes[size];
527 snprintf(name, sizeof(name), "bf_U%u", size);
528 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
529 size <= 32 ? 32 : size );
530 u_modes[size] = mode;
533 type_dbg_info *dbgi = get_type_dbg_info_(type);
534 res = new_d_type_primitive(mode, dbgi);
535 set_primitive_base_type(res, base_tp);
540 static ir_type *create_bitfield_type(bitfield_type_t *const type)
542 type_t *base = skip_typeref(type->base_type);
543 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
544 ir_type *irbase = get_ir_type(base);
546 unsigned size = type->bit_size;
548 assert(!is_type_float(base));
549 if (is_type_signed(base)) {
550 return get_signed_int_type_for_bit_size(irbase, size,
551 (const type_t*) type);
553 return get_unsigned_int_type_for_bit_size(irbase, size,
554 (const type_t*) type);
558 #define INVALID_TYPE ((ir_type_ptr)-1)
561 COMPOUND_IS_STRUCT = false,
562 COMPOUND_IS_UNION = true
566 * Construct firm type from ast struct type.
568 static ir_type *create_compound_type(compound_type_t *type,
569 bool incomplete, bool is_union)
571 compound_t *compound = type->compound;
573 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
574 return compound->irtype;
577 symbol_t *type_symbol = compound->base.symbol;
579 if (type_symbol != NULL) {
580 id = new_id_from_str(type_symbol->string);
583 id = id_unique("__anonymous_union.%u");
585 id = id_unique("__anonymous_struct.%u");
591 irtype = new_type_union(id);
593 irtype = new_type_struct(id);
596 compound->irtype_complete = false;
597 compound->irtype = irtype;
603 layout_union_type(type);
605 layout_struct_type(type);
608 compound->irtype_complete = true;
610 entity_t *entry = compound->members.entities;
611 for ( ; entry != NULL; entry = entry->base.next) {
612 if (entry->kind != ENTITY_COMPOUND_MEMBER)
615 symbol_t *symbol = entry->base.symbol;
616 type_t *entry_type = entry->declaration.type;
618 if (symbol == NULL) {
619 /* anonymous bitfield member, skip */
620 if (entry_type->kind == TYPE_BITFIELD)
622 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
623 || entry_type->kind == TYPE_COMPOUND_UNION);
624 ident = id_unique("anon.%u");
626 ident = new_id_from_str(symbol->string);
629 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
631 ir_type *entry_irtype = get_ir_type(entry_type);
632 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
634 set_entity_offset(entity, entry->compound_member.offset);
635 set_entity_offset_bits_remainder(entity,
636 entry->compound_member.bit_offset);
638 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
639 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
640 entry->compound_member.entity = entity;
643 set_type_alignment_bytes(irtype, compound->alignment);
644 set_type_size_bytes(irtype, compound->size);
645 set_type_state(irtype, layout_fixed);
650 static ir_type *create_enum_type(enum_type_t *const type)
652 type->base.firm_type = ir_type_int;
654 ir_mode *const mode = mode_int;
655 ir_tarval *const one = get_mode_one(mode);
656 ir_tarval * tv_next = get_mode_null(mode);
658 bool constant_folding_old = constant_folding;
659 constant_folding = true;
661 enum_t *enume = type->enume;
662 entity_t *entry = enume->base.next;
663 for (; entry != NULL; entry = entry->base.next) {
664 if (entry->kind != ENTITY_ENUM_VALUE)
667 expression_t *const init = entry->enum_value.value;
669 ir_node *const cnst = expression_to_firm(init);
670 if (!is_Const(cnst)) {
671 panic("couldn't fold constant");
673 tv_next = get_Const_tarval(cnst);
675 entry->enum_value.tv = tv_next;
676 tv_next = tarval_add(tv_next, one);
679 constant_folding = constant_folding_old;
681 return create_atomic_type(type->akind, (const type_t*) type);
684 static ir_type *get_ir_type_incomplete(type_t *type)
686 assert(type != NULL);
687 type = skip_typeref(type);
689 if (type->base.firm_type != NULL) {
690 assert(type->base.firm_type != INVALID_TYPE);
691 return type->base.firm_type;
694 switch (type->kind) {
695 case TYPE_COMPOUND_STRUCT:
696 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
697 case TYPE_COMPOUND_UNION:
698 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
700 return get_ir_type(type);
704 ir_type *get_ir_type(type_t *type)
706 assert(type != NULL);
708 type = skip_typeref(type);
710 if (type->base.firm_type != NULL) {
711 assert(type->base.firm_type != INVALID_TYPE);
712 return type->base.firm_type;
715 ir_type *firm_type = NULL;
716 switch (type->kind) {
718 /* Happens while constant folding, when there was an error */
719 return create_atomic_type(ATOMIC_TYPE_VOID, NULL);
722 firm_type = create_atomic_type(type->atomic.akind, type);
725 firm_type = create_complex_type(&type->complex);
728 firm_type = create_imaginary_type(&type->imaginary);
731 firm_type = create_method_type(&type->function, false);
734 firm_type = create_pointer_type(&type->pointer);
737 firm_type = create_reference_type(&type->reference);
740 firm_type = create_array_type(&type->array);
742 case TYPE_COMPOUND_STRUCT:
743 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
745 case TYPE_COMPOUND_UNION:
746 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
749 firm_type = create_enum_type(&type->enumt);
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);
782 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
783 * int that it returns bigger modes for floating point on some platforms
784 * (x87 internally does arithemtic with 80bits)
786 static ir_mode *get_ir_mode_arithmetic(type_t *type)
788 ir_mode *mode = get_ir_mode_storage(type);
789 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
790 return mode_float_arithmetic;
796 /** Names of the runtime functions. */
797 static const struct {
798 int id; /**< the rts id */
799 int n_res; /**< number of return values */
800 const char *name; /**< the name of the rts function */
801 int n_params; /**< number of parameters */
802 unsigned flags; /**< language flags */
804 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
805 { rts_abort, 0, "abort", 0, _C89 },
806 { rts_alloca, 1, "alloca", 1, _ALL },
807 { rts_abs, 1, "abs", 1, _C89 },
808 { rts_labs, 1, "labs", 1, _C89 },
809 { rts_llabs, 1, "llabs", 1, _C99 },
810 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
812 { rts_fabs, 1, "fabs", 1, _C89 },
813 { rts_sqrt, 1, "sqrt", 1, _C89 },
814 { rts_cbrt, 1, "cbrt", 1, _C99 },
815 { rts_exp, 1, "exp", 1, _C89 },
816 { rts_exp2, 1, "exp2", 1, _C89 },
817 { rts_exp10, 1, "exp10", 1, _GNUC },
818 { rts_log, 1, "log", 1, _C89 },
819 { rts_log2, 1, "log2", 1, _C89 },
820 { rts_log10, 1, "log10", 1, _C89 },
821 { rts_pow, 1, "pow", 2, _C89 },
822 { rts_sin, 1, "sin", 1, _C89 },
823 { rts_cos, 1, "cos", 1, _C89 },
824 { rts_tan, 1, "tan", 1, _C89 },
825 { rts_asin, 1, "asin", 1, _C89 },
826 { rts_acos, 1, "acos", 1, _C89 },
827 { rts_atan, 1, "atan", 1, _C89 },
828 { rts_sinh, 1, "sinh", 1, _C89 },
829 { rts_cosh, 1, "cosh", 1, _C89 },
830 { rts_tanh, 1, "tanh", 1, _C89 },
832 { rts_fabsf, 1, "fabsf", 1, _C99 },
833 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
834 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
835 { rts_expf, 1, "expf", 1, _C99 },
836 { rts_exp2f, 1, "exp2f", 1, _C99 },
837 { rts_exp10f, 1, "exp10f", 1, _GNUC },
838 { rts_logf, 1, "logf", 1, _C99 },
839 { rts_log2f, 1, "log2f", 1, _C99 },
840 { rts_log10f, 1, "log10f", 1, _C99 },
841 { rts_powf, 1, "powf", 2, _C99 },
842 { rts_sinf, 1, "sinf", 1, _C99 },
843 { rts_cosf, 1, "cosf", 1, _C99 },
844 { rts_tanf, 1, "tanf", 1, _C99 },
845 { rts_asinf, 1, "asinf", 1, _C99 },
846 { rts_acosf, 1, "acosf", 1, _C99 },
847 { rts_atanf, 1, "atanf", 1, _C99 },
848 { rts_sinhf, 1, "sinhf", 1, _C99 },
849 { rts_coshf, 1, "coshf", 1, _C99 },
850 { rts_tanhf, 1, "tanhf", 1, _C99 },
852 { rts_fabsl, 1, "fabsl", 1, _C99 },
853 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
854 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
855 { rts_expl, 1, "expl", 1, _C99 },
856 { rts_exp2l, 1, "exp2l", 1, _C99 },
857 { rts_exp10l, 1, "exp10l", 1, _GNUC },
858 { rts_logl, 1, "logl", 1, _C99 },
859 { rts_log2l, 1, "log2l", 1, _C99 },
860 { rts_log10l, 1, "log10l", 1, _C99 },
861 { rts_powl, 1, "powl", 2, _C99 },
862 { rts_sinl, 1, "sinl", 1, _C99 },
863 { rts_cosl, 1, "cosl", 1, _C99 },
864 { rts_tanl, 1, "tanl", 1, _C99 },
865 { rts_asinl, 1, "asinl", 1, _C99 },
866 { rts_acosl, 1, "acosl", 1, _C99 },
867 { rts_atanl, 1, "atanl", 1, _C99 },
868 { rts_sinhl, 1, "sinhl", 1, _C99 },
869 { rts_coshl, 1, "coshl", 1, _C99 },
870 { rts_tanhl, 1, "tanhl", 1, _C99 },
872 { rts_strcmp, 1, "strcmp", 2, _C89 },
873 { rts_strncmp, 1, "strncmp", 3, _C89 },
874 { rts_strcpy, 1, "strcpy", 2, _C89 },
875 { rts_strlen, 1, "strlen", 1, _C89 },
876 { rts_memcpy, 1, "memcpy", 3, _C89 },
877 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
878 { rts_memmove, 1, "memmove", 3, _C89 },
879 { rts_memset, 1, "memset", 3, _C89 },
880 { rts_memcmp, 1, "memcmp", 3, _C89 },
883 static ident *rts_idents[lengthof(rts_data)];
885 static create_ld_ident_func create_ld_ident = create_name_linux_elf;
887 void set_create_ld_ident(ident *(*func)(entity_t*))
889 create_ld_ident = func;
893 * Handle GNU attributes for entities
895 * @param ent the entity
896 * @param decl the routine declaration
898 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
900 assert(is_declaration(entity));
901 decl_modifiers_t modifiers = entity->declaration.modifiers;
903 if (is_method_entity(irentity)) {
904 if (modifiers & DM_PURE) {
905 set_entity_additional_properties(irentity, mtp_property_pure);
907 if (modifiers & DM_CONST) {
908 add_entity_additional_properties(irentity, mtp_property_const);
911 if (modifiers & DM_USED) {
912 add_entity_linkage(irentity, IR_LINKAGE_HIDDEN_USER);
914 if (modifiers & DM_WEAK) {
915 add_entity_linkage(irentity, IR_LINKAGE_WEAK);
919 static bool is_main(entity_t *entity)
921 static symbol_t *sym_main = NULL;
922 if (sym_main == NULL) {
923 sym_main = symbol_table_insert("main");
926 if (entity->base.symbol != sym_main)
928 /* must be in outermost scope */
929 if (entity->base.parent_scope != ¤t_translation_unit->scope)
936 * Creates an entity representing a function.
938 * @param entity the function declaration/definition
939 * @param owner_type the owner type of this function, NULL
940 * for global functions
942 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
944 assert(entity->kind == ENTITY_FUNCTION);
945 if (entity->function.irentity != NULL) {
946 return entity->function.irentity;
949 entity_t *original_entity = entity;
950 if (entity->function.btk != bk_none) {
951 entity = get_builtin_replacement(entity);
956 if (is_main(entity)) {
957 /* force main to C linkage */
958 type_t *type = entity->declaration.type;
959 assert(is_type_function(type));
960 if (type->function.linkage != LINKAGE_C) {
961 type_t *new_type = duplicate_type(type);
962 new_type->function.linkage = LINKAGE_C;
963 type = identify_new_type(new_type);
964 entity->declaration.type = type;
968 symbol_t *symbol = entity->base.symbol;
969 ident *id = new_id_from_str(symbol->string);
971 /* already an entity defined? */
972 ir_entity *irentity = entitymap_get(&entitymap, symbol);
973 bool const has_body = entity->function.statement != NULL;
974 if (irentity != NULL) {
975 if (get_entity_visibility(irentity) == ir_visibility_external
977 set_entity_visibility(irentity, ir_visibility_default);
982 ir_type *ir_type_method;
983 if (entity->function.need_closure)
984 ir_type_method = create_method_type(&entity->declaration.type->function, true);
986 ir_type_method = get_ir_type(entity->declaration.type);
988 bool nested_function = false;
989 if (owner_type == NULL)
990 owner_type = get_glob_type();
992 nested_function = true;
994 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
995 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
999 ld_id = id_unique("inner.%u");
1001 ld_id = create_ld_ident(entity);
1002 set_entity_ld_ident(irentity, ld_id);
1004 handle_decl_modifiers(irentity, entity);
1006 if (! nested_function) {
1007 /* static inline => local
1008 * extern inline => local
1009 * inline without definition => local
1010 * inline with definition => external_visible */
1011 storage_class_tag_t const storage_class
1012 = (storage_class_tag_t) entity->declaration.storage_class;
1013 bool const is_inline = entity->function.is_inline;
1015 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1016 set_entity_visibility(irentity, ir_visibility_default);
1017 } else if (storage_class == STORAGE_CLASS_STATIC ||
1018 (is_inline && has_body)) {
1019 set_entity_visibility(irentity, ir_visibility_local);
1020 } else if (has_body) {
1021 set_entity_visibility(irentity, ir_visibility_default);
1023 set_entity_visibility(irentity, ir_visibility_external);
1026 /* nested functions are always local */
1027 set_entity_visibility(irentity, ir_visibility_local);
1030 /* We should check for file scope here, but as long as we compile C only
1031 this is not needed. */
1032 if (!freestanding && !has_body) {
1033 /* check for a known runtime function */
1034 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1035 if (id != rts_idents[i])
1038 function_type_t *function_type
1039 = &entity->declaration.type->function;
1040 /* rts_entities code can't handle a "wrong" number of parameters */
1041 if (function_type->unspecified_parameters)
1044 /* check number of parameters */
1045 int n_params = count_parameters(function_type);
1046 if (n_params != rts_data[i].n_params)
1049 type_t *return_type = skip_typeref(function_type->return_type);
1050 int n_res = return_type != type_void ? 1 : 0;
1051 if (n_res != rts_data[i].n_res)
1054 /* ignore those rts functions not necessary needed for current mode */
1055 if ((c_mode & rts_data[i].flags) == 0)
1057 assert(rts_entities[rts_data[i].id] == NULL);
1058 rts_entities[rts_data[i].id] = irentity;
1062 entitymap_insert(&entitymap, symbol, irentity);
1065 original_entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1066 original_entity->function.irentity = irentity;
1072 * Creates a SymConst for a given entity.
1074 * @param dbgi debug info
1075 * @param entity the entity
1077 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
1079 assert(entity != NULL);
1080 union symconst_symbol sym;
1081 sym.entity_p = entity;
1082 return new_d_SymConst(dbgi, mode_P, sym, symconst_addr_ent);
1085 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1087 ir_mode *value_mode = get_irn_mode(value);
1089 if (value_mode == dest_mode)
1092 if (dest_mode == mode_b) {
1093 ir_node *zero = new_Const(get_mode_null(value_mode));
1094 ir_node *cmp = new_d_Cmp(dbgi, value, zero, ir_relation_less_greater);
1098 return new_d_Conv(dbgi, value, dest_mode);
1101 static ir_node *create_Const_from_bool(ir_mode *const mode, bool const v)
1103 return new_Const((v ? get_mode_one : get_mode_null)(mode));
1107 * Creates a SymConst node representing a wide string literal.
1109 * @param literal the wide string literal
1111 static ir_node *wide_string_literal_to_firm(
1112 const string_literal_expression_t *literal)
1114 ir_type *const global_type = get_glob_type();
1115 ir_type *const elem_type = ir_type_wchar_t;
1116 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1117 ir_type *const type = new_type_array(1, elem_type);
1119 ident *const id = id_unique("str.%u");
1120 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1121 set_entity_ld_ident(entity, id);
1122 set_entity_visibility(entity, ir_visibility_private);
1123 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1125 ir_mode *const mode = get_type_mode(elem_type);
1126 const size_t slen = wstrlen(&literal->value);
1128 set_array_lower_bound_int(type, 0, 0);
1129 set_array_upper_bound_int(type, 0, slen);
1130 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1131 set_type_state(type, layout_fixed);
1133 ir_initializer_t *initializer = create_initializer_compound(slen);
1134 const char *p = literal->value.begin;
1135 for (size_t i = 0; i < slen; ++i) {
1136 assert(p < literal->value.begin + literal->value.size);
1137 utf32 v = read_utf8_char(&p);
1138 ir_tarval *tv = new_tarval_from_long(v, mode);
1139 ir_initializer_t *val = create_initializer_tarval(tv);
1140 set_initializer_compound_value(initializer, i, val);
1142 set_entity_initializer(entity, initializer);
1144 return create_symconst(dbgi, entity);
1148 * Creates a SymConst node representing a string constant.
1150 * @param src_pos the source position of the string constant
1151 * @param id_prefix a prefix for the name of the generated string constant
1152 * @param value the value of the string constant
1154 static ir_node *string_to_firm(const source_position_t *const src_pos,
1155 const char *const id_prefix,
1156 const string_t *const value)
1158 ir_type *const global_type = get_glob_type();
1159 dbg_info *const dbgi = get_dbg_info(src_pos);
1160 ir_type *const type = new_type_array(1, ir_type_const_char);
1162 ident *const id = id_unique(id_prefix);
1163 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1164 set_entity_ld_ident(entity, id);
1165 set_entity_visibility(entity, ir_visibility_private);
1166 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1168 ir_type *const elem_type = ir_type_const_char;
1169 ir_mode *const mode = get_type_mode(elem_type);
1171 const char* const string = value->begin;
1172 const size_t slen = value->size;
1174 set_array_lower_bound_int(type, 0, 0);
1175 set_array_upper_bound_int(type, 0, slen);
1176 set_type_size_bytes(type, slen);
1177 set_type_state(type, layout_fixed);
1179 ir_initializer_t *initializer = create_initializer_compound(slen);
1180 for (size_t i = 0; i < slen; ++i) {
1181 ir_tarval *tv = new_tarval_from_long(string[i], mode);
1182 ir_initializer_t *val = create_initializer_tarval(tv);
1183 set_initializer_compound_value(initializer, i, val);
1185 set_entity_initializer(entity, initializer);
1187 return create_symconst(dbgi, entity);
1190 static bool try_create_integer(literal_expression_t *literal,
1191 type_t *type, unsigned char base)
1193 const char *string = literal->value.begin;
1194 size_t size = literal->value.size;
1196 assert(type->kind == TYPE_ATOMIC);
1197 atomic_type_kind_t akind = type->atomic.akind;
1199 ir_mode *mode = atomic_modes[akind];
1200 ir_tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1201 if (tv == tarval_bad)
1204 literal->base.type = type;
1205 literal->target_value = tv;
1209 static void create_integer_tarval(literal_expression_t *literal)
1213 symbol_t *suffix = literal->suffix;
1215 if (suffix != NULL) {
1216 for (const char *c = suffix->string; *c != '\0'; ++c) {
1217 if (*c == 'u' || *c == 'U') { ++us; }
1218 if (*c == 'l' || *c == 'L') { ++ls; }
1223 switch (literal->base.kind) {
1224 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1225 case EXPR_LITERAL_INTEGER: base = 10; break;
1226 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1227 default: panic("invalid literal kind");
1230 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1232 /* now try if the constant is small enough for some types */
1233 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1235 if (us == 0 && try_create_integer(literal, type_int, base))
1237 if ((us == 1 || base != 10)
1238 && try_create_integer(literal, type_unsigned_int, base))
1242 if (us == 0 && try_create_integer(literal, type_long, base))
1244 if ((us == 1 || base != 10)
1245 && try_create_integer(literal, type_unsigned_long, base))
1248 /* last try? then we should not report tarval_bad */
1249 if (us != 1 && base == 10)
1250 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1251 if (us == 0 && try_create_integer(literal, type_long_long, base))
1255 assert(us == 1 || base != 10);
1256 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1257 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1259 panic("internal error when parsing number literal");
1262 tarval_set_integer_overflow_mode(old_mode);
1265 void determine_literal_type(literal_expression_t *literal)
1267 switch (literal->base.kind) {
1268 case EXPR_LITERAL_INTEGER:
1269 case EXPR_LITERAL_INTEGER_OCTAL:
1270 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1271 create_integer_tarval(literal);
1279 * Creates a Const node representing a constant.
1281 static ir_node *literal_to_firm(const literal_expression_t *literal)
1283 type_t *type = skip_typeref(literal->base.type);
1284 ir_mode *mode = get_ir_mode_storage(type);
1285 const char *string = literal->value.begin;
1286 size_t size = literal->value.size;
1289 switch (literal->base.kind) {
1290 case EXPR_LITERAL_WIDE_CHARACTER: {
1291 utf32 v = read_utf8_char(&string);
1293 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1295 tv = new_tarval_from_str(buf, len, mode);
1298 case EXPR_LITERAL_CHARACTER: {
1300 if (size == 1 && char_is_signed) {
1301 v = (signed char)string[0];
1304 for (size_t i = 0; i < size; ++i) {
1305 v = (v << 8) | ((unsigned char)string[i]);
1309 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1311 tv = new_tarval_from_str(buf, len, mode);
1314 case EXPR_LITERAL_INTEGER:
1315 case EXPR_LITERAL_INTEGER_OCTAL:
1316 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1317 assert(literal->target_value != NULL);
1318 tv = literal->target_value;
1320 case EXPR_LITERAL_FLOATINGPOINT:
1321 tv = new_tarval_from_str(string, size, mode);
1323 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1324 char buffer[size + 2];
1325 memcpy(buffer, "0x", 2);
1326 memcpy(buffer+2, string, size);
1327 tv = new_tarval_from_str(buffer, size+2, mode);
1330 case EXPR_LITERAL_BOOLEAN:
1331 if (string[0] == 't') {
1332 tv = get_mode_one(mode);
1334 assert(string[0] == 'f');
1335 tv = get_mode_null(mode);
1338 case EXPR_LITERAL_MS_NOOP:
1339 tv = get_mode_null(mode);
1344 panic("Invalid literal kind found");
1347 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1348 ir_node *res = new_d_Const(dbgi, tv);
1349 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1350 return create_conv(dbgi, res, mode_arith);
1354 * Allocate an area of size bytes aligned at alignment
1357 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1359 static unsigned area_cnt = 0;
1362 ir_type *tp = new_type_array(1, ir_type_char);
1363 set_array_bounds_int(tp, 0, 0, size);
1364 set_type_alignment_bytes(tp, alignment);
1366 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1367 ident *name = new_id_from_str(buf);
1368 ir_entity *area = new_entity(frame_type, name, tp);
1370 /* mark this entity as compiler generated */
1371 set_entity_compiler_generated(area, 1);
1376 * Return a node representing a trampoline region
1377 * for a given function entity.
1379 * @param dbgi debug info
1380 * @param entity the function entity
1382 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1384 ir_entity *region = NULL;
1387 if (current_trampolines != NULL) {
1388 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1389 if (current_trampolines[i].function == entity) {
1390 region = current_trampolines[i].region;
1395 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1397 ir_graph *irg = current_ir_graph;
1398 if (region == NULL) {
1399 /* create a new region */
1400 ir_type *frame_tp = get_irg_frame_type(irg);
1401 trampoline_region reg;
1402 reg.function = entity;
1404 reg.region = alloc_trampoline(frame_tp,
1405 be_params->trampoline_size,
1406 be_params->trampoline_align);
1407 ARR_APP1(trampoline_region, current_trampolines, reg);
1408 region = reg.region;
1410 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1415 * Creates a trampoline for a function represented by an entity.
1417 * @param dbgi debug info
1418 * @param mode the (reference) mode for the function address
1419 * @param entity the function entity
1421 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1424 assert(entity != NULL);
1426 in[0] = get_trampoline_region(dbgi, entity);
1427 in[1] = create_symconst(dbgi, entity);
1428 in[2] = get_irg_frame(current_ir_graph);
1430 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1431 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1432 return new_Proj(irn, mode, pn_Builtin_1_result);
1436 * Dereference an address.
1438 * @param dbgi debug info
1439 * @param type the type of the dereferenced result (the points_to type)
1440 * @param addr the address to dereference
1442 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1443 ir_node *const addr)
1445 ir_type *irtype = get_ir_type(type);
1446 if (is_compound_type(irtype)
1447 || is_Method_type(irtype)
1448 || is_Array_type(irtype)) {
1452 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1453 ? cons_volatile : cons_none;
1454 ir_mode *const mode = get_type_mode(irtype);
1455 ir_node *const memory = get_store();
1456 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1457 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1458 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1460 set_store(load_mem);
1462 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1463 return create_conv(dbgi, load_res, mode_arithmetic);
1467 * Creates a strict Conv (to the node's mode) if necessary.
1469 * @param dbgi debug info
1470 * @param node the node to strict conv
1472 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1474 ir_mode *mode = get_irn_mode(node);
1476 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1478 if (!mode_is_float(mode))
1481 /* check if there is already a Conv */
1482 if (is_Conv(node)) {
1483 /* convert it into a strict Conv */
1484 set_Conv_strict(node, 1);
1488 /* otherwise create a new one */
1489 return new_d_strictConv(dbgi, node, mode);
1493 * Returns the correct base address depending on whether it is a parameter or a
1494 * normal local variable.
1496 static ir_node *get_local_frame(ir_entity *const ent)
1498 ir_graph *const irg = current_ir_graph;
1499 const ir_type *const owner = get_entity_owner(ent);
1500 if (owner == current_outer_frame || owner == current_outer_value_type) {
1501 assert(current_static_link != NULL);
1502 return current_static_link;
1504 return get_irg_frame(irg);
1509 * Keep all memory edges of the given block.
1511 static void keep_all_memory(ir_node *block)
1513 ir_node *old = get_cur_block();
1515 set_cur_block(block);
1516 keep_alive(get_store());
1517 /* TODO: keep all memory edges from restricted pointers */
1521 static ir_node *reference_expression_enum_value_to_firm(
1522 const reference_expression_t *ref)
1524 entity_t *entity = ref->entity;
1525 type_t *type = skip_typeref(entity->enum_value.enum_type);
1526 /* make sure the type is constructed */
1527 (void) get_ir_type(type);
1529 return new_Const(entity->enum_value.tv);
1532 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1534 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1535 entity_t *entity = ref->entity;
1536 assert(is_declaration(entity));
1537 type_t *type = skip_typeref(entity->declaration.type);
1539 /* make sure the type is constructed */
1540 (void) get_ir_type(type);
1542 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1543 ir_entity *irentity = get_function_entity(entity, NULL);
1544 /* for gcc compatibility we have to produce (dummy) addresses for some
1545 * builtins which don't have entities */
1546 if (irentity == NULL) {
1547 if (warning.other) {
1548 warningf(&ref->base.source_position,
1549 "taking address of builtin '%Y'",
1550 ref->entity->base.symbol);
1553 /* simply create a NULL pointer */
1554 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1555 ir_node *res = new_Const(get_mode_null(mode));
1561 switch ((declaration_kind_t) entity->declaration.kind) {
1562 case DECLARATION_KIND_UNKNOWN:
1565 case DECLARATION_KIND_LOCAL_VARIABLE: {
1566 ir_mode *const mode = get_ir_mode_storage(type);
1567 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1568 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1570 case DECLARATION_KIND_PARAMETER: {
1571 ir_mode *const mode = get_ir_mode_storage(type);
1572 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1573 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1575 case DECLARATION_KIND_FUNCTION: {
1576 return create_symconst(dbgi, entity->function.irentity);
1578 case DECLARATION_KIND_INNER_FUNCTION: {
1579 ir_mode *const mode = get_ir_mode_storage(type);
1580 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1581 /* inner function not using the closure */
1582 return create_symconst(dbgi, entity->function.irentity);
1584 /* need trampoline here */
1585 return create_trampoline(dbgi, mode, entity->function.irentity);
1588 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1589 const variable_t *variable = &entity->variable;
1590 ir_node *const addr = create_symconst(dbgi, variable->v.entity);
1591 return deref_address(dbgi, variable->base.type, addr);
1594 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1595 ir_entity *irentity = entity->variable.v.entity;
1596 ir_node *frame = get_local_frame(irentity);
1597 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1598 return deref_address(dbgi, entity->declaration.type, sel);
1600 case DECLARATION_KIND_PARAMETER_ENTITY: {
1601 ir_entity *irentity = entity->parameter.v.entity;
1602 ir_node *frame = get_local_frame(irentity);
1603 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1604 return deref_address(dbgi, entity->declaration.type, sel);
1607 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1608 return entity->variable.v.vla_base;
1610 case DECLARATION_KIND_COMPOUND_MEMBER:
1611 panic("not implemented reference type");
1614 panic("reference to declaration with unknown type found");
1617 static ir_node *reference_addr(const reference_expression_t *ref)
1619 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1620 entity_t *entity = ref->entity;
1621 assert(is_declaration(entity));
1623 switch((declaration_kind_t) entity->declaration.kind) {
1624 case DECLARATION_KIND_UNKNOWN:
1626 case DECLARATION_KIND_PARAMETER:
1627 case DECLARATION_KIND_LOCAL_VARIABLE:
1628 /* you can store to a local variable (so we don't panic but return NULL
1629 * as an indicator for no real address) */
1631 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1632 ir_node *const addr = create_symconst(dbgi, entity->variable.v.entity);
1635 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1636 ir_entity *irentity = entity->variable.v.entity;
1637 ir_node *frame = get_local_frame(irentity);
1638 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1642 case DECLARATION_KIND_PARAMETER_ENTITY: {
1643 ir_entity *irentity = entity->parameter.v.entity;
1644 ir_node *frame = get_local_frame(irentity);
1645 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1650 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1651 return entity->variable.v.vla_base;
1653 case DECLARATION_KIND_FUNCTION: {
1654 return create_symconst(dbgi, entity->function.irentity);
1657 case DECLARATION_KIND_INNER_FUNCTION: {
1658 type_t *const type = skip_typeref(entity->declaration.type);
1659 ir_mode *const mode = get_ir_mode_storage(type);
1660 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1661 /* inner function not using the closure */
1662 return create_symconst(dbgi, entity->function.irentity);
1664 /* need trampoline here */
1665 return create_trampoline(dbgi, mode, entity->function.irentity);
1669 case DECLARATION_KIND_COMPOUND_MEMBER:
1670 panic("not implemented reference type");
1673 panic("reference to declaration with unknown type found");
1677 * Generate an unary builtin.
1679 * @param kind the builtin kind to generate
1680 * @param op the operand
1681 * @param function_type the function type for the GNU builtin routine
1682 * @param db debug info
1684 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1687 in[0] = expression_to_firm(op);
1689 ir_type *tp = get_ir_type(function_type);
1690 ir_type *res = get_method_res_type(tp, 0);
1691 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1692 set_irn_pinned(irn, op_pin_state_floats);
1693 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1697 * Generate a pinned 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_pinned(ir_builtin_kind kind, expression_t *op,
1705 type_t *function_type, dbg_info *db)
1708 in[0] = expression_to_firm(op);
1710 ir_type *tp = get_ir_type(function_type);
1711 ir_type *res = get_method_res_type(tp, 0);
1712 ir_node *mem = get_store();
1713 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1714 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1715 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1719 * Generate an binary-void-return builtin.
1721 * @param kind the builtin kind to generate
1722 * @param op1 the first operand
1723 * @param op2 the second operand
1724 * @param function_type the function type for the GNU builtin routine
1725 * @param db debug info
1727 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1728 expression_t *op2, type_t *function_type,
1732 in[0] = expression_to_firm(op1);
1733 in[1] = expression_to_firm(op2);
1735 ir_type *tp = get_ir_type(function_type);
1736 ir_node *mem = get_store();
1737 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1738 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1743 * Transform calls to builtin functions.
1745 static ir_node *process_builtin_call(const call_expression_t *call)
1747 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1749 assert(call->function->kind == EXPR_REFERENCE);
1750 reference_expression_t *builtin = &call->function->reference;
1752 type_t *expr_type = skip_typeref(builtin->base.type);
1753 assert(is_type_pointer(expr_type));
1755 type_t *function_type = skip_typeref(expr_type->pointer.points_to);
1757 switch (builtin->entity->function.btk) {
1758 case bk_gnu_builtin_alloca: {
1759 if (call->arguments == NULL || call->arguments->next != NULL) {
1760 panic("invalid number of parameters on __builtin_alloca");
1762 expression_t *argument = call->arguments->expression;
1763 ir_node *size = expression_to_firm(argument);
1765 ir_node *store = get_store();
1766 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1768 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1770 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1775 case bk_gnu_builtin_huge_val:
1776 case bk_gnu_builtin_huge_valf:
1777 case bk_gnu_builtin_huge_vall:
1778 case bk_gnu_builtin_inf:
1779 case bk_gnu_builtin_inff:
1780 case bk_gnu_builtin_infl: {
1781 type_t *type = function_type->function.return_type;
1782 ir_mode *mode = get_ir_mode_arithmetic(type);
1783 ir_tarval *tv = get_mode_infinite(mode);
1784 ir_node *res = new_d_Const(dbgi, tv);
1787 case bk_gnu_builtin_nan:
1788 case bk_gnu_builtin_nanf:
1789 case bk_gnu_builtin_nanl: {
1790 /* Ignore string for now... */
1791 assert(is_type_function(function_type));
1792 type_t *type = function_type->function.return_type;
1793 ir_mode *mode = get_ir_mode_arithmetic(type);
1794 ir_tarval *tv = get_mode_NAN(mode);
1795 ir_node *res = new_d_Const(dbgi, tv);
1798 case bk_gnu_builtin_expect: {
1799 expression_t *argument = call->arguments->expression;
1800 return _expression_to_firm(argument);
1802 case bk_gnu_builtin_va_end:
1803 /* evaluate the argument of va_end for its side effects */
1804 _expression_to_firm(call->arguments->expression);
1806 case bk_gnu_builtin_frame_address: {
1807 expression_t *const expression = call->arguments->expression;
1808 bool val = fold_constant_to_bool(expression);
1811 return get_irg_frame(current_ir_graph);
1813 /* get the argument */
1816 in[0] = expression_to_firm(expression);
1817 in[1] = get_irg_frame(current_ir_graph);
1818 ir_type *tp = get_ir_type(function_type);
1819 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1820 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1823 case bk_gnu_builtin_return_address: {
1824 expression_t *const expression = call->arguments->expression;
1827 in[0] = expression_to_firm(expression);
1828 in[1] = get_irg_frame(current_ir_graph);
1829 ir_type *tp = get_ir_type(function_type);
1830 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1831 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1833 case bk_gnu_builtin_ffs:
1834 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1835 case bk_gnu_builtin_clz:
1836 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1837 case bk_gnu_builtin_ctz:
1838 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1839 case bk_gnu_builtin_popcount:
1840 case bk_ms__popcount:
1841 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1842 case bk_gnu_builtin_parity:
1843 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1844 case bk_gnu_builtin_prefetch: {
1845 call_argument_t *const args = call->arguments;
1846 expression_t *const addr = args->expression;
1849 in[0] = _expression_to_firm(addr);
1850 if (args->next != NULL) {
1851 expression_t *const rw = args->next->expression;
1853 in[1] = _expression_to_firm(rw);
1855 if (args->next->next != NULL) {
1856 expression_t *const locality = args->next->next->expression;
1858 in[2] = expression_to_firm(locality);
1860 in[2] = new_Const_long(mode_int, 3);
1863 in[1] = new_Const_long(mode_int, 0);
1864 in[2] = new_Const_long(mode_int, 3);
1866 ir_type *tp = get_ir_type(function_type);
1867 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1868 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1871 case bk_gnu_builtin_object_size: {
1872 /* determine value of "type" */
1873 expression_t *type_expression = call->arguments->next->expression;
1874 long type_val = fold_constant_to_int(type_expression);
1875 type_t *type = function_type->function.return_type;
1876 ir_mode *mode = get_ir_mode_arithmetic(type);
1877 /* just produce a "I don't know" result */
1878 ir_tarval *result = type_val & 2 ? get_mode_null(mode) :
1879 get_mode_minus_one(mode);
1881 return new_d_Const(dbgi, result);
1883 case bk_gnu_builtin_trap:
1886 ir_type *tp = get_ir_type(function_type);
1887 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1888 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1891 case bk_ms__debugbreak: {
1892 ir_type *tp = get_ir_type(function_type);
1893 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1894 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1897 case bk_ms_ReturnAddress: {
1900 in[0] = new_Const(get_mode_null(mode_int));
1901 in[1] = get_irg_frame(current_ir_graph);
1902 ir_type *tp = get_ir_type(function_type);
1903 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1904 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1907 case bk_ms_rotl64: {
1908 ir_node *val = expression_to_firm(call->arguments->expression);
1909 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1910 ir_mode *mode = get_irn_mode(val);
1911 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1914 case bk_ms_rotr64: {
1915 ir_node *val = expression_to_firm(call->arguments->expression);
1916 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1917 ir_mode *mode = get_irn_mode(val);
1918 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1919 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1920 return new_d_Rotl(dbgi, val, sub, mode);
1922 case bk_ms_byteswap_ushort:
1923 case bk_ms_byteswap_ulong:
1924 case bk_ms_byteswap_uint64:
1925 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1928 case bk_ms__indword:
1929 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1930 case bk_ms__outbyte:
1931 case bk_ms__outword:
1932 case bk_ms__outdword:
1933 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1934 call->arguments->next->expression, function_type, dbgi);
1936 panic("unsupported builtin found");
1941 * Transform a call expression.
1942 * Handles some special cases, like alloca() calls, which must be resolved
1943 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1944 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1947 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1949 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1950 assert(currently_reachable());
1952 expression_t *function = call->function;
1953 if (function->kind == EXPR_REFERENCE) {
1954 const reference_expression_t *ref = &function->reference;
1955 entity_t *entity = ref->entity;
1957 if (entity->kind == ENTITY_FUNCTION) {
1958 ir_entity *irentity = entity->function.irentity;
1959 if (irentity == NULL)
1960 irentity = get_function_entity(entity, NULL);
1962 if (irentity == NULL && entity->function.btk != bk_none) {
1963 return process_builtin_call(call);
1967 if (irentity == rts_entities[rts_alloca]) {
1968 /* handle alloca() call */
1969 expression_t *argument = call->arguments->expression;
1970 ir_node *size = expression_to_firm(argument);
1971 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1973 size = create_conv(dbgi, size, mode);
1975 ir_node *store = get_store();
1976 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1977 firm_unknown_type, stack_alloc);
1978 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1980 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1987 ir_node *callee = expression_to_firm(function);
1989 type_t *type = skip_typeref(function->base.type);
1990 assert(is_type_pointer(type));
1991 pointer_type_t *pointer_type = &type->pointer;
1992 type_t *points_to = skip_typeref(pointer_type->points_to);
1993 assert(is_type_function(points_to));
1994 function_type_t *function_type = &points_to->function;
1996 int n_parameters = 0;
1997 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1998 ir_type *new_method_type = NULL;
1999 if (function_type->variadic || function_type->unspecified_parameters) {
2000 const call_argument_t *argument = call->arguments;
2001 for ( ; argument != NULL; argument = argument->next) {
2005 /* we need to construct a new method type matching the call
2007 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2008 int n_res = get_method_n_ress(ir_method_type);
2009 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2010 set_method_calling_convention(new_method_type,
2011 get_method_calling_convention(ir_method_type));
2012 set_method_additional_properties(new_method_type,
2013 get_method_additional_properties(ir_method_type));
2014 set_method_variadicity(new_method_type,
2015 get_method_variadicity(ir_method_type));
2017 for (int i = 0; i < n_res; ++i) {
2018 set_method_res_type(new_method_type, i,
2019 get_method_res_type(ir_method_type, i));
2021 argument = call->arguments;
2022 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2023 expression_t *expression = argument->expression;
2024 ir_type *irtype = get_ir_type(expression->base.type);
2025 set_method_param_type(new_method_type, i, irtype);
2027 ir_method_type = new_method_type;
2029 n_parameters = get_method_n_params(ir_method_type);
2032 ir_node *in[n_parameters];
2034 const call_argument_t *argument = call->arguments;
2035 for (int n = 0; n < n_parameters; ++n) {
2036 expression_t *expression = argument->expression;
2037 ir_node *arg_node = expression_to_firm(expression);
2039 type_t *arg_type = skip_typeref(expression->base.type);
2040 if (!is_type_compound(arg_type)) {
2041 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2042 arg_node = create_conv(dbgi, arg_node, mode);
2043 arg_node = do_strict_conv(dbgi, arg_node);
2048 argument = argument->next;
2051 ir_node *store = get_store();
2052 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2054 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2057 type_t *return_type = skip_typeref(function_type->return_type);
2058 ir_node *result = NULL;
2060 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2061 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2063 if (is_type_scalar(return_type)) {
2064 ir_mode *mode = get_ir_mode_storage(return_type);
2065 result = new_d_Proj(dbgi, resproj, mode, 0);
2066 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2067 result = create_conv(NULL, result, mode_arith);
2069 ir_mode *mode = mode_P_data;
2070 result = new_d_Proj(dbgi, resproj, mode, 0);
2074 if (function->kind == EXPR_REFERENCE &&
2075 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2076 /* A dead end: Keep the Call and the Block. Also place all further
2077 * nodes into a new and unreachable block. */
2079 keep_alive(get_cur_block());
2080 ir_node *block = new_Block(0, NULL);
2081 set_cur_block(block);
2087 static void statement_to_firm(statement_t *statement);
2088 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2090 static ir_node *expression_to_addr(const expression_t *expression);
2091 static ir_node *create_condition_evaluation(const expression_t *expression,
2092 ir_node *true_block,
2093 ir_node *false_block);
2095 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2098 if (!is_type_compound(type)) {
2099 ir_mode *mode = get_ir_mode_storage(type);
2100 value = create_conv(dbgi, value, mode);
2101 value = do_strict_conv(dbgi, value);
2104 ir_node *memory = get_store();
2106 if (is_type_scalar(type)) {
2107 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2108 ? cons_volatile : cons_none;
2109 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2110 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2111 set_store(store_mem);
2113 ir_type *irtype = get_ir_type(type);
2114 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2115 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2116 set_store(copyb_mem);
2120 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2122 ir_tarval *all_one = get_mode_all_one(mode);
2123 int mode_size = get_mode_size_bits(mode);
2125 assert(offset >= 0);
2127 assert(offset + size <= mode_size);
2128 if (size == mode_size) {
2132 long shiftr = get_mode_size_bits(mode) - size;
2133 long shiftl = offset;
2134 ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2135 ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2136 ir_tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2137 ir_tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2142 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2143 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2145 ir_type *entity_type = get_entity_type(entity);
2146 ir_type *base_type = get_primitive_base_type(entity_type);
2147 assert(base_type != NULL);
2148 ir_mode *mode = get_type_mode(base_type);
2150 value = create_conv(dbgi, value, mode);
2152 /* kill upper bits of value and shift to right position */
2153 int bitoffset = get_entity_offset_bits_remainder(entity);
2154 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2156 ir_tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2157 ir_node *mask_node = new_d_Const(dbgi, mask);
2158 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2159 ir_tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2160 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2161 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2163 /* load current value */
2164 ir_node *mem = get_store();
2165 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2166 set_volatile ? cons_volatile : cons_none);
2167 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2168 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2169 ir_tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2170 ir_tarval *inv_mask = tarval_not(shift_mask);
2171 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2172 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2174 /* construct new value and store */
2175 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2176 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2177 set_volatile ? cons_volatile : cons_none);
2178 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2179 set_store(store_mem);
2181 return value_masked;
2184 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2187 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2188 type_t *type = expression->base.type;
2189 ir_mode *mode = get_ir_mode_storage(type);
2190 ir_node *mem = get_store();
2191 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2192 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2193 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2195 load_res = create_conv(dbgi, load_res, mode_int);
2197 set_store(load_mem);
2199 /* kill upper bits */
2200 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2201 ir_entity *entity = expression->compound_entry->compound_member.entity;
2202 int bitoffset = get_entity_offset_bits_remainder(entity);
2203 ir_type *entity_type = get_entity_type(entity);
2204 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2205 long shift_bitsl = machine_size - bitoffset - bitsize;
2206 assert(shift_bitsl >= 0);
2207 ir_tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2208 ir_node *countl = new_d_Const(dbgi, tvl);
2209 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2211 long shift_bitsr = bitoffset + shift_bitsl;
2212 assert(shift_bitsr <= (long) machine_size);
2213 ir_tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2214 ir_node *countr = new_d_Const(dbgi, tvr);
2216 if (mode_is_signed(mode)) {
2217 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2219 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2222 return create_conv(dbgi, shiftr, mode);
2225 /* make sure the selected compound type is constructed */
2226 static void construct_select_compound(const select_expression_t *expression)
2228 type_t *type = skip_typeref(expression->compound->base.type);
2229 if (is_type_pointer(type)) {
2230 type = type->pointer.points_to;
2232 (void) get_ir_type(type);
2235 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2236 ir_node *value, ir_node *addr)
2238 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2239 type_t *type = skip_typeref(expression->base.type);
2241 if (!is_type_compound(type)) {
2242 ir_mode *mode = get_ir_mode_storage(type);
2243 value = create_conv(dbgi, value, mode);
2244 value = do_strict_conv(dbgi, value);
2247 if (expression->kind == EXPR_REFERENCE) {
2248 const reference_expression_t *ref = &expression->reference;
2250 entity_t *entity = ref->entity;
2251 assert(is_declaration(entity));
2252 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2253 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2254 set_value(entity->variable.v.value_number, value);
2256 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2257 set_value(entity->parameter.v.value_number, value);
2263 addr = expression_to_addr(expression);
2264 assert(addr != NULL);
2266 if (expression->kind == EXPR_SELECT) {
2267 const select_expression_t *select = &expression->select;
2269 construct_select_compound(select);
2271 entity_t *entity = select->compound_entry;
2272 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2273 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2274 ir_entity *irentity = entity->compound_member.entity;
2276 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2277 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2283 assign_value(dbgi, addr, type, value);
2287 static void set_value_for_expression(const expression_t *expression,
2290 set_value_for_expression_addr(expression, value, NULL);
2293 static ir_node *get_value_from_lvalue(const expression_t *expression,
2296 if (expression->kind == EXPR_REFERENCE) {
2297 const reference_expression_t *ref = &expression->reference;
2299 entity_t *entity = ref->entity;
2300 assert(entity->kind == ENTITY_VARIABLE
2301 || entity->kind == ENTITY_PARAMETER);
2302 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2304 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2305 value_number = entity->variable.v.value_number;
2306 assert(addr == NULL);
2307 type_t *type = skip_typeref(expression->base.type);
2308 ir_mode *mode = get_ir_mode_storage(type);
2309 ir_node *res = get_value(value_number, mode);
2310 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2311 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2312 value_number = entity->parameter.v.value_number;
2313 assert(addr == NULL);
2314 type_t *type = skip_typeref(expression->base.type);
2315 ir_mode *mode = get_ir_mode_storage(type);
2316 ir_node *res = get_value(value_number, mode);
2317 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2321 assert(addr != NULL);
2322 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2325 if (expression->kind == EXPR_SELECT &&
2326 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2327 construct_select_compound(&expression->select);
2328 value = bitfield_extract_to_firm(&expression->select, addr);
2330 value = deref_address(dbgi, expression->base.type, addr);
2337 static ir_node *create_incdec(const unary_expression_t *expression)
2339 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2340 const expression_t *value_expr = expression->value;
2341 ir_node *addr = expression_to_addr(value_expr);
2342 ir_node *value = get_value_from_lvalue(value_expr, addr);
2344 type_t *type = skip_typeref(expression->base.type);
2345 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2348 if (is_type_pointer(type)) {
2349 pointer_type_t *pointer_type = &type->pointer;
2350 offset = get_type_size_node(pointer_type->points_to);
2352 assert(is_type_arithmetic(type));
2353 offset = new_Const(get_mode_one(mode));
2357 ir_node *store_value;
2358 switch(expression->base.kind) {
2359 case EXPR_UNARY_POSTFIX_INCREMENT:
2361 store_value = new_d_Add(dbgi, value, offset, mode);
2363 case EXPR_UNARY_POSTFIX_DECREMENT:
2365 store_value = new_d_Sub(dbgi, value, offset, mode);
2367 case EXPR_UNARY_PREFIX_INCREMENT:
2368 result = new_d_Add(dbgi, value, offset, mode);
2369 store_value = result;
2371 case EXPR_UNARY_PREFIX_DECREMENT:
2372 result = new_d_Sub(dbgi, value, offset, mode);
2373 store_value = result;
2376 panic("no incdec expr in create_incdec");
2379 set_value_for_expression_addr(value_expr, store_value, addr);
2384 static bool is_local_variable(expression_t *expression)
2386 if (expression->kind != EXPR_REFERENCE)
2388 reference_expression_t *ref_expr = &expression->reference;
2389 entity_t *entity = ref_expr->entity;
2390 if (entity->kind != ENTITY_VARIABLE)
2392 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2393 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2396 static ir_relation get_relation(const expression_kind_t kind)
2399 case EXPR_BINARY_EQUAL: return ir_relation_equal;
2400 case EXPR_BINARY_ISLESSGREATER: return ir_relation_less_greater;
2401 case EXPR_BINARY_NOTEQUAL: return ir_relation_unordered_less_greater;
2402 case EXPR_BINARY_ISLESS:
2403 case EXPR_BINARY_LESS: return ir_relation_less;
2404 case EXPR_BINARY_ISLESSEQUAL:
2405 case EXPR_BINARY_LESSEQUAL: return ir_relation_less_equal;
2406 case EXPR_BINARY_ISGREATER:
2407 case EXPR_BINARY_GREATER: return ir_relation_greater;
2408 case EXPR_BINARY_ISGREATEREQUAL:
2409 case EXPR_BINARY_GREATEREQUAL: return ir_relation_greater_equal;
2410 case EXPR_BINARY_ISUNORDERED: return ir_relation_unordered;
2415 panic("trying to get pn_Cmp from non-comparison binexpr type");
2419 * Handle the assume optimizer hint: check if a Confirm
2420 * node can be created.
2422 * @param dbi debug info
2423 * @param expr the IL assume expression
2425 * we support here only some simple cases:
2430 static ir_node *handle_assume_compare(dbg_info *dbi,
2431 const binary_expression_t *expression)
2433 expression_t *op1 = expression->left;
2434 expression_t *op2 = expression->right;
2435 entity_t *var2, *var = NULL;
2436 ir_node *res = NULL;
2437 ir_relation relation = get_relation(expression->base.kind);
2439 if (is_local_variable(op1) && is_local_variable(op2)) {
2440 var = op1->reference.entity;
2441 var2 = op2->reference.entity;
2443 type_t *const type = skip_typeref(var->declaration.type);
2444 ir_mode *const mode = get_ir_mode_storage(type);
2446 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2447 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2449 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_relation(relation));
2450 set_value(var2->variable.v.value_number, res);
2452 res = new_d_Confirm(dbi, irn1, irn2, relation);
2453 set_value(var->variable.v.value_number, res);
2458 expression_t *con = NULL;
2459 if (is_local_variable(op1) && is_constant_expression(op2) == EXPR_CLASS_CONSTANT) {
2460 var = op1->reference.entity;
2462 } else if (is_constant_expression(op1) == EXPR_CLASS_CONSTANT && is_local_variable(op2)) {
2463 relation = get_inversed_relation(relation);
2464 var = op2->reference.entity;
2469 type_t *const type = skip_typeref(var->declaration.type);
2470 ir_mode *const mode = get_ir_mode_storage(type);
2472 res = get_value(var->variable.v.value_number, mode);
2473 res = new_d_Confirm(dbi, res, expression_to_firm(con), relation);
2474 set_value(var->variable.v.value_number, res);
2480 * Handle the assume optimizer hint.
2482 * @param dbi debug info
2483 * @param expr the IL assume expression
2485 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2487 switch(expression->kind) {
2488 case EXPR_BINARY_EQUAL:
2489 case EXPR_BINARY_NOTEQUAL:
2490 case EXPR_BINARY_LESS:
2491 case EXPR_BINARY_LESSEQUAL:
2492 case EXPR_BINARY_GREATER:
2493 case EXPR_BINARY_GREATEREQUAL:
2494 return handle_assume_compare(dbi, &expression->binary);
2500 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2501 type_t *from_type, type_t *type)
2503 type = skip_typeref(type);
2504 if (type == type_void) {
2505 /* make sure firm type is constructed */
2506 (void) get_ir_type(type);
2509 if (!is_type_scalar(type)) {
2510 /* make sure firm type is constructed */
2511 (void) get_ir_type(type);
2515 from_type = skip_typeref(from_type);
2516 ir_mode *mode = get_ir_mode_storage(type);
2517 /* check for conversion from / to __based types */
2518 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2519 const variable_t *from_var = from_type->pointer.base_variable;
2520 const variable_t *to_var = type->pointer.base_variable;
2521 if (from_var != to_var) {
2522 if (from_var != NULL) {
2523 ir_node *const addr = create_symconst(dbgi, from_var->v.entity);
2524 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2525 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2527 if (to_var != NULL) {
2528 ir_node *const addr = create_symconst(dbgi, to_var->v.entity);
2529 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2530 value_node = new_d_Sub(dbgi, value_node, base, mode);
2535 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2536 /* bool adjustments (we save a mode_Bu, but have to temporarily
2537 * convert to mode_b so we only get a 0/1 value */
2538 value_node = create_conv(dbgi, value_node, mode_b);
2541 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2542 ir_node *node = create_conv(dbgi, value_node, mode);
2543 node = do_strict_conv(dbgi, node);
2544 node = create_conv(dbgi, node, mode_arith);
2549 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2551 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2552 type_t *type = skip_typeref(expression->base.type);
2554 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2555 return expression_to_addr(expression->value);
2557 const expression_t *value = expression->value;
2559 switch(expression->base.kind) {
2560 case EXPR_UNARY_NEGATE: {
2561 ir_node *value_node = expression_to_firm(value);
2562 ir_mode *mode = get_ir_mode_arithmetic(type);
2563 return new_d_Minus(dbgi, value_node, mode);
2565 case EXPR_UNARY_PLUS:
2566 return expression_to_firm(value);
2567 case EXPR_UNARY_BITWISE_NEGATE: {
2568 ir_node *value_node = expression_to_firm(value);
2569 ir_mode *mode = get_ir_mode_arithmetic(type);
2570 return new_d_Not(dbgi, value_node, mode);
2572 case EXPR_UNARY_NOT: {
2573 ir_node *value_node = _expression_to_firm(value);
2574 value_node = create_conv(dbgi, value_node, mode_b);
2575 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2578 case EXPR_UNARY_DEREFERENCE: {
2579 ir_node *value_node = expression_to_firm(value);
2580 type_t *value_type = skip_typeref(value->base.type);
2581 assert(is_type_pointer(value_type));
2583 /* check for __based */
2584 const variable_t *const base_var = value_type->pointer.base_variable;
2585 if (base_var != NULL) {
2586 ir_node *const addr = create_symconst(dbgi, base_var->v.entity);
2587 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2588 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2590 type_t *points_to = value_type->pointer.points_to;
2591 return deref_address(dbgi, points_to, value_node);
2593 case EXPR_UNARY_POSTFIX_INCREMENT:
2594 case EXPR_UNARY_POSTFIX_DECREMENT:
2595 case EXPR_UNARY_PREFIX_INCREMENT:
2596 case EXPR_UNARY_PREFIX_DECREMENT:
2597 return create_incdec(expression);
2598 case EXPR_UNARY_CAST_IMPLICIT:
2599 case EXPR_UNARY_CAST: {
2600 ir_node *value_node = expression_to_firm(value);
2601 type_t *from_type = value->base.type;
2602 return create_cast(dbgi, value_node, from_type, type);
2604 case EXPR_UNARY_ASSUME:
2605 return handle_assume(dbgi, value);
2610 panic("invalid UNEXPR type found");
2614 * produces a 0/1 depending of the value of a mode_b node
2616 static ir_node *produce_condition_result(const expression_t *expression,
2617 ir_mode *mode, dbg_info *dbgi)
2619 ir_node *const one_block = new_immBlock();
2620 ir_node *const zero_block = new_immBlock();
2621 create_condition_evaluation(expression, one_block, zero_block);
2622 mature_immBlock(one_block);
2623 mature_immBlock(zero_block);
2625 ir_node *const jmp_one = new_rd_Jmp(dbgi, one_block);
2626 ir_node *const jmp_zero = new_rd_Jmp(dbgi, zero_block);
2627 ir_node *const in_cf[2] = { jmp_one, jmp_zero };
2628 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
2629 set_cur_block(block);
2631 ir_node *const one = new_Const(get_mode_one(mode));
2632 ir_node *const zero = new_Const(get_mode_null(mode));
2633 ir_node *const in[2] = { one, zero };
2634 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
2639 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2640 ir_node *value, type_t *type)
2642 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2643 assert(is_type_pointer(type));
2644 pointer_type_t *const pointer_type = &type->pointer;
2645 type_t *const points_to = skip_typeref(pointer_type->points_to);
2646 ir_node * elem_size = get_type_size_node(points_to);
2647 elem_size = create_conv(dbgi, elem_size, mode);
2648 value = create_conv(dbgi, value, mode);
2649 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2653 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2654 ir_node *left, ir_node *right)
2657 type_t *type_left = skip_typeref(expression->left->base.type);
2658 type_t *type_right = skip_typeref(expression->right->base.type);
2660 expression_kind_t kind = expression->base.kind;
2663 case EXPR_BINARY_SHIFTLEFT:
2664 case EXPR_BINARY_SHIFTRIGHT:
2665 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2666 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2667 mode = get_ir_mode_arithmetic(expression->base.type);
2668 right = create_conv(dbgi, right, mode_uint);
2671 case EXPR_BINARY_SUB:
2672 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2673 const pointer_type_t *const ptr_type = &type_left->pointer;
2675 mode = get_ir_mode_arithmetic(expression->base.type);
2676 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2677 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2678 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2679 ir_node *const no_mem = new_NoMem();
2680 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2681 mode, op_pin_state_floats);
2682 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2685 case EXPR_BINARY_SUB_ASSIGN:
2686 if (is_type_pointer(type_left)) {
2687 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2688 mode = get_ir_mode_arithmetic(type_left);
2693 case EXPR_BINARY_ADD:
2694 case EXPR_BINARY_ADD_ASSIGN:
2695 if (is_type_pointer(type_left)) {
2696 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2697 mode = get_ir_mode_arithmetic(type_left);
2699 } else if (is_type_pointer(type_right)) {
2700 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2701 mode = get_ir_mode_arithmetic(type_right);
2708 mode = get_ir_mode_arithmetic(type_right);
2709 left = create_conv(dbgi, left, mode);
2714 case EXPR_BINARY_ADD_ASSIGN:
2715 case EXPR_BINARY_ADD:
2716 return new_d_Add(dbgi, left, right, mode);
2717 case EXPR_BINARY_SUB_ASSIGN:
2718 case EXPR_BINARY_SUB:
2719 return new_d_Sub(dbgi, left, right, mode);
2720 case EXPR_BINARY_MUL_ASSIGN:
2721 case EXPR_BINARY_MUL:
2722 return new_d_Mul(dbgi, left, right, mode);
2723 case EXPR_BINARY_BITWISE_AND:
2724 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2725 return new_d_And(dbgi, left, right, mode);
2726 case EXPR_BINARY_BITWISE_OR:
2727 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2728 return new_d_Or(dbgi, left, right, mode);
2729 case EXPR_BINARY_BITWISE_XOR:
2730 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2731 return new_d_Eor(dbgi, left, right, mode);
2732 case EXPR_BINARY_SHIFTLEFT:
2733 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2734 return new_d_Shl(dbgi, left, right, mode);
2735 case EXPR_BINARY_SHIFTRIGHT:
2736 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2737 if (mode_is_signed(mode)) {
2738 return new_d_Shrs(dbgi, left, right, mode);
2740 return new_d_Shr(dbgi, left, right, mode);
2742 case EXPR_BINARY_DIV:
2743 case EXPR_BINARY_DIV_ASSIGN: {
2744 ir_node *pin = new_Pin(new_NoMem());
2745 ir_node *op = new_d_Div(dbgi, pin, left, right, mode,
2746 op_pin_state_floats);
2747 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2750 case EXPR_BINARY_MOD:
2751 case EXPR_BINARY_MOD_ASSIGN: {
2752 ir_node *pin = new_Pin(new_NoMem());
2753 assert(!mode_is_float(mode));
2754 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2755 op_pin_state_floats);
2756 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2760 panic("unexpected expression kind");
2764 static ir_node *create_lazy_op(const binary_expression_t *expression)
2766 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2767 type_t *type = skip_typeref(expression->base.type);
2768 ir_mode *mode = get_ir_mode_arithmetic(type);
2770 if (is_constant_expression(expression->left) == EXPR_CLASS_CONSTANT) {
2771 bool val = fold_constant_to_bool(expression->left);
2772 expression_kind_t ekind = expression->base.kind;
2773 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2774 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2776 return new_Const(get_mode_null(mode));
2780 return new_Const(get_mode_one(mode));
2784 if (is_constant_expression(expression->right) == EXPR_CLASS_CONSTANT) {
2785 bool valr = fold_constant_to_bool(expression->right);
2786 return create_Const_from_bool(mode, valr);
2789 return produce_condition_result(expression->right, mode, dbgi);
2792 return produce_condition_result((const expression_t*) expression, mode,
2796 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2797 ir_node *right, ir_mode *mode);
2799 static ir_node *create_assign_binop(const binary_expression_t *expression)
2801 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2802 const expression_t *left_expr = expression->left;
2803 type_t *type = skip_typeref(left_expr->base.type);
2804 ir_node *right = expression_to_firm(expression->right);
2805 ir_node *left_addr = expression_to_addr(left_expr);
2806 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2807 ir_node *result = create_op(dbgi, expression, left, right);
2809 result = create_cast(dbgi, result, expression->right->base.type, type);
2810 result = do_strict_conv(dbgi, result);
2812 result = set_value_for_expression_addr(left_expr, result, left_addr);
2814 if (!is_type_compound(type)) {
2815 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2816 result = create_conv(dbgi, result, mode_arithmetic);
2821 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2823 expression_kind_t kind = expression->base.kind;
2826 case EXPR_BINARY_EQUAL:
2827 case EXPR_BINARY_NOTEQUAL:
2828 case EXPR_BINARY_LESS:
2829 case EXPR_BINARY_LESSEQUAL:
2830 case EXPR_BINARY_GREATER:
2831 case EXPR_BINARY_GREATEREQUAL:
2832 case EXPR_BINARY_ISGREATER:
2833 case EXPR_BINARY_ISGREATEREQUAL:
2834 case EXPR_BINARY_ISLESS:
2835 case EXPR_BINARY_ISLESSEQUAL:
2836 case EXPR_BINARY_ISLESSGREATER:
2837 case EXPR_BINARY_ISUNORDERED: {
2838 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2839 ir_node *left = expression_to_firm(expression->left);
2840 ir_node *right = expression_to_firm(expression->right);
2841 ir_relation relation = get_relation(kind);
2842 ir_node *cmp = new_d_Cmp(dbgi, left, right, relation);
2845 case EXPR_BINARY_ASSIGN: {
2846 ir_node *addr = expression_to_addr(expression->left);
2847 ir_node *right = expression_to_firm(expression->right);
2849 = set_value_for_expression_addr(expression->left, right, addr);
2851 type_t *type = skip_typeref(expression->base.type);
2852 if (!is_type_compound(type)) {
2853 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2854 res = create_conv(NULL, res, mode_arithmetic);
2858 case EXPR_BINARY_ADD:
2859 case EXPR_BINARY_SUB:
2860 case EXPR_BINARY_MUL:
2861 case EXPR_BINARY_DIV:
2862 case EXPR_BINARY_MOD:
2863 case EXPR_BINARY_BITWISE_AND:
2864 case EXPR_BINARY_BITWISE_OR:
2865 case EXPR_BINARY_BITWISE_XOR:
2866 case EXPR_BINARY_SHIFTLEFT:
2867 case EXPR_BINARY_SHIFTRIGHT:
2869 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2870 ir_node *left = expression_to_firm(expression->left);
2871 ir_node *right = expression_to_firm(expression->right);
2872 return create_op(dbgi, expression, left, right);
2874 case EXPR_BINARY_LOGICAL_AND:
2875 case EXPR_BINARY_LOGICAL_OR:
2876 return create_lazy_op(expression);
2877 case EXPR_BINARY_COMMA:
2878 /* create side effects of left side */
2879 (void) expression_to_firm(expression->left);
2880 return _expression_to_firm(expression->right);
2882 case EXPR_BINARY_ADD_ASSIGN:
2883 case EXPR_BINARY_SUB_ASSIGN:
2884 case EXPR_BINARY_MUL_ASSIGN:
2885 case EXPR_BINARY_MOD_ASSIGN:
2886 case EXPR_BINARY_DIV_ASSIGN:
2887 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2888 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2889 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2890 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2891 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2892 return create_assign_binop(expression);
2894 panic("TODO binexpr type");
2898 static ir_node *array_access_addr(const array_access_expression_t *expression)
2900 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2901 ir_node *base_addr = expression_to_firm(expression->array_ref);
2902 ir_node *offset = expression_to_firm(expression->index);
2903 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2904 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2905 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2910 static ir_node *array_access_to_firm(
2911 const array_access_expression_t *expression)
2913 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2914 ir_node *addr = array_access_addr(expression);
2915 type_t *type = revert_automatic_type_conversion(
2916 (const expression_t*) expression);
2917 type = skip_typeref(type);
2919 return deref_address(dbgi, type, addr);
2922 static long get_offsetof_offset(const offsetof_expression_t *expression)
2924 type_t *orig_type = expression->type;
2927 designator_t *designator = expression->designator;
2928 for ( ; designator != NULL; designator = designator->next) {
2929 type_t *type = skip_typeref(orig_type);
2930 /* be sure the type is constructed */
2931 (void) get_ir_type(type);
2933 if (designator->symbol != NULL) {
2934 assert(is_type_compound(type));
2935 symbol_t *symbol = designator->symbol;
2937 compound_t *compound = type->compound.compound;
2938 entity_t *iter = compound->members.entities;
2939 for ( ; iter != NULL; iter = iter->base.next) {
2940 if (iter->base.symbol == symbol) {
2944 assert(iter != NULL);
2946 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2947 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2948 offset += get_entity_offset(iter->compound_member.entity);
2950 orig_type = iter->declaration.type;
2952 expression_t *array_index = designator->array_index;
2953 assert(designator->array_index != NULL);
2954 assert(is_type_array(type));
2956 long index = fold_constant_to_int(array_index);
2957 ir_type *arr_type = get_ir_type(type);
2958 ir_type *elem_type = get_array_element_type(arr_type);
2959 long elem_size = get_type_size_bytes(elem_type);
2961 offset += index * elem_size;
2963 orig_type = type->array.element_type;
2970 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2972 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2973 long offset = get_offsetof_offset(expression);
2974 ir_tarval *tv = new_tarval_from_long(offset, mode);
2975 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2977 return new_d_Const(dbgi, tv);
2980 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2981 ir_entity *entity, type_t *type);
2983 static ir_node *compound_literal_to_firm(
2984 const compound_literal_expression_t *expression)
2986 type_t *type = expression->type;
2988 /* create an entity on the stack */
2989 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2991 ident *const id = id_unique("CompLit.%u");
2992 ir_type *const irtype = get_ir_type(type);
2993 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2994 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2995 set_entity_ld_ident(entity, id);
2997 /* create initialisation code */
2998 initializer_t *initializer = expression->initializer;
2999 create_local_initializer(initializer, dbgi, entity, type);
3001 /* create a sel for the compound literal address */
3002 ir_node *frame = get_irg_frame(current_ir_graph);
3003 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3008 * Transform a sizeof expression into Firm code.
3010 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3012 type_t *const type = skip_typeref(expression->type);
3013 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3014 if (is_type_array(type) && type->array.is_vla
3015 && expression->tp_expression != NULL) {
3016 expression_to_firm(expression->tp_expression);
3018 /* strange gnu extensions: sizeof(function) == 1 */
3019 if (is_type_function(type)) {
3020 ir_mode *mode = get_ir_mode_storage(type_size_t);
3021 return new_Const(get_mode_one(mode));
3024 return get_type_size_node(type);
3027 static entity_t *get_expression_entity(const expression_t *expression)
3029 if (expression->kind != EXPR_REFERENCE)
3032 return expression->reference.entity;
3035 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3037 switch(entity->kind) {
3038 DECLARATION_KIND_CASES
3039 return entity->declaration.alignment;
3042 return entity->compound.alignment;
3043 case ENTITY_TYPEDEF:
3044 return entity->typedefe.alignment;
3052 * Transform an alignof expression into Firm code.
3054 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3056 unsigned alignment = 0;
3058 const expression_t *tp_expression = expression->tp_expression;
3059 if (tp_expression != NULL) {
3060 entity_t *entity = get_expression_entity(tp_expression);
3061 if (entity != NULL) {
3062 if (entity->kind == ENTITY_FUNCTION) {
3063 /* a gnu-extension */
3066 alignment = get_cparser_entity_alignment(entity);
3071 if (alignment == 0) {
3072 type_t *type = expression->type;
3073 alignment = get_type_alignment(type);
3076 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3077 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3078 ir_tarval *tv = new_tarval_from_long(alignment, mode);
3079 return new_d_Const(dbgi, tv);
3082 static void init_ir_types(void);
3084 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3086 assert(is_type_valid(skip_typeref(expression->base.type)));
3088 bool constant_folding_old = constant_folding;
3089 constant_folding = true;
3093 assert(is_constant_expression(expression) == EXPR_CLASS_CONSTANT);
3095 ir_graph *old_current_ir_graph = current_ir_graph;
3096 current_ir_graph = get_const_code_irg();
3098 ir_node *cnst = expression_to_firm(expression);
3099 current_ir_graph = old_current_ir_graph;
3101 if (!is_Const(cnst)) {
3102 panic("couldn't fold constant");
3105 constant_folding = constant_folding_old;
3107 return get_Const_tarval(cnst);
3110 long fold_constant_to_int(const expression_t *expression)
3112 if (expression->kind == EXPR_INVALID)
3115 ir_tarval *tv = fold_constant_to_tarval(expression);
3116 if (!tarval_is_long(tv)) {
3117 panic("result of constant folding is not integer");
3120 return get_tarval_long(tv);
3123 bool fold_constant_to_bool(const expression_t *expression)
3125 if (expression->kind == EXPR_INVALID)
3127 ir_tarval *tv = fold_constant_to_tarval(expression);
3128 return !tarval_is_null(tv);
3131 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3133 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3135 /* first try to fold a constant condition */
3136 if (is_constant_expression(expression->condition) == EXPR_CLASS_CONSTANT) {
3137 bool val = fold_constant_to_bool(expression->condition);
3139 expression_t *true_expression = expression->true_expression;
3140 if (true_expression == NULL)
3141 true_expression = expression->condition;
3142 return expression_to_firm(true_expression);
3144 return expression_to_firm(expression->false_expression);
3148 ir_node *const true_block = new_immBlock();
3149 ir_node *const false_block = new_immBlock();
3150 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3151 mature_immBlock(true_block);
3152 mature_immBlock(false_block);
3154 set_cur_block(true_block);
3156 if (expression->true_expression != NULL) {
3157 true_val = expression_to_firm(expression->true_expression);
3158 } else if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3159 true_val = cond_expr;
3161 /* Condition ended with a short circuit (&&, ||, !) operation or a
3162 * comparison. Generate a "1" as value for the true branch. */
3163 true_val = new_Const(get_mode_one(mode_Is));
3165 ir_node *const true_jmp = new_d_Jmp(dbgi);
3167 set_cur_block(false_block);
3168 ir_node *const false_val = expression_to_firm(expression->false_expression);
3169 ir_node *const false_jmp = new_d_Jmp(dbgi);
3171 /* create the common block */
3172 ir_node *const in_cf[2] = { true_jmp, false_jmp };
3173 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
3174 set_cur_block(block);
3176 /* TODO improve static semantics, so either both or no values are NULL */
3177 if (true_val == NULL || false_val == NULL)
3180 ir_node *const in[2] = { true_val, false_val };
3181 type_t *const type = skip_typeref(expression->base.type);
3183 if (is_type_compound(type)) {
3186 mode = get_ir_mode_arithmetic(type);
3188 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
3194 * Returns an IR-node representing the address of a field.
3196 static ir_node *select_addr(const select_expression_t *expression)
3198 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3200 construct_select_compound(expression);
3202 ir_node *compound_addr = expression_to_firm(expression->compound);
3204 entity_t *entry = expression->compound_entry;
3205 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3206 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3208 if (constant_folding) {
3209 ir_mode *mode = get_irn_mode(compound_addr);
3210 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3211 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3212 return new_d_Add(dbgi, compound_addr, ofs, mode);
3214 ir_entity *irentity = entry->compound_member.entity;
3215 assert(irentity != NULL);
3216 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3220 static ir_node *select_to_firm(const select_expression_t *expression)
3222 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3223 ir_node *addr = select_addr(expression);
3224 type_t *type = revert_automatic_type_conversion(
3225 (const expression_t*) expression);
3226 type = skip_typeref(type);
3228 entity_t *entry = expression->compound_entry;
3229 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3230 type_t *entry_type = skip_typeref(entry->declaration.type);
3232 if (entry_type->kind == TYPE_BITFIELD) {
3233 return bitfield_extract_to_firm(expression, addr);
3236 return deref_address(dbgi, type, addr);
3239 /* Values returned by __builtin_classify_type. */
3240 typedef enum gcc_type_class
3246 enumeral_type_class,
3249 reference_type_class,
3253 function_type_class,
3264 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3266 type_t *type = expr->type_expression->base.type;
3268 /* FIXME gcc returns different values depending on whether compiling C or C++
3269 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3272 type = skip_typeref(type);
3273 switch (type->kind) {
3275 const atomic_type_t *const atomic_type = &type->atomic;
3276 switch (atomic_type->akind) {
3277 /* should not be reached */
3278 case ATOMIC_TYPE_INVALID:
3282 /* gcc cannot do that */
3283 case ATOMIC_TYPE_VOID:
3284 tc = void_type_class;
3287 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3288 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3289 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3290 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3291 case ATOMIC_TYPE_SHORT:
3292 case ATOMIC_TYPE_USHORT:
3293 case ATOMIC_TYPE_INT:
3294 case ATOMIC_TYPE_UINT:
3295 case ATOMIC_TYPE_LONG:
3296 case ATOMIC_TYPE_ULONG:
3297 case ATOMIC_TYPE_LONGLONG:
3298 case ATOMIC_TYPE_ULONGLONG:
3299 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3300 tc = integer_type_class;
3303 case ATOMIC_TYPE_FLOAT:
3304 case ATOMIC_TYPE_DOUBLE:
3305 case ATOMIC_TYPE_LONG_DOUBLE:
3306 tc = real_type_class;
3309 panic("Unexpected atomic type in classify_type_to_firm().");
3312 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3313 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3314 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3315 case TYPE_ARRAY: /* gcc handles this as pointer */
3316 case TYPE_FUNCTION: /* gcc handles this as pointer */
3317 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3318 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3319 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3321 /* gcc handles this as integer */
3322 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3324 /* gcc classifies the referenced type */
3325 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3327 /* typedef/typeof should be skipped already */
3334 panic("unexpected TYPE classify_type_to_firm().");
3338 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3339 ir_tarval *const tv = new_tarval_from_long(tc, mode_int);
3340 return new_d_Const(dbgi, tv);
3343 static ir_node *function_name_to_firm(
3344 const funcname_expression_t *const expr)
3346 switch(expr->kind) {
3347 case FUNCNAME_FUNCTION:
3348 case FUNCNAME_PRETTY_FUNCTION:
3349 case FUNCNAME_FUNCDNAME:
3350 if (current_function_name == NULL) {
3351 const source_position_t *const src_pos = &expr->base.source_position;
3352 const char *name = current_function_entity->base.symbol->string;
3353 const string_t string = { name, strlen(name) + 1 };
3354 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3356 return current_function_name;
3357 case FUNCNAME_FUNCSIG:
3358 if (current_funcsig == NULL) {
3359 const source_position_t *const src_pos = &expr->base.source_position;
3360 ir_entity *ent = get_irg_entity(current_ir_graph);
3361 const char *const name = get_entity_ld_name(ent);
3362 const string_t string = { name, strlen(name) + 1 };
3363 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3365 return current_funcsig;
3367 panic("Unsupported function name");
3370 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3372 statement_t *statement = expr->statement;
3374 assert(statement->kind == STATEMENT_COMPOUND);
3375 return compound_statement_to_firm(&statement->compound);
3378 static ir_node *va_start_expression_to_firm(
3379 const va_start_expression_t *const expr)
3381 type_t *const type = current_function_entity->declaration.type;
3382 ir_type *const method_type = get_ir_type(type);
3383 int const n = get_method_n_params(method_type) - 1;
3384 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3385 ir_node *const frame = get_irg_frame(current_ir_graph);
3386 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3387 ir_node *const no_mem = new_NoMem();
3388 ir_node *const arg_sel =
3389 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3391 type_t *const param_type = expr->parameter->base.type;
3392 ir_node *const cnst = get_type_size_node(param_type);
3393 ir_mode *const mode = get_irn_mode(cnst);
3394 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3395 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3396 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3397 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3398 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3399 set_value_for_expression(expr->ap, add);
3404 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3406 type_t *const type = expr->base.type;
3407 expression_t *const ap_expr = expr->ap;
3408 ir_node *const ap_addr = expression_to_addr(ap_expr);
3409 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3410 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3411 ir_node *const res = deref_address(dbgi, type, ap);
3413 ir_node *const cnst = get_type_size_node(expr->base.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, ap, c4, mode_P_data);
3421 set_value_for_expression_addr(ap_expr, add, ap_addr);
3427 * Generate Firm for a va_copy expression.
3429 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3431 ir_node *const src = expression_to_firm(expr->src);
3432 set_value_for_expression(expr->dst, src);
3436 static ir_node *dereference_addr(const unary_expression_t *const expression)
3438 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3439 return expression_to_firm(expression->value);
3443 * Returns a IR-node representing an lvalue of the given expression.
3445 static ir_node *expression_to_addr(const expression_t *expression)
3447 switch(expression->kind) {
3448 case EXPR_ARRAY_ACCESS:
3449 return array_access_addr(&expression->array_access);
3451 return call_expression_to_firm(&expression->call);
3452 case EXPR_COMPOUND_LITERAL:
3453 return compound_literal_to_firm(&expression->compound_literal);
3454 case EXPR_REFERENCE:
3455 return reference_addr(&expression->reference);
3457 return select_addr(&expression->select);
3458 case EXPR_UNARY_DEREFERENCE:
3459 return dereference_addr(&expression->unary);
3463 panic("trying to get address of non-lvalue");
3466 static ir_node *builtin_constant_to_firm(
3467 const builtin_constant_expression_t *expression)
3469 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3470 bool const v = is_constant_expression(expression->value) == EXPR_CLASS_CONSTANT;
3471 return create_Const_from_bool(mode, v);
3474 static ir_node *builtin_types_compatible_to_firm(
3475 const builtin_types_compatible_expression_t *expression)
3477 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3478 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3479 bool const value = types_compatible(left, right);
3480 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3481 return create_Const_from_bool(mode, value);
3484 static ir_node *get_label_block(label_t *label)
3486 if (label->block != NULL)
3487 return label->block;
3489 /* beware: might be called from create initializer with current_ir_graph
3490 * set to const_code_irg. */
3491 ir_graph *rem = current_ir_graph;
3492 current_ir_graph = current_function;
3494 ir_node *block = new_immBlock();
3496 label->block = block;
3498 ARR_APP1(label_t *, all_labels, label);
3500 current_ir_graph = rem;
3505 * Pointer to a label. This is used for the
3506 * GNU address-of-label extension.
3508 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3510 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3511 ir_node *block = get_label_block(label->label);
3512 ir_entity *entity = create_Block_entity(block);
3514 symconst_symbol value;
3515 value.entity_p = entity;
3516 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3520 * creates firm nodes for an expression. The difference between this function
3521 * and expression_to_firm is, that this version might produce mode_b nodes
3522 * instead of mode_Is.
3524 static ir_node *_expression_to_firm(const expression_t *expression)
3527 if (!constant_folding) {
3528 assert(!expression->base.transformed);
3529 ((expression_t*) expression)->base.transformed = true;
3533 switch (expression->kind) {
3535 return literal_to_firm(&expression->literal);
3536 case EXPR_STRING_LITERAL:
3537 return string_to_firm(&expression->base.source_position, "str.%u",
3538 &expression->literal.value);
3539 case EXPR_WIDE_STRING_LITERAL:
3540 return wide_string_literal_to_firm(&expression->string_literal);
3541 case EXPR_REFERENCE:
3542 return reference_expression_to_firm(&expression->reference);
3543 case EXPR_REFERENCE_ENUM_VALUE:
3544 return reference_expression_enum_value_to_firm(&expression->reference);
3546 return call_expression_to_firm(&expression->call);
3548 return unary_expression_to_firm(&expression->unary);
3550 return binary_expression_to_firm(&expression->binary);
3551 case EXPR_ARRAY_ACCESS:
3552 return array_access_to_firm(&expression->array_access);
3554 return sizeof_to_firm(&expression->typeprop);
3556 return alignof_to_firm(&expression->typeprop);
3557 case EXPR_CONDITIONAL:
3558 return conditional_to_firm(&expression->conditional);
3560 return select_to_firm(&expression->select);
3561 case EXPR_CLASSIFY_TYPE:
3562 return classify_type_to_firm(&expression->classify_type);
3564 return function_name_to_firm(&expression->funcname);
3565 case EXPR_STATEMENT:
3566 return statement_expression_to_firm(&expression->statement);
3568 return va_start_expression_to_firm(&expression->va_starte);
3570 return va_arg_expression_to_firm(&expression->va_arge);
3572 return va_copy_expression_to_firm(&expression->va_copye);
3573 case EXPR_BUILTIN_CONSTANT_P:
3574 return builtin_constant_to_firm(&expression->builtin_constant);
3575 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3576 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3578 return offsetof_to_firm(&expression->offsetofe);
3579 case EXPR_COMPOUND_LITERAL:
3580 return compound_literal_to_firm(&expression->compound_literal);
3581 case EXPR_LABEL_ADDRESS:
3582 return label_address_to_firm(&expression->label_address);
3588 panic("invalid expression found");
3592 * Check if a given expression is a GNU __builtin_expect() call.
3594 static bool is_builtin_expect(const expression_t *expression)
3596 if (expression->kind != EXPR_CALL)
3599 expression_t *function = expression->call.function;
3600 if (function->kind != EXPR_REFERENCE)
3602 reference_expression_t *ref = &function->reference;
3603 if (ref->entity->kind != ENTITY_FUNCTION ||
3604 ref->entity->function.btk != bk_gnu_builtin_expect)
3610 static bool produces_mode_b(const expression_t *expression)
3612 switch (expression->kind) {
3613 case EXPR_BINARY_EQUAL:
3614 case EXPR_BINARY_NOTEQUAL:
3615 case EXPR_BINARY_LESS:
3616 case EXPR_BINARY_LESSEQUAL:
3617 case EXPR_BINARY_GREATER:
3618 case EXPR_BINARY_GREATEREQUAL:
3619 case EXPR_BINARY_ISGREATER:
3620 case EXPR_BINARY_ISGREATEREQUAL:
3621 case EXPR_BINARY_ISLESS:
3622 case EXPR_BINARY_ISLESSEQUAL:
3623 case EXPR_BINARY_ISLESSGREATER:
3624 case EXPR_BINARY_ISUNORDERED:
3625 case EXPR_UNARY_NOT:
3629 if (is_builtin_expect(expression)) {
3630 expression_t *argument = expression->call.arguments->expression;
3631 return produces_mode_b(argument);
3634 case EXPR_BINARY_COMMA:
3635 return produces_mode_b(expression->binary.right);
3642 static ir_node *expression_to_firm(const expression_t *expression)
3644 if (!produces_mode_b(expression)) {
3645 ir_node *res = _expression_to_firm(expression);
3646 assert(res == NULL || get_irn_mode(res) != mode_b);
3650 if (is_constant_expression(expression) == EXPR_CLASS_CONSTANT) {
3651 bool const constant_folding_old = constant_folding;
3652 constant_folding = true;
3653 ir_node *res = _expression_to_firm(expression);
3654 constant_folding = constant_folding_old;
3655 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3656 assert(is_Const(res));
3657 return create_Const_from_bool(mode, !is_Const_null(res));
3660 /* we have to produce a 0/1 from the mode_b expression */
3661 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3662 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3663 return produce_condition_result(expression, mode, dbgi);
3667 * create a short-circuit expression evaluation that tries to construct
3668 * efficient control flow structures for &&, || and ! expressions
3670 static ir_node *create_condition_evaluation(const expression_t *expression,
3671 ir_node *true_block,
3672 ir_node *false_block)
3674 switch(expression->kind) {
3675 case EXPR_UNARY_NOT: {
3676 const unary_expression_t *unary_expression = &expression->unary;
3677 create_condition_evaluation(unary_expression->value, false_block,
3681 case EXPR_BINARY_LOGICAL_AND: {
3682 const binary_expression_t *binary_expression = &expression->binary;
3684 ir_node *extra_block = new_immBlock();
3685 create_condition_evaluation(binary_expression->left, extra_block,
3687 mature_immBlock(extra_block);
3688 set_cur_block(extra_block);
3689 create_condition_evaluation(binary_expression->right, true_block,
3693 case EXPR_BINARY_LOGICAL_OR: {
3694 const binary_expression_t *binary_expression = &expression->binary;
3696 ir_node *extra_block = new_immBlock();
3697 create_condition_evaluation(binary_expression->left, true_block,
3699 mature_immBlock(extra_block);
3700 set_cur_block(extra_block);
3701 create_condition_evaluation(binary_expression->right, true_block,
3709 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3710 ir_node *cond_expr = _expression_to_firm(expression);
3711 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3712 ir_node *cond = new_d_Cond(dbgi, condition);
3713 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3714 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3716 /* set branch prediction info based on __builtin_expect */
3717 if (is_builtin_expect(expression) && is_Cond(cond)) {
3718 call_argument_t *argument = expression->call.arguments->next;
3719 if (is_constant_expression(argument->expression) == EXPR_CLASS_CONSTANT) {
3720 bool const cnst = fold_constant_to_bool(argument->expression);
3721 cond_jmp_predicate const pred = cnst ? COND_JMP_PRED_TRUE : COND_JMP_PRED_FALSE;
3722 set_Cond_jmp_pred(cond, pred);
3726 add_immBlock_pred(true_block, true_proj);
3727 add_immBlock_pred(false_block, false_proj);
3729 set_unreachable_now();
3733 static void create_variable_entity(entity_t *variable,
3734 declaration_kind_t declaration_kind,
3735 ir_type *parent_type)
3737 assert(variable->kind == ENTITY_VARIABLE);
3738 type_t *type = skip_typeref(variable->declaration.type);
3740 ident *const id = new_id_from_str(variable->base.symbol->string);
3741 ir_type *const irtype = get_ir_type(type);
3742 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3743 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3744 unsigned alignment = variable->declaration.alignment;
3746 set_entity_alignment(irentity, alignment);
3748 handle_decl_modifiers(irentity, variable);
3750 variable->declaration.kind = (unsigned char) declaration_kind;
3751 variable->variable.v.entity = irentity;
3752 set_entity_ld_ident(irentity, create_ld_ident(variable));
3754 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3755 set_entity_volatility(irentity, volatility_is_volatile);
3760 typedef struct type_path_entry_t type_path_entry_t;
3761 struct type_path_entry_t {
3763 ir_initializer_t *initializer;
3765 entity_t *compound_entry;
3768 typedef struct type_path_t type_path_t;
3769 struct type_path_t {
3770 type_path_entry_t *path;
3775 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3777 size_t len = ARR_LEN(path->path);
3779 for (size_t i = 0; i < len; ++i) {
3780 const type_path_entry_t *entry = & path->path[i];
3782 type_t *type = skip_typeref(entry->type);
3783 if (is_type_compound(type)) {
3784 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3785 } else if (is_type_array(type)) {
3786 fprintf(stderr, "[%u]", (unsigned) entry->index);
3788 fprintf(stderr, "-INVALID-");
3791 fprintf(stderr, " (");
3792 print_type(path->top_type);
3793 fprintf(stderr, ")");
3796 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3798 size_t len = ARR_LEN(path->path);
3800 return & path->path[len-1];
3803 static type_path_entry_t *append_to_type_path(type_path_t *path)
3805 size_t len = ARR_LEN(path->path);
3806 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3808 type_path_entry_t *result = & path->path[len];
3809 memset(result, 0, sizeof(result[0]));
3813 static size_t get_compound_member_count(const compound_type_t *type)
3815 compound_t *compound = type->compound;
3816 size_t n_members = 0;
3817 entity_t *member = compound->members.entities;
3818 for ( ; member != NULL; member = member->base.next) {
3825 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3827 type_t *orig_top_type = path->top_type;
3828 type_t *top_type = skip_typeref(orig_top_type);
3830 assert(is_type_compound(top_type) || is_type_array(top_type));
3832 if (ARR_LEN(path->path) == 0) {
3835 type_path_entry_t *top = get_type_path_top(path);
3836 ir_initializer_t *initializer = top->initializer;
3837 return get_initializer_compound_value(initializer, top->index);
3841 static void descend_into_subtype(type_path_t *path)
3843 type_t *orig_top_type = path->top_type;
3844 type_t *top_type = skip_typeref(orig_top_type);
3846 assert(is_type_compound(top_type) || is_type_array(top_type));
3848 ir_initializer_t *initializer = get_initializer_entry(path);
3850 type_path_entry_t *top = append_to_type_path(path);
3851 top->type = top_type;
3855 if (is_type_compound(top_type)) {
3856 compound_t *compound = top_type->compound.compound;
3857 entity_t *entry = compound->members.entities;
3859 top->compound_entry = entry;
3861 len = get_compound_member_count(&top_type->compound);
3862 if (entry != NULL) {
3863 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3864 path->top_type = entry->declaration.type;
3867 assert(is_type_array(top_type));
3868 assert(top_type->array.size > 0);
3871 path->top_type = top_type->array.element_type;
3872 len = top_type->array.size;
3874 if (initializer == NULL
3875 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3876 initializer = create_initializer_compound(len);
3877 /* we have to set the entry at the 2nd latest path entry... */
3878 size_t path_len = ARR_LEN(path->path);
3879 assert(path_len >= 1);
3881 type_path_entry_t *entry = & path->path[path_len-2];
3882 ir_initializer_t *tinitializer = entry->initializer;
3883 set_initializer_compound_value(tinitializer, entry->index,
3887 top->initializer = initializer;
3890 static void ascend_from_subtype(type_path_t *path)
3892 type_path_entry_t *top = get_type_path_top(path);
3894 path->top_type = top->type;
3896 size_t len = ARR_LEN(path->path);
3897 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3900 static void walk_designator(type_path_t *path, const designator_t *designator)
3902 /* designators start at current object type */
3903 ARR_RESIZE(type_path_entry_t, path->path, 1);
3905 for ( ; designator != NULL; designator = designator->next) {
3906 type_path_entry_t *top = get_type_path_top(path);
3907 type_t *orig_type = top->type;
3908 type_t *type = skip_typeref(orig_type);
3910 if (designator->symbol != NULL) {
3911 assert(is_type_compound(type));
3913 symbol_t *symbol = designator->symbol;
3915 compound_t *compound = type->compound.compound;
3916 entity_t *iter = compound->members.entities;
3917 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3918 if (iter->base.symbol == symbol) {
3919 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3923 assert(iter != NULL);
3925 /* revert previous initialisations of other union elements */
3926 if (type->kind == TYPE_COMPOUND_UNION) {
3927 ir_initializer_t *initializer = top->initializer;
3928 if (initializer != NULL
3929 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3930 /* are we writing to a new element? */
3931 ir_initializer_t *oldi
3932 = get_initializer_compound_value(initializer, index);
3933 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3934 /* clear initializer */
3936 = get_initializer_compound_n_entries(initializer);
3937 ir_initializer_t *nulli = get_initializer_null();
3938 for (size_t i = 0; i < len; ++i) {
3939 set_initializer_compound_value(initializer, i,
3946 top->type = orig_type;
3947 top->compound_entry = iter;
3949 orig_type = iter->declaration.type;
3951 expression_t *array_index = designator->array_index;
3952 assert(designator->array_index != NULL);
3953 assert(is_type_array(type));
3955 long index = fold_constant_to_int(array_index);
3958 if (type->array.size_constant) {
3959 long array_size = type->array.size;
3960 assert(index < array_size);
3964 top->type = orig_type;
3965 top->index = (size_t) index;
3966 orig_type = type->array.element_type;
3968 path->top_type = orig_type;
3970 if (designator->next != NULL) {
3971 descend_into_subtype(path);
3975 path->invalid = false;
3978 static void advance_current_object(type_path_t *path)
3980 if (path->invalid) {
3981 /* TODO: handle this... */
3982 panic("invalid initializer in ast2firm (excessive elements)");
3985 type_path_entry_t *top = get_type_path_top(path);
3987 type_t *type = skip_typeref(top->type);
3988 if (is_type_union(type)) {
3989 /* only the first element is initialized in unions */
3990 top->compound_entry = NULL;
3991 } else if (is_type_struct(type)) {
3992 entity_t *entry = top->compound_entry;
3995 entry = entry->base.next;
3996 top->compound_entry = entry;
3997 if (entry != NULL) {
3998 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3999 path->top_type = entry->declaration.type;
4003 assert(is_type_array(type));
4006 if (!type->array.size_constant || top->index < type->array.size) {
4011 /* we're past the last member of the current sub-aggregate, try if we
4012 * can ascend in the type hierarchy and continue with another subobject */
4013 size_t len = ARR_LEN(path->path);
4016 ascend_from_subtype(path);
4017 advance_current_object(path);
4019 path->invalid = true;
4024 static ir_initializer_t *create_ir_initializer(
4025 const initializer_t *initializer, type_t *type);
4027 static ir_initializer_t *create_ir_initializer_value(
4028 const initializer_value_t *initializer)
4030 if (is_type_compound(initializer->value->base.type)) {
4031 panic("initializer creation for compounds not implemented yet");
4033 type_t *type = initializer->value->base.type;
4034 expression_t *expr = initializer->value;
4035 if (initializer_use_bitfield_basetype) {
4036 type_t *skipped = skip_typeref(type);
4037 if (skipped->kind == TYPE_BITFIELD) {
4038 /* remove the bitfield cast... */
4039 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4040 expr = expr->unary.value;
4041 type = skipped->bitfield.base_type;
4044 ir_node *value = expression_to_firm(expr);
4045 ir_mode *mode = get_ir_mode_storage(type);
4046 value = create_conv(NULL, value, mode);
4047 return create_initializer_const(value);
4050 /** test wether type can be initialized by a string constant */
4051 static bool is_string_type(type_t *type)
4054 if (is_type_pointer(type)) {
4055 inner = skip_typeref(type->pointer.points_to);
4056 } else if(is_type_array(type)) {
4057 inner = skip_typeref(type->array.element_type);
4062 return is_type_integer(inner);
4065 static ir_initializer_t *create_ir_initializer_list(
4066 const initializer_list_t *initializer, type_t *type)
4069 memset(&path, 0, sizeof(path));
4070 path.top_type = type;
4071 path.path = NEW_ARR_F(type_path_entry_t, 0);
4073 descend_into_subtype(&path);
4075 for (size_t i = 0; i < initializer->len; ++i) {
4076 const initializer_t *sub_initializer = initializer->initializers[i];
4078 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4079 walk_designator(&path, sub_initializer->designator.designator);
4083 if (sub_initializer->kind == INITIALIZER_VALUE) {
4084 /* we might have to descend into types until we're at a scalar
4087 type_t *orig_top_type = path.top_type;
4088 type_t *top_type = skip_typeref(orig_top_type);
4090 if (is_type_scalar(top_type))
4092 descend_into_subtype(&path);
4094 } else if (sub_initializer->kind == INITIALIZER_STRING
4095 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4096 /* we might have to descend into types until we're at a scalar
4099 type_t *orig_top_type = path.top_type;
4100 type_t *top_type = skip_typeref(orig_top_type);
4102 if (is_string_type(top_type))
4104 descend_into_subtype(&path);
4108 ir_initializer_t *sub_irinitializer
4109 = create_ir_initializer(sub_initializer, path.top_type);
4111 size_t path_len = ARR_LEN(path.path);
4112 assert(path_len >= 1);
4113 type_path_entry_t *entry = & path.path[path_len-1];
4114 ir_initializer_t *tinitializer = entry->initializer;
4115 set_initializer_compound_value(tinitializer, entry->index,
4118 advance_current_object(&path);
4121 assert(ARR_LEN(path.path) >= 1);
4122 ir_initializer_t *result = path.path[0].initializer;
4123 DEL_ARR_F(path.path);
4128 static ir_initializer_t *create_ir_initializer_string(
4129 const initializer_string_t *initializer, type_t *type)
4131 type = skip_typeref(type);
4133 size_t string_len = initializer->string.size;
4134 assert(type->kind == TYPE_ARRAY);
4135 assert(type->array.size_constant);
4136 size_t len = type->array.size;
4137 ir_initializer_t *irinitializer = create_initializer_compound(len);
4139 const char *string = initializer->string.begin;
4140 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4142 for (size_t i = 0; i < len; ++i) {
4147 ir_tarval *tv = new_tarval_from_long(c, mode);
4148 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4150 set_initializer_compound_value(irinitializer, i, char_initializer);
4153 return irinitializer;
4156 static ir_initializer_t *create_ir_initializer_wide_string(
4157 const initializer_wide_string_t *initializer, type_t *type)
4159 assert(type->kind == TYPE_ARRAY);
4160 assert(type->array.size_constant);
4161 size_t len = type->array.size;
4162 size_t string_len = wstrlen(&initializer->string);
4163 ir_initializer_t *irinitializer = create_initializer_compound(len);
4165 const char *p = initializer->string.begin;
4166 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4168 for (size_t i = 0; i < len; ++i) {
4170 if (i < string_len) {
4171 c = read_utf8_char(&p);
4173 ir_tarval *tv = new_tarval_from_long(c, mode);
4174 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4176 set_initializer_compound_value(irinitializer, i, char_initializer);
4179 return irinitializer;
4182 static ir_initializer_t *create_ir_initializer(
4183 const initializer_t *initializer, type_t *type)
4185 switch(initializer->kind) {
4186 case INITIALIZER_STRING:
4187 return create_ir_initializer_string(&initializer->string, type);
4189 case INITIALIZER_WIDE_STRING:
4190 return create_ir_initializer_wide_string(&initializer->wide_string,
4193 case INITIALIZER_LIST:
4194 return create_ir_initializer_list(&initializer->list, type);
4196 case INITIALIZER_VALUE:
4197 return create_ir_initializer_value(&initializer->value);
4199 case INITIALIZER_DESIGNATOR:
4200 panic("unexpected designator initializer found");
4202 panic("unknown initializer");
4205 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4206 * are elements [...] the remainder of the aggregate shall be initialized
4207 * implicitly the same as objects that have static storage duration. */
4208 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4211 /* for unions we must NOT do anything for null initializers */
4212 ir_type *owner = get_entity_owner(entity);
4213 if (is_Union_type(owner)) {
4217 ir_type *ent_type = get_entity_type(entity);
4218 /* create sub-initializers for a compound type */
4219 if (is_compound_type(ent_type)) {
4220 unsigned n_members = get_compound_n_members(ent_type);
4221 for (unsigned n = 0; n < n_members; ++n) {
4222 ir_entity *member = get_compound_member(ent_type, n);
4223 ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4225 create_dynamic_null_initializer(member, dbgi, addr);
4229 if (is_Array_type(ent_type)) {
4230 assert(has_array_upper_bound(ent_type, 0));
4231 long n = get_array_upper_bound_int(ent_type, 0);
4232 for (long i = 0; i < n; ++i) {
4233 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4234 ir_node *cnst = new_d_Const(dbgi, index_tv);
4235 ir_node *in[1] = { cnst };
4236 ir_entity *arrent = get_array_element_entity(ent_type);
4237 ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4239 create_dynamic_null_initializer(arrent, dbgi, addr);
4244 ir_mode *value_mode = get_type_mode(ent_type);
4245 ir_node *node = new_Const(get_mode_null(value_mode));
4247 /* is it a bitfield type? */
4248 if (is_Primitive_type(ent_type) &&
4249 get_primitive_base_type(ent_type) != NULL) {
4250 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4254 ir_node *mem = get_store();
4255 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4256 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4260 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4261 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4263 switch(get_initializer_kind(initializer)) {
4264 case IR_INITIALIZER_NULL:
4265 create_dynamic_null_initializer(entity, dbgi, base_addr);
4267 case IR_INITIALIZER_CONST: {
4268 ir_node *node = get_initializer_const_value(initializer);
4269 ir_type *ent_type = get_entity_type(entity);
4271 /* is it a bitfield type? */
4272 if (is_Primitive_type(ent_type) &&
4273 get_primitive_base_type(ent_type) != NULL) {
4274 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4278 assert(get_type_mode(type) == get_irn_mode(node));
4279 ir_node *mem = get_store();
4280 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4281 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4285 case IR_INITIALIZER_TARVAL: {
4286 ir_tarval *tv = get_initializer_tarval_value(initializer);
4287 ir_node *cnst = new_d_Const(dbgi, tv);
4288 ir_type *ent_type = get_entity_type(entity);
4290 /* is it a bitfield type? */
4291 if (is_Primitive_type(ent_type) &&
4292 get_primitive_base_type(ent_type) != NULL) {
4293 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4297 assert(get_type_mode(type) == get_tarval_mode(tv));
4298 ir_node *mem = get_store();
4299 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4300 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4304 case IR_INITIALIZER_COMPOUND: {
4305 assert(is_compound_type(type) || is_Array_type(type));
4307 if (is_Array_type(type)) {
4308 assert(has_array_upper_bound(type, 0));
4309 n_members = get_array_upper_bound_int(type, 0);
4311 n_members = get_compound_n_members(type);
4314 if (get_initializer_compound_n_entries(initializer)
4315 != (unsigned) n_members)
4316 panic("initializer doesn't match compound type");
4318 for (int i = 0; i < n_members; ++i) {
4321 ir_entity *sub_entity;
4322 if (is_Array_type(type)) {
4323 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4324 ir_node *cnst = new_d_Const(dbgi, index_tv);
4325 ir_node *in[1] = { cnst };
4326 irtype = get_array_element_type(type);
4327 sub_entity = get_array_element_entity(type);
4328 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4331 sub_entity = get_compound_member(type, i);
4332 irtype = get_entity_type(sub_entity);
4333 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4337 ir_initializer_t *sub_init
4338 = get_initializer_compound_value(initializer, i);
4340 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4347 panic("invalid IR_INITIALIZER found");
4350 static void create_dynamic_initializer(ir_initializer_t *initializer,
4351 dbg_info *dbgi, ir_entity *entity)
4353 ir_node *frame = get_irg_frame(current_ir_graph);
4354 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4355 ir_type *type = get_entity_type(entity);
4357 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4360 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4361 ir_entity *entity, type_t *type)
4363 ir_node *memory = get_store();
4364 ir_node *nomem = new_NoMem();
4365 ir_node *frame = get_irg_frame(current_ir_graph);
4366 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4368 if (initializer->kind == INITIALIZER_VALUE) {
4369 initializer_value_t *initializer_value = &initializer->value;
4371 ir_node *value = expression_to_firm(initializer_value->value);
4372 type = skip_typeref(type);
4373 assign_value(dbgi, addr, type, value);
4377 if (is_constant_initializer(initializer) == EXPR_CLASS_VARIABLE) {
4378 bool old_initializer_use_bitfield_basetype
4379 = initializer_use_bitfield_basetype;
4380 initializer_use_bitfield_basetype = true;
4381 ir_initializer_t *irinitializer
4382 = create_ir_initializer(initializer, type);
4383 initializer_use_bitfield_basetype
4384 = old_initializer_use_bitfield_basetype;
4386 create_dynamic_initializer(irinitializer, dbgi, entity);
4390 /* create the ir_initializer */
4391 ir_graph *const old_current_ir_graph = current_ir_graph;
4392 current_ir_graph = get_const_code_irg();
4394 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4396 assert(current_ir_graph == get_const_code_irg());
4397 current_ir_graph = old_current_ir_graph;
4399 /* create a "template" entity which is copied to the entity on the stack */
4400 ident *const id = id_unique("initializer.%u");
4401 ir_type *const irtype = get_ir_type(type);
4402 ir_type *const global_type = get_glob_type();
4403 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4404 set_entity_ld_ident(init_entity, id);
4406 set_entity_visibility(init_entity, ir_visibility_private);
4407 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4409 set_entity_initializer(init_entity, irinitializer);
4411 ir_node *const src_addr = create_symconst(dbgi, init_entity);
4412 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4414 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4415 set_store(copyb_mem);
4418 static void create_initializer_local_variable_entity(entity_t *entity)
4420 assert(entity->kind == ENTITY_VARIABLE);
4421 initializer_t *initializer = entity->variable.initializer;
4422 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4423 ir_entity *irentity = entity->variable.v.entity;
4424 type_t *type = entity->declaration.type;
4426 create_local_initializer(initializer, dbgi, irentity, type);
4429 static void create_variable_initializer(entity_t *entity)
4431 assert(entity->kind == ENTITY_VARIABLE);
4432 initializer_t *initializer = entity->variable.initializer;
4433 if (initializer == NULL)
4436 declaration_kind_t declaration_kind
4437 = (declaration_kind_t) entity->declaration.kind;
4438 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4439 create_initializer_local_variable_entity(entity);
4443 type_t *type = entity->declaration.type;
4444 type_qualifiers_t tq = get_type_qualifier(type, true);
4446 if (initializer->kind == INITIALIZER_VALUE) {
4447 initializer_value_t *initializer_value = &initializer->value;
4448 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4450 ir_node *value = expression_to_firm(initializer_value->value);
4452 type_t *init_type = initializer_value->value->base.type;
4453 ir_mode *mode = get_ir_mode_storage(init_type);
4454 value = create_conv(dbgi, value, mode);
4455 value = do_strict_conv(dbgi, value);
4457 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4458 set_value(entity->variable.v.value_number, value);
4460 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4462 ir_entity *irentity = entity->variable.v.entity;
4464 if (tq & TYPE_QUALIFIER_CONST
4465 && get_entity_owner(irentity) != get_tls_type()) {
4466 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4468 set_atomic_ent_value(irentity, value);
4471 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4472 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4474 ir_entity *irentity = entity->variable.v.entity;
4475 ir_initializer_t *irinitializer
4476 = create_ir_initializer(initializer, type);
4478 if (tq & TYPE_QUALIFIER_CONST) {
4479 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4481 set_entity_initializer(irentity, irinitializer);
4485 static void create_variable_length_array(entity_t *entity)
4487 assert(entity->kind == ENTITY_VARIABLE);
4488 assert(entity->variable.initializer == NULL);
4490 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4491 entity->variable.v.vla_base = NULL;
4493 /* TODO: record VLA somewhere so we create the free node when we leave
4497 static void allocate_variable_length_array(entity_t *entity)
4499 assert(entity->kind == ENTITY_VARIABLE);
4500 assert(entity->variable.initializer == NULL);
4501 assert(currently_reachable());
4503 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4504 type_t *type = entity->declaration.type;
4505 ir_type *el_type = get_ir_type(type->array.element_type);
4507 /* make sure size_node is calculated */
4508 get_type_size_node(type);
4509 ir_node *elems = type->array.size_node;
4510 ir_node *mem = get_store();
4511 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4513 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4514 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4517 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4518 entity->variable.v.vla_base = addr;
4522 * Creates a Firm local variable from a declaration.
4524 static void create_local_variable(entity_t *entity)
4526 assert(entity->kind == ENTITY_VARIABLE);
4527 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4529 bool needs_entity = entity->variable.address_taken;
4530 type_t *type = skip_typeref(entity->declaration.type);
4532 /* is it a variable length array? */
4533 if (is_type_array(type) && !type->array.size_constant) {
4534 create_variable_length_array(entity);
4536 } else if (is_type_array(type) || is_type_compound(type)) {
4537 needs_entity = true;
4538 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4539 needs_entity = true;
4543 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4544 create_variable_entity(entity,
4545 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4548 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4549 entity->variable.v.value_number = next_value_number_function;
4550 set_irg_loc_description(current_ir_graph, next_value_number_function,
4552 ++next_value_number_function;
4556 static void create_local_static_variable(entity_t *entity)
4558 assert(entity->kind == ENTITY_VARIABLE);
4559 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4561 type_t *type = skip_typeref(entity->declaration.type);
4562 ir_type *const var_type = entity->variable.thread_local ?
4563 get_tls_type() : get_glob_type();
4564 ir_type *const irtype = get_ir_type(type);
4565 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4567 size_t l = strlen(entity->base.symbol->string);
4568 char buf[l + sizeof(".%u")];
4569 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4570 ident *const id = id_unique(buf);
4571 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4573 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4574 set_entity_volatility(irentity, volatility_is_volatile);
4577 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4578 entity->variable.v.entity = irentity;
4580 set_entity_ld_ident(irentity, id);
4581 set_entity_visibility(irentity, ir_visibility_local);
4583 ir_graph *const old_current_ir_graph = current_ir_graph;
4584 current_ir_graph = get_const_code_irg();
4586 create_variable_initializer(entity);
4588 assert(current_ir_graph == get_const_code_irg());
4589 current_ir_graph = old_current_ir_graph;
4594 static void return_statement_to_firm(return_statement_t *statement)
4596 if (!currently_reachable())
4599 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4600 type_t *type = current_function_entity->declaration.type;
4601 ir_type *func_irtype = get_ir_type(type);
4605 if (get_method_n_ress(func_irtype) > 0) {
4606 ir_type *res_type = get_method_res_type(func_irtype, 0);
4608 if (statement->value != NULL) {
4609 ir_node *node = expression_to_firm(statement->value);
4610 if (!is_compound_type(res_type)) {
4611 type_t *ret_value_type = statement->value->base.type;
4612 ir_mode *mode = get_ir_mode_storage(ret_value_type);
4613 node = create_conv(dbgi, node, mode);
4614 node = do_strict_conv(dbgi, node);
4619 if (is_compound_type(res_type)) {
4622 mode = get_type_mode(res_type);
4624 in[0] = new_Unknown(mode);
4628 /* build return_value for its side effects */
4629 if (statement->value != NULL) {
4630 expression_to_firm(statement->value);
4635 ir_node *store = get_store();
4636 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4638 ir_node *end_block = get_irg_end_block(current_ir_graph);
4639 add_immBlock_pred(end_block, ret);
4641 set_unreachable_now();
4644 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4646 if (!currently_reachable())
4649 return expression_to_firm(statement->expression);
4652 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4654 entity_t *entity = compound->scope.entities;
4655 for ( ; entity != NULL; entity = entity->base.next) {
4656 if (!is_declaration(entity))
4659 create_local_declaration(entity);
4662 ir_node *result = NULL;
4663 statement_t *statement = compound->statements;
4664 for ( ; statement != NULL; statement = statement->base.next) {
4665 if (statement->base.next == NULL
4666 && statement->kind == STATEMENT_EXPRESSION) {
4667 result = expression_statement_to_firm(
4668 &statement->expression);
4671 statement_to_firm(statement);
4677 static void create_global_variable(entity_t *entity)
4679 ir_linkage linkage = IR_LINKAGE_DEFAULT;
4680 ir_visibility visibility = ir_visibility_default;
4681 ir_entity *irentity;
4682 assert(entity->kind == ENTITY_VARIABLE);
4684 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4685 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4686 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4687 case STORAGE_CLASS_NONE:
4688 visibility = ir_visibility_default;
4689 /* uninitialized globals get merged in C */
4690 if (entity->variable.initializer == NULL)
4691 linkage |= IR_LINKAGE_MERGE;
4693 case STORAGE_CLASS_TYPEDEF:
4694 case STORAGE_CLASS_AUTO:
4695 case STORAGE_CLASS_REGISTER:
4696 panic("invalid storage class for global var");
4699 ir_type *var_type = get_glob_type();
4700 if (entity->variable.thread_local) {
4701 var_type = get_tls_type();
4702 /* LINKAGE_MERGE not supported by current linkers */
4703 linkage &= ~IR_LINKAGE_MERGE;
4705 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4706 irentity = entity->variable.v.entity;
4707 add_entity_linkage(irentity, linkage);
4708 set_entity_visibility(irentity, visibility);
4711 static void create_local_declaration(entity_t *entity)
4713 assert(is_declaration(entity));
4715 /* construct type */
4716 (void) get_ir_type(entity->declaration.type);
4717 if (entity->base.symbol == NULL) {
4721 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4722 case STORAGE_CLASS_STATIC:
4723 if (entity->kind == ENTITY_FUNCTION) {
4724 (void)get_function_entity(entity, NULL);
4726 create_local_static_variable(entity);
4729 case STORAGE_CLASS_EXTERN:
4730 if (entity->kind == ENTITY_FUNCTION) {
4731 assert(entity->function.statement == NULL);
4732 (void)get_function_entity(entity, NULL);
4734 create_global_variable(entity);
4735 create_variable_initializer(entity);
4738 case STORAGE_CLASS_NONE:
4739 case STORAGE_CLASS_AUTO:
4740 case STORAGE_CLASS_REGISTER:
4741 if (entity->kind == ENTITY_FUNCTION) {
4742 if (entity->function.statement != NULL) {
4743 ir_type *owner = get_irg_frame_type(current_ir_graph);
4744 (void)get_function_entity(entity, owner);
4745 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4746 enqueue_inner_function(entity);
4748 (void)get_function_entity(entity, NULL);
4751 create_local_variable(entity);
4754 case STORAGE_CLASS_TYPEDEF:
4757 panic("invalid storage class found");
4760 static void initialize_local_declaration(entity_t *entity)
4762 if (entity->base.symbol == NULL)
4765 // no need to emit code in dead blocks
4766 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4767 && !currently_reachable())
4770 switch ((declaration_kind_t) entity->declaration.kind) {
4771 case DECLARATION_KIND_LOCAL_VARIABLE:
4772 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4773 create_variable_initializer(entity);
4776 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4777 allocate_variable_length_array(entity);
4780 case DECLARATION_KIND_COMPOUND_MEMBER:
4781 case DECLARATION_KIND_GLOBAL_VARIABLE:
4782 case DECLARATION_KIND_FUNCTION:
4783 case DECLARATION_KIND_INNER_FUNCTION:
4786 case DECLARATION_KIND_PARAMETER:
4787 case DECLARATION_KIND_PARAMETER_ENTITY:
4788 panic("can't initialize parameters");
4790 case DECLARATION_KIND_UNKNOWN:
4791 panic("can't initialize unknown declaration");
4793 panic("invalid declaration kind");
4796 static void declaration_statement_to_firm(declaration_statement_t *statement)
4798 entity_t *entity = statement->declarations_begin;
4802 entity_t *const last = statement->declarations_end;
4803 for ( ;; entity = entity->base.next) {
4804 if (is_declaration(entity)) {
4805 initialize_local_declaration(entity);
4806 } else if (entity->kind == ENTITY_TYPEDEF) {
4807 /* ยง6.7.7:3 Any array size expressions associated with variable length
4808 * array declarators are evaluated each time the declaration of the
4809 * typedef name is reached in the order of execution. */
4810 type_t *const type = skip_typeref(entity->typedefe.type);
4811 if (is_type_array(type) && type->array.is_vla)
4812 get_vla_size(&type->array);
4819 static void if_statement_to_firm(if_statement_t *statement)
4821 /* Create the condition. */
4822 ir_node *true_block = NULL;
4823 ir_node *false_block = NULL;
4824 if (currently_reachable()) {
4825 true_block = new_immBlock();
4826 false_block = new_immBlock();
4827 create_condition_evaluation(statement->condition, true_block, false_block);
4828 mature_immBlock(true_block);
4831 /* Create the false statement.
4832 * Handle false before true, so if no false statement is present, then the
4833 * empty false block is reused as fallthrough block. */
4834 ir_node *fallthrough_block = NULL;
4835 if (statement->false_statement != NULL) {
4836 if (false_block != NULL) {
4837 mature_immBlock(false_block);
4839 set_cur_block(false_block);
4840 statement_to_firm(statement->false_statement);
4841 if (currently_reachable()) {
4842 fallthrough_block = new_immBlock();
4843 add_immBlock_pred(fallthrough_block, new_Jmp());
4846 fallthrough_block = false_block;
4849 /* Create the true statement. */
4850 set_cur_block(true_block);
4851 statement_to_firm(statement->true_statement);
4852 if (currently_reachable()) {
4853 if (fallthrough_block == NULL) {
4854 fallthrough_block = new_immBlock();
4856 add_immBlock_pred(fallthrough_block, new_Jmp());
4859 /* Handle the block after the if-statement. */
4860 if (fallthrough_block != NULL) {
4861 mature_immBlock(fallthrough_block);
4863 set_cur_block(fallthrough_block);
4866 /* Create a jump node which jumps into target_block, if the current block is
4868 static void jump_if_reachable(ir_node *const target_block)
4870 ir_node *const pred = currently_reachable() ? new_Jmp() : new_Bad(mode_X);
4871 add_immBlock_pred(target_block, pred);
4874 static void while_statement_to_firm(while_statement_t *statement)
4876 /* Create the header block */
4877 ir_node *const header_block = new_immBlock();
4878 jump_if_reachable(header_block);
4880 /* Create the condition. */
4881 ir_node * body_block;
4882 ir_node * false_block;
4883 expression_t *const cond = statement->condition;
4884 if (is_constant_expression(cond) == EXPR_CLASS_CONSTANT &&
4885 fold_constant_to_bool(cond)) {
4886 /* Shortcut for while (true). */
4887 body_block = header_block;
4890 keep_alive(header_block);
4891 keep_all_memory(header_block);
4893 body_block = new_immBlock();
4894 false_block = new_immBlock();
4896 set_cur_block(header_block);
4897 create_condition_evaluation(cond, body_block, false_block);
4898 mature_immBlock(body_block);
4901 ir_node *const old_continue_label = continue_label;
4902 ir_node *const old_break_label = break_label;
4903 continue_label = header_block;
4904 break_label = false_block;
4906 /* Create the loop body. */
4907 set_cur_block(body_block);
4908 statement_to_firm(statement->body);
4909 jump_if_reachable(header_block);
4911 mature_immBlock(header_block);
4912 assert(false_block == NULL || false_block == break_label);
4913 false_block = break_label;
4914 if (false_block != NULL) {
4915 mature_immBlock(false_block);
4917 set_cur_block(false_block);
4919 assert(continue_label == header_block);
4920 continue_label = old_continue_label;
4921 break_label = old_break_label;
4924 static ir_node *get_break_label(void)
4926 if (break_label == NULL) {
4927 break_label = new_immBlock();
4932 static void do_while_statement_to_firm(do_while_statement_t *statement)
4934 /* create the header block */
4935 ir_node *header_block = new_immBlock();
4938 ir_node *body_block = new_immBlock();
4939 jump_if_reachable(body_block);
4941 ir_node *old_continue_label = continue_label;
4942 ir_node *old_break_label = break_label;
4943 continue_label = header_block;
4946 set_cur_block(body_block);
4947 statement_to_firm(statement->body);
4948 ir_node *const false_block = get_break_label();
4950 assert(continue_label == header_block);
4951 continue_label = old_continue_label;
4952 break_label = old_break_label;
4954 jump_if_reachable(header_block);
4956 /* create the condition */
4957 mature_immBlock(header_block);
4958 set_cur_block(header_block);
4960 create_condition_evaluation(statement->condition, body_block, false_block);
4961 mature_immBlock(body_block);
4962 mature_immBlock(false_block);
4964 set_cur_block(false_block);
4967 static void for_statement_to_firm(for_statement_t *statement)
4969 /* create declarations */
4970 entity_t *entity = statement->scope.entities;
4971 for ( ; entity != NULL; entity = entity->base.next) {
4972 if (!is_declaration(entity))
4975 create_local_declaration(entity);
4978 if (currently_reachable()) {
4979 entity = statement->scope.entities;
4980 for ( ; entity != NULL; entity = entity->base.next) {
4981 if (!is_declaration(entity))
4984 initialize_local_declaration(entity);
4987 if (statement->initialisation != NULL) {
4988 expression_to_firm(statement->initialisation);
4992 /* Create the header block */
4993 ir_node *const header_block = new_immBlock();
4994 jump_if_reachable(header_block);
4996 /* Create the condition. */
4997 ir_node *body_block;
4998 ir_node *false_block;
4999 if (statement->condition != NULL) {
5000 body_block = new_immBlock();
5001 false_block = new_immBlock();
5003 set_cur_block(header_block);
5004 create_condition_evaluation(statement->condition, body_block, false_block);
5005 mature_immBlock(body_block);
5008 body_block = header_block;
5011 keep_alive(header_block);
5012 keep_all_memory(header_block);
5015 /* Create the step block, if necessary. */
5016 ir_node * step_block = header_block;
5017 expression_t *const step = statement->step;
5019 step_block = new_immBlock();
5022 ir_node *const old_continue_label = continue_label;
5023 ir_node *const old_break_label = break_label;
5024 continue_label = step_block;
5025 break_label = false_block;
5027 /* Create the loop body. */
5028 set_cur_block(body_block);
5029 statement_to_firm(statement->body);
5030 jump_if_reachable(step_block);
5032 /* Create the step code. */
5034 mature_immBlock(step_block);
5035 set_cur_block(step_block);
5036 expression_to_firm(step);
5037 jump_if_reachable(header_block);
5040 mature_immBlock(header_block);
5041 assert(false_block == NULL || false_block == break_label);
5042 false_block = break_label;
5043 if (false_block != NULL) {
5044 mature_immBlock(false_block);
5046 set_cur_block(false_block);
5048 assert(continue_label == step_block);
5049 continue_label = old_continue_label;
5050 break_label = old_break_label;
5053 static void create_jump_statement(const statement_t *statement,
5054 ir_node *target_block)
5056 if (!currently_reachable())
5059 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5060 ir_node *jump = new_d_Jmp(dbgi);
5061 add_immBlock_pred(target_block, jump);
5063 set_unreachable_now();
5066 static void switch_statement_to_firm(switch_statement_t *statement)
5068 ir_node *first_block = NULL;
5069 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5070 ir_node *cond = NULL;
5072 if (currently_reachable()) {
5073 ir_node *expression = expression_to_firm(statement->expression);
5074 cond = new_d_Cond(dbgi, expression);
5075 first_block = get_cur_block();
5078 set_unreachable_now();
5080 ir_node *const old_switch_cond = current_switch_cond;
5081 ir_node *const old_break_label = break_label;
5082 const bool old_saw_default_label = saw_default_label;
5083 saw_default_label = false;
5084 current_switch_cond = cond;
5086 switch_statement_t *const old_switch = current_switch;
5087 current_switch = statement;
5089 /* determine a free number for the default label */
5090 unsigned long num_cases = 0;
5091 long default_proj_nr = 0;
5092 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5093 if (l->expression == NULL) {
5097 if (l->last_case >= l->first_case)
5098 num_cases += l->last_case - l->first_case + 1;
5099 if (l->last_case > default_proj_nr)
5100 default_proj_nr = l->last_case;
5103 if (default_proj_nr == LONG_MAX) {
5104 /* Bad: an overflow will occur, we cannot be sure that the
5105 * maximum + 1 is a free number. Scan the values a second
5106 * time to find a free number.
5108 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5110 memset(bits, 0, (num_cases + 7) >> 3);
5111 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5112 if (l->expression == NULL) {
5116 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5117 if (start < num_cases && l->last_case >= 0) {
5118 unsigned long end = (unsigned long)l->last_case < num_cases ?
5119 (unsigned long)l->last_case : num_cases - 1;
5120 for (unsigned long cns = start; cns <= end; ++cns) {
5121 bits[cns >> 3] |= (1 << (cns & 7));
5125 /* We look at the first num_cases constants:
5126 * Either they are dense, so we took the last (num_cases)
5127 * one, or they are not dense, so we will find one free
5131 for (i = 0; i < num_cases; ++i)
5132 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5136 default_proj_nr = i;
5140 statement->default_proj_nr = default_proj_nr;
5141 /* safety check: cond might already be folded to a Bad */
5142 if (cond != NULL && is_Cond(cond)) {
5143 set_Cond_default_proj(cond, default_proj_nr);
5146 statement_to_firm(statement->body);
5148 jump_if_reachable(get_break_label());
5150 if (!saw_default_label && first_block != NULL) {
5151 set_cur_block(first_block);
5152 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5153 add_immBlock_pred(get_break_label(), proj);
5156 if (break_label != NULL) {
5157 mature_immBlock(break_label);
5159 set_cur_block(break_label);
5161 assert(current_switch_cond == cond);
5162 current_switch = old_switch;
5163 current_switch_cond = old_switch_cond;
5164 break_label = old_break_label;
5165 saw_default_label = old_saw_default_label;
5168 static void case_label_to_firm(const case_label_statement_t *statement)
5170 if (statement->is_empty_range)
5173 ir_node *block = new_immBlock();
5174 /* Fallthrough from previous case */
5175 jump_if_reachable(block);
5177 if (current_switch_cond != NULL) {
5178 set_cur_block(get_nodes_block(current_switch_cond));
5179 dbg_info *const dbgi = get_dbg_info(&statement->base.source_position);
5180 if (statement->expression != NULL) {
5181 long pn = statement->first_case;
5182 long end_pn = statement->last_case;
5183 assert(pn <= end_pn);
5184 /* create jumps for all cases in the given range */
5186 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5187 add_immBlock_pred(block, proj);
5188 } while (pn++ < end_pn);
5190 saw_default_label = true;
5191 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5192 current_switch->default_proj_nr);
5193 add_immBlock_pred(block, proj);
5197 mature_immBlock(block);
5198 set_cur_block(block);
5200 statement_to_firm(statement->statement);
5203 static void label_to_firm(const label_statement_t *statement)
5205 ir_node *block = get_label_block(statement->label);
5206 jump_if_reachable(block);
5208 set_cur_block(block);
5210 keep_all_memory(block);
5212 statement_to_firm(statement->statement);
5215 static void goto_to_firm(const goto_statement_t *statement)
5217 if (!currently_reachable())
5220 if (statement->expression) {
5221 ir_node *irn = expression_to_firm(statement->expression);
5222 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5223 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5225 set_irn_link(ijmp, ijmp_list);
5228 ir_node *block = get_label_block(statement->label);
5229 ir_node *jmp = new_Jmp();
5230 add_immBlock_pred(block, jmp);
5232 set_unreachable_now();
5235 static void asm_statement_to_firm(const asm_statement_t *statement)
5237 bool needs_memory = false;
5239 if (statement->is_volatile) {
5240 needs_memory = true;
5243 size_t n_clobbers = 0;
5244 asm_clobber_t *clobber = statement->clobbers;
5245 for ( ; clobber != NULL; clobber = clobber->next) {
5246 const char *clobber_str = clobber->clobber.begin;
5248 if (!be_is_valid_clobber(clobber_str)) {
5249 errorf(&statement->base.source_position,
5250 "invalid clobber '%s' specified", clobber->clobber);
5254 if (strcmp(clobber_str, "memory") == 0) {
5255 needs_memory = true;
5259 ident *id = new_id_from_str(clobber_str);
5260 obstack_ptr_grow(&asm_obst, id);
5263 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5264 ident **clobbers = NULL;
5265 if (n_clobbers > 0) {
5266 clobbers = obstack_finish(&asm_obst);
5269 size_t n_inputs = 0;
5270 asm_argument_t *argument = statement->inputs;
5271 for ( ; argument != NULL; argument = argument->next)
5273 size_t n_outputs = 0;
5274 argument = statement->outputs;
5275 for ( ; argument != NULL; argument = argument->next)
5278 unsigned next_pos = 0;
5280 ir_node *ins[n_inputs + n_outputs + 1];
5283 ir_asm_constraint tmp_in_constraints[n_outputs];
5285 const expression_t *out_exprs[n_outputs];
5286 ir_node *out_addrs[n_outputs];
5287 size_t out_size = 0;
5289 argument = statement->outputs;
5290 for ( ; argument != NULL; argument = argument->next) {
5291 const char *constraints = argument->constraints.begin;
5292 asm_constraint_flags_t asm_flags
5293 = be_parse_asm_constraints(constraints);
5295 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5296 warningf(&statement->base.source_position,
5297 "some constraints in '%s' are not supported", constraints);
5299 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5300 errorf(&statement->base.source_position,
5301 "some constraints in '%s' are invalid", constraints);
5304 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5305 errorf(&statement->base.source_position,
5306 "no write flag specified for output constraints '%s'",
5311 unsigned pos = next_pos++;
5312 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5313 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5314 expression_t *expr = argument->expression;
5315 ir_node *addr = expression_to_addr(expr);
5316 /* in+output, construct an artifical same_as constraint on the
5318 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5320 ir_node *value = get_value_from_lvalue(expr, addr);
5322 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5324 ir_asm_constraint constraint;
5325 constraint.pos = pos;
5326 constraint.constraint = new_id_from_str(buf);
5327 constraint.mode = get_ir_mode_storage(expr->base.type);
5328 tmp_in_constraints[in_size] = constraint;
5329 ins[in_size] = value;
5334 out_exprs[out_size] = expr;
5335 out_addrs[out_size] = addr;
5337 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5338 /* pure memory ops need no input (but we have to make sure we
5339 * attach to the memory) */
5340 assert(! (asm_flags &
5341 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5342 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5343 needs_memory = true;
5345 /* we need to attach the address to the inputs */
5346 expression_t *expr = argument->expression;
5348 ir_asm_constraint constraint;
5349 constraint.pos = pos;
5350 constraint.constraint = new_id_from_str(constraints);
5351 constraint.mode = NULL;
5352 tmp_in_constraints[in_size] = constraint;
5354 ins[in_size] = expression_to_addr(expr);
5358 errorf(&statement->base.source_position,
5359 "only modifiers but no place set in constraints '%s'",
5364 ir_asm_constraint constraint;
5365 constraint.pos = pos;
5366 constraint.constraint = new_id_from_str(constraints);
5367 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5369 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5371 assert(obstack_object_size(&asm_obst)
5372 == out_size * sizeof(ir_asm_constraint));
5373 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5376 obstack_grow(&asm_obst, tmp_in_constraints,
5377 in_size * sizeof(tmp_in_constraints[0]));
5378 /* find and count input and output arguments */
5379 argument = statement->inputs;
5380 for ( ; argument != NULL; argument = argument->next) {
5381 const char *constraints = argument->constraints.begin;
5382 asm_constraint_flags_t asm_flags
5383 = be_parse_asm_constraints(constraints);
5385 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5386 errorf(&statement->base.source_position,
5387 "some constraints in '%s' are not supported", constraints);
5390 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5391 errorf(&statement->base.source_position,
5392 "some constraints in '%s' are invalid", constraints);
5395 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5396 errorf(&statement->base.source_position,
5397 "write flag specified for input constraints '%s'",
5403 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5404 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5405 /* we can treat this as "normal" input */
5406 input = expression_to_firm(argument->expression);
5407 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5408 /* pure memory ops need no input (but we have to make sure we
5409 * attach to the memory) */
5410 assert(! (asm_flags &
5411 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5412 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5413 needs_memory = true;
5414 input = expression_to_addr(argument->expression);
5416 errorf(&statement->base.source_position,
5417 "only modifiers but no place set in constraints '%s'",
5422 ir_asm_constraint constraint;
5423 constraint.pos = next_pos++;
5424 constraint.constraint = new_id_from_str(constraints);
5425 constraint.mode = get_irn_mode(input);
5427 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5428 ins[in_size++] = input;
5432 ir_asm_constraint constraint;
5433 constraint.pos = next_pos++;
5434 constraint.constraint = new_id_from_str("");
5435 constraint.mode = mode_M;
5437 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5438 ins[in_size++] = get_store();
5441 assert(obstack_object_size(&asm_obst)
5442 == in_size * sizeof(ir_asm_constraint));
5443 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5445 /* create asm node */
5446 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5448 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5450 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5451 out_size, output_constraints,
5452 n_clobbers, clobbers, asm_text);
5454 if (statement->is_volatile) {
5455 set_irn_pinned(node, op_pin_state_pinned);
5457 set_irn_pinned(node, op_pin_state_floats);
5460 /* create output projs & connect them */
5462 ir_node *projm = new_Proj(node, mode_M, out_size);
5467 for (i = 0; i < out_size; ++i) {
5468 const expression_t *out_expr = out_exprs[i];
5470 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5471 ir_node *proj = new_Proj(node, mode, pn);
5472 ir_node *addr = out_addrs[i];
5474 set_value_for_expression_addr(out_expr, proj, addr);
5478 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5480 statement_to_firm(statement->try_statement);
5481 warningf(&statement->base.source_position, "structured exception handling ignored");
5484 static void leave_statement_to_firm(leave_statement_t *statement)
5486 errorf(&statement->base.source_position, "__leave not supported yet");
5490 * Transform a statement.
5492 static void statement_to_firm(statement_t *statement)
5495 assert(!statement->base.transformed);
5496 statement->base.transformed = true;
5499 switch (statement->kind) {
5500 case STATEMENT_INVALID:
5501 panic("invalid statement found");
5502 case STATEMENT_EMPTY:
5505 case STATEMENT_COMPOUND:
5506 compound_statement_to_firm(&statement->compound);
5508 case STATEMENT_RETURN:
5509 return_statement_to_firm(&statement->returns);
5511 case STATEMENT_EXPRESSION:
5512 expression_statement_to_firm(&statement->expression);
5515 if_statement_to_firm(&statement->ifs);
5517 case STATEMENT_WHILE:
5518 while_statement_to_firm(&statement->whiles);
5520 case STATEMENT_DO_WHILE:
5521 do_while_statement_to_firm(&statement->do_while);
5523 case STATEMENT_DECLARATION:
5524 declaration_statement_to_firm(&statement->declaration);
5526 case STATEMENT_BREAK:
5527 create_jump_statement(statement, get_break_label());
5529 case STATEMENT_CONTINUE:
5530 create_jump_statement(statement, continue_label);
5532 case STATEMENT_SWITCH:
5533 switch_statement_to_firm(&statement->switchs);
5535 case STATEMENT_CASE_LABEL:
5536 case_label_to_firm(&statement->case_label);
5539 for_statement_to_firm(&statement->fors);
5541 case STATEMENT_LABEL:
5542 label_to_firm(&statement->label);
5544 case STATEMENT_GOTO:
5545 goto_to_firm(&statement->gotos);
5548 asm_statement_to_firm(&statement->asms);
5550 case STATEMENT_MS_TRY:
5551 ms_try_statement_to_firm(&statement->ms_try);
5553 case STATEMENT_LEAVE:
5554 leave_statement_to_firm(&statement->leave);
5557 panic("statement not implemented");
5560 static int count_local_variables(const entity_t *entity,
5561 const entity_t *const last)
5564 entity_t const *const end = last != NULL ? last->base.next : NULL;
5565 for (; entity != end; entity = entity->base.next) {
5569 if (entity->kind == ENTITY_VARIABLE) {
5570 type = skip_typeref(entity->declaration.type);
5571 address_taken = entity->variable.address_taken;
5572 } else if (entity->kind == ENTITY_PARAMETER) {
5573 type = skip_typeref(entity->declaration.type);
5574 address_taken = entity->parameter.address_taken;
5579 if (!address_taken && is_type_scalar(type))
5585 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5587 int *const count = env;
5589 switch (stmt->kind) {
5590 case STATEMENT_DECLARATION: {
5591 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5592 *count += count_local_variables(decl_stmt->declarations_begin,
5593 decl_stmt->declarations_end);
5598 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5607 * Return the number of local (alias free) variables used by a function.
5609 static int get_function_n_local_vars(entity_t *entity)
5611 const function_t *function = &entity->function;
5614 /* count parameters */
5615 count += count_local_variables(function->parameters.entities, NULL);
5617 /* count local variables declared in body */
5618 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5623 * Build Firm code for the parameters of a function.
5625 static void initialize_function_parameters(entity_t *entity)
5627 assert(entity->kind == ENTITY_FUNCTION);
5628 ir_graph *irg = current_ir_graph;
5629 ir_node *args = get_irg_args(irg);
5630 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5631 int first_param_nr = 0;
5633 if (entity->function.need_closure) {
5634 /* add an extra parameter for the static link */
5635 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5640 entity_t *parameter = entity->function.parameters.entities;
5641 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5642 if (parameter->kind != ENTITY_PARAMETER)
5645 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5646 type_t *type = skip_typeref(parameter->declaration.type);
5648 bool needs_entity = parameter->parameter.address_taken;
5649 assert(!is_type_array(type));
5650 if (is_type_compound(type)) {
5651 needs_entity = true;
5655 ir_entity *param = get_method_value_param_ent(function_irtype, n);
5656 ident *id = new_id_from_str(parameter->base.symbol->string);
5657 set_entity_ident(param, id);
5659 parameter->declaration.kind
5660 = DECLARATION_KIND_PARAMETER_ENTITY;
5661 parameter->parameter.v.entity = param;
5665 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5666 ir_mode *param_mode = get_type_mode(param_irtype);
5668 long pn = n + first_param_nr;
5669 ir_node *value = new_r_Proj(args, param_mode, pn);
5671 ir_mode *mode = get_ir_mode_storage(type);
5672 value = create_conv(NULL, value, mode);
5673 value = do_strict_conv(NULL, value);
5675 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5676 parameter->parameter.v.value_number = next_value_number_function;
5677 set_irg_loc_description(current_ir_graph, next_value_number_function,
5679 ++next_value_number_function;
5681 set_value(parameter->parameter.v.value_number, value);
5686 * Handle additional decl modifiers for IR-graphs
5688 * @param irg the IR-graph
5689 * @param dec_modifiers additional modifiers
5691 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5692 decl_modifiers_t decl_modifiers)
5694 if (decl_modifiers & DM_RETURNS_TWICE) {
5695 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5696 add_irg_additional_properties(irg, mtp_property_returns_twice);
5698 if (decl_modifiers & DM_NORETURN) {
5699 /* TRUE if the declaration includes the Microsoft
5700 __declspec(noreturn) specifier. */
5701 add_irg_additional_properties(irg, mtp_property_noreturn);
5703 if (decl_modifiers & DM_NOTHROW) {
5704 /* TRUE if the declaration includes the Microsoft
5705 __declspec(nothrow) specifier. */
5706 add_irg_additional_properties(irg, mtp_property_nothrow);
5708 if (decl_modifiers & DM_NAKED) {
5709 /* TRUE if the declaration includes the Microsoft
5710 __declspec(naked) specifier. */
5711 add_irg_additional_properties(irg, mtp_property_naked);
5713 if (decl_modifiers & DM_FORCEINLINE) {
5714 /* TRUE if the declaration includes the
5715 Microsoft __forceinline specifier. */
5716 set_irg_inline_property(irg, irg_inline_forced);
5718 if (decl_modifiers & DM_NOINLINE) {
5719 /* TRUE if the declaration includes the Microsoft
5720 __declspec(noinline) specifier. */
5721 set_irg_inline_property(irg, irg_inline_forbidden);
5725 static void add_function_pointer(ir_type *segment, ir_entity *method,
5726 const char *unique_template)
5728 ir_type *method_type = get_entity_type(method);
5729 ir_type *ptr_type = new_type_pointer(method_type);
5731 /* these entities don't really have a name but firm only allows
5733 * Note that we mustn't give these entities a name since for example
5734 * Mach-O doesn't allow them. */
5735 ident *ide = id_unique(unique_template);
5736 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5737 ir_graph *irg = get_const_code_irg();
5738 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5741 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5742 set_entity_compiler_generated(ptr, 1);
5743 set_entity_visibility(ptr, ir_visibility_private);
5744 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5745 set_atomic_ent_value(ptr, val);
5749 * Generate possible IJmp branches to a given label block.
5751 static void gen_ijmp_branches(ir_node *block)
5754 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5755 add_immBlock_pred(block, ijmp);
5760 * Create code for a function and all inner functions.
5762 * @param entity the function entity
5764 static void create_function(entity_t *entity)
5766 assert(entity->kind == ENTITY_FUNCTION);
5767 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5769 if (entity->function.statement == NULL)
5772 if (is_main(entity) && enable_main_collect2_hack) {
5773 prepare_main_collect2(entity);
5776 inner_functions = NULL;
5777 current_trampolines = NULL;
5779 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5780 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5781 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5783 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5784 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5785 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5788 current_function_entity = entity;
5789 current_function_name = NULL;
5790 current_funcsig = NULL;
5792 assert(all_labels == NULL);
5793 all_labels = NEW_ARR_F(label_t *, 0);
5796 int n_local_vars = get_function_n_local_vars(entity);
5797 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5798 current_ir_graph = irg;
5800 ir_graph *old_current_function = current_function;
5801 current_function = irg;
5803 set_irg_fp_model(irg, firm_fp_model);
5804 tarval_enable_fp_ops(1);
5805 set_irn_dbg_info(get_irg_start_block(irg),
5806 get_entity_dbg_info(function_entity));
5808 ir_node *first_block = get_cur_block();
5810 /* set inline flags */
5811 if (entity->function.is_inline)
5812 set_irg_inline_property(irg, irg_inline_recomended);
5813 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5815 next_value_number_function = 0;
5816 initialize_function_parameters(entity);
5817 current_static_link = entity->function.static_link;
5819 statement_to_firm(entity->function.statement);
5821 ir_node *end_block = get_irg_end_block(irg);
5823 /* do we have a return statement yet? */
5824 if (currently_reachable()) {
5825 type_t *type = skip_typeref(entity->declaration.type);
5826 assert(is_type_function(type));
5827 const function_type_t *func_type = &type->function;
5828 const type_t *return_type
5829 = skip_typeref(func_type->return_type);
5832 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5833 ret = new_Return(get_store(), 0, NULL);
5836 if (is_type_scalar(return_type)) {
5837 mode = get_ir_mode_storage(func_type->return_type);
5843 /* ยง5.1.2.2.3 main implicitly returns 0 */
5844 if (is_main(entity)) {
5845 in[0] = new_Const(get_mode_null(mode));
5847 in[0] = new_Unknown(mode);
5849 ret = new_Return(get_store(), 1, in);
5851 add_immBlock_pred(end_block, ret);
5854 bool has_computed_gotos = false;
5855 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5856 label_t *label = all_labels[i];
5857 if (label->address_taken) {
5858 gen_ijmp_branches(label->block);
5859 has_computed_gotos = true;
5861 mature_immBlock(label->block);
5863 if (has_computed_gotos) {
5864 /* if we have computed goto's in the function, we cannot inline it */
5865 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5866 source_position_t const *const pos = &entity->base.source_position;
5867 warningf(pos, "'%N' can never be inlined because it contains a computed goto", entity);
5869 set_irg_inline_property(irg, irg_inline_forbidden);
5872 DEL_ARR_F(all_labels);
5875 mature_immBlock(first_block);
5876 mature_immBlock(end_block);
5878 irg_finalize_cons(irg);
5880 /* finalize the frame type */
5881 ir_type *frame_type = get_irg_frame_type(irg);
5882 int n = get_compound_n_members(frame_type);
5885 for (int i = 0; i < n; ++i) {
5886 ir_entity *member = get_compound_member(frame_type, i);
5887 ir_type *entity_type = get_entity_type(member);
5889 int align = get_type_alignment_bytes(entity_type);
5890 if (align > align_all)
5894 misalign = offset % align;
5896 offset += align - misalign;
5900 set_entity_offset(member, offset);
5901 offset += get_type_size_bytes(entity_type);
5903 set_type_size_bytes(frame_type, offset);
5904 set_type_alignment_bytes(frame_type, align_all);
5906 irg_verify(irg, VERIFY_ENFORCE_SSA);
5907 current_function = old_current_function;
5909 if (current_trampolines != NULL) {
5910 DEL_ARR_F(current_trampolines);
5911 current_trampolines = NULL;
5914 /* create inner functions if any */
5915 entity_t **inner = inner_functions;
5916 if (inner != NULL) {
5917 ir_type *rem_outer_frame = current_outer_frame;
5918 current_outer_frame = get_irg_frame_type(current_ir_graph);
5919 ir_type *rem_outer_value_type = current_outer_value_type;
5920 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5921 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5922 create_function(inner[i]);
5926 current_outer_value_type = rem_outer_value_type;
5927 current_outer_frame = rem_outer_frame;
5931 static void scope_to_firm(scope_t *scope)
5933 /* first pass: create declarations */
5934 entity_t *entity = scope->entities;
5935 for ( ; entity != NULL; entity = entity->base.next) {
5936 if (entity->base.symbol == NULL)
5939 if (entity->kind == ENTITY_FUNCTION) {
5940 if (entity->function.btk != bk_none) {
5941 /* builtins have no representation */
5944 (void)get_function_entity(entity, NULL);
5945 } else if (entity->kind == ENTITY_VARIABLE) {
5946 create_global_variable(entity);
5947 } else if (entity->kind == ENTITY_NAMESPACE) {
5948 scope_to_firm(&entity->namespacee.members);
5952 /* second pass: create code/initializers */
5953 entity = scope->entities;
5954 for ( ; entity != NULL; entity = entity->base.next) {
5955 if (entity->base.symbol == NULL)
5958 if (entity->kind == ENTITY_FUNCTION) {
5959 if (entity->function.btk != bk_none) {
5960 /* builtins have no representation */
5963 create_function(entity);
5964 } else if (entity->kind == ENTITY_VARIABLE) {
5965 assert(entity->declaration.kind
5966 == DECLARATION_KIND_GLOBAL_VARIABLE);
5967 current_ir_graph = get_const_code_irg();
5968 create_variable_initializer(entity);
5973 void init_ast2firm(void)
5975 obstack_init(&asm_obst);
5976 init_atomic_modes();
5978 ir_set_debug_retrieve(dbg_retrieve);
5979 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
5981 /* create idents for all known runtime functions */
5982 for (size_t i = 0; i < lengthof(rts_data); ++i) {
5983 rts_idents[i] = new_id_from_str(rts_data[i].name);
5986 entitymap_init(&entitymap);
5989 static void init_ir_types(void)
5991 static int ir_types_initialized = 0;
5992 if (ir_types_initialized)
5994 ir_types_initialized = 1;
5996 ir_type_int = get_ir_type(type_int);
5997 ir_type_char = get_ir_type(type_char);
5998 ir_type_const_char = get_ir_type(type_const_char);
5999 ir_type_wchar_t = get_ir_type(type_wchar_t);
6000 ir_type_void = get_ir_type(type_void);
6002 be_params = be_get_backend_param();
6003 mode_float_arithmetic = be_params->mode_float_arithmetic;
6005 stack_param_align = be_params->stack_param_align;
6008 void exit_ast2firm(void)
6010 entitymap_destroy(&entitymap);
6011 obstack_free(&asm_obst, NULL);
6014 static void global_asm_to_firm(statement_t *s)
6016 for (; s != NULL; s = s->base.next) {
6017 assert(s->kind == STATEMENT_ASM);
6019 char const *const text = s->asms.asm_text.begin;
6020 size_t size = s->asms.asm_text.size;
6022 /* skip the last \0 */
6023 if (text[size - 1] == '\0')
6026 ident *const id = new_id_from_chars(text, size);
6031 void translation_unit_to_firm(translation_unit_t *unit)
6033 /* initialize firm arithmetic */
6034 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6035 ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6037 /* just to be sure */
6038 continue_label = NULL;
6040 current_switch_cond = NULL;
6041 current_translation_unit = unit;
6045 scope_to_firm(&unit->scope);
6046 global_asm_to_firm(unit->global_asm);
6048 current_ir_graph = NULL;
6049 current_translation_unit = NULL;