2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include <libfirm/firm.h>
28 #include <libfirm/adt/obst.h>
29 #include <libfirm/be.h>
33 #include "adt/error.h"
34 #include "adt/array.h"
42 #include "diagnostic.h"
43 #include "lang_features.h"
45 #include "type_hash.h"
47 #include "walk_statements.h"
50 #include "entitymap_t.h"
51 #include "driver/firm_opt.h"
52 #include "driver/firm_cmdline.h"
54 typedef struct trampoline_region trampoline_region;
55 struct trampoline_region {
56 ir_entity *function; /**< The function that is called by this trampoline */
57 ir_entity *region; /**< created region for the trampoline */
60 static const backend_params *be_params;
62 static ir_type *ir_type_char;
63 static ir_type *ir_type_const_char;
64 static ir_type *ir_type_wchar_t;
65 static ir_type *ir_type_void;
66 static ir_type *ir_type_int;
68 /* architecture specific floating point arithmetic mode (if any) */
69 static ir_mode *mode_float_arithmetic;
71 /* alignment of stack parameters */
72 static unsigned stack_param_align;
74 static int next_value_number_function;
75 static ir_node *continue_label;
76 static ir_node *break_label;
77 static ir_node *current_switch_cond;
78 static bool saw_default_label;
79 static label_t **all_labels;
80 static entity_t **inner_functions;
81 static ir_node *ijmp_list;
82 static bool constant_folding;
83 static bool initializer_use_bitfield_basetype;
85 extern bool have_const_functions;
87 static const entity_t *current_function_entity;
88 static ir_node *current_function_name;
89 static ir_node *current_funcsig;
90 static switch_statement_t *current_switch;
91 static ir_graph *current_function;
92 static translation_unit_t *current_translation_unit;
93 static trampoline_region *current_trampolines;
94 static ir_type *current_outer_frame;
95 static ir_type *current_outer_value_type;
96 static ir_node *current_static_link;
98 static entitymap_t entitymap;
100 static struct obstack asm_obst;
102 typedef enum declaration_kind_t {
103 DECLARATION_KIND_UNKNOWN,
104 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
105 DECLARATION_KIND_GLOBAL_VARIABLE,
106 DECLARATION_KIND_LOCAL_VARIABLE,
107 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
108 DECLARATION_KIND_PARAMETER,
109 DECLARATION_KIND_PARAMETER_ENTITY,
110 DECLARATION_KIND_FUNCTION,
111 DECLARATION_KIND_COMPOUND_MEMBER,
112 DECLARATION_KIND_INNER_FUNCTION
113 } declaration_kind_t;
115 static ir_mode *get_ir_mode_storage(type_t *type);
117 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
118 * int that it returns bigger modes for floating point on some platforms
119 * (x87 internally does arithemtic with 80bits)
121 static ir_mode *get_ir_mode_arithmetic(type_t *type);
123 static ir_type *get_ir_type_incomplete(type_t *type);
125 static void enqueue_inner_function(entity_t *entity)
127 if (inner_functions == NULL)
128 inner_functions = NEW_ARR_F(entity_t *, 0);
129 ARR_APP1(entity_t*, inner_functions, entity);
132 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
134 const entity_t *entity = get_irg_loc_description(irg, pos);
136 if (entity != NULL) {
137 warningf(&entity->base.source_position,
138 "%s '%#T' might be used uninitialized",
139 get_entity_kind_name(entity->kind),
140 entity->declaration.type, entity->base.symbol);
142 return new_r_Unknown(irg, mode);
145 static const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
147 const source_position_t *pos = (const source_position_t*) dbg;
152 return pos->input_name;
155 static dbg_info *get_dbg_info(const source_position_t *pos)
157 return (dbg_info*) pos;
160 static void dbg_print_type_dbg_info(char *buffer, size_t buffer_size,
161 const type_dbg_info *dbg)
164 print_to_buffer(buffer, buffer_size);
165 const type_t *type = (const type_t*) dbg;
167 finish_print_to_buffer();
170 static type_dbg_info *get_type_dbg_info_(const type_t *type)
172 return (type_dbg_info*) type;
175 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
177 static ir_mode *mode_int, *mode_uint;
179 static ir_node *_expression_to_firm(const expression_t *expression);
180 static ir_node *expression_to_firm(const expression_t *expression);
181 static void create_local_declaration(entity_t *entity);
183 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
185 unsigned flags = get_atomic_type_flags(kind);
186 unsigned size = get_atomic_type_size(kind);
187 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
188 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
191 unsigned bit_size = size * 8;
192 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
193 unsigned modulo_shift;
194 ir_mode_arithmetic arithmetic;
196 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
197 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
198 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
200 sort = irms_int_number;
201 arithmetic = irma_twos_complement;
202 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
204 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
205 snprintf(name, sizeof(name), "F%u", bit_size);
206 sort = irms_float_number;
207 arithmetic = irma_ieee754;
210 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
218 * Initialises the atomic modes depending on the machine size.
220 static void init_atomic_modes(void)
222 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
223 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
225 mode_int = atomic_modes[ATOMIC_TYPE_INT];
226 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
228 /* there's no real void type in firm */
229 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
231 /* initialize pointer modes */
233 ir_mode_sort sort = irms_reference;
234 unsigned bit_size = machine_size;
236 ir_mode_arithmetic arithmetic = irma_twos_complement;
237 unsigned modulo_shift
238 = bit_size < machine_size ? machine_size : bit_size;
240 snprintf(name, sizeof(name), "p%u", machine_size);
241 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
244 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
245 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
247 /* Hmm, pointers should be machine size */
248 set_modeP_data(ptr_mode);
249 set_modeP_code(ptr_mode);
252 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
254 assert(kind <= ATOMIC_TYPE_LAST);
255 return atomic_modes[kind];
258 static ir_node *get_vla_size(array_type_t *const type)
260 ir_node *size_node = type->size_node;
261 if (size_node == NULL) {
262 size_node = expression_to_firm(type->size_expression);
263 type->size_node = size_node;
269 * Return a node representing the size of a type.
271 static ir_node *get_type_size_node(type_t *type)
273 type = skip_typeref(type);
275 if (is_type_array(type) && type->array.is_vla) {
276 ir_node *size_node = get_vla_size(&type->array);
277 ir_node *elem_size = get_type_size_node(type->array.element_type);
278 ir_mode *mode = get_irn_mode(size_node);
279 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
283 ir_mode *mode = get_ir_mode_storage(type_size_t);
285 sym.type_p = get_ir_type(type);
286 return new_SymConst(mode, sym, symconst_type_size);
289 static unsigned count_parameters(const function_type_t *function_type)
293 function_parameter_t *parameter = function_type->parameters;
294 for ( ; parameter != NULL; parameter = parameter->next) {
302 * Creates a Firm type for an atomic type
304 static ir_type *create_atomic_type(atomic_type_kind_t akind, const type_t *type)
306 ir_mode *mode = atomic_modes[akind];
307 type_dbg_info *dbgi = get_type_dbg_info_(type);
308 ir_type *irtype = new_d_type_primitive(mode, dbgi);
309 il_alignment_t alignment = get_atomic_type_alignment(akind);
311 set_type_alignment_bytes(irtype, alignment);
317 * Creates a Firm type for a complex type
319 static ir_type *create_complex_type(const complex_type_t *type)
321 atomic_type_kind_t kind = type->akind;
322 ir_mode *mode = atomic_modes[kind];
323 ident *id = get_mode_ident(mode);
327 /* FIXME: finish the array */
332 * Creates a Firm type for an imaginary type
334 static ir_type *create_imaginary_type(imaginary_type_t *type)
336 return create_atomic_type(type->akind, (const type_t*) type);
340 * return type of a parameter (and take transparent union gnu extension into
343 static type_t *get_parameter_type(type_t *orig_type)
345 type_t *type = skip_typeref(orig_type);
346 if (is_type_union(type)
347 && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
348 compound_t *compound = type->compound.compound;
349 type = compound->members.entities->declaration.type;
355 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
357 type_t *return_type = skip_typeref(function_type->return_type);
359 int n_parameters = count_parameters(function_type)
360 + (for_closure ? 1 : 0);
361 int n_results = return_type == type_void ? 0 : 1;
362 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) function_type);
363 ir_type *irtype = new_d_type_method(n_parameters, n_results, dbgi);
365 if (return_type != type_void) {
366 ir_type *restype = get_ir_type(return_type);
367 set_method_res_type(irtype, 0, restype);
370 function_parameter_t *parameter = function_type->parameters;
373 ir_type *p_irtype = get_ir_type(type_void_ptr);
374 set_method_param_type(irtype, n, p_irtype);
377 for ( ; parameter != NULL; parameter = parameter->next) {
378 type_t *type = get_parameter_type(parameter->type);
379 ir_type *p_irtype = get_ir_type(type);
380 set_method_param_type(irtype, n, p_irtype);
384 bool is_variadic = function_type->variadic;
387 set_method_variadicity(irtype, variadicity_variadic);
389 unsigned cc = get_method_calling_convention(irtype);
390 switch (function_type->calling_convention) {
391 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
394 set_method_calling_convention(irtype, SET_CDECL(cc));
401 /* only non-variadic function can use stdcall, else use cdecl */
402 set_method_calling_convention(irtype, SET_STDCALL(cc));
408 /* only non-variadic function can use fastcall, else use cdecl */
409 set_method_calling_convention(irtype, SET_FASTCALL(cc));
413 /* Hmm, leave default, not accepted by the parser yet. */
418 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
423 static ir_type *create_pointer_type(pointer_type_t *type)
425 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
426 type_t *points_to = type->points_to;
427 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
428 ir_type *ir_type = new_d_type_pointer(ir_points_to, dbgi);
433 static ir_type *create_reference_type(reference_type_t *type)
435 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
436 type_t *refers_to = type->refers_to;
437 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
438 ir_type *ir_type = new_d_type_pointer(ir_refers_to, dbgi);
443 static ir_type *create_array_type(array_type_t *type)
445 type_dbg_info *dbgi = get_type_dbg_info_((const type_t*) type);
446 type_t *element_type = type->element_type;
447 ir_type *ir_element_type = get_ir_type(element_type);
448 ir_type *ir_type = new_d_type_array(1, ir_element_type, dbgi);
450 const int align = get_type_alignment_bytes(ir_element_type);
451 set_type_alignment_bytes(ir_type, align);
453 if (type->size_constant) {
454 int n_elements = type->size;
456 set_array_bounds_int(ir_type, 0, 0, n_elements);
458 size_t elemsize = get_type_size_bytes(ir_element_type);
459 if (elemsize % align > 0) {
460 elemsize += align - (elemsize % align);
462 set_type_size_bytes(ir_type, n_elements * elemsize);
464 set_array_lower_bound_int(ir_type, 0, 0);
466 set_type_state(ir_type, layout_fixed);
472 * Return the signed integer type of size bits.
474 * @param size the size
476 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
480 static ir_mode *s_modes[64 + 1] = {NULL, };
484 if (size <= 0 || size > 64)
487 mode = s_modes[size];
491 snprintf(name, sizeof(name), "bf_I%u", size);
492 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
493 size <= 32 ? 32 : size );
494 s_modes[size] = mode;
497 type_dbg_info *dbgi = get_type_dbg_info_(type);
498 res = new_d_type_primitive(mode, dbgi);
499 set_primitive_base_type(res, base_tp);
505 * Return the unsigned integer type of size bits.
507 * @param size the size
509 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
513 static ir_mode *u_modes[64 + 1] = {NULL, };
517 if (size <= 0 || size > 64)
520 mode = u_modes[size];
524 snprintf(name, sizeof(name), "bf_U%u", size);
525 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
526 size <= 32 ? 32 : size );
527 u_modes[size] = mode;
530 type_dbg_info *dbgi = get_type_dbg_info_(type);
531 res = new_d_type_primitive(mode, dbgi);
532 set_primitive_base_type(res, base_tp);
537 static ir_type *create_bitfield_type(bitfield_type_t *const type)
539 type_t *base = skip_typeref(type->base_type);
540 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
541 ir_type *irbase = get_ir_type(base);
543 unsigned size = type->bit_size;
545 assert(!is_type_float(base));
546 if (is_type_signed(base)) {
547 return get_signed_int_type_for_bit_size(irbase, size,
548 (const type_t*) type);
550 return get_unsigned_int_type_for_bit_size(irbase, size,
551 (const type_t*) type);
555 #define INVALID_TYPE ((ir_type_ptr)-1)
558 COMPOUND_IS_STRUCT = false,
559 COMPOUND_IS_UNION = true
563 * Construct firm type from ast struct type.
565 static ir_type *create_compound_type(compound_type_t *type,
566 bool incomplete, bool is_union)
568 compound_t *compound = type->compound;
570 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
571 return compound->irtype;
574 symbol_t *symbol = compound->base.symbol;
576 if (symbol != NULL) {
577 id = new_id_from_str(symbol->string);
580 id = id_unique("__anonymous_union.%u");
582 id = id_unique("__anonymous_struct.%u");
588 irtype = new_type_union(id);
590 irtype = new_type_struct(id);
593 compound->irtype_complete = false;
594 compound->irtype = irtype;
600 layout_union_type(type);
602 layout_struct_type(type);
605 compound->irtype_complete = true;
607 entity_t *entry = compound->members.entities;
608 for ( ; entry != NULL; entry = entry->base.next) {
609 if (entry->kind != ENTITY_COMPOUND_MEMBER)
612 symbol_t *symbol = entry->base.symbol;
613 type_t *entry_type = entry->declaration.type;
615 if (symbol == NULL) {
616 /* anonymous bitfield member, skip */
617 if (entry_type->kind == TYPE_BITFIELD)
619 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
620 || entry_type->kind == TYPE_COMPOUND_UNION);
621 ident = id_unique("anon.%u");
623 ident = new_id_from_str(symbol->string);
626 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
628 ir_type *entry_irtype = get_ir_type(entry_type);
629 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
631 set_entity_offset(entity, entry->compound_member.offset);
632 set_entity_offset_bits_remainder(entity,
633 entry->compound_member.bit_offset);
635 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
636 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
637 entry->compound_member.entity = entity;
640 set_type_alignment_bytes(irtype, compound->alignment);
641 set_type_size_bytes(irtype, compound->size);
642 set_type_state(irtype, layout_fixed);
647 static ir_type *create_enum_type(enum_type_t *const type)
649 type->base.firm_type = ir_type_int;
651 ir_mode *const mode = mode_int;
652 tarval *const one = get_mode_one(mode);
653 tarval * tv_next = get_mode_null(mode);
655 bool constant_folding_old = constant_folding;
656 constant_folding = true;
658 enum_t *enume = type->enume;
659 entity_t *entry = enume->base.next;
660 for (; entry != NULL; entry = entry->base.next) {
661 if (entry->kind != ENTITY_ENUM_VALUE)
664 expression_t *const init = entry->enum_value.value;
666 ir_node *const cnst = expression_to_firm(init);
667 if (!is_Const(cnst)) {
668 panic("couldn't fold constant");
670 tv_next = get_Const_tarval(cnst);
672 entry->enum_value.tv = tv_next;
673 tv_next = tarval_add(tv_next, one);
676 constant_folding = constant_folding_old;
678 return create_atomic_type(type->akind, (const type_t*) type);
681 static ir_type *get_ir_type_incomplete(type_t *type)
683 assert(type != NULL);
684 type = skip_typeref(type);
686 if (type->base.firm_type != NULL) {
687 assert(type->base.firm_type != INVALID_TYPE);
688 return type->base.firm_type;
691 switch (type->kind) {
692 case TYPE_COMPOUND_STRUCT:
693 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
694 case TYPE_COMPOUND_UNION:
695 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
697 return get_ir_type(type);
701 ir_type *get_ir_type(type_t *type)
703 assert(type != NULL);
705 type = skip_typeref(type);
707 if (type->base.firm_type != NULL) {
708 assert(type->base.firm_type != INVALID_TYPE);
709 return type->base.firm_type;
712 ir_type *firm_type = NULL;
713 switch (type->kind) {
715 /* Happens while constant folding, when there was an error */
716 return create_atomic_type(ATOMIC_TYPE_VOID, NULL);
719 firm_type = create_atomic_type(type->atomic.akind, type);
722 firm_type = create_complex_type(&type->complex);
725 firm_type = create_imaginary_type(&type->imaginary);
728 firm_type = create_method_type(&type->function, false);
731 firm_type = create_pointer_type(&type->pointer);
734 firm_type = create_reference_type(&type->reference);
737 firm_type = create_array_type(&type->array);
739 case TYPE_COMPOUND_STRUCT:
740 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
742 case TYPE_COMPOUND_UNION:
743 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
746 firm_type = create_enum_type(&type->enumt);
749 firm_type = get_ir_type(type->builtin.real_type);
752 firm_type = create_bitfield_type(&type->bitfield);
760 if (firm_type == NULL)
761 panic("unknown type found");
763 type->base.firm_type = firm_type;
767 static ir_mode *get_ir_mode_storage(type_t *type)
769 ir_type *irtype = get_ir_type(type);
771 /* firm doesn't report a mode for arrays somehow... */
772 if (is_Array_type(irtype)) {
776 ir_mode *mode = get_type_mode(irtype);
777 assert(mode != NULL);
781 static ir_mode *get_ir_mode_arithmetic(type_t *type)
783 ir_mode *mode = get_ir_mode_storage(type);
784 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
785 return mode_float_arithmetic;
791 /** Names of the runtime functions. */
792 static const struct {
793 int id; /**< the rts id */
794 int n_res; /**< number of return values */
795 const char *name; /**< the name of the rts function */
796 int n_params; /**< number of parameters */
797 unsigned flags; /**< language flags */
799 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
800 { rts_abort, 0, "abort", 0, _C89 },
801 { rts_alloca, 1, "alloca", 1, _ALL },
802 { rts_abs, 1, "abs", 1, _C89 },
803 { rts_labs, 1, "labs", 1, _C89 },
804 { rts_llabs, 1, "llabs", 1, _C99 },
805 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
807 { rts_fabs, 1, "fabs", 1, _C89 },
808 { rts_sqrt, 1, "sqrt", 1, _C89 },
809 { rts_cbrt, 1, "cbrt", 1, _C99 },
810 { rts_exp, 1, "exp", 1, _C89 },
811 { rts_exp2, 1, "exp2", 1, _C89 },
812 { rts_exp10, 1, "exp10", 1, _GNUC },
813 { rts_log, 1, "log", 1, _C89 },
814 { rts_log2, 1, "log2", 1, _C89 },
815 { rts_log10, 1, "log10", 1, _C89 },
816 { rts_pow, 1, "pow", 2, _C89 },
817 { rts_sin, 1, "sin", 1, _C89 },
818 { rts_cos, 1, "cos", 1, _C89 },
819 { rts_tan, 1, "tan", 1, _C89 },
820 { rts_asin, 1, "asin", 1, _C89 },
821 { rts_acos, 1, "acos", 1, _C89 },
822 { rts_atan, 1, "atan", 1, _C89 },
823 { rts_sinh, 1, "sinh", 1, _C89 },
824 { rts_cosh, 1, "cosh", 1, _C89 },
825 { rts_tanh, 1, "tanh", 1, _C89 },
827 { rts_fabsf, 1, "fabsf", 1, _C99 },
828 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
829 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
830 { rts_expf, 1, "expf", 1, _C99 },
831 { rts_exp2f, 1, "exp2f", 1, _C99 },
832 { rts_exp10f, 1, "exp10f", 1, _GNUC },
833 { rts_logf, 1, "logf", 1, _C99 },
834 { rts_log2f, 1, "log2f", 1, _C99 },
835 { rts_log10f, 1, "log10f", 1, _C99 },
836 { rts_powf, 1, "powf", 2, _C99 },
837 { rts_sinf, 1, "sinf", 1, _C99 },
838 { rts_cosf, 1, "cosf", 1, _C99 },
839 { rts_tanf, 1, "tanf", 1, _C99 },
840 { rts_asinf, 1, "asinf", 1, _C99 },
841 { rts_acosf, 1, "acosf", 1, _C99 },
842 { rts_atanf, 1, "atanf", 1, _C99 },
843 { rts_sinhf, 1, "sinhf", 1, _C99 },
844 { rts_coshf, 1, "coshf", 1, _C99 },
845 { rts_tanhf, 1, "tanhf", 1, _C99 },
847 { rts_fabsl, 1, "fabsl", 1, _C99 },
848 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
849 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
850 { rts_expl, 1, "expl", 1, _C99 },
851 { rts_exp2l, 1, "exp2l", 1, _C99 },
852 { rts_exp10l, 1, "exp10l", 1, _GNUC },
853 { rts_logl, 1, "logl", 1, _C99 },
854 { rts_log2l, 1, "log2l", 1, _C99 },
855 { rts_log10l, 1, "log10l", 1, _C99 },
856 { rts_powl, 1, "powl", 2, _C99 },
857 { rts_sinl, 1, "sinl", 1, _C99 },
858 { rts_cosl, 1, "cosl", 1, _C99 },
859 { rts_tanl, 1, "tanl", 1, _C99 },
860 { rts_asinl, 1, "asinl", 1, _C99 },
861 { rts_acosl, 1, "acosl", 1, _C99 },
862 { rts_atanl, 1, "atanl", 1, _C99 },
863 { rts_sinhl, 1, "sinhl", 1, _C99 },
864 { rts_coshl, 1, "coshl", 1, _C99 },
865 { rts_tanhl, 1, "tanhl", 1, _C99 },
867 { rts_strcmp, 1, "strcmp", 2, _C89 },
868 { rts_strncmp, 1, "strncmp", 3, _C89 },
869 { rts_strcpy, 1, "strcpy", 2, _C89 },
870 { rts_strlen, 1, "strlen", 1, _C89 },
871 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
872 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
873 { rts_memmove, 1, "memmove", 3, _C89 }, /* HMM, man say its C99 */
874 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
875 { rts_memcmp, 1, "memcmp", 3, _C89 }, /* HMM, man say its C99 */
878 static ident *rts_idents[lengthof(rts_data)];
880 static ident* (*create_ld_ident)(entity_t*) = create_name_linux_elf;
883 * Handle GNU attributes for entities
885 * @param ent the entity
886 * @param decl the routine declaration
888 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
890 assert(is_declaration(entity));
891 decl_modifiers_t modifiers = entity->declaration.modifiers;
893 if (is_method_entity(irentity)) {
894 if (modifiers & DM_PURE) {
895 set_entity_additional_property(irentity, mtp_property_pure);
897 if (modifiers & DM_CONST) {
898 set_entity_additional_property(irentity, mtp_property_const);
899 have_const_functions = true;
902 if (modifiers & DM_USED) {
903 add_entity_linkage(irentity, IR_LINKAGE_HIDDEN_USER);
905 if (modifiers & DM_WEAK) {
906 add_entity_linkage(irentity, IR_LINKAGE_WEAK);
910 static bool is_main(entity_t *entity)
912 static symbol_t *sym_main = NULL;
913 if (sym_main == NULL) {
914 sym_main = symbol_table_insert("main");
917 if (entity->base.symbol != sym_main)
919 /* must be in outermost scope */
920 if (entity->base.parent_scope != ¤t_translation_unit->scope)
927 * Creates an entity representing a function.
929 * @param entity the function declaration/definition
930 * @param owner_type the owner type of this function, NULL
931 * for global functions
933 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
935 assert(entity->kind == ENTITY_FUNCTION);
936 if (entity->function.irentity != NULL) {
937 return entity->function.irentity;
940 entity_t *original_entity = entity;
941 if (entity->function.btk != bk_none) {
942 entity = get_builtin_replacement(entity);
947 if (is_main(entity)) {
948 /* force main to C linkage */
949 type_t *type = entity->declaration.type;
950 assert(is_type_function(type));
951 if (type->function.linkage != LINKAGE_C) {
952 type_t *new_type = duplicate_type(type);
953 new_type->function.linkage = LINKAGE_C;
954 type = identify_new_type(new_type);
955 entity->declaration.type = type;
959 symbol_t *symbol = entity->base.symbol;
960 ident *id = new_id_from_str(symbol->string);
962 /* already an entity defined? */
963 ir_entity *irentity = entitymap_get(&entitymap, symbol);
964 bool const has_body = entity->function.statement != NULL;
965 if (irentity != NULL) {
966 if (get_entity_visibility(irentity) == ir_visibility_external
968 set_entity_visibility(irentity, ir_visibility_default);
973 ir_type *ir_type_method;
974 if (entity->function.need_closure)
975 ir_type_method = create_method_type(&entity->declaration.type->function, true);
977 ir_type_method = get_ir_type(entity->declaration.type);
979 bool nested_function = false;
980 if (owner_type == NULL)
981 owner_type = get_glob_type();
983 nested_function = true;
985 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
986 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
990 ld_id = id_unique("inner.%u");
992 ld_id = create_ld_ident(entity);
993 set_entity_ld_ident(irentity, ld_id);
995 handle_decl_modifiers(irentity, entity);
997 if (! nested_function) {
998 /* static inline => local
999 * extern inline => local
1000 * inline without definition => local
1001 * inline with definition => external_visible */
1002 storage_class_tag_t const storage_class
1003 = (storage_class_tag_t) entity->declaration.storage_class;
1004 bool const is_inline = entity->function.is_inline;
1006 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1007 set_entity_visibility(irentity, ir_visibility_default);
1008 } else if (storage_class == STORAGE_CLASS_STATIC ||
1009 (is_inline && has_body)) {
1010 set_entity_visibility(irentity, ir_visibility_local);
1011 } else if (has_body) {
1012 set_entity_visibility(irentity, ir_visibility_default);
1014 set_entity_visibility(irentity, ir_visibility_external);
1017 /* nested functions are always local */
1018 set_entity_visibility(irentity, ir_visibility_local);
1021 /* We should check for file scope here, but as long as we compile C only
1022 this is not needed. */
1023 if (! firm_opt.freestanding && !has_body) {
1024 /* check for a known runtime function */
1025 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1026 if (id != rts_idents[i])
1029 /* ignore those rts functions not necessary needed for current mode */
1030 if ((c_mode & rts_data[i].flags) == 0)
1032 assert(rts_entities[rts_data[i].id] == NULL);
1033 rts_entities[rts_data[i].id] = irentity;
1037 entitymap_insert(&entitymap, symbol, irentity);
1040 original_entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1041 original_entity->function.irentity = irentity;
1047 * Creates a SymConst for a given entity.
1049 * @param dbgi debug info
1050 * @param mode the (reference) mode for the SymConst
1051 * @param entity the entity
1053 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1056 assert(entity != NULL);
1057 union symconst_symbol sym;
1058 sym.entity_p = entity;
1059 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1062 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1064 ir_mode *value_mode = get_irn_mode(value);
1066 if (value_mode == dest_mode || is_Bad(value))
1069 if (dest_mode == mode_b) {
1070 ir_node *zero = new_Const(get_mode_null(value_mode));
1071 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1072 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1076 return new_d_Conv(dbgi, value, dest_mode);
1080 * Creates a SymConst node representing a wide string literal.
1082 * @param literal the wide string literal
1084 static ir_node *wide_string_literal_to_firm(
1085 const string_literal_expression_t *literal)
1087 ir_type *const global_type = get_glob_type();
1088 ir_type *const elem_type = ir_type_wchar_t;
1089 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1090 ir_type *const type = new_type_array(1, elem_type);
1092 ident *const id = id_unique("str.%u");
1093 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1094 set_entity_ld_ident(entity, id);
1095 set_entity_visibility(entity, ir_visibility_private);
1096 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1098 ir_mode *const mode = get_type_mode(elem_type);
1099 const size_t slen = wstrlen(&literal->value);
1101 set_array_lower_bound_int(type, 0, 0);
1102 set_array_upper_bound_int(type, 0, slen);
1103 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1104 set_type_state(type, layout_fixed);
1106 ir_initializer_t *initializer = create_initializer_compound(slen);
1107 const char *p = literal->value.begin;
1108 for (size_t i = 0; i < slen; ++i) {
1109 assert(p < literal->value.begin + literal->value.size);
1110 utf32 v = read_utf8_char(&p);
1111 tarval *tv = new_tarval_from_long(v, mode);
1112 ir_initializer_t *val = create_initializer_tarval(tv);
1113 set_initializer_compound_value(initializer, i, val);
1115 set_entity_initializer(entity, initializer);
1117 return create_symconst(dbgi, mode_P_data, entity);
1121 * Creates a SymConst node representing a string constant.
1123 * @param src_pos the source position of the string constant
1124 * @param id_prefix a prefix for the name of the generated string constant
1125 * @param value the value of the string constant
1127 static ir_node *string_to_firm(const source_position_t *const src_pos,
1128 const char *const id_prefix,
1129 const string_t *const value)
1131 ir_type *const global_type = get_glob_type();
1132 dbg_info *const dbgi = get_dbg_info(src_pos);
1133 ir_type *const type = new_type_array(1, ir_type_const_char);
1135 ident *const id = id_unique(id_prefix);
1136 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1137 set_entity_ld_ident(entity, id);
1138 set_entity_visibility(entity, ir_visibility_private);
1139 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1141 ir_type *const elem_type = ir_type_const_char;
1142 ir_mode *const mode = get_type_mode(elem_type);
1144 const char* const string = value->begin;
1145 const size_t slen = value->size;
1147 set_array_lower_bound_int(type, 0, 0);
1148 set_array_upper_bound_int(type, 0, slen);
1149 set_type_size_bytes(type, slen);
1150 set_type_state(type, layout_fixed);
1152 ir_initializer_t *initializer = create_initializer_compound(slen);
1153 for (size_t i = 0; i < slen; ++i) {
1154 tarval *tv = new_tarval_from_long(string[i], mode);
1155 ir_initializer_t *val = create_initializer_tarval(tv);
1156 set_initializer_compound_value(initializer, i, val);
1158 set_entity_initializer(entity, initializer);
1160 return create_symconst(dbgi, mode_P_data, entity);
1163 static bool try_create_integer(literal_expression_t *literal,
1164 type_t *type, unsigned char base)
1166 const char *string = literal->value.begin;
1167 size_t size = literal->value.size;
1169 assert(type->kind == TYPE_ATOMIC);
1170 atomic_type_kind_t akind = type->atomic.akind;
1172 ir_mode *mode = atomic_modes[akind];
1173 tarval *tv = new_integer_tarval_from_str(string, size, 1, base, mode);
1174 if (tv == tarval_bad)
1177 literal->base.type = type;
1178 literal->target_value = tv;
1182 static void create_integer_tarval(literal_expression_t *literal)
1186 symbol_t *suffix = literal->suffix;
1188 if (suffix != NULL) {
1189 for (const char *c = suffix->string; *c != '\0'; ++c) {
1190 if (*c == 'u' || *c == 'U') { ++us; }
1191 if (*c == 'l' || *c == 'L') { ++ls; }
1196 switch (literal->base.kind) {
1197 case EXPR_LITERAL_INTEGER_OCTAL: base = 8; break;
1198 case EXPR_LITERAL_INTEGER: base = 10; break;
1199 case EXPR_LITERAL_INTEGER_HEXADECIMAL: base = 16; break;
1200 default: panic("invalid literal kind");
1203 tarval_int_overflow_mode_t old_mode = tarval_get_integer_overflow_mode();
1205 /* now try if the constant is small enough for some types */
1206 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1208 if (us == 0 && try_create_integer(literal, type_int, base))
1210 if ((us == 1 || base != 10)
1211 && try_create_integer(literal, type_unsigned_int, base))
1215 if (us == 0 && try_create_integer(literal, type_long, base))
1217 if ((us == 1 || base != 10)
1218 && try_create_integer(literal, type_unsigned_long, base))
1221 /* last try? then we should not report tarval_bad */
1222 if (us != 1 && base == 10)
1223 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1224 if (us == 0 && try_create_integer(literal, type_long_long, base))
1228 assert(us == 1 || base != 10);
1229 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
1230 bool res = try_create_integer(literal, type_unsigned_long_long, base);
1232 panic("internal error when parsing number literal");
1235 tarval_set_integer_overflow_mode(old_mode);
1238 void determine_literal_type(literal_expression_t *literal)
1240 switch (literal->base.kind) {
1241 case EXPR_LITERAL_INTEGER:
1242 case EXPR_LITERAL_INTEGER_OCTAL:
1243 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1244 create_integer_tarval(literal);
1252 * Creates a Const node representing a constant.
1254 static ir_node *literal_to_firm(const literal_expression_t *literal)
1256 type_t *type = skip_typeref(literal->base.type);
1257 ir_mode *mode = get_ir_mode_storage(type);
1258 const char *string = literal->value.begin;
1259 size_t size = literal->value.size;
1262 switch (literal->base.kind) {
1263 case EXPR_LITERAL_WIDE_CHARACTER: {
1264 utf32 v = read_utf8_char(&string);
1266 size_t len = snprintf(buf, sizeof(buf), UTF32_PRINTF_FORMAT, v);
1268 tv = new_tarval_from_str(buf, len, mode);
1271 case EXPR_LITERAL_CHARACTER: {
1273 if (size == 1 && char_is_signed) {
1274 v = (signed char)string[0];
1277 for (size_t i = 0; i < size; ++i) {
1278 v = (v << 8) | ((unsigned char)string[i]);
1282 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1284 tv = new_tarval_from_str(buf, len, mode);
1287 case EXPR_LITERAL_INTEGER:
1288 case EXPR_LITERAL_INTEGER_OCTAL:
1289 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
1290 assert(literal->target_value != NULL);
1291 tv = literal->target_value;
1293 case EXPR_LITERAL_FLOATINGPOINT:
1294 tv = new_tarval_from_str(string, size, mode);
1296 case EXPR_LITERAL_FLOATINGPOINT_HEXADECIMAL: {
1297 char buffer[size + 2];
1298 memcpy(buffer, "0x", 2);
1299 memcpy(buffer+2, string, size);
1300 tv = new_tarval_from_str(buffer, size+2, mode);
1303 case EXPR_LITERAL_BOOLEAN:
1304 if (string[0] == 't') {
1305 tv = get_mode_one(mode);
1307 assert(string[0] == 'f');
1308 tv = get_mode_null(mode);
1311 case EXPR_LITERAL_MS_NOOP:
1312 tv = get_mode_null(mode);
1317 panic("Invalid literal kind found");
1320 dbg_info *dbgi = get_dbg_info(&literal->base.source_position);
1321 ir_node *res = new_d_Const(dbgi, tv);
1322 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1323 return create_conv(dbgi, res, mode_arith);
1327 * Allocate an area of size bytes aligned at alignment
1330 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1332 static unsigned area_cnt = 0;
1335 ir_type *tp = new_type_array(1, ir_type_char);
1336 set_array_bounds_int(tp, 0, 0, size);
1337 set_type_alignment_bytes(tp, alignment);
1339 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1340 ident *name = new_id_from_str(buf);
1341 ir_entity *area = new_entity(frame_type, name, tp);
1343 /* mark this entity as compiler generated */
1344 set_entity_compiler_generated(area, 1);
1349 * Return a node representing a trampoline region
1350 * for a given function entity.
1352 * @param dbgi debug info
1353 * @param entity the function entity
1355 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1357 ir_entity *region = NULL;
1360 if (current_trampolines != NULL) {
1361 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1362 if (current_trampolines[i].function == entity) {
1363 region = current_trampolines[i].region;
1368 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1370 ir_graph *irg = current_ir_graph;
1371 if (region == NULL) {
1372 /* create a new region */
1373 ir_type *frame_tp = get_irg_frame_type(irg);
1374 trampoline_region reg;
1375 reg.function = entity;
1377 reg.region = alloc_trampoline(frame_tp,
1378 be_params->trampoline_size,
1379 be_params->trampoline_align);
1380 ARR_APP1(trampoline_region, current_trampolines, reg);
1381 region = reg.region;
1383 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1388 * Creates a trampoline for a function represented by an entity.
1390 * @param dbgi debug info
1391 * @param mode the (reference) mode for the function address
1392 * @param entity the function entity
1394 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1397 assert(entity != NULL);
1399 in[0] = get_trampoline_region(dbgi, entity);
1400 in[1] = create_symconst(dbgi, mode, entity);
1401 in[2] = get_irg_frame(current_ir_graph);
1403 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1404 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1405 return new_Proj(irn, mode, pn_Builtin_1_result);
1409 * Dereference an address.
1411 * @param dbgi debug info
1412 * @param type the type of the dereferenced result (the points_to type)
1413 * @param addr the address to dereference
1415 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1416 ir_node *const addr)
1418 ir_type *irtype = get_ir_type(type);
1419 if (is_compound_type(irtype)
1420 || is_Method_type(irtype)
1421 || is_Array_type(irtype)) {
1425 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1426 ? cons_volatile : cons_none;
1427 ir_mode *const mode = get_type_mode(irtype);
1428 ir_node *const memory = get_store();
1429 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1430 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1431 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1433 set_store(load_mem);
1435 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1436 return create_conv(dbgi, load_res, mode_arithmetic);
1440 * Creates a strict Conv (to the node's mode) if necessary.
1442 * @param dbgi debug info
1443 * @param node the node to strict conv
1445 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1447 ir_mode *mode = get_irn_mode(node);
1449 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1451 if (!mode_is_float(mode))
1454 /* check if there is already a Conv */
1455 if (is_Conv(node)) {
1456 /* convert it into a strict Conv */
1457 set_Conv_strict(node, 1);
1461 /* otherwise create a new one */
1462 return new_d_strictConv(dbgi, node, mode);
1466 * Returns the address of a global variable.
1468 * @param dbgi debug info
1469 * @param variable the variable
1471 static ir_node *get_global_var_address(dbg_info *const dbgi,
1472 const variable_t *const variable)
1474 ir_entity *const irentity = variable->v.entity;
1475 if (variable->thread_local) {
1476 ir_node *const no_mem = new_NoMem();
1477 ir_node *const tls = get_irg_tls(current_ir_graph);
1478 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1480 return create_symconst(dbgi, mode_P_data, irentity);
1485 * Returns the correct base address depending on whether it is a parameter or a
1486 * normal local variable.
1488 static ir_node *get_local_frame(ir_entity *const ent)
1490 ir_graph *const irg = current_ir_graph;
1491 const ir_type *const owner = get_entity_owner(ent);
1492 if (owner == current_outer_frame || owner == current_outer_value_type) {
1493 assert(current_static_link != NULL);
1494 return current_static_link;
1496 return get_irg_frame(irg);
1501 * Keep all memory edges of the given block.
1503 static void keep_all_memory(ir_node *block)
1505 ir_node *old = get_cur_block();
1507 set_cur_block(block);
1508 keep_alive(get_store());
1509 /* TODO: keep all memory edges from restricted pointers */
1513 static ir_node *reference_expression_enum_value_to_firm(
1514 const reference_expression_t *ref)
1516 entity_t *entity = ref->entity;
1517 type_t *type = skip_typeref(entity->enum_value.enum_type);
1518 /* make sure the type is constructed */
1519 (void) get_ir_type(type);
1521 return new_Const(entity->enum_value.tv);
1524 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1526 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1527 entity_t *entity = ref->entity;
1528 assert(is_declaration(entity));
1529 type_t *type = skip_typeref(entity->declaration.type);
1531 /* make sure the type is constructed */
1532 (void) get_ir_type(type);
1534 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1535 ir_entity *irentity = get_function_entity(entity, NULL);
1536 /* for gcc compatibility we have to produce (dummy) addresses for some
1537 * builtins which don't have entities */
1538 if (irentity == NULL) {
1539 if (warning.other) {
1540 warningf(&ref->base.source_position,
1541 "taking address of builtin '%Y'",
1542 ref->entity->base.symbol);
1545 /* simply create a NULL pointer */
1546 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1547 ir_node *res = new_Const_long(mode, 0);
1553 switch ((declaration_kind_t) entity->declaration.kind) {
1554 case DECLARATION_KIND_UNKNOWN:
1557 case DECLARATION_KIND_LOCAL_VARIABLE: {
1558 ir_mode *const mode = get_ir_mode_storage(type);
1559 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1560 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1562 case DECLARATION_KIND_PARAMETER: {
1563 ir_mode *const mode = get_ir_mode_storage(type);
1564 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1565 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1567 case DECLARATION_KIND_FUNCTION: {
1568 ir_mode *const mode = get_ir_mode_storage(type);
1569 return create_symconst(dbgi, mode, 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, mode, 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 = get_global_var_address(dbgi, variable);
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 = get_global_var_address(dbgi, &entity->variable);
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 type_t *const type = skip_typeref(entity->declaration.type);
1648 ir_mode *const mode = get_ir_mode_storage(type);
1649 return create_symconst(dbgi, mode, entity->function.irentity);
1652 case DECLARATION_KIND_INNER_FUNCTION: {
1653 type_t *const type = skip_typeref(entity->declaration.type);
1654 ir_mode *const mode = get_ir_mode_storage(type);
1655 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1656 /* inner function not using the closure */
1657 return create_symconst(dbgi, mode, entity->function.irentity);
1659 /* need trampoline here */
1660 return create_trampoline(dbgi, mode, entity->function.irentity);
1664 case DECLARATION_KIND_COMPOUND_MEMBER:
1665 panic("not implemented reference type");
1668 panic("reference to declaration with unknown type found");
1672 * Generate an unary builtin.
1674 * @param kind the builtin kind to generate
1675 * @param op the operand
1676 * @param function_type the function type for the GNU builtin routine
1677 * @param db debug info
1679 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1682 in[0] = expression_to_firm(op);
1684 ir_type *tp = get_ir_type(function_type);
1685 ir_type *res = get_method_res_type(tp, 0);
1686 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1687 set_irn_pinned(irn, op_pin_state_floats);
1688 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1692 * Generate a pinned unary builtin.
1694 * @param kind the builtin kind to generate
1695 * @param op the operand
1696 * @param function_type the function type for the GNU builtin routine
1697 * @param db debug info
1699 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op,
1700 type_t *function_type, dbg_info *db)
1703 in[0] = expression_to_firm(op);
1705 ir_type *tp = get_ir_type(function_type);
1706 ir_type *res = get_method_res_type(tp, 0);
1707 ir_node *mem = get_store();
1708 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1709 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1710 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1714 * Generate an binary-void-return builtin.
1716 * @param kind the builtin kind to generate
1717 * @param op1 the first operand
1718 * @param op2 the second operand
1719 * @param function_type the function type for the GNU builtin routine
1720 * @param db debug info
1722 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1,
1723 expression_t *op2, type_t *function_type,
1727 in[0] = expression_to_firm(op1);
1728 in[1] = expression_to_firm(op2);
1730 ir_type *tp = get_ir_type(function_type);
1731 ir_node *mem = get_store();
1732 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1733 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1738 * Transform calls to builtin functions.
1740 static ir_node *process_builtin_call(const call_expression_t *call)
1742 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1744 assert(call->function->kind == EXPR_REFERENCE);
1745 reference_expression_t *builtin = &call->function->reference;
1747 type_t *type = skip_typeref(builtin->base.type);
1748 assert(is_type_pointer(type));
1750 type_t *function_type = skip_typeref(type->pointer.points_to);
1752 switch (builtin->entity->function.btk) {
1753 case bk_gnu_builtin_alloca: {
1754 if (call->arguments == NULL || call->arguments->next != NULL) {
1755 panic("invalid number of parameters on __builtin_alloca");
1757 expression_t *argument = call->arguments->expression;
1758 ir_node *size = expression_to_firm(argument);
1760 ir_node *store = get_store();
1761 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1763 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1765 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1770 case bk_gnu_builtin_huge_val:
1771 case bk_gnu_builtin_huge_valf:
1772 case bk_gnu_builtin_huge_vall:
1773 case bk_gnu_builtin_inf:
1774 case bk_gnu_builtin_inff:
1775 case bk_gnu_builtin_infl: {
1776 type_t *type = function_type->function.return_type;
1777 ir_mode *mode = get_ir_mode_arithmetic(type);
1778 tarval *tv = get_mode_infinite(mode);
1779 ir_node *res = new_d_Const(dbgi, tv);
1782 case bk_gnu_builtin_nan:
1783 case bk_gnu_builtin_nanf:
1784 case bk_gnu_builtin_nanl: {
1785 /* Ignore string for now... */
1786 assert(is_type_function(function_type));
1787 type_t *type = function_type->function.return_type;
1788 ir_mode *mode = get_ir_mode_arithmetic(type);
1789 tarval *tv = get_mode_NAN(mode);
1790 ir_node *res = new_d_Const(dbgi, tv);
1793 case bk_gnu_builtin_expect: {
1794 expression_t *argument = call->arguments->expression;
1795 return _expression_to_firm(argument);
1797 case bk_gnu_builtin_va_end:
1798 /* evaluate the argument of va_end for its side effects */
1799 _expression_to_firm(call->arguments->expression);
1801 case bk_gnu_builtin_frame_address: {
1802 expression_t *const expression = call->arguments->expression;
1803 bool val = fold_constant_to_bool(expression);
1806 return get_irg_frame(current_ir_graph);
1808 /* get the argument */
1811 in[0] = expression_to_firm(expression);
1812 in[1] = get_irg_frame(current_ir_graph);
1813 ir_type *tp = get_ir_type(function_type);
1814 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1815 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1818 case bk_gnu_builtin_return_address: {
1820 expression_t *const expression = call->arguments->expression;
1823 in[0] = expression_to_firm(expression);
1824 in[1] = get_irg_frame(current_ir_graph);
1825 ir_type *tp = get_ir_type(function_type);
1826 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1827 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1829 case bk_gnu_builtin_ffs:
1830 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1831 case bk_gnu_builtin_clz:
1832 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1833 case bk_gnu_builtin_ctz:
1834 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1835 case bk_gnu_builtin_popcount:
1836 case bk_ms__popcount:
1837 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1838 case bk_gnu_builtin_parity:
1839 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1840 case bk_gnu_builtin_prefetch: {
1841 call_argument_t *const args = call->arguments;
1842 expression_t *const addr = args->expression;
1845 in[0] = _expression_to_firm(addr);
1846 if (args->next != NULL) {
1847 expression_t *const rw = args->next->expression;
1849 in[1] = _expression_to_firm(rw);
1851 if (args->next->next != NULL) {
1852 expression_t *const locality = args->next->next->expression;
1854 in[2] = expression_to_firm(locality);
1856 in[2] = new_Const_long(mode_int, 3);
1859 in[1] = new_Const_long(mode_int, 0);
1860 in[2] = new_Const_long(mode_int, 3);
1862 ir_type *tp = get_ir_type(function_type);
1863 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1864 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1867 case bk_gnu_builtin_trap:
1870 ir_type *tp = get_ir_type(function_type);
1871 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1872 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1875 case bk_ms__debugbreak: {
1876 ir_type *tp = get_ir_type(function_type);
1877 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1878 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1881 case bk_ms_ReturnAddress: {
1884 in[0] = new_Const_long(mode_int, 0);
1885 in[1] = get_irg_frame(current_ir_graph);
1886 ir_type *tp = get_ir_type(function_type);
1887 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1888 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1891 case bk_ms_rotl64: {
1892 ir_node *val = expression_to_firm(call->arguments->expression);
1893 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1894 ir_mode *mode = get_irn_mode(val);
1895 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1898 case bk_ms_rotr64: {
1899 ir_node *val = expression_to_firm(call->arguments->expression);
1900 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1901 ir_mode *mode = get_irn_mode(val);
1902 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1903 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1904 return new_d_Rotl(dbgi, val, sub, mode);
1906 case bk_ms_byteswap_ushort:
1907 case bk_ms_byteswap_ulong:
1908 case bk_ms_byteswap_uint64:
1909 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1912 case bk_ms__indword:
1913 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1914 case bk_ms__outbyte:
1915 case bk_ms__outword:
1916 case bk_ms__outdword:
1917 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1918 call->arguments->next->expression, function_type, dbgi);
1920 panic("unsupported builtin found");
1925 * Transform a call expression.
1926 * Handles some special cases, like alloca() calls, which must be resolved
1927 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1928 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1931 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1933 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1934 assert(get_cur_block() != NULL);
1936 expression_t *function = call->function;
1937 if (function->kind == EXPR_REFERENCE) {
1938 const reference_expression_t *ref = &function->reference;
1939 entity_t *entity = ref->entity;
1941 if (entity->kind == ENTITY_FUNCTION) {
1942 ir_entity *irentity = entity->function.irentity;
1943 if (irentity == NULL)
1944 irentity = get_function_entity(entity, NULL);
1946 if (irentity == NULL && entity->function.btk != bk_none) {
1947 return process_builtin_call(call);
1950 if (irentity == rts_entities[rts_alloca]) {
1951 /* handle alloca() call */
1952 expression_t *argument = call->arguments->expression;
1953 ir_node *size = expression_to_firm(argument);
1954 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1956 size = create_conv(dbgi, size, mode);
1958 ir_node *store = get_store();
1959 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1960 firm_unknown_type, stack_alloc);
1961 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1963 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1969 ir_node *callee = expression_to_firm(function);
1971 type_t *type = skip_typeref(function->base.type);
1972 assert(is_type_pointer(type));
1973 pointer_type_t *pointer_type = &type->pointer;
1974 type_t *points_to = skip_typeref(pointer_type->points_to);
1975 assert(is_type_function(points_to));
1976 function_type_t *function_type = &points_to->function;
1978 int n_parameters = 0;
1979 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1980 ir_type *new_method_type = NULL;
1981 if (function_type->variadic || function_type->unspecified_parameters) {
1982 const call_argument_t *argument = call->arguments;
1983 for ( ; argument != NULL; argument = argument->next) {
1987 /* we need to construct a new method type matching the call
1989 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
1990 int n_res = get_method_n_ress(ir_method_type);
1991 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
1992 set_method_calling_convention(new_method_type,
1993 get_method_calling_convention(ir_method_type));
1994 set_method_additional_properties(new_method_type,
1995 get_method_additional_properties(ir_method_type));
1996 set_method_variadicity(new_method_type,
1997 get_method_variadicity(ir_method_type));
1999 for (int i = 0; i < n_res; ++i) {
2000 set_method_res_type(new_method_type, i,
2001 get_method_res_type(ir_method_type, i));
2003 argument = call->arguments;
2004 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
2005 expression_t *expression = argument->expression;
2006 ir_type *irtype = get_ir_type(expression->base.type);
2007 set_method_param_type(new_method_type, i, irtype);
2009 ir_method_type = new_method_type;
2011 n_parameters = get_method_n_params(ir_method_type);
2014 ir_node *in[n_parameters];
2016 const call_argument_t *argument = call->arguments;
2017 for (int n = 0; n < n_parameters; ++n) {
2018 expression_t *expression = argument->expression;
2019 ir_node *arg_node = expression_to_firm(expression);
2021 type_t *type = skip_typeref(expression->base.type);
2022 if (!is_type_compound(type)) {
2023 ir_mode *mode = get_ir_mode_storage(expression->base.type);
2024 arg_node = create_conv(dbgi, arg_node, mode);
2025 arg_node = do_strict_conv(dbgi, arg_node);
2030 argument = argument->next;
2033 ir_node *store = get_store();
2034 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
2036 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
2039 type_t *return_type = skip_typeref(function_type->return_type);
2040 ir_node *result = NULL;
2042 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
2043 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
2045 if (is_type_scalar(return_type)) {
2046 ir_mode *mode = get_ir_mode_storage(return_type);
2047 result = new_d_Proj(dbgi, resproj, mode, 0);
2048 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
2049 result = create_conv(NULL, result, mode_arith);
2051 ir_mode *mode = mode_P_data;
2052 result = new_d_Proj(dbgi, resproj, mode, 0);
2056 if (function->kind == EXPR_REFERENCE &&
2057 function->reference.entity->declaration.modifiers & DM_NORETURN) {
2058 /* A dead end: Keep the Call and the Block. Also place all further
2059 * nodes into a new and unreachable block. */
2061 keep_alive(get_cur_block());
2062 ir_node *block = new_Block(0, NULL);
2063 set_cur_block(block);
2069 static void statement_to_firm(statement_t *statement);
2070 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
2072 static ir_node *expression_to_addr(const expression_t *expression);
2073 static ir_node *create_condition_evaluation(const expression_t *expression,
2074 ir_node *true_block,
2075 ir_node *false_block);
2077 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
2080 if (!is_type_compound(type)) {
2081 ir_mode *mode = get_ir_mode_storage(type);
2082 value = create_conv(dbgi, value, mode);
2083 value = do_strict_conv(dbgi, value);
2086 ir_node *memory = get_store();
2088 if (is_type_scalar(type)) {
2089 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2090 ? cons_volatile : cons_none;
2091 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2092 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2093 set_store(store_mem);
2095 ir_type *irtype = get_ir_type(type);
2096 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2097 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2098 set_store(copyb_mem);
2102 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2104 tarval *all_one = get_mode_all_one(mode);
2105 int mode_size = get_mode_size_bits(mode);
2107 assert(offset >= 0);
2109 assert(offset + size <= mode_size);
2110 if (size == mode_size) {
2114 long shiftr = get_mode_size_bits(mode) - size;
2115 long shiftl = offset;
2116 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2117 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2118 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2119 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2124 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2125 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2127 ir_type *entity_type = get_entity_type(entity);
2128 ir_type *base_type = get_primitive_base_type(entity_type);
2129 assert(base_type != NULL);
2130 ir_mode *mode = get_type_mode(base_type);
2132 value = create_conv(dbgi, value, mode);
2134 /* kill upper bits of value and shift to right position */
2135 int bitoffset = get_entity_offset_bits_remainder(entity);
2136 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2138 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2139 ir_node *mask_node = new_d_Const(dbgi, mask);
2140 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2141 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2142 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2143 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2145 /* load current value */
2146 ir_node *mem = get_store();
2147 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2148 set_volatile ? cons_volatile : cons_none);
2149 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2150 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2151 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2152 tarval *inv_mask = tarval_not(shift_mask);
2153 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2154 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2156 /* construct new value and store */
2157 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2158 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2159 set_volatile ? cons_volatile : cons_none);
2160 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2161 set_store(store_mem);
2163 return value_masked;
2166 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2169 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2170 type_t *type = expression->base.type;
2171 ir_mode *mode = get_ir_mode_storage(type);
2172 ir_node *mem = get_store();
2173 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2174 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2175 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2177 load_res = create_conv(dbgi, load_res, mode_int);
2179 set_store(load_mem);
2181 /* kill upper bits */
2182 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2183 ir_entity *entity = expression->compound_entry->compound_member.entity;
2184 int bitoffset = get_entity_offset_bits_remainder(entity);
2185 ir_type *entity_type = get_entity_type(entity);
2186 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2187 long shift_bitsl = machine_size - bitoffset - bitsize;
2188 assert(shift_bitsl >= 0);
2189 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2190 ir_node *countl = new_d_Const(dbgi, tvl);
2191 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2193 long shift_bitsr = bitoffset + shift_bitsl;
2194 assert(shift_bitsr <= (long) machine_size);
2195 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2196 ir_node *countr = new_d_Const(dbgi, tvr);
2198 if (mode_is_signed(mode)) {
2199 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2201 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2204 return create_conv(dbgi, shiftr, mode);
2207 /* make sure the selected compound type is constructed */
2208 static void construct_select_compound(const select_expression_t *expression)
2210 type_t *type = skip_typeref(expression->compound->base.type);
2211 if (is_type_pointer(type)) {
2212 type = type->pointer.points_to;
2214 (void) get_ir_type(type);
2217 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2218 ir_node *value, ir_node *addr)
2220 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2221 type_t *type = skip_typeref(expression->base.type);
2223 if (!is_type_compound(type)) {
2224 ir_mode *mode = get_ir_mode_storage(type);
2225 value = create_conv(dbgi, value, mode);
2226 value = do_strict_conv(dbgi, value);
2229 if (expression->kind == EXPR_REFERENCE) {
2230 const reference_expression_t *ref = &expression->reference;
2232 entity_t *entity = ref->entity;
2233 assert(is_declaration(entity));
2234 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2235 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2236 set_value(entity->variable.v.value_number, value);
2238 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2239 set_value(entity->parameter.v.value_number, value);
2245 addr = expression_to_addr(expression);
2246 assert(addr != NULL);
2248 if (expression->kind == EXPR_SELECT) {
2249 const select_expression_t *select = &expression->select;
2251 construct_select_compound(select);
2253 entity_t *entity = select->compound_entry;
2254 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2255 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2256 ir_entity *irentity = entity->compound_member.entity;
2258 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2259 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2265 assign_value(dbgi, addr, type, value);
2269 static void set_value_for_expression(const expression_t *expression,
2272 set_value_for_expression_addr(expression, value, NULL);
2275 static ir_node *get_value_from_lvalue(const expression_t *expression,
2278 if (expression->kind == EXPR_REFERENCE) {
2279 const reference_expression_t *ref = &expression->reference;
2281 entity_t *entity = ref->entity;
2282 assert(entity->kind == ENTITY_VARIABLE
2283 || entity->kind == ENTITY_PARAMETER);
2284 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2286 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2287 value_number = entity->variable.v.value_number;
2288 assert(addr == NULL);
2289 type_t *type = skip_typeref(expression->base.type);
2290 ir_mode *mode = get_ir_mode_storage(type);
2291 ir_node *res = get_value(value_number, mode);
2292 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2293 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2294 value_number = entity->parameter.v.value_number;
2295 assert(addr == NULL);
2296 type_t *type = skip_typeref(expression->base.type);
2297 ir_mode *mode = get_ir_mode_storage(type);
2298 ir_node *res = get_value(value_number, mode);
2299 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2303 assert(addr != NULL);
2304 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2307 if (expression->kind == EXPR_SELECT &&
2308 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2309 construct_select_compound(&expression->select);
2310 value = bitfield_extract_to_firm(&expression->select, addr);
2312 value = deref_address(dbgi, expression->base.type, addr);
2319 static ir_node *create_incdec(const unary_expression_t *expression)
2321 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2322 const expression_t *value_expr = expression->value;
2323 ir_node *addr = expression_to_addr(value_expr);
2324 ir_node *value = get_value_from_lvalue(value_expr, addr);
2326 type_t *type = skip_typeref(expression->base.type);
2327 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2330 if (is_type_pointer(type)) {
2331 pointer_type_t *pointer_type = &type->pointer;
2332 offset = get_type_size_node(pointer_type->points_to);
2334 assert(is_type_arithmetic(type));
2335 offset = new_Const(get_mode_one(mode));
2339 ir_node *store_value;
2340 switch(expression->base.kind) {
2341 case EXPR_UNARY_POSTFIX_INCREMENT:
2343 store_value = new_d_Add(dbgi, value, offset, mode);
2345 case EXPR_UNARY_POSTFIX_DECREMENT:
2347 store_value = new_d_Sub(dbgi, value, offset, mode);
2349 case EXPR_UNARY_PREFIX_INCREMENT:
2350 result = new_d_Add(dbgi, value, offset, mode);
2351 store_value = result;
2353 case EXPR_UNARY_PREFIX_DECREMENT:
2354 result = new_d_Sub(dbgi, value, offset, mode);
2355 store_value = result;
2358 panic("no incdec expr in create_incdec");
2361 set_value_for_expression_addr(value_expr, store_value, addr);
2366 static bool is_local_variable(expression_t *expression)
2368 if (expression->kind != EXPR_REFERENCE)
2370 reference_expression_t *ref_expr = &expression->reference;
2371 entity_t *entity = ref_expr->entity;
2372 if (entity->kind != ENTITY_VARIABLE)
2374 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2375 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2378 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2381 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2382 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2383 case EXPR_BINARY_NOTEQUAL:
2384 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2385 case EXPR_BINARY_ISLESS:
2386 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2387 case EXPR_BINARY_ISLESSEQUAL:
2388 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2389 case EXPR_BINARY_ISGREATER:
2390 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2391 case EXPR_BINARY_ISGREATEREQUAL:
2392 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2393 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2398 panic("trying to get pn_Cmp from non-comparison binexpr type");
2402 * Handle the assume optimizer hint: check if a Confirm
2403 * node can be created.
2405 * @param dbi debug info
2406 * @param expr the IL assume expression
2408 * we support here only some simple cases:
2413 static ir_node *handle_assume_compare(dbg_info *dbi,
2414 const binary_expression_t *expression)
2416 expression_t *op1 = expression->left;
2417 expression_t *op2 = expression->right;
2418 entity_t *var2, *var = NULL;
2419 ir_node *res = NULL;
2422 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2424 if (is_local_variable(op1) && is_local_variable(op2)) {
2425 var = op1->reference.entity;
2426 var2 = op2->reference.entity;
2428 type_t *const type = skip_typeref(var->declaration.type);
2429 ir_mode *const mode = get_ir_mode_storage(type);
2431 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2432 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2434 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2435 set_value(var2->variable.v.value_number, res);
2437 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2438 set_value(var->variable.v.value_number, res);
2444 if (is_local_variable(op1) && is_constant_expression(op2)) {
2445 var = op1->reference.entity;
2447 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2448 cmp_val = get_inversed_pnc(cmp_val);
2449 var = op2->reference.entity;
2454 type_t *const type = skip_typeref(var->declaration.type);
2455 ir_mode *const mode = get_ir_mode_storage(type);
2457 res = get_value(var->variable.v.value_number, mode);
2458 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2459 set_value(var->variable.v.value_number, res);
2465 * Handle the assume optimizer hint.
2467 * @param dbi debug info
2468 * @param expr the IL assume expression
2470 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2472 switch(expression->kind) {
2473 case EXPR_BINARY_EQUAL:
2474 case EXPR_BINARY_NOTEQUAL:
2475 case EXPR_BINARY_LESS:
2476 case EXPR_BINARY_LESSEQUAL:
2477 case EXPR_BINARY_GREATER:
2478 case EXPR_BINARY_GREATEREQUAL:
2479 return handle_assume_compare(dbi, &expression->binary);
2485 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2486 type_t *from_type, type_t *type)
2488 type = skip_typeref(type);
2489 if (!is_type_scalar(type)) {
2490 /* make sure firm type is constructed */
2491 (void) get_ir_type(type);
2495 from_type = skip_typeref(from_type);
2496 ir_mode *mode = get_ir_mode_storage(type);
2497 /* check for conversion from / to __based types */
2498 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2499 const variable_t *from_var = from_type->pointer.base_variable;
2500 const variable_t *to_var = type->pointer.base_variable;
2501 if (from_var != to_var) {
2502 if (from_var != NULL) {
2503 ir_node *const addr = get_global_var_address(dbgi, from_var);
2504 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2505 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2507 if (to_var != NULL) {
2508 ir_node *const addr = get_global_var_address(dbgi, to_var);
2509 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2510 value_node = new_d_Sub(dbgi, value_node, base, mode);
2515 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2516 /* bool adjustments (we save a mode_Bu, but have to temporarily
2517 * convert to mode_b so we only get a 0/1 value */
2518 value_node = create_conv(dbgi, value_node, mode_b);
2521 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2522 ir_node *node = create_conv(dbgi, value_node, mode);
2523 node = do_strict_conv(dbgi, node);
2524 node = create_conv(dbgi, node, mode_arith);
2529 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2531 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2532 type_t *type = skip_typeref(expression->base.type);
2534 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2535 return expression_to_addr(expression->value);
2537 const expression_t *value = expression->value;
2539 switch(expression->base.kind) {
2540 case EXPR_UNARY_NEGATE: {
2541 ir_node *value_node = expression_to_firm(value);
2542 ir_mode *mode = get_ir_mode_arithmetic(type);
2543 return new_d_Minus(dbgi, value_node, mode);
2545 case EXPR_UNARY_PLUS:
2546 return expression_to_firm(value);
2547 case EXPR_UNARY_BITWISE_NEGATE: {
2548 ir_node *value_node = expression_to_firm(value);
2549 ir_mode *mode = get_ir_mode_arithmetic(type);
2550 return new_d_Not(dbgi, value_node, mode);
2552 case EXPR_UNARY_NOT: {
2553 ir_node *value_node = _expression_to_firm(value);
2554 value_node = create_conv(dbgi, value_node, mode_b);
2555 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2558 case EXPR_UNARY_DEREFERENCE: {
2559 ir_node *value_node = expression_to_firm(value);
2560 type_t *value_type = skip_typeref(value->base.type);
2561 assert(is_type_pointer(value_type));
2563 /* check for __based */
2564 const variable_t *const base_var = value_type->pointer.base_variable;
2565 if (base_var != NULL) {
2566 ir_node *const addr = get_global_var_address(dbgi, base_var);
2567 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2568 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2570 type_t *points_to = value_type->pointer.points_to;
2571 return deref_address(dbgi, points_to, value_node);
2573 case EXPR_UNARY_POSTFIX_INCREMENT:
2574 case EXPR_UNARY_POSTFIX_DECREMENT:
2575 case EXPR_UNARY_PREFIX_INCREMENT:
2576 case EXPR_UNARY_PREFIX_DECREMENT:
2577 return create_incdec(expression);
2578 case EXPR_UNARY_CAST_IMPLICIT:
2579 case EXPR_UNARY_CAST: {
2580 ir_node *value_node = expression_to_firm(value);
2581 type_t *from_type = value->base.type;
2582 return create_cast(dbgi, value_node, from_type, type);
2584 case EXPR_UNARY_ASSUME:
2585 if (firm_opt.confirm)
2586 return handle_assume(dbgi, value);
2593 panic("invalid UNEXPR type found");
2597 * produces a 0/1 depending of the value of a mode_b node
2599 static ir_node *produce_condition_result(const expression_t *expression,
2600 ir_mode *mode, dbg_info *dbgi)
2602 ir_node *cur_block = get_cur_block();
2604 ir_node *one_block = new_immBlock();
2605 set_cur_block(one_block);
2606 ir_node *one = new_Const(get_mode_one(mode));
2607 ir_node *jmp_one = new_d_Jmp(dbgi);
2609 ir_node *zero_block = new_immBlock();
2610 set_cur_block(zero_block);
2611 ir_node *zero = new_Const(get_mode_null(mode));
2612 ir_node *jmp_zero = new_d_Jmp(dbgi);
2614 set_cur_block(cur_block);
2615 create_condition_evaluation(expression, one_block, zero_block);
2616 mature_immBlock(one_block);
2617 mature_immBlock(zero_block);
2619 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2620 ir_node *block = new_Block(2, in_cf);
2621 set_cur_block(block);
2623 ir_node *in[2] = { one, zero };
2624 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2629 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2630 ir_node *value, type_t *type)
2632 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2633 assert(is_type_pointer(type));
2634 pointer_type_t *const pointer_type = &type->pointer;
2635 type_t *const points_to = skip_typeref(pointer_type->points_to);
2636 ir_node * elem_size = get_type_size_node(points_to);
2637 elem_size = create_conv(dbgi, elem_size, mode);
2638 value = create_conv(dbgi, value, mode);
2639 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2643 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2644 ir_node *left, ir_node *right)
2647 type_t *type_left = skip_typeref(expression->left->base.type);
2648 type_t *type_right = skip_typeref(expression->right->base.type);
2650 expression_kind_t kind = expression->base.kind;
2653 case EXPR_BINARY_SHIFTLEFT:
2654 case EXPR_BINARY_SHIFTRIGHT:
2655 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2656 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2657 mode = get_irn_mode(left);
2658 right = create_conv(dbgi, right, mode_uint);
2661 case EXPR_BINARY_SUB:
2662 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2663 const pointer_type_t *const ptr_type = &type_left->pointer;
2665 mode = get_ir_mode_arithmetic(expression->base.type);
2666 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2667 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2668 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2669 ir_node *const no_mem = new_NoMem();
2670 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2671 mode, op_pin_state_floats);
2672 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2675 case EXPR_BINARY_SUB_ASSIGN:
2676 if (is_type_pointer(type_left)) {
2677 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2678 mode = get_ir_mode_arithmetic(type_left);
2683 case EXPR_BINARY_ADD:
2684 case EXPR_BINARY_ADD_ASSIGN:
2685 if (is_type_pointer(type_left)) {
2686 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2687 mode = get_ir_mode_arithmetic(type_left);
2689 } else if (is_type_pointer(type_right)) {
2690 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2691 mode = get_ir_mode_arithmetic(type_right);
2698 mode = get_ir_mode_arithmetic(type_right);
2699 left = create_conv(dbgi, left, mode);
2704 case EXPR_BINARY_ADD_ASSIGN:
2705 case EXPR_BINARY_ADD:
2706 return new_d_Add(dbgi, left, right, mode);
2707 case EXPR_BINARY_SUB_ASSIGN:
2708 case EXPR_BINARY_SUB:
2709 return new_d_Sub(dbgi, left, right, mode);
2710 case EXPR_BINARY_MUL_ASSIGN:
2711 case EXPR_BINARY_MUL:
2712 return new_d_Mul(dbgi, left, right, mode);
2713 case EXPR_BINARY_BITWISE_AND:
2714 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2715 return new_d_And(dbgi, left, right, mode);
2716 case EXPR_BINARY_BITWISE_OR:
2717 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2718 return new_d_Or(dbgi, left, right, mode);
2719 case EXPR_BINARY_BITWISE_XOR:
2720 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2721 return new_d_Eor(dbgi, left, right, mode);
2722 case EXPR_BINARY_SHIFTLEFT:
2723 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2724 return new_d_Shl(dbgi, left, right, mode);
2725 case EXPR_BINARY_SHIFTRIGHT:
2726 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2727 if (mode_is_signed(mode)) {
2728 return new_d_Shrs(dbgi, left, right, mode);
2730 return new_d_Shr(dbgi, left, right, mode);
2732 case EXPR_BINARY_DIV:
2733 case EXPR_BINARY_DIV_ASSIGN: {
2734 ir_node *pin = new_Pin(new_NoMem());
2737 if (mode_is_float(mode)) {
2738 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2739 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2741 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2742 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2746 case EXPR_BINARY_MOD:
2747 case EXPR_BINARY_MOD_ASSIGN: {
2748 ir_node *pin = new_Pin(new_NoMem());
2749 assert(!mode_is_float(mode));
2750 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2751 op_pin_state_floats);
2752 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2756 panic("unexpected expression kind");
2760 static ir_node *create_lazy_op(const binary_expression_t *expression)
2762 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2763 type_t *type = skip_typeref(expression->base.type);
2764 ir_mode *mode = get_ir_mode_arithmetic(type);
2766 if (is_constant_expression(expression->left)) {
2767 bool val = fold_constant_to_bool(expression->left);
2768 expression_kind_t ekind = expression->base.kind;
2769 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2770 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2772 return new_Const(get_mode_null(mode));
2776 return new_Const(get_mode_one(mode));
2780 if (is_constant_expression(expression->right)) {
2781 bool valr = fold_constant_to_bool(expression->right);
2783 new_Const(get_mode_one(mode)) :
2784 new_Const(get_mode_null(mode));
2787 return produce_condition_result(expression->right, mode, dbgi);
2790 return produce_condition_result((const expression_t*) expression, mode,
2794 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2795 ir_node *right, ir_mode *mode);
2797 static ir_node *create_assign_binop(const binary_expression_t *expression)
2799 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2800 const expression_t *left_expr = expression->left;
2801 type_t *type = skip_typeref(left_expr->base.type);
2802 ir_node *right = expression_to_firm(expression->right);
2803 ir_node *left_addr = expression_to_addr(left_expr);
2804 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2805 ir_node *result = create_op(dbgi, expression, left, right);
2807 result = create_cast(dbgi, result, expression->right->base.type, type);
2808 result = do_strict_conv(dbgi, result);
2810 result = set_value_for_expression_addr(left_expr, result, left_addr);
2812 if (!is_type_compound(type)) {
2813 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2814 result = create_conv(dbgi, result, mode_arithmetic);
2819 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2821 expression_kind_t kind = expression->base.kind;
2824 case EXPR_BINARY_EQUAL:
2825 case EXPR_BINARY_NOTEQUAL:
2826 case EXPR_BINARY_LESS:
2827 case EXPR_BINARY_LESSEQUAL:
2828 case EXPR_BINARY_GREATER:
2829 case EXPR_BINARY_GREATEREQUAL:
2830 case EXPR_BINARY_ISGREATER:
2831 case EXPR_BINARY_ISGREATEREQUAL:
2832 case EXPR_BINARY_ISLESS:
2833 case EXPR_BINARY_ISLESSEQUAL:
2834 case EXPR_BINARY_ISLESSGREATER:
2835 case EXPR_BINARY_ISUNORDERED: {
2836 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2837 ir_node *left = expression_to_firm(expression->left);
2838 ir_node *right = expression_to_firm(expression->right);
2839 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2840 long pnc = get_pnc(kind, expression->left->base.type);
2841 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2844 case EXPR_BINARY_ASSIGN: {
2845 ir_node *addr = expression_to_addr(expression->left);
2846 ir_node *right = expression_to_firm(expression->right);
2848 = set_value_for_expression_addr(expression->left, right, addr);
2850 type_t *type = skip_typeref(expression->base.type);
2851 if (!is_type_compound(type)) {
2852 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2853 res = create_conv(NULL, res, mode_arithmetic);
2857 case EXPR_BINARY_ADD:
2858 case EXPR_BINARY_SUB:
2859 case EXPR_BINARY_MUL:
2860 case EXPR_BINARY_DIV:
2861 case EXPR_BINARY_MOD:
2862 case EXPR_BINARY_BITWISE_AND:
2863 case EXPR_BINARY_BITWISE_OR:
2864 case EXPR_BINARY_BITWISE_XOR:
2865 case EXPR_BINARY_SHIFTLEFT:
2866 case EXPR_BINARY_SHIFTRIGHT:
2868 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2869 ir_node *left = expression_to_firm(expression->left);
2870 ir_node *right = expression_to_firm(expression->right);
2871 return create_op(dbgi, expression, left, right);
2873 case EXPR_BINARY_LOGICAL_AND:
2874 case EXPR_BINARY_LOGICAL_OR:
2875 return create_lazy_op(expression);
2876 case EXPR_BINARY_COMMA:
2877 /* create side effects of left side */
2878 (void) expression_to_firm(expression->left);
2879 return _expression_to_firm(expression->right);
2881 case EXPR_BINARY_ADD_ASSIGN:
2882 case EXPR_BINARY_SUB_ASSIGN:
2883 case EXPR_BINARY_MUL_ASSIGN:
2884 case EXPR_BINARY_MOD_ASSIGN:
2885 case EXPR_BINARY_DIV_ASSIGN:
2886 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2887 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2888 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2889 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2890 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2891 return create_assign_binop(expression);
2893 panic("TODO binexpr type");
2897 static ir_node *array_access_addr(const array_access_expression_t *expression)
2899 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2900 ir_node *base_addr = expression_to_firm(expression->array_ref);
2901 ir_node *offset = expression_to_firm(expression->index);
2902 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2903 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2904 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2909 static ir_node *array_access_to_firm(
2910 const array_access_expression_t *expression)
2912 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2913 ir_node *addr = array_access_addr(expression);
2914 type_t *type = revert_automatic_type_conversion(
2915 (const expression_t*) expression);
2916 type = skip_typeref(type);
2918 return deref_address(dbgi, type, addr);
2921 static long get_offsetof_offset(const offsetof_expression_t *expression)
2923 type_t *orig_type = expression->type;
2926 designator_t *designator = expression->designator;
2927 for ( ; designator != NULL; designator = designator->next) {
2928 type_t *type = skip_typeref(orig_type);
2929 /* be sure the type is constructed */
2930 (void) get_ir_type(type);
2932 if (designator->symbol != NULL) {
2933 assert(is_type_compound(type));
2934 symbol_t *symbol = designator->symbol;
2936 compound_t *compound = type->compound.compound;
2937 entity_t *iter = compound->members.entities;
2938 for ( ; iter != NULL; iter = iter->base.next) {
2939 if (iter->base.symbol == symbol) {
2943 assert(iter != NULL);
2945 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2946 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2947 offset += get_entity_offset(iter->compound_member.entity);
2949 orig_type = iter->declaration.type;
2951 expression_t *array_index = designator->array_index;
2952 assert(designator->array_index != NULL);
2953 assert(is_type_array(type));
2955 long index = fold_constant_to_int(array_index);
2956 ir_type *arr_type = get_ir_type(type);
2957 ir_type *elem_type = get_array_element_type(arr_type);
2958 long elem_size = get_type_size_bytes(elem_type);
2960 offset += index * elem_size;
2962 orig_type = type->array.element_type;
2969 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2971 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2972 long offset = get_offsetof_offset(expression);
2973 tarval *tv = new_tarval_from_long(offset, mode);
2974 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2976 return new_d_Const(dbgi, tv);
2979 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2980 ir_entity *entity, type_t *type);
2982 static ir_node *compound_literal_to_firm(
2983 const compound_literal_expression_t *expression)
2985 type_t *type = expression->type;
2987 /* create an entity on the stack */
2988 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2990 ident *const id = id_unique("CompLit.%u");
2991 ir_type *const irtype = get_ir_type(type);
2992 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2993 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2994 set_entity_ld_ident(entity, id);
2996 /* create initialisation code */
2997 initializer_t *initializer = expression->initializer;
2998 create_local_initializer(initializer, dbgi, entity, type);
3000 /* create a sel for the compound literal address */
3001 ir_node *frame = get_irg_frame(current_ir_graph);
3002 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3007 * Transform a sizeof expression into Firm code.
3009 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
3011 type_t *const type = skip_typeref(expression->type);
3012 /* §6.5.3.4:2 if the type is a VLA, evaluate the expression. */
3013 if (is_type_array(type) && type->array.is_vla
3014 && expression->tp_expression != NULL) {
3015 expression_to_firm(expression->tp_expression);
3018 return get_type_size_node(type);
3021 static entity_t *get_expression_entity(const expression_t *expression)
3023 if (expression->kind != EXPR_REFERENCE)
3026 return expression->reference.entity;
3029 static unsigned get_cparser_entity_alignment(const entity_t *entity)
3031 switch(entity->kind) {
3032 DECLARATION_KIND_CASES
3033 return entity->declaration.alignment;
3036 return entity->compound.alignment;
3037 case ENTITY_TYPEDEF:
3038 return entity->typedefe.alignment;
3046 * Transform an alignof expression into Firm code.
3048 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
3050 unsigned alignment = 0;
3052 const expression_t *tp_expression = expression->tp_expression;
3053 if (tp_expression != NULL) {
3054 entity_t *entity = get_expression_entity(tp_expression);
3055 if (entity != NULL) {
3056 alignment = get_cparser_entity_alignment(entity);
3060 if (alignment == 0) {
3061 type_t *type = expression->type;
3062 alignment = get_type_alignment(type);
3065 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3066 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3067 tarval *tv = new_tarval_from_long(alignment, mode);
3068 return new_d_Const(dbgi, tv);
3071 static void init_ir_types(void);
3073 static tarval *fold_constant_to_tarval(const expression_t *expression)
3075 assert(is_type_valid(skip_typeref(expression->base.type)));
3077 bool constant_folding_old = constant_folding;
3078 constant_folding = true;
3082 assert(is_constant_expression(expression));
3084 ir_graph *old_current_ir_graph = current_ir_graph;
3085 current_ir_graph = get_const_code_irg();
3087 ir_node *cnst = expression_to_firm(expression);
3088 current_ir_graph = old_current_ir_graph;
3090 if (!is_Const(cnst)) {
3091 panic("couldn't fold constant");
3094 constant_folding = constant_folding_old;
3096 tarval *tv = get_Const_tarval(cnst);
3100 long fold_constant_to_int(const expression_t *expression)
3102 if (expression->kind == EXPR_INVALID)
3105 tarval *tv = fold_constant_to_tarval(expression);
3106 if (!tarval_is_long(tv)) {
3107 panic("result of constant folding is not integer");
3110 return get_tarval_long(tv);
3113 bool fold_constant_to_bool(const expression_t *expression)
3115 if (expression->kind == EXPR_INVALID)
3117 tarval *tv = fold_constant_to_tarval(expression);
3118 return !tarval_is_null(tv);
3121 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3123 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3125 /* first try to fold a constant condition */
3126 if (is_constant_expression(expression->condition)) {
3127 bool val = fold_constant_to_bool(expression->condition);
3129 expression_t *true_expression = expression->true_expression;
3130 if (true_expression == NULL)
3131 true_expression = expression->condition;
3132 return expression_to_firm(true_expression);
3134 return expression_to_firm(expression->false_expression);
3138 ir_node *cur_block = get_cur_block();
3140 /* create the true block */
3141 ir_node *true_block = new_immBlock();
3142 set_cur_block(true_block);
3144 ir_node *true_val = expression->true_expression != NULL ?
3145 expression_to_firm(expression->true_expression) : NULL;
3146 ir_node *true_jmp = new_Jmp();
3148 /* create the false block */
3149 ir_node *false_block = new_immBlock();
3150 set_cur_block(false_block);
3152 ir_node *false_val = expression_to_firm(expression->false_expression);
3153 ir_node *false_jmp = new_Jmp();
3155 /* create the condition evaluation */
3156 set_cur_block(cur_block);
3157 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3158 if (expression->true_expression == NULL) {
3159 if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3160 true_val = cond_expr;
3162 /* Condition ended with a short circuit (&&, ||, !) operation or a
3163 * comparison. Generate a "1" as value for the true branch. */
3164 true_val = new_Const(get_mode_one(mode_Is));
3167 mature_immBlock(true_block);
3168 mature_immBlock(false_block);
3170 /* create the common block */
3171 ir_node *in_cf[2] = { true_jmp, false_jmp };
3172 ir_node *block = new_Block(2, in_cf);
3173 set_cur_block(block);
3175 /* TODO improve static semantics, so either both or no values are NULL */
3176 if (true_val == NULL || false_val == NULL)
3179 ir_node *in[2] = { true_val, false_val };
3180 ir_mode *mode = get_irn_mode(true_val);
3181 assert(get_irn_mode(false_val) == mode);
3182 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
3188 * Returns an IR-node representing the address of a field.
3190 static ir_node *select_addr(const select_expression_t *expression)
3192 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3194 construct_select_compound(expression);
3196 ir_node *compound_addr = expression_to_firm(expression->compound);
3198 entity_t *entry = expression->compound_entry;
3199 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3200 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3202 if (constant_folding) {
3203 ir_mode *mode = get_irn_mode(compound_addr);
3204 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3205 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3206 return new_d_Add(dbgi, compound_addr, ofs, mode);
3208 ir_entity *irentity = entry->compound_member.entity;
3209 assert(irentity != NULL);
3210 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3214 static ir_node *select_to_firm(const select_expression_t *expression)
3216 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3217 ir_node *addr = select_addr(expression);
3218 type_t *type = revert_automatic_type_conversion(
3219 (const expression_t*) expression);
3220 type = skip_typeref(type);
3222 entity_t *entry = expression->compound_entry;
3223 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3224 type_t *entry_type = skip_typeref(entry->declaration.type);
3226 if (entry_type->kind == TYPE_BITFIELD) {
3227 return bitfield_extract_to_firm(expression, addr);
3230 return deref_address(dbgi, type, addr);
3233 /* Values returned by __builtin_classify_type. */
3234 typedef enum gcc_type_class
3240 enumeral_type_class,
3243 reference_type_class,
3247 function_type_class,
3258 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3260 type_t *type = expr->type_expression->base.type;
3262 /* FIXME gcc returns different values depending on whether compiling C or C++
3263 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3266 type = skip_typeref(type);
3267 switch (type->kind) {
3269 const atomic_type_t *const atomic_type = &type->atomic;
3270 switch (atomic_type->akind) {
3271 /* should not be reached */
3272 case ATOMIC_TYPE_INVALID:
3276 /* gcc cannot do that */
3277 case ATOMIC_TYPE_VOID:
3278 tc = void_type_class;
3281 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3282 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3283 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3284 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3285 case ATOMIC_TYPE_SHORT:
3286 case ATOMIC_TYPE_USHORT:
3287 case ATOMIC_TYPE_INT:
3288 case ATOMIC_TYPE_UINT:
3289 case ATOMIC_TYPE_LONG:
3290 case ATOMIC_TYPE_ULONG:
3291 case ATOMIC_TYPE_LONGLONG:
3292 case ATOMIC_TYPE_ULONGLONG:
3293 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3294 tc = integer_type_class;
3297 case ATOMIC_TYPE_FLOAT:
3298 case ATOMIC_TYPE_DOUBLE:
3299 case ATOMIC_TYPE_LONG_DOUBLE:
3300 tc = real_type_class;
3303 panic("Unexpected atomic type in classify_type_to_firm().");
3306 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3307 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3308 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3309 case TYPE_ARRAY: /* gcc handles this as pointer */
3310 case TYPE_FUNCTION: /* gcc handles this as pointer */
3311 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3312 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3313 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3315 /* gcc handles this as integer */
3316 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3318 /* gcc classifies the referenced type */
3319 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3322 /* typedef/typeof should be skipped already */
3329 panic("unexpected TYPE classify_type_to_firm().");
3333 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3334 tarval *const tv = new_tarval_from_long(tc, mode_int);
3335 return new_d_Const(dbgi, tv);
3338 static ir_node *function_name_to_firm(
3339 const funcname_expression_t *const expr)
3341 switch(expr->kind) {
3342 case FUNCNAME_FUNCTION:
3343 case FUNCNAME_PRETTY_FUNCTION:
3344 case FUNCNAME_FUNCDNAME:
3345 if (current_function_name == NULL) {
3346 const source_position_t *const src_pos = &expr->base.source_position;
3347 const char *name = current_function_entity->base.symbol->string;
3348 const string_t string = { name, strlen(name) + 1 };
3349 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3351 return current_function_name;
3352 case FUNCNAME_FUNCSIG:
3353 if (current_funcsig == NULL) {
3354 const source_position_t *const src_pos = &expr->base.source_position;
3355 ir_entity *ent = get_irg_entity(current_ir_graph);
3356 const char *const name = get_entity_ld_name(ent);
3357 const string_t string = { name, strlen(name) + 1 };
3358 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3360 return current_funcsig;
3362 panic("Unsupported function name");
3365 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3367 statement_t *statement = expr->statement;
3369 assert(statement->kind == STATEMENT_COMPOUND);
3370 return compound_statement_to_firm(&statement->compound);
3373 static ir_node *va_start_expression_to_firm(
3374 const va_start_expression_t *const expr)
3376 type_t *const type = current_function_entity->declaration.type;
3377 ir_type *const method_type = get_ir_type(type);
3378 int const n = get_method_n_params(method_type) - 1;
3379 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3380 ir_node *const frame = get_irg_frame(current_ir_graph);
3381 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3382 ir_node *const no_mem = new_NoMem();
3383 ir_node *const arg_sel =
3384 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3386 type_t *const param_type = expr->parameter->base.type;
3387 ir_node *const cnst = get_type_size_node(param_type);
3388 ir_mode *const mode = get_irn_mode(cnst);
3389 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3390 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3391 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3392 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3393 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3394 set_value_for_expression(expr->ap, add);
3399 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3401 type_t *const type = expr->base.type;
3402 expression_t *const ap_expr = expr->ap;
3403 ir_node *const ap_addr = expression_to_addr(ap_expr);
3404 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3405 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3406 ir_node *const res = deref_address(dbgi, type, ap);
3408 ir_node *const cnst = get_type_size_node(expr->base.type);
3409 ir_mode *const mode = get_irn_mode(cnst);
3410 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3411 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3412 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3413 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3414 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3416 set_value_for_expression_addr(ap_expr, add, ap_addr);
3422 * Generate Firm for a va_copy expression.
3424 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3426 ir_node *const src = expression_to_firm(expr->src);
3427 set_value_for_expression(expr->dst, src);
3431 static ir_node *dereference_addr(const unary_expression_t *const expression)
3433 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3434 return expression_to_firm(expression->value);
3438 * Returns a IR-node representing an lvalue of the given expression.
3440 static ir_node *expression_to_addr(const expression_t *expression)
3442 switch(expression->kind) {
3443 case EXPR_ARRAY_ACCESS:
3444 return array_access_addr(&expression->array_access);
3446 return call_expression_to_firm(&expression->call);
3447 case EXPR_COMPOUND_LITERAL:
3448 return compound_literal_to_firm(&expression->compound_literal);
3449 case EXPR_REFERENCE:
3450 return reference_addr(&expression->reference);
3452 return select_addr(&expression->select);
3453 case EXPR_UNARY_DEREFERENCE:
3454 return dereference_addr(&expression->unary);
3458 panic("trying to get address of non-lvalue");
3461 static ir_node *builtin_constant_to_firm(
3462 const builtin_constant_expression_t *expression)
3464 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3467 if (is_constant_expression(expression->value)) {
3472 return new_Const_long(mode, v);
3475 static ir_node *builtin_types_compatible_to_firm(
3476 const builtin_types_compatible_expression_t *expression)
3478 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3479 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3480 long const value = types_compatible(left, right) ? 1 : 0;
3481 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3482 return new_Const_long(mode, value);
3485 static ir_node *get_label_block(label_t *label)
3487 if (label->block != NULL)
3488 return label->block;
3490 /* beware: might be called from create initializer with current_ir_graph
3491 * set to const_code_irg. */
3492 ir_graph *rem = current_ir_graph;
3493 current_ir_graph = current_function;
3495 ir_node *block = new_immBlock();
3497 label->block = block;
3499 ARR_APP1(label_t *, all_labels, label);
3501 current_ir_graph = rem;
3506 * Pointer to a label. This is used for the
3507 * GNU address-of-label extension.
3509 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3511 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3512 ir_node *block = get_label_block(label->label);
3513 ir_entity *entity = create_Block_entity(block);
3515 symconst_symbol value;
3516 value.entity_p = entity;
3517 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3521 * creates firm nodes for an expression. The difference between this function
3522 * and expression_to_firm is, that this version might produce mode_b nodes
3523 * instead of mode_Is.
3525 static ir_node *_expression_to_firm(const expression_t *expression)
3528 if (!constant_folding) {
3529 assert(!expression->base.transformed);
3530 ((expression_t*) expression)->base.transformed = true;
3534 switch (expression->kind) {
3536 return literal_to_firm(&expression->literal);
3537 case EXPR_STRING_LITERAL:
3538 return string_to_firm(&expression->base.source_position, "str.%u",
3539 &expression->literal.value);
3540 case EXPR_WIDE_STRING_LITERAL:
3541 return wide_string_literal_to_firm(&expression->string_literal);
3542 case EXPR_REFERENCE:
3543 return reference_expression_to_firm(&expression->reference);
3544 case EXPR_REFERENCE_ENUM_VALUE:
3545 return reference_expression_enum_value_to_firm(&expression->reference);
3547 return call_expression_to_firm(&expression->call);
3549 return unary_expression_to_firm(&expression->unary);
3551 return binary_expression_to_firm(&expression->binary);
3552 case EXPR_ARRAY_ACCESS:
3553 return array_access_to_firm(&expression->array_access);
3555 return sizeof_to_firm(&expression->typeprop);
3557 return alignof_to_firm(&expression->typeprop);
3558 case EXPR_CONDITIONAL:
3559 return conditional_to_firm(&expression->conditional);
3561 return select_to_firm(&expression->select);
3562 case EXPR_CLASSIFY_TYPE:
3563 return classify_type_to_firm(&expression->classify_type);
3565 return function_name_to_firm(&expression->funcname);
3566 case EXPR_STATEMENT:
3567 return statement_expression_to_firm(&expression->statement);
3569 return va_start_expression_to_firm(&expression->va_starte);
3571 return va_arg_expression_to_firm(&expression->va_arge);
3573 return va_copy_expression_to_firm(&expression->va_copye);
3574 case EXPR_BUILTIN_CONSTANT_P:
3575 return builtin_constant_to_firm(&expression->builtin_constant);
3576 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3577 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3579 return offsetof_to_firm(&expression->offsetofe);
3580 case EXPR_COMPOUND_LITERAL:
3581 return compound_literal_to_firm(&expression->compound_literal);
3582 case EXPR_LABEL_ADDRESS:
3583 return label_address_to_firm(&expression->label_address);
3589 panic("invalid expression found");
3593 * Check if a given expression is a GNU __builtin_expect() call.
3595 static bool is_builtin_expect(const expression_t *expression)
3597 if (expression->kind != EXPR_CALL)
3600 expression_t *function = expression->call.function;
3601 if (function->kind != EXPR_REFERENCE)
3603 reference_expression_t *ref = &function->reference;
3604 if (ref->entity->kind != ENTITY_FUNCTION ||
3605 ref->entity->function.btk != bk_gnu_builtin_expect)
3611 static bool produces_mode_b(const expression_t *expression)
3613 switch (expression->kind) {
3614 case EXPR_BINARY_EQUAL:
3615 case EXPR_BINARY_NOTEQUAL:
3616 case EXPR_BINARY_LESS:
3617 case EXPR_BINARY_LESSEQUAL:
3618 case EXPR_BINARY_GREATER:
3619 case EXPR_BINARY_GREATEREQUAL:
3620 case EXPR_BINARY_ISGREATER:
3621 case EXPR_BINARY_ISGREATEREQUAL:
3622 case EXPR_BINARY_ISLESS:
3623 case EXPR_BINARY_ISLESSEQUAL:
3624 case EXPR_BINARY_ISLESSGREATER:
3625 case EXPR_BINARY_ISUNORDERED:
3626 case EXPR_UNARY_NOT:
3630 if (is_builtin_expect(expression)) {
3631 expression_t *argument = expression->call.arguments->expression;
3632 return produces_mode_b(argument);
3635 case EXPR_BINARY_COMMA:
3636 return produces_mode_b(expression->binary.right);
3643 static ir_node *expression_to_firm(const expression_t *expression)
3645 if (!produces_mode_b(expression)) {
3646 ir_node *res = _expression_to_firm(expression);
3647 assert(res == NULL || get_irn_mode(res) != mode_b);
3651 if (is_constant_expression(expression)) {
3652 ir_node *res = _expression_to_firm(expression);
3653 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3654 assert(is_Const(res));
3655 if (is_Const_null(res)) {
3656 return new_Const_long(mode, 0);
3658 return new_Const_long(mode, 1);
3662 /* we have to produce a 0/1 from the mode_b expression */
3663 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3664 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3665 return produce_condition_result(expression, mode, dbgi);
3669 * create a short-circuit expression evaluation that tries to construct
3670 * efficient control flow structures for &&, || and ! expressions
3672 static ir_node *create_condition_evaluation(const expression_t *expression,
3673 ir_node *true_block,
3674 ir_node *false_block)
3676 switch(expression->kind) {
3677 case EXPR_UNARY_NOT: {
3678 const unary_expression_t *unary_expression = &expression->unary;
3679 create_condition_evaluation(unary_expression->value, false_block,
3683 case EXPR_BINARY_LOGICAL_AND: {
3684 const binary_expression_t *binary_expression = &expression->binary;
3686 ir_node *extra_block = new_immBlock();
3687 create_condition_evaluation(binary_expression->left, extra_block,
3689 mature_immBlock(extra_block);
3690 set_cur_block(extra_block);
3691 create_condition_evaluation(binary_expression->right, true_block,
3695 case EXPR_BINARY_LOGICAL_OR: {
3696 const binary_expression_t *binary_expression = &expression->binary;
3698 ir_node *extra_block = new_immBlock();
3699 create_condition_evaluation(binary_expression->left, true_block,
3701 mature_immBlock(extra_block);
3702 set_cur_block(extra_block);
3703 create_condition_evaluation(binary_expression->right, true_block,
3711 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3712 ir_node *cond_expr = _expression_to_firm(expression);
3713 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3714 ir_node *cond = new_d_Cond(dbgi, condition);
3715 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3716 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3718 /* set branch prediction info based on __builtin_expect */
3719 if (is_builtin_expect(expression) && is_Cond(cond)) {
3720 call_argument_t *argument = expression->call.arguments->next;
3721 if (is_constant_expression(argument->expression)) {
3722 bool cnst = fold_constant_to_bool(argument->expression);
3723 cond_jmp_predicate pred;
3725 if (cnst == false) {
3726 pred = COND_JMP_PRED_FALSE;
3728 pred = COND_JMP_PRED_TRUE;
3730 set_Cond_jmp_pred(cond, pred);
3734 add_immBlock_pred(true_block, true_proj);
3735 add_immBlock_pred(false_block, false_proj);
3737 set_cur_block(NULL);
3741 static void create_variable_entity(entity_t *variable,
3742 declaration_kind_t declaration_kind,
3743 ir_type *parent_type)
3745 assert(variable->kind == ENTITY_VARIABLE);
3746 type_t *type = skip_typeref(variable->declaration.type);
3748 ident *const id = new_id_from_str(variable->base.symbol->string);
3749 ir_type *const irtype = get_ir_type(type);
3750 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3751 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3752 unsigned alignment = variable->declaration.alignment;
3754 set_entity_alignment(irentity, alignment);
3756 handle_decl_modifiers(irentity, variable);
3758 variable->declaration.kind = (unsigned char) declaration_kind;
3759 variable->variable.v.entity = irentity;
3760 set_entity_ld_ident(irentity, create_ld_ident(variable));
3762 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3763 set_entity_volatility(irentity, volatility_is_volatile);
3768 typedef struct type_path_entry_t type_path_entry_t;
3769 struct type_path_entry_t {
3771 ir_initializer_t *initializer;
3773 entity_t *compound_entry;
3776 typedef struct type_path_t type_path_t;
3777 struct type_path_t {
3778 type_path_entry_t *path;
3783 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3785 size_t len = ARR_LEN(path->path);
3787 for (size_t i = 0; i < len; ++i) {
3788 const type_path_entry_t *entry = & path->path[i];
3790 type_t *type = skip_typeref(entry->type);
3791 if (is_type_compound(type)) {
3792 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3793 } else if (is_type_array(type)) {
3794 fprintf(stderr, "[%u]", (unsigned) entry->index);
3796 fprintf(stderr, "-INVALID-");
3799 fprintf(stderr, " (");
3800 print_type(path->top_type);
3801 fprintf(stderr, ")");
3804 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3806 size_t len = ARR_LEN(path->path);
3808 return & path->path[len-1];
3811 static type_path_entry_t *append_to_type_path(type_path_t *path)
3813 size_t len = ARR_LEN(path->path);
3814 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3816 type_path_entry_t *result = & path->path[len];
3817 memset(result, 0, sizeof(result[0]));
3821 static size_t get_compound_member_count(const compound_type_t *type)
3823 compound_t *compound = type->compound;
3824 size_t n_members = 0;
3825 entity_t *member = compound->members.entities;
3826 for ( ; member != NULL; member = member->base.next) {
3833 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3835 type_t *orig_top_type = path->top_type;
3836 type_t *top_type = skip_typeref(orig_top_type);
3838 assert(is_type_compound(top_type) || is_type_array(top_type));
3840 if (ARR_LEN(path->path) == 0) {
3843 type_path_entry_t *top = get_type_path_top(path);
3844 ir_initializer_t *initializer = top->initializer;
3845 return get_initializer_compound_value(initializer, top->index);
3849 static void descend_into_subtype(type_path_t *path)
3851 type_t *orig_top_type = path->top_type;
3852 type_t *top_type = skip_typeref(orig_top_type);
3854 assert(is_type_compound(top_type) || is_type_array(top_type));
3856 ir_initializer_t *initializer = get_initializer_entry(path);
3858 type_path_entry_t *top = append_to_type_path(path);
3859 top->type = top_type;
3863 if (is_type_compound(top_type)) {
3864 compound_t *compound = top_type->compound.compound;
3865 entity_t *entry = compound->members.entities;
3867 top->compound_entry = entry;
3869 len = get_compound_member_count(&top_type->compound);
3870 if (entry != NULL) {
3871 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3872 path->top_type = entry->declaration.type;
3875 assert(is_type_array(top_type));
3876 assert(top_type->array.size > 0);
3879 path->top_type = top_type->array.element_type;
3880 len = top_type->array.size;
3882 if (initializer == NULL
3883 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3884 initializer = create_initializer_compound(len);
3885 /* we have to set the entry at the 2nd latest path entry... */
3886 size_t path_len = ARR_LEN(path->path);
3887 assert(path_len >= 1);
3889 type_path_entry_t *entry = & path->path[path_len-2];
3890 ir_initializer_t *tinitializer = entry->initializer;
3891 set_initializer_compound_value(tinitializer, entry->index,
3895 top->initializer = initializer;
3898 static void ascend_from_subtype(type_path_t *path)
3900 type_path_entry_t *top = get_type_path_top(path);
3902 path->top_type = top->type;
3904 size_t len = ARR_LEN(path->path);
3905 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3908 static void walk_designator(type_path_t *path, const designator_t *designator)
3910 /* designators start at current object type */
3911 ARR_RESIZE(type_path_entry_t, path->path, 1);
3913 for ( ; designator != NULL; designator = designator->next) {
3914 type_path_entry_t *top = get_type_path_top(path);
3915 type_t *orig_type = top->type;
3916 type_t *type = skip_typeref(orig_type);
3918 if (designator->symbol != NULL) {
3919 assert(is_type_compound(type));
3921 symbol_t *symbol = designator->symbol;
3923 compound_t *compound = type->compound.compound;
3924 entity_t *iter = compound->members.entities;
3925 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3926 if (iter->base.symbol == symbol) {
3927 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3931 assert(iter != NULL);
3933 /* revert previous initialisations of other union elements */
3934 if (type->kind == TYPE_COMPOUND_UNION) {
3935 ir_initializer_t *initializer = top->initializer;
3936 if (initializer != NULL
3937 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3938 /* are we writing to a new element? */
3939 ir_initializer_t *oldi
3940 = get_initializer_compound_value(initializer, index);
3941 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3942 /* clear initializer */
3944 = get_initializer_compound_n_entries(initializer);
3945 ir_initializer_t *nulli = get_initializer_null();
3946 for (size_t i = 0; i < len; ++i) {
3947 set_initializer_compound_value(initializer, i,
3954 top->type = orig_type;
3955 top->compound_entry = iter;
3957 orig_type = iter->declaration.type;
3959 expression_t *array_index = designator->array_index;
3960 assert(designator->array_index != NULL);
3961 assert(is_type_array(type));
3963 long index = fold_constant_to_int(array_index);
3966 if (type->array.size_constant) {
3967 long array_size = type->array.size;
3968 assert(index < array_size);
3972 top->type = orig_type;
3973 top->index = (size_t) index;
3974 orig_type = type->array.element_type;
3976 path->top_type = orig_type;
3978 if (designator->next != NULL) {
3979 descend_into_subtype(path);
3983 path->invalid = false;
3986 static void advance_current_object(type_path_t *path)
3988 if (path->invalid) {
3989 /* TODO: handle this... */
3990 panic("invalid initializer in ast2firm (excessive elements)");
3993 type_path_entry_t *top = get_type_path_top(path);
3995 type_t *type = skip_typeref(top->type);
3996 if (is_type_union(type)) {
3997 /* only the first element is initialized in unions */
3998 top->compound_entry = NULL;
3999 } else if (is_type_struct(type)) {
4000 entity_t *entry = top->compound_entry;
4003 entry = entry->base.next;
4004 top->compound_entry = entry;
4005 if (entry != NULL) {
4006 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
4007 path->top_type = entry->declaration.type;
4011 assert(is_type_array(type));
4014 if (!type->array.size_constant || top->index < type->array.size) {
4019 /* we're past the last member of the current sub-aggregate, try if we
4020 * can ascend in the type hierarchy and continue with another subobject */
4021 size_t len = ARR_LEN(path->path);
4024 ascend_from_subtype(path);
4025 advance_current_object(path);
4027 path->invalid = true;
4032 static ir_initializer_t *create_ir_initializer(
4033 const initializer_t *initializer, type_t *type);
4035 static ir_initializer_t *create_ir_initializer_value(
4036 const initializer_value_t *initializer)
4038 if (is_type_compound(initializer->value->base.type)) {
4039 panic("initializer creation for compounds not implemented yet");
4041 type_t *type = initializer->value->base.type;
4042 expression_t *expr = initializer->value;
4043 if (initializer_use_bitfield_basetype) {
4044 type_t *skipped = skip_typeref(type);
4045 if (skipped->kind == TYPE_BITFIELD) {
4046 /* remove the bitfield cast... */
4047 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
4048 expr = expr->unary.value;
4049 type = skipped->bitfield.base_type;
4052 ir_node *value = expression_to_firm(expr);
4053 ir_mode *mode = get_ir_mode_storage(type);
4054 value = create_conv(NULL, value, mode);
4055 return create_initializer_const(value);
4058 /** test wether type can be initialized by a string constant */
4059 static bool is_string_type(type_t *type)
4062 if (is_type_pointer(type)) {
4063 inner = skip_typeref(type->pointer.points_to);
4064 } else if(is_type_array(type)) {
4065 inner = skip_typeref(type->array.element_type);
4070 return is_type_integer(inner);
4073 static ir_initializer_t *create_ir_initializer_list(
4074 const initializer_list_t *initializer, type_t *type)
4077 memset(&path, 0, sizeof(path));
4078 path.top_type = type;
4079 path.path = NEW_ARR_F(type_path_entry_t, 0);
4081 descend_into_subtype(&path);
4083 for (size_t i = 0; i < initializer->len; ++i) {
4084 const initializer_t *sub_initializer = initializer->initializers[i];
4086 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4087 walk_designator(&path, sub_initializer->designator.designator);
4091 if (sub_initializer->kind == INITIALIZER_VALUE) {
4092 /* we might have to descend into types until we're at a scalar
4095 type_t *orig_top_type = path.top_type;
4096 type_t *top_type = skip_typeref(orig_top_type);
4098 if (is_type_scalar(top_type))
4100 descend_into_subtype(&path);
4102 } else if (sub_initializer->kind == INITIALIZER_STRING
4103 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4104 /* we might have to descend into types until we're at a scalar
4107 type_t *orig_top_type = path.top_type;
4108 type_t *top_type = skip_typeref(orig_top_type);
4110 if (is_string_type(top_type))
4112 descend_into_subtype(&path);
4116 ir_initializer_t *sub_irinitializer
4117 = create_ir_initializer(sub_initializer, path.top_type);
4119 size_t path_len = ARR_LEN(path.path);
4120 assert(path_len >= 1);
4121 type_path_entry_t *entry = & path.path[path_len-1];
4122 ir_initializer_t *tinitializer = entry->initializer;
4123 set_initializer_compound_value(tinitializer, entry->index,
4126 advance_current_object(&path);
4129 assert(ARR_LEN(path.path) >= 1);
4130 ir_initializer_t *result = path.path[0].initializer;
4131 DEL_ARR_F(path.path);
4136 static ir_initializer_t *create_ir_initializer_string(
4137 const initializer_string_t *initializer, type_t *type)
4139 type = skip_typeref(type);
4141 size_t string_len = initializer->string.size;
4142 assert(type->kind == TYPE_ARRAY);
4143 assert(type->array.size_constant);
4144 size_t len = type->array.size;
4145 ir_initializer_t *irinitializer = create_initializer_compound(len);
4147 const char *string = initializer->string.begin;
4148 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4150 for (size_t i = 0; i < len; ++i) {
4155 tarval *tv = new_tarval_from_long(c, mode);
4156 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4158 set_initializer_compound_value(irinitializer, i, char_initializer);
4161 return irinitializer;
4164 static ir_initializer_t *create_ir_initializer_wide_string(
4165 const initializer_wide_string_t *initializer, type_t *type)
4167 assert(type->kind == TYPE_ARRAY);
4168 assert(type->array.size_constant);
4169 size_t len = type->array.size;
4170 size_t string_len = wstrlen(&initializer->string);
4171 ir_initializer_t *irinitializer = create_initializer_compound(len);
4173 const char *p = initializer->string.begin;
4174 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4176 for (size_t i = 0; i < len; ++i) {
4178 if (i < string_len) {
4179 c = read_utf8_char(&p);
4181 tarval *tv = new_tarval_from_long(c, mode);
4182 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4184 set_initializer_compound_value(irinitializer, i, char_initializer);
4187 return irinitializer;
4190 static ir_initializer_t *create_ir_initializer(
4191 const initializer_t *initializer, type_t *type)
4193 switch(initializer->kind) {
4194 case INITIALIZER_STRING:
4195 return create_ir_initializer_string(&initializer->string, type);
4197 case INITIALIZER_WIDE_STRING:
4198 return create_ir_initializer_wide_string(&initializer->wide_string,
4201 case INITIALIZER_LIST:
4202 return create_ir_initializer_list(&initializer->list, type);
4204 case INITIALIZER_VALUE:
4205 return create_ir_initializer_value(&initializer->value);
4207 case INITIALIZER_DESIGNATOR:
4208 panic("unexpected designator initializer found");
4210 panic("unknown initializer");
4213 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4214 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4216 switch(get_initializer_kind(initializer)) {
4217 case IR_INITIALIZER_NULL: {
4218 /* NULL is undefined for dynamic initializers */
4221 case IR_INITIALIZER_CONST: {
4222 ir_node *node = get_initializer_const_value(initializer);
4223 ir_type *ent_type = get_entity_type(entity);
4225 /* is it a bitfield type? */
4226 if (is_Primitive_type(ent_type) &&
4227 get_primitive_base_type(ent_type) != NULL) {
4228 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4232 assert(get_type_mode(type) == get_irn_mode(node));
4233 ir_node *mem = get_store();
4234 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4235 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4239 case IR_INITIALIZER_TARVAL: {
4240 tarval *tv = get_initializer_tarval_value(initializer);
4241 ir_node *cnst = new_d_Const(dbgi, tv);
4242 ir_type *ent_type = get_entity_type(entity);
4244 /* is it a bitfield type? */
4245 if (is_Primitive_type(ent_type) &&
4246 get_primitive_base_type(ent_type) != NULL) {
4247 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4251 assert(get_type_mode(type) == get_tarval_mode(tv));
4252 ir_node *mem = get_store();
4253 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4254 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4258 case IR_INITIALIZER_COMPOUND: {
4259 assert(is_compound_type(type));
4261 if (is_Array_type(type)) {
4262 assert(has_array_upper_bound(type, 0));
4263 n_members = get_array_upper_bound_int(type, 0);
4265 n_members = get_compound_n_members(type);
4268 if (get_initializer_compound_n_entries(initializer)
4269 != (unsigned) n_members)
4270 panic("initializer doesn't match compound type");
4272 for (int i = 0; i < n_members; ++i) {
4275 ir_entity *sub_entity;
4276 if (is_Array_type(type)) {
4277 tarval *index_tv = new_tarval_from_long(i, mode_uint);
4278 ir_node *cnst = new_d_Const(dbgi, index_tv);
4279 ir_node *in[1] = { cnst };
4280 irtype = get_array_element_type(type);
4281 sub_entity = get_array_element_entity(type);
4282 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4285 sub_entity = get_compound_member(type, i);
4286 irtype = get_entity_type(sub_entity);
4287 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4291 ir_initializer_t *sub_init
4292 = get_initializer_compound_value(initializer, i);
4294 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4301 panic("invalid IR_INITIALIZER found");
4304 static void create_dynamic_initializer(ir_initializer_t *initializer,
4305 dbg_info *dbgi, ir_entity *entity)
4307 ir_node *frame = get_irg_frame(current_ir_graph);
4308 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4309 ir_type *type = get_entity_type(entity);
4311 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4314 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4315 ir_entity *entity, type_t *type)
4317 ir_node *memory = get_store();
4318 ir_node *nomem = new_NoMem();
4319 ir_node *frame = get_irg_frame(current_ir_graph);
4320 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4322 if (initializer->kind == INITIALIZER_VALUE) {
4323 initializer_value_t *initializer_value = &initializer->value;
4325 ir_node *value = expression_to_firm(initializer_value->value);
4326 type = skip_typeref(type);
4327 assign_value(dbgi, addr, type, value);
4331 if (!is_constant_initializer(initializer)) {
4332 bool old_initializer_use_bitfield_basetype
4333 = initializer_use_bitfield_basetype;
4334 initializer_use_bitfield_basetype = true;
4335 ir_initializer_t *irinitializer
4336 = create_ir_initializer(initializer, type);
4337 initializer_use_bitfield_basetype
4338 = old_initializer_use_bitfield_basetype;
4340 create_dynamic_initializer(irinitializer, dbgi, entity);
4344 /* create the ir_initializer */
4345 ir_graph *const old_current_ir_graph = current_ir_graph;
4346 current_ir_graph = get_const_code_irg();
4348 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4350 assert(current_ir_graph == get_const_code_irg());
4351 current_ir_graph = old_current_ir_graph;
4353 /* create a "template" entity which is copied to the entity on the stack */
4354 ident *const id = id_unique("initializer.%u");
4355 ir_type *const irtype = get_ir_type(type);
4356 ir_type *const global_type = get_glob_type();
4357 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4358 set_entity_ld_ident(init_entity, id);
4360 set_entity_visibility(init_entity, ir_visibility_private);
4361 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4363 set_entity_initializer(init_entity, irinitializer);
4365 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4366 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4368 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4369 set_store(copyb_mem);
4372 static void create_initializer_local_variable_entity(entity_t *entity)
4374 assert(entity->kind == ENTITY_VARIABLE);
4375 initializer_t *initializer = entity->variable.initializer;
4376 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4377 ir_entity *irentity = entity->variable.v.entity;
4378 type_t *type = entity->declaration.type;
4380 create_local_initializer(initializer, dbgi, irentity, type);
4383 static void create_variable_initializer(entity_t *entity)
4385 assert(entity->kind == ENTITY_VARIABLE);
4386 initializer_t *initializer = entity->variable.initializer;
4387 if (initializer == NULL)
4390 declaration_kind_t declaration_kind
4391 = (declaration_kind_t) entity->declaration.kind;
4392 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4393 create_initializer_local_variable_entity(entity);
4397 type_t *type = entity->declaration.type;
4398 type_qualifiers_t tq = get_type_qualifier(type, true);
4400 if (initializer->kind == INITIALIZER_VALUE) {
4401 initializer_value_t *initializer_value = &initializer->value;
4402 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4404 ir_node *value = expression_to_firm(initializer_value->value);
4406 type_t *type = initializer_value->value->base.type;
4407 ir_mode *mode = get_ir_mode_storage(type);
4408 value = create_conv(dbgi, value, mode);
4409 value = do_strict_conv(dbgi, value);
4411 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4412 set_value(entity->variable.v.value_number, value);
4414 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4416 ir_entity *irentity = entity->variable.v.entity;
4418 if (tq & TYPE_QUALIFIER_CONST
4419 && get_entity_owner(irentity) != get_tls_type()) {
4420 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4422 set_atomic_ent_value(irentity, value);
4425 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4426 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4428 ir_entity *irentity = entity->variable.v.entity;
4429 ir_initializer_t *irinitializer
4430 = create_ir_initializer(initializer, type);
4432 if (tq & TYPE_QUALIFIER_CONST) {
4433 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4435 set_entity_initializer(irentity, irinitializer);
4439 static void create_variable_length_array(entity_t *entity)
4441 assert(entity->kind == ENTITY_VARIABLE);
4442 assert(entity->variable.initializer == NULL);
4444 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4445 entity->variable.v.vla_base = NULL;
4447 /* TODO: record VLA somewhere so we create the free node when we leave
4451 static void allocate_variable_length_array(entity_t *entity)
4453 assert(entity->kind == ENTITY_VARIABLE);
4454 assert(entity->variable.initializer == NULL);
4455 assert(get_cur_block() != NULL);
4457 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4458 type_t *type = entity->declaration.type;
4459 ir_type *el_type = get_ir_type(type->array.element_type);
4461 /* make sure size_node is calculated */
4462 get_type_size_node(type);
4463 ir_node *elems = type->array.size_node;
4464 ir_node *mem = get_store();
4465 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4467 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4468 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4471 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4472 entity->variable.v.vla_base = addr;
4476 * Creates a Firm local variable from a declaration.
4478 static void create_local_variable(entity_t *entity)
4480 assert(entity->kind == ENTITY_VARIABLE);
4481 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4483 bool needs_entity = entity->variable.address_taken;
4484 type_t *type = skip_typeref(entity->declaration.type);
4486 /* is it a variable length array? */
4487 if (is_type_array(type) && !type->array.size_constant) {
4488 create_variable_length_array(entity);
4490 } else if (is_type_array(type) || is_type_compound(type)) {
4491 needs_entity = true;
4492 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4493 needs_entity = true;
4497 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4498 create_variable_entity(entity,
4499 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4502 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4503 entity->variable.v.value_number = next_value_number_function;
4504 set_irg_loc_description(current_ir_graph, next_value_number_function,
4506 ++next_value_number_function;
4510 static void create_local_static_variable(entity_t *entity)
4512 assert(entity->kind == ENTITY_VARIABLE);
4513 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4515 type_t *type = skip_typeref(entity->declaration.type);
4516 ir_type *const var_type = entity->variable.thread_local ?
4517 get_tls_type() : get_glob_type();
4518 ir_type *const irtype = get_ir_type(type);
4519 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4521 size_t l = strlen(entity->base.symbol->string);
4522 char buf[l + sizeof(".%u")];
4523 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4524 ident *const id = id_unique(buf);
4525 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4527 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4528 set_entity_volatility(irentity, volatility_is_volatile);
4531 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4532 entity->variable.v.entity = irentity;
4534 set_entity_ld_ident(irentity, id);
4535 set_entity_visibility(irentity, ir_visibility_local);
4537 ir_graph *const old_current_ir_graph = current_ir_graph;
4538 current_ir_graph = get_const_code_irg();
4540 create_variable_initializer(entity);
4542 assert(current_ir_graph == get_const_code_irg());
4543 current_ir_graph = old_current_ir_graph;
4548 static void return_statement_to_firm(return_statement_t *statement)
4550 if (get_cur_block() == NULL)
4553 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4554 type_t *type = current_function_entity->declaration.type;
4555 ir_type *func_irtype = get_ir_type(type);
4560 if (get_method_n_ress(func_irtype) > 0) {
4561 ir_type *res_type = get_method_res_type(func_irtype, 0);
4563 if (statement->value != NULL) {
4564 ir_node *node = expression_to_firm(statement->value);
4565 if (!is_compound_type(res_type)) {
4566 type_t *type = statement->value->base.type;
4567 ir_mode *mode = get_ir_mode_storage(type);
4568 node = create_conv(dbgi, node, mode);
4569 node = do_strict_conv(dbgi, node);
4574 if (is_compound_type(res_type)) {
4577 mode = get_type_mode(res_type);
4579 in[0] = new_Unknown(mode);
4583 /* build return_value for its side effects */
4584 if (statement->value != NULL) {
4585 expression_to_firm(statement->value);
4590 ir_node *store = get_store();
4591 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4593 ir_node *end_block = get_irg_end_block(current_ir_graph);
4594 add_immBlock_pred(end_block, ret);
4596 set_cur_block(NULL);
4599 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4601 if (get_cur_block() == NULL)
4604 return expression_to_firm(statement->expression);
4607 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4609 entity_t *entity = compound->scope.entities;
4610 for ( ; entity != NULL; entity = entity->base.next) {
4611 if (!is_declaration(entity))
4614 create_local_declaration(entity);
4617 ir_node *result = NULL;
4618 statement_t *statement = compound->statements;
4619 for ( ; statement != NULL; statement = statement->base.next) {
4620 if (statement->base.next == NULL
4621 && statement->kind == STATEMENT_EXPRESSION) {
4622 result = expression_statement_to_firm(
4623 &statement->expression);
4626 statement_to_firm(statement);
4632 static void create_global_variable(entity_t *entity)
4634 ir_linkage linkage = 0;
4635 ir_visibility visibility = ir_visibility_default;
4636 ir_entity *irentity;
4637 assert(entity->kind == ENTITY_VARIABLE);
4639 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4640 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4641 case STORAGE_CLASS_STATIC: visibility = ir_visibility_local; break;
4642 case STORAGE_CLASS_NONE:
4643 visibility = ir_visibility_default;
4644 /* uninitialized globals get merged in C */
4645 if (entity->variable.initializer == NULL)
4646 linkage |= IR_LINKAGE_MERGE;
4648 case STORAGE_CLASS_TYPEDEF:
4649 case STORAGE_CLASS_AUTO:
4650 case STORAGE_CLASS_REGISTER:
4651 panic("invalid storage class for global var");
4654 ir_type *var_type = get_glob_type();
4655 if (entity->variable.thread_local) {
4656 var_type = get_tls_type();
4657 /* LINKAGE_MERGE not supported by current linkers */
4658 linkage &= ~IR_LINKAGE_MERGE;
4660 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4661 irentity = entity->variable.v.entity;
4662 add_entity_linkage(irentity, linkage);
4663 set_entity_visibility(irentity, visibility);
4666 static void create_local_declaration(entity_t *entity)
4668 assert(is_declaration(entity));
4670 /* construct type */
4671 (void) get_ir_type(entity->declaration.type);
4672 if (entity->base.symbol == NULL) {
4676 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4677 case STORAGE_CLASS_STATIC:
4678 if (entity->kind == ENTITY_FUNCTION) {
4679 (void)get_function_entity(entity, NULL);
4681 create_local_static_variable(entity);
4684 case STORAGE_CLASS_EXTERN:
4685 if (entity->kind == ENTITY_FUNCTION) {
4686 assert(entity->function.statement == NULL);
4687 (void)get_function_entity(entity, NULL);
4689 create_global_variable(entity);
4690 create_variable_initializer(entity);
4693 case STORAGE_CLASS_NONE:
4694 case STORAGE_CLASS_AUTO:
4695 case STORAGE_CLASS_REGISTER:
4696 if (entity->kind == ENTITY_FUNCTION) {
4697 if (entity->function.statement != NULL) {
4698 ir_type *owner = get_irg_frame_type(current_ir_graph);
4699 (void)get_function_entity(entity, owner);
4700 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4701 enqueue_inner_function(entity);
4703 (void)get_function_entity(entity, NULL);
4706 create_local_variable(entity);
4709 case STORAGE_CLASS_TYPEDEF:
4712 panic("invalid storage class found");
4715 static void initialize_local_declaration(entity_t *entity)
4717 if (entity->base.symbol == NULL)
4720 // no need to emit code in dead blocks
4721 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4722 && get_cur_block() == NULL)
4725 switch ((declaration_kind_t) entity->declaration.kind) {
4726 case DECLARATION_KIND_LOCAL_VARIABLE:
4727 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4728 create_variable_initializer(entity);
4731 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4732 allocate_variable_length_array(entity);
4735 case DECLARATION_KIND_COMPOUND_MEMBER:
4736 case DECLARATION_KIND_GLOBAL_VARIABLE:
4737 case DECLARATION_KIND_FUNCTION:
4738 case DECLARATION_KIND_INNER_FUNCTION:
4741 case DECLARATION_KIND_PARAMETER:
4742 case DECLARATION_KIND_PARAMETER_ENTITY:
4743 panic("can't initialize parameters");
4745 case DECLARATION_KIND_UNKNOWN:
4746 panic("can't initialize unknown declaration");
4748 panic("invalid declaration kind");
4751 static void declaration_statement_to_firm(declaration_statement_t *statement)
4753 entity_t *entity = statement->declarations_begin;
4757 entity_t *const last = statement->declarations_end;
4758 for ( ;; entity = entity->base.next) {
4759 if (is_declaration(entity)) {
4760 initialize_local_declaration(entity);
4761 } else if (entity->kind == ENTITY_TYPEDEF) {
4762 /* §6.7.7:3 Any array size expressions associated with variable length
4763 * array declarators are evaluated each time the declaration of the
4764 * typedef name is reached in the order of execution. */
4765 type_t *const type = skip_typeref(entity->typedefe.type);
4766 if (is_type_array(type) && type->array.is_vla)
4767 get_vla_size(&type->array);
4774 static void if_statement_to_firm(if_statement_t *statement)
4776 ir_node *cur_block = get_cur_block();
4778 ir_node *fallthrough_block = NULL;
4780 /* the true (blocks) */
4781 ir_node *true_block = NULL;
4782 if (statement->true_statement != NULL) {
4783 true_block = new_immBlock();
4784 set_cur_block(true_block);
4785 statement_to_firm(statement->true_statement);
4786 if (get_cur_block() != NULL) {
4787 ir_node *jmp = new_Jmp();
4788 if (fallthrough_block == NULL)
4789 fallthrough_block = new_immBlock();
4790 add_immBlock_pred(fallthrough_block, jmp);
4794 /* the false (blocks) */
4795 ir_node *false_block = NULL;
4796 if (statement->false_statement != NULL) {
4797 false_block = new_immBlock();
4798 set_cur_block(false_block);
4800 statement_to_firm(statement->false_statement);
4801 if (get_cur_block() != NULL) {
4802 ir_node *jmp = new_Jmp();
4803 if (fallthrough_block == NULL)
4804 fallthrough_block = new_immBlock();
4805 add_immBlock_pred(fallthrough_block, jmp);
4809 /* create the condition */
4810 if (cur_block != NULL) {
4811 if (true_block == NULL || false_block == NULL) {
4812 if (fallthrough_block == NULL)
4813 fallthrough_block = new_immBlock();
4814 if (true_block == NULL)
4815 true_block = fallthrough_block;
4816 if (false_block == NULL)
4817 false_block = fallthrough_block;
4820 set_cur_block(cur_block);
4821 create_condition_evaluation(statement->condition, true_block,
4825 mature_immBlock(true_block);
4826 if (false_block != fallthrough_block && false_block != NULL) {
4827 mature_immBlock(false_block);
4829 if (fallthrough_block != NULL) {
4830 mature_immBlock(fallthrough_block);
4833 set_cur_block(fallthrough_block);
4836 static void while_statement_to_firm(while_statement_t *statement)
4838 ir_node *jmp = NULL;
4839 if (get_cur_block() != NULL) {
4843 /* create the header block */
4844 ir_node *header_block = new_immBlock();
4846 add_immBlock_pred(header_block, jmp);
4850 ir_node *old_continue_label = continue_label;
4851 ir_node *old_break_label = break_label;
4852 continue_label = header_block;
4855 ir_node *body_block = new_immBlock();
4856 set_cur_block(body_block);
4857 statement_to_firm(statement->body);
4858 ir_node *false_block = break_label;
4860 assert(continue_label == header_block);
4861 continue_label = old_continue_label;
4862 break_label = old_break_label;
4864 if (get_cur_block() != NULL) {
4866 add_immBlock_pred(header_block, jmp);
4869 /* shortcut for while(true) */
4870 if (is_constant_expression(statement->condition)
4871 && fold_constant_to_bool(statement->condition) != 0) {
4872 set_cur_block(header_block);
4873 ir_node *header_jmp = new_Jmp();
4874 add_immBlock_pred(body_block, header_jmp);
4876 keep_alive(body_block);
4877 keep_all_memory(body_block);
4878 set_cur_block(body_block);
4880 if (false_block == NULL) {
4881 false_block = new_immBlock();
4884 /* create the condition */
4885 set_cur_block(header_block);
4887 create_condition_evaluation(statement->condition, body_block,
4891 mature_immBlock(body_block);
4892 mature_immBlock(header_block);
4893 if (false_block != NULL) {
4894 mature_immBlock(false_block);
4897 set_cur_block(false_block);
4900 static void do_while_statement_to_firm(do_while_statement_t *statement)
4902 ir_node *jmp = NULL;
4903 if (get_cur_block() != NULL) {
4907 /* create the header block */
4908 ir_node *header_block = new_immBlock();
4911 ir_node *body_block = new_immBlock();
4913 add_immBlock_pred(body_block, jmp);
4916 ir_node *old_continue_label = continue_label;
4917 ir_node *old_break_label = break_label;
4918 continue_label = header_block;
4921 set_cur_block(body_block);
4922 statement_to_firm(statement->body);
4923 ir_node *false_block = break_label;
4925 assert(continue_label == header_block);
4926 continue_label = old_continue_label;
4927 break_label = old_break_label;
4929 if (get_cur_block() != NULL) {
4930 ir_node *body_jmp = new_Jmp();
4931 add_immBlock_pred(header_block, body_jmp);
4932 mature_immBlock(header_block);
4935 if (false_block == NULL) {
4936 false_block = new_immBlock();
4939 /* create the condition */
4940 set_cur_block(header_block);
4942 create_condition_evaluation(statement->condition, body_block, false_block);
4943 mature_immBlock(body_block);
4944 mature_immBlock(header_block);
4945 mature_immBlock(false_block);
4947 set_cur_block(false_block);
4950 static void for_statement_to_firm(for_statement_t *statement)
4952 ir_node *jmp = NULL;
4954 /* create declarations */
4955 entity_t *entity = statement->scope.entities;
4956 for ( ; entity != NULL; entity = entity->base.next) {
4957 if (!is_declaration(entity))
4960 create_local_declaration(entity);
4963 if (get_cur_block() != NULL) {
4964 entity = statement->scope.entities;
4965 for ( ; entity != NULL; entity = entity->base.next) {
4966 if (!is_declaration(entity))
4969 initialize_local_declaration(entity);
4972 if (statement->initialisation != NULL) {
4973 expression_to_firm(statement->initialisation);
4980 /* create the step block */
4981 ir_node *const step_block = new_immBlock();
4982 set_cur_block(step_block);
4983 if (statement->step != NULL) {
4984 expression_to_firm(statement->step);
4986 ir_node *const step_jmp = new_Jmp();
4988 /* create the header block */
4989 ir_node *const header_block = new_immBlock();
4990 set_cur_block(header_block);
4992 add_immBlock_pred(header_block, jmp);
4994 add_immBlock_pred(header_block, step_jmp);
4996 /* the false block */
4997 ir_node *const false_block = new_immBlock();
5000 ir_node *body_block;
5001 if (statement->body != NULL) {
5002 ir_node *const old_continue_label = continue_label;
5003 ir_node *const old_break_label = break_label;
5004 continue_label = step_block;
5005 break_label = false_block;
5007 body_block = new_immBlock();
5008 set_cur_block(body_block);
5009 statement_to_firm(statement->body);
5011 assert(continue_label == step_block);
5012 assert(break_label == false_block);
5013 continue_label = old_continue_label;
5014 break_label = old_break_label;
5016 if (get_cur_block() != NULL) {
5018 add_immBlock_pred(step_block, jmp);
5021 body_block = step_block;
5024 /* create the condition */
5025 set_cur_block(header_block);
5026 if (statement->condition != NULL) {
5027 create_condition_evaluation(statement->condition, body_block,
5030 keep_alive(header_block);
5031 keep_all_memory(header_block);
5033 add_immBlock_pred(body_block, jmp);
5036 mature_immBlock(body_block);
5037 mature_immBlock(false_block);
5038 mature_immBlock(step_block);
5039 mature_immBlock(header_block);
5040 mature_immBlock(false_block);
5042 set_cur_block(false_block);
5045 static void create_jump_statement(const statement_t *statement,
5046 ir_node *target_block)
5048 if (get_cur_block() == NULL)
5051 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5052 ir_node *jump = new_d_Jmp(dbgi);
5053 add_immBlock_pred(target_block, jump);
5055 set_cur_block(NULL);
5058 static ir_node *get_break_label(void)
5060 if (break_label == NULL) {
5061 break_label = new_immBlock();
5066 static void switch_statement_to_firm(switch_statement_t *statement)
5068 ir_node *first_block = NULL;
5069 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5070 ir_node *cond = NULL;
5072 if (get_cur_block() != NULL) {
5073 ir_node *expression = expression_to_firm(statement->expression);
5074 cond = new_d_Cond(dbgi, expression);
5075 first_block = get_cur_block();
5078 set_cur_block(NULL);
5080 ir_node *const old_switch_cond = current_switch_cond;
5081 ir_node *const old_break_label = break_label;
5082 const bool old_saw_default_label = saw_default_label;
5083 saw_default_label = false;
5084 current_switch_cond = cond;
5086 switch_statement_t *const old_switch = current_switch;
5087 current_switch = statement;
5089 /* determine a free number for the default label */
5090 unsigned long num_cases = 0;
5092 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5093 if (l->expression == NULL) {
5097 if (l->last_case >= l->first_case)
5098 num_cases += l->last_case - l->first_case + 1;
5099 if (l->last_case > def_nr)
5100 def_nr = l->last_case;
5103 if (def_nr == INT_MAX) {
5104 /* Bad: an overflow will occur, we cannot be sure that the
5105 * maximum + 1 is a free number. Scan the values a second
5106 * time to find a free number.
5108 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5110 memset(bits, 0, (num_cases + 7) >> 3);
5111 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5112 if (l->expression == NULL) {
5116 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5117 if (start < num_cases && l->last_case >= 0) {
5118 unsigned long end = (unsigned long)l->last_case < num_cases ?
5119 (unsigned long)l->last_case : num_cases - 1;
5120 for (unsigned long cns = start; cns <= end; ++cns) {
5121 bits[cns >> 3] |= (1 << (cns & 7));
5125 /* We look at the first num_cases constants:
5126 * Either they are dense, so we took the last (num_cases)
5127 * one, or they are not dense, so we will find one free
5131 for (i = 0; i < num_cases; ++i)
5132 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5140 statement->default_proj_nr = def_nr;
5142 if (statement->body != NULL) {
5143 statement_to_firm(statement->body);
5146 if (get_cur_block() != NULL) {
5147 ir_node *jmp = new_Jmp();
5148 add_immBlock_pred(get_break_label(), jmp);
5151 if (!saw_default_label && first_block != NULL) {
5152 set_cur_block(first_block);
5153 ir_node *const proj = new_d_defaultProj(dbgi, cond,
5154 statement->default_proj_nr);
5155 add_immBlock_pred(get_break_label(), proj);
5158 if (break_label != NULL) {
5159 mature_immBlock(break_label);
5161 set_cur_block(break_label);
5163 assert(current_switch_cond == cond);
5164 current_switch = old_switch;
5165 current_switch_cond = old_switch_cond;
5166 break_label = old_break_label;
5167 saw_default_label = old_saw_default_label;
5170 static void case_label_to_firm(const case_label_statement_t *statement)
5172 if (statement->is_empty_range)
5175 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5177 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
5180 ir_node *block = new_immBlock();
5182 if (current_switch_cond != NULL) {
5183 set_cur_block(get_nodes_block(current_switch_cond));
5184 if (statement->expression != NULL) {
5185 long pn = statement->first_case;
5186 long end_pn = statement->last_case;
5187 assert(pn <= end_pn);
5188 /* create jumps for all cases in the given range */
5190 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5191 add_immBlock_pred(block, proj);
5192 } while (pn++ < end_pn);
5194 saw_default_label = true;
5195 proj = new_d_defaultProj(dbgi, current_switch_cond,
5196 current_switch->default_proj_nr);
5198 add_immBlock_pred(block, proj);
5202 if (fallthrough != NULL) {
5203 add_immBlock_pred(block, fallthrough);
5205 mature_immBlock(block);
5206 set_cur_block(block);
5208 if (statement->statement != NULL) {
5209 statement_to_firm(statement->statement);
5213 static void label_to_firm(const label_statement_t *statement)
5215 ir_node *block = get_label_block(statement->label);
5217 if (get_cur_block() != NULL) {
5218 ir_node *jmp = new_Jmp();
5219 add_immBlock_pred(block, jmp);
5222 set_cur_block(block);
5224 keep_all_memory(block);
5226 if (statement->statement != NULL) {
5227 statement_to_firm(statement->statement);
5231 static void goto_to_firm(const goto_statement_t *statement)
5233 if (get_cur_block() == NULL)
5236 if (statement->expression) {
5237 ir_node *irn = expression_to_firm(statement->expression);
5238 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5239 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5241 set_irn_link(ijmp, ijmp_list);
5244 ir_node *block = get_label_block(statement->label);
5245 ir_node *jmp = new_Jmp();
5246 add_immBlock_pred(block, jmp);
5248 set_cur_block(NULL);
5251 static void asm_statement_to_firm(const asm_statement_t *statement)
5253 bool needs_memory = false;
5255 if (statement->is_volatile) {
5256 needs_memory = true;
5259 size_t n_clobbers = 0;
5260 asm_clobber_t *clobber = statement->clobbers;
5261 for ( ; clobber != NULL; clobber = clobber->next) {
5262 const char *clobber_str = clobber->clobber.begin;
5264 if (!be_is_valid_clobber(clobber_str)) {
5265 errorf(&statement->base.source_position,
5266 "invalid clobber '%s' specified", clobber->clobber);
5270 if (strcmp(clobber_str, "memory") == 0) {
5271 needs_memory = true;
5275 ident *id = new_id_from_str(clobber_str);
5276 obstack_ptr_grow(&asm_obst, id);
5279 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5280 ident **clobbers = NULL;
5281 if (n_clobbers > 0) {
5282 clobbers = obstack_finish(&asm_obst);
5285 size_t n_inputs = 0;
5286 asm_argument_t *argument = statement->inputs;
5287 for ( ; argument != NULL; argument = argument->next)
5289 size_t n_outputs = 0;
5290 argument = statement->outputs;
5291 for ( ; argument != NULL; argument = argument->next)
5294 unsigned next_pos = 0;
5296 ir_node *ins[n_inputs + n_outputs + 1];
5299 ir_asm_constraint tmp_in_constraints[n_outputs];
5301 const expression_t *out_exprs[n_outputs];
5302 ir_node *out_addrs[n_outputs];
5303 size_t out_size = 0;
5305 argument = statement->outputs;
5306 for ( ; argument != NULL; argument = argument->next) {
5307 const char *constraints = argument->constraints.begin;
5308 asm_constraint_flags_t asm_flags
5309 = be_parse_asm_constraints(constraints);
5311 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5312 warningf(&statement->base.source_position,
5313 "some constraints in '%s' are not supported", constraints);
5315 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5316 errorf(&statement->base.source_position,
5317 "some constraints in '%s' are invalid", constraints);
5320 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5321 errorf(&statement->base.source_position,
5322 "no write flag specified for output constraints '%s'",
5327 unsigned pos = next_pos++;
5328 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5329 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5330 expression_t *expr = argument->expression;
5331 ir_node *addr = expression_to_addr(expr);
5332 /* in+output, construct an artifical same_as constraint on the
5334 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5336 ir_node *value = get_value_from_lvalue(expr, addr);
5338 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5340 ir_asm_constraint constraint;
5341 constraint.pos = pos;
5342 constraint.constraint = new_id_from_str(buf);
5343 constraint.mode = get_ir_mode_storage(expr->base.type);
5344 tmp_in_constraints[in_size] = constraint;
5345 ins[in_size] = value;
5350 out_exprs[out_size] = expr;
5351 out_addrs[out_size] = addr;
5353 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5354 /* pure memory ops need no input (but we have to make sure we
5355 * attach to the memory) */
5356 assert(! (asm_flags &
5357 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5358 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5359 needs_memory = true;
5361 /* we need to attach the address to the inputs */
5362 expression_t *expr = argument->expression;
5364 ir_asm_constraint constraint;
5365 constraint.pos = pos;
5366 constraint.constraint = new_id_from_str(constraints);
5367 constraint.mode = NULL;
5368 tmp_in_constraints[in_size] = constraint;
5370 ins[in_size] = expression_to_addr(expr);
5374 errorf(&statement->base.source_position,
5375 "only modifiers but no place set in constraints '%s'",
5380 ir_asm_constraint constraint;
5381 constraint.pos = pos;
5382 constraint.constraint = new_id_from_str(constraints);
5383 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5385 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5387 assert(obstack_object_size(&asm_obst)
5388 == out_size * sizeof(ir_asm_constraint));
5389 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5392 obstack_grow(&asm_obst, tmp_in_constraints,
5393 in_size * sizeof(tmp_in_constraints[0]));
5394 /* find and count input and output arguments */
5395 argument = statement->inputs;
5396 for ( ; argument != NULL; argument = argument->next) {
5397 const char *constraints = argument->constraints.begin;
5398 asm_constraint_flags_t asm_flags
5399 = be_parse_asm_constraints(constraints);
5401 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5402 errorf(&statement->base.source_position,
5403 "some constraints in '%s' are not supported", constraints);
5406 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5407 errorf(&statement->base.source_position,
5408 "some constraints in '%s' are invalid", constraints);
5411 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5412 errorf(&statement->base.source_position,
5413 "write flag specified for input constraints '%s'",
5419 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5420 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5421 /* we can treat this as "normal" input */
5422 input = expression_to_firm(argument->expression);
5423 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5424 /* pure memory ops need no input (but we have to make sure we
5425 * attach to the memory) */
5426 assert(! (asm_flags &
5427 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5428 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5429 needs_memory = true;
5430 input = expression_to_addr(argument->expression);
5432 errorf(&statement->base.source_position,
5433 "only modifiers but no place set in constraints '%s'",
5438 ir_asm_constraint constraint;
5439 constraint.pos = next_pos++;
5440 constraint.constraint = new_id_from_str(constraints);
5441 constraint.mode = get_irn_mode(input);
5443 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5444 ins[in_size++] = input;
5448 ir_asm_constraint constraint;
5449 constraint.pos = next_pos++;
5450 constraint.constraint = new_id_from_str("");
5451 constraint.mode = mode_M;
5453 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5454 ins[in_size++] = get_store();
5457 assert(obstack_object_size(&asm_obst)
5458 == in_size * sizeof(ir_asm_constraint));
5459 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5461 /* create asm node */
5462 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5464 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5466 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5467 out_size, output_constraints,
5468 n_clobbers, clobbers, asm_text);
5470 if (statement->is_volatile) {
5471 set_irn_pinned(node, op_pin_state_pinned);
5473 set_irn_pinned(node, op_pin_state_floats);
5476 /* create output projs & connect them */
5478 ir_node *projm = new_Proj(node, mode_M, out_size+1);
5483 for (i = 0; i < out_size; ++i) {
5484 const expression_t *out_expr = out_exprs[i];
5486 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5487 ir_node *proj = new_Proj(node, mode, pn);
5488 ir_node *addr = out_addrs[i];
5490 set_value_for_expression_addr(out_expr, proj, addr);
5494 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5496 statement_to_firm(statement->try_statement);
5497 warningf(&statement->base.source_position, "structured exception handling ignored");
5500 static void leave_statement_to_firm(leave_statement_t *statement)
5502 errorf(&statement->base.source_position, "__leave not supported yet");
5506 * Transform a statement.
5508 static void statement_to_firm(statement_t *statement)
5511 assert(!statement->base.transformed);
5512 statement->base.transformed = true;
5515 switch (statement->kind) {
5516 case STATEMENT_INVALID:
5517 panic("invalid statement found");
5518 case STATEMENT_EMPTY:
5521 case STATEMENT_COMPOUND:
5522 compound_statement_to_firm(&statement->compound);
5524 case STATEMENT_RETURN:
5525 return_statement_to_firm(&statement->returns);
5527 case STATEMENT_EXPRESSION:
5528 expression_statement_to_firm(&statement->expression);
5531 if_statement_to_firm(&statement->ifs);
5533 case STATEMENT_WHILE:
5534 while_statement_to_firm(&statement->whiles);
5536 case STATEMENT_DO_WHILE:
5537 do_while_statement_to_firm(&statement->do_while);
5539 case STATEMENT_DECLARATION:
5540 declaration_statement_to_firm(&statement->declaration);
5542 case STATEMENT_BREAK:
5543 create_jump_statement(statement, get_break_label());
5545 case STATEMENT_CONTINUE:
5546 create_jump_statement(statement, continue_label);
5548 case STATEMENT_SWITCH:
5549 switch_statement_to_firm(&statement->switchs);
5551 case STATEMENT_CASE_LABEL:
5552 case_label_to_firm(&statement->case_label);
5555 for_statement_to_firm(&statement->fors);
5557 case STATEMENT_LABEL:
5558 label_to_firm(&statement->label);
5560 case STATEMENT_GOTO:
5561 goto_to_firm(&statement->gotos);
5564 asm_statement_to_firm(&statement->asms);
5566 case STATEMENT_MS_TRY:
5567 ms_try_statement_to_firm(&statement->ms_try);
5569 case STATEMENT_LEAVE:
5570 leave_statement_to_firm(&statement->leave);
5573 panic("statement not implemented");
5576 static int count_local_variables(const entity_t *entity,
5577 const entity_t *const last)
5580 entity_t const *const end = last != NULL ? last->base.next : NULL;
5581 for (; entity != end; entity = entity->base.next) {
5585 if (entity->kind == ENTITY_VARIABLE) {
5586 type = skip_typeref(entity->declaration.type);
5587 address_taken = entity->variable.address_taken;
5588 } else if (entity->kind == ENTITY_PARAMETER) {
5589 type = skip_typeref(entity->declaration.type);
5590 address_taken = entity->parameter.address_taken;
5595 if (!address_taken && is_type_scalar(type))
5601 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5603 int *const count = env;
5605 switch (stmt->kind) {
5606 case STATEMENT_DECLARATION: {
5607 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5608 *count += count_local_variables(decl_stmt->declarations_begin,
5609 decl_stmt->declarations_end);
5614 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5623 * Return the number of local (alias free) variables used by a function.
5625 static int get_function_n_local_vars(entity_t *entity)
5627 const function_t *function = &entity->function;
5630 /* count parameters */
5631 count += count_local_variables(function->parameters.entities, NULL);
5633 /* count local variables declared in body */
5634 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5639 * Build Firm code for the parameters of a function.
5641 static void initialize_function_parameters(entity_t *entity)
5643 assert(entity->kind == ENTITY_FUNCTION);
5644 ir_graph *irg = current_ir_graph;
5645 ir_node *args = get_irg_args(irg);
5646 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5647 int first_param_nr = 0;
5649 if (entity->function.need_closure) {
5650 /* add an extra parameter for the static link */
5651 entity->function.static_link = new_r_Proj(args, mode_P_data, 0);
5656 entity_t *parameter = entity->function.parameters.entities;
5657 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5658 if (parameter->kind != ENTITY_PARAMETER)
5661 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5662 type_t *type = skip_typeref(parameter->declaration.type);
5664 bool needs_entity = parameter->parameter.address_taken;
5665 assert(!is_type_array(type));
5666 if (is_type_compound(type)) {
5667 needs_entity = true;
5671 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5672 ident *id = new_id_from_str(parameter->base.symbol->string);
5673 set_entity_ident(entity, id);
5675 parameter->declaration.kind
5676 = DECLARATION_KIND_PARAMETER_ENTITY;
5677 parameter->parameter.v.entity = entity;
5681 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5682 ir_mode *param_mode = get_type_mode(param_irtype);
5684 long pn = n + first_param_nr;
5685 ir_node *value = new_r_Proj(args, param_mode, pn);
5687 ir_mode *mode = get_ir_mode_storage(type);
5688 value = create_conv(NULL, value, mode);
5689 value = do_strict_conv(NULL, value);
5691 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5692 parameter->parameter.v.value_number = next_value_number_function;
5693 set_irg_loc_description(current_ir_graph, next_value_number_function,
5695 ++next_value_number_function;
5697 set_value(parameter->parameter.v.value_number, value);
5702 * Handle additional decl modifiers for IR-graphs
5704 * @param irg the IR-graph
5705 * @param dec_modifiers additional modifiers
5707 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5708 decl_modifiers_t decl_modifiers)
5710 if (decl_modifiers & DM_RETURNS_TWICE) {
5711 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5712 set_irg_additional_property(irg, mtp_property_returns_twice);
5714 if (decl_modifiers & DM_NORETURN) {
5715 /* TRUE if the declaration includes the Microsoft
5716 __declspec(noreturn) specifier. */
5717 set_irg_additional_property(irg, mtp_property_noreturn);
5719 if (decl_modifiers & DM_NOTHROW) {
5720 /* TRUE if the declaration includes the Microsoft
5721 __declspec(nothrow) specifier. */
5722 set_irg_additional_property(irg, mtp_property_nothrow);
5724 if (decl_modifiers & DM_NAKED) {
5725 /* TRUE if the declaration includes the Microsoft
5726 __declspec(naked) specifier. */
5727 set_irg_additional_property(irg, mtp_property_naked);
5729 if (decl_modifiers & DM_FORCEINLINE) {
5730 /* TRUE if the declaration includes the
5731 Microsoft __forceinline specifier. */
5732 set_irg_inline_property(irg, irg_inline_forced);
5734 if (decl_modifiers & DM_NOINLINE) {
5735 /* TRUE if the declaration includes the Microsoft
5736 __declspec(noinline) specifier. */
5737 set_irg_inline_property(irg, irg_inline_forbidden);
5741 static void add_function_pointer(ir_type *segment, ir_entity *method,
5742 const char *unique_template)
5744 ir_type *method_type = get_entity_type(method);
5745 ir_type *ptr_type = new_type_pointer(method_type);
5747 /* these entities don't really have a name but firm only allows
5749 * Note that we mustn't give these entities a name since for example
5750 * Mach-O doesn't allow them. */
5751 ident *ide = id_unique(unique_template);
5752 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5753 ir_graph *irg = get_const_code_irg();
5754 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5757 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5758 set_entity_compiler_generated(ptr, 1);
5759 set_entity_visibility(ptr, ir_visibility_local);
5760 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5761 set_atomic_ent_value(ptr, val);
5765 * Generate possible IJmp branches to a given label block.
5767 static void gen_ijmp_branches(ir_node *block)
5770 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5771 add_immBlock_pred(block, ijmp);
5776 * Create code for a function and all inner functions.
5778 * @param entity the function entity
5780 static void create_function(entity_t *entity)
5782 assert(entity->kind == ENTITY_FUNCTION);
5783 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5785 if (entity->function.statement == NULL)
5788 if (is_main(entity) && firm_opt.os_support == OS_SUPPORT_MINGW) {
5789 prepare_main_collect2(entity);
5792 inner_functions = NULL;
5793 current_trampolines = NULL;
5795 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5796 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5797 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5799 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5800 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5801 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5804 current_function_entity = entity;
5805 current_function_name = NULL;
5806 current_funcsig = NULL;
5808 assert(all_labels == NULL);
5809 all_labels = NEW_ARR_F(label_t *, 0);
5812 int n_local_vars = get_function_n_local_vars(entity);
5813 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5815 ir_graph *old_current_function = current_function;
5816 current_function = irg;
5818 set_irg_fp_model(irg, firm_opt.fp_model);
5819 tarval_enable_fp_ops(1);
5820 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5822 ir_node *first_block = get_cur_block();
5824 /* set inline flags */
5825 if (entity->function.is_inline)
5826 set_irg_inline_property(irg, irg_inline_recomended);
5827 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5829 next_value_number_function = 0;
5830 initialize_function_parameters(entity);
5831 current_static_link = entity->function.static_link;
5833 statement_to_firm(entity->function.statement);
5835 ir_node *end_block = get_irg_end_block(irg);
5837 /* do we have a return statement yet? */
5838 if (get_cur_block() != NULL) {
5839 type_t *type = skip_typeref(entity->declaration.type);
5840 assert(is_type_function(type));
5841 const function_type_t *func_type = &type->function;
5842 const type_t *return_type
5843 = skip_typeref(func_type->return_type);
5846 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5847 ret = new_Return(get_store(), 0, NULL);
5850 if (is_type_scalar(return_type)) {
5851 mode = get_ir_mode_storage(func_type->return_type);
5857 /* §5.1.2.2.3 main implicitly returns 0 */
5858 if (is_main(entity)) {
5859 in[0] = new_Const(get_mode_null(mode));
5861 in[0] = new_Unknown(mode);
5863 ret = new_Return(get_store(), 1, in);
5865 add_immBlock_pred(end_block, ret);
5868 bool has_computed_gotos = false;
5869 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5870 label_t *label = all_labels[i];
5871 if (label->address_taken) {
5872 gen_ijmp_branches(label->block);
5873 has_computed_gotos = true;
5875 mature_immBlock(label->block);
5877 if (has_computed_gotos) {
5878 /* if we have computed goto's in the function, we cannot inline it */
5879 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5880 warningf(&entity->base.source_position,
5881 "function '%Y' can never be inlined because it contains a computed goto",
5882 entity->base.symbol);
5884 set_irg_inline_property(irg, irg_inline_forbidden);
5887 DEL_ARR_F(all_labels);
5890 mature_immBlock(first_block);
5891 mature_immBlock(end_block);
5893 irg_finalize_cons(irg);
5895 /* finalize the frame type */
5896 ir_type *frame_type = get_irg_frame_type(irg);
5897 int n = get_compound_n_members(frame_type);
5900 for (int i = 0; i < n; ++i) {
5901 ir_entity *entity = get_compound_member(frame_type, i);
5902 ir_type *entity_type = get_entity_type(entity);
5904 int align = get_type_alignment_bytes(entity_type);
5905 if (align > align_all)
5909 misalign = offset % align;
5911 offset += align - misalign;
5915 set_entity_offset(entity, offset);
5916 offset += get_type_size_bytes(entity_type);
5918 set_type_size_bytes(frame_type, offset);
5919 set_type_alignment_bytes(frame_type, align_all);
5922 current_function = old_current_function;
5924 if (current_trampolines != NULL) {
5925 DEL_ARR_F(current_trampolines);
5926 current_trampolines = NULL;
5929 /* create inner functions if any */
5930 entity_t **inner = inner_functions;
5931 if (inner != NULL) {
5932 ir_type *rem_outer_frame = current_outer_frame;
5933 current_outer_frame = get_irg_frame_type(current_ir_graph);
5934 ir_type *rem_outer_value_type = current_outer_value_type;
5935 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5936 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5937 create_function(inner[i]);
5941 current_outer_value_type = rem_outer_value_type;
5942 current_outer_frame = rem_outer_frame;
5946 static void scope_to_firm(scope_t *scope)
5948 /* first pass: create declarations */
5949 entity_t *entity = scope->entities;
5950 for ( ; entity != NULL; entity = entity->base.next) {
5951 if (entity->base.symbol == NULL)
5954 if (entity->kind == ENTITY_FUNCTION) {
5955 if (entity->function.btk != bk_none) {
5956 /* builtins have no representation */
5959 (void)get_function_entity(entity, NULL);
5960 } else if (entity->kind == ENTITY_VARIABLE) {
5961 create_global_variable(entity);
5962 } else if (entity->kind == ENTITY_NAMESPACE) {
5963 scope_to_firm(&entity->namespacee.members);
5967 /* second pass: create code/initializers */
5968 entity = scope->entities;
5969 for ( ; entity != NULL; entity = entity->base.next) {
5970 if (entity->base.symbol == NULL)
5973 if (entity->kind == ENTITY_FUNCTION) {
5974 if (entity->function.btk != bk_none) {
5975 /* builtins have no representation */
5978 create_function(entity);
5979 } else if (entity->kind == ENTITY_VARIABLE) {
5980 assert(entity->declaration.kind
5981 == DECLARATION_KIND_GLOBAL_VARIABLE);
5982 current_ir_graph = get_const_code_irg();
5983 create_variable_initializer(entity);
5988 void init_ast2firm(void)
5990 obstack_init(&asm_obst);
5991 init_atomic_modes();
5993 ir_set_debug_retrieve(dbg_retrieve);
5994 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
5996 /* OS option must be set to the backend */
5997 switch (firm_opt.os_support) {
5998 case OS_SUPPORT_MINGW:
5999 create_ld_ident = create_name_win32;
6001 case OS_SUPPORT_LINUX:
6002 create_ld_ident = create_name_linux_elf;
6004 case OS_SUPPORT_MACHO:
6005 create_ld_ident = create_name_macho;
6008 panic("unexpected OS support mode");
6011 /* create idents for all known runtime functions */
6012 for (size_t i = 0; i < lengthof(rts_data); ++i) {
6013 rts_idents[i] = new_id_from_str(rts_data[i].name);
6016 entitymap_init(&entitymap);
6019 static void init_ir_types(void)
6021 static int ir_types_initialized = 0;
6022 if (ir_types_initialized)
6024 ir_types_initialized = 1;
6026 ir_type_int = get_ir_type(type_int);
6027 ir_type_char = get_ir_type(type_char);
6028 ir_type_const_char = get_ir_type(type_const_char);
6029 ir_type_wchar_t = get_ir_type(type_wchar_t);
6030 ir_type_void = get_ir_type(type_void);
6032 be_params = be_get_backend_param();
6033 mode_float_arithmetic = be_params->mode_float_arithmetic;
6035 stack_param_align = be_params->stack_param_align;
6038 void exit_ast2firm(void)
6040 entitymap_destroy(&entitymap);
6041 obstack_free(&asm_obst, NULL);
6044 static void global_asm_to_firm(statement_t *s)
6046 for (; s != NULL; s = s->base.next) {
6047 assert(s->kind == STATEMENT_ASM);
6049 char const *const text = s->asms.asm_text.begin;
6050 size_t size = s->asms.asm_text.size;
6052 /* skip the last \0 */
6053 if (text[size - 1] == '\0')
6056 ident *const id = new_id_from_chars(text, size);
6061 void translation_unit_to_firm(translation_unit_t *unit)
6063 /* initialize firm arithmetic */
6064 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
6066 /* just to be sure */
6067 continue_label = NULL;
6069 current_switch_cond = NULL;
6070 current_translation_unit = unit;
6074 scope_to_firm(&unit->scope);
6075 global_asm_to_firm(unit->global_asm);
6077 current_ir_graph = NULL;
6078 current_translation_unit = NULL;