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 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
175 static ir_mode *mode_int, *mode_uint;
177 static ir_node *_expression_to_firm(const expression_t *expression);
178 static ir_node *expression_to_firm(const expression_t *expression);
179 static void create_local_declaration(entity_t *entity);
181 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
183 unsigned flags = get_atomic_type_flags(kind);
184 unsigned size = get_atomic_type_size(kind);
185 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
186 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
189 unsigned bit_size = size * 8;
190 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
191 unsigned modulo_shift;
192 ir_mode_arithmetic arithmetic;
194 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
195 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
196 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
198 sort = irms_int_number;
199 arithmetic = irma_twos_complement;
200 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
202 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
203 snprintf(name, sizeof(name), "F%u", bit_size);
204 sort = irms_float_number;
205 arithmetic = irma_ieee754;
208 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
216 * Initialises the atomic modes depending on the machine size.
218 static void init_atomic_modes(void)
220 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
221 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
223 mode_int = atomic_modes[ATOMIC_TYPE_INT];
224 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
226 /* there's no real void type in firm */
227 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
229 /* initialize pointer modes */
231 ir_mode_sort sort = irms_reference;
232 unsigned bit_size = machine_size;
234 ir_mode_arithmetic arithmetic = irma_twos_complement;
235 unsigned modulo_shift
236 = bit_size < machine_size ? machine_size : bit_size;
238 snprintf(name, sizeof(name), "p%u", machine_size);
239 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
242 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
243 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
245 /* Hmm, pointers should be machine size */
246 set_modeP_data(ptr_mode);
247 set_modeP_code(ptr_mode);
250 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
252 assert(kind <= ATOMIC_TYPE_LAST);
253 return atomic_modes[kind];
256 static ir_node *get_vla_size(array_type_t *const type)
258 ir_node *size_node = type->size_node;
259 if (size_node == NULL) {
260 size_node = expression_to_firm(type->size_expression);
261 type->size_node = size_node;
267 * Return a node representing the size of a type.
269 static ir_node *get_type_size_node(type_t *type)
271 type = skip_typeref(type);
273 if (is_type_array(type) && type->array.is_vla) {
274 ir_node *size_node = get_vla_size(&type->array);
275 ir_node *elem_size = get_type_size_node(type->array.element_type);
276 ir_mode *mode = get_irn_mode(size_node);
277 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
281 ir_mode *mode = get_ir_mode_storage(type_size_t);
283 sym.type_p = get_ir_type(type);
284 return new_SymConst(mode, sym, symconst_type_size);
287 static unsigned count_parameters(const function_type_t *function_type)
291 function_parameter_t *parameter = function_type->parameters;
292 for ( ; parameter != NULL; parameter = parameter->next) {
300 * Creates a Firm type for an atomic type
302 static ir_type *create_atomic_type(atomic_type_kind_t akind, const type_t *type)
304 ir_mode *mode = atomic_modes[akind];
305 type_dbg_info *dbgi = get_type_dbg_info_(type);
306 ir_type *irtype = new_d_type_primitive(mode, dbgi);
307 il_alignment_t alignment = get_atomic_type_alignment(akind);
309 set_type_alignment_bytes(irtype, alignment);
315 * Creates a Firm type for a complex type
317 static ir_type *create_complex_type(const complex_type_t *type)
319 atomic_type_kind_t kind = type->akind;
320 ir_mode *mode = atomic_modes[kind];
321 ident *id = get_mode_ident(mode);
325 /* FIXME: finish the array */
330 * Creates a Firm type for an imaginary type
332 static ir_type *create_imaginary_type(imaginary_type_t *type)
334 return create_atomic_type(type->akind, (const type_t*) type);
338 * return type of a parameter (and take transparent union gnu extension into
341 static type_t *get_parameter_type(type_t *orig_type)
343 type_t *type = skip_typeref(orig_type);
344 if (is_type_union(type)
345 && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
346 compound_t *compound = type->compound.compound;
347 type = compound->members.entities->declaration.type;
353 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
355 type_t *return_type = skip_typeref(function_type->return_type);
357 int n_parameters = count_parameters(function_type)
358 + (for_closure ? 1 : 0);
359 int n_results = return_type == type_void ? 0 : 1;
360 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) function_type);
361 ir_type *irtype = new_d_type_method(n_parameters, n_results, dbgi);
363 if (return_type != type_void) {
364 ir_type *restype = get_ir_type(return_type);
365 set_method_res_type(irtype, 0, restype);
368 function_parameter_t *parameter = function_type->parameters;
371 ir_type *p_irtype = get_ir_type(type_void_ptr);
372 set_method_param_type(irtype, n, p_irtype);
375 for ( ; parameter != NULL; parameter = parameter->next) {
376 type_t *type = get_parameter_type(parameter->type);
377 ir_type *p_irtype = get_ir_type(type);
378 set_method_param_type(irtype, n, p_irtype);
382 bool is_variadic = function_type->variadic;
385 set_method_variadicity(irtype, variadicity_variadic);
387 unsigned cc = get_method_calling_convention(irtype);
388 switch (function_type->calling_convention) {
389 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
392 set_method_calling_convention(irtype, SET_CDECL(cc));
399 /* only non-variadic function can use stdcall, else use cdecl */
400 set_method_calling_convention(irtype, SET_STDCALL(cc));
406 /* only non-variadic function can use fastcall, else use cdecl */
407 set_method_calling_convention(irtype, SET_FASTCALL(cc));
411 /* Hmm, leave default, not accepted by the parser yet. */
416 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
421 static ir_type *create_pointer_type(pointer_type_t *type)
423 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
424 type_t *points_to = type->points_to;
425 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
426 ir_type *ir_type = new_d_type_pointer(ir_points_to, dbgi);
431 static ir_type *create_reference_type(reference_type_t *type)
433 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
434 type_t *refers_to = type->refers_to;
435 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
436 ir_type *ir_type = new_d_type_pointer(ir_refers_to, dbgi);
441 static ir_type *create_array_type(array_type_t *type)
443 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
444 type_t *element_type = type->element_type;
445 ir_type *ir_element_type = get_ir_type(element_type);
446 ir_type *ir_type = new_d_type_array(1, ir_element_type, dbgi);
448 const int align = get_type_alignment_bytes(ir_element_type);
449 set_type_alignment_bytes(ir_type, align);
451 if (type->size_constant) {
452 int n_elements = type->size;
454 set_array_bounds_int(ir_type, 0, 0, n_elements);
456 size_t elemsize = get_type_size_bytes(ir_element_type);
457 if (elemsize % align > 0) {
458 elemsize += align - (elemsize % align);
460 set_type_size_bytes(ir_type, n_elements * elemsize);
462 set_array_lower_bound_int(ir_type, 0, 0);
464 set_type_state(ir_type, layout_fixed);
470 * Return the signed integer type of size bits.
472 * @param size the size
474 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
478 static ir_mode *s_modes[64 + 1] = {NULL, };
482 if (size <= 0 || size > 64)
485 mode = s_modes[size];
489 snprintf(name, sizeof(name), "bf_I%u", size);
490 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
491 size <= 32 ? 32 : size );
492 s_modes[size] = mode;
495 type_dbg_info *dbgi = get_type_dbg_info_(type);
496 res = new_d_type_primitive(mode, dbgi);
497 set_primitive_base_type(res, base_tp);
503 * Return the unsigned integer type of size bits.
505 * @param size the size
507 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
511 static ir_mode *u_modes[64 + 1] = {NULL, };
515 if (size <= 0 || size > 64)
518 mode = u_modes[size];
522 snprintf(name, sizeof(name), "bf_U%u", size);
523 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
524 size <= 32 ? 32 : size );
525 u_modes[size] = mode;
528 type_dbg_info *dbgi = get_type_dbg_info_(type);
529 res = new_d_type_primitive(mode, dbgi);
530 set_primitive_base_type(res, base_tp);
535 static ir_type *create_bitfield_type(bitfield_type_t *const type)
537 type_t *base = skip_typeref(type->base_type);
538 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
539 ir_type *irbase = get_ir_type(base);
541 unsigned size = type->bit_size;
543 assert(!is_type_float(base));
544 if (is_type_signed(base)) {
545 return get_signed_int_type_for_bit_size(irbase, size,
546 (const type_t*) type);
548 return get_unsigned_int_type_for_bit_size(irbase, size,
549 (const type_t*) type);
553 #define INVALID_TYPE ((ir_type_ptr)-1)
556 COMPOUND_IS_STRUCT = false,
557 COMPOUND_IS_UNION = true
561 * Construct firm type from ast struct type.
563 static ir_type *create_compound_type(compound_type_t *type,
564 bool incomplete, bool is_union)
566 compound_t *compound = type->compound;
568 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
569 return compound->irtype;
572 symbol_t *symbol = compound->base.symbol;
574 if (symbol != NULL) {
575 id = new_id_from_str(symbol->string);
578 id = id_unique("__anonymous_union.%u");
580 id = id_unique("__anonymous_struct.%u");
586 irtype = new_type_union(id);
588 irtype = new_type_struct(id);
591 compound->irtype_complete = false;
592 compound->irtype = irtype;
598 layout_union_type(type);
600 layout_struct_type(type);
603 compound->irtype_complete = true;
605 entity_t *entry = compound->members.entities;
606 for ( ; entry != NULL; entry = entry->base.next) {
607 if (entry->kind != ENTITY_COMPOUND_MEMBER)
610 symbol_t *symbol = entry->base.symbol;
611 type_t *entry_type = entry->declaration.type;
613 if (symbol == NULL) {
614 /* anonymous bitfield member, skip */
615 if (entry_type->kind == TYPE_BITFIELD)
617 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
618 || entry_type->kind == TYPE_COMPOUND_UNION);
619 ident = id_unique("anon.%u");
621 ident = new_id_from_str(symbol->string);
624 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
626 ir_type *entry_irtype = get_ir_type(entry_type);
627 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
629 set_entity_offset(entity, entry->compound_member.offset);
630 set_entity_offset_bits_remainder(entity,
631 entry->compound_member.bit_offset);
633 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
634 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
635 entry->compound_member.entity = entity;
638 set_type_alignment_bytes(irtype, compound->alignment);
639 set_type_size_bytes(irtype, compound->size);
640 set_type_state(irtype, layout_fixed);
645 static ir_type *create_enum_type(enum_type_t *const type)
647 type->base.firm_type = ir_type_int;
649 ir_mode *const mode = mode_int;
650 ir_tarval *const one = get_mode_one(mode);
651 ir_tarval * tv_next = get_mode_null(mode);
653 bool constant_folding_old = constant_folding;
654 constant_folding = true;
656 enum_t *enume = type->enume;
657 entity_t *entry = enume->base.next;
658 for (; entry != NULL; entry = entry->base.next) {
659 if (entry->kind != ENTITY_ENUM_VALUE)
662 expression_t *const init = entry->enum_value.value;
664 ir_node *const cnst = expression_to_firm(init);
665 if (!is_Const(cnst)) {
666 panic("couldn't fold constant");
668 tv_next = get_Const_tarval(cnst);
670 entry->enum_value.tv = tv_next;
671 tv_next = tarval_add(tv_next, one);
674 constant_folding = constant_folding_old;
676 return create_atomic_type(type->akind, (const type_t*) type);
679 static ir_type *get_ir_type_incomplete(type_t *type)
681 assert(type != NULL);
682 type = skip_typeref(type);
684 if (type->base.firm_type != NULL) {
685 assert(type->base.firm_type != INVALID_TYPE);
686 return type->base.firm_type;
689 switch (type->kind) {
690 case TYPE_COMPOUND_STRUCT:
691 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
692 case TYPE_COMPOUND_UNION:
693 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
695 return get_ir_type(type);
699 ir_type *get_ir_type(type_t *type)
701 assert(type != NULL);
703 type = skip_typeref(type);
705 if (type->base.firm_type != NULL) {
706 assert(type->base.firm_type != INVALID_TYPE);
707 return type->base.firm_type;
710 ir_type *firm_type = NULL;
711 switch (type->kind) {
713 /* Happens while constant folding, when there was an error */
714 return create_atomic_type(ATOMIC_TYPE_VOID, NULL);
717 firm_type = create_atomic_type(type->atomic.akind, type);
720 firm_type = create_complex_type(&type->complex);
723 firm_type = create_imaginary_type(&type->imaginary);
726 firm_type = create_method_type(&type->function, false);
729 firm_type = create_pointer_type(&type->pointer);
732 firm_type = create_reference_type(&type->reference);
735 firm_type = create_array_type(&type->array);
737 case TYPE_COMPOUND_STRUCT:
738 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
740 case TYPE_COMPOUND_UNION:
741 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
744 firm_type = create_enum_type(&type->enumt);
747 firm_type = get_ir_type(type->builtin.real_type);
750 firm_type = create_bitfield_type(&type->bitfield);
758 if (firm_type == NULL)
759 panic("unknown type found");
761 type->base.firm_type = firm_type;
765 static ir_mode *get_ir_mode_storage(type_t *type)
767 ir_type *irtype = get_ir_type(type);
769 /* firm doesn't report a mode for arrays somehow... */
770 if (is_Array_type(irtype)) {
774 ir_mode *mode = get_type_mode(irtype);
775 assert(mode != NULL);
779 static ir_mode *get_ir_mode_arithmetic(type_t *type)
781 ir_mode *mode = get_ir_mode_storage(type);
782 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
783 return mode_float_arithmetic;
789 /** Names of the runtime functions. */
790 static const struct {
791 int id; /**< the rts id */
792 int n_res; /**< number of return values */
793 const char *name; /**< the name of the rts function */
794 int n_params; /**< number of parameters */
795 unsigned flags; /**< language flags */
797 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
798 { rts_abort, 0, "abort", 0, _C89 },
799 { rts_alloca, 1, "alloca", 1, _ALL },
800 { rts_abs, 1, "abs", 1, _C89 },
801 { rts_labs, 1, "labs", 1, _C89 },
802 { rts_llabs, 1, "llabs", 1, _C99 },
803 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
805 { rts_fabs, 1, "fabs", 1, _C89 },
806 { rts_sqrt, 1, "sqrt", 1, _C89 },
807 { rts_cbrt, 1, "cbrt", 1, _C99 },
808 { rts_exp, 1, "exp", 1, _C89 },
809 { rts_exp2, 1, "exp2", 1, _C89 },
810 { rts_exp10, 1, "exp10", 1, _GNUC },
811 { rts_log, 1, "log", 1, _C89 },
812 { rts_log2, 1, "log2", 1, _C89 },
813 { rts_log10, 1, "log10", 1, _C89 },
814 { rts_pow, 1, "pow", 2, _C89 },
815 { rts_sin, 1, "sin", 1, _C89 },
816 { rts_cos, 1, "cos", 1, _C89 },
817 { rts_tan, 1, "tan", 1, _C89 },
818 { rts_asin, 1, "asin", 1, _C89 },
819 { rts_acos, 1, "acos", 1, _C89 },
820 { rts_atan, 1, "atan", 1, _C89 },
821 { rts_sinh, 1, "sinh", 1, _C89 },
822 { rts_cosh, 1, "cosh", 1, _C89 },
823 { rts_tanh, 1, "tanh", 1, _C89 },
825 { rts_fabsf, 1, "fabsf", 1, _C99 },
826 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
827 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
828 { rts_expf, 1, "expf", 1, _C99 },
829 { rts_exp2f, 1, "exp2f", 1, _C99 },
830 { rts_exp10f, 1, "exp10f", 1, _GNUC },
831 { rts_logf, 1, "logf", 1, _C99 },
832 { rts_log2f, 1, "log2f", 1, _C99 },
833 { rts_log10f, 1, "log10f", 1, _C99 },
834 { rts_powf, 1, "powf", 2, _C99 },
835 { rts_sinf, 1, "sinf", 1, _C99 },
836 { rts_cosf, 1, "cosf", 1, _C99 },
837 { rts_tanf, 1, "tanf", 1, _C99 },
838 { rts_asinf, 1, "asinf", 1, _C99 },
839 { rts_acosf, 1, "acosf", 1, _C99 },
840 { rts_atanf, 1, "atanf", 1, _C99 },
841 { rts_sinhf, 1, "sinhf", 1, _C99 },
842 { rts_coshf, 1, "coshf", 1, _C99 },
843 { rts_tanhf, 1, "tanhf", 1, _C99 },
845 { rts_fabsl, 1, "fabsl", 1, _C99 },
846 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
847 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
848 { rts_expl, 1, "expl", 1, _C99 },
849 { rts_exp2l, 1, "exp2l", 1, _C99 },
850 { rts_exp10l, 1, "exp10l", 1, _GNUC },
851 { rts_logl, 1, "logl", 1, _C99 },
852 { rts_log2l, 1, "log2l", 1, _C99 },
853 { rts_log10l, 1, "log10l", 1, _C99 },
854 { rts_powl, 1, "powl", 2, _C99 },
855 { rts_sinl, 1, "sinl", 1, _C99 },
856 { rts_cosl, 1, "cosl", 1, _C99 },
857 { rts_tanl, 1, "tanl", 1, _C99 },
858 { rts_asinl, 1, "asinl", 1, _C99 },
859 { rts_acosl, 1, "acosl", 1, _C99 },
860 { rts_atanl, 1, "atanl", 1, _C99 },
861 { rts_sinhl, 1, "sinhl", 1, _C99 },
862 { rts_coshl, 1, "coshl", 1, _C99 },
863 { rts_tanhl, 1, "tanhl", 1, _C99 },
865 { rts_strcmp, 1, "strcmp", 2, _C89 },
866 { rts_strncmp, 1, "strncmp", 3, _C89 },
867 { rts_strcpy, 1, "strcpy", 2, _C89 },
868 { rts_strlen, 1, "strlen", 1, _C89 },
869 { rts_memcpy, 1, "memcpy", 3, _C89 },
870 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
871 { rts_memmove, 1, "memmove", 3, _C89 },
872 { rts_memset, 1, "memset", 3, _C89 },
873 { rts_memcmp, 1, "memcmp", 3, _C89 },
876 static ident *rts_idents[lengthof(rts_data)];
878 static create_ld_ident_func create_ld_ident = create_name_linux_elf;
880 void set_create_ld_ident(ident *(*func)(entity_t*))
882 create_ld_ident = func;
886 * Handle GNU attributes for entities
888 * @param ent the entity
889 * @param decl the routine declaration
891 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
893 assert(is_declaration(entity));
894 decl_modifiers_t modifiers = entity->declaration.modifiers;
896 if (is_method_entity(irentity)) {
897 if (modifiers & DM_PURE) {
898 set_entity_additional_properties(irentity, mtp_property_pure);
900 if (modifiers & DM_CONST) {
901 add_entity_additional_properties(irentity, mtp_property_const);
904 if (modifiers & DM_USED) {
905 add_entity_linkage(irentity, IR_LINKAGE_HIDDEN_USER);
907 if (modifiers & DM_WEAK) {
908 add_entity_linkage(irentity, IR_LINKAGE_WEAK);
912 static bool is_main(entity_t *entity)
914 static symbol_t *sym_main = NULL;
915 if (sym_main == NULL) {
916 sym_main = symbol_table_insert("main");
919 if (entity->base.symbol != sym_main)
921 /* must be in outermost scope */
922 if (entity->base.parent_scope != ¤t_translation_unit->scope)
929 * Creates an entity representing a function.
931 * @param entity the function declaration/definition
932 * @param owner_type the owner type of this function, NULL
933 * for global functions
935 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
937 assert(entity->kind == ENTITY_FUNCTION);
938 if (entity->function.irentity != NULL) {
939 return entity->function.irentity;
942 entity_t *original_entity = entity;
943 if (entity->function.btk != bk_none) {
944 entity = get_builtin_replacement(entity);
949 if (is_main(entity)) {
950 /* force main to C linkage */
951 type_t *type = entity->declaration.type;
952 assert(is_type_function(type));
953 if (type->function.linkage != LINKAGE_C) {
954 type_t *new_type = duplicate_type(type);
955 new_type->function.linkage = LINKAGE_C;
956 type = identify_new_type(new_type);
957 entity->declaration.type = type;
961 symbol_t *symbol = entity->base.symbol;
962 ident *id = new_id_from_str(symbol->string);
964 /* already an entity defined? */
965 ir_entity *irentity = entitymap_get(&entitymap, symbol);
966 bool const has_body = entity->function.statement != NULL;
967 if (irentity != NULL) {
968 if (get_entity_visibility(irentity) == ir_visibility_external
970 set_entity_visibility(irentity, ir_visibility_default);
975 ir_type *ir_type_method;
976 if (entity->function.need_closure)
977 ir_type_method = create_method_type(&entity->declaration.type->function, true);
979 ir_type_method = get_ir_type(entity->declaration.type);
981 bool nested_function = false;
982 if (owner_type == NULL)
983 owner_type = get_glob_type();
985 nested_function = true;
987 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
988 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
992 ld_id = id_unique("inner.%u");
994 ld_id = create_ld_ident(entity);
995 set_entity_ld_ident(irentity, ld_id);
997 handle_decl_modifiers(irentity, entity);
999 if (! nested_function) {
1000 /* static inline => local
1001 * extern inline => local
1002 * inline without definition => local
1003 * inline with definition => external_visible */
1004 storage_class_tag_t const storage_class
1005 = (storage_class_tag_t) entity->declaration.storage_class;
1006 bool const is_inline = entity->function.is_inline;
1008 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1009 set_entity_visibility(irentity, ir_visibility_default);
1010 } else if (storage_class == STORAGE_CLASS_STATIC ||
1011 (is_inline && has_body)) {
1012 set_entity_visibility(irentity, ir_visibility_local);
1013 } else if (has_body) {
1014 set_entity_visibility(irentity, ir_visibility_default);
1016 set_entity_visibility(irentity, ir_visibility_external);
1019 /* nested functions are always local */
1020 set_entity_visibility(irentity, ir_visibility_local);
1023 /* We should check for file scope here, but as long as we compile C only
1024 this is not needed. */
1025 if (!freestanding && !has_body) {
1026 /* check for a known runtime function */
1027 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1028 if (id != rts_idents[i])
1031 function_type_t *function_type
1032 = &entity->declaration.type->function;
1033 /* rts_entities code can't handle a "wrong" number of parameters */
1034 if (function_type->unspecified_parameters)
1037 /* check number of parameters */
1038 int n_params = count_parameters(function_type);
1039 if (n_params != rts_data[i].n_params)
1042 type_t *return_type = skip_typeref(function_type->return_type);
1043 int n_res = return_type != type_void ? 1 : 0;
1044 if (n_res != rts_data[i].n_res)
1047 /* ignore those rts functions not necessary needed for current mode */
1048 if ((c_mode & rts_data[i].flags) == 0)
1050 assert(rts_entities[rts_data[i].id] == NULL);
1051 rts_entities[rts_data[i].id] = irentity;
1055 entitymap_insert(&entitymap, symbol, irentity);
1058 original_entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1059 original_entity->function.irentity = irentity;
1065 * Creates a SymConst for a given entity.
1067 * @param dbgi debug info
1068 * @param entity the entity
1070 static ir_node *create_symconst(dbg_info *dbgi, ir_entity *entity)
1072 assert(entity != NULL);
1073 union symconst_symbol sym;
1074 sym.entity_p = entity;
1075 return new_d_SymConst(dbgi, mode_P, sym, symconst_addr_ent);
1078 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1080 ir_mode *value_mode = get_irn_mode(value);
1082 if (value_mode == dest_mode)
1085 if (dest_mode == mode_b) {
1086 ir_node *zero = new_Const(get_mode_null(value_mode));
1087 ir_node *cmp = new_d_Cmp(dbgi, value, zero, ir_relation_less_greater);
1091 return new_d_Conv(dbgi, value, dest_mode);
1094 static ir_node *create_Const_from_bool(ir_mode *const mode, bool const v)
1096 return new_Const((v ? get_mode_one : get_mode_null)(mode));
1100 * Creates a SymConst node representing a wide string literal.
1102 * @param literal the wide string literal
1104 static ir_node *wide_string_literal_to_firm(
1105 const string_literal_expression_t *literal)
1107 ir_type *const global_type = get_glob_type();
1108 ir_type *const elem_type = ir_type_wchar_t;
1109 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1110 ir_type *const type = new_type_array(1, elem_type);
1112 ident *const id = id_unique("str.%u");
1113 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1114 set_entity_ld_ident(entity, id);
1115 set_entity_visibility(entity, ir_visibility_private);
1116 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1118 ir_mode *const mode = get_type_mode(elem_type);
1119 const size_t slen = wstrlen(&literal->value);
1121 set_array_lower_bound_int(type, 0, 0);
1122 set_array_upper_bound_int(type, 0, slen);
1123 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1124 set_type_state(type, layout_fixed);
1126 ir_initializer_t *initializer = create_initializer_compound(slen);
1127 const char *p = literal->value.begin;
1128 for (size_t i = 0; i < slen; ++i) {
1129 assert(p < literal->value.begin + literal->value.size);
1130 utf32 v = read_utf8_char(&p);
1131 ir_tarval *tv = new_tarval_from_long(v, mode);
1132 ir_initializer_t *val = create_initializer_tarval(tv);
1133 set_initializer_compound_value(initializer, i, val);
1135 set_entity_initializer(entity, initializer);
1137 return create_symconst(dbgi, entity);
1141 * Creates a SymConst node representing a string constant.
1143 * @param src_pos the source position of the string constant
1144 * @param id_prefix a prefix for the name of the generated string constant
1145 * @param value the value of the string constant
1147 static ir_node *string_to_firm(const source_position_t *const src_pos,
1148 const char *const id_prefix,
1149 const string_t *const value)
1151 ir_type *const global_type = get_glob_type();
1152 dbg_info *const dbgi = get_dbg_info(src_pos);
1153 ir_type *const type = new_type_array(1, ir_type_const_char);
1155 ident *const id = id_unique(id_prefix);
1156 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1157 set_entity_ld_ident(entity, id);
1158 set_entity_visibility(entity, ir_visibility_private);
1159 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1161 ir_type *const elem_type = ir_type_const_char;
1162 ir_mode *const mode = get_type_mode(elem_type);
1164 const char* const string = value->begin;
1165 const size_t slen = value->size;
1167 set_array_lower_bound_int(type, 0, 0);
1168 set_array_upper_bound_int(type, 0, slen);
1169 set_type_size_bytes(type, slen);
1170 set_type_state(type, layout_fixed);
1172 ir_initializer_t *initializer = create_initializer_compound(slen);
1173 for (size_t i = 0; i < slen; ++i) {
1174 ir_tarval *tv = new_tarval_from_long(string[i], mode);
1175 ir_initializer_t *val = create_initializer_tarval(tv);
1176 set_initializer_compound_value(initializer, i, val);
1178 set_entity_initializer(entity, initializer);
1180 return create_symconst(dbgi, entity);
1183 static bool try_create_integer(literal_expression_t *literal,
1184 type_t *type, unsigned char base)
1186 const char *string = literal->value.begin;
1187 size_t size = literal->value.size;
1189 assert(type->kind == TYPE_ATOMIC);
1190 atomic_type_kind_t akind = type->atomic.akind;
1192 ir_mode *mode = atomic_modes[akind];
1193 ir_tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1194 if (tv == tarval_bad)
1197 literal->base.type = type;
1198 literal->target_value = tv;
1202 static void create_integer_tarval(literal_expression_t *literal)
1206 symbol_t *suffix = literal->suffix;
1208 if (suffix != NULL) {
1209 for (const char *c = suffix->string; *c != '\0'; ++c) {
1210 if (*c == 'u' || *c == 'U') { ++us; }
1211 if (*c == 'l' || *c == 'L') { ++ls; }
1216 switch (literal->base.kind) {
1217 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1218 case EXPR_LITERAL_INTEGER: base = 10; break;
1219 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1220 default: panic("invalid literal kind");
1223 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1225 /* now try if the constant is small enough for some types */
1226 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1228 if (us == 0 && try_create_integer(literal, type_int, base))
1230 if ((us == 1 || base != 10)
1231 && try_create_integer(literal, type_unsigned_int, base))
1235 if (us == 0 && try_create_integer(literal, type_long, base))
1237 if ((us == 1 || base != 10)
1238 && try_create_integer(literal, type_unsigned_long, base))
1241 /* last try? then we should not report tarval_bad */
1242 if (us != 1 && base == 10)
1243 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1244 if (us == 0 && try_create_integer(literal, type_long_long, base))
1248 assert(us == 1 || base != 10);
1249 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1250 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1252 panic("internal error when parsing number literal");
1255 tarval_set_integer_overflow_mode(old_mode);
1258 void determine_literal_type(literal_expression_t *literal)
1260 switch (literal->base.kind) {
1261 case EXPR_LITERAL_INTEGER:
1262 case EXPR_LITERAL_INTEGER_OCTAL:
1263 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1264 create_integer_tarval(literal);
1272 * Creates a Const node representing a constant.
1274 static ir_node *literal_to_firm(const literal_expression_t *literal)
1276 type_t *type = skip_typeref(literal->base.type);
1277 ir_mode *mode = get_ir_mode_storage(type);
1278 const char *string = literal->value.begin;
1279 size_t size = literal->value.size;
1282 switch (literal->base.kind) {
1283 case EXPR_LITERAL_WIDE_CHARACTER: {
1284 utf32 v = read_utf8_char(&string);
1286 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1288 tv = new_tarval_from_str(buf, len, mode);
1291 case EXPR_LITERAL_CHARACTER: {
1293 if (size == 1 && char_is_signed) {
1294 v = (signed char)string[0];
1297 for (size_t i = 0; i < size; ++i) {
1298 v = (v << 8) | ((unsigned char)string[i]);
1302 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1304 tv = new_tarval_from_str(buf, len, mode);
1307 case EXPR_LITERAL_INTEGER:
1308 case EXPR_LITERAL_INTEGER_OCTAL:
1309 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1310 assert(literal->target_value != NULL);
1311 tv = literal->target_value;
1313 case EXPR_LITERAL_FLOATINGPOINT:
1314 tv = new_tarval_from_str(string, size, mode);
1316 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1317 char buffer[size + 2];
1318 memcpy(buffer, "0x", 2);
1319 memcpy(buffer+2, string, size);
1320 tv = new_tarval_from_str(buffer, size+2, mode);
1323 case EXPR_LITERAL_BOOLEAN:
1324 if (string[0] == 't') {
1325 tv = get_mode_one(mode);
1327 assert(string[0] == 'f');
1328 tv = get_mode_null(mode);
1331 case EXPR_LITERAL_MS_NOOP:
1332 tv = get_mode_null(mode);
1337 panic("Invalid literal kind found");
1340 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1341 ir_node *res = new_d_Const(dbgi, tv);
1342 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1343 return create_conv(dbgi, res, mode_arith);
1347 * Allocate an area of size bytes aligned at alignment
1350 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1352 static unsigned area_cnt = 0;
1355 ir_type *tp = new_type_array(1, ir_type_char);
1356 set_array_bounds_int(tp, 0, 0, size);
1357 set_type_alignment_bytes(tp, alignment);
1359 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1360 ident *name = new_id_from_str(buf);
1361 ir_entity *area = new_entity(frame_type, name, tp);
1363 /* mark this entity as compiler generated */
1364 set_entity_compiler_generated(area, 1);
1369 * Return a node representing a trampoline region
1370 * for a given function entity.
1372 * @param dbgi debug info
1373 * @param entity the function entity
1375 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1377 ir_entity *region = NULL;
1380 if (current_trampolines != NULL) {
1381 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1382 if (current_trampolines[i].function == entity) {
1383 region = current_trampolines[i].region;
1388 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1390 ir_graph *irg = current_ir_graph;
1391 if (region == NULL) {
1392 /* create a new region */
1393 ir_type *frame_tp = get_irg_frame_type(irg);
1394 trampoline_region reg;
1395 reg.function = entity;
1397 reg.region = alloc_trampoline(frame_tp,
1398 be_params->trampoline_size,
1399 be_params->trampoline_align);
1400 ARR_APP1(trampoline_region, current_trampolines, reg);
1401 region = reg.region;
1403 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1408 * Creates a trampoline for a function represented by an entity.
1410 * @param dbgi debug info
1411 * @param mode the (reference) mode for the function address
1412 * @param entity the function entity
1414 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1417 assert(entity != NULL);
1419 in[0] = get_trampoline_region(dbgi, entity);
1420 in[1] = create_symconst(dbgi, entity);
1421 in[2] = get_irg_frame(current_ir_graph);
1423 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1424 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1425 return new_Proj(irn, mode, pn_Builtin_1_result);
1429 * Dereference an address.
1431 * @param dbgi debug info
1432 * @param type the type of the dereferenced result (the points_to type)
1433 * @param addr the address to dereference
1435 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1436 ir_node *const addr)
1438 ir_type *irtype = get_ir_type(type);
1439 if (is_compound_type(irtype)
1440 || is_Method_type(irtype)
1441 || is_Array_type(irtype)) {
1445 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1446 ? cons_volatile : cons_none;
1447 ir_mode *const mode = get_type_mode(irtype);
1448 ir_node *const memory = get_store();
1449 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1450 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1451 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1453 set_store(load_mem);
1455 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1456 return create_conv(dbgi, load_res, mode_arithmetic);
1460 * Creates a strict Conv (to the node's mode) if necessary.
1462 * @param dbgi debug info
1463 * @param node the node to strict conv
1465 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1467 ir_mode *mode = get_irn_mode(node);
1469 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1471 if (!mode_is_float(mode))
1474 /* check if there is already a Conv */
1475 if (is_Conv(node)) {
1476 /* convert it into a strict Conv */
1477 set_Conv_strict(node, 1);
1481 /* otherwise create a new one */
1482 return new_d_strictConv(dbgi, node, mode);
1486 * Returns the correct base address depending on whether it is a parameter or a
1487 * normal local variable.
1489 static ir_node *get_local_frame(ir_entity *const ent)
1491 ir_graph *const irg = current_ir_graph;
1492 const ir_type *const owner = get_entity_owner(ent);
1493 if (owner == current_outer_frame || owner == current_outer_value_type) {
1494 assert(current_static_link != NULL);
1495 return current_static_link;
1497 return get_irg_frame(irg);
1502 * Keep all memory edges of the given block.
1504 static void keep_all_memory(ir_node *block)
1506 ir_node *old = get_cur_block();
1508 set_cur_block(block);
1509 keep_alive(get_store());
1510 /* TODO: keep all memory edges from restricted pointers */
1514 static ir_node *reference_expression_enum_value_to_firm(
1515 const reference_expression_t *ref)
1517 entity_t *entity = ref->entity;
1518 type_t *type = skip_typeref(entity->enum_value.enum_type);
1519 /* make sure the type is constructed */
1520 (void) get_ir_type(type);
1522 return new_Const(entity->enum_value.tv);
1525 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1527 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1528 entity_t *entity = ref->entity;
1529 assert(is_declaration(entity));
1530 type_t *type = skip_typeref(entity->declaration.type);
1532 /* make sure the type is constructed */
1533 (void) get_ir_type(type);
1535 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1536 ir_entity *irentity = get_function_entity(entity, NULL);
1537 /* for gcc compatibility we have to produce (dummy) addresses for some
1538 * builtins which don't have entities */
1539 if (irentity == NULL) {
1540 if (warning.other) {
1541 warningf(&ref->base.source_position,
1542 "taking address of builtin '%Y'",
1543 ref->entity->base.symbol);
1546 /* simply create a NULL pointer */
1547 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1548 ir_node *res = new_Const(get_mode_null(mode));
1554 switch ((declaration_kind_t) entity->declaration.kind) {
1555 case DECLARATION_KIND_UNKNOWN:
1558 case DECLARATION_KIND_LOCAL_VARIABLE: {
1559 ir_mode *const mode = get_ir_mode_storage(type);
1560 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1561 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1563 case DECLARATION_KIND_PARAMETER: {
1564 ir_mode *const mode = get_ir_mode_storage(type);
1565 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1566 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1568 case DECLARATION_KIND_FUNCTION: {
1569 return create_symconst(dbgi, entity->function.irentity);
1571 case DECLARATION_KIND_INNER_FUNCTION: {
1572 ir_mode *const mode = get_ir_mode_storage(type);
1573 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1574 /* inner function not using the closure */
1575 return create_symconst(dbgi, entity->function.irentity);
1577 /* need trampoline here */
1578 return create_trampoline(dbgi, mode, entity->function.irentity);
1581 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1582 const variable_t *variable = &entity->variable;
1583 ir_node *const addr = create_symconst(dbgi, variable->v.entity);
1584 return deref_address(dbgi, variable->base.type, addr);
1587 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1588 ir_entity *irentity = entity->variable.v.entity;
1589 ir_node *frame = get_local_frame(irentity);
1590 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1591 return deref_address(dbgi, entity->declaration.type, sel);
1593 case DECLARATION_KIND_PARAMETER_ENTITY: {
1594 ir_entity *irentity = entity->parameter.v.entity;
1595 ir_node *frame = get_local_frame(irentity);
1596 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1597 return deref_address(dbgi, entity->declaration.type, sel);
1600 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1601 return entity->variable.v.vla_base;
1603 case DECLARATION_KIND_COMPOUND_MEMBER:
1604 panic("not implemented reference type");
1607 panic("reference to declaration with unknown type found");
1610 static ir_node *reference_addr(const reference_expression_t *ref)
1612 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1613 entity_t *entity = ref->entity;
1614 assert(is_declaration(entity));
1616 switch((declaration_kind_t) entity->declaration.kind) {
1617 case DECLARATION_KIND_UNKNOWN:
1619 case DECLARATION_KIND_PARAMETER:
1620 case DECLARATION_KIND_LOCAL_VARIABLE:
1621 /* you can store to a local variable (so we don't panic but return NULL
1622 * as an indicator for no real address) */
1624 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1625 ir_node *const addr = create_symconst(dbgi, entity->variable.v.entity);
1628 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1629 ir_entity *irentity = entity->variable.v.entity;
1630 ir_node *frame = get_local_frame(irentity);
1631 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1635 case DECLARATION_KIND_PARAMETER_ENTITY: {
1636 ir_entity *irentity = entity->parameter.v.entity;
1637 ir_node *frame = get_local_frame(irentity);
1638 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1643 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1644 return entity->variable.v.vla_base;
1646 case DECLARATION_KIND_FUNCTION: {
1647 return create_symconst(dbgi, entity->function.irentity);
1650 case DECLARATION_KIND_INNER_FUNCTION: {
1651 type_t *const type = skip_typeref(entity->declaration.type);
1652 ir_mode *const mode = get_ir_mode_storage(type);
1653 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1654 /* inner function not using the closure */
1655 return create_symconst(dbgi, entity->function.irentity);
1657 /* need trampoline here */
1658 return create_trampoline(dbgi, mode, entity->function.irentity);
1662 case DECLARATION_KIND_COMPOUND_MEMBER:
1663 panic("not implemented reference type");
1666 panic("reference to declaration with unknown type found");
1670 * Generate an unary builtin.
1672 * @param kind the builtin kind to generate
1673 * @param op the operand
1674 * @param function_type the function type for the GNU builtin routine
1675 * @param db debug info
1677 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1680 in[0] = expression_to_firm(op);
1682 ir_type *tp = get_ir_type(function_type);
1683 ir_type *res = get_method_res_type(tp, 0);
1684 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1685 set_irn_pinned(irn, op_pin_state_floats);
1686 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1690 * Generate a pinned unary builtin.
1692 * @param kind the builtin kind to generate
1693 * @param op the operand
1694 * @param function_type the function type for the GNU builtin routine
1695 * @param db debug info
1697 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1698 type_t *function_type, dbg_info *db)
1701 in[0] = expression_to_firm(op);
1703 ir_type *tp = get_ir_type(function_type);
1704 ir_type *res = get_method_res_type(tp, 0);
1705 ir_node *mem = get_store();
1706 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1707 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1708 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1712 * Generate an binary-void-return builtin.
1714 * @param kind the builtin kind to generate
1715 * @param op1 the first operand
1716 * @param op2 the second operand
1717 * @param function_type the function type for the GNU builtin routine
1718 * @param db debug info
1720 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1721 expression_t *op2, type_t *function_type,
1725 in[0] = expression_to_firm(op1);
1726 in[1] = expression_to_firm(op2);
1728 ir_type *tp = get_ir_type(function_type);
1729 ir_node *mem = get_store();
1730 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1731 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1736 * Transform calls to builtin functions.
1738 static ir_node *process_builtin_call(const call_expression_t *call)
1740 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1742 assert(call->function->kind == EXPR_REFERENCE);
1743 reference_expression_t *builtin = &call->function->reference;
1745 type_t *type = skip_typeref(builtin->base.type);
1746 assert(is_type_pointer(type));
1748 type_t *function_type = skip_typeref(type->pointer.points_to);
1750 switch (builtin->entity->function.btk) {
1751 case bk_gnu_builtin_alloca: {
1752 if (call->arguments == NULL || call->arguments->next != NULL) {
1753 panic("invalid number of parameters on __builtin_alloca");
1755 expression_t *argument = call->arguments->expression;
1756 ir_node *size = expression_to_firm(argument);
1758 ir_node *store = get_store();
1759 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1761 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1763 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1768 case bk_gnu_builtin_huge_val:
1769 case bk_gnu_builtin_huge_valf:
1770 case bk_gnu_builtin_huge_vall:
1771 case bk_gnu_builtin_inf:
1772 case bk_gnu_builtin_inff:
1773 case bk_gnu_builtin_infl: {
1774 type_t *type = function_type->function.return_type;
1775 ir_mode *mode = get_ir_mode_arithmetic(type);
1776 ir_tarval *tv = get_mode_infinite(mode);
1777 ir_node *res = new_d_Const(dbgi, tv);
1780 case bk_gnu_builtin_nan:
1781 case bk_gnu_builtin_nanf:
1782 case bk_gnu_builtin_nanl: {
1783 /* Ignore string for now... */
1784 assert(is_type_function(function_type));
1785 type_t *type = function_type->function.return_type;
1786 ir_mode *mode = get_ir_mode_arithmetic(type);
1787 ir_tarval *tv = get_mode_NAN(mode);
1788 ir_node *res = new_d_Const(dbgi, tv);
1791 case bk_gnu_builtin_expect: {
1792 expression_t *argument = call->arguments->expression;
1793 return _expression_to_firm(argument);
1795 case bk_gnu_builtin_va_end:
1796 /* evaluate the argument of va_end for its side effects */
1797 _expression_to_firm(call->arguments->expression);
1799 case bk_gnu_builtin_frame_address: {
1800 expression_t *const expression = call->arguments->expression;
1801 bool val = fold_constant_to_bool(expression);
1804 return get_irg_frame(current_ir_graph);
1806 /* get the argument */
1809 in[0] = expression_to_firm(expression);
1810 in[1] = get_irg_frame(current_ir_graph);
1811 ir_type *tp = get_ir_type(function_type);
1812 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1813 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1816 case bk_gnu_builtin_return_address: {
1817 expression_t *const expression = call->arguments->expression;
1820 in[0] = expression_to_firm(expression);
1821 in[1] = get_irg_frame(current_ir_graph);
1822 ir_type *tp = get_ir_type(function_type);
1823 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1824 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1826 case bk_gnu_builtin_ffs:
1827 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1828 case bk_gnu_builtin_clz:
1829 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1830 case bk_gnu_builtin_ctz:
1831 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1832 case bk_gnu_builtin_popcount:
1833 case bk_ms__popcount:
1834 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1835 case bk_gnu_builtin_parity:
1836 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1837 case bk_gnu_builtin_prefetch: {
1838 call_argument_t *const args = call->arguments;
1839 expression_t *const addr = args->expression;
1842 in[0] = _expression_to_firm(addr);
1843 if (args->next != NULL) {
1844 expression_t *const rw = args->next->expression;
1846 in[1] = _expression_to_firm(rw);
1848 if (args->next->next != NULL) {
1849 expression_t *const locality = args->next->next->expression;
1851 in[2] = expression_to_firm(locality);
1853 in[2] = new_Const_long(mode_int, 3);
1856 in[1] = new_Const_long(mode_int, 0);
1857 in[2] = new_Const_long(mode_int, 3);
1859 ir_type *tp = get_ir_type(function_type);
1860 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1861 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1864 case bk_gnu_builtin_object_size: {
1865 /* determine value of "type" */
1866 expression_t *type_expression = call->arguments->next->expression;
1867 long type_val = fold_constant_to_int(type_expression);
1868 type_t *type = function_type->function.return_type;
1869 ir_mode *mode = get_ir_mode_arithmetic(type);
1870 /* just produce a "I don't know" result */
1871 ir_tarval *result = type_val & 2 ? get_mode_null(mode) :
1872 get_mode_minus_one(mode);
1874 return new_d_Const(dbgi, result);
1876 case bk_gnu_builtin_trap:
1879 ir_type *tp = get_ir_type(function_type);
1880 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1881 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1884 case bk_ms__debugbreak: {
1885 ir_type *tp = get_ir_type(function_type);
1886 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1887 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1890 case bk_ms_ReturnAddress: {
1893 in[0] = new_Const(get_mode_null(mode_int));
1894 in[1] = get_irg_frame(current_ir_graph);
1895 ir_type *tp = get_ir_type(function_type);
1896 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1897 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1900 case bk_ms_rotl64: {
1901 ir_node *val = expression_to_firm(call->arguments->expression);
1902 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1903 ir_mode *mode = get_irn_mode(val);
1904 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1907 case bk_ms_rotr64: {
1908 ir_node *val = expression_to_firm(call->arguments->expression);
1909 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1910 ir_mode *mode = get_irn_mode(val);
1911 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1912 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1913 return new_d_Rotl(dbgi, val, sub, mode);
1915 case bk_ms_byteswap_ushort:
1916 case bk_ms_byteswap_ulong:
1917 case bk_ms_byteswap_uint64:
1918 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1921 case bk_ms__indword:
1922 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1923 case bk_ms__outbyte:
1924 case bk_ms__outword:
1925 case bk_ms__outdword:
1926 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1927 call->arguments->next->expression, function_type, dbgi);
1929 panic("unsupported builtin found");
1934 * Transform a call expression.
1935 * Handles some special cases, like alloca() calls, which must be resolved
1936 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1937 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1940 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1942 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1943 assert(get_cur_block() != NULL);
1945 expression_t *function = call->function;
1946 if (function->kind == EXPR_REFERENCE) {
1947 const reference_expression_t *ref = &function->reference;
1948 entity_t *entity = ref->entity;
1950 if (entity->kind == ENTITY_FUNCTION) {
1951 ir_entity *irentity = entity->function.irentity;
1952 if (irentity == NULL)
1953 irentity = get_function_entity(entity, NULL);
1955 if (irentity == NULL && entity->function.btk != bk_none) {
1956 return process_builtin_call(call);
1960 if (irentity == rts_entities[rts_alloca]) {
1961 /* handle alloca() call */
1962 expression_t *argument = call->arguments->expression;
1963 ir_node *size = expression_to_firm(argument);
1964 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1966 size = create_conv(dbgi, size, mode);
1968 ir_node *store = get_store();
1969 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1970 firm_unknown_type, stack_alloc);
1971 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1973 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1980 ir_node *callee = expression_to_firm(function);
1982 type_t *type = skip_typeref(function->base.type);
1983 assert(is_type_pointer(type));
1984 pointer_type_t *pointer_type = &type->pointer;
1985 type_t *points_to = skip_typeref(pointer_type->points_to);
1986 assert(is_type_function(points_to));
1987 function_type_t *function_type = &points_to->function;
1989 int n_parameters = 0;
1990 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1991 ir_type *new_method_type = NULL;
1992 if (function_type->variadic || function_type->unspecified_parameters) {
1993 const call_argument_t *argument = call->arguments;
1994 for ( ; argument != NULL; argument = argument->next) {
1998 /* we need to construct a new method type matching the call
2000 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
2001 int n_res = get_method_n_ress(ir_method_type);
2002 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
2003 set_method_calling_convention(new_method_type,
2004 get_method_calling_convention(ir_method_type));
2005 set_method_additional_properties(new_method_type,
2006 get_method_additional_properties(ir_method_type));
2007 set_method_variadicity(new_method_type,
2008 get_method_variadicity(ir_method_type));
2010 for (int i = 0; i < n_res; ++i) {
2011 set_method_res_type(new_method_type, i,
2012 get_method_res_type(ir_method_type, i));
2014 argument = call->arguments;
2015 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2016 expression_t *expression = argument->expression;
2017 ir_type *irtype = get_ir_type(expression->base.type);
2018 set_method_param_type(new_method_type, i, irtype);
2020 ir_method_type = new_method_type;
2022 n_parameters = get_method_n_params(ir_method_type);
2025 ir_node *in[n_parameters];
2027 const call_argument_t *argument = call->arguments;
2028 for (int n = 0; n < n_parameters; ++n) {
2029 expression_t *expression = argument->expression;
2030 ir_node *arg_node = expression_to_firm(expression);
2032 type_t *type = skip_typeref(expression->base.type);
2033 if (!is_type_compound(type)) {
2034 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2035 arg_node = create_conv(dbgi, arg_node, mode);
2036 arg_node = do_strict_conv(dbgi, arg_node);
2041 argument = argument->next;
2044 ir_node *store = get_store();
2045 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2047 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2050 type_t *return_type = skip_typeref(function_type->return_type);
2051 ir_node *result = NULL;
2053 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2054 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2056 if (is_type_scalar(return_type)) {
2057 ir_mode *mode = get_ir_mode_storage(return_type);
2058 result = new_d_Proj(dbgi, resproj, mode, 0);
2059 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2060 result = create_conv(NULL, result, mode_arith);
2062 ir_mode *mode = mode_P_data;
2063 result = new_d_Proj(dbgi, resproj, mode, 0);
2067 if (function->kind == EXPR_REFERENCE &&
2068 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2069 /* A dead end: Keep the Call and the Block. Also place all further
2070 * nodes into a new and unreachable block. */
2072 keep_alive(get_cur_block());
2073 ir_node *block = new_Block(0, NULL);
2074 set_cur_block(block);
2080 static void statement_to_firm(statement_t *statement);
2081 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2083 static ir_node *expression_to_addr(const expression_t *expression);
2084 static ir_node *create_condition_evaluation(const expression_t *expression,
2085 ir_node *true_block,
2086 ir_node *false_block);
2088 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2091 if (!is_type_compound(type)) {
2092 ir_mode *mode = get_ir_mode_storage(type);
2093 value = create_conv(dbgi, value, mode);
2094 value = do_strict_conv(dbgi, value);
2097 ir_node *memory = get_store();
2099 if (is_type_scalar(type)) {
2100 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2101 ? cons_volatile : cons_none;
2102 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2103 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2104 set_store(store_mem);
2106 ir_type *irtype = get_ir_type(type);
2107 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2108 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2109 set_store(copyb_mem);
2113 static ir_tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2115 ir_tarval *all_one = get_mode_all_one(mode);
2116 int mode_size = get_mode_size_bits(mode);
2118 assert(offset >= 0);
2120 assert(offset + size <= mode_size);
2121 if (size == mode_size) {
2125 long shiftr = get_mode_size_bits(mode) - size;
2126 long shiftl = offset;
2127 ir_tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2128 ir_tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2129 ir_tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2130 ir_tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2135 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2136 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2138 ir_type *entity_type = get_entity_type(entity);
2139 ir_type *base_type = get_primitive_base_type(entity_type);
2140 assert(base_type != NULL);
2141 ir_mode *mode = get_type_mode(base_type);
2143 value = create_conv(dbgi, value, mode);
2145 /* kill upper bits of value and shift to right position */
2146 int bitoffset = get_entity_offset_bits_remainder(entity);
2147 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2149 ir_tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2150 ir_node *mask_node = new_d_Const(dbgi, mask);
2151 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2152 ir_tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2153 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2154 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2156 /* load current value */
2157 ir_node *mem = get_store();
2158 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2159 set_volatile ? cons_volatile : cons_none);
2160 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2161 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2162 ir_tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2163 ir_tarval *inv_mask = tarval_not(shift_mask);
2164 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2165 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2167 /* construct new value and store */
2168 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2169 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2170 set_volatile ? cons_volatile : cons_none);
2171 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2172 set_store(store_mem);
2174 return value_masked;
2177 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2180 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2181 type_t *type = expression->base.type;
2182 ir_mode *mode = get_ir_mode_storage(type);
2183 ir_node *mem = get_store();
2184 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2185 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2186 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2188 load_res = create_conv(dbgi, load_res, mode_int);
2190 set_store(load_mem);
2192 /* kill upper bits */
2193 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2194 ir_entity *entity = expression->compound_entry->compound_member.entity;
2195 int bitoffset = get_entity_offset_bits_remainder(entity);
2196 ir_type *entity_type = get_entity_type(entity);
2197 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2198 long shift_bitsl = machine_size - bitoffset - bitsize;
2199 assert(shift_bitsl >= 0);
2200 ir_tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2201 ir_node *countl = new_d_Const(dbgi, tvl);
2202 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2204 long shift_bitsr = bitoffset + shift_bitsl;
2205 assert(shift_bitsr <= (long) machine_size);
2206 ir_tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2207 ir_node *countr = new_d_Const(dbgi, tvr);
2209 if (mode_is_signed(mode)) {
2210 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2212 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2215 return create_conv(dbgi, shiftr, mode);
2218 /* make sure the selected compound type is constructed */
2219 static void construct_select_compound(const select_expression_t *expression)
2221 type_t *type = skip_typeref(expression->compound->base.type);
2222 if (is_type_pointer(type)) {
2223 type = type->pointer.points_to;
2225 (void) get_ir_type(type);
2228 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2229 ir_node *value, ir_node *addr)
2231 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2232 type_t *type = skip_typeref(expression->base.type);
2234 if (!is_type_compound(type)) {
2235 ir_mode *mode = get_ir_mode_storage(type);
2236 value = create_conv(dbgi, value, mode);
2237 value = do_strict_conv(dbgi, value);
2240 if (expression->kind == EXPR_REFERENCE) {
2241 const reference_expression_t *ref = &expression->reference;
2243 entity_t *entity = ref->entity;
2244 assert(is_declaration(entity));
2245 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2246 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2247 set_value(entity->variable.v.value_number, value);
2249 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2250 set_value(entity->parameter.v.value_number, value);
2256 addr = expression_to_addr(expression);
2257 assert(addr != NULL);
2259 if (expression->kind == EXPR_SELECT) {
2260 const select_expression_t *select = &expression->select;
2262 construct_select_compound(select);
2264 entity_t *entity = select->compound_entry;
2265 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2266 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2267 ir_entity *irentity = entity->compound_member.entity;
2269 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2270 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2276 assign_value(dbgi, addr, type, value);
2280 static void set_value_for_expression(const expression_t *expression,
2283 set_value_for_expression_addr(expression, value, NULL);
2286 static ir_node *get_value_from_lvalue(const expression_t *expression,
2289 if (expression->kind == EXPR_REFERENCE) {
2290 const reference_expression_t *ref = &expression->reference;
2292 entity_t *entity = ref->entity;
2293 assert(entity->kind == ENTITY_VARIABLE
2294 || entity->kind == ENTITY_PARAMETER);
2295 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2297 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2298 value_number = entity->variable.v.value_number;
2299 assert(addr == NULL);
2300 type_t *type = skip_typeref(expression->base.type);
2301 ir_mode *mode = get_ir_mode_storage(type);
2302 ir_node *res = get_value(value_number, mode);
2303 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2304 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2305 value_number = entity->parameter.v.value_number;
2306 assert(addr == NULL);
2307 type_t *type = skip_typeref(expression->base.type);
2308 ir_mode *mode = get_ir_mode_storage(type);
2309 ir_node *res = get_value(value_number, mode);
2310 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2314 assert(addr != NULL);
2315 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2318 if (expression->kind == EXPR_SELECT &&
2319 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2320 construct_select_compound(&expression->select);
2321 value = bitfield_extract_to_firm(&expression->select, addr);
2323 value = deref_address(dbgi, expression->base.type, addr);
2330 static ir_node *create_incdec(const unary_expression_t *expression)
2332 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2333 const expression_t *value_expr = expression->value;
2334 ir_node *addr = expression_to_addr(value_expr);
2335 ir_node *value = get_value_from_lvalue(value_expr, addr);
2337 type_t *type = skip_typeref(expression->base.type);
2338 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2341 if (is_type_pointer(type)) {
2342 pointer_type_t *pointer_type = &type->pointer;
2343 offset = get_type_size_node(pointer_type->points_to);
2345 assert(is_type_arithmetic(type));
2346 offset = new_Const(get_mode_one(mode));
2350 ir_node *store_value;
2351 switch(expression->base.kind) {
2352 case EXPR_UNARY_POSTFIX_INCREMENT:
2354 store_value = new_d_Add(dbgi, value, offset, mode);
2356 case EXPR_UNARY_POSTFIX_DECREMENT:
2358 store_value = new_d_Sub(dbgi, value, offset, mode);
2360 case EXPR_UNARY_PREFIX_INCREMENT:
2361 result = new_d_Add(dbgi, value, offset, mode);
2362 store_value = result;
2364 case EXPR_UNARY_PREFIX_DECREMENT:
2365 result = new_d_Sub(dbgi, value, offset, mode);
2366 store_value = result;
2369 panic("no incdec expr in create_incdec");
2372 set_value_for_expression_addr(value_expr, store_value, addr);
2377 static bool is_local_variable(expression_t *expression)
2379 if (expression->kind != EXPR_REFERENCE)
2381 reference_expression_t *ref_expr = &expression->reference;
2382 entity_t *entity = ref_expr->entity;
2383 if (entity->kind != ENTITY_VARIABLE)
2385 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2386 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2389 static ir_relation get_relation(const expression_kind_t kind)
2392 case EXPR_BINARY_EQUAL: return ir_relation_equal;
2393 case EXPR_BINARY_ISLESSGREATER: return ir_relation_less_greater;
2394 case EXPR_BINARY_NOTEQUAL: return ir_relation_unordered_less_greater;
2395 case EXPR_BINARY_ISLESS:
2396 case EXPR_BINARY_LESS: return ir_relation_less;
2397 case EXPR_BINARY_ISLESSEQUAL:
2398 case EXPR_BINARY_LESSEQUAL: return ir_relation_less_equal;
2399 case EXPR_BINARY_ISGREATER:
2400 case EXPR_BINARY_GREATER: return ir_relation_greater;
2401 case EXPR_BINARY_ISGREATEREQUAL:
2402 case EXPR_BINARY_GREATEREQUAL: return ir_relation_greater_equal;
2403 case EXPR_BINARY_ISUNORDERED: return ir_relation_unordered;
2408 panic("trying to get pn_Cmp from non-comparison binexpr type");
2412 * Handle the assume optimizer hint: check if a Confirm
2413 * node can be created.
2415 * @param dbi debug info
2416 * @param expr the IL assume expression
2418 * we support here only some simple cases:
2423 static ir_node *handle_assume_compare(dbg_info *dbi,
2424 const binary_expression_t *expression)
2426 expression_t *op1 = expression->left;
2427 expression_t *op2 = expression->right;
2428 entity_t *var2, *var = NULL;
2429 ir_node *res = NULL;
2430 ir_relation relation = get_relation(expression->base.kind);
2432 if (is_local_variable(op1) && is_local_variable(op2)) {
2433 var = op1->reference.entity;
2434 var2 = op2->reference.entity;
2436 type_t *const type = skip_typeref(var->declaration.type);
2437 ir_mode *const mode = get_ir_mode_storage(type);
2439 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2440 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2442 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_relation(relation));
2443 set_value(var2->variable.v.value_number, res);
2445 res = new_d_Confirm(dbi, irn1, irn2, relation);
2446 set_value(var->variable.v.value_number, res);
2452 if (is_local_variable(op1) && is_constant_expression(op2) == EXPR_CLASS_CONSTANT) {
2453 var = op1->reference.entity;
2455 } else if (is_constant_expression(op1) == EXPR_CLASS_CONSTANT && is_local_variable(op2)) {
2456 relation = get_inversed_relation(relation);
2457 var = op2->reference.entity;
2462 type_t *const type = skip_typeref(var->declaration.type);
2463 ir_mode *const mode = get_ir_mode_storage(type);
2465 res = get_value(var->variable.v.value_number, mode);
2466 res = new_d_Confirm(dbi, res, expression_to_firm(con), relation);
2467 set_value(var->variable.v.value_number, res);
2473 * Handle the assume optimizer hint.
2475 * @param dbi debug info
2476 * @param expr the IL assume expression
2478 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2480 switch(expression->kind) {
2481 case EXPR_BINARY_EQUAL:
2482 case EXPR_BINARY_NOTEQUAL:
2483 case EXPR_BINARY_LESS:
2484 case EXPR_BINARY_LESSEQUAL:
2485 case EXPR_BINARY_GREATER:
2486 case EXPR_BINARY_GREATEREQUAL:
2487 return handle_assume_compare(dbi, &expression->binary);
2493 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2494 type_t *from_type, type_t *type)
2496 type = skip_typeref(type);
2497 if (type == type_void) {
2498 /* make sure firm type is constructed */
2499 (void) get_ir_type(type);
2502 if (!is_type_scalar(type)) {
2503 /* make sure firm type is constructed */
2504 (void) get_ir_type(type);
2508 from_type = skip_typeref(from_type);
2509 ir_mode *mode = get_ir_mode_storage(type);
2510 /* check for conversion from / to __based types */
2511 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2512 const variable_t *from_var = from_type->pointer.base_variable;
2513 const variable_t *to_var = type->pointer.base_variable;
2514 if (from_var != to_var) {
2515 if (from_var != NULL) {
2516 ir_node *const addr = create_symconst(dbgi, from_var->v.entity);
2517 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2518 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2520 if (to_var != NULL) {
2521 ir_node *const addr = create_symconst(dbgi, to_var->v.entity);
2522 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2523 value_node = new_d_Sub(dbgi, value_node, base, mode);
2528 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2529 /* bool adjustments (we save a mode_Bu, but have to temporarily
2530 * convert to mode_b so we only get a 0/1 value */
2531 value_node = create_conv(dbgi, value_node, mode_b);
2534 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2535 ir_node *node = create_conv(dbgi, value_node, mode);
2536 node = do_strict_conv(dbgi, node);
2537 node = create_conv(dbgi, node, mode_arith);
2542 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2544 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2545 type_t *type = skip_typeref(expression->base.type);
2547 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2548 return expression_to_addr(expression->value);
2550 const expression_t *value = expression->value;
2552 switch(expression->base.kind) {
2553 case EXPR_UNARY_NEGATE: {
2554 ir_node *value_node = expression_to_firm(value);
2555 ir_mode *mode = get_ir_mode_arithmetic(type);
2556 return new_d_Minus(dbgi, value_node, mode);
2558 case EXPR_UNARY_PLUS:
2559 return expression_to_firm(value);
2560 case EXPR_UNARY_BITWISE_NEGATE: {
2561 ir_node *value_node = expression_to_firm(value);
2562 ir_mode *mode = get_ir_mode_arithmetic(type);
2563 return new_d_Not(dbgi, value_node, mode);
2565 case EXPR_UNARY_NOT: {
2566 ir_node *value_node = _expression_to_firm(value);
2567 value_node = create_conv(dbgi, value_node, mode_b);
2568 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2571 case EXPR_UNARY_DEREFERENCE: {
2572 ir_node *value_node = expression_to_firm(value);
2573 type_t *value_type = skip_typeref(value->base.type);
2574 assert(is_type_pointer(value_type));
2576 /* check for __based */
2577 const variable_t *const base_var = value_type->pointer.base_variable;
2578 if (base_var != NULL) {
2579 ir_node *const addr = create_symconst(dbgi, base_var->v.entity);
2580 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2581 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2583 type_t *points_to = value_type->pointer.points_to;
2584 return deref_address(dbgi, points_to, value_node);
2586 case EXPR_UNARY_POSTFIX_INCREMENT:
2587 case EXPR_UNARY_POSTFIX_DECREMENT:
2588 case EXPR_UNARY_PREFIX_INCREMENT:
2589 case EXPR_UNARY_PREFIX_DECREMENT:
2590 return create_incdec(expression);
2591 case EXPR_UNARY_CAST_IMPLICIT:
2592 case EXPR_UNARY_CAST: {
2593 ir_node *value_node = expression_to_firm(value);
2594 type_t *from_type = value->base.type;
2595 return create_cast(dbgi, value_node, from_type, type);
2597 case EXPR_UNARY_ASSUME:
2598 return handle_assume(dbgi, value);
2603 panic("invalid UNEXPR type found");
2607 * produces a 0/1 depending of the value of a mode_b node
2609 static ir_node *produce_condition_result(const expression_t *expression,
2610 ir_mode *mode, dbg_info *dbgi)
2612 ir_node *const one_block = new_immBlock();
2613 ir_node *const zero_block = new_immBlock();
2614 create_condition_evaluation(expression, one_block, zero_block);
2615 mature_immBlock(one_block);
2616 mature_immBlock(zero_block);
2618 ir_node *const jmp_one = new_rd_Jmp(dbgi, one_block);
2619 ir_node *const jmp_zero = new_rd_Jmp(dbgi, zero_block);
2620 ir_node *const in_cf[2] = { jmp_one, jmp_zero };
2621 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
2622 set_cur_block(block);
2624 ir_node *const one = new_Const(get_mode_one(mode));
2625 ir_node *const zero = new_Const(get_mode_null(mode));
2626 ir_node *const in[2] = { one, zero };
2627 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
2632 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2633 ir_node *value, type_t *type)
2635 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2636 assert(is_type_pointer(type));
2637 pointer_type_t *const pointer_type = &type->pointer;
2638 type_t *const points_to = skip_typeref(pointer_type->points_to);
2639 ir_node * elem_size = get_type_size_node(points_to);
2640 elem_size = create_conv(dbgi, elem_size, mode);
2641 value = create_conv(dbgi, value, mode);
2642 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2646 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2647 ir_node *left, ir_node *right)
2650 type_t *type_left = skip_typeref(expression->left->base.type);
2651 type_t *type_right = skip_typeref(expression->right->base.type);
2653 expression_kind_t kind = expression->base.kind;
2656 case EXPR_BINARY_SHIFTLEFT:
2657 case EXPR_BINARY_SHIFTRIGHT:
2658 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2659 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2660 mode = get_ir_mode_arithmetic(expression->base.type);
2661 right = create_conv(dbgi, right, mode_uint);
2664 case EXPR_BINARY_SUB:
2665 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2666 const pointer_type_t *const ptr_type = &type_left->pointer;
2668 mode = get_ir_mode_arithmetic(expression->base.type);
2669 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2670 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2671 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2672 ir_node *const no_mem = new_NoMem();
2673 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2674 mode, op_pin_state_floats);
2675 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2678 case EXPR_BINARY_SUB_ASSIGN:
2679 if (is_type_pointer(type_left)) {
2680 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2681 mode = get_ir_mode_arithmetic(type_left);
2686 case EXPR_BINARY_ADD:
2687 case EXPR_BINARY_ADD_ASSIGN:
2688 if (is_type_pointer(type_left)) {
2689 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2690 mode = get_ir_mode_arithmetic(type_left);
2692 } else if (is_type_pointer(type_right)) {
2693 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2694 mode = get_ir_mode_arithmetic(type_right);
2701 mode = get_ir_mode_arithmetic(type_right);
2702 left = create_conv(dbgi, left, mode);
2707 case EXPR_BINARY_ADD_ASSIGN:
2708 case EXPR_BINARY_ADD:
2709 return new_d_Add(dbgi, left, right, mode);
2710 case EXPR_BINARY_SUB_ASSIGN:
2711 case EXPR_BINARY_SUB:
2712 return new_d_Sub(dbgi, left, right, mode);
2713 case EXPR_BINARY_MUL_ASSIGN:
2714 case EXPR_BINARY_MUL:
2715 return new_d_Mul(dbgi, left, right, mode);
2716 case EXPR_BINARY_BITWISE_AND:
2717 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2718 return new_d_And(dbgi, left, right, mode);
2719 case EXPR_BINARY_BITWISE_OR:
2720 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2721 return new_d_Or(dbgi, left, right, mode);
2722 case EXPR_BINARY_BITWISE_XOR:
2723 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2724 return new_d_Eor(dbgi, left, right, mode);
2725 case EXPR_BINARY_SHIFTLEFT:
2726 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2727 return new_d_Shl(dbgi, left, right, mode);
2728 case EXPR_BINARY_SHIFTRIGHT:
2729 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2730 if (mode_is_signed(mode)) {
2731 return new_d_Shrs(dbgi, left, right, mode);
2733 return new_d_Shr(dbgi, left, right, mode);
2735 case EXPR_BINARY_DIV:
2736 case EXPR_BINARY_DIV_ASSIGN: {
2737 ir_node *pin = new_Pin(new_NoMem());
2738 ir_node *op = new_d_Div(dbgi, pin, left, right, mode,
2739 op_pin_state_floats);
2740 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2743 case EXPR_BINARY_MOD:
2744 case EXPR_BINARY_MOD_ASSIGN: {
2745 ir_node *pin = new_Pin(new_NoMem());
2746 assert(!mode_is_float(mode));
2747 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2748 op_pin_state_floats);
2749 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2753 panic("unexpected expression kind");
2757 static ir_node *create_lazy_op(const binary_expression_t *expression)
2759 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2760 type_t *type = skip_typeref(expression->base.type);
2761 ir_mode *mode = get_ir_mode_arithmetic(type);
2763 if (is_constant_expression(expression->left) == EXPR_CLASS_CONSTANT) {
2764 bool val = fold_constant_to_bool(expression->left);
2765 expression_kind_t ekind = expression->base.kind;
2766 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2767 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2769 return new_Const(get_mode_null(mode));
2773 return new_Const(get_mode_one(mode));
2777 if (is_constant_expression(expression->right) == EXPR_CLASS_CONSTANT) {
2778 bool valr = fold_constant_to_bool(expression->right);
2779 return create_Const_from_bool(mode, valr);
2782 return produce_condition_result(expression->right, mode, dbgi);
2785 return produce_condition_result((const expression_t*) expression, mode,
2789 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2790 ir_node *right, ir_mode *mode);
2792 static ir_node *create_assign_binop(const binary_expression_t *expression)
2794 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2795 const expression_t *left_expr = expression->left;
2796 type_t *type = skip_typeref(left_expr->base.type);
2797 ir_node *right = expression_to_firm(expression->right);
2798 ir_node *left_addr = expression_to_addr(left_expr);
2799 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2800 ir_node *result = create_op(dbgi, expression, left, right);
2802 result = create_cast(dbgi, result, expression->right->base.type, type);
2803 result = do_strict_conv(dbgi, result);
2805 result = set_value_for_expression_addr(left_expr, result, left_addr);
2807 if (!is_type_compound(type)) {
2808 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2809 result = create_conv(dbgi, result, mode_arithmetic);
2814 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2816 expression_kind_t kind = expression->base.kind;
2819 case EXPR_BINARY_EQUAL:
2820 case EXPR_BINARY_NOTEQUAL:
2821 case EXPR_BINARY_LESS:
2822 case EXPR_BINARY_LESSEQUAL:
2823 case EXPR_BINARY_GREATER:
2824 case EXPR_BINARY_GREATEREQUAL:
2825 case EXPR_BINARY_ISGREATER:
2826 case EXPR_BINARY_ISGREATEREQUAL:
2827 case EXPR_BINARY_ISLESS:
2828 case EXPR_BINARY_ISLESSEQUAL:
2829 case EXPR_BINARY_ISLESSGREATER:
2830 case EXPR_BINARY_ISUNORDERED: {
2831 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2832 ir_node *left = expression_to_firm(expression->left);
2833 ir_node *right = expression_to_firm(expression->right);
2834 ir_relation relation = get_relation(kind);
2835 ir_node *cmp = new_d_Cmp(dbgi, left, right, relation);
2838 case EXPR_BINARY_ASSIGN: {
2839 ir_node *addr = expression_to_addr(expression->left);
2840 ir_node *right = expression_to_firm(expression->right);
2842 = set_value_for_expression_addr(expression->left, right, addr);
2844 type_t *type = skip_typeref(expression->base.type);
2845 if (!is_type_compound(type)) {
2846 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2847 res = create_conv(NULL, res, mode_arithmetic);
2851 case EXPR_BINARY_ADD:
2852 case EXPR_BINARY_SUB:
2853 case EXPR_BINARY_MUL:
2854 case EXPR_BINARY_DIV:
2855 case EXPR_BINARY_MOD:
2856 case EXPR_BINARY_BITWISE_AND:
2857 case EXPR_BINARY_BITWISE_OR:
2858 case EXPR_BINARY_BITWISE_XOR:
2859 case EXPR_BINARY_SHIFTLEFT:
2860 case EXPR_BINARY_SHIFTRIGHT:
2862 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2863 ir_node *left = expression_to_firm(expression->left);
2864 ir_node *right = expression_to_firm(expression->right);
2865 return create_op(dbgi, expression, left, right);
2867 case EXPR_BINARY_LOGICAL_AND:
2868 case EXPR_BINARY_LOGICAL_OR:
2869 return create_lazy_op(expression);
2870 case EXPR_BINARY_COMMA:
2871 /* create side effects of left side */
2872 (void) expression_to_firm(expression->left);
2873 return _expression_to_firm(expression->right);
2875 case EXPR_BINARY_ADD_ASSIGN:
2876 case EXPR_BINARY_SUB_ASSIGN:
2877 case EXPR_BINARY_MUL_ASSIGN:
2878 case EXPR_BINARY_MOD_ASSIGN:
2879 case EXPR_BINARY_DIV_ASSIGN:
2880 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2881 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2882 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2883 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2884 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2885 return create_assign_binop(expression);
2887 panic("TODO binexpr type");
2891 static ir_node *array_access_addr(const array_access_expression_t *expression)
2893 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2894 ir_node *base_addr = expression_to_firm(expression->array_ref);
2895 ir_node *offset = expression_to_firm(expression->index);
2896 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2897 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2898 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2903 static ir_node *array_access_to_firm(
2904 const array_access_expression_t *expression)
2906 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2907 ir_node *addr = array_access_addr(expression);
2908 type_t *type = revert_automatic_type_conversion(
2909 (const expression_t*) expression);
2910 type = skip_typeref(type);
2912 return deref_address(dbgi, type, addr);
2915 static long get_offsetof_offset(const offsetof_expression_t *expression)
2917 type_t *orig_type = expression->type;
2920 designator_t *designator = expression->designator;
2921 for ( ; designator != NULL; designator = designator->next) {
2922 type_t *type = skip_typeref(orig_type);
2923 /* be sure the type is constructed */
2924 (void) get_ir_type(type);
2926 if (designator->symbol != NULL) {
2927 assert(is_type_compound(type));
2928 symbol_t *symbol = designator->symbol;
2930 compound_t *compound = type->compound.compound;
2931 entity_t *iter = compound->members.entities;
2932 for ( ; iter != NULL; iter = iter->base.next) {
2933 if (iter->base.symbol == symbol) {
2937 assert(iter != NULL);
2939 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2940 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2941 offset += get_entity_offset(iter->compound_member.entity);
2943 orig_type = iter->declaration.type;
2945 expression_t *array_index = designator->array_index;
2946 assert(designator->array_index != NULL);
2947 assert(is_type_array(type));
2949 long index = fold_constant_to_int(array_index);
2950 ir_type *arr_type = get_ir_type(type);
2951 ir_type *elem_type = get_array_element_type(arr_type);
2952 long elem_size = get_type_size_bytes(elem_type);
2954 offset += index * elem_size;
2956 orig_type = type->array.element_type;
2963 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2965 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2966 long offset = get_offsetof_offset(expression);
2967 ir_tarval *tv = new_tarval_from_long(offset, mode);
2968 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2970 return new_d_Const(dbgi, tv);
2973 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2974 ir_entity *entity, type_t *type);
2976 static ir_node *compound_literal_to_firm(
2977 const compound_literal_expression_t *expression)
2979 type_t *type = expression->type;
2981 /* create an entity on the stack */
2982 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2984 ident *const id = id_unique("CompLit.%u");
2985 ir_type *const irtype = get_ir_type(type);
2986 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2987 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2988 set_entity_ld_ident(entity, id);
2990 /* create initialisation code */
2991 initializer_t *initializer = expression->initializer;
2992 create_local_initializer(initializer, dbgi, entity, type);
2994 /* create a sel for the compound literal address */
2995 ir_node *frame = get_irg_frame(current_ir_graph);
2996 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3001 * Transform a sizeof expression into Firm code.
3003 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3005 type_t *const type = skip_typeref(expression->type);
3006 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3007 if (is_type_array(type) && type->array.is_vla
3008 && expression->tp_expression != NULL) {
3009 expression_to_firm(expression->tp_expression);
3011 /* strange gnu extensions: sizeof(function) == 1 */
3012 if (is_type_function(type)) {
3013 ir_mode *mode = get_ir_mode_storage(type_size_t);
3014 return new_Const(get_mode_one(mode));
3017 return get_type_size_node(type);
3020 static entity_t *get_expression_entity(const expression_t *expression)
3022 if (expression->kind != EXPR_REFERENCE)
3025 return expression->reference.entity;
3028 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3030 switch(entity->kind) {
3031 DECLARATION_KIND_CASES
3032 return entity->declaration.alignment;
3035 return entity->compound.alignment;
3036 case ENTITY_TYPEDEF:
3037 return entity->typedefe.alignment;
3045 * Transform an alignof expression into Firm code.
3047 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3049 unsigned alignment = 0;
3051 const expression_t *tp_expression = expression->tp_expression;
3052 if (tp_expression != NULL) {
3053 entity_t *entity = get_expression_entity(tp_expression);
3054 if (entity != NULL) {
3055 if (entity->kind == ENTITY_FUNCTION) {
3056 /* a gnu-extension */
3059 alignment = get_cparser_entity_alignment(entity);
3064 if (alignment == 0) {
3065 type_t *type = expression->type;
3066 alignment = get_type_alignment(type);
3069 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3070 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3071 ir_tarval *tv = new_tarval_from_long(alignment, mode);
3072 return new_d_Const(dbgi, tv);
3075 static void init_ir_types(void);
3077 static ir_tarval *fold_constant_to_tarval(const expression_t *expression)
3079 assert(is_type_valid(skip_typeref(expression->base.type)));
3081 bool constant_folding_old = constant_folding;
3082 constant_folding = true;
3086 assert(is_constant_expression(expression) == EXPR_CLASS_CONSTANT);
3088 ir_graph *old_current_ir_graph = current_ir_graph;
3089 current_ir_graph = get_const_code_irg();
3091 ir_node *cnst = expression_to_firm(expression);
3092 current_ir_graph = old_current_ir_graph;
3094 if (!is_Const(cnst)) {
3095 panic("couldn't fold constant");
3098 constant_folding = constant_folding_old;
3100 return get_Const_tarval(cnst);
3103 long fold_constant_to_int(const expression_t *expression)
3105 if (expression->kind == EXPR_INVALID)
3108 ir_tarval *tv = fold_constant_to_tarval(expression);
3109 if (!tarval_is_long(tv)) {
3110 panic("result of constant folding is not integer");
3113 return get_tarval_long(tv);
3116 bool fold_constant_to_bool(const expression_t *expression)
3118 if (expression->kind == EXPR_INVALID)
3120 ir_tarval *tv = fold_constant_to_tarval(expression);
3121 return !tarval_is_null(tv);
3124 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3126 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3128 /* first try to fold a constant condition */
3129 if (is_constant_expression(expression->condition) == EXPR_CLASS_CONSTANT) {
3130 bool val = fold_constant_to_bool(expression->condition);
3132 expression_t *true_expression = expression->true_expression;
3133 if (true_expression == NULL)
3134 true_expression = expression->condition;
3135 return expression_to_firm(true_expression);
3137 return expression_to_firm(expression->false_expression);
3141 ir_node *const true_block = new_immBlock();
3142 ir_node *const false_block = new_immBlock();
3143 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3144 mature_immBlock(true_block);
3145 mature_immBlock(false_block);
3147 set_cur_block(true_block);
3149 if (expression->true_expression != NULL) {
3150 true_val = expression_to_firm(expression->true_expression);
3151 } else if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3152 true_val = cond_expr;
3154 /* Condition ended with a short circuit (&&, ||, !) operation or a
3155 * comparison. Generate a "1" as value for the true branch. */
3156 true_val = new_Const(get_mode_one(mode_Is));
3158 ir_node *const true_jmp = new_d_Jmp(dbgi);
3160 set_cur_block(false_block);
3161 ir_node *const false_val = expression_to_firm(expression->false_expression);
3162 ir_node *const false_jmp = new_d_Jmp(dbgi);
3164 /* create the common block */
3165 ir_node *const in_cf[2] = { true_jmp, false_jmp };
3166 ir_node *const block = new_Block(lengthof(in_cf), in_cf);
3167 set_cur_block(block);
3169 /* TODO improve static semantics, so either both or no values are NULL */
3170 if (true_val == NULL || false_val == NULL)
3173 ir_node *const in[2] = { true_val, false_val };
3174 type_t *const type = skip_typeref(expression->base.type);
3176 if (is_type_compound(type)) {
3179 mode = get_ir_mode_arithmetic(type);
3181 ir_node *const val = new_d_Phi(dbgi, lengthof(in), in, mode);
3187 * Returns an IR-node representing the address of a field.
3189 static ir_node *select_addr(const select_expression_t *expression)
3191 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3193 construct_select_compound(expression);
3195 ir_node *compound_addr = expression_to_firm(expression->compound);
3197 entity_t *entry = expression->compound_entry;
3198 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3199 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3201 if (constant_folding) {
3202 ir_mode *mode = get_irn_mode(compound_addr);
3203 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3204 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3205 return new_d_Add(dbgi, compound_addr, ofs, mode);
3207 ir_entity *irentity = entry->compound_member.entity;
3208 assert(irentity != NULL);
3209 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3213 static ir_node *select_to_firm(const select_expression_t *expression)
3215 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3216 ir_node *addr = select_addr(expression);
3217 type_t *type = revert_automatic_type_conversion(
3218 (const expression_t*) expression);
3219 type = skip_typeref(type);
3221 entity_t *entry = expression->compound_entry;
3222 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3223 type_t *entry_type = skip_typeref(entry->declaration.type);
3225 if (entry_type->kind == TYPE_BITFIELD) {
3226 return bitfield_extract_to_firm(expression, addr);
3229 return deref_address(dbgi, type, addr);
3232 /* Values returned by __builtin_classify_type. */
3233 typedef enum gcc_type_class
3239 enumeral_type_class,
3242 reference_type_class,
3246 function_type_class,
3257 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3259 type_t *type = expr->type_expression->base.type;
3261 /* FIXME gcc returns different values depending on whether compiling C or C++
3262 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3265 type = skip_typeref(type);
3266 switch (type->kind) {
3268 const atomic_type_t *const atomic_type = &type->atomic;
3269 switch (atomic_type->akind) {
3270 /* should not be reached */
3271 case ATOMIC_TYPE_INVALID:
3275 /* gcc cannot do that */
3276 case ATOMIC_TYPE_VOID:
3277 tc = void_type_class;
3280 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3281 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3282 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3283 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3284 case ATOMIC_TYPE_SHORT:
3285 case ATOMIC_TYPE_USHORT:
3286 case ATOMIC_TYPE_INT:
3287 case ATOMIC_TYPE_UINT:
3288 case ATOMIC_TYPE_LONG:
3289 case ATOMIC_TYPE_ULONG:
3290 case ATOMIC_TYPE_LONGLONG:
3291 case ATOMIC_TYPE_ULONGLONG:
3292 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3293 tc = integer_type_class;
3296 case ATOMIC_TYPE_FLOAT:
3297 case ATOMIC_TYPE_DOUBLE:
3298 case ATOMIC_TYPE_LONG_DOUBLE:
3299 tc = real_type_class;
3302 panic("Unexpected atomic type in classify_type_to_firm().");
3305 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3306 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3307 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3308 case TYPE_ARRAY: /* gcc handles this as pointer */
3309 case TYPE_FUNCTION: /* gcc handles this as pointer */
3310 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3311 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3312 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3314 /* gcc handles this as integer */
3315 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3317 /* gcc classifies the referenced type */
3318 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3321 /* typedef/typeof should be skipped already */
3328 panic("unexpected TYPE classify_type_to_firm().");
3332 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3333 ir_tarval *const tv = new_tarval_from_long(tc, mode_int);
3334 return new_d_Const(dbgi, tv);
3337 static ir_node *function_name_to_firm(
3338 const funcname_expression_t *const expr)
3340 switch(expr->kind) {
3341 case FUNCNAME_FUNCTION:
3342 case FUNCNAME_PRETTY_FUNCTION:
3343 case FUNCNAME_FUNCDNAME:
3344 if (current_function_name == NULL) {
3345 const source_position_t *const src_pos = &expr->base.source_position;
3346 const char *name = current_function_entity->base.symbol->string;
3347 const string_t string = { name, strlen(name) + 1 };
3348 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3350 return current_function_name;
3351 case FUNCNAME_FUNCSIG:
3352 if (current_funcsig == NULL) {
3353 const source_position_t *const src_pos = &expr->base.source_position;
3354 ir_entity *ent = get_irg_entity(current_ir_graph);
3355 const char *const name = get_entity_ld_name(ent);
3356 const string_t string = { name, strlen(name) + 1 };
3357 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3359 return current_funcsig;
3361 panic("Unsupported function name");
3364 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3366 statement_t *statement = expr->statement;
3368 assert(statement->kind == STATEMENT_COMPOUND);
3369 return compound_statement_to_firm(&statement->compound);
3372 static ir_node *va_start_expression_to_firm(
3373 const va_start_expression_t *const expr)
3375 type_t *const type = current_function_entity->declaration.type;
3376 ir_type *const method_type = get_ir_type(type);
3377 int const n = get_method_n_params(method_type) - 1;
3378 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3379 ir_node *const frame = get_irg_frame(current_ir_graph);
3380 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3381 ir_node *const no_mem = new_NoMem();
3382 ir_node *const arg_sel =
3383 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3385 type_t *const param_type = expr->parameter->base.type;
3386 ir_node *const cnst = get_type_size_node(param_type);
3387 ir_mode *const mode = get_irn_mode(cnst);
3388 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3389 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3390 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3391 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3392 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3393 set_value_for_expression(expr->ap, add);
3398 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3400 type_t *const type = expr->base.type;
3401 expression_t *const ap_expr = expr->ap;
3402 ir_node *const ap_addr = expression_to_addr(ap_expr);
3403 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3404 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3405 ir_node *const res = deref_address(dbgi, type, ap);
3407 ir_node *const cnst = get_type_size_node(expr->base.type);
3408 ir_mode *const mode = get_irn_mode(cnst);
3409 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3410 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3411 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3412 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3413 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3415 set_value_for_expression_addr(ap_expr, add, ap_addr);
3421 * Generate Firm for a va_copy expression.
3423 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3425 ir_node *const src = expression_to_firm(expr->src);
3426 set_value_for_expression(expr->dst, src);
3430 static ir_node *dereference_addr(const unary_expression_t *const expression)
3432 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3433 return expression_to_firm(expression->value);
3437 * Returns a IR-node representing an lvalue of the given expression.
3439 static ir_node *expression_to_addr(const expression_t *expression)
3441 switch(expression->kind) {
3442 case EXPR_ARRAY_ACCESS:
3443 return array_access_addr(&expression->array_access);
3445 return call_expression_to_firm(&expression->call);
3446 case EXPR_COMPOUND_LITERAL:
3447 return compound_literal_to_firm(&expression->compound_literal);
3448 case EXPR_REFERENCE:
3449 return reference_addr(&expression->reference);
3451 return select_addr(&expression->select);
3452 case EXPR_UNARY_DEREFERENCE:
3453 return dereference_addr(&expression->unary);
3457 panic("trying to get address of non-lvalue");
3460 static ir_node *builtin_constant_to_firm(
3461 const builtin_constant_expression_t *expression)
3463 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3464 bool const v = is_constant_expression(expression->value) == EXPR_CLASS_CONSTANT;
3465 return create_Const_from_bool(mode, v);
3468 static ir_node *builtin_types_compatible_to_firm(
3469 const builtin_types_compatible_expression_t *expression)
3471 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3472 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3473 bool const value = types_compatible(left, right);
3474 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3475 return create_Const_from_bool(mode, value);
3478 static ir_node *get_label_block(label_t *label)
3480 if (label->block != NULL)
3481 return label->block;
3483 /* beware: might be called from create initializer with current_ir_graph
3484 * set to const_code_irg. */
3485 ir_graph *rem = current_ir_graph;
3486 current_ir_graph = current_function;
3488 ir_node *block = new_immBlock();
3490 label->block = block;
3492 ARR_APP1(label_t *, all_labels, label);
3494 current_ir_graph = rem;
3499 * Pointer to a label. This is used for the
3500 * GNU address-of-label extension.
3502 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3504 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3505 ir_node *block = get_label_block(label->label);
3506 ir_entity *entity = create_Block_entity(block);
3508 symconst_symbol value;
3509 value.entity_p = entity;
3510 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3514 * creates firm nodes for an expression. The difference between this function
3515 * and expression_to_firm is, that this version might produce mode_b nodes
3516 * instead of mode_Is.
3518 static ir_node *_expression_to_firm(const expression_t *expression)
3521 if (!constant_folding) {
3522 assert(!expression->base.transformed);
3523 ((expression_t*) expression)->base.transformed = true;
3527 switch (expression->kind) {
3529 return literal_to_firm(&expression->literal);
3530 case EXPR_STRING_LITERAL:
3531 return string_to_firm(&expression->base.source_position, "str.%u",
3532 &expression->literal.value);
3533 case EXPR_WIDE_STRING_LITERAL:
3534 return wide_string_literal_to_firm(&expression->string_literal);
3535 case EXPR_REFERENCE:
3536 return reference_expression_to_firm(&expression->reference);
3537 case EXPR_REFERENCE_ENUM_VALUE:
3538 return reference_expression_enum_value_to_firm(&expression->reference);
3540 return call_expression_to_firm(&expression->call);
3542 return unary_expression_to_firm(&expression->unary);
3544 return binary_expression_to_firm(&expression->binary);
3545 case EXPR_ARRAY_ACCESS:
3546 return array_access_to_firm(&expression->array_access);
3548 return sizeof_to_firm(&expression->typeprop);
3550 return alignof_to_firm(&expression->typeprop);
3551 case EXPR_CONDITIONAL:
3552 return conditional_to_firm(&expression->conditional);
3554 return select_to_firm(&expression->select);
3555 case EXPR_CLASSIFY_TYPE:
3556 return classify_type_to_firm(&expression->classify_type);
3558 return function_name_to_firm(&expression->funcname);
3559 case EXPR_STATEMENT:
3560 return statement_expression_to_firm(&expression->statement);
3562 return va_start_expression_to_firm(&expression->va_starte);
3564 return va_arg_expression_to_firm(&expression->va_arge);
3566 return va_copy_expression_to_firm(&expression->va_copye);
3567 case EXPR_BUILTIN_CONSTANT_P:
3568 return builtin_constant_to_firm(&expression->builtin_constant);
3569 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3570 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3572 return offsetof_to_firm(&expression->offsetofe);
3573 case EXPR_COMPOUND_LITERAL:
3574 return compound_literal_to_firm(&expression->compound_literal);
3575 case EXPR_LABEL_ADDRESS:
3576 return label_address_to_firm(&expression->label_address);
3582 panic("invalid expression found");
3586 * Check if a given expression is a GNU __builtin_expect() call.
3588 static bool is_builtin_expect(const expression_t *expression)
3590 if (expression->kind != EXPR_CALL)
3593 expression_t *function = expression->call.function;
3594 if (function->kind != EXPR_REFERENCE)
3596 reference_expression_t *ref = &function->reference;
3597 if (ref->entity->kind != ENTITY_FUNCTION ||
3598 ref->entity->function.btk != bk_gnu_builtin_expect)
3604 static bool produces_mode_b(const expression_t *expression)
3606 switch (expression->kind) {
3607 case EXPR_BINARY_EQUAL:
3608 case EXPR_BINARY_NOTEQUAL:
3609 case EXPR_BINARY_LESS:
3610 case EXPR_BINARY_LESSEQUAL:
3611 case EXPR_BINARY_GREATER:
3612 case EXPR_BINARY_GREATEREQUAL:
3613 case EXPR_BINARY_ISGREATER:
3614 case EXPR_BINARY_ISGREATEREQUAL:
3615 case EXPR_BINARY_ISLESS:
3616 case EXPR_BINARY_ISLESSEQUAL:
3617 case EXPR_BINARY_ISLESSGREATER:
3618 case EXPR_BINARY_ISUNORDERED:
3619 case EXPR_UNARY_NOT:
3623 if (is_builtin_expect(expression)) {
3624 expression_t *argument = expression->call.arguments->expression;
3625 return produces_mode_b(argument);
3628 case EXPR_BINARY_COMMA:
3629 return produces_mode_b(expression->binary.right);
3636 static ir_node *expression_to_firm(const expression_t *expression)
3638 if (!produces_mode_b(expression)) {
3639 ir_node *res = _expression_to_firm(expression);
3640 assert(res == NULL || get_irn_mode(res) != mode_b);
3644 if (is_constant_expression(expression) == EXPR_CLASS_CONSTANT) {
3645 bool const constant_folding_old = constant_folding;
3646 constant_folding = true;
3647 ir_node *res = _expression_to_firm(expression);
3648 constant_folding = constant_folding_old;
3649 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3650 assert(is_Const(res));
3651 return create_Const_from_bool(mode, !is_Const_null(res));
3654 /* we have to produce a 0/1 from the mode_b expression */
3655 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3656 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3657 return produce_condition_result(expression, mode, dbgi);
3661 * create a short-circuit expression evaluation that tries to construct
3662 * efficient control flow structures for &&, || and ! expressions
3664 static ir_node *create_condition_evaluation(const expression_t *expression,
3665 ir_node *true_block,
3666 ir_node *false_block)
3668 switch(expression->kind) {
3669 case EXPR_UNARY_NOT: {
3670 const unary_expression_t *unary_expression = &expression->unary;
3671 create_condition_evaluation(unary_expression->value, false_block,
3675 case EXPR_BINARY_LOGICAL_AND: {
3676 const binary_expression_t *binary_expression = &expression->binary;
3678 ir_node *extra_block = new_immBlock();
3679 create_condition_evaluation(binary_expression->left, extra_block,
3681 mature_immBlock(extra_block);
3682 set_cur_block(extra_block);
3683 create_condition_evaluation(binary_expression->right, true_block,
3687 case EXPR_BINARY_LOGICAL_OR: {
3688 const binary_expression_t *binary_expression = &expression->binary;
3690 ir_node *extra_block = new_immBlock();
3691 create_condition_evaluation(binary_expression->left, true_block,
3693 mature_immBlock(extra_block);
3694 set_cur_block(extra_block);
3695 create_condition_evaluation(binary_expression->right, true_block,
3703 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3704 ir_node *cond_expr = _expression_to_firm(expression);
3705 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3706 ir_node *cond = new_d_Cond(dbgi, condition);
3707 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3708 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3710 /* set branch prediction info based on __builtin_expect */
3711 if (is_builtin_expect(expression) && is_Cond(cond)) {
3712 call_argument_t *argument = expression->call.arguments->next;
3713 if (is_constant_expression(argument->expression) == EXPR_CLASS_CONSTANT) {
3714 bool const cnst = fold_constant_to_bool(argument->expression);
3715 cond_jmp_predicate const pred = cnst ? COND_JMP_PRED_TRUE : COND_JMP_PRED_FALSE;
3716 set_Cond_jmp_pred(cond, pred);
3720 add_immBlock_pred(true_block, true_proj);
3721 add_immBlock_pred(false_block, false_proj);
3723 set_cur_block(NULL);
3727 static void create_variable_entity(entity_t *variable,
3728 declaration_kind_t declaration_kind,
3729 ir_type *parent_type)
3731 assert(variable->kind == ENTITY_VARIABLE);
3732 type_t *type = skip_typeref(variable->declaration.type);
3734 ident *const id = new_id_from_str(variable->base.symbol->string);
3735 ir_type *const irtype = get_ir_type(type);
3736 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3737 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3738 unsigned alignment = variable->declaration.alignment;
3740 set_entity_alignment(irentity, alignment);
3742 handle_decl_modifiers(irentity, variable);
3744 variable->declaration.kind = (unsigned char) declaration_kind;
3745 variable->variable.v.entity = irentity;
3746 set_entity_ld_ident(irentity, create_ld_ident(variable));
3748 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3749 set_entity_volatility(irentity, volatility_is_volatile);
3754 typedef struct type_path_entry_t type_path_entry_t;
3755 struct type_path_entry_t {
3757 ir_initializer_t *initializer;
3759 entity_t *compound_entry;
3762 typedef struct type_path_t type_path_t;
3763 struct type_path_t {
3764 type_path_entry_t *path;
3769 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3771 size_t len = ARR_LEN(path->path);
3773 for (size_t i = 0; i < len; ++i) {
3774 const type_path_entry_t *entry = & path->path[i];
3776 type_t *type = skip_typeref(entry->type);
3777 if (is_type_compound(type)) {
3778 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3779 } else if (is_type_array(type)) {
3780 fprintf(stderr, "[%u]", (unsigned) entry->index);
3782 fprintf(stderr, "-INVALID-");
3785 fprintf(stderr, " (");
3786 print_type(path->top_type);
3787 fprintf(stderr, ")");
3790 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3792 size_t len = ARR_LEN(path->path);
3794 return & path->path[len-1];
3797 static type_path_entry_t *append_to_type_path(type_path_t *path)
3799 size_t len = ARR_LEN(path->path);
3800 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3802 type_path_entry_t *result = & path->path[len];
3803 memset(result, 0, sizeof(result[0]));
3807 static size_t get_compound_member_count(const compound_type_t *type)
3809 compound_t *compound = type->compound;
3810 size_t n_members = 0;
3811 entity_t *member = compound->members.entities;
3812 for ( ; member != NULL; member = member->base.next) {
3819 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3821 type_t *orig_top_type = path->top_type;
3822 type_t *top_type = skip_typeref(orig_top_type);
3824 assert(is_type_compound(top_type) || is_type_array(top_type));
3826 if (ARR_LEN(path->path) == 0) {
3829 type_path_entry_t *top = get_type_path_top(path);
3830 ir_initializer_t *initializer = top->initializer;
3831 return get_initializer_compound_value(initializer, top->index);
3835 static void descend_into_subtype(type_path_t *path)
3837 type_t *orig_top_type = path->top_type;
3838 type_t *top_type = skip_typeref(orig_top_type);
3840 assert(is_type_compound(top_type) || is_type_array(top_type));
3842 ir_initializer_t *initializer = get_initializer_entry(path);
3844 type_path_entry_t *top = append_to_type_path(path);
3845 top->type = top_type;
3849 if (is_type_compound(top_type)) {
3850 compound_t *compound = top_type->compound.compound;
3851 entity_t *entry = compound->members.entities;
3853 top->compound_entry = entry;
3855 len = get_compound_member_count(&top_type->compound);
3856 if (entry != NULL) {
3857 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3858 path->top_type = entry->declaration.type;
3861 assert(is_type_array(top_type));
3862 assert(top_type->array.size > 0);
3865 path->top_type = top_type->array.element_type;
3866 len = top_type->array.size;
3868 if (initializer == NULL
3869 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3870 initializer = create_initializer_compound(len);
3871 /* we have to set the entry at the 2nd latest path entry... */
3872 size_t path_len = ARR_LEN(path->path);
3873 assert(path_len >= 1);
3875 type_path_entry_t *entry = & path->path[path_len-2];
3876 ir_initializer_t *tinitializer = entry->initializer;
3877 set_initializer_compound_value(tinitializer, entry->index,
3881 top->initializer = initializer;
3884 static void ascend_from_subtype(type_path_t *path)
3886 type_path_entry_t *top = get_type_path_top(path);
3888 path->top_type = top->type;
3890 size_t len = ARR_LEN(path->path);
3891 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3894 static void walk_designator(type_path_t *path, const designator_t *designator)
3896 /* designators start at current object type */
3897 ARR_RESIZE(type_path_entry_t, path->path, 1);
3899 for ( ; designator != NULL; designator = designator->next) {
3900 type_path_entry_t *top = get_type_path_top(path);
3901 type_t *orig_type = top->type;
3902 type_t *type = skip_typeref(orig_type);
3904 if (designator->symbol != NULL) {
3905 assert(is_type_compound(type));
3907 symbol_t *symbol = designator->symbol;
3909 compound_t *compound = type->compound.compound;
3910 entity_t *iter = compound->members.entities;
3911 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3912 if (iter->base.symbol == symbol) {
3913 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3917 assert(iter != NULL);
3919 /* revert previous initialisations of other union elements */
3920 if (type->kind == TYPE_COMPOUND_UNION) {
3921 ir_initializer_t *initializer = top->initializer;
3922 if (initializer != NULL
3923 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3924 /* are we writing to a new element? */
3925 ir_initializer_t *oldi
3926 = get_initializer_compound_value(initializer, index);
3927 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3928 /* clear initializer */
3930 = get_initializer_compound_n_entries(initializer);
3931 ir_initializer_t *nulli = get_initializer_null();
3932 for (size_t i = 0; i < len; ++i) {
3933 set_initializer_compound_value(initializer, i,
3940 top->type = orig_type;
3941 top->compound_entry = iter;
3943 orig_type = iter->declaration.type;
3945 expression_t *array_index = designator->array_index;
3946 assert(designator->array_index != NULL);
3947 assert(is_type_array(type));
3949 long index = fold_constant_to_int(array_index);
3952 if (type->array.size_constant) {
3953 long array_size = type->array.size;
3954 assert(index < array_size);
3958 top->type = orig_type;
3959 top->index = (size_t) index;
3960 orig_type = type->array.element_type;
3962 path->top_type = orig_type;
3964 if (designator->next != NULL) {
3965 descend_into_subtype(path);
3969 path->invalid = false;
3972 static void advance_current_object(type_path_t *path)
3974 if (path->invalid) {
3975 /* TODO: handle this... */
3976 panic("invalid initializer in ast2firm (excessive elements)");
3979 type_path_entry_t *top = get_type_path_top(path);
3981 type_t *type = skip_typeref(top->type);
3982 if (is_type_union(type)) {
3983 /* only the first element is initialized in unions */
3984 top->compound_entry = NULL;
3985 } else if (is_type_struct(type)) {
3986 entity_t *entry = top->compound_entry;
3989 entry = entry->base.next;
3990 top->compound_entry = entry;
3991 if (entry != NULL) {
3992 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3993 path->top_type = entry->declaration.type;
3997 assert(is_type_array(type));
4000 if (!type->array.size_constant || top->index < type->array.size) {
4005 /* we're past the last member of the current sub-aggregate, try if we
4006 * can ascend in the type hierarchy and continue with another subobject */
4007 size_t len = ARR_LEN(path->path);
4010 ascend_from_subtype(path);
4011 advance_current_object(path);
4013 path->invalid = true;
4018 static ir_initializer_t *create_ir_initializer(
4019 const initializer_t *initializer, type_t *type);
4021 static ir_initializer_t *create_ir_initializer_value(
4022 const initializer_value_t *initializer)
4024 if (is_type_compound(initializer->value->base.type)) {
4025 panic("initializer creation for compounds not implemented yet");
4027 type_t *type = initializer->value->base.type;
4028 expression_t *expr = initializer->value;
4029 if (initializer_use_bitfield_basetype) {
4030 type_t *skipped = skip_typeref(type);
4031 if (skipped->kind == TYPE_BITFIELD) {
4032 /* remove the bitfield cast... */
4033 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4034 expr = expr->unary.value;
4035 type = skipped->bitfield.base_type;
4038 ir_node *value = expression_to_firm(expr);
4039 ir_mode *mode = get_ir_mode_storage(type);
4040 value = create_conv(NULL, value, mode);
4041 return create_initializer_const(value);
4044 /** test wether type can be initialized by a string constant */
4045 static bool is_string_type(type_t *type)
4048 if (is_type_pointer(type)) {
4049 inner = skip_typeref(type->pointer.points_to);
4050 } else if(is_type_array(type)) {
4051 inner = skip_typeref(type->array.element_type);
4056 return is_type_integer(inner);
4059 static ir_initializer_t *create_ir_initializer_list(
4060 const initializer_list_t *initializer, type_t *type)
4063 memset(&path, 0, sizeof(path));
4064 path.top_type = type;
4065 path.path = NEW_ARR_F(type_path_entry_t, 0);
4067 descend_into_subtype(&path);
4069 for (size_t i = 0; i < initializer->len; ++i) {
4070 const initializer_t *sub_initializer = initializer->initializers[i];
4072 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4073 walk_designator(&path, sub_initializer->designator.designator);
4077 if (sub_initializer->kind == INITIALIZER_VALUE) {
4078 /* we might have to descend into types until we're at a scalar
4081 type_t *orig_top_type = path.top_type;
4082 type_t *top_type = skip_typeref(orig_top_type);
4084 if (is_type_scalar(top_type))
4086 descend_into_subtype(&path);
4088 } else if (sub_initializer->kind == INITIALIZER_STRING
4089 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
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_string_type(top_type))
4098 descend_into_subtype(&path);
4102 ir_initializer_t *sub_irinitializer
4103 = create_ir_initializer(sub_initializer, path.top_type);
4105 size_t path_len = ARR_LEN(path.path);
4106 assert(path_len >= 1);
4107 type_path_entry_t *entry = & path.path[path_len-1];
4108 ir_initializer_t *tinitializer = entry->initializer;
4109 set_initializer_compound_value(tinitializer, entry->index,
4112 advance_current_object(&path);
4115 assert(ARR_LEN(path.path) >= 1);
4116 ir_initializer_t *result = path.path[0].initializer;
4117 DEL_ARR_F(path.path);
4122 static ir_initializer_t *create_ir_initializer_string(
4123 const initializer_string_t *initializer, type_t *type)
4125 type = skip_typeref(type);
4127 size_t string_len = initializer->string.size;
4128 assert(type->kind == TYPE_ARRAY);
4129 assert(type->array.size_constant);
4130 size_t len = type->array.size;
4131 ir_initializer_t *irinitializer = create_initializer_compound(len);
4133 const char *string = initializer->string.begin;
4134 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4136 for (size_t i = 0; i < len; ++i) {
4141 ir_tarval *tv = new_tarval_from_long(c, mode);
4142 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4144 set_initializer_compound_value(irinitializer, i, char_initializer);
4147 return irinitializer;
4150 static ir_initializer_t *create_ir_initializer_wide_string(
4151 const initializer_wide_string_t *initializer, type_t *type)
4153 assert(type->kind == TYPE_ARRAY);
4154 assert(type->array.size_constant);
4155 size_t len = type->array.size;
4156 size_t string_len = wstrlen(&initializer->string);
4157 ir_initializer_t *irinitializer = create_initializer_compound(len);
4159 const char *p = initializer->string.begin;
4160 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4162 for (size_t i = 0; i < len; ++i) {
4164 if (i < string_len) {
4165 c = read_utf8_char(&p);
4167 ir_tarval *tv = new_tarval_from_long(c, mode);
4168 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4170 set_initializer_compound_value(irinitializer, i, char_initializer);
4173 return irinitializer;
4176 static ir_initializer_t *create_ir_initializer(
4177 const initializer_t *initializer, type_t *type)
4179 switch(initializer->kind) {
4180 case INITIALIZER_STRING:
4181 return create_ir_initializer_string(&initializer->string, type);
4183 case INITIALIZER_WIDE_STRING:
4184 return create_ir_initializer_wide_string(&initializer->wide_string,
4187 case INITIALIZER_LIST:
4188 return create_ir_initializer_list(&initializer->list, type);
4190 case INITIALIZER_VALUE:
4191 return create_ir_initializer_value(&initializer->value);
4193 case INITIALIZER_DESIGNATOR:
4194 panic("unexpected designator initializer found");
4196 panic("unknown initializer");
4199 /** ANSI C ยง6.7.8:21: If there are fewer initializers [..] than there
4200 * are elements [...] the remainder of the aggregate shall be initialized
4201 * implicitly the same as objects that have static storage duration. */
4202 static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
4205 /* for unions we must NOT do anything for null initializers */
4206 ir_type *owner = get_entity_owner(entity);
4207 if (is_Union_type(owner)) {
4211 ir_type *ent_type = get_entity_type(entity);
4212 /* create sub-initializers for a compound type */
4213 if (is_compound_type(ent_type)) {
4214 unsigned n_members = get_compound_n_members(ent_type);
4215 for (unsigned n = 0; n < n_members; ++n) {
4216 ir_entity *member = get_compound_member(ent_type, n);
4217 ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4219 create_dynamic_null_initializer(member, dbgi, addr);
4223 if (is_Array_type(ent_type)) {
4224 assert(has_array_upper_bound(ent_type, 0));
4225 long n = get_array_upper_bound_int(ent_type, 0);
4226 for (long i = 0; i < n; ++i) {
4227 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4228 ir_node *cnst = new_d_Const(dbgi, index_tv);
4229 ir_node *in[1] = { cnst };
4230 ir_entity *arrent = get_array_element_entity(ent_type);
4231 ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4233 create_dynamic_null_initializer(arrent, dbgi, addr);
4238 ir_mode *value_mode = get_type_mode(ent_type);
4239 ir_node *node = new_Const(get_mode_null(value_mode));
4241 /* is it a bitfield type? */
4242 if (is_Primitive_type(ent_type) &&
4243 get_primitive_base_type(ent_type) != NULL) {
4244 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4248 ir_node *mem = get_store();
4249 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4250 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4254 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4255 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4257 switch(get_initializer_kind(initializer)) {
4258 case IR_INITIALIZER_NULL:
4259 create_dynamic_null_initializer(entity, dbgi, base_addr);
4261 case IR_INITIALIZER_CONST: {
4262 ir_node *node = get_initializer_const_value(initializer);
4263 ir_type *ent_type = get_entity_type(entity);
4265 /* is it a bitfield type? */
4266 if (is_Primitive_type(ent_type) &&
4267 get_primitive_base_type(ent_type) != NULL) {
4268 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4272 assert(get_type_mode(type) == get_irn_mode(node));
4273 ir_node *mem = get_store();
4274 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4275 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4279 case IR_INITIALIZER_TARVAL: {
4280 ir_tarval *tv = get_initializer_tarval_value(initializer);
4281 ir_node *cnst = new_d_Const(dbgi, tv);
4282 ir_type *ent_type = get_entity_type(entity);
4284 /* is it a bitfield type? */
4285 if (is_Primitive_type(ent_type) &&
4286 get_primitive_base_type(ent_type) != NULL) {
4287 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4291 assert(get_type_mode(type) == get_tarval_mode(tv));
4292 ir_node *mem = get_store();
4293 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4294 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4298 case IR_INITIALIZER_COMPOUND: {
4299 assert(is_compound_type(type) || is_Array_type(type));
4301 if (is_Array_type(type)) {
4302 assert(has_array_upper_bound(type, 0));
4303 n_members = get_array_upper_bound_int(type, 0);
4305 n_members = get_compound_n_members(type);
4308 if (get_initializer_compound_n_entries(initializer)
4309 != (unsigned) n_members)
4310 panic("initializer doesn't match compound type");
4312 for (int i = 0; i < n_members; ++i) {
4315 ir_entity *sub_entity;
4316 if (is_Array_type(type)) {
4317 ir_tarval *index_tv = new_tarval_from_long(i, mode_uint);
4318 ir_node *cnst = new_d_Const(dbgi, index_tv);
4319 ir_node *in[1] = { cnst };
4320 irtype = get_array_element_type(type);
4321 sub_entity = get_array_element_entity(type);
4322 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4325 sub_entity = get_compound_member(type, i);
4326 irtype = get_entity_type(sub_entity);
4327 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4331 ir_initializer_t *sub_init
4332 = get_initializer_compound_value(initializer, i);
4334 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4341 panic("invalid IR_INITIALIZER found");
4344 static void create_dynamic_initializer(ir_initializer_t *initializer,
4345 dbg_info *dbgi, ir_entity *entity)
4347 ir_node *frame = get_irg_frame(current_ir_graph);
4348 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4349 ir_type *type = get_entity_type(entity);
4351 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4354 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4355 ir_entity *entity, type_t *type)
4357 ir_node *memory = get_store();
4358 ir_node *nomem = new_NoMem();
4359 ir_node *frame = get_irg_frame(current_ir_graph);
4360 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4362 if (initializer->kind == INITIALIZER_VALUE) {
4363 initializer_value_t *initializer_value = &initializer->value;
4365 ir_node *value = expression_to_firm(initializer_value->value);
4366 type = skip_typeref(type);
4367 assign_value(dbgi, addr, type, value);
4371 if (is_constant_initializer(initializer) == EXPR_CLASS_VARIABLE) {
4372 bool old_initializer_use_bitfield_basetype
4373 = initializer_use_bitfield_basetype;
4374 initializer_use_bitfield_basetype = true;
4375 ir_initializer_t *irinitializer
4376 = create_ir_initializer(initializer, type);
4377 initializer_use_bitfield_basetype
4378 = old_initializer_use_bitfield_basetype;
4380 create_dynamic_initializer(irinitializer, dbgi, entity);
4384 /* create the ir_initializer */
4385 ir_graph *const old_current_ir_graph = current_ir_graph;
4386 current_ir_graph = get_const_code_irg();
4388 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4390 assert(current_ir_graph == get_const_code_irg());
4391 current_ir_graph = old_current_ir_graph;
4393 /* create a "template" entity which is copied to the entity on the stack */
4394 ident *const id = id_unique("initializer.%u");
4395 ir_type *const irtype = get_ir_type(type);
4396 ir_type *const global_type = get_glob_type();
4397 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4398 set_entity_ld_ident(init_entity, id);
4400 set_entity_visibility(init_entity, ir_visibility_private);
4401 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4403 set_entity_initializer(init_entity, irinitializer);
4405 ir_node *const src_addr = create_symconst(dbgi, init_entity);
4406 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4408 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4409 set_store(copyb_mem);
4412 static void create_initializer_local_variable_entity(entity_t *entity)
4414 assert(entity->kind == ENTITY_VARIABLE);
4415 initializer_t *initializer = entity->variable.initializer;
4416 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4417 ir_entity *irentity = entity->variable.v.entity;
4418 type_t *type = entity->declaration.type;
4420 create_local_initializer(initializer, dbgi, irentity, type);
4423 static void create_variable_initializer(entity_t *entity)
4425 assert(entity->kind == ENTITY_VARIABLE);
4426 initializer_t *initializer = entity->variable.initializer;
4427 if (initializer == NULL)
4430 declaration_kind_t declaration_kind
4431 = (declaration_kind_t) entity->declaration.kind;
4432 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4433 create_initializer_local_variable_entity(entity);
4437 type_t *type = entity->declaration.type;
4438 type_qualifiers_t tq = get_type_qualifier(type, true);
4440 if (initializer->kind == INITIALIZER_VALUE) {
4441 initializer_value_t *initializer_value = &initializer->value;
4442 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4444 ir_node *value = expression_to_firm(initializer_value->value);
4446 type_t *type = initializer_value->value->base.type;
4447 ir_mode *mode = get_ir_mode_storage(type);
4448 value = create_conv(dbgi, value, mode);
4449 value = do_strict_conv(dbgi, value);
4451 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4452 set_value(entity->variable.v.value_number, value);
4454 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4456 ir_entity *irentity = entity->variable.v.entity;
4458 if (tq & TYPE_QUALIFIER_CONST
4459 && get_entity_owner(irentity) != get_tls_type()) {
4460 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4462 set_atomic_ent_value(irentity, value);
4465 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4466 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4468 ir_entity *irentity = entity->variable.v.entity;
4469 ir_initializer_t *irinitializer
4470 = create_ir_initializer(initializer, type);
4472 if (tq & TYPE_QUALIFIER_CONST) {
4473 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4475 set_entity_initializer(irentity, irinitializer);
4479 static void create_variable_length_array(entity_t *entity)
4481 assert(entity->kind == ENTITY_VARIABLE);
4482 assert(entity->variable.initializer == NULL);
4484 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4485 entity->variable.v.vla_base = NULL;
4487 /* TODO: record VLA somewhere so we create the free node when we leave
4491 static void allocate_variable_length_array(entity_t *entity)
4493 assert(entity->kind == ENTITY_VARIABLE);
4494 assert(entity->variable.initializer == NULL);
4495 assert(get_cur_block() != NULL);
4497 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4498 type_t *type = entity->declaration.type;
4499 ir_type *el_type = get_ir_type(type->array.element_type);
4501 /* make sure size_node is calculated */
4502 get_type_size_node(type);
4503 ir_node *elems = type->array.size_node;
4504 ir_node *mem = get_store();
4505 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4507 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4508 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4511 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4512 entity->variable.v.vla_base = addr;
4516 * Creates a Firm local variable from a declaration.
4518 static void create_local_variable(entity_t *entity)
4520 assert(entity->kind == ENTITY_VARIABLE);
4521 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4523 bool needs_entity = entity->variable.address_taken;
4524 type_t *type = skip_typeref(entity->declaration.type);
4526 /* is it a variable length array? */
4527 if (is_type_array(type) && !type->array.size_constant) {
4528 create_variable_length_array(entity);
4530 } else if (is_type_array(type) || is_type_compound(type)) {
4531 needs_entity = true;
4532 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4533 needs_entity = true;
4537 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4538 create_variable_entity(entity,
4539 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4542 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4543 entity->variable.v.value_number = next_value_number_function;
4544 set_irg_loc_description(current_ir_graph, next_value_number_function,
4546 ++next_value_number_function;
4550 static void create_local_static_variable(entity_t *entity)
4552 assert(entity->kind == ENTITY_VARIABLE);
4553 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4555 type_t *type = skip_typeref(entity->declaration.type);
4556 ir_type *const var_type = entity->variable.thread_local ?
4557 get_tls_type() : get_glob_type();
4558 ir_type *const irtype = get_ir_type(type);
4559 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4561 size_t l = strlen(entity->base.symbol->string);
4562 char buf[l + sizeof(".%u")];
4563 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4564 ident *const id = id_unique(buf);
4565 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4567 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4568 set_entity_volatility(irentity, volatility_is_volatile);
4571 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4572 entity->variable.v.entity = irentity;
4574 set_entity_ld_ident(irentity, id);
4575 set_entity_visibility(irentity, ir_visibility_local);
4577 ir_graph *const old_current_ir_graph = current_ir_graph;
4578 current_ir_graph = get_const_code_irg();
4580 create_variable_initializer(entity);
4582 assert(current_ir_graph == get_const_code_irg());
4583 current_ir_graph = old_current_ir_graph;
4588 static void return_statement_to_firm(return_statement_t *statement)
4590 if (get_cur_block() == NULL)
4593 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4594 type_t *type = current_function_entity->declaration.type;
4595 ir_type *func_irtype = get_ir_type(type);
4600 if (get_method_n_ress(func_irtype) > 0) {
4601 ir_type *res_type = get_method_res_type(func_irtype, 0);
4603 if (statement->value != NULL) {
4604 ir_node *node = expression_to_firm(statement->value);
4605 if (!is_compound_type(res_type)) {
4606 type_t *type = statement->value->base.type;
4607 ir_mode *mode = get_ir_mode_storage(type);
4608 node = create_conv(dbgi, node, mode);
4609 node = do_strict_conv(dbgi, node);
4614 if (is_compound_type(res_type)) {
4617 mode = get_type_mode(res_type);
4619 in[0] = new_Unknown(mode);
4623 /* build return_value for its side effects */
4624 if (statement->value != NULL) {
4625 expression_to_firm(statement->value);
4630 ir_node *store = get_store();
4631 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4633 ir_node *end_block = get_irg_end_block(current_ir_graph);
4634 add_immBlock_pred(end_block, ret);
4636 set_cur_block(NULL);
4639 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4641 if (get_cur_block() == NULL)
4644 return expression_to_firm(statement->expression);
4647 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4649 entity_t *entity = compound->scope.entities;
4650 for ( ; entity != NULL; entity = entity->base.next) {
4651 if (!is_declaration(entity))
4654 create_local_declaration(entity);
4657 ir_node *result = NULL;
4658 statement_t *statement = compound->statements;
4659 for ( ; statement != NULL; statement = statement->base.next) {
4660 if (statement->base.next == NULL
4661 && statement->kind == STATEMENT_EXPRESSION) {
4662 result = expression_statement_to_firm(
4663 &statement->expression);
4666 statement_to_firm(statement);
4672 static void create_global_variable(entity_t *entity)
4674 ir_linkage linkage = IR_LINKAGE_DEFAULT;
4675 ir_visibility visibility = ir_visibility_default;
4676 ir_entity *irentity;
4677 assert(entity->kind == ENTITY_VARIABLE);
4679 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4680 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4681 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4682 case STORAGE_CLASS_NONE:
4683 visibility = ir_visibility_default;
4684 /* uninitialized globals get merged in C */
4685 if (entity->variable.initializer == NULL)
4686 linkage |= IR_LINKAGE_MERGE;
4688 case STORAGE_CLASS_TYPEDEF:
4689 case STORAGE_CLASS_AUTO:
4690 case STORAGE_CLASS_REGISTER:
4691 panic("invalid storage class for global var");
4694 ir_type *var_type = get_glob_type();
4695 if (entity->variable.thread_local) {
4696 var_type = get_tls_type();
4697 /* LINKAGE_MERGE not supported by current linkers */
4698 linkage &= ~IR_LINKAGE_MERGE;
4700 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4701 irentity = entity->variable.v.entity;
4702 add_entity_linkage(irentity, linkage);
4703 set_entity_visibility(irentity, visibility);
4706 static void create_local_declaration(entity_t *entity)
4708 assert(is_declaration(entity));
4710 /* construct type */
4711 (void) get_ir_type(entity->declaration.type);
4712 if (entity->base.symbol == NULL) {
4716 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4717 case STORAGE_CLASS_STATIC:
4718 if (entity->kind == ENTITY_FUNCTION) {
4719 (void)get_function_entity(entity, NULL);
4721 create_local_static_variable(entity);
4724 case STORAGE_CLASS_EXTERN:
4725 if (entity->kind == ENTITY_FUNCTION) {
4726 assert(entity->function.statement == NULL);
4727 (void)get_function_entity(entity, NULL);
4729 create_global_variable(entity);
4730 create_variable_initializer(entity);
4733 case STORAGE_CLASS_NONE:
4734 case STORAGE_CLASS_AUTO:
4735 case STORAGE_CLASS_REGISTER:
4736 if (entity->kind == ENTITY_FUNCTION) {
4737 if (entity->function.statement != NULL) {
4738 ir_type *owner = get_irg_frame_type(current_ir_graph);
4739 (void)get_function_entity(entity, owner);
4740 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4741 enqueue_inner_function(entity);
4743 (void)get_function_entity(entity, NULL);
4746 create_local_variable(entity);
4749 case STORAGE_CLASS_TYPEDEF:
4752 panic("invalid storage class found");
4755 static void initialize_local_declaration(entity_t *entity)
4757 if (entity->base.symbol == NULL)
4760 // no need to emit code in dead blocks
4761 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4762 && get_cur_block() == NULL)
4765 switch ((declaration_kind_t) entity->declaration.kind) {
4766 case DECLARATION_KIND_LOCAL_VARIABLE:
4767 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4768 create_variable_initializer(entity);
4771 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4772 allocate_variable_length_array(entity);
4775 case DECLARATION_KIND_COMPOUND_MEMBER:
4776 case DECLARATION_KIND_GLOBAL_VARIABLE:
4777 case DECLARATION_KIND_FUNCTION:
4778 case DECLARATION_KIND_INNER_FUNCTION:
4781 case DECLARATION_KIND_PARAMETER:
4782 case DECLARATION_KIND_PARAMETER_ENTITY:
4783 panic("can't initialize parameters");
4785 case DECLARATION_KIND_UNKNOWN:
4786 panic("can't initialize unknown declaration");
4788 panic("invalid declaration kind");
4791 static void declaration_statement_to_firm(declaration_statement_t *statement)
4793 entity_t *entity = statement->declarations_begin;
4797 entity_t *const last = statement->declarations_end;
4798 for ( ;; entity = entity->base.next) {
4799 if (is_declaration(entity)) {
4800 initialize_local_declaration(entity);
4801 } else if (entity->kind == ENTITY_TYPEDEF) {
4802 /* ยง6.7.7:3 Any array size expressions associated with variable length
4803 * array declarators are evaluated each time the declaration of the
4804 * typedef name is reached in the order of execution. */
4805 type_t *const type = skip_typeref(entity->typedefe.type);
4806 if (is_type_array(type) && type->array.is_vla)
4807 get_vla_size(&type->array);
4814 static void if_statement_to_firm(if_statement_t *statement)
4816 /* Create the condition. */
4817 ir_node *true_block = NULL;
4818 ir_node *false_block = NULL;
4819 if (get_cur_block() != NULL) {
4820 true_block = new_immBlock();
4821 false_block = new_immBlock();
4822 create_condition_evaluation(statement->condition, true_block, false_block);
4823 mature_immBlock(true_block);
4826 /* Create the false statement.
4827 * Handle false before true, so if no false statement is present, then the
4828 * empty false block is reused as fallthrough block. */
4829 ir_node *fallthrough_block = NULL;
4830 if (statement->false_statement != NULL) {
4831 if (false_block != NULL) {
4832 mature_immBlock(false_block);
4834 set_cur_block(false_block);
4835 statement_to_firm(statement->false_statement);
4836 if (get_cur_block() != NULL) {
4837 fallthrough_block = new_immBlock();
4838 add_immBlock_pred(fallthrough_block, new_Jmp());
4841 fallthrough_block = false_block;
4844 /* Create the true statement. */
4845 set_cur_block(true_block);
4846 statement_to_firm(statement->true_statement);
4847 if (get_cur_block() != NULL) {
4848 if (fallthrough_block == NULL) {
4849 fallthrough_block = new_immBlock();
4851 add_immBlock_pred(fallthrough_block, new_Jmp());
4854 /* Handle the block after the if-statement. */
4855 if (fallthrough_block != NULL) {
4856 mature_immBlock(fallthrough_block);
4858 set_cur_block(fallthrough_block);
4861 /* Create a jump node which jumps into target_block, if the current block is
4863 static void jump_if_reachable(ir_node *const target_block)
4865 if (get_cur_block() != NULL) {
4866 add_immBlock_pred(target_block, new_Jmp());
4870 static void while_statement_to_firm(while_statement_t *statement)
4872 /* Create the header block */
4873 ir_node *const header_block = new_immBlock();
4874 jump_if_reachable(header_block);
4876 /* Create the condition. */
4877 ir_node * body_block;
4878 ir_node * false_block;
4879 expression_t *const cond = statement->condition;
4880 if (is_constant_expression(cond) == EXPR_CLASS_CONSTANT &&
4881 fold_constant_to_bool(cond)) {
4882 /* Shortcut for while (true). */
4883 body_block = header_block;
4886 keep_alive(header_block);
4887 keep_all_memory(header_block);
4889 body_block = new_immBlock();
4890 false_block = new_immBlock();
4892 set_cur_block(header_block);
4893 create_condition_evaluation(cond, body_block, false_block);
4894 mature_immBlock(body_block);
4897 ir_node *const old_continue_label = continue_label;
4898 ir_node *const old_break_label = break_label;
4899 continue_label = header_block;
4900 break_label = false_block;
4902 /* Create the loop body. */
4903 set_cur_block(body_block);
4904 statement_to_firm(statement->body);
4905 jump_if_reachable(header_block);
4907 mature_immBlock(header_block);
4908 assert(false_block == NULL || false_block == break_label);
4909 false_block = break_label;
4910 if (false_block != NULL) {
4911 mature_immBlock(false_block);
4913 set_cur_block(false_block);
4915 assert(continue_label == header_block);
4916 continue_label = old_continue_label;
4917 break_label = old_break_label;
4920 static ir_node *get_break_label(void)
4922 if (break_label == NULL) {
4923 break_label = new_immBlock();
4928 static void do_while_statement_to_firm(do_while_statement_t *statement)
4930 /* create the header block */
4931 ir_node *header_block = new_immBlock();
4934 ir_node *body_block = new_immBlock();
4935 jump_if_reachable(body_block);
4937 ir_node *old_continue_label = continue_label;
4938 ir_node *old_break_label = break_label;
4939 continue_label = header_block;
4942 set_cur_block(body_block);
4943 statement_to_firm(statement->body);
4944 ir_node *const false_block = get_break_label();
4946 assert(continue_label == header_block);
4947 continue_label = old_continue_label;
4948 break_label = old_break_label;
4950 jump_if_reachable(header_block);
4952 /* create the condition */
4953 mature_immBlock(header_block);
4954 set_cur_block(header_block);
4956 create_condition_evaluation(statement->condition, body_block, false_block);
4957 mature_immBlock(body_block);
4958 mature_immBlock(false_block);
4960 set_cur_block(false_block);
4963 static void for_statement_to_firm(for_statement_t *statement)
4965 /* create declarations */
4966 entity_t *entity = statement->scope.entities;
4967 for ( ; entity != NULL; entity = entity->base.next) {
4968 if (!is_declaration(entity))
4971 create_local_declaration(entity);
4974 if (get_cur_block() != NULL) {
4975 entity = statement->scope.entities;
4976 for ( ; entity != NULL; entity = entity->base.next) {
4977 if (!is_declaration(entity))
4980 initialize_local_declaration(entity);
4983 if (statement->initialisation != NULL) {
4984 expression_to_firm(statement->initialisation);
4988 /* Create the header block */
4989 ir_node *const header_block = new_immBlock();
4990 jump_if_reachable(header_block);
4992 /* Create the condition. */
4993 ir_node *body_block;
4994 ir_node *false_block;
4995 if (statement->condition != NULL) {
4996 body_block = new_immBlock();
4997 false_block = new_immBlock();
4999 set_cur_block(header_block);
5000 create_condition_evaluation(statement->condition, body_block, false_block);
5001 mature_immBlock(body_block);
5004 body_block = header_block;
5007 keep_alive(header_block);
5008 keep_all_memory(header_block);
5011 /* Create the step block, if necessary. */
5012 ir_node * step_block = header_block;
5013 expression_t *const step = statement->step;
5015 step_block = new_immBlock();
5018 ir_node *const old_continue_label = continue_label;
5019 ir_node *const old_break_label = break_label;
5020 continue_label = step_block;
5021 break_label = false_block;
5023 /* Create the loop body. */
5024 set_cur_block(body_block);
5025 statement_to_firm(statement->body);
5026 jump_if_reachable(step_block);
5028 /* Create the step code. */
5030 mature_immBlock(step_block);
5031 set_cur_block(step_block);
5032 expression_to_firm(step);
5033 jump_if_reachable(header_block);
5036 mature_immBlock(header_block);
5037 assert(false_block == NULL || false_block == break_label);
5038 false_block = break_label;
5039 if (false_block != NULL) {
5040 mature_immBlock(false_block);
5042 set_cur_block(false_block);
5044 assert(continue_label == step_block);
5045 continue_label = old_continue_label;
5046 break_label = old_break_label;
5049 static void create_jump_statement(const statement_t *statement,
5050 ir_node *target_block)
5052 if (get_cur_block() == NULL)
5055 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5056 ir_node *jump = new_d_Jmp(dbgi);
5057 add_immBlock_pred(target_block, jump);
5059 set_cur_block(NULL);
5062 static void switch_statement_to_firm(switch_statement_t *statement)
5064 ir_node *first_block = NULL;
5065 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5066 ir_node *cond = NULL;
5068 if (get_cur_block() != NULL) {
5069 ir_node *expression = expression_to_firm(statement->expression);
5070 cond = new_d_Cond(dbgi, expression);
5071 first_block = get_cur_block();
5074 set_cur_block(NULL);
5076 ir_node *const old_switch_cond = current_switch_cond;
5077 ir_node *const old_break_label = break_label;
5078 const bool old_saw_default_label = saw_default_label;
5079 saw_default_label = false;
5080 current_switch_cond = cond;
5082 switch_statement_t *const old_switch = current_switch;
5083 current_switch = statement;
5085 /* determine a free number for the default label */
5086 unsigned long num_cases = 0;
5087 long default_proj_nr = 0;
5088 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5089 if (l->expression == NULL) {
5093 if (l->last_case >= l->first_case)
5094 num_cases += l->last_case - l->first_case + 1;
5095 if (l->last_case > default_proj_nr)
5096 default_proj_nr = l->last_case;
5099 if (default_proj_nr == INT_MAX) {
5100 /* Bad: an overflow will occur, we cannot be sure that the
5101 * maximum + 1 is a free number. Scan the values a second
5102 * time to find a free number.
5104 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5106 memset(bits, 0, (num_cases + 7) >> 3);
5107 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5108 if (l->expression == NULL) {
5112 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5113 if (start < num_cases && l->last_case >= 0) {
5114 unsigned long end = (unsigned long)l->last_case < num_cases ?
5115 (unsigned long)l->last_case : num_cases - 1;
5116 for (unsigned long cns = start; cns <= end; ++cns) {
5117 bits[cns >> 3] |= (1 << (cns & 7));
5121 /* We look at the first num_cases constants:
5122 * Either they are dense, so we took the last (num_cases)
5123 * one, or they are not dense, so we will find one free
5127 for (i = 0; i < num_cases; ++i)
5128 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5132 default_proj_nr = i;
5136 statement->default_proj_nr = default_proj_nr;
5137 /* safety check: cond might already be folded to a Bad */
5138 if (cond != NULL && is_Cond(cond)) {
5139 set_Cond_default_proj(cond, default_proj_nr);
5142 statement_to_firm(statement->body);
5144 jump_if_reachable(get_break_label());
5146 if (!saw_default_label && first_block != NULL) {
5147 set_cur_block(first_block);
5148 ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr);
5149 add_immBlock_pred(get_break_label(), proj);
5152 if (break_label != NULL) {
5153 mature_immBlock(break_label);
5155 set_cur_block(break_label);
5157 assert(current_switch_cond == cond);
5158 current_switch = old_switch;
5159 current_switch_cond = old_switch_cond;
5160 break_label = old_break_label;
5161 saw_default_label = old_saw_default_label;
5164 static void case_label_to_firm(const case_label_statement_t *statement)
5166 if (statement->is_empty_range)
5169 ir_node *block = new_immBlock();
5170 /* Fallthrough from previous case */
5171 jump_if_reachable(block);
5173 if (current_switch_cond != NULL) {
5174 set_cur_block(get_nodes_block(current_switch_cond));
5175 dbg_info *const dbgi = get_dbg_info(&statement->base.source_position);
5176 if (statement->expression != NULL) {
5177 long pn = statement->first_case;
5178 long end_pn = statement->last_case;
5179 assert(pn <= end_pn);
5180 /* create jumps for all cases in the given range */
5182 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5183 add_immBlock_pred(block, proj);
5184 } while (pn++ < end_pn);
5186 saw_default_label = true;
5187 ir_node *const proj = new_d_Proj(dbgi, current_switch_cond, mode_X,
5188 current_switch->default_proj_nr);
5189 add_immBlock_pred(block, proj);
5193 mature_immBlock(block);
5194 set_cur_block(block);
5196 statement_to_firm(statement->statement);
5199 static void label_to_firm(const label_statement_t *statement)
5201 ir_node *block = get_label_block(statement->label);
5202 jump_if_reachable(block);
5204 set_cur_block(block);
5206 keep_all_memory(block);
5208 statement_to_firm(statement->statement);
5211 static void goto_to_firm(const goto_statement_t *statement)
5213 if (get_cur_block() == NULL)
5216 if (statement->expression) {
5217 ir_node *irn = expression_to_firm(statement->expression);
5218 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5219 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5221 set_irn_link(ijmp, ijmp_list);
5224 ir_node *block = get_label_block(statement->label);
5225 ir_node *jmp = new_Jmp();
5226 add_immBlock_pred(block, jmp);
5228 set_cur_block(NULL);
5231 static void asm_statement_to_firm(const asm_statement_t *statement)
5233 bool needs_memory = false;
5235 if (statement->is_volatile) {
5236 needs_memory = true;
5239 size_t n_clobbers = 0;
5240 asm_clobber_t *clobber = statement->clobbers;
5241 for ( ; clobber != NULL; clobber = clobber->next) {
5242 const char *clobber_str = clobber->clobber.begin;
5244 if (!be_is_valid_clobber(clobber_str)) {
5245 errorf(&statement->base.source_position,
5246 "invalid clobber '%s' specified", clobber->clobber);
5250 if (strcmp(clobber_str, "memory") == 0) {
5251 needs_memory = true;
5255 ident *id = new_id_from_str(clobber_str);
5256 obstack_ptr_grow(&asm_obst, id);
5259 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5260 ident **clobbers = NULL;
5261 if (n_clobbers > 0) {
5262 clobbers = obstack_finish(&asm_obst);
5265 size_t n_inputs = 0;
5266 asm_argument_t *argument = statement->inputs;
5267 for ( ; argument != NULL; argument = argument->next)
5269 size_t n_outputs = 0;
5270 argument = statement->outputs;
5271 for ( ; argument != NULL; argument = argument->next)
5274 unsigned next_pos = 0;
5276 ir_node *ins[n_inputs + n_outputs + 1];
5279 ir_asm_constraint tmp_in_constraints[n_outputs];
5281 const expression_t *out_exprs[n_outputs];
5282 ir_node *out_addrs[n_outputs];
5283 size_t out_size = 0;
5285 argument = statement->outputs;
5286 for ( ; argument != NULL; argument = argument->next) {
5287 const char *constraints = argument->constraints.begin;
5288 asm_constraint_flags_t asm_flags
5289 = be_parse_asm_constraints(constraints);
5291 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5292 warningf(&statement->base.source_position,
5293 "some constraints in '%s' are not supported", constraints);
5295 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5296 errorf(&statement->base.source_position,
5297 "some constraints in '%s' are invalid", constraints);
5300 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5301 errorf(&statement->base.source_position,
5302 "no write flag specified for output constraints '%s'",
5307 unsigned pos = next_pos++;
5308 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5309 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5310 expression_t *expr = argument->expression;
5311 ir_node *addr = expression_to_addr(expr);
5312 /* in+output, construct an artifical same_as constraint on the
5314 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5316 ir_node *value = get_value_from_lvalue(expr, addr);
5318 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5320 ir_asm_constraint constraint;
5321 constraint.pos = pos;
5322 constraint.constraint = new_id_from_str(buf);
5323 constraint.mode = get_ir_mode_storage(expr->base.type);
5324 tmp_in_constraints[in_size] = constraint;
5325 ins[in_size] = value;
5330 out_exprs[out_size] = expr;
5331 out_addrs[out_size] = addr;
5333 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5334 /* pure memory ops need no input (but we have to make sure we
5335 * attach to the memory) */
5336 assert(! (asm_flags &
5337 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5338 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5339 needs_memory = true;
5341 /* we need to attach the address to the inputs */
5342 expression_t *expr = argument->expression;
5344 ir_asm_constraint constraint;
5345 constraint.pos = pos;
5346 constraint.constraint = new_id_from_str(constraints);
5347 constraint.mode = NULL;
5348 tmp_in_constraints[in_size] = constraint;
5350 ins[in_size] = expression_to_addr(expr);
5354 errorf(&statement->base.source_position,
5355 "only modifiers but no place set in constraints '%s'",
5360 ir_asm_constraint constraint;
5361 constraint.pos = pos;
5362 constraint.constraint = new_id_from_str(constraints);
5363 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5365 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5367 assert(obstack_object_size(&asm_obst)
5368 == out_size * sizeof(ir_asm_constraint));
5369 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5372 obstack_grow(&asm_obst, tmp_in_constraints,
5373 in_size * sizeof(tmp_in_constraints[0]));
5374 /* find and count input and output arguments */
5375 argument = statement->inputs;
5376 for ( ; argument != NULL; argument = argument->next) {
5377 const char *constraints = argument->constraints.begin;
5378 asm_constraint_flags_t asm_flags
5379 = be_parse_asm_constraints(constraints);
5381 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5382 errorf(&statement->base.source_position,
5383 "some constraints in '%s' are not supported", constraints);
5386 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5387 errorf(&statement->base.source_position,
5388 "some constraints in '%s' are invalid", constraints);
5391 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5392 errorf(&statement->base.source_position,
5393 "write flag specified for input constraints '%s'",
5399 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5400 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5401 /* we can treat this as "normal" input */
5402 input = expression_to_firm(argument->expression);
5403 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5404 /* pure memory ops need no input (but we have to make sure we
5405 * attach to the memory) */
5406 assert(! (asm_flags &
5407 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5408 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5409 needs_memory = true;
5410 input = expression_to_addr(argument->expression);
5412 errorf(&statement->base.source_position,
5413 "only modifiers but no place set in constraints '%s'",
5418 ir_asm_constraint constraint;
5419 constraint.pos = next_pos++;
5420 constraint.constraint = new_id_from_str(constraints);
5421 constraint.mode = get_irn_mode(input);
5423 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5424 ins[in_size++] = input;
5428 ir_asm_constraint constraint;
5429 constraint.pos = next_pos++;
5430 constraint.constraint = new_id_from_str("");
5431 constraint.mode = mode_M;
5433 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5434 ins[in_size++] = get_store();
5437 assert(obstack_object_size(&asm_obst)
5438 == in_size * sizeof(ir_asm_constraint));
5439 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5441 /* create asm node */
5442 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5444 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5446 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5447 out_size, output_constraints,
5448 n_clobbers, clobbers, asm_text);
5450 if (statement->is_volatile) {
5451 set_irn_pinned(node, op_pin_state_pinned);
5453 set_irn_pinned(node, op_pin_state_floats);
5456 /* create output projs & connect them */
5458 ir_node *projm = new_Proj(node, mode_M, out_size);
5463 for (i = 0; i < out_size; ++i) {
5464 const expression_t *out_expr = out_exprs[i];
5466 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5467 ir_node *proj = new_Proj(node, mode, pn);
5468 ir_node *addr = out_addrs[i];
5470 set_value_for_expression_addr(out_expr, proj, addr);
5474 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5476 statement_to_firm(statement->try_statement);
5477 warningf(&statement->base.source_position, "structured exception handling ignored");
5480 static void leave_statement_to_firm(leave_statement_t *statement)
5482 errorf(&statement->base.source_position, "__leave not supported yet");
5486 * Transform a statement.
5488 static void statement_to_firm(statement_t *statement)
5491 assert(!statement->base.transformed);
5492 statement->base.transformed = true;
5495 switch (statement->kind) {
5496 case STATEMENT_INVALID:
5497 panic("invalid statement found");
5498 case STATEMENT_EMPTY:
5501 case STATEMENT_COMPOUND:
5502 compound_statement_to_firm(&statement->compound);
5504 case STATEMENT_RETURN:
5505 return_statement_to_firm(&statement->returns);
5507 case STATEMENT_EXPRESSION:
5508 expression_statement_to_firm(&statement->expression);
5511 if_statement_to_firm(&statement->ifs);
5513 case STATEMENT_WHILE:
5514 while_statement_to_firm(&statement->whiles);
5516 case STATEMENT_DO_WHILE:
5517 do_while_statement_to_firm(&statement->do_while);
5519 case STATEMENT_DECLARATION:
5520 declaration_statement_to_firm(&statement->declaration);
5522 case STATEMENT_BREAK:
5523 create_jump_statement(statement, get_break_label());
5525 case STATEMENT_CONTINUE:
5526 create_jump_statement(statement, continue_label);
5528 case STATEMENT_SWITCH:
5529 switch_statement_to_firm(&statement->switchs);
5531 case STATEMENT_CASE_LABEL:
5532 case_label_to_firm(&statement->case_label);
5535 for_statement_to_firm(&statement->fors);
5537 case STATEMENT_LABEL:
5538 label_to_firm(&statement->label);
5540 case STATEMENT_GOTO:
5541 goto_to_firm(&statement->gotos);
5544 asm_statement_to_firm(&statement->asms);
5546 case STATEMENT_MS_TRY:
5547 ms_try_statement_to_firm(&statement->ms_try);
5549 case STATEMENT_LEAVE:
5550 leave_statement_to_firm(&statement->leave);
5553 panic("statement not implemented");
5556 static int count_local_variables(const entity_t *entity,
5557 const entity_t *const last)
5560 entity_t const *const end = last != NULL ? last->base.next : NULL;
5561 for (; entity != end; entity = entity->base.next) {
5565 if (entity->kind == ENTITY_VARIABLE) {
5566 type = skip_typeref(entity->declaration.type);
5567 address_taken = entity->variable.address_taken;
5568 } else if (entity->kind == ENTITY_PARAMETER) {
5569 type = skip_typeref(entity->declaration.type);
5570 address_taken = entity->parameter.address_taken;
5575 if (!address_taken && is_type_scalar(type))
5581 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5583 int *const count = env;
5585 switch (stmt->kind) {
5586 case STATEMENT_DECLARATION: {
5587 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5588 *count += count_local_variables(decl_stmt->declarations_begin,
5589 decl_stmt->declarations_end);
5594 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5603 * Return the number of local (alias free) variables used by a function.
5605 static int get_function_n_local_vars(entity_t *entity)
5607 const function_t *function = &entity->function;
5610 /* count parameters */
5611 count += count_local_variables(function->parameters.entities, NULL);
5613 /* count local variables declared in body */
5614 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5619 * Build Firm code for the parameters of a function.
5621 static void initialize_function_parameters(entity_t *entity)
5623 assert(entity->kind == ENTITY_FUNCTION);
5624 ir_graph *irg = current_ir_graph;
5625 ir_node *args = get_irg_args(irg);
5626 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5627 int first_param_nr = 0;
5629 if (entity->function.need_closure) {
5630 /* add an extra parameter for the static link */
5631 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5636 entity_t *parameter = entity->function.parameters.entities;
5637 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5638 if (parameter->kind != ENTITY_PARAMETER)
5641 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5642 type_t *type = skip_typeref(parameter->declaration.type);
5644 bool needs_entity = parameter->parameter.address_taken;
5645 assert(!is_type_array(type));
5646 if (is_type_compound(type)) {
5647 needs_entity = true;
5651 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5652 ident *id = new_id_from_str(parameter->base.symbol->string);
5653 set_entity_ident(entity, id);
5655 parameter->declaration.kind
5656 = DECLARATION_KIND_PARAMETER_ENTITY;
5657 parameter->parameter.v.entity = entity;
5661 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5662 ir_mode *param_mode = get_type_mode(param_irtype);
5664 long pn = n + first_param_nr;
5665 ir_node *value = new_r_Proj(args, param_mode, pn);
5667 ir_mode *mode = get_ir_mode_storage(type);
5668 value = create_conv(NULL, value, mode);
5669 value = do_strict_conv(NULL, value);
5671 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5672 parameter->parameter.v.value_number = next_value_number_function;
5673 set_irg_loc_description(current_ir_graph, next_value_number_function,
5675 ++next_value_number_function;
5677 set_value(parameter->parameter.v.value_number, value);
5682 * Handle additional decl modifiers for IR-graphs
5684 * @param irg the IR-graph
5685 * @param dec_modifiers additional modifiers
5687 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5688 decl_modifiers_t decl_modifiers)
5690 if (decl_modifiers & DM_RETURNS_TWICE) {
5691 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5692 add_irg_additional_properties(irg, mtp_property_returns_twice);
5694 if (decl_modifiers & DM_NORETURN) {
5695 /* TRUE if the declaration includes the Microsoft
5696 __declspec(noreturn) specifier. */
5697 add_irg_additional_properties(irg, mtp_property_noreturn);
5699 if (decl_modifiers & DM_NOTHROW) {
5700 /* TRUE if the declaration includes the Microsoft
5701 __declspec(nothrow) specifier. */
5702 add_irg_additional_properties(irg, mtp_property_nothrow);
5704 if (decl_modifiers & DM_NAKED) {
5705 /* TRUE if the declaration includes the Microsoft
5706 __declspec(naked) specifier. */
5707 add_irg_additional_properties(irg, mtp_property_naked);
5709 if (decl_modifiers & DM_FORCEINLINE) {
5710 /* TRUE if the declaration includes the
5711 Microsoft __forceinline specifier. */
5712 set_irg_inline_property(irg, irg_inline_forced);
5714 if (decl_modifiers & DM_NOINLINE) {
5715 /* TRUE if the declaration includes the Microsoft
5716 __declspec(noinline) specifier. */
5717 set_irg_inline_property(irg, irg_inline_forbidden);
5721 static void add_function_pointer(ir_type *segment, ir_entity *method,
5722 const char *unique_template)
5724 ir_type *method_type = get_entity_type(method);
5725 ir_type *ptr_type = new_type_pointer(method_type);
5727 /* these entities don't really have a name but firm only allows
5729 * Note that we mustn't give these entities a name since for example
5730 * Mach-O doesn't allow them. */
5731 ident *ide = id_unique(unique_template);
5732 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5733 ir_graph *irg = get_const_code_irg();
5734 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5737 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5738 set_entity_compiler_generated(ptr, 1);
5739 set_entity_visibility(ptr, ir_visibility_private);
5740 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5741 set_atomic_ent_value(ptr, val);
5745 * Generate possible IJmp branches to a given label block.
5747 static void gen_ijmp_branches(ir_node *block)
5750 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5751 add_immBlock_pred(block, ijmp);
5756 * Create code for a function and all inner functions.
5758 * @param entity the function entity
5760 static void create_function(entity_t *entity)
5762 assert(entity->kind == ENTITY_FUNCTION);
5763 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5765 if (entity->function.statement == NULL)
5768 if (is_main(entity) && enable_main_collect2_hack) {
5769 prepare_main_collect2(entity);
5772 inner_functions = NULL;
5773 current_trampolines = NULL;
5775 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5776 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5777 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5779 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5780 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5781 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5784 current_function_entity = entity;
5785 current_function_name = NULL;
5786 current_funcsig = NULL;
5788 assert(all_labels == NULL);
5789 all_labels = NEW_ARR_F(label_t *, 0);
5792 int n_local_vars = get_function_n_local_vars(entity);
5793 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5794 current_ir_graph = irg;
5796 ir_graph *old_current_function = current_function;
5797 current_function = irg;
5799 set_irg_fp_model(irg, firm_opt.fp_model);
5800 tarval_enable_fp_ops(1);
5801 set_irn_dbg_info(get_irg_start_block(irg),
5802 get_entity_dbg_info(function_entity));
5804 ir_node *first_block = get_cur_block();
5806 /* set inline flags */
5807 if (entity->function.is_inline)
5808 set_irg_inline_property(irg, irg_inline_recomended);
5809 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5811 next_value_number_function = 0;
5812 initialize_function_parameters(entity);
5813 current_static_link = entity->function.static_link;
5815 statement_to_firm(entity->function.statement);
5817 ir_node *end_block = get_irg_end_block(irg);
5819 /* do we have a return statement yet? */
5820 if (get_cur_block() != NULL) {
5821 type_t *type = skip_typeref(entity->declaration.type);
5822 assert(is_type_function(type));
5823 const function_type_t *func_type = &type->function;
5824 const type_t *return_type
5825 = skip_typeref(func_type->return_type);
5828 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5829 ret = new_Return(get_store(), 0, NULL);
5832 if (is_type_scalar(return_type)) {
5833 mode = get_ir_mode_storage(func_type->return_type);
5839 /* ยง5.1.2.2.3 main implicitly returns 0 */
5840 if (is_main(entity)) {
5841 in[0] = new_Const(get_mode_null(mode));
5843 in[0] = new_Unknown(mode);
5845 ret = new_Return(get_store(), 1, in);
5847 add_immBlock_pred(end_block, ret);
5850 bool has_computed_gotos = false;
5851 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5852 label_t *label = all_labels[i];
5853 if (label->address_taken) {
5854 gen_ijmp_branches(label->block);
5855 has_computed_gotos = true;
5857 mature_immBlock(label->block);
5859 if (has_computed_gotos) {
5860 /* if we have computed goto's in the function, we cannot inline it */
5861 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5862 warningf(&entity->base.source_position,
5863 "function '%Y' can never be inlined because it contains a computed goto",
5864 entity->base.symbol);
5866 set_irg_inline_property(irg, irg_inline_forbidden);
5869 DEL_ARR_F(all_labels);
5872 mature_immBlock(first_block);
5873 mature_immBlock(end_block);
5875 irg_finalize_cons(irg);
5877 /* finalize the frame type */
5878 ir_type *frame_type = get_irg_frame_type(irg);
5879 int n = get_compound_n_members(frame_type);
5882 for (int i = 0; i < n; ++i) {
5883 ir_entity *entity = get_compound_member(frame_type, i);
5884 ir_type *entity_type = get_entity_type(entity);
5886 int align = get_type_alignment_bytes(entity_type);
5887 if (align > align_all)
5891 misalign = offset % align;
5893 offset += align - misalign;
5897 set_entity_offset(entity, offset);
5898 offset += get_type_size_bytes(entity_type);
5900 set_type_size_bytes(frame_type, offset);
5901 set_type_alignment_bytes(frame_type, align_all);
5903 irg_verify(irg, VERIFY_ENFORCE_SSA);
5904 current_function = old_current_function;
5906 if (current_trampolines != NULL) {
5907 DEL_ARR_F(current_trampolines);
5908 current_trampolines = NULL;
5911 /* create inner functions if any */
5912 entity_t **inner = inner_functions;
5913 if (inner != NULL) {
5914 ir_type *rem_outer_frame = current_outer_frame;
5915 current_outer_frame = get_irg_frame_type(current_ir_graph);
5916 ir_type *rem_outer_value_type = current_outer_value_type;
5917 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5918 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5919 create_function(inner[i]);
5923 current_outer_value_type = rem_outer_value_type;
5924 current_outer_frame = rem_outer_frame;
5928 static void scope_to_firm(scope_t *scope)
5930 /* first pass: create declarations */
5931 entity_t *entity = scope->entities;
5932 for ( ; entity != NULL; entity = entity->base.next) {
5933 if (entity->base.symbol == NULL)
5936 if (entity->kind == ENTITY_FUNCTION) {
5937 if (entity->function.btk != bk_none) {
5938 /* builtins have no representation */
5941 (void)get_function_entity(entity, NULL);
5942 } else if (entity->kind == ENTITY_VARIABLE) {
5943 create_global_variable(entity);
5944 } else if (entity->kind == ENTITY_NAMESPACE) {
5945 scope_to_firm(&entity->namespacee.members);
5949 /* second pass: create code/initializers */
5950 entity = scope->entities;
5951 for ( ; entity != NULL; entity = entity->base.next) {
5952 if (entity->base.symbol == NULL)
5955 if (entity->kind == ENTITY_FUNCTION) {
5956 if (entity->function.btk != bk_none) {
5957 /* builtins have no representation */
5960 create_function(entity);
5961 } else if (entity->kind == ENTITY_VARIABLE) {
5962 assert(entity->declaration.kind
5963 == DECLARATION_KIND_GLOBAL_VARIABLE);
5964 current_ir_graph = get_const_code_irg();
5965 create_variable_initializer(entity);
5970 void init_ast2firm(void)
5972 obstack_init(&asm_obst);
5973 init_atomic_modes();
5975 ir_set_debug_retrieve(dbg_retrieve);
5976 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
5978 /* create idents for all known runtime functions */
5979 for (size_t i = 0; i < lengthof(rts_data); ++i) {
5980 rts_idents[i] = new_id_from_str(rts_data[i].name);
5983 entitymap_init(&entitymap);
5986 static void init_ir_types(void)
5988 static int ir_types_initialized = 0;
5989 if (ir_types_initialized)
5991 ir_types_initialized = 1;
5993 ir_type_int = get_ir_type(type_int);
5994 ir_type_char = get_ir_type(type_char);
5995 ir_type_const_char = get_ir_type(type_const_char);
5996 ir_type_wchar_t = get_ir_type(type_wchar_t);
5997 ir_type_void = get_ir_type(type_void);
5999 be_params = be_get_backend_param();
6000 mode_float_arithmetic = be_params->mode_float_arithmetic;
6002 stack_param_align = be_params->stack_param_align;
6005 void exit_ast2firm(void)
6007 entitymap_destroy(&entitymap);
6008 obstack_free(&asm_obst, NULL);
6011 static void global_asm_to_firm(statement_t *s)
6013 for (; s != NULL; s = s->base.next) {
6014 assert(s->kind == STATEMENT_ASM);
6016 char const *const text = s->asms.asm_text.begin;
6017 size_t size = s->asms.asm_text.size;
6019 /* skip the last \0 */
6020 if (text[size - 1] == '\0')
6023 ident *const id = new_id_from_chars(text, size);
6028 void translation_unit_to_firm(translation_unit_t *unit)
6030 /* initialize firm arithmetic */
6031 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6032 ir_set_uninitialized_local_variable_func(uninitialized_local_var);
6034 /* just to be sure */
6035 continue_label = NULL;
6037 current_switch_cond = NULL;
6038 current_translation_unit = unit;
6042 scope_to_firm(&unit->scope);
6043 global_asm_to_firm(unit->global_asm);
6045 current_ir_graph = NULL;
6046 current_translation_unit = NULL;