2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include <libfirm/firm.h>
28 #include <libfirm/adt/obst.h>
29 #include <libfirm/be.h>
33 #include "adt/error.h"
34 #include "adt/array.h"
42 #include "diagnostic.h"
43 #include "lang_features.h"
45 #include "type_hash.h"
47 #include "walk_statements.h"
50 #include "entitymap_t.h"
51 #include "driver/firm_opt.h"
52 #include "driver/firm_cmdline.h"
54 typedef struct trampoline_region trampoline_region;
55 struct trampoline_region {
56 ir_entity *function; /**< The function that is called by this trampoline */
57 ir_entity *region; /**< created region for the trampoline */
60 static const backend_params *be_params;
62 static ir_type *ir_type_char;
63 static ir_type *ir_type_const_char;
64 static ir_type *ir_type_wchar_t;
65 static ir_type *ir_type_void;
66 static ir_type *ir_type_int;
68 /* architecture specific floating point arithmetic mode (if any) */
69 static ir_mode *mode_float_arithmetic;
71 /* alignment of stack parameters */
72 static unsigned stack_param_align;
74 static int next_value_number_function;
75 static ir_node *continue_label;
76 static ir_node *break_label;
77 static ir_node *current_switch_cond;
78 static bool saw_default_label;
79 static label_t **all_labels;
80 static entity_t **inner_functions;
81 static ir_node *ijmp_list;
82 static bool constant_folding;
83 static bool initializer_use_bitfield_basetype;
85 static const entity_t *current_function_entity;
86 static ir_node *current_function_name;
87 static ir_node *current_funcsig;
88 static switch_statement_t *current_switch;
89 static ir_graph *current_function;
90 static translation_unit_t *current_translation_unit;
91 static trampoline_region *current_trampolines;
92 static ir_type *current_outer_frame;
93 static ir_type *current_outer_value_type;
94 static ir_node *current_static_link;
96 static entitymap_t entitymap;
98 static struct obstack asm_obst;
100 typedef enum declaration_kind_t {
101 DECLARATION_KIND_UNKNOWN,
102 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
103 DECLARATION_KIND_GLOBAL_VARIABLE,
104 DECLARATION_KIND_LOCAL_VARIABLE,
105 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
106 DECLARATION_KIND_PARAMETER,
107 DECLARATION_KIND_PARAMETER_ENTITY,
108 DECLARATION_KIND_FUNCTION,
109 DECLARATION_KIND_COMPOUND_MEMBER,
110 DECLARATION_KIND_INNER_FUNCTION
111 } declaration_kind_t;
113 static ir_mode *get_ir_mode_storage(type_t *type);
115 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
116 * int that it returns bigger modes for floating point on some platforms
117 * (x87 internally does arithemtic with 80bits)
119 static ir_mode *get_ir_mode_arithmetic(type_t *type);
121 static ir_type *get_ir_type_incomplete(type_t *type);
123 static void enqueue_inner_function(entity_t *entity)
125 if (inner_functions == NULL)
126 inner_functions = NEW_ARR_F(entity_t *, 0);
127 ARR_APP1(entity_t*, inner_functions, entity);
130 static ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
132 const entity_t *entity = get_irg_loc_description(irg, pos);
134 if (entity != NULL && warning.uninitialized) {
135 warningf(&entity->base.source_position,
136 "%s '%#T' might be used uninitialized",
137 get_entity_kind_name(entity->kind),
138 entity->declaration.type, entity->base.symbol);
140 return new_r_Unknown(irg, mode);
143 static const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
145 const source_position_t *pos = (const source_position_t*) dbg;
150 return pos->input_name;
153 static dbg_info *get_dbg_info(const source_position_t *pos)
155 return (dbg_info*) pos;
158 static void dbg_print_type_dbg_info(char *buffer, size_t buffer_size,
159 const type_dbg_info *dbg)
162 print_to_buffer(buffer, buffer_size);
163 const type_t *type = (const type_t*) dbg;
165 finish_print_to_buffer();
168 static type_dbg_info *get_type_dbg_info_(const type_t *type)
170 return (type_dbg_info*) type;
173 /* is the current block a reachable one? */
174 static bool currently_reachable()
176 ir_node *block = get_cur_block();
177 if (block == NULL || is_Bad(block))
183 static void set_unreachable_now()
185 ir_node *bad = new_Bad(mode_BB);
189 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
191 static ir_mode *mode_int, *mode_uint;
193 static ir_node *_expression_to_firm(const expression_t *expression);
194 static ir_node *expression_to_firm(const expression_t *expression);
195 static void create_local_declaration(entity_t *entity);
197 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
199 unsigned flags = get_atomic_type_flags(kind);
200 unsigned size = get_atomic_type_size(kind);
201 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
202 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
205 unsigned bit_size = size * 8;
206 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
207 unsigned modulo_shift;
208 ir_mode_arithmetic arithmetic;
210 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
211 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
212 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
214 sort = irms_int_number;
215 arithmetic = irma_twos_complement;
216 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
218 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
219 snprintf(name, sizeof(name), "F%u", bit_size);
220 sort = irms_float_number;
221 arithmetic = irma_ieee754;
224 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
232 * Initialises the atomic modes depending on the machine size.
234 static void init_atomic_modes(void)
236 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
237 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
239 mode_int = atomic_modes[ATOMIC_TYPE_INT];
240 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
242 /* there's no real void type in firm */
243 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
245 /* initialize pointer modes */
247 ir_mode_sort sort = irms_reference;
248 unsigned bit_size = machine_size;
250 ir_mode_arithmetic arithmetic = irma_twos_complement;
251 unsigned modulo_shift
252 = bit_size < machine_size ? machine_size : bit_size;
254 snprintf(name, sizeof(name), "p%u", machine_size);
255 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
258 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
259 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
261 /* Hmm, pointers should be machine size */
262 set_modeP_data(ptr_mode);
263 set_modeP_code(ptr_mode);
266 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
268 assert(kind <= ATOMIC_TYPE_LAST);
269 return atomic_modes[kind];
272 static ir_node *get_vla_size(array_type_t *const type)
274 ir_node *size_node = type->size_node;
275 if (size_node == NULL) {
276 size_node = expression_to_firm(type->size_expression);
277 type->size_node = size_node;
283 * Return a node representing the size of a type.
285 static ir_node *get_type_size_node(type_t *type)
287 type = skip_typeref(type);
289 if (is_type_array(type) && type->array.is_vla) {
290 ir_node *size_node = get_vla_size(&type->array);
291 ir_node *elem_size = get_type_size_node(type->array.element_type);
292 ir_mode *mode = get_irn_mode(size_node);
293 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
297 ir_mode *mode = get_ir_mode_storage(type_size_t);
299 sym.type_p = get_ir_type(type);
300 return new_SymConst(mode, sym, symconst_type_size);
303 static unsigned count_parameters(const function_type_t *function_type)
307 function_parameter_t *parameter = function_type->parameters;
308 for ( ; parameter != NULL; parameter = parameter->next) {
316 * Creates a Firm type for an atomic type
318 static ir_type *create_atomic_type(atomic_type_kind_t akind, const type_t *type)
320 ir_mode *mode = atomic_modes[akind];
321 type_dbg_info *dbgi = get_type_dbg_info_(type);
322 ir_type *irtype = new_d_type_primitive(mode, dbgi);
323 il_alignment_t alignment = get_atomic_type_alignment(akind);
325 set_type_alignment_bytes(irtype, alignment);
331 * Creates a Firm type for a complex type
333 static ir_type *create_complex_type(const complex_type_t *type)
335 atomic_type_kind_t kind = type->akind;
336 ir_mode *mode = atomic_modes[kind];
337 ident *id = get_mode_ident(mode);
341 /* FIXME: finish the array */
346 * Creates a Firm type for an imaginary type
348 static ir_type *create_imaginary_type(imaginary_type_t *type)
350 return create_atomic_type(type->akind, (const type_t*) type);
354 * return type of a parameter (and take transparent union gnu extension into
357 static type_t *get_parameter_type(type_t *orig_type)
359 type_t *type = skip_typeref(orig_type);
360 if (is_type_union(type)
361 && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
362 compound_t *compound = type->compound.compound;
363 type = compound->members.entities->declaration.type;
369 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
371 type_t *return_type = skip_typeref(function_type->return_type);
373 int n_parameters = count_parameters(function_type)
374 + (for_closure ? 1 : 0);
375 int n_results = return_type == type_void ? 0 : 1;
376 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) function_type);
377 ir_type *irtype = new_d_type_method(n_parameters, n_results, dbgi);
379 if (return_type != type_void) {
380 ir_type *restype = get_ir_type(return_type);
381 set_method_res_type(irtype, 0, restype);
384 function_parameter_t *parameter = function_type->parameters;
387 ir_type *p_irtype = get_ir_type(type_void_ptr);
388 set_method_param_type(irtype, n, p_irtype);
391 for ( ; parameter != NULL; parameter = parameter->next) {
392 type_t *type = get_parameter_type(parameter->type);
393 ir_type *p_irtype = get_ir_type(type);
394 set_method_param_type(irtype, n, p_irtype);
398 bool is_variadic = function_type->variadic;
401 set_method_variadicity(irtype, variadicity_variadic);
403 unsigned cc = get_method_calling_convention(irtype);
404 switch (function_type->calling_convention) {
405 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
408 set_method_calling_convention(irtype, SET_CDECL(cc));
415 /* only non-variadic function can use stdcall, else use cdecl */
416 set_method_calling_convention(irtype, SET_STDCALL(cc));
422 /* only non-variadic function can use fastcall, else use cdecl */
423 set_method_calling_convention(irtype, SET_FASTCALL(cc));
427 /* Hmm, leave default, not accepted by the parser yet. */
432 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
437 static ir_type *create_pointer_type(pointer_type_t *type)
439 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
440 type_t *points_to = type->points_to;
441 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
442 ir_type *ir_type = new_d_type_pointer(ir_points_to, dbgi);
447 static ir_type *create_reference_type(reference_type_t *type)
449 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
450 type_t *refers_to = type->refers_to;
451 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
452 ir_type *ir_type = new_d_type_pointer(ir_refers_to, dbgi);
457 static ir_type *create_array_type(array_type_t *type)
459 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
460 type_t *element_type = type->element_type;
461 ir_type *ir_element_type = get_ir_type(element_type);
462 ir_type *ir_type = new_d_type_array(1, ir_element_type, dbgi);
464 const int align = get_type_alignment_bytes(ir_element_type);
465 set_type_alignment_bytes(ir_type, align);
467 if (type->size_constant) {
468 int n_elements = type->size;
470 set_array_bounds_int(ir_type, 0, 0, n_elements);
472 size_t elemsize = get_type_size_bytes(ir_element_type);
473 if (elemsize % align > 0) {
474 elemsize += align - (elemsize % align);
476 set_type_size_bytes(ir_type, n_elements * elemsize);
478 set_array_lower_bound_int(ir_type, 0, 0);
480 set_type_state(ir_type, layout_fixed);
486 * Return the signed integer type of size bits.
488 * @param size the size
490 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
494 static ir_mode *s_modes[64 + 1] = {NULL, };
498 if (size <= 0 || size > 64)
501 mode = s_modes[size];
505 snprintf(name, sizeof(name), "bf_I%u", size);
506 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
507 size <= 32 ? 32 : size );
508 s_modes[size] = mode;
511 type_dbg_info *dbgi = get_type_dbg_info_(type);
512 res = new_d_type_primitive(mode, dbgi);
513 set_primitive_base_type(res, base_tp);
519 * Return the unsigned integer type of size bits.
521 * @param size the size
523 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
527 static ir_mode *u_modes[64 + 1] = {NULL, };
531 if (size <= 0 || size > 64)
534 mode = u_modes[size];
538 snprintf(name, sizeof(name), "bf_U%u", size);
539 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
540 size <= 32 ? 32 : size );
541 u_modes[size] = mode;
544 type_dbg_info *dbgi = get_type_dbg_info_(type);
545 res = new_d_type_primitive(mode, dbgi);
546 set_primitive_base_type(res, base_tp);
551 static ir_type *create_bitfield_type(bitfield_type_t *const type)
553 type_t *base = skip_typeref(type->base_type);
554 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
555 ir_type *irbase = get_ir_type(base);
557 unsigned size = type->bit_size;
559 assert(!is_type_float(base));
560 if (is_type_signed(base)) {
561 return get_signed_int_type_for_bit_size(irbase, size,
562 (const type_t*) type);
564 return get_unsigned_int_type_for_bit_size(irbase, size,
565 (const type_t*) type);
569 #define INVALID_TYPE ((ir_type_ptr)-1)
572 COMPOUND_IS_STRUCT = false,
573 COMPOUND_IS_UNION = true
577 * Construct firm type from ast struct type.
579 static ir_type *create_compound_type(compound_type_t *type,
580 bool incomplete, bool is_union)
582 compound_t *compound = type->compound;
584 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
585 return compound->irtype;
588 symbol_t *symbol = compound->base.symbol;
590 if (symbol != NULL) {
591 id = new_id_from_str(symbol->string);
594 id = id_unique("__anonymous_union.%u");
596 id = id_unique("__anonymous_struct.%u");
602 irtype = new_type_union(id);
604 irtype = new_type_struct(id);
607 compound->irtype_complete = false;
608 compound->irtype = irtype;
614 layout_union_type(type);
616 layout_struct_type(type);
619 compound->irtype_complete = true;
621 entity_t *entry = compound->members.entities;
622 for ( ; entry != NULL; entry = entry->base.next) {
623 if (entry->kind != ENTITY_COMPOUND_MEMBER)
626 symbol_t *symbol = entry->base.symbol;
627 type_t *entry_type = entry->declaration.type;
629 if (symbol == NULL) {
630 /* anonymous bitfield member, skip */
631 if (entry_type->kind == TYPE_BITFIELD)
633 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
634 || entry_type->kind == TYPE_COMPOUND_UNION);
635 ident = id_unique("anon.%u");
637 ident = new_id_from_str(symbol->string);
640 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
642 ir_type *entry_irtype = get_ir_type(entry_type);
643 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
645 set_entity_offset(entity, entry->compound_member.offset);
646 set_entity_offset_bits_remainder(entity,
647 entry->compound_member.bit_offset);
649 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
650 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
651 entry->compound_member.entity = entity;
654 set_type_alignment_bytes(irtype, compound->alignment);
655 set_type_size_bytes(irtype, compound->size);
656 set_type_state(irtype, layout_fixed);
661 static ir_type *create_enum_type(enum_type_t *const type)
663 type->base.firm_type = ir_type_int;
665 ir_mode *const mode = mode_int;
666 ir_tarval *const one = get_mode_one(mode);
667 ir_tarval * tv_next = get_mode_null(mode);
669 bool constant_folding_old = constant_folding;
670 constant_folding = true;
672 enum_t *enume = type->enume;
673 entity_t *entry = enume->base.next;
674 for (; entry != NULL; entry = entry->base.next) {
675 if (entry->kind != ENTITY_ENUM_VALUE)
678 expression_t *const init = entry->enum_value.value;
680 ir_node *const cnst = expression_to_firm(init);
681 if (!is_Const(cnst)) {
682 panic("couldn't fold constant");
684 tv_next = get_Const_tarval(cnst);
686 entry->enum_value.tv = tv_next;
687 tv_next = tarval_add(tv_next, one);
690 constant_folding = constant_folding_old;
692 return create_atomic_type(type->akind, (const type_t*) type);
695 static ir_type *get_ir_type_incomplete(type_t *type)
697 assert(type != NULL);
698 type = skip_typeref(type);
700 if (type->base.firm_type != NULL) {
701 assert(type->base.firm_type != INVALID_TYPE);
702 return type->base.firm_type;
705 switch (type->kind) {
706 case TYPE_COMPOUND_STRUCT:
707 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
708 case TYPE_COMPOUND_UNION:
709 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
711 return get_ir_type(type);
715 ir_type *get_ir_type(type_t *type)
717 assert(type != NULL);
719 type = skip_typeref(type);
721 if (type->base.firm_type != NULL) {
722 assert(type->base.firm_type != INVALID_TYPE);
723 return type->base.firm_type;
726 ir_type *firm_type = NULL;
727 switch (type->kind) {
729 /* Happens while constant folding, when there was an error */
730 return create_atomic_type(ATOMIC_TYPE_VOID, NULL);
733 firm_type = create_atomic_type(type->atomic.akind, type);
736 firm_type = create_complex_type(&type->complex);
739 firm_type = create_imaginary_type(&type->imaginary);
742 firm_type = create_method_type(&type->function, false);
745 firm_type = create_pointer_type(&type->pointer);
748 firm_type = create_reference_type(&type->reference);
751 firm_type = create_array_type(&type->array);
753 case TYPE_COMPOUND_STRUCT:
754 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
756 case TYPE_COMPOUND_UNION:
757 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
760 firm_type = create_enum_type(&type->enumt);
763 firm_type = create_bitfield_type(&type->bitfield);
771 if (firm_type == NULL)
772 panic("unknown type found");
774 type->base.firm_type = firm_type;
778 static ir_mode *get_ir_mode_storage(type_t *type)
780 ir_type *irtype = get_ir_type(type);
782 /* firm doesn't report a mode for arrays somehow... */
783 if (is_Array_type(irtype)) {
787 ir_mode *mode = get_type_mode(irtype);
788 assert(mode != NULL);
792 static ir_mode *get_ir_mode_arithmetic(type_t *type)
794 ir_mode *mode = get_ir_mode_storage(type);
795 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
796 return mode_float_arithmetic;
802 /** Names of the runtime functions. */
803 static const struct {
804 int id; /**< the rts id */
805 int n_res; /**< number of return values */
806 const char *name; /**< the name of the rts function */
807 int n_params; /**< number of parameters */
808 unsigned flags; /**< language flags */
810 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
811 { rts_abort, 0, "abort", 0, _C89 },
812 { rts_alloca, 1, "alloca", 1, _ALL },
813 { rts_abs, 1, "abs", 1, _C89 },
814 { rts_labs, 1, "labs", 1, _C89 },
815 { rts_llabs, 1, "llabs", 1, _C99 },
816 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
818 { rts_fabs, 1, "fabs", 1, _C89 },
819 { rts_sqrt, 1, "sqrt", 1, _C89 },
820 { rts_cbrt, 1, "cbrt", 1, _C99 },
821 { rts_exp, 1, "exp", 1, _C89 },
822 { rts_exp2, 1, "exp2", 1, _C89 },
823 { rts_exp10, 1, "exp10", 1, _GNUC },
824 { rts_log, 1, "log", 1, _C89 },
825 { rts_log2, 1, "log2", 1, _C89 },
826 { rts_log10, 1, "log10", 1, _C89 },
827 { rts_pow, 1, "pow", 2, _C89 },
828 { rts_sin, 1, "sin", 1, _C89 },
829 { rts_cos, 1, "cos", 1, _C89 },
830 { rts_tan, 1, "tan", 1, _C89 },
831 { rts_asin, 1, "asin", 1, _C89 },
832 { rts_acos, 1, "acos", 1, _C89 },
833 { rts_atan, 1, "atan", 1, _C89 },
834 { rts_sinh, 1, "sinh", 1, _C89 },
835 { rts_cosh, 1, "cosh", 1, _C89 },
836 { rts_tanh, 1, "tanh", 1, _C89 },
838 { rts_fabsf, 1, "fabsf", 1, _C99 },
839 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
840 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
841 { rts_expf, 1, "expf", 1, _C99 },
842 { rts_exp2f, 1, "exp2f", 1, _C99 },
843 { rts_exp10f, 1, "exp10f", 1, _GNUC },
844 { rts_logf, 1, "logf", 1, _C99 },
845 { rts_log2f, 1, "log2f", 1, _C99 },
846 { rts_log10f, 1, "log10f", 1, _C99 },
847 { rts_powf, 1, "powf", 2, _C99 },
848 { rts_sinf, 1, "sinf", 1, _C99 },
849 { rts_cosf, 1, "cosf", 1, _C99 },
850 { rts_tanf, 1, "tanf", 1, _C99 },
851 { rts_asinf, 1, "asinf", 1, _C99 },
852 { rts_acosf, 1, "acosf", 1, _C99 },
853 { rts_atanf, 1, "atanf", 1, _C99 },
854 { rts_sinhf, 1, "sinhf", 1, _C99 },
855 { rts_coshf, 1, "coshf", 1, _C99 },
856 { rts_tanhf, 1, "tanhf", 1, _C99 },
858 { rts_fabsl, 1, "fabsl", 1, _C99 },
859 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
860 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
861 { rts_expl, 1, "expl", 1, _C99 },
862 { rts_exp2l, 1, "exp2l", 1, _C99 },
863 { rts_exp10l, 1, "exp10l", 1, _GNUC },
864 { rts_logl, 1, "logl", 1, _C99 },
865 { rts_log2l, 1, "log2l", 1, _C99 },
866 { rts_log10l, 1, "log10l", 1, _C99 },
867 { rts_powl, 1, "powl", 2, _C99 },
868 { rts_sinl, 1, "sinl", 1, _C99 },
869 { rts_cosl, 1, "cosl", 1, _C99 },
870 { rts_tanl, 1, "tanl", 1, _C99 },
871 { rts_asinl, 1, "asinl", 1, _C99 },
872 { rts_acosl, 1, "acosl", 1, _C99 },
873 { rts_atanl, 1, "atanl", 1, _C99 },
874 { rts_sinhl, 1, "sinhl", 1, _C99 },
875 { rts_coshl, 1, "coshl", 1, _C99 },
876 { rts_tanhl, 1, "tanhl", 1, _C99 },
878 { rts_strcmp, 1, "strcmp", 2, _C89 },
879 { rts_strncmp, 1, "strncmp", 3, _C89 },
880 { rts_strcpy, 1, "strcpy", 2, _C89 },
881 { rts_strlen, 1, "strlen", 1, _C89 },
882 { rts_memcpy, 1, "memcpy", 3, _C89 },
883 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
884 { rts_memmove, 1, "memmove", 3, _C89 },
885 { rts_memset, 1, "memset", 3, _C89 },
886 { rts_memcmp, 1, "memcmp", 3, _C89 },
889 static ident *rts_idents[lengthof(rts_data)];
891 static create_ld_ident_func create_ld_ident = create_name_linux_elf;
893 void set_create_ld_ident(ident *(*func)(entity_t*))
895 create_ld_ident = func;
899 * Handle GNU attributes for entities
901 * @param ent the entity
902 * @param decl the routine declaration
904 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
906 assert(is_declaration(entity));
907 decl_modifiers_t modifiers = entity->declaration.modifiers;
909 if (is_method_entity(irentity)) {
910 if (modifiers & DM_PURE) {
911 set_entity_additional_properties(irentity, mtp_property_pure);
913 if (modifiers & DM_CONST) {
914 add_entity_additional_properties(irentity, mtp_property_const);
917 if (modifiers & DM_USED) {
918 add_entity_linkage(irentity, IR_LINKAGE_HIDDEN_USER);
920 if (modifiers & DM_WEAK) {
921 add_entity_linkage(irentity, IR_LINKAGE_WEAK);
925 static bool is_main(entity_t *entity)
927 static symbol_t *sym_main = NULL;
928 if (sym_main == NULL) {
929 sym_main = symbol_table_insert("main");
932 if (entity->base.symbol != sym_main)
934 /* must be in outermost scope */
935 if (entity->base.parent_scope != ¤t_translation_unit->scope)
942 * Creates an entity representing a function.
944 * @param entity the function declaration/definition
945 * @param owner_type the owner type of this function, NULL
946 * for global functions
948 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
950 assert(entity->kind == ENTITY_FUNCTION);
951 if (entity->function.irentity != NULL) {
952 return entity->function.irentity;
955 entity_t *original_entity = entity;
956 if (entity->function.btk != bk_none) {
957 entity = get_builtin_replacement(entity);
962 if (is_main(entity)) {
963 /* force main to C linkage */
964 type_t *type = entity->declaration.type;
965 assert(is_type_function(type));
966 if (type->function.linkage != LINKAGE_C) {
967 type_t *new_type = duplicate_type(type);
968 new_type->function.linkage = LINKAGE_C;
969 type = identify_new_type(new_type);
970 entity->declaration.type = type;
974 symbol_t *symbol = entity->base.symbol;
975 ident *id = new_id_from_str(symbol->string);
977 /* already an entity defined? */
978 ir_entity *irentity = entitymap_get(&entitymap, symbol);
979 bool const has_body = entity->function.statement != NULL;
980 if (irentity != NULL) {
981 if (get_entity_visibility(irentity) == ir_visibility_external
983 set_entity_visibility(irentity, ir_visibility_default);
988 ir_type *ir_type_method;
989 if (entity->function.need_closure)
990 ir_type_method = create_method_type(&entity->declaration.type->function, true);
992 ir_type_method = get_ir_type(entity->declaration.type);
994 bool nested_function = false;
995 if (owner_type == NULL)
996 owner_type = get_glob_type();
998 nested_function = true;
1000 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
1001 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
1004 if (nested_function)
1005 ld_id = id_unique("inner.%u");
1007 ld_id = create_ld_ident(entity);
1008 set_entity_ld_ident(irentity, ld_id);
1010 handle_decl_modifiers(irentity, entity);
1012 if (! nested_function) {
1013 /* static inline => local
1014 * extern inline => local
1015 * inline without definition => local
1016 * inline with definition => external_visible */
1017 storage_class_tag_t const storage_class
1018 = (storage_class_tag_t) entity->declaration.storage_class;
1019 bool const is_inline = entity->function.is_inline;
1021 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1022 set_entity_visibility(irentity, ir_visibility_default);
1023 } else if (storage_class == STORAGE_CLASS_STATIC ||
1024 (is_inline && has_body)) {
1025 set_entity_visibility(irentity, ir_visibility_local);
1026 } else if (has_body) {
1027 set_entity_visibility(irentity, ir_visibility_default);
1029 set_entity_visibility(irentity, ir_visibility_external);
1032 /* nested functions are always local */
1033 set_entity_visibility(irentity, ir_visibility_local);
1036 /* We should check for file scope here, but as long as we compile C only
1037 this is not needed. */
1038 if (!freestanding && !has_body) {
1039 /* check for a known runtime function */
1040 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1041 if (id != rts_idents[i])
1044 function_type_t *function_type
1045 = &entity->declaration.type->function;
1046 /* rts_entities code can't handle a "wrong" number of parameters */
1047 if (function_type->unspecified_parameters)
1050 /* check number of parameters */
1051 int n_params = count_parameters(function_type);
1052 if (n_params != rts_data[i].n_params)
1055 type_t *return_type = skip_typeref(function_type->return_type);
1056 int n_res = return_type != type_void ? 1 : 0;
1057 if (n_res != rts_data[i].n_res)
1060 /* ignore those rts functions not necessary needed for current mode */
1061 if ((c_mode & rts_data[i].flags) == 0)
1063 assert(rts_entities[rts_data[i].id] == NULL);
1064 rts_entities[rts_data[i].id] = irentity;
1068 entitymap_insert(&entitymap, symbol, irentity);
1071 original_entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1072 original_entity->function.irentity = irentity;
1078 * Creates a SymConst for a given entity.
1080 * @param dbgi debug info
1081 * @param entity the entity
1083 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
1085 assert(entity != NULL);
1086 union symconst_symbol sym;
1087 sym.entity_p = entity;
1088 return new_d_SymConst(dbgi, mode_P, sym, symconst_addr_ent);
1091 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1093 ir_mode *value_mode = get_irn_mode(value);
1095 if (value_mode == dest_mode)
1098 if (dest_mode == mode_b) {
1099 ir_node *zero = new_Const(get_mode_null(value_mode));
1100 ir_node *cmp = new_d_Cmp(dbgi, value, zero, ir_relation_less_greater);
1104 return new_d_Conv(dbgi, value, dest_mode);
1107 static ir_node *create_Const_from_bool(ir_mode *const mode, bool const v)
1109 return new_Const((v ? get_mode_one : get_mode_null)(mode));
1113 * Creates a SymConst node representing a wide string literal.
1115 * @param literal the wide string literal
1117 static ir_node *wide_string_literal_to_firm(
1118 const string_literal_expression_t *literal)
1120 ir_type *const global_type = get_glob_type();
1121 ir_type *const elem_type = ir_type_wchar_t;
1122 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1123 ir_type *const type = new_type_array(1, elem_type);
1125 ident *const id = id_unique("str.%u");
1126 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1127 set_entity_ld_ident(entity, id);
1128 set_entity_visibility(entity, ir_visibility_private);
1129 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1131 ir_mode *const mode = get_type_mode(elem_type);
1132 const size_t slen = wstrlen(&literal->value);
1134 set_array_lower_bound_int(type, 0, 0);
1135 set_array_upper_bound_int(type, 0, slen);
1136 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1137 set_type_state(type, layout_fixed);
1139 ir_initializer_t *initializer = create_initializer_compound(slen);
1140 const char *p = literal->value.begin;
1141 for (size_t i = 0; i < slen; ++i) {
1142 assert(p < literal->value.begin + literal->value.size);
1143 utf32 v = read_utf8_char(&p);
1144 ir_tarval *tv = new_tarval_from_long(v, mode);
1145 ir_initializer_t *val = create_initializer_tarval(tv);
1146 set_initializer_compound_value(initializer, i, val);
1148 set_entity_initializer(entity, initializer);
1150 return create_symconst(dbgi, entity);
1154 * Creates a SymConst node representing a string constant.
1156 * @param src_pos the source position of the string constant
1157 * @param id_prefix a prefix for the name of the generated string constant
1158 * @param value the value of the string constant
1160 static ir_node *string_to_firm(const source_position_t *const src_pos,
1161 const char *const id_prefix,
1162 const string_t *const value)
1164 ir_type *const global_type = get_glob_type();
1165 dbg_info *const dbgi = get_dbg_info(src_pos);
1166 ir_type *const type = new_type_array(1, ir_type_const_char);
1168 ident *const id = id_unique(id_prefix);
1169 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1170 set_entity_ld_ident(entity, id);
1171 set_entity_visibility(entity, ir_visibility_private);
1172 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1174 ir_type *const elem_type = ir_type_const_char;
1175 ir_mode *const mode = get_type_mode(elem_type);
1177 const char* const string = value->begin;
1178 const size_t slen = value->size;
1180 set_array_lower_bound_int(type, 0, 0);
1181 set_array_upper_bound_int(type, 0, slen);
1182 set_type_size_bytes(type, slen);
1183 set_type_state(type, layout_fixed);
1185 ir_initializer_t *initializer = create_initializer_compound(slen);
1186 for (size_t i = 0; i < slen; ++i) {
1187 ir_tarval *tv = new_tarval_from_long(string[i], mode);
1188 ir_initializer_t *val = create_initializer_tarval(tv);
1189 set_initializer_compound_value(initializer, i, val);
1191 set_entity_initializer(entity, initializer);
1193 return create_symconst(dbgi, entity);
1196 static bool try_create_integer(literal_expression_t *literal,
1197 type_t *type, unsigned char base)
1199 const char *string = literal->value.begin;
1200 size_t size = literal->value.size;
1202 assert(type->kind == TYPE_ATOMIC);
1203 atomic_type_kind_t akind = type->atomic.akind;
1205 ir_mode *mode = atomic_modes[akind];
1206 ir_tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1207 if (tv == tarval_bad)
1210 literal->base.type = type;
1211 literal->target_value = tv;
1215 static void create_integer_tarval(literal_expression_t *literal)
1219 symbol_t *suffix = literal->suffix;
1221 if (suffix != NULL) {
1222 for (const char *c = suffix->string; *c != '\0'; ++c) {
1223 if (*c == 'u' || *c == 'U') { ++us; }
1224 if (*c == 'l' || *c == 'L') { ++ls; }
1229 switch (literal->base.kind) {
1230 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1231 case EXPR_LITERAL_INTEGER: base = 10; break;
1232 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1233 default: panic("invalid literal kind");
1236 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1238 /* now try if the constant is small enough for some types */
1239 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1241 if (us == 0 && try_create_integer(literal, type_int, base))
1243 if ((us == 1 || base != 10)
1244 && try_create_integer(literal, type_unsigned_int, base))
1248 if (us == 0 && try_create_integer(literal, type_long, base))
1250 if ((us == 1 || base != 10)
1251 && try_create_integer(literal, type_unsigned_long, base))
1254 /* last try? then we should not report tarval_bad */
1255 if (us != 1 && base == 10)
1256 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1257 if (us == 0 && try_create_integer(literal, type_long_long, base))
1261 assert(us == 1 || base != 10);
1262 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1263 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1265 panic("internal error when parsing number literal");
1268 tarval_set_integer_overflow_mode(old_mode);
1271 void determine_literal_type(literal_expression_t *literal)
1273 switch (literal->base.kind) {
1274 case EXPR_LITERAL_INTEGER:
1275 case EXPR_LITERAL_INTEGER_OCTAL:
1276 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1277 create_integer_tarval(literal);
1285 * Creates a Const node representing a constant.
1287 static ir_node *literal_to_firm(const literal_expression_t *literal)
1289 type_t *type = skip_typeref(literal->base.type);
1290 ir_mode *mode = get_ir_mode_storage(type);
1291 const char *string = literal->value.begin;
1292 size_t size = literal->value.size;
1295 switch (literal->base.kind) {
1296 case EXPR_LITERAL_WIDE_CHARACTER: {
1297 utf32 v = read_utf8_char(&string);
1299 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1301 tv = new_tarval_from_str(buf, len, mode);
1304 case EXPR_LITERAL_CHARACTER: {
1306 if (size == 1 && char_is_signed) {
1307 v = (signed char)string[0];
1310 for (size_t i = 0; i < size; ++i) {
1311 v = (v << 8) | ((unsigned char)string[i]);
1315 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1317 tv = new_tarval_from_str(buf, len, mode);
1320 case EXPR_LITERAL_INTEGER:
1321 case EXPR_LITERAL_INTEGER_OCTAL:
1322 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1323 assert(literal->target_value != NULL);
1324 tv = literal->target_value;
1326 case EXPR_LITERAL_FLOATINGPOINT:
1327 tv = new_tarval_from_str(string, size, mode);
1329 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1330 char buffer[size + 2];
1331 memcpy(buffer, "0x", 2);
1332 memcpy(buffer+2, string, size);
1333 tv = new_tarval_from_str(buffer, size+2, mode);
1336 case EXPR_LITERAL_BOOLEAN:
1337 if (string[0] == 't') {
1338 tv = get_mode_one(mode);
1340 assert(string[0] == 'f');
1341 tv = get_mode_null(mode);
1344 case EXPR_LITERAL_MS_NOOP:
1345 tv = get_mode_null(mode);
1350 panic("Invalid literal kind found");
1353 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1354 ir_node *res = new_d_Const(dbgi, tv);
1355 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1356 return create_conv(dbgi, res, mode_arith);
1360 * Allocate an area of size bytes aligned at alignment
1363 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1365 static unsigned area_cnt = 0;
1368 ir_type *tp = new_type_array(1, ir_type_char);
1369 set_array_bounds_int(tp, 0, 0, size);
1370 set_type_alignment_bytes(tp, alignment);
1372 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1373 ident *name = new_id_from_str(buf);
1374 ir_entity *area = new_entity(frame_type, name, tp);
1376 /* mark this entity as compiler generated */
1377 set_entity_compiler_generated(area, 1);
1382 * Return a node representing a trampoline region
1383 * for a given function entity.
1385 * @param dbgi debug info
1386 * @param entity the function entity
1388 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1390 ir_entity *region = NULL;
1393 if (current_trampolines != NULL) {
1394 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1395 if (current_trampolines[i].function == entity) {
1396 region = current_trampolines[i].region;
1401 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1403 ir_graph *irg = current_ir_graph;
1404 if (region == NULL) {
1405 /* create a new region */
1406 ir_type *frame_tp = get_irg_frame_type(irg);
1407 trampoline_region reg;
1408 reg.function = entity;
1410 reg.region = alloc_trampoline(frame_tp,
1411 be_params->trampoline_size,
1412 be_params->trampoline_align);
1413 ARR_APP1(trampoline_region, current_trampolines, reg);
1414 region = reg.region;
1416 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1421 * Creates a trampoline for a function represented by an entity.
1423 * @param dbgi debug info
1424 * @param mode the (reference) mode for the function address
1425 * @param entity the function entity
1427 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1430 assert(entity != NULL);
1432 in[0] = get_trampoline_region(dbgi, entity);
1433 in[1] = create_symconst(dbgi, entity);
1434 in[2] = get_irg_frame(current_ir_graph);
1436 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1437 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1438 return new_Proj(irn, mode, pn_Builtin_1_result);
1442 * Dereference an address.
1444 * @param dbgi debug info
1445 * @param type the type of the dereferenced result (the points_to type)
1446 * @param addr the address to dereference
1448 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1449 ir_node *const addr)
1451 ir_type *irtype = get_ir_type(type);
1452 if (is_compound_type(irtype)
1453 || is_Method_type(irtype)
1454 || is_Array_type(irtype)) {
1458 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1459 ? cons_volatile : cons_none;
1460 ir_mode *const mode = get_type_mode(irtype);
1461 ir_node *const memory = get_store();
1462 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1463 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1464 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1466 set_store(load_mem);
1468 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1469 return create_conv(dbgi, load_res, mode_arithmetic);
1473 * Creates a strict Conv (to the node's mode) if necessary.
1475 * @param dbgi debug info
1476 * @param node the node to strict conv
1478 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1480 ir_mode *mode = get_irn_mode(node);
1482 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1484 if (!mode_is_float(mode))
1487 /* check if there is already a Conv */
1488 if (is_Conv(node)) {
1489 /* convert it into a strict Conv */
1490 set_Conv_strict(node, 1);
1494 /* otherwise create a new one */
1495 return new_d_strictConv(dbgi, node, mode);
1499 * Returns the correct base address depending on whether it is a parameter or a
1500 * normal local variable.
1502 static ir_node *get_local_frame(ir_entity *const ent)
1504 ir_graph *const irg = current_ir_graph;
1505 const ir_type *const owner = get_entity_owner(ent);
1506 if (owner == current_outer_frame || owner == current_outer_value_type) {
1507 assert(current_static_link != NULL);
1508 return current_static_link;
1510 return get_irg_frame(irg);
1515 * Keep all memory edges of the given block.
1517 static void keep_all_memory(ir_node *block)
1519 ir_node *old = get_cur_block();
1521 set_cur_block(block);
1522 keep_alive(get_store());
1523 /* TODO: keep all memory edges from restricted pointers */
1527 static ir_node *reference_expression_enum_value_to_firm(
1528 const reference_expression_t *ref)
1530 entity_t *entity = ref->entity;
1531 type_t *type = skip_typeref(entity->enum_value.enum_type);
1532 /* make sure the type is constructed */
1533 (void) get_ir_type(type);
1535 return new_Const(entity->enum_value.tv);
1538 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1540 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1541 entity_t *entity = ref->entity;
1542 assert(is_declaration(entity));
1543 type_t *type = skip_typeref(entity->declaration.type);
1545 /* make sure the type is constructed */
1546 (void) get_ir_type(type);
1548 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1549 ir_entity *irentity = get_function_entity(entity, NULL);
1550 /* for gcc compatibility we have to produce (dummy) addresses for some
1551 * builtins which don't have entities */
1552 if (irentity == NULL) {
1553 if (warning.other) {
1554 warningf(&ref->base.source_position,
1555 "taking address of builtin '%Y'",
1556 ref->entity->base.symbol);
1559 /* simply create a NULL pointer */
1560 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1561 ir_node *res = new_Const(get_mode_null(mode));
1567 switch ((declaration_kind_t) entity->declaration.kind) {
1568 case DECLARATION_KIND_UNKNOWN:
1571 case DECLARATION_KIND_LOCAL_VARIABLE: {
1572 ir_mode *const mode = get_ir_mode_storage(type);
1573 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1574 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1576 case DECLARATION_KIND_PARAMETER: {
1577 ir_mode *const mode = get_ir_mode_storage(type);
1578 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1579 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1581 case DECLARATION_KIND_FUNCTION: {
1582 return create_symconst(dbgi, entity->function.irentity);
1584 case DECLARATION_KIND_INNER_FUNCTION: {
1585 ir_mode *const mode = get_ir_mode_storage(type);
1586 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1587 /* inner function not using the closure */
1588 return create_symconst(dbgi, entity->function.irentity);
1590 /* need trampoline here */
1591 return create_trampoline(dbgi, mode, entity->function.irentity);
1594 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1595 const variable_t *variable = &entity->variable;
1596 ir_node *const addr = create_symconst(dbgi, variable->v.entity);
1597 return deref_address(dbgi, variable->base.type, addr);
1600 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1601 ir_entity *irentity = entity->variable.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);
1606 case DECLARATION_KIND_PARAMETER_ENTITY: {
1607 ir_entity *irentity = entity->parameter.v.entity;
1608 ir_node *frame = get_local_frame(irentity);
1609 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1610 return deref_address(dbgi, entity->declaration.type, sel);
1613 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1614 return entity->variable.v.vla_base;
1616 case DECLARATION_KIND_COMPOUND_MEMBER:
1617 panic("not implemented reference type");
1620 panic("reference to declaration with unknown type found");
1623 static ir_node *reference_addr(const reference_expression_t *ref)
1625 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1626 entity_t *entity = ref->entity;
1627 assert(is_declaration(entity));
1629 switch((declaration_kind_t) entity->declaration.kind) {
1630 case DECLARATION_KIND_UNKNOWN:
1632 case DECLARATION_KIND_PARAMETER:
1633 case DECLARATION_KIND_LOCAL_VARIABLE:
1634 /* you can store to a local variable (so we don't panic but return NULL
1635 * as an indicator for no real address) */
1637 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1638 ir_node *const addr = create_symconst(dbgi, entity->variable.v.entity);
1641 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1642 ir_entity *irentity = entity->variable.v.entity;
1643 ir_node *frame = get_local_frame(irentity);
1644 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1648 case DECLARATION_KIND_PARAMETER_ENTITY: {
1649 ir_entity *irentity = entity->parameter.v.entity;
1650 ir_node *frame = get_local_frame(irentity);
1651 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1656 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1657 return entity->variable.v.vla_base;
1659 case DECLARATION_KIND_FUNCTION: {
1660 return create_symconst(dbgi, entity->function.irentity);
1663 case DECLARATION_KIND_INNER_FUNCTION: {
1664 type_t *const type = skip_typeref(entity->declaration.type);
1665 ir_mode *const mode = get_ir_mode_storage(type);
1666 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1667 /* inner function not using the closure */
1668 return create_symconst(dbgi, entity->function.irentity);
1670 /* need trampoline here */
1671 return create_trampoline(dbgi, mode, entity->function.irentity);
1675 case DECLARATION_KIND_COMPOUND_MEMBER:
1676 panic("not implemented reference type");
1679 panic("reference to declaration with unknown type found");
1683 * Generate an unary builtin.
1685 * @param kind the builtin kind to generate
1686 * @param op the operand
1687 * @param function_type the function type for the GNU builtin routine
1688 * @param db debug info
1690 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1693 in[0] = expression_to_firm(op);
1695 ir_type *tp = get_ir_type(function_type);
1696 ir_type *res = get_method_res_type(tp, 0);
1697 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1698 set_irn_pinned(irn, op_pin_state_floats);
1699 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1703 * Generate a pinned unary builtin.
1705 * @param kind the builtin kind to generate
1706 * @param op the operand
1707 * @param function_type the function type for the GNU builtin routine
1708 * @param db debug info
1710 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1711 type_t *function_type, dbg_info *db)
1714 in[0] = expression_to_firm(op);
1716 ir_type *tp = get_ir_type(function_type);
1717 ir_type *res = get_method_res_type(tp, 0);
1718 ir_node *mem = get_store();
1719 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1720 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1721 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1725 * Generate an binary-void-return builtin.
1727 * @param kind the builtin kind to generate
1728 * @param op1 the first operand
1729 * @param op2 the second operand
1730 * @param function_type the function type for the GNU builtin routine
1731 * @param db debug info
1733 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1734 expression_t *op2, type_t *function_type,
1738 in[0] = expression_to_firm(op1);
1739 in[1] = expression_to_firm(op2);
1741 ir_type *tp = get_ir_type(function_type);
1742 ir_node *mem = get_store();
1743 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1744 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1749 * Transform calls to builtin functions.
1751 static ir_node *process_builtin_call(const call_expression_t *call)
1753 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1755 assert(call->function->kind == EXPR_REFERENCE);
1756 reference_expression_t *builtin = &call->function->reference;
1758 type_t *type = skip_typeref(builtin->base.type);
1759 assert(is_type_pointer(type));
1761 type_t *function_type = skip_typeref(type->pointer.points_to);
1763 switch (builtin->entity->function.btk) {
1764 case bk_gnu_builtin_alloca: {
1765 if (call->arguments == NULL || call->arguments->next != NULL) {
1766 panic("invalid number of parameters on __builtin_alloca");
1768 expression_t *argument = call->arguments->expression;
1769 ir_node *size = expression_to_firm(argument);
1771 ir_node *store = get_store();
1772 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1774 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1776 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1781 case bk_gnu_builtin_huge_val:
1782 case bk_gnu_builtin_huge_valf:
1783 case bk_gnu_builtin_huge_vall:
1784 case bk_gnu_builtin_inf:
1785 case bk_gnu_builtin_inff:
1786 case bk_gnu_builtin_infl: {
1787 type_t *type = function_type->function.return_type;
1788 ir_mode *mode = get_ir_mode_arithmetic(type);
1789 ir_tarval *tv = get_mode_infinite(mode);
1790 ir_node *res = new_d_Const(dbgi, tv);
1793 case bk_gnu_builtin_nan:
1794 case bk_gnu_builtin_nanf:
1795 case bk_gnu_builtin_nanl: {
1796 /* Ignore string for now... */
1797 assert(is_type_function(function_type));
1798 type_t *type = function_type->function.return_type;
1799 ir_mode *mode = get_ir_mode_arithmetic(type);
1800 ir_tarval *tv = get_mode_NAN(mode);
1801 ir_node *res = new_d_Const(dbgi, tv);
1804 case bk_gnu_builtin_expect: {
1805 expression_t *argument = call->arguments->expression;
1806 return _expression_to_firm(argument);
1808 case bk_gnu_builtin_va_end:
1809 /* evaluate the argument of va_end for its side effects */
1810 _expression_to_firm(call->arguments->expression);
1812 case bk_gnu_builtin_frame_address: {
1813 expression_t *const expression = call->arguments->expression;
1814 bool val = fold_constant_to_bool(expression);
1817 return get_irg_frame(current_ir_graph);
1819 /* get the argument */
1822 in[0] = expression_to_firm(expression);
1823 in[1] = get_irg_frame(current_ir_graph);
1824 ir_type *tp = get_ir_type(function_type);
1825 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1826 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1829 case bk_gnu_builtin_return_address: {
1830 expression_t *const expression = call->arguments->expression;
1833 in[0] = expression_to_firm(expression);
1834 in[1] = get_irg_frame(current_ir_graph);
1835 ir_type *tp = get_ir_type(function_type);
1836 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1837 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1839 case bk_gnu_builtin_ffs:
1840 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1841 case bk_gnu_builtin_clz:
1842 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1843 case bk_gnu_builtin_ctz:
1844 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1845 case bk_gnu_builtin_popcount:
1846 case bk_ms__popcount:
1847 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1848 case bk_gnu_builtin_parity:
1849 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1850 case bk_gnu_builtin_prefetch: {
1851 call_argument_t *const args = call->arguments;
1852 expression_t *const addr = args->expression;
1855 in[0] = _expression_to_firm(addr);
1856 if (args->next != NULL) {
1857 expression_t *const rw = args->next->expression;
1859 in[1] = _expression_to_firm(rw);
1861 if (args->next->next != NULL) {
1862 expression_t *const locality = args->next->next->expression;
1864 in[2] = expression_to_firm(locality);
1866 in[2] = new_Const_long(mode_int, 3);
1869 in[1] = new_Const_long(mode_int, 0);
1870 in[2] = new_Const_long(mode_int, 3);
1872 ir_type *tp = get_ir_type(function_type);
1873 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1874 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1877 case bk_gnu_builtin_object_size: {
1878 /* determine value of "type" */
1879 expression_t *type_expression = call->arguments->next->expression;
1880 long type_val = fold_constant_to_int(type_expression);
1881 type_t *type = function_type->function.return_type;
1882 ir_mode *mode = get_ir_mode_arithmetic(type);
1883 /* just produce a "I don't know" result */
1884 ir_tarval *result = type_val & 2 ? get_mode_null(mode) :
1885 get_mode_minus_one(mode);
1887 return new_d_Const(dbgi, result);
1889 case bk_gnu_builtin_trap:
1892 ir_type *tp = get_ir_type(function_type);
1893 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1894 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1897 case bk_ms__debugbreak: {
1898 ir_type *tp = get_ir_type(function_type);
1899 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1900 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1903 case bk_ms_ReturnAddress: {
1906 in[0] = new_Const(get_mode_null(mode_int));
1907 in[1] = get_irg_frame(current_ir_graph);
1908 ir_type *tp = get_ir_type(function_type);
1909 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1910 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1913 case bk_ms_rotl64: {
1914 ir_node *val = expression_to_firm(call->arguments->expression);
1915 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1916 ir_mode *mode = get_irn_mode(val);
1917 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1920 case bk_ms_rotr64: {
1921 ir_node *val = expression_to_firm(call->arguments->expression);
1922 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1923 ir_mode *mode = get_irn_mode(val);
1924 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1925 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1926 return new_d_Rotl(dbgi, val, sub, mode);
1928 case bk_ms_byteswap_ushort:
1929 case bk_ms_byteswap_ulong:
1930 case bk_ms_byteswap_uint64:
1931 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1934 case bk_ms__indword:
1935 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1936 case bk_ms__outbyte:
1937 case bk_ms__outword:
1938 case bk_ms__outdword:
1939 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1940 call->arguments->next->expression, function_type, dbgi);
1942 panic("unsupported builtin found");
1947 * Transform a call expression.
1948 * Handles some special cases, like alloca() calls, which must be resolved
1949 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1950 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1953 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1955 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1956 assert(currently_reachable());
1958 expression_t *function = call->function;
1959 if (function->kind == EXPR_REFERENCE) {
1960 const reference_expression_t *ref = &function->reference;
1961 entity_t *entity = ref->entity;
1963 if (entity->kind == ENTITY_FUNCTION) {
1964 ir_entity *irentity = entity->function.irentity;
1965 if (irentity == NULL)
1966 irentity = get_function_entity(entity, NULL);
1968 if (irentity == NULL && entity->function.btk != bk_none) {
1969 return process_builtin_call(call);
1973 if (irentity == rts_entities[rts_alloca]) {
1974 /* handle alloca() call */
1975 expression_t *argument = call->arguments->expression;
1976 ir_node *size = expression_to_firm(argument);
1977 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1979 size = create_conv(dbgi, size, mode);
1981 ir_node *store = get_store();
1982 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1983 firm_unknown_type, stack_alloc);
1984 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1986 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1993 ir_node *callee = expression_to_firm(function);
1995 type_t *type = skip_typeref(function->base.type);
1996 assert(is_type_pointer(type));
1997 pointer_type_t *pointer_type = &type->pointer;
1998 type_t *points_to = skip_typeref(pointer_type->points_to);
1999 assert(is_type_function(points_to));
2000 function_type_t *function_type = &points_to->function;
2002 int n_parameters = 0;
2003 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
2004 ir_type *new_method_type = NULL;
2005 if (function_type->variadic || function_type->unspecified_parameters) {
2006 const call_argument_t *argument = call->arguments;
2007 for ( ; argument != NULL; argument = argument->next) {
2011 /* we need to construct a new method type matching the call
2013 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2014 int n_res = get_method_n_ress(ir_method_type);
2015 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2016 set_method_calling_convention(new_method_type,
2017 get_method_calling_convention(ir_method_type));
2018 set_method_additional_properties(new_method_type,
2019 get_method_additional_properties(ir_method_type));
2020 set_method_variadicity(new_method_type,
2021 get_method_variadicity(ir_method_type));
2023 for (int i = 0; i < n_res; ++i) {
2024 set_method_res_type(new_method_type, i,
2025 get_method_res_type(ir_method_type, i));
2027 argument = call->arguments;
2028 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2029 expression_t *expression = argument->expression;
2030 ir_type *irtype = get_ir_type(expression->base.type);
2031 set_method_param_type(new_method_type, i, irtype);
2033 ir_method_type = new_method_type;
2035 n_parameters = get_method_n_params(ir_method_type);
2038 ir_node *in[n_parameters];
2040 const call_argument_t *argument = call->arguments;
2041 for (int n = 0; n < n_parameters; ++n) {
2042 expression_t *expression = argument->expression;
2043 ir_node *arg_node = expression_to_firm(expression);
2045 type_t *type = skip_typeref(expression->base.type);
2046 if (!is_type_compound(type)) {
2047 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2048 arg_node = create_conv(dbgi, arg_node, mode);
2049 arg_node = do_strict_conv(dbgi, arg_node);
2054 argument = argument->next;
2057 ir_node *store = get_store();
2058 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2060 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2063 type_t *return_type = skip_typeref(function_type->return_type);
2064 ir_node *result = NULL;
2066 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2067 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2069 if (is_type_scalar(return_type)) {
2070 ir_mode *mode = get_ir_mode_storage(return_type);
2071 result = new_d_Proj(dbgi, resproj, mode, 0);
2072 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2073 result = create_conv(NULL, result, mode_arith);
2075 ir_mode *mode = mode_P_data;
2076 result = new_d_Proj(dbgi, resproj, mode, 0);
2080 if (function->kind == EXPR_REFERENCE &&
2081 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2082 /* A dead end: Keep the Call and the Block. Also place all further
2083 * nodes into a new and unreachable block. */
2085 keep_alive(get_cur_block());
2086 ir_node *block = new_Block(0, NULL);
2087 set_cur_block(block);
2093 static void statement_to_firm(statement_t *statement);
2094 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2096 static ir_node *expression_to_addr(const expression_t *expression);
2097 static ir_node *create_condition_evaluation(const expression_t *expression,
2098 ir_node *true_block,
2099 ir_node *false_block);
2101 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2104 if (!is_type_compound(type)) {
2105 ir_mode *mode = get_ir_mode_storage(type);
2106 value = create_conv(dbgi, value, mode);
2107 value = do_strict_conv(dbgi, value);
2110 ir_node *memory = get_store();
2112 if (is_type_scalar(type)) {
2113 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2114 ? cons_volatile : cons_none;
2115 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2116 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2117 set_store(store_mem);
2119 ir_type *irtype = get_ir_type(type);
2120 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2121 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2122 set_store(copyb_mem);
2126 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2128 ir_tarval *all_one = get_mode_all_one(mode);
2129 int mode_size = get_mode_size_bits(mode);
2131 assert(offset >= 0);
2133 assert(offset + size <= mode_size);
2134 if (size == mode_size) {
2138 long shiftr = get_mode_size_bits(mode) - size;
2139 long shiftl = offset;
2140 ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2141 ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2142 ir_tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2143 ir_tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2148 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2149 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2151 ir_type *entity_type = get_entity_type(entity);
2152 ir_type *base_type = get_primitive_base_type(entity_type);
2153 assert(base_type != NULL);
2154 ir_mode *mode = get_type_mode(base_type);
2156 value = create_conv(dbgi, value, mode);
2158 /* kill upper bits of value and shift to right position */
2159 int bitoffset = get_entity_offset_bits_remainder(entity);
2160 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2162 ir_tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2163 ir_node *mask_node = new_d_Const(dbgi, mask);
2164 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2165 ir_tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2166 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2167 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2169 /* load current value */
2170 ir_node *mem = get_store();
2171 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2172 set_volatile ? cons_volatile : cons_none);
2173 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2174 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2175 ir_tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2176 ir_tarval *inv_mask = tarval_not(shift_mask);
2177 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2178 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2180 /* construct new value and store */
2181 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2182 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2183 set_volatile ? cons_volatile : cons_none);
2184 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2185 set_store(store_mem);
2187 return value_masked;
2190 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2193 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2194 type_t *type = expression->base.type;
2195 ir_mode *mode = get_ir_mode_storage(type);
2196 ir_node *mem = get_store();
2197 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2198 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2199 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2201 load_res = create_conv(dbgi, load_res, mode_int);
2203 set_store(load_mem);
2205 /* kill upper bits */
2206 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2207 ir_entity *entity = expression->compound_entry->compound_member.entity;
2208 int bitoffset = get_entity_offset_bits_remainder(entity);
2209 ir_type *entity_type = get_entity_type(entity);
2210 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2211 long shift_bitsl = machine_size - bitoffset - bitsize;
2212 assert(shift_bitsl >= 0);
2213 ir_tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2214 ir_node *countl = new_d_Const(dbgi, tvl);
2215 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2217 long shift_bitsr = bitoffset + shift_bitsl;
2218 assert(shift_bitsr <= (long) machine_size);
2219 ir_tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2220 ir_node *countr = new_d_Const(dbgi, tvr);
2222 if (mode_is_signed(mode)) {
2223 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2225 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2228 return create_conv(dbgi, shiftr, mode);
2231 /* make sure the selected compound type is constructed */
2232 static void construct_select_compound(const select_expression_t *expression)
2234 type_t *type = skip_typeref(expression->compound->base.type);
2235 if (is_type_pointer(type)) {
2236 type = type->pointer.points_to;
2238 (void) get_ir_type(type);
2241 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2242 ir_node *value, ir_node *addr)
2244 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2245 type_t *type = skip_typeref(expression->base.type);
2247 if (!is_type_compound(type)) {
2248 ir_mode *mode = get_ir_mode_storage(type);
2249 value = create_conv(dbgi, value, mode);
2250 value = do_strict_conv(dbgi, value);
2253 if (expression->kind == EXPR_REFERENCE) {
2254 const reference_expression_t *ref = &expression->reference;
2256 entity_t *entity = ref->entity;
2257 assert(is_declaration(entity));
2258 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2259 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2260 set_value(entity->variable.v.value_number, value);
2262 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2263 set_value(entity->parameter.v.value_number, value);
2269 addr = expression_to_addr(expression);
2270 assert(addr != NULL);
2272 if (expression->kind == EXPR_SELECT) {
2273 const select_expression_t *select = &expression->select;
2275 construct_select_compound(select);
2277 entity_t *entity = select->compound_entry;
2278 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2279 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2280 ir_entity *irentity = entity->compound_member.entity;
2282 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2283 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2289 assign_value(dbgi, addr, type, value);
2293 static void set_value_for_expression(const expression_t *expression,
2296 set_value_for_expression_addr(expression, value, NULL);
2299 static ir_node *get_value_from_lvalue(const expression_t *expression,
2302 if (expression->kind == EXPR_REFERENCE) {
2303 const reference_expression_t *ref = &expression->reference;
2305 entity_t *entity = ref->entity;
2306 assert(entity->kind == ENTITY_VARIABLE
2307 || entity->kind == ENTITY_PARAMETER);
2308 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2310 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2311 value_number = entity->variable.v.value_number;
2312 assert(addr == NULL);
2313 type_t *type = skip_typeref(expression->base.type);
2314 ir_mode *mode = get_ir_mode_storage(type);
2315 ir_node *res = get_value(value_number, mode);
2316 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2317 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2318 value_number = entity->parameter.v.value_number;
2319 assert(addr == NULL);
2320 type_t *type = skip_typeref(expression->base.type);
2321 ir_mode *mode = get_ir_mode_storage(type);
2322 ir_node *res = get_value(value_number, mode);
2323 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2327 assert(addr != NULL);
2328 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2331 if (expression->kind == EXPR_SELECT &&
2332 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2333 construct_select_compound(&expression->select);
2334 value = bitfield_extract_to_firm(&expression->select, addr);
2336 value = deref_address(dbgi, expression->base.type, addr);
2343 static ir_node *create_incdec(const unary_expression_t *expression)
2345 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2346 const expression_t *value_expr = expression->value;
2347 ir_node *addr = expression_to_addr(value_expr);
2348 ir_node *value = get_value_from_lvalue(value_expr, addr);
2350 type_t *type = skip_typeref(expression->base.type);
2351 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2354 if (is_type_pointer(type)) {
2355 pointer_type_t *pointer_type = &type->pointer;
2356 offset = get_type_size_node(pointer_type->points_to);
2358 assert(is_type_arithmetic(type));
2359 offset = new_Const(get_mode_one(mode));
2363 ir_node *store_value;
2364 switch(expression->base.kind) {
2365 case EXPR_UNARY_POSTFIX_INCREMENT:
2367 store_value = new_d_Add(dbgi, value, offset, mode);
2369 case EXPR_UNARY_POSTFIX_DECREMENT:
2371 store_value = new_d_Sub(dbgi, value, offset, mode);
2373 case EXPR_UNARY_PREFIX_INCREMENT:
2374 result = new_d_Add(dbgi, value, offset, mode);
2375 store_value = result;
2377 case EXPR_UNARY_PREFIX_DECREMENT:
2378 result = new_d_Sub(dbgi, value, offset, mode);
2379 store_value = result;
2382 panic("no incdec expr in create_incdec");
2385 set_value_for_expression_addr(value_expr, store_value, addr);
2390 static bool is_local_variable(expression_t *expression)
2392 if (expression->kind != EXPR_REFERENCE)
2394 reference_expression_t *ref_expr = &expression->reference;
2395 entity_t *entity = ref_expr->entity;
2396 if (entity->kind != ENTITY_VARIABLE)
2398 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2399 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2402 static ir_relation get_relation(const expression_kind_t kind)
2405 case EXPR_BINARY_EQUAL: return ir_relation_equal;
2406 case EXPR_BINARY_ISLESSGREATER: return ir_relation_less_greater;
2407 case EXPR_BINARY_NOTEQUAL: return ir_relation_unordered_less_greater;
2408 case EXPR_BINARY_ISLESS:
2409 case EXPR_BINARY_LESS: return ir_relation_less;
2410 case EXPR_BINARY_ISLESSEQUAL:
2411 case EXPR_BINARY_LESSEQUAL: return ir_relation_less_equal;
2412 case EXPR_BINARY_ISGREATER:
2413 case EXPR_BINARY_GREATER: return ir_relation_greater;
2414 case EXPR_BINARY_ISGREATEREQUAL:
2415 case EXPR_BINARY_GREATEREQUAL: return ir_relation_greater_equal;
2416 case EXPR_BINARY_ISUNORDERED: return ir_relation_unordered;
2421 panic("trying to get pn_Cmp from non-comparison binexpr type");
2425 * Handle the assume optimizer hint: check if a Confirm
2426 * node can be created.
2428 * @param dbi debug info
2429 * @param expr the IL assume expression
2431 * we support here only some simple cases:
2436 static ir_node *handle_assume_compare(dbg_info *dbi,
2437 const binary_expression_t *expression)
2439 expression_t *op1 = expression->left;
2440 expression_t *op2 = expression->right;
2441 entity_t *var2, *var = NULL;
2442 ir_node *res = NULL;
2443 ir_relation relation = get_relation(expression->base.kind);
2445 if (is_local_variable(op1) && is_local_variable(op2)) {
2446 var = op1->reference.entity;
2447 var2 = op2->reference.entity;
2449 type_t *const type = skip_typeref(var->declaration.type);
2450 ir_mode *const mode = get_ir_mode_storage(type);
2452 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2453 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2455 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_relation(relation));
2456 set_value(var2->variable.v.value_number, res);
2458 res = new_d_Confirm(dbi, irn1, irn2, relation);
2459 set_value(var->variable.v.value_number, res);
2465 if (is_local_variable(op1) && is_constant_expression(op2) == EXPR_CLASS_CONSTANT) {
2466 var = op1->reference.entity;
2468 } else if (is_constant_expression(op1) == EXPR_CLASS_CONSTANT && is_local_variable(op2)) {
2469 relation = get_inversed_relation(relation);
2470 var = op2->reference.entity;
2475 type_t *const type = skip_typeref(var->declaration.type);
2476 ir_mode *const mode = get_ir_mode_storage(type);
2478 res = get_value(var->variable.v.value_number, mode);
2479 res = new_d_Confirm(dbi, res, expression_to_firm(con), relation);
2480 set_value(var->variable.v.value_number, res);
2486 * Handle the assume optimizer hint.
2488 * @param dbi debug info
2489 * @param expr the IL assume expression
2491 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2493 switch(expression->kind) {
2494 case EXPR_BINARY_EQUAL:
2495 case EXPR_BINARY_NOTEQUAL:
2496 case EXPR_BINARY_LESS:
2497 case EXPR_BINARY_LESSEQUAL:
2498 case EXPR_BINARY_GREATER:
2499 case EXPR_BINARY_GREATEREQUAL:
2500 return handle_assume_compare(dbi, &expression->binary);
2506 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2507 type_t *from_type, type_t *type)
2509 type = skip_typeref(type);
2510 if (type == type_void) {
2511 /* make sure firm type is constructed */
2512 (void) get_ir_type(type);
2515 if (!is_type_scalar(type)) {
2516 /* make sure firm type is constructed */
2517 (void) get_ir_type(type);
2521 from_type = skip_typeref(from_type);
2522 ir_mode *mode = get_ir_mode_storage(type);
2523 /* check for conversion from / to __based types */
2524 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2525 const variable_t *from_var = from_type->pointer.base_variable;
2526 const variable_t *to_var = type->pointer.base_variable;
2527 if (from_var != to_var) {
2528 if (from_var != NULL) {
2529 ir_node *const addr = create_symconst(dbgi, from_var->v.entity);
2530 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2531 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2533 if (to_var != NULL) {
2534 ir_node *const addr = create_symconst(dbgi, to_var->v.entity);
2535 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2536 value_node = new_d_Sub(dbgi, value_node, base, mode);
2541 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2542 /* bool adjustments (we save a mode_Bu, but have to temporarily
2543 * convert to mode_b so we only get a 0/1 value */
2544 value_node = create_conv(dbgi, value_node, mode_b);
2547 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2548 ir_node *node = create_conv(dbgi, value_node, mode);
2549 node = do_strict_conv(dbgi, node);
2550 node = create_conv(dbgi, node, mode_arith);
2555 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2557 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2558 type_t *type = skip_typeref(expression->base.type);
2560 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2561 return expression_to_addr(expression->value);
2563 const expression_t *value = expression->value;
2565 switch(expression->base.kind) {
2566 case EXPR_UNARY_NEGATE: {
2567 ir_node *value_node = expression_to_firm(value);
2568 ir_mode *mode = get_ir_mode_arithmetic(type);
2569 return new_d_Minus(dbgi, value_node, mode);
2571 case EXPR_UNARY_PLUS:
2572 return expression_to_firm(value);
2573 case EXPR_UNARY_BITWISE_NEGATE: {
2574 ir_node *value_node = expression_to_firm(value);
2575 ir_mode *mode = get_ir_mode_arithmetic(type);
2576 return new_d_Not(dbgi, value_node, mode);
2578 case EXPR_UNARY_NOT: {
2579 ir_node *value_node = _expression_to_firm(value);
2580 value_node = create_conv(dbgi, value_node, mode_b);
2581 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2584 case EXPR_UNARY_DEREFERENCE: {
2585 ir_node *value_node = expression_to_firm(value);
2586 type_t *value_type = skip_typeref(value->base.type);
2587 assert(is_type_pointer(value_type));
2589 /* check for __based */
2590 const variable_t *const base_var = value_type->pointer.base_variable;
2591 if (base_var != NULL) {
2592 ir_node *const addr = create_symconst(dbgi, base_var->v.entity);
2593 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2594 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2596 type_t *points_to = value_type->pointer.points_to;
2597 return deref_address(dbgi, points_to, value_node);
2599 case EXPR_UNARY_POSTFIX_INCREMENT:
2600 case EXPR_UNARY_POSTFIX_DECREMENT:
2601 case EXPR_UNARY_PREFIX_INCREMENT:
2602 case EXPR_UNARY_PREFIX_DECREMENT:
2603 return create_incdec(expression);
2604 case EXPR_UNARY_CAST_IMPLICIT:
2605 case EXPR_UNARY_CAST: {
2606 ir_node *value_node = expression_to_firm(value);
2607 type_t *from_type = value->base.type;
2608 return create_cast(dbgi, value_node, from_type, type);
2610 case EXPR_UNARY_ASSUME:
2611 return handle_assume(dbgi, value);
2616 panic("invalid UNEXPR type found");
2620 * produces a 0/1 depending of the value of a mode_b node
2622 static ir_node *produce_condition_result(const expression_t *expression,
2623 ir_mode *mode, dbg_info *dbgi)
2625 ir_node *const one_block = new_immBlock();
2626 ir_node *const zero_block = new_immBlock();
2627 create_condition_evaluation(expression, one_block, zero_block);
2628 mature_immBlock(one_block);
2629 mature_immBlock(zero_block);
2631 ir_node *const jmp_one = new_rd_Jmp(dbgi, one_block);
2632 ir_node *const jmp_zero = new_rd_Jmp(dbgi, zero_block);
2633 ir_node *const in_cf[2] = { jmp_one, jmp_zero };
2634 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
2635 set_cur_block(block);
2637 ir_node *const one = new_Const(get_mode_one(mode));
2638 ir_node *const zero = new_Const(get_mode_null(mode));
2639 ir_node *const in[2] = { one, zero };
2640 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
2645 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2646 ir_node *value, type_t *type)
2648 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2649 assert(is_type_pointer(type));
2650 pointer_type_t *const pointer_type = &type->pointer;
2651 type_t *const points_to = skip_typeref(pointer_type->points_to);
2652 ir_node * elem_size = get_type_size_node(points_to);
2653 elem_size = create_conv(dbgi, elem_size, mode);
2654 value = create_conv(dbgi, value, mode);
2655 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2659 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2660 ir_node *left, ir_node *right)
2663 type_t *type_left = skip_typeref(expression->left->base.type);
2664 type_t *type_right = skip_typeref(expression->right->base.type);
2666 expression_kind_t kind = expression->base.kind;
2669 case EXPR_BINARY_SHIFTLEFT:
2670 case EXPR_BINARY_SHIFTRIGHT:
2671 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2672 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2673 mode = get_ir_mode_arithmetic(expression->base.type);
2674 right = create_conv(dbgi, right, mode_uint);
2677 case EXPR_BINARY_SUB:
2678 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2679 const pointer_type_t *const ptr_type = &type_left->pointer;
2681 mode = get_ir_mode_arithmetic(expression->base.type);
2682 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2683 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2684 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2685 ir_node *const no_mem = new_NoMem();
2686 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2687 mode, op_pin_state_floats);
2688 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2691 case EXPR_BINARY_SUB_ASSIGN:
2692 if (is_type_pointer(type_left)) {
2693 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2694 mode = get_ir_mode_arithmetic(type_left);
2699 case EXPR_BINARY_ADD:
2700 case EXPR_BINARY_ADD_ASSIGN:
2701 if (is_type_pointer(type_left)) {
2702 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2703 mode = get_ir_mode_arithmetic(type_left);
2705 } else if (is_type_pointer(type_right)) {
2706 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2707 mode = get_ir_mode_arithmetic(type_right);
2714 mode = get_ir_mode_arithmetic(type_right);
2715 left = create_conv(dbgi, left, mode);
2720 case EXPR_BINARY_ADD_ASSIGN:
2721 case EXPR_BINARY_ADD:
2722 return new_d_Add(dbgi, left, right, mode);
2723 case EXPR_BINARY_SUB_ASSIGN:
2724 case EXPR_BINARY_SUB:
2725 return new_d_Sub(dbgi, left, right, mode);
2726 case EXPR_BINARY_MUL_ASSIGN:
2727 case EXPR_BINARY_MUL:
2728 return new_d_Mul(dbgi, left, right, mode);
2729 case EXPR_BINARY_BITWISE_AND:
2730 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2731 return new_d_And(dbgi, left, right, mode);
2732 case EXPR_BINARY_BITWISE_OR:
2733 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2734 return new_d_Or(dbgi, left, right, mode);
2735 case EXPR_BINARY_BITWISE_XOR:
2736 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2737 return new_d_Eor(dbgi, left, right, mode);
2738 case EXPR_BINARY_SHIFTLEFT:
2739 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2740 return new_d_Shl(dbgi, left, right, mode);
2741 case EXPR_BINARY_SHIFTRIGHT:
2742 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2743 if (mode_is_signed(mode)) {
2744 return new_d_Shrs(dbgi, left, right, mode);
2746 return new_d_Shr(dbgi, left, right, mode);
2748 case EXPR_BINARY_DIV:
2749 case EXPR_BINARY_DIV_ASSIGN: {
2750 ir_node *pin = new_Pin(new_NoMem());
2751 ir_node *op = new_d_Div(dbgi, pin, left, right, mode,
2752 op_pin_state_floats);
2753 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2756 case EXPR_BINARY_MOD:
2757 case EXPR_BINARY_MOD_ASSIGN: {
2758 ir_node *pin = new_Pin(new_NoMem());
2759 assert(!mode_is_float(mode));
2760 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2761 op_pin_state_floats);
2762 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2766 panic("unexpected expression kind");
2770 static ir_node *create_lazy_op(const binary_expression_t *expression)
2772 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2773 type_t *type = skip_typeref(expression->base.type);
2774 ir_mode *mode = get_ir_mode_arithmetic(type);
2776 if (is_constant_expression(expression->left) == EXPR_CLASS_CONSTANT) {
2777 bool val = fold_constant_to_bool(expression->left);
2778 expression_kind_t ekind = expression->base.kind;
2779 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2780 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2782 return new_Const(get_mode_null(mode));
2786 return new_Const(get_mode_one(mode));
2790 if (is_constant_expression(expression->right) == EXPR_CLASS_CONSTANT) {
2791 bool valr = fold_constant_to_bool(expression->right);
2792 return create_Const_from_bool(mode, valr);
2795 return produce_condition_result(expression->right, mode, dbgi);
2798 return produce_condition_result((const expression_t*) expression, mode,
2802 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2803 ir_node *right, ir_mode *mode);
2805 static ir_node *create_assign_binop(const binary_expression_t *expression)
2807 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2808 const expression_t *left_expr = expression->left;
2809 type_t *type = skip_typeref(left_expr->base.type);
2810 ir_node *right = expression_to_firm(expression->right);
2811 ir_node *left_addr = expression_to_addr(left_expr);
2812 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2813 ir_node *result = create_op(dbgi, expression, left, right);
2815 result = create_cast(dbgi, result, expression->right->base.type, type);
2816 result = do_strict_conv(dbgi, result);
2818 result = set_value_for_expression_addr(left_expr, result, left_addr);
2820 if (!is_type_compound(type)) {
2821 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2822 result = create_conv(dbgi, result, mode_arithmetic);
2827 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2829 expression_kind_t kind = expression->base.kind;
2832 case EXPR_BINARY_EQUAL:
2833 case EXPR_BINARY_NOTEQUAL:
2834 case EXPR_BINARY_LESS:
2835 case EXPR_BINARY_LESSEQUAL:
2836 case EXPR_BINARY_GREATER:
2837 case EXPR_BINARY_GREATEREQUAL:
2838 case EXPR_BINARY_ISGREATER:
2839 case EXPR_BINARY_ISGREATEREQUAL:
2840 case EXPR_BINARY_ISLESS:
2841 case EXPR_BINARY_ISLESSEQUAL:
2842 case EXPR_BINARY_ISLESSGREATER:
2843 case EXPR_BINARY_ISUNORDERED: {
2844 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2845 ir_node *left = expression_to_firm(expression->left);
2846 ir_node *right = expression_to_firm(expression->right);
2847 ir_relation relation = get_relation(kind);
2848 ir_node *cmp = new_d_Cmp(dbgi, left, right, relation);
2851 case EXPR_BINARY_ASSIGN: {
2852 ir_node *addr = expression_to_addr(expression->left);
2853 ir_node *right = expression_to_firm(expression->right);
2855 = set_value_for_expression_addr(expression->left, right, addr);
2857 type_t *type = skip_typeref(expression->base.type);
2858 if (!is_type_compound(type)) {
2859 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2860 res = create_conv(NULL, res, mode_arithmetic);
2864 case EXPR_BINARY_ADD:
2865 case EXPR_BINARY_SUB:
2866 case EXPR_BINARY_MUL:
2867 case EXPR_BINARY_DIV:
2868 case EXPR_BINARY_MOD:
2869 case EXPR_BINARY_BITWISE_AND:
2870 case EXPR_BINARY_BITWISE_OR:
2871 case EXPR_BINARY_BITWISE_XOR:
2872 case EXPR_BINARY_SHIFTLEFT:
2873 case EXPR_BINARY_SHIFTRIGHT:
2875 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2876 ir_node *left = expression_to_firm(expression->left);
2877 ir_node *right = expression_to_firm(expression->right);
2878 return create_op(dbgi, expression, left, right);
2880 case EXPR_BINARY_LOGICAL_AND:
2881 case EXPR_BINARY_LOGICAL_OR:
2882 return create_lazy_op(expression);
2883 case EXPR_BINARY_COMMA:
2884 /* create side effects of left side */
2885 (void) expression_to_firm(expression->left);
2886 return _expression_to_firm(expression->right);
2888 case EXPR_BINARY_ADD_ASSIGN:
2889 case EXPR_BINARY_SUB_ASSIGN:
2890 case EXPR_BINARY_MUL_ASSIGN:
2891 case EXPR_BINARY_MOD_ASSIGN:
2892 case EXPR_BINARY_DIV_ASSIGN:
2893 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2894 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2895 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2896 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2897 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2898 return create_assign_binop(expression);
2900 panic("TODO binexpr type");
2904 static ir_node *array_access_addr(const array_access_expression_t *expression)
2906 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2907 ir_node *base_addr = expression_to_firm(expression->array_ref);
2908 ir_node *offset = expression_to_firm(expression->index);
2909 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2910 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2911 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2916 static ir_node *array_access_to_firm(
2917 const array_access_expression_t *expression)
2919 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2920 ir_node *addr = array_access_addr(expression);
2921 type_t *type = revert_automatic_type_conversion(
2922 (const expression_t*) expression);
2923 type = skip_typeref(type);
2925 return deref_address(dbgi, type, addr);
2928 static long get_offsetof_offset(const offsetof_expression_t *expression)
2930 type_t *orig_type = expression->type;
2933 designator_t *designator = expression->designator;
2934 for ( ; designator != NULL; designator = designator->next) {
2935 type_t *type = skip_typeref(orig_type);
2936 /* be sure the type is constructed */
2937 (void) get_ir_type(type);
2939 if (designator->symbol != NULL) {
2940 assert(is_type_compound(type));
2941 symbol_t *symbol = designator->symbol;
2943 compound_t *compound = type->compound.compound;
2944 entity_t *iter = compound->members.entities;
2945 for ( ; iter != NULL; iter = iter->base.next) {
2946 if (iter->base.symbol == symbol) {
2950 assert(iter != NULL);
2952 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2953 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2954 offset += get_entity_offset(iter->compound_member.entity);
2956 orig_type = iter->declaration.type;
2958 expression_t *array_index = designator->array_index;
2959 assert(designator->array_index != NULL);
2960 assert(is_type_array(type));
2962 long index = fold_constant_to_int(array_index);
2963 ir_type *arr_type = get_ir_type(type);
2964 ir_type *elem_type = get_array_element_type(arr_type);
2965 long elem_size = get_type_size_bytes(elem_type);
2967 offset += index * elem_size;
2969 orig_type = type->array.element_type;
2976 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2978 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2979 long offset = get_offsetof_offset(expression);
2980 ir_tarval *tv = new_tarval_from_long(offset, mode);
2981 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2983 return new_d_Const(dbgi, tv);
2986 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2987 ir_entity *entity, type_t *type);
2989 static ir_node *compound_literal_to_firm(
2990 const compound_literal_expression_t *expression)
2992 type_t *type = expression->type;
2994 /* create an entity on the stack */
2995 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2997 ident *const id = id_unique("CompLit.%u");
2998 ir_type *const irtype = get_ir_type(type);
2999 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3000 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
3001 set_entity_ld_ident(entity, id);
3003 /* create initialisation code */
3004 initializer_t *initializer = expression->initializer;
3005 create_local_initializer(initializer, dbgi, entity, type);
3007 /* create a sel for the compound literal address */
3008 ir_node *frame = get_irg_frame(current_ir_graph);
3009 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3014 * Transform a sizeof expression into Firm code.
3016 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3018 type_t *const type = skip_typeref(expression->type);
3019 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3020 if (is_type_array(type) && type->array.is_vla
3021 && expression->tp_expression != NULL) {
3022 expression_to_firm(expression->tp_expression);
3024 /* strange gnu extensions: sizeof(function) == 1 */
3025 if (is_type_function(type)) {
3026 ir_mode *mode = get_ir_mode_storage(type_size_t);
3027 return new_Const(get_mode_one(mode));
3030 return get_type_size_node(type);
3033 static entity_t *get_expression_entity(const expression_t *expression)
3035 if (expression->kind != EXPR_REFERENCE)
3038 return expression->reference.entity;
3041 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3043 switch(entity->kind) {
3044 DECLARATION_KIND_CASES
3045 return entity->declaration.alignment;
3048 return entity->compound.alignment;
3049 case ENTITY_TYPEDEF:
3050 return entity->typedefe.alignment;
3058 * Transform an alignof expression into Firm code.
3060 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3062 unsigned alignment = 0;
3064 const expression_t *tp_expression = expression->tp_expression;
3065 if (tp_expression != NULL) {
3066 entity_t *entity = get_expression_entity(tp_expression);
3067 if (entity != NULL) {
3068 if (entity->kind == ENTITY_FUNCTION) {
3069 /* a gnu-extension */
3072 alignment = get_cparser_entity_alignment(entity);
3077 if (alignment == 0) {
3078 type_t *type = expression->type;
3079 alignment = get_type_alignment(type);
3082 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3083 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3084 ir_tarval *tv = new_tarval_from_long(alignment, mode);
3085 return new_d_Const(dbgi, tv);
3088 static void init_ir_types(void);
3090 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3092 assert(is_type_valid(skip_typeref(expression->base.type)));
3094 bool constant_folding_old = constant_folding;
3095 constant_folding = true;
3099 assert(is_constant_expression(expression) == EXPR_CLASS_CONSTANT);
3101 ir_graph *old_current_ir_graph = current_ir_graph;
3102 current_ir_graph = get_const_code_irg();
3104 ir_node *cnst = expression_to_firm(expression);
3105 current_ir_graph = old_current_ir_graph;
3107 if (!is_Const(cnst)) {
3108 panic("couldn't fold constant");
3111 constant_folding = constant_folding_old;
3113 return get_Const_tarval(cnst);
3116 long fold_constant_to_int(const expression_t *expression)
3118 if (expression->kind == EXPR_INVALID)
3121 ir_tarval *tv = fold_constant_to_tarval(expression);
3122 if (!tarval_is_long(tv)) {
3123 panic("result of constant folding is not integer");
3126 return get_tarval_long(tv);
3129 bool fold_constant_to_bool(const expression_t *expression)
3131 if (expression->kind == EXPR_INVALID)
3133 ir_tarval *tv = fold_constant_to_tarval(expression);
3134 return !tarval_is_null(tv);
3137 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3139 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3141 /* first try to fold a constant condition */
3142 if (is_constant_expression(expression->condition) == EXPR_CLASS_CONSTANT) {
3143 bool val = fold_constant_to_bool(expression->condition);
3145 expression_t *true_expression = expression->true_expression;
3146 if (true_expression == NULL)
3147 true_expression = expression->condition;
3148 return expression_to_firm(true_expression);
3150 return expression_to_firm(expression->false_expression);
3154 ir_node *const true_block = new_immBlock();
3155 ir_node *const false_block = new_immBlock();
3156 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3157 mature_immBlock(true_block);
3158 mature_immBlock(false_block);
3160 set_cur_block(true_block);
3162 if (expression->true_expression != NULL) {
3163 true_val = expression_to_firm(expression->true_expression);
3164 } else if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3165 true_val = cond_expr;
3167 /* Condition ended with a short circuit (&&, ||, !) operation or a
3168 * comparison. Generate a "1" as value for the true branch. */
3169 true_val = new_Const(get_mode_one(mode_Is));
3171 ir_node *const true_jmp = new_d_Jmp(dbgi);
3173 set_cur_block(false_block);
3174 ir_node *const false_val = expression_to_firm(expression->false_expression);
3175 ir_node *const false_jmp = new_d_Jmp(dbgi);
3177 /* create the common block */
3178 ir_node *const in_cf[2] = { true_jmp, false_jmp };
3179 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
3180 set_cur_block(block);
3182 /* TODO improve static semantics, so either both or no values are NULL */
3183 if (true_val == NULL || false_val == NULL)
3186 ir_node *const in[2] = { true_val, false_val };
3187 type_t *const type = skip_typeref(expression->base.type);
3189 if (is_type_compound(type)) {
3192 mode = get_ir_mode_arithmetic(type);
3194 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
3200 * Returns an IR-node representing the address of a field.
3202 static ir_node *select_addr(const select_expression_t *expression)
3204 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3206 construct_select_compound(expression);
3208 ir_node *compound_addr = expression_to_firm(expression->compound);
3210 entity_t *entry = expression->compound_entry;
3211 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3212 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3214 if (constant_folding) {
3215 ir_mode *mode = get_irn_mode(compound_addr);
3216 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3217 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3218 return new_d_Add(dbgi, compound_addr, ofs, mode);
3220 ir_entity *irentity = entry->compound_member.entity;
3221 assert(irentity != NULL);
3222 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3226 static ir_node *select_to_firm(const select_expression_t *expression)
3228 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3229 ir_node *addr = select_addr(expression);
3230 type_t *type = revert_automatic_type_conversion(
3231 (const expression_t*) expression);
3232 type = skip_typeref(type);
3234 entity_t *entry = expression->compound_entry;
3235 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3236 type_t *entry_type = skip_typeref(entry->declaration.type);
3238 if (entry_type->kind == TYPE_BITFIELD) {
3239 return bitfield_extract_to_firm(expression, addr);
3242 return deref_address(dbgi, type, addr);
3245 /* Values returned by __builtin_classify_type. */
3246 typedef enum gcc_type_class
3252 enumeral_type_class,
3255 reference_type_class,
3259 function_type_class,
3270 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3272 type_t *type = expr->type_expression->base.type;
3274 /* FIXME gcc returns different values depending on whether compiling C or C++
3275 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3278 type = skip_typeref(type);
3279 switch (type->kind) {
3281 const atomic_type_t *const atomic_type = &type->atomic;
3282 switch (atomic_type->akind) {
3283 /* should not be reached */
3284 case ATOMIC_TYPE_INVALID:
3288 /* gcc cannot do that */
3289 case ATOMIC_TYPE_VOID:
3290 tc = void_type_class;
3293 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3294 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3295 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3296 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3297 case ATOMIC_TYPE_SHORT:
3298 case ATOMIC_TYPE_USHORT:
3299 case ATOMIC_TYPE_INT:
3300 case ATOMIC_TYPE_UINT:
3301 case ATOMIC_TYPE_LONG:
3302 case ATOMIC_TYPE_ULONG:
3303 case ATOMIC_TYPE_LONGLONG:
3304 case ATOMIC_TYPE_ULONGLONG:
3305 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3306 tc = integer_type_class;
3309 case ATOMIC_TYPE_FLOAT:
3310 case ATOMIC_TYPE_DOUBLE:
3311 case ATOMIC_TYPE_LONG_DOUBLE:
3312 tc = real_type_class;
3315 panic("Unexpected atomic type in classify_type_to_firm().");
3318 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3319 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3320 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3321 case TYPE_ARRAY: /* gcc handles this as pointer */
3322 case TYPE_FUNCTION: /* gcc handles this as pointer */
3323 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3324 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3325 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3327 /* gcc handles this as integer */
3328 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3330 /* gcc classifies the referenced type */
3331 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3333 /* typedef/typeof should be skipped already */
3340 panic("unexpected TYPE classify_type_to_firm().");
3344 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3345 ir_tarval *const tv = new_tarval_from_long(tc, mode_int);
3346 return new_d_Const(dbgi, tv);
3349 static ir_node *function_name_to_firm(
3350 const funcname_expression_t *const expr)
3352 switch(expr->kind) {
3353 case FUNCNAME_FUNCTION:
3354 case FUNCNAME_PRETTY_FUNCTION:
3355 case FUNCNAME_FUNCDNAME:
3356 if (current_function_name == NULL) {
3357 const source_position_t *const src_pos = &expr->base.source_position;
3358 const char *name = current_function_entity->base.symbol->string;
3359 const string_t string = { name, strlen(name) + 1 };
3360 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3362 return current_function_name;
3363 case FUNCNAME_FUNCSIG:
3364 if (current_funcsig == NULL) {
3365 const source_position_t *const src_pos = &expr->base.source_position;
3366 ir_entity *ent = get_irg_entity(current_ir_graph);
3367 const char *const name = get_entity_ld_name(ent);
3368 const string_t string = { name, strlen(name) + 1 };
3369 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3371 return current_funcsig;
3373 panic("Unsupported function name");
3376 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3378 statement_t *statement = expr->statement;
3380 assert(statement->kind == STATEMENT_COMPOUND);
3381 return compound_statement_to_firm(&statement->compound);
3384 static ir_node *va_start_expression_to_firm(
3385 const va_start_expression_t *const expr)
3387 type_t *const type = current_function_entity->declaration.type;
3388 ir_type *const method_type = get_ir_type(type);
3389 int const n = get_method_n_params(method_type) - 1;
3390 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3391 ir_node *const frame = get_irg_frame(current_ir_graph);
3392 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3393 ir_node *const no_mem = new_NoMem();
3394 ir_node *const arg_sel =
3395 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3397 type_t *const param_type = expr->parameter->base.type;
3398 ir_node *const cnst = get_type_size_node(param_type);
3399 ir_mode *const mode = get_irn_mode(cnst);
3400 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3401 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3402 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3403 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3404 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3405 set_value_for_expression(expr->ap, add);
3410 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3412 type_t *const type = expr->base.type;
3413 expression_t *const ap_expr = expr->ap;
3414 ir_node *const ap_addr = expression_to_addr(ap_expr);
3415 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3416 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3417 ir_node *const res = deref_address(dbgi, type, ap);
3419 ir_node *const cnst = get_type_size_node(expr->base.type);
3420 ir_mode *const mode = get_irn_mode(cnst);
3421 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3422 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3423 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3424 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3425 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3427 set_value_for_expression_addr(ap_expr, add, ap_addr);
3433 * Generate Firm for a va_copy expression.
3435 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3437 ir_node *const src = expression_to_firm(expr->src);
3438 set_value_for_expression(expr->dst, src);
3442 static ir_node *dereference_addr(const unary_expression_t *const expression)
3444 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3445 return expression_to_firm(expression->value);
3449 * Returns a IR-node representing an lvalue of the given expression.
3451 static ir_node *expression_to_addr(const expression_t *expression)
3453 switch(expression->kind) {
3454 case EXPR_ARRAY_ACCESS:
3455 return array_access_addr(&expression->array_access);
3457 return call_expression_to_firm(&expression->call);
3458 case EXPR_COMPOUND_LITERAL:
3459 return compound_literal_to_firm(&expression->compound_literal);
3460 case EXPR_REFERENCE:
3461 return reference_addr(&expression->reference);
3463 return select_addr(&expression->select);
3464 case EXPR_UNARY_DEREFERENCE:
3465 return dereference_addr(&expression->unary);
3469 panic("trying to get address of non-lvalue");
3472 static ir_node *builtin_constant_to_firm(
3473 const builtin_constant_expression_t *expression)
3475 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3476 bool const v = is_constant_expression(expression->value) == EXPR_CLASS_CONSTANT;
3477 return create_Const_from_bool(mode, v);
3480 static ir_node *builtin_types_compatible_to_firm(
3481 const builtin_types_compatible_expression_t *expression)
3483 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3484 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3485 bool const value = types_compatible(left, right);
3486 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3487 return create_Const_from_bool(mode, value);
3490 static ir_node *get_label_block(label_t *label)
3492 if (label->block != NULL)
3493 return label->block;
3495 /* beware: might be called from create initializer with current_ir_graph
3496 * set to const_code_irg. */
3497 ir_graph *rem = current_ir_graph;
3498 current_ir_graph = current_function;
3500 ir_node *block = new_immBlock();
3502 label->block = block;
3504 ARR_APP1(label_t *, all_labels, label);
3506 current_ir_graph = rem;
3511 * Pointer to a label. This is used for the
3512 * GNU address-of-label extension.
3514 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3516 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3517 ir_node *block = get_label_block(label->label);
3518 ir_entity *entity = create_Block_entity(block);
3520 symconst_symbol value;
3521 value.entity_p = entity;
3522 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3526 * creates firm nodes for an expression. The difference between this function
3527 * and expression_to_firm is, that this version might produce mode_b nodes
3528 * instead of mode_Is.
3530 static ir_node *_expression_to_firm(const expression_t *expression)
3533 if (!constant_folding) {
3534 assert(!expression->base.transformed);
3535 ((expression_t*) expression)->base.transformed = true;
3539 switch (expression->kind) {
3541 return literal_to_firm(&expression->literal);
3542 case EXPR_STRING_LITERAL:
3543 return string_to_firm(&expression->base.source_position, "str.%u",
3544 &expression->literal.value);
3545 case EXPR_WIDE_STRING_LITERAL:
3546 return wide_string_literal_to_firm(&expression->string_literal);
3547 case EXPR_REFERENCE:
3548 return reference_expression_to_firm(&expression->reference);
3549 case EXPR_REFERENCE_ENUM_VALUE:
3550 return reference_expression_enum_value_to_firm(&expression->reference);
3552 return call_expression_to_firm(&expression->call);
3554 return unary_expression_to_firm(&expression->unary);
3556 return binary_expression_to_firm(&expression->binary);
3557 case EXPR_ARRAY_ACCESS:
3558 return array_access_to_firm(&expression->array_access);
3560 return sizeof_to_firm(&expression->typeprop);
3562 return alignof_to_firm(&expression->typeprop);
3563 case EXPR_CONDITIONAL:
3564 return conditional_to_firm(&expression->conditional);
3566 return select_to_firm(&expression->select);
3567 case EXPR_CLASSIFY_TYPE:
3568 return classify_type_to_firm(&expression->classify_type);
3570 return function_name_to_firm(&expression->funcname);
3571 case EXPR_STATEMENT:
3572 return statement_expression_to_firm(&expression->statement);
3574 return va_start_expression_to_firm(&expression->va_starte);
3576 return va_arg_expression_to_firm(&expression->va_arge);
3578 return va_copy_expression_to_firm(&expression->va_copye);
3579 case EXPR_BUILTIN_CONSTANT_P:
3580 return builtin_constant_to_firm(&expression->builtin_constant);
3581 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3582 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3584 return offsetof_to_firm(&expression->offsetofe);
3585 case EXPR_COMPOUND_LITERAL:
3586 return compound_literal_to_firm(&expression->compound_literal);
3587 case EXPR_LABEL_ADDRESS:
3588 return label_address_to_firm(&expression->label_address);
3594 panic("invalid expression found");
3598 * Check if a given expression is a GNU __builtin_expect() call.
3600 static bool is_builtin_expect(const expression_t *expression)
3602 if (expression->kind != EXPR_CALL)
3605 expression_t *function = expression->call.function;
3606 if (function->kind != EXPR_REFERENCE)
3608 reference_expression_t *ref = &function->reference;
3609 if (ref->entity->kind != ENTITY_FUNCTION ||
3610 ref->entity->function.btk != bk_gnu_builtin_expect)
3616 static bool produces_mode_b(const expression_t *expression)
3618 switch (expression->kind) {
3619 case EXPR_BINARY_EQUAL:
3620 case EXPR_BINARY_NOTEQUAL:
3621 case EXPR_BINARY_LESS:
3622 case EXPR_BINARY_LESSEQUAL:
3623 case EXPR_BINARY_GREATER:
3624 case EXPR_BINARY_GREATEREQUAL:
3625 case EXPR_BINARY_ISGREATER:
3626 case EXPR_BINARY_ISGREATEREQUAL:
3627 case EXPR_BINARY_ISLESS:
3628 case EXPR_BINARY_ISLESSEQUAL:
3629 case EXPR_BINARY_ISLESSGREATER:
3630 case EXPR_BINARY_ISUNORDERED:
3631 case EXPR_UNARY_NOT:
3635 if (is_builtin_expect(expression)) {
3636 expression_t *argument = expression->call.arguments->expression;
3637 return produces_mode_b(argument);
3640 case EXPR_BINARY_COMMA:
3641 return produces_mode_b(expression->binary.right);
3648 static ir_node *expression_to_firm(const expression_t *expression)
3650 if (!produces_mode_b(expression)) {
3651 ir_node *res = _expression_to_firm(expression);
3652 assert(res == NULL || get_irn_mode(res) != mode_b);
3656 if (is_constant_expression(expression) == EXPR_CLASS_CONSTANT) {
3657 bool const constant_folding_old = constant_folding;
3658 constant_folding = true;
3659 ir_node *res = _expression_to_firm(expression);
3660 constant_folding = constant_folding_old;
3661 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3662 assert(is_Const(res));
3663 return create_Const_from_bool(mode, !is_Const_null(res));
3666 /* we have to produce a 0/1 from the mode_b expression */
3667 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3668 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3669 return produce_condition_result(expression, mode, dbgi);
3673 * create a short-circuit expression evaluation that tries to construct
3674 * efficient control flow structures for &&, || and ! expressions
3676 static ir_node *create_condition_evaluation(const expression_t *expression,
3677 ir_node *true_block,
3678 ir_node *false_block)
3680 switch(expression->kind) {
3681 case EXPR_UNARY_NOT: {
3682 const unary_expression_t *unary_expression = &expression->unary;
3683 create_condition_evaluation(unary_expression->value, false_block,
3687 case EXPR_BINARY_LOGICAL_AND: {
3688 const binary_expression_t *binary_expression = &expression->binary;
3690 ir_node *extra_block = new_immBlock();
3691 create_condition_evaluation(binary_expression->left, extra_block,
3693 mature_immBlock(extra_block);
3694 set_cur_block(extra_block);
3695 create_condition_evaluation(binary_expression->right, true_block,
3699 case EXPR_BINARY_LOGICAL_OR: {
3700 const binary_expression_t *binary_expression = &expression->binary;
3702 ir_node *extra_block = new_immBlock();
3703 create_condition_evaluation(binary_expression->left, true_block,
3705 mature_immBlock(extra_block);
3706 set_cur_block(extra_block);
3707 create_condition_evaluation(binary_expression->right, true_block,
3715 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3716 ir_node *cond_expr = _expression_to_firm(expression);
3717 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3718 ir_node *cond = new_d_Cond(dbgi, condition);
3719 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3720 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3722 /* set branch prediction info based on __builtin_expect */
3723 if (is_builtin_expect(expression) && is_Cond(cond)) {
3724 call_argument_t *argument = expression->call.arguments->next;
3725 if (is_constant_expression(argument->expression) == EXPR_CLASS_CONSTANT) {
3726 bool const cnst = fold_constant_to_bool(argument->expression);
3727 cond_jmp_predicate const pred = cnst ? COND_JMP_PRED_TRUE : COND_JMP_PRED_FALSE;
3728 set_Cond_jmp_pred(cond, pred);
3732 add_immBlock_pred(true_block, true_proj);
3733 add_immBlock_pred(false_block, false_proj);
3735 set_unreachable_now();
3739 static void create_variable_entity(entity_t *variable,
3740 declaration_kind_t declaration_kind,
3741 ir_type *parent_type)
3743 assert(variable->kind == ENTITY_VARIABLE);
3744 type_t *type = skip_typeref(variable->declaration.type);
3746 ident *const id = new_id_from_str(variable->base.symbol->string);
3747 ir_type *const irtype = get_ir_type(type);
3748 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3749 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3750 unsigned alignment = variable->declaration.alignment;
3752 set_entity_alignment(irentity, alignment);
3754 handle_decl_modifiers(irentity, variable);
3756 variable->declaration.kind = (unsigned char) declaration_kind;
3757 variable->variable.v.entity = irentity;
3758 set_entity_ld_ident(irentity, create_ld_ident(variable));
3760 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3761 set_entity_volatility(irentity, volatility_is_volatile);
3766 typedef struct type_path_entry_t type_path_entry_t;
3767 struct type_path_entry_t {
3769 ir_initializer_t *initializer;
3771 entity_t *compound_entry;
3774 typedef struct type_path_t type_path_t;
3775 struct type_path_t {
3776 type_path_entry_t *path;
3781 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3783 size_t len = ARR_LEN(path->path);
3785 for (size_t i = 0; i < len; ++i) {
3786 const type_path_entry_t *entry = & path->path[i];
3788 type_t *type = skip_typeref(entry->type);
3789 if (is_type_compound(type)) {
3790 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3791 } else if (is_type_array(type)) {
3792 fprintf(stderr, "[%u]", (unsigned) entry->index);
3794 fprintf(stderr, "-INVALID-");
3797 fprintf(stderr, " (");
3798 print_type(path->top_type);
3799 fprintf(stderr, ")");
3802 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3804 size_t len = ARR_LEN(path->path);
3806 return & path->path[len-1];
3809 static type_path_entry_t *append_to_type_path(type_path_t *path)
3811 size_t len = ARR_LEN(path->path);
3812 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3814 type_path_entry_t *result = & path->path[len];
3815 memset(result, 0, sizeof(result[0]));
3819 static size_t get_compound_member_count(const compound_type_t *type)
3821 compound_t *compound = type->compound;
3822 size_t n_members = 0;
3823 entity_t *member = compound->members.entities;
3824 for ( ; member != NULL; member = member->base.next) {
3831 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3833 type_t *orig_top_type = path->top_type;
3834 type_t *top_type = skip_typeref(orig_top_type);
3836 assert(is_type_compound(top_type) || is_type_array(top_type));
3838 if (ARR_LEN(path->path) == 0) {
3841 type_path_entry_t *top = get_type_path_top(path);
3842 ir_initializer_t *initializer = top->initializer;
3843 return get_initializer_compound_value(initializer, top->index);
3847 static void descend_into_subtype(type_path_t *path)
3849 type_t *orig_top_type = path->top_type;
3850 type_t *top_type = skip_typeref(orig_top_type);
3852 assert(is_type_compound(top_type) || is_type_array(top_type));
3854 ir_initializer_t *initializer = get_initializer_entry(path);
3856 type_path_entry_t *top = append_to_type_path(path);
3857 top->type = top_type;
3861 if (is_type_compound(top_type)) {
3862 compound_t *compound = top_type->compound.compound;
3863 entity_t *entry = compound->members.entities;
3865 top->compound_entry = entry;
3867 len = get_compound_member_count(&top_type->compound);
3868 if (entry != NULL) {
3869 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3870 path->top_type = entry->declaration.type;
3873 assert(is_type_array(top_type));
3874 assert(top_type->array.size > 0);
3877 path->top_type = top_type->array.element_type;
3878 len = top_type->array.size;
3880 if (initializer == NULL
3881 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3882 initializer = create_initializer_compound(len);
3883 /* we have to set the entry at the 2nd latest path entry... */
3884 size_t path_len = ARR_LEN(path->path);
3885 assert(path_len >= 1);
3887 type_path_entry_t *entry = & path->path[path_len-2];
3888 ir_initializer_t *tinitializer = entry->initializer;
3889 set_initializer_compound_value(tinitializer, entry->index,
3893 top->initializer = initializer;
3896 static void ascend_from_subtype(type_path_t *path)
3898 type_path_entry_t *top = get_type_path_top(path);
3900 path->top_type = top->type;
3902 size_t len = ARR_LEN(path->path);
3903 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3906 static void walk_designator(type_path_t *path, const designator_t *designator)
3908 /* designators start at current object type */
3909 ARR_RESIZE(type_path_entry_t, path->path, 1);
3911 for ( ; designator != NULL; designator = designator->next) {
3912 type_path_entry_t *top = get_type_path_top(path);
3913 type_t *orig_type = top->type;
3914 type_t *type = skip_typeref(orig_type);
3916 if (designator->symbol != NULL) {
3917 assert(is_type_compound(type));
3919 symbol_t *symbol = designator->symbol;
3921 compound_t *compound = type->compound.compound;
3922 entity_t *iter = compound->members.entities;
3923 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3924 if (iter->base.symbol == symbol) {
3925 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3929 assert(iter != NULL);
3931 /* revert previous initialisations of other union elements */
3932 if (type->kind == TYPE_COMPOUND_UNION) {
3933 ir_initializer_t *initializer = top->initializer;
3934 if (initializer != NULL
3935 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3936 /* are we writing to a new element? */
3937 ir_initializer_t *oldi
3938 = get_initializer_compound_value(initializer, index);
3939 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3940 /* clear initializer */
3942 = get_initializer_compound_n_entries(initializer);
3943 ir_initializer_t *nulli = get_initializer_null();
3944 for (size_t i = 0; i < len; ++i) {
3945 set_initializer_compound_value(initializer, i,
3952 top->type = orig_type;
3953 top->compound_entry = iter;
3955 orig_type = iter->declaration.type;
3957 expression_t *array_index = designator->array_index;
3958 assert(designator->array_index != NULL);
3959 assert(is_type_array(type));
3961 long index = fold_constant_to_int(array_index);
3964 if (type->array.size_constant) {
3965 long array_size = type->array.size;
3966 assert(index < array_size);
3970 top->type = orig_type;
3971 top->index = (size_t) index;
3972 orig_type = type->array.element_type;
3974 path->top_type = orig_type;
3976 if (designator->next != NULL) {
3977 descend_into_subtype(path);
3981 path->invalid = false;
3984 static void advance_current_object(type_path_t *path)
3986 if (path->invalid) {
3987 /* TODO: handle this... */
3988 panic("invalid initializer in ast2firm (excessive elements)");
3991 type_path_entry_t *top = get_type_path_top(path);
3993 type_t *type = skip_typeref(top->type);
3994 if (is_type_union(type)) {
3995 /* only the first element is initialized in unions */
3996 top->compound_entry = NULL;
3997 } else if (is_type_struct(type)) {
3998 entity_t *entry = top->compound_entry;
4001 entry = entry->base.next;
4002 top->compound_entry = entry;
4003 if (entry != NULL) {
4004 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
4005 path->top_type = entry->declaration.type;
4009 assert(is_type_array(type));
4012 if (!type->array.size_constant || top->index < type->array.size) {
4017 /* we're past the last member of the current sub-aggregate, try if we
4018 * can ascend in the type hierarchy and continue with another subobject */
4019 size_t len = ARR_LEN(path->path);
4022 ascend_from_subtype(path);
4023 advance_current_object(path);
4025 path->invalid = true;
4030 static ir_initializer_t *create_ir_initializer(
4031 const initializer_t *initializer, type_t *type);
4033 static ir_initializer_t *create_ir_initializer_value(
4034 const initializer_value_t *initializer)
4036 if (is_type_compound(initializer->value->base.type)) {
4037 panic("initializer creation for compounds not implemented yet");
4039 type_t *type = initializer->value->base.type;
4040 expression_t *expr = initializer->value;
4041 if (initializer_use_bitfield_basetype) {
4042 type_t *skipped = skip_typeref(type);
4043 if (skipped->kind == TYPE_BITFIELD) {
4044 /* remove the bitfield cast... */
4045 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4046 expr = expr->unary.value;
4047 type = skipped->bitfield.base_type;
4050 ir_node *value = expression_to_firm(expr);
4051 ir_mode *mode = get_ir_mode_storage(type);
4052 value = create_conv(NULL, value, mode);
4053 return create_initializer_const(value);
4056 /** test wether type can be initialized by a string constant */
4057 static bool is_string_type(type_t *type)
4060 if (is_type_pointer(type)) {
4061 inner = skip_typeref(type->pointer.points_to);
4062 } else if(is_type_array(type)) {
4063 inner = skip_typeref(type->array.element_type);
4068 return is_type_integer(inner);
4071 static ir_initializer_t *create_ir_initializer_list(
4072 const initializer_list_t *initializer, type_t *type)
4075 memset(&path, 0, sizeof(path));
4076 path.top_type = type;
4077 path.path = NEW_ARR_F(type_path_entry_t, 0);
4079 descend_into_subtype(&path);
4081 for (size_t i = 0; i < initializer->len; ++i) {
4082 const initializer_t *sub_initializer = initializer->initializers[i];
4084 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4085 walk_designator(&path, sub_initializer->designator.designator);
4089 if (sub_initializer->kind == INITIALIZER_VALUE) {
4090 /* we might have to descend into types until we're at a scalar
4093 type_t *orig_top_type = path.top_type;
4094 type_t *top_type = skip_typeref(orig_top_type);
4096 if (is_type_scalar(top_type))
4098 descend_into_subtype(&path);
4100 } else if (sub_initializer->kind == INITIALIZER_STRING
4101 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4102 /* we might have to descend into types until we're at a scalar
4105 type_t *orig_top_type = path.top_type;
4106 type_t *top_type = skip_typeref(orig_top_type);
4108 if (is_string_type(top_type))
4110 descend_into_subtype(&path);
4114 ir_initializer_t *sub_irinitializer
4115 = create_ir_initializer(sub_initializer, path.top_type);
4117 size_t path_len = ARR_LEN(path.path);
4118 assert(path_len >= 1);
4119 type_path_entry_t *entry = & path.path[path_len-1];
4120 ir_initializer_t *tinitializer = entry->initializer;
4121 set_initializer_compound_value(tinitializer, entry->index,
4124 advance_current_object(&path);
4127 assert(ARR_LEN(path.path) >= 1);
4128 ir_initializer_t *result = path.path[0].initializer;
4129 DEL_ARR_F(path.path);
4134 static ir_initializer_t *create_ir_initializer_string(
4135 const initializer_string_t *initializer, type_t *type)
4137 type = skip_typeref(type);
4139 size_t string_len = initializer->string.size;
4140 assert(type->kind == TYPE_ARRAY);
4141 assert(type->array.size_constant);
4142 size_t len = type->array.size;
4143 ir_initializer_t *irinitializer = create_initializer_compound(len);
4145 const char *string = initializer->string.begin;
4146 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4148 for (size_t i = 0; i < len; ++i) {
4153 ir_tarval *tv = new_tarval_from_long(c, mode);
4154 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4156 set_initializer_compound_value(irinitializer, i, char_initializer);
4159 return irinitializer;
4162 static ir_initializer_t *create_ir_initializer_wide_string(
4163 const initializer_wide_string_t *initializer, type_t *type)
4165 assert(type->kind == TYPE_ARRAY);
4166 assert(type->array.size_constant);
4167 size_t len = type->array.size;
4168 size_t string_len = wstrlen(&initializer->string);
4169 ir_initializer_t *irinitializer = create_initializer_compound(len);
4171 const char *p = initializer->string.begin;
4172 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4174 for (size_t i = 0; i < len; ++i) {
4176 if (i < string_len) {
4177 c = read_utf8_char(&p);
4179 ir_tarval *tv = new_tarval_from_long(c, mode);
4180 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4182 set_initializer_compound_value(irinitializer, i, char_initializer);
4185 return irinitializer;
4188 static ir_initializer_t *create_ir_initializer(
4189 const initializer_t *initializer, type_t *type)
4191 switch(initializer->kind) {
4192 case INITIALIZER_STRING:
4193 return create_ir_initializer_string(&initializer->string, type);
4195 case INITIALIZER_WIDE_STRING:
4196 return create_ir_initializer_wide_string(&initializer->wide_string,
4199 case INITIALIZER_LIST:
4200 return create_ir_initializer_list(&initializer->list, type);
4202 case INITIALIZER_VALUE:
4203 return create_ir_initializer_value(&initializer->value);
4205 case INITIALIZER_DESIGNATOR:
4206 panic("unexpected designator initializer found");
4208 panic("unknown initializer");
4211 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4212 * are elements [...] the remainder of the aggregate shall be initialized
4213 * implicitly the same as objects that have static storage duration. */
4214 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4217 /* for unions we must NOT do anything for null initializers */
4218 ir_type *owner = get_entity_owner(entity);
4219 if (is_Union_type(owner)) {
4223 ir_type *ent_type = get_entity_type(entity);
4224 /* create sub-initializers for a compound type */
4225 if (is_compound_type(ent_type)) {
4226 unsigned n_members = get_compound_n_members(ent_type);
4227 for (unsigned n = 0; n < n_members; ++n) {
4228 ir_entity *member = get_compound_member(ent_type, n);
4229 ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4231 create_dynamic_null_initializer(member, dbgi, addr);
4235 if (is_Array_type(ent_type)) {
4236 assert(has_array_upper_bound(ent_type, 0));
4237 long n = get_array_upper_bound_int(ent_type, 0);
4238 for (long i = 0; i < n; ++i) {
4239 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4240 ir_node *cnst = new_d_Const(dbgi, index_tv);
4241 ir_node *in[1] = { cnst };
4242 ir_entity *arrent = get_array_element_entity(ent_type);
4243 ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4245 create_dynamic_null_initializer(arrent, dbgi, addr);
4250 ir_mode *value_mode = get_type_mode(ent_type);
4251 ir_node *node = new_Const(get_mode_null(value_mode));
4253 /* is it a bitfield type? */
4254 if (is_Primitive_type(ent_type) &&
4255 get_primitive_base_type(ent_type) != NULL) {
4256 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4260 ir_node *mem = get_store();
4261 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4262 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4266 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4267 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4269 switch(get_initializer_kind(initializer)) {
4270 case IR_INITIALIZER_NULL:
4271 create_dynamic_null_initializer(entity, dbgi, base_addr);
4273 case IR_INITIALIZER_CONST: {
4274 ir_node *node = get_initializer_const_value(initializer);
4275 ir_type *ent_type = get_entity_type(entity);
4277 /* is it a bitfield type? */
4278 if (is_Primitive_type(ent_type) &&
4279 get_primitive_base_type(ent_type) != NULL) {
4280 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4284 assert(get_type_mode(type) == get_irn_mode(node));
4285 ir_node *mem = get_store();
4286 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4287 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4291 case IR_INITIALIZER_TARVAL: {
4292 ir_tarval *tv = get_initializer_tarval_value(initializer);
4293 ir_node *cnst = new_d_Const(dbgi, tv);
4294 ir_type *ent_type = get_entity_type(entity);
4296 /* is it a bitfield type? */
4297 if (is_Primitive_type(ent_type) &&
4298 get_primitive_base_type(ent_type) != NULL) {
4299 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4303 assert(get_type_mode(type) == get_tarval_mode(tv));
4304 ir_node *mem = get_store();
4305 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4306 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4310 case IR_INITIALIZER_COMPOUND: {
4311 assert(is_compound_type(type) || is_Array_type(type));
4313 if (is_Array_type(type)) {
4314 assert(has_array_upper_bound(type, 0));
4315 n_members = get_array_upper_bound_int(type, 0);
4317 n_members = get_compound_n_members(type);
4320 if (get_initializer_compound_n_entries(initializer)
4321 != (unsigned) n_members)
4322 panic("initializer doesn't match compound type");
4324 for (int i = 0; i < n_members; ++i) {
4327 ir_entity *sub_entity;
4328 if (is_Array_type(type)) {
4329 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4330 ir_node *cnst = new_d_Const(dbgi, index_tv);
4331 ir_node *in[1] = { cnst };
4332 irtype = get_array_element_type(type);
4333 sub_entity = get_array_element_entity(type);
4334 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4337 sub_entity = get_compound_member(type, i);
4338 irtype = get_entity_type(sub_entity);
4339 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4343 ir_initializer_t *sub_init
4344 = get_initializer_compound_value(initializer, i);
4346 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4353 panic("invalid IR_INITIALIZER found");
4356 static void create_dynamic_initializer(ir_initializer_t *initializer,
4357 dbg_info *dbgi, ir_entity *entity)
4359 ir_node *frame = get_irg_frame(current_ir_graph);
4360 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4361 ir_type *type = get_entity_type(entity);
4363 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4366 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4367 ir_entity *entity, type_t *type)
4369 ir_node *memory = get_store();
4370 ir_node *nomem = new_NoMem();
4371 ir_node *frame = get_irg_frame(current_ir_graph);
4372 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4374 if (initializer->kind == INITIALIZER_VALUE) {
4375 initializer_value_t *initializer_value = &initializer->value;
4377 ir_node *value = expression_to_firm(initializer_value->value);
4378 type = skip_typeref(type);
4379 assign_value(dbgi, addr, type, value);
4383 if (is_constant_initializer(initializer) == EXPR_CLASS_VARIABLE) {
4384 bool old_initializer_use_bitfield_basetype
4385 = initializer_use_bitfield_basetype;
4386 initializer_use_bitfield_basetype = true;
4387 ir_initializer_t *irinitializer
4388 = create_ir_initializer(initializer, type);
4389 initializer_use_bitfield_basetype
4390 = old_initializer_use_bitfield_basetype;
4392 create_dynamic_initializer(irinitializer, dbgi, entity);
4396 /* create the ir_initializer */
4397 ir_graph *const old_current_ir_graph = current_ir_graph;
4398 current_ir_graph = get_const_code_irg();
4400 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4402 assert(current_ir_graph == get_const_code_irg());
4403 current_ir_graph = old_current_ir_graph;
4405 /* create a "template" entity which is copied to the entity on the stack */
4406 ident *const id = id_unique("initializer.%u");
4407 ir_type *const irtype = get_ir_type(type);
4408 ir_type *const global_type = get_glob_type();
4409 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4410 set_entity_ld_ident(init_entity, id);
4412 set_entity_visibility(init_entity, ir_visibility_private);
4413 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4415 set_entity_initializer(init_entity, irinitializer);
4417 ir_node *const src_addr = create_symconst(dbgi, init_entity);
4418 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4420 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4421 set_store(copyb_mem);
4424 static void create_initializer_local_variable_entity(entity_t *entity)
4426 assert(entity->kind == ENTITY_VARIABLE);
4427 initializer_t *initializer = entity->variable.initializer;
4428 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4429 ir_entity *irentity = entity->variable.v.entity;
4430 type_t *type = entity->declaration.type;
4432 create_local_initializer(initializer, dbgi, irentity, type);
4435 static void create_variable_initializer(entity_t *entity)
4437 assert(entity->kind == ENTITY_VARIABLE);
4438 initializer_t *initializer = entity->variable.initializer;
4439 if (initializer == NULL)
4442 declaration_kind_t declaration_kind
4443 = (declaration_kind_t) entity->declaration.kind;
4444 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4445 create_initializer_local_variable_entity(entity);
4449 type_t *type = entity->declaration.type;
4450 type_qualifiers_t tq = get_type_qualifier(type, true);
4452 if (initializer->kind == INITIALIZER_VALUE) {
4453 initializer_value_t *initializer_value = &initializer->value;
4454 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4456 ir_node *value = expression_to_firm(initializer_value->value);
4458 type_t *type = initializer_value->value->base.type;
4459 ir_mode *mode = get_ir_mode_storage(type);
4460 value = create_conv(dbgi, value, mode);
4461 value = do_strict_conv(dbgi, value);
4463 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4464 set_value(entity->variable.v.value_number, value);
4466 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4468 ir_entity *irentity = entity->variable.v.entity;
4470 if (tq & TYPE_QUALIFIER_CONST
4471 && get_entity_owner(irentity) != get_tls_type()) {
4472 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4474 set_atomic_ent_value(irentity, value);
4477 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4478 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4480 ir_entity *irentity = entity->variable.v.entity;
4481 ir_initializer_t *irinitializer
4482 = create_ir_initializer(initializer, type);
4484 if (tq & TYPE_QUALIFIER_CONST) {
4485 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4487 set_entity_initializer(irentity, irinitializer);
4491 static void create_variable_length_array(entity_t *entity)
4493 assert(entity->kind == ENTITY_VARIABLE);
4494 assert(entity->variable.initializer == NULL);
4496 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4497 entity->variable.v.vla_base = NULL;
4499 /* TODO: record VLA somewhere so we create the free node when we leave
4503 static void allocate_variable_length_array(entity_t *entity)
4505 assert(entity->kind == ENTITY_VARIABLE);
4506 assert(entity->variable.initializer == NULL);
4507 assert(currently_reachable());
4509 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4510 type_t *type = entity->declaration.type;
4511 ir_type *el_type = get_ir_type(type->array.element_type);
4513 /* make sure size_node is calculated */
4514 get_type_size_node(type);
4515 ir_node *elems = type->array.size_node;
4516 ir_node *mem = get_store();
4517 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4519 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4520 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4523 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4524 entity->variable.v.vla_base = addr;
4528 * Creates a Firm local variable from a declaration.
4530 static void create_local_variable(entity_t *entity)
4532 assert(entity->kind == ENTITY_VARIABLE);
4533 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4535 bool needs_entity = entity->variable.address_taken;
4536 type_t *type = skip_typeref(entity->declaration.type);
4538 /* is it a variable length array? */
4539 if (is_type_array(type) && !type->array.size_constant) {
4540 create_variable_length_array(entity);
4542 } else if (is_type_array(type) || is_type_compound(type)) {
4543 needs_entity = true;
4544 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4545 needs_entity = true;
4549 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4550 create_variable_entity(entity,
4551 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4554 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4555 entity->variable.v.value_number = next_value_number_function;
4556 set_irg_loc_description(current_ir_graph, next_value_number_function,
4558 ++next_value_number_function;
4562 static void create_local_static_variable(entity_t *entity)
4564 assert(entity->kind == ENTITY_VARIABLE);
4565 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4567 type_t *type = skip_typeref(entity->declaration.type);
4568 ir_type *const var_type = entity->variable.thread_local ?
4569 get_tls_type() : get_glob_type();
4570 ir_type *const irtype = get_ir_type(type);
4571 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4573 size_t l = strlen(entity->base.symbol->string);
4574 char buf[l + sizeof(".%u")];
4575 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4576 ident *const id = id_unique(buf);
4577 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4579 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4580 set_entity_volatility(irentity, volatility_is_volatile);
4583 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4584 entity->variable.v.entity = irentity;
4586 set_entity_ld_ident(irentity, id);
4587 set_entity_visibility(irentity, ir_visibility_local);
4589 ir_graph *const old_current_ir_graph = current_ir_graph;
4590 current_ir_graph = get_const_code_irg();
4592 create_variable_initializer(entity);
4594 assert(current_ir_graph == get_const_code_irg());
4595 current_ir_graph = old_current_ir_graph;
4600 static void return_statement_to_firm(return_statement_t *statement)
4602 if (!currently_reachable())
4605 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4606 type_t *type = current_function_entity->declaration.type;
4607 ir_type *func_irtype = get_ir_type(type);
4612 if (get_method_n_ress(func_irtype) > 0) {
4613 ir_type *res_type = get_method_res_type(func_irtype, 0);
4615 if (statement->value != NULL) {
4616 ir_node *node = expression_to_firm(statement->value);
4617 if (!is_compound_type(res_type)) {
4618 type_t *type = statement->value->base.type;
4619 ir_mode *mode = get_ir_mode_storage(type);
4620 node = create_conv(dbgi, node, mode);
4621 node = do_strict_conv(dbgi, node);
4626 if (is_compound_type(res_type)) {
4629 mode = get_type_mode(res_type);
4631 in[0] = new_Unknown(mode);
4635 /* build return_value for its side effects */
4636 if (statement->value != NULL) {
4637 expression_to_firm(statement->value);
4642 ir_node *store = get_store();
4643 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4645 ir_node *end_block = get_irg_end_block(current_ir_graph);
4646 add_immBlock_pred(end_block, ret);
4648 set_unreachable_now();
4651 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4653 if (!currently_reachable())
4656 return expression_to_firm(statement->expression);
4659 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4661 entity_t *entity = compound->scope.entities;
4662 for ( ; entity != NULL; entity = entity->base.next) {
4663 if (!is_declaration(entity))
4666 create_local_declaration(entity);
4669 ir_node *result = NULL;
4670 statement_t *statement = compound->statements;
4671 for ( ; statement != NULL; statement = statement->base.next) {
4672 if (statement->base.next == NULL
4673 && statement->kind == STATEMENT_EXPRESSION) {
4674 result = expression_statement_to_firm(
4675 &statement->expression);
4678 statement_to_firm(statement);
4684 static void create_global_variable(entity_t *entity)
4686 ir_linkage linkage = IR_LINKAGE_DEFAULT;
4687 ir_visibility visibility = ir_visibility_default;
4688 ir_entity *irentity;
4689 assert(entity->kind == ENTITY_VARIABLE);
4691 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4692 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4693 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4694 case STORAGE_CLASS_NONE:
4695 visibility = ir_visibility_default;
4696 /* uninitialized globals get merged in C */
4697 if (entity->variable.initializer == NULL)
4698 linkage |= IR_LINKAGE_MERGE;
4700 case STORAGE_CLASS_TYPEDEF:
4701 case STORAGE_CLASS_AUTO:
4702 case STORAGE_CLASS_REGISTER:
4703 panic("invalid storage class for global var");
4706 ir_type *var_type = get_glob_type();
4707 if (entity->variable.thread_local) {
4708 var_type = get_tls_type();
4709 /* LINKAGE_MERGE not supported by current linkers */
4710 linkage &= ~IR_LINKAGE_MERGE;
4712 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4713 irentity = entity->variable.v.entity;
4714 add_entity_linkage(irentity, linkage);
4715 set_entity_visibility(irentity, visibility);
4718 static void create_local_declaration(entity_t *entity)
4720 assert(is_declaration(entity));
4722 /* construct type */
4723 (void) get_ir_type(entity->declaration.type);
4724 if (entity->base.symbol == NULL) {
4728 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4729 case STORAGE_CLASS_STATIC:
4730 if (entity->kind == ENTITY_FUNCTION) {
4731 (void)get_function_entity(entity, NULL);
4733 create_local_static_variable(entity);
4736 case STORAGE_CLASS_EXTERN:
4737 if (entity->kind == ENTITY_FUNCTION) {
4738 assert(entity->function.statement == NULL);
4739 (void)get_function_entity(entity, NULL);
4741 create_global_variable(entity);
4742 create_variable_initializer(entity);
4745 case STORAGE_CLASS_NONE:
4746 case STORAGE_CLASS_AUTO:
4747 case STORAGE_CLASS_REGISTER:
4748 if (entity->kind == ENTITY_FUNCTION) {
4749 if (entity->function.statement != NULL) {
4750 ir_type *owner = get_irg_frame_type(current_ir_graph);
4751 (void)get_function_entity(entity, owner);
4752 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4753 enqueue_inner_function(entity);
4755 (void)get_function_entity(entity, NULL);
4758 create_local_variable(entity);
4761 case STORAGE_CLASS_TYPEDEF:
4764 panic("invalid storage class found");
4767 static void initialize_local_declaration(entity_t *entity)
4769 if (entity->base.symbol == NULL)
4772 // no need to emit code in dead blocks
4773 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4774 && !currently_reachable())
4777 switch ((declaration_kind_t) entity->declaration.kind) {
4778 case DECLARATION_KIND_LOCAL_VARIABLE:
4779 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4780 create_variable_initializer(entity);
4783 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4784 allocate_variable_length_array(entity);
4787 case DECLARATION_KIND_COMPOUND_MEMBER:
4788 case DECLARATION_KIND_GLOBAL_VARIABLE:
4789 case DECLARATION_KIND_FUNCTION:
4790 case DECLARATION_KIND_INNER_FUNCTION:
4793 case DECLARATION_KIND_PARAMETER:
4794 case DECLARATION_KIND_PARAMETER_ENTITY:
4795 panic("can't initialize parameters");
4797 case DECLARATION_KIND_UNKNOWN:
4798 panic("can't initialize unknown declaration");
4800 panic("invalid declaration kind");
4803 static void declaration_statement_to_firm(declaration_statement_t *statement)
4805 entity_t *entity = statement->declarations_begin;
4809 entity_t *const last = statement->declarations_end;
4810 for ( ;; entity = entity->base.next) {
4811 if (is_declaration(entity)) {
4812 initialize_local_declaration(entity);
4813 } else if (entity->kind == ENTITY_TYPEDEF) {
4814 /* ยง6.7.7:3 Any array size expressions associated with variable length
4815 * array declarators are evaluated each time the declaration of the
4816 * typedef name is reached in the order of execution. */
4817 type_t *const type = skip_typeref(entity->typedefe.type);
4818 if (is_type_array(type) && type->array.is_vla)
4819 get_vla_size(&type->array);
4826 static void if_statement_to_firm(if_statement_t *statement)
4828 /* Create the condition. */
4829 ir_node *true_block = NULL;
4830 ir_node *false_block = NULL;
4831 if (currently_reachable()) {
4832 true_block = new_immBlock();
4833 false_block = new_immBlock();
4834 create_condition_evaluation(statement->condition, true_block, false_block);
4835 mature_immBlock(true_block);
4838 /* Create the false statement.
4839 * Handle false before true, so if no false statement is present, then the
4840 * empty false block is reused as fallthrough block. */
4841 ir_node *fallthrough_block = NULL;
4842 if (statement->false_statement != NULL) {
4843 if (false_block != NULL) {
4844 mature_immBlock(false_block);
4846 set_cur_block(false_block);
4847 statement_to_firm(statement->false_statement);
4848 if (currently_reachable()) {
4849 fallthrough_block = new_immBlock();
4850 add_immBlock_pred(fallthrough_block, new_Jmp());
4853 fallthrough_block = false_block;
4856 /* Create the true statement. */
4857 set_cur_block(true_block);
4858 statement_to_firm(statement->true_statement);
4859 if (currently_reachable()) {
4860 if (fallthrough_block == NULL) {
4861 fallthrough_block = new_immBlock();
4863 add_immBlock_pred(fallthrough_block, new_Jmp());
4866 /* Handle the block after the if-statement. */
4867 if (fallthrough_block != NULL) {
4868 mature_immBlock(fallthrough_block);
4870 set_cur_block(fallthrough_block);
4873 /* Create a jump node which jumps into target_block, if the current block is
4875 static void jump_if_reachable(ir_node *const target_block)
4877 if (currently_reachable()) {
4878 add_immBlock_pred(target_block, new_Jmp());
4882 static void while_statement_to_firm(while_statement_t *statement)
4884 /* Create the header block */
4885 ir_node *const header_block = new_immBlock();
4886 jump_if_reachable(header_block);
4888 /* Create the condition. */
4889 ir_node * body_block;
4890 ir_node * false_block;
4891 expression_t *const cond = statement->condition;
4892 if (is_constant_expression(cond) == EXPR_CLASS_CONSTANT &&
4893 fold_constant_to_bool(cond)) {
4894 /* Shortcut for while (true). */
4895 body_block = header_block;
4898 keep_alive(header_block);
4899 keep_all_memory(header_block);
4901 body_block = new_immBlock();
4902 false_block = new_immBlock();
4904 set_cur_block(header_block);
4905 create_condition_evaluation(cond, body_block, false_block);
4906 mature_immBlock(body_block);
4909 ir_node *const old_continue_label = continue_label;
4910 ir_node *const old_break_label = break_label;
4911 continue_label = header_block;
4912 break_label = false_block;
4914 /* Create the loop body. */
4915 set_cur_block(body_block);
4916 statement_to_firm(statement->body);
4917 jump_if_reachable(header_block);
4919 mature_immBlock(header_block);
4920 assert(false_block == NULL || false_block == break_label);
4921 false_block = break_label;
4922 if (false_block != NULL) {
4923 mature_immBlock(false_block);
4925 set_cur_block(false_block);
4927 assert(continue_label == header_block);
4928 continue_label = old_continue_label;
4929 break_label = old_break_label;
4932 static ir_node *get_break_label(void)
4934 if (break_label == NULL) {
4935 break_label = new_immBlock();
4940 static void do_while_statement_to_firm(do_while_statement_t *statement)
4942 /* create the header block */
4943 ir_node *header_block = new_immBlock();
4946 ir_node *body_block = new_immBlock();
4947 jump_if_reachable(body_block);
4949 ir_node *old_continue_label = continue_label;
4950 ir_node *old_break_label = break_label;
4951 continue_label = header_block;
4954 set_cur_block(body_block);
4955 statement_to_firm(statement->body);
4956 ir_node *const false_block = get_break_label();
4958 assert(continue_label == header_block);
4959 continue_label = old_continue_label;
4960 break_label = old_break_label;
4962 jump_if_reachable(header_block);
4964 /* create the condition */
4965 mature_immBlock(header_block);
4966 set_cur_block(header_block);
4968 create_condition_evaluation(statement->condition, body_block, false_block);
4969 mature_immBlock(body_block);
4970 mature_immBlock(false_block);
4972 set_cur_block(false_block);
4975 static void for_statement_to_firm(for_statement_t *statement)
4977 /* create declarations */
4978 entity_t *entity = statement->scope.entities;
4979 for ( ; entity != NULL; entity = entity->base.next) {
4980 if (!is_declaration(entity))
4983 create_local_declaration(entity);
4986 if (currently_reachable()) {
4987 entity = statement->scope.entities;
4988 for ( ; entity != NULL; entity = entity->base.next) {
4989 if (!is_declaration(entity))
4992 initialize_local_declaration(entity);
4995 if (statement->initialisation != NULL) {
4996 expression_to_firm(statement->initialisation);
5000 /* Create the header block */
5001 ir_node *const header_block = new_immBlock();
5002 jump_if_reachable(header_block);
5004 /* Create the condition. */
5005 ir_node *body_block;
5006 ir_node *false_block;
5007 if (statement->condition != NULL) {
5008 body_block = new_immBlock();
5009 false_block = new_immBlock();
5011 set_cur_block(header_block);
5012 create_condition_evaluation(statement->condition, body_block, false_block);
5013 mature_immBlock(body_block);
5016 body_block = header_block;
5019 keep_alive(header_block);
5020 keep_all_memory(header_block);
5023 /* Create the step block, if necessary. */
5024 ir_node * step_block = header_block;
5025 expression_t *const step = statement->step;
5027 step_block = new_immBlock();
5030 ir_node *const old_continue_label = continue_label;
5031 ir_node *const old_break_label = break_label;
5032 continue_label = step_block;
5033 break_label = false_block;
5035 /* Create the loop body. */
5036 set_cur_block(body_block);
5037 statement_to_firm(statement->body);
5038 jump_if_reachable(step_block);
5040 /* Create the step code. */
5042 mature_immBlock(step_block);
5043 set_cur_block(step_block);
5044 expression_to_firm(step);
5045 jump_if_reachable(header_block);
5048 mature_immBlock(header_block);
5049 assert(false_block == NULL || false_block == break_label);
5050 false_block = break_label;
5051 if (false_block != NULL) {
5052 mature_immBlock(false_block);
5054 set_cur_block(false_block);
5056 assert(continue_label == step_block);
5057 continue_label = old_continue_label;
5058 break_label = old_break_label;
5061 static void create_jump_statement(const statement_t *statement,
5062 ir_node *target_block)
5064 if (!currently_reachable())
5067 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5068 ir_node *jump = new_d_Jmp(dbgi);
5069 add_immBlock_pred(target_block, jump);
5071 set_unreachable_now();
5074 static void switch_statement_to_firm(switch_statement_t *statement)
5076 ir_node *first_block = NULL;
5077 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5078 ir_node *cond = NULL;
5080 if (currently_reachable()) {
5081 ir_node *expression = expression_to_firm(statement->expression);
5082 cond = new_d_Cond(dbgi, expression);
5083 first_block = get_cur_block();
5086 set_unreachable_now();
5088 ir_node *const old_switch_cond = current_switch_cond;
5089 ir_node *const old_break_label = break_label;
5090 const bool old_saw_default_label = saw_default_label;
5091 saw_default_label = false;
5092 current_switch_cond = cond;
5094 switch_statement_t *const old_switch = current_switch;
5095 current_switch = statement;
5097 /* determine a free number for the default label */
5098 unsigned long num_cases = 0;
5099 long default_proj_nr = 0;
5100 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5101 if (l->expression == NULL) {
5105 if (l->last_case >= l->first_case)
5106 num_cases += l->last_case - l->first_case + 1;
5107 if (l->last_case > default_proj_nr)
5108 default_proj_nr = l->last_case;
5111 if (default_proj_nr == INT_MAX) {
5112 /* Bad: an overflow will occur, we cannot be sure that the
5113 * maximum + 1 is a free number. Scan the values a second
5114 * time to find a free number.
5116 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5118 memset(bits, 0, (num_cases + 7) >> 3);
5119 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5120 if (l->expression == NULL) {
5124 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5125 if (start < num_cases && l->last_case >= 0) {
5126 unsigned long end = (unsigned long)l->last_case < num_cases ?
5127 (unsigned long)l->last_case : num_cases - 1;
5128 for (unsigned long cns = start; cns <= end; ++cns) {
5129 bits[cns >> 3] |= (1 << (cns & 7));
5133 /* We look at the first num_cases constants:
5134 * Either they are dense, so we took the last (num_cases)
5135 * one, or they are not dense, so we will find one free
5139 for (i = 0; i < num_cases; ++i)
5140 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5144 default_proj_nr = i;
5148 statement->default_proj_nr = default_proj_nr;
5149 /* safety check: cond might already be folded to a Bad */
5150 if (cond != NULL && is_Cond(cond)) {
5151 set_Cond_default_proj(cond, default_proj_nr);
5154 statement_to_firm(statement->body);
5156 jump_if_reachable(get_break_label());
5158 if (!saw_default_label && first_block != NULL) {
5159 set_cur_block(first_block);
5160 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5161 add_immBlock_pred(get_break_label(), proj);
5164 if (break_label != NULL) {
5165 mature_immBlock(break_label);
5167 set_cur_block(break_label);
5169 assert(current_switch_cond == cond);
5170 current_switch = old_switch;
5171 current_switch_cond = old_switch_cond;
5172 break_label = old_break_label;
5173 saw_default_label = old_saw_default_label;
5176 static void case_label_to_firm(const case_label_statement_t *statement)
5178 if (statement->is_empty_range)
5181 ir_node *block = new_immBlock();
5182 /* Fallthrough from previous case */
5183 jump_if_reachable(block);
5185 if (current_switch_cond != NULL) {
5186 set_cur_block(get_nodes_block(current_switch_cond));
5187 dbg_info *const dbgi = get_dbg_info(&statement->base.source_position);
5188 if (statement->expression != NULL) {
5189 long pn = statement->first_case;
5190 long end_pn = statement->last_case;
5191 assert(pn <= end_pn);
5192 /* create jumps for all cases in the given range */
5194 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5195 add_immBlock_pred(block, proj);
5196 } while (pn++ < end_pn);
5198 saw_default_label = true;
5199 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5200 current_switch->default_proj_nr);
5201 add_immBlock_pred(block, proj);
5205 mature_immBlock(block);
5206 set_cur_block(block);
5208 statement_to_firm(statement->statement);
5211 static void label_to_firm(const label_statement_t *statement)
5213 ir_node *block = get_label_block(statement->label);
5214 jump_if_reachable(block);
5216 set_cur_block(block);
5218 keep_all_memory(block);
5220 statement_to_firm(statement->statement);
5223 static void goto_to_firm(const goto_statement_t *statement)
5225 if (!currently_reachable())
5228 if (statement->expression) {
5229 ir_node *irn = expression_to_firm(statement->expression);
5230 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5231 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5233 set_irn_link(ijmp, ijmp_list);
5236 ir_node *block = get_label_block(statement->label);
5237 ir_node *jmp = new_Jmp();
5238 add_immBlock_pred(block, jmp);
5240 set_unreachable_now();
5243 static void asm_statement_to_firm(const asm_statement_t *statement)
5245 bool needs_memory = false;
5247 if (statement->is_volatile) {
5248 needs_memory = true;
5251 size_t n_clobbers = 0;
5252 asm_clobber_t *clobber = statement->clobbers;
5253 for ( ; clobber != NULL; clobber = clobber->next) {
5254 const char *clobber_str = clobber->clobber.begin;
5256 if (!be_is_valid_clobber(clobber_str)) {
5257 errorf(&statement->base.source_position,
5258 "invalid clobber '%s' specified", clobber->clobber);
5262 if (strcmp(clobber_str, "memory") == 0) {
5263 needs_memory = true;
5267 ident *id = new_id_from_str(clobber_str);
5268 obstack_ptr_grow(&asm_obst, id);
5271 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5272 ident **clobbers = NULL;
5273 if (n_clobbers > 0) {
5274 clobbers = obstack_finish(&asm_obst);
5277 size_t n_inputs = 0;
5278 asm_argument_t *argument = statement->inputs;
5279 for ( ; argument != NULL; argument = argument->next)
5281 size_t n_outputs = 0;
5282 argument = statement->outputs;
5283 for ( ; argument != NULL; argument = argument->next)
5286 unsigned next_pos = 0;
5288 ir_node *ins[n_inputs + n_outputs + 1];
5291 ir_asm_constraint tmp_in_constraints[n_outputs];
5293 const expression_t *out_exprs[n_outputs];
5294 ir_node *out_addrs[n_outputs];
5295 size_t out_size = 0;
5297 argument = statement->outputs;
5298 for ( ; argument != NULL; argument = argument->next) {
5299 const char *constraints = argument->constraints.begin;
5300 asm_constraint_flags_t asm_flags
5301 = be_parse_asm_constraints(constraints);
5303 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5304 warningf(&statement->base.source_position,
5305 "some constraints in '%s' are not supported", constraints);
5307 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5308 errorf(&statement->base.source_position,
5309 "some constraints in '%s' are invalid", constraints);
5312 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5313 errorf(&statement->base.source_position,
5314 "no write flag specified for output constraints '%s'",
5319 unsigned pos = next_pos++;
5320 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5321 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5322 expression_t *expr = argument->expression;
5323 ir_node *addr = expression_to_addr(expr);
5324 /* in+output, construct an artifical same_as constraint on the
5326 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5328 ir_node *value = get_value_from_lvalue(expr, addr);
5330 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5332 ir_asm_constraint constraint;
5333 constraint.pos = pos;
5334 constraint.constraint = new_id_from_str(buf);
5335 constraint.mode = get_ir_mode_storage(expr->base.type);
5336 tmp_in_constraints[in_size] = constraint;
5337 ins[in_size] = value;
5342 out_exprs[out_size] = expr;
5343 out_addrs[out_size] = addr;
5345 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5346 /* pure memory ops need no input (but we have to make sure we
5347 * attach to the memory) */
5348 assert(! (asm_flags &
5349 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5350 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5351 needs_memory = true;
5353 /* we need to attach the address to the inputs */
5354 expression_t *expr = argument->expression;
5356 ir_asm_constraint constraint;
5357 constraint.pos = pos;
5358 constraint.constraint = new_id_from_str(constraints);
5359 constraint.mode = NULL;
5360 tmp_in_constraints[in_size] = constraint;
5362 ins[in_size] = expression_to_addr(expr);
5366 errorf(&statement->base.source_position,
5367 "only modifiers but no place set in constraints '%s'",
5372 ir_asm_constraint constraint;
5373 constraint.pos = pos;
5374 constraint.constraint = new_id_from_str(constraints);
5375 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5377 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5379 assert(obstack_object_size(&asm_obst)
5380 == out_size * sizeof(ir_asm_constraint));
5381 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5384 obstack_grow(&asm_obst, tmp_in_constraints,
5385 in_size * sizeof(tmp_in_constraints[0]));
5386 /* find and count input and output arguments */
5387 argument = statement->inputs;
5388 for ( ; argument != NULL; argument = argument->next) {
5389 const char *constraints = argument->constraints.begin;
5390 asm_constraint_flags_t asm_flags
5391 = be_parse_asm_constraints(constraints);
5393 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5394 errorf(&statement->base.source_position,
5395 "some constraints in '%s' are not supported", constraints);
5398 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5399 errorf(&statement->base.source_position,
5400 "some constraints in '%s' are invalid", constraints);
5403 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5404 errorf(&statement->base.source_position,
5405 "write flag specified for input constraints '%s'",
5411 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5412 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5413 /* we can treat this as "normal" input */
5414 input = expression_to_firm(argument->expression);
5415 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5416 /* pure memory ops need no input (but we have to make sure we
5417 * attach to the memory) */
5418 assert(! (asm_flags &
5419 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5420 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5421 needs_memory = true;
5422 input = expression_to_addr(argument->expression);
5424 errorf(&statement->base.source_position,
5425 "only modifiers but no place set in constraints '%s'",
5430 ir_asm_constraint constraint;
5431 constraint.pos = next_pos++;
5432 constraint.constraint = new_id_from_str(constraints);
5433 constraint.mode = get_irn_mode(input);
5435 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5436 ins[in_size++] = input;
5440 ir_asm_constraint constraint;
5441 constraint.pos = next_pos++;
5442 constraint.constraint = new_id_from_str("");
5443 constraint.mode = mode_M;
5445 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5446 ins[in_size++] = get_store();
5449 assert(obstack_object_size(&asm_obst)
5450 == in_size * sizeof(ir_asm_constraint));
5451 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5453 /* create asm node */
5454 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5456 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5458 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5459 out_size, output_constraints,
5460 n_clobbers, clobbers, asm_text);
5462 if (statement->is_volatile) {
5463 set_irn_pinned(node, op_pin_state_pinned);
5465 set_irn_pinned(node, op_pin_state_floats);
5468 /* create output projs & connect them */
5470 ir_node *projm = new_Proj(node, mode_M, out_size);
5475 for (i = 0; i < out_size; ++i) {
5476 const expression_t *out_expr = out_exprs[i];
5478 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5479 ir_node *proj = new_Proj(node, mode, pn);
5480 ir_node *addr = out_addrs[i];
5482 set_value_for_expression_addr(out_expr, proj, addr);
5486 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5488 statement_to_firm(statement->try_statement);
5489 warningf(&statement->base.source_position, "structured exception handling ignored");
5492 static void leave_statement_to_firm(leave_statement_t *statement)
5494 errorf(&statement->base.source_position, "__leave not supported yet");
5498 * Transform a statement.
5500 static void statement_to_firm(statement_t *statement)
5503 assert(!statement->base.transformed);
5504 statement->base.transformed = true;
5507 switch (statement->kind) {
5508 case STATEMENT_INVALID:
5509 panic("invalid statement found");
5510 case STATEMENT_EMPTY:
5513 case STATEMENT_COMPOUND:
5514 compound_statement_to_firm(&statement->compound);
5516 case STATEMENT_RETURN:
5517 return_statement_to_firm(&statement->returns);
5519 case STATEMENT_EXPRESSION:
5520 expression_statement_to_firm(&statement->expression);
5523 if_statement_to_firm(&statement->ifs);
5525 case STATEMENT_WHILE:
5526 while_statement_to_firm(&statement->whiles);
5528 case STATEMENT_DO_WHILE:
5529 do_while_statement_to_firm(&statement->do_while);
5531 case STATEMENT_DECLARATION:
5532 declaration_statement_to_firm(&statement->declaration);
5534 case STATEMENT_BREAK:
5535 create_jump_statement(statement, get_break_label());
5537 case STATEMENT_CONTINUE:
5538 create_jump_statement(statement, continue_label);
5540 case STATEMENT_SWITCH:
5541 switch_statement_to_firm(&statement->switchs);
5543 case STATEMENT_CASE_LABEL:
5544 case_label_to_firm(&statement->case_label);
5547 for_statement_to_firm(&statement->fors);
5549 case STATEMENT_LABEL:
5550 label_to_firm(&statement->label);
5552 case STATEMENT_GOTO:
5553 goto_to_firm(&statement->gotos);
5556 asm_statement_to_firm(&statement->asms);
5558 case STATEMENT_MS_TRY:
5559 ms_try_statement_to_firm(&statement->ms_try);
5561 case STATEMENT_LEAVE:
5562 leave_statement_to_firm(&statement->leave);
5565 panic("statement not implemented");
5568 static int count_local_variables(const entity_t *entity,
5569 const entity_t *const last)
5572 entity_t const *const end = last != NULL ? last->base.next : NULL;
5573 for (; entity != end; entity = entity->base.next) {
5577 if (entity->kind == ENTITY_VARIABLE) {
5578 type = skip_typeref(entity->declaration.type);
5579 address_taken = entity->variable.address_taken;
5580 } else if (entity->kind == ENTITY_PARAMETER) {
5581 type = skip_typeref(entity->declaration.type);
5582 address_taken = entity->parameter.address_taken;
5587 if (!address_taken && is_type_scalar(type))
5593 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5595 int *const count = env;
5597 switch (stmt->kind) {
5598 case STATEMENT_DECLARATION: {
5599 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5600 *count += count_local_variables(decl_stmt->declarations_begin,
5601 decl_stmt->declarations_end);
5606 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5615 * Return the number of local (alias free) variables used by a function.
5617 static int get_function_n_local_vars(entity_t *entity)
5619 const function_t *function = &entity->function;
5622 /* count parameters */
5623 count += count_local_variables(function->parameters.entities, NULL);
5625 /* count local variables declared in body */
5626 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5631 * Build Firm code for the parameters of a function.
5633 static void initialize_function_parameters(entity_t *entity)
5635 assert(entity->kind == ENTITY_FUNCTION);
5636 ir_graph *irg = current_ir_graph;
5637 ir_node *args = get_irg_args(irg);
5638 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5639 int first_param_nr = 0;
5641 if (entity->function.need_closure) {
5642 /* add an extra parameter for the static link */
5643 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5648 entity_t *parameter = entity->function.parameters.entities;
5649 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5650 if (parameter->kind != ENTITY_PARAMETER)
5653 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5654 type_t *type = skip_typeref(parameter->declaration.type);
5656 bool needs_entity = parameter->parameter.address_taken;
5657 assert(!is_type_array(type));
5658 if (is_type_compound(type)) {
5659 needs_entity = true;
5663 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5664 ident *id = new_id_from_str(parameter->base.symbol->string);
5665 set_entity_ident(entity, id);
5667 parameter->declaration.kind
5668 = DECLARATION_KIND_PARAMETER_ENTITY;
5669 parameter->parameter.v.entity = entity;
5673 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5674 ir_mode *param_mode = get_type_mode(param_irtype);
5676 long pn = n + first_param_nr;
5677 ir_node *value = new_r_Proj(args, param_mode, pn);
5679 ir_mode *mode = get_ir_mode_storage(type);
5680 value = create_conv(NULL, value, mode);
5681 value = do_strict_conv(NULL, value);
5683 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5684 parameter->parameter.v.value_number = next_value_number_function;
5685 set_irg_loc_description(current_ir_graph, next_value_number_function,
5687 ++next_value_number_function;
5689 set_value(parameter->parameter.v.value_number, value);
5694 * Handle additional decl modifiers for IR-graphs
5696 * @param irg the IR-graph
5697 * @param dec_modifiers additional modifiers
5699 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5700 decl_modifiers_t decl_modifiers)
5702 if (decl_modifiers & DM_RETURNS_TWICE) {
5703 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5704 add_irg_additional_properties(irg, mtp_property_returns_twice);
5706 if (decl_modifiers & DM_NORETURN) {
5707 /* TRUE if the declaration includes the Microsoft
5708 __declspec(noreturn) specifier. */
5709 add_irg_additional_properties(irg, mtp_property_noreturn);
5711 if (decl_modifiers & DM_NOTHROW) {
5712 /* TRUE if the declaration includes the Microsoft
5713 __declspec(nothrow) specifier. */
5714 add_irg_additional_properties(irg, mtp_property_nothrow);
5716 if (decl_modifiers & DM_NAKED) {
5717 /* TRUE if the declaration includes the Microsoft
5718 __declspec(naked) specifier. */
5719 add_irg_additional_properties(irg, mtp_property_naked);
5721 if (decl_modifiers & DM_FORCEINLINE) {
5722 /* TRUE if the declaration includes the
5723 Microsoft __forceinline specifier. */
5724 set_irg_inline_property(irg, irg_inline_forced);
5726 if (decl_modifiers & DM_NOINLINE) {
5727 /* TRUE if the declaration includes the Microsoft
5728 __declspec(noinline) specifier. */
5729 set_irg_inline_property(irg, irg_inline_forbidden);
5733 static void add_function_pointer(ir_type *segment, ir_entity *method,
5734 const char *unique_template)
5736 ir_type *method_type = get_entity_type(method);
5737 ir_type *ptr_type = new_type_pointer(method_type);
5739 /* these entities don't really have a name but firm only allows
5741 * Note that we mustn't give these entities a name since for example
5742 * Mach-O doesn't allow them. */
5743 ident *ide = id_unique(unique_template);
5744 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5745 ir_graph *irg = get_const_code_irg();
5746 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5749 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5750 set_entity_compiler_generated(ptr, 1);
5751 set_entity_visibility(ptr, ir_visibility_private);
5752 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5753 set_atomic_ent_value(ptr, val);
5757 * Generate possible IJmp branches to a given label block.
5759 static void gen_ijmp_branches(ir_node *block)
5762 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5763 add_immBlock_pred(block, ijmp);
5768 * Create code for a function and all inner functions.
5770 * @param entity the function entity
5772 static void create_function(entity_t *entity)
5774 assert(entity->kind == ENTITY_FUNCTION);
5775 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5777 if (entity->function.statement == NULL)
5780 if (is_main(entity) && enable_main_collect2_hack) {
5781 prepare_main_collect2(entity);
5784 inner_functions = NULL;
5785 current_trampolines = NULL;
5787 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5788 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5789 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5791 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5792 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5793 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5796 current_function_entity = entity;
5797 current_function_name = NULL;
5798 current_funcsig = NULL;
5800 assert(all_labels == NULL);
5801 all_labels = NEW_ARR_F(label_t *, 0);
5804 int n_local_vars = get_function_n_local_vars(entity);
5805 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5806 current_ir_graph = irg;
5808 ir_graph *old_current_function = current_function;
5809 current_function = irg;
5811 set_irg_fp_model(irg, firm_opt.fp_model);
5812 tarval_enable_fp_ops(1);
5813 set_irn_dbg_info(get_irg_start_block(irg),
5814 get_entity_dbg_info(function_entity));
5816 ir_node *first_block = get_cur_block();
5818 /* set inline flags */
5819 if (entity->function.is_inline)
5820 set_irg_inline_property(irg, irg_inline_recomended);
5821 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5823 next_value_number_function = 0;
5824 initialize_function_parameters(entity);
5825 current_static_link = entity->function.static_link;
5827 statement_to_firm(entity->function.statement);
5829 ir_node *end_block = get_irg_end_block(irg);
5831 /* do we have a return statement yet? */
5832 if (currently_reachable()) {
5833 type_t *type = skip_typeref(entity->declaration.type);
5834 assert(is_type_function(type));
5835 const function_type_t *func_type = &type->function;
5836 const type_t *return_type
5837 = skip_typeref(func_type->return_type);
5840 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5841 ret = new_Return(get_store(), 0, NULL);
5844 if (is_type_scalar(return_type)) {
5845 mode = get_ir_mode_storage(func_type->return_type);
5851 /* ยง5.1.2.2.3 main implicitly returns 0 */
5852 if (is_main(entity)) {
5853 in[0] = new_Const(get_mode_null(mode));
5855 in[0] = new_Unknown(mode);
5857 ret = new_Return(get_store(), 1, in);
5859 add_immBlock_pred(end_block, ret);
5862 bool has_computed_gotos = false;
5863 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5864 label_t *label = all_labels[i];
5865 if (label->address_taken) {
5866 gen_ijmp_branches(label->block);
5867 has_computed_gotos = true;
5869 mature_immBlock(label->block);
5871 if (has_computed_gotos) {
5872 /* if we have computed goto's in the function, we cannot inline it */
5873 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5874 warningf(&entity->base.source_position,
5875 "function '%Y' can never be inlined because it contains a computed goto",
5876 entity->base.symbol);
5878 set_irg_inline_property(irg, irg_inline_forbidden);
5881 DEL_ARR_F(all_labels);
5884 mature_immBlock(first_block);
5885 mature_immBlock(end_block);
5887 irg_finalize_cons(irg);
5889 /* finalize the frame type */
5890 ir_type *frame_type = get_irg_frame_type(irg);
5891 int n = get_compound_n_members(frame_type);
5894 for (int i = 0; i < n; ++i) {
5895 ir_entity *entity = get_compound_member(frame_type, i);
5896 ir_type *entity_type = get_entity_type(entity);
5898 int align = get_type_alignment_bytes(entity_type);
5899 if (align > align_all)
5903 misalign = offset % align;
5905 offset += align - misalign;
5909 set_entity_offset(entity, offset);
5910 offset += get_type_size_bytes(entity_type);
5912 set_type_size_bytes(frame_type, offset);
5913 set_type_alignment_bytes(frame_type, align_all);
5915 irg_verify(irg, VERIFY_ENFORCE_SSA);
5916 current_function = old_current_function;
5918 if (current_trampolines != NULL) {
5919 DEL_ARR_F(current_trampolines);
5920 current_trampolines = NULL;
5923 /* create inner functions if any */
5924 entity_t **inner = inner_functions;
5925 if (inner != NULL) {
5926 ir_type *rem_outer_frame = current_outer_frame;
5927 current_outer_frame = get_irg_frame_type(current_ir_graph);
5928 ir_type *rem_outer_value_type = current_outer_value_type;
5929 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5930 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5931 create_function(inner[i]);
5935 current_outer_value_type = rem_outer_value_type;
5936 current_outer_frame = rem_outer_frame;
5940 static void scope_to_firm(scope_t *scope)
5942 /* first pass: create declarations */
5943 entity_t *entity = scope->entities;
5944 for ( ; entity != NULL; entity = entity->base.next) {
5945 if (entity->base.symbol == NULL)
5948 if (entity->kind == ENTITY_FUNCTION) {
5949 if (entity->function.btk != bk_none) {
5950 /* builtins have no representation */
5953 (void)get_function_entity(entity, NULL);
5954 } else if (entity->kind == ENTITY_VARIABLE) {
5955 create_global_variable(entity);
5956 } else if (entity->kind == ENTITY_NAMESPACE) {
5957 scope_to_firm(&entity->namespacee.members);
5961 /* second pass: create code/initializers */
5962 entity = scope->entities;
5963 for ( ; entity != NULL; entity = entity->base.next) {
5964 if (entity->base.symbol == NULL)
5967 if (entity->kind == ENTITY_FUNCTION) {
5968 if (entity->function.btk != bk_none) {
5969 /* builtins have no representation */
5972 create_function(entity);
5973 } else if (entity->kind == ENTITY_VARIABLE) {
5974 assert(entity->declaration.kind
5975 == DECLARATION_KIND_GLOBAL_VARIABLE);
5976 current_ir_graph = get_const_code_irg();
5977 create_variable_initializer(entity);
5982 void init_ast2firm(void)
5984 obstack_init(&asm_obst);
5985 init_atomic_modes();
5987 ir_set_debug_retrieve(dbg_retrieve);
5988 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
5990 /* create idents for all known runtime functions */
5991 for (size_t i = 0; i < lengthof(rts_data); ++i) {
5992 rts_idents[i] = new_id_from_str(rts_data[i].name);
5995 entitymap_init(&entitymap);
5998 static void init_ir_types(void)
6000 static int ir_types_initialized = 0;
6001 if (ir_types_initialized)
6003 ir_types_initialized = 1;
6005 ir_type_int = get_ir_type(type_int);
6006 ir_type_char = get_ir_type(type_char);
6007 ir_type_const_char = get_ir_type(type_const_char);
6008 ir_type_wchar_t = get_ir_type(type_wchar_t);
6009 ir_type_void = get_ir_type(type_void);
6011 be_params = be_get_backend_param();
6012 mode_float_arithmetic = be_params->mode_float_arithmetic;
6014 stack_param_align = be_params->stack_param_align;
6017 void exit_ast2firm(void)
6019 entitymap_destroy(&entitymap);
6020 obstack_free(&asm_obst, NULL);
6023 static void global_asm_to_firm(statement_t *s)
6025 for (; s != NULL; s = s->base.next) {
6026 assert(s->kind == STATEMENT_ASM);
6028 char const *const text = s->asms.asm_text.begin;
6029 size_t size = s->asms.asm_text.size;
6031 /* skip the last \0 */
6032 if (text[size - 1] == '\0')
6035 ident *const id = new_id_from_chars(text, size);
6040 void translation_unit_to_firm(translation_unit_t *unit)
6042 /* initialize firm arithmetic */
6043 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6044 ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6046 /* just to be sure */
6047 continue_label = NULL;
6049 current_switch_cond = NULL;
6050 current_translation_unit = unit;
6054 scope_to_firm(&unit->scope);
6055 global_asm_to_firm(unit->global_asm);
6057 current_ir_graph = NULL;
6058 current_translation_unit = NULL;