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_tarval_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 declaration the function declaration
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 if (is_main(entity)) {
941 /* force main to C linkage */
942 type_t *type = entity->declaration.type;
943 assert(is_type_function(type));
944 if (type->function.linkage != LINKAGE_C) {
945 type_t *new_type = duplicate_type(type);
946 new_type->function.linkage = LINKAGE_C;
947 type = identify_new_type(new_type);
948 entity->declaration.type = type;
952 symbol_t *symbol = entity->base.symbol;
953 ident *id = new_id_from_str(symbol->string);
956 /* already an entity defined? */
957 ir_entity *irentity = entitymap_get(&entitymap, symbol);
958 bool const has_body = entity->function.statement != NULL;
959 if (irentity != NULL) {
960 if (get_entity_visibility(irentity) == ir_visibility_external
962 set_entity_visibility(irentity, ir_visibility_default);
967 ir_type *ir_type_method;
968 if (entity->function.need_closure)
969 ir_type_method = create_method_type(&entity->declaration.type->function, true);
971 ir_type_method = get_ir_type(entity->declaration.type);
973 bool nested_function = false;
974 if (owner_type == NULL)
975 owner_type = get_glob_type();
977 nested_function = true;
979 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
980 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
984 ld_id = id_unique("inner.%u");
986 ld_id = create_ld_ident(entity);
987 set_entity_ld_ident(irentity, ld_id);
989 handle_decl_modifiers(irentity, entity);
991 if (! nested_function) {
992 /* static inline => local
993 * extern inline => local
994 * inline without definition => local
995 * inline with definition => external_visible */
996 storage_class_tag_t const storage_class
997 = (storage_class_tag_t) entity->declaration.storage_class;
998 bool const is_inline = entity->function.is_inline;
1000 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1001 set_entity_visibility(irentity, ir_visibility_default);
1002 } else if (storage_class == STORAGE_CLASS_STATIC ||
1003 (is_inline && has_body)) {
1004 set_entity_visibility(irentity, ir_visibility_local);
1005 } else if (has_body) {
1006 set_entity_visibility(irentity, ir_visibility_default);
1008 set_entity_visibility(irentity, ir_visibility_external);
1011 /* nested functions are always local */
1012 set_entity_visibility(irentity, ir_visibility_local);
1015 /* We should check for file scope here, but as long as we compile C only
1016 this is not needed. */
1017 if (! firm_opt.freestanding && !has_body) {
1018 /* check for a known runtime function */
1019 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1020 if (id != rts_idents[i])
1023 /* ignore those rts functions not necessary needed for current mode */
1024 if ((c_mode & rts_data[i].flags) == 0)
1026 assert(rts_entities[rts_data[i].id] == NULL);
1027 rts_entities[rts_data[i].id] = irentity;
1031 entitymap_insert(&entitymap, symbol, irentity);
1034 entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1035 entity->function.irentity = irentity;
1040 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1042 ir_mode *value_mode = get_irn_mode(value);
1044 if (value_mode == dest_mode || is_Bad(value))
1047 if (dest_mode == mode_b) {
1048 ir_node *zero = new_Const(get_mode_null(value_mode));
1049 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1050 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1054 return new_d_Conv(dbgi, value, dest_mode);
1058 * Creates a Const node representing a constant.
1060 static ir_node *const_to_firm(const const_expression_t *cnst)
1062 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1063 type_t *type = skip_typeref(cnst->base.type);
1064 ir_mode *mode = get_ir_mode_storage(type);
1069 if (mode_is_float(mode)) {
1070 tv = new_tarval_from_double(cnst->v.float_value, mode);
1072 if (mode_is_signed(mode)) {
1073 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1075 len = snprintf(buf, sizeof(buf), "%llu",
1076 (unsigned long long) cnst->v.int_value);
1078 tv = new_tarval_from_str(buf, len, mode);
1081 ir_node *res = new_d_Const(dbgi, tv);
1082 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1083 return create_conv(dbgi, res, mode_arith);
1087 * Creates a Const node representing a character constant.
1089 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1091 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1092 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1095 size_t const size = cnst->v.character.size;
1096 if (size == 1 && char_is_signed) {
1097 v = (signed char)cnst->v.character.begin[0];
1100 for (size_t i = 0; i < size; ++i) {
1101 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1105 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1106 tarval *tv = new_tarval_from_str(buf, len, mode);
1108 return new_d_Const(dbgi, tv);
1112 * Creates a Const node representing a wide character constant.
1114 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1116 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1117 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1119 long long int v = cnst->v.wide_character.begin[0];
1122 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1123 tarval *tv = new_tarval_from_str(buf, len, mode);
1125 return new_d_Const(dbgi, tv);
1129 * Allocate an area of size bytes aligned at alignment
1132 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment)
1134 static unsigned area_cnt = 0;
1137 ir_type *tp = new_type_array(1, ir_type_char);
1138 set_array_bounds_int(tp, 0, 0, size);
1139 set_type_alignment_bytes(tp, alignment);
1141 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1142 ident *name = new_id_from_str(buf);
1143 ir_entity *area = new_entity(frame_type, name, tp);
1145 /* mark this entity as compiler generated */
1146 set_entity_compiler_generated(area, 1);
1151 * Return a node representing a trampoline region
1152 * for a given function entity.
1154 * @param dbgi debug info
1155 * @param entity the function entity
1157 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1159 ir_entity *region = NULL;
1162 if (current_trampolines != NULL) {
1163 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1164 if (current_trampolines[i].function == entity) {
1165 region = current_trampolines[i].region;
1170 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1172 ir_graph *irg = current_ir_graph;
1173 if (region == NULL) {
1174 /* create a new region */
1175 ir_type *frame_tp = get_irg_frame_type(irg);
1176 trampoline_region reg;
1177 reg.function = entity;
1179 reg.region = alloc_trampoline(frame_tp,
1180 be_params->trampoline_size,
1181 be_params->trampoline_align);
1182 ARR_APP1(trampoline_region, current_trampolines, reg);
1183 region = reg.region;
1185 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1191 * Creates a SymConst for a given entity.
1193 * @param dbgi debug info
1194 * @param mode the (reference) mode for the SymConst
1195 * @param entity the entity
1197 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1200 assert(entity != NULL);
1201 union symconst_symbol sym;
1202 sym.entity_p = entity;
1203 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1207 * Creates a trampoline for a function represented by an entity.
1209 * @param dbgi debug info
1210 * @param mode the (reference) mode for the function address
1211 * @param entity the function entity
1213 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1216 assert(entity != NULL);
1218 in[0] = get_trampoline_region(dbgi, entity);
1219 in[1] = create_symconst(dbgi, mode, entity);
1220 in[2] = get_irg_frame(current_ir_graph);
1222 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1223 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1224 return new_Proj(irn, mode, pn_Builtin_1_result);
1228 * Creates a SymConst node representing a string constant.
1230 * @param src_pos the source position of the string constant
1231 * @param id_prefix a prefix for the name of the generated string constant
1232 * @param value the value of the string constant
1234 static ir_node *string_to_firm(const source_position_t *const src_pos,
1235 const char *const id_prefix,
1236 const string_t *const value)
1238 ir_type *const global_type = get_glob_type();
1239 dbg_info *const dbgi = get_dbg_info(src_pos);
1240 ir_type *const type = new_type_array(1, ir_type_const_char);
1242 ident *const id = id_unique(id_prefix);
1243 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1244 set_entity_ld_ident(entity, id);
1245 set_entity_visibility(entity, ir_visibility_local);
1246 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1248 ir_type *const elem_type = ir_type_const_char;
1249 ir_mode *const mode = get_type_mode(elem_type);
1251 const char* const string = value->begin;
1252 const size_t slen = value->size;
1254 set_array_lower_bound_int(type, 0, 0);
1255 set_array_upper_bound_int(type, 0, slen);
1256 set_type_size_bytes(type, slen);
1257 set_type_state(type, layout_fixed);
1259 ir_initializer_t *initializer = create_initializer_compound(slen);
1260 for (size_t i = 0; i < slen; ++i) {
1261 tarval *tv = new_tarval_from_long(string[i], mode);
1262 ir_initializer_t *val = create_initializer_tarval(tv);
1263 set_initializer_compound_value(initializer, i, val);
1265 set_entity_initializer(entity, initializer);
1267 return create_symconst(dbgi, mode_P_data, entity);
1271 * Creates a SymConst node representing a string literal.
1273 * @param literal the string literal
1275 static ir_node *string_literal_to_firm(
1276 const string_literal_expression_t* literal)
1278 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1283 * Creates a SymConst node representing a wide string literal.
1285 * @param literal the wide string literal
1287 static ir_node *wide_string_literal_to_firm(
1288 const wide_string_literal_expression_t* const literal)
1290 ir_type *const global_type = get_glob_type();
1291 ir_type *const elem_type = ir_type_wchar_t;
1292 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1293 ir_type *const type = new_type_array(1, elem_type);
1295 ident *const id = id_unique("Lstr.%u");
1296 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1297 set_entity_ld_ident(entity, id);
1298 set_entity_visibility(entity, ir_visibility_local);
1299 add_entity_linkage(entity, IR_LINKAGE_CONSTANT);
1301 ir_mode *const mode = get_type_mode(elem_type);
1303 const wchar_rep_t *const string = literal->value.begin;
1304 const size_t slen = literal->value.size;
1306 set_array_lower_bound_int(type, 0, 0);
1307 set_array_upper_bound_int(type, 0, slen);
1308 set_type_size_bytes(type, slen * get_mode_size_bytes(mode));
1309 set_type_state(type, layout_fixed);
1311 ir_initializer_t *initializer = create_initializer_compound(slen);
1312 for (size_t i = 0; i < slen; ++i) {
1313 tarval *tv = new_tarval_from_long(string[i], mode);
1314 ir_initializer_t *val = create_initializer_tarval(tv);
1315 set_initializer_compound_value(initializer, i, val);
1317 set_entity_initializer(entity, initializer);
1319 return create_symconst(dbgi, mode_P_data, entity);
1323 * Dereference an address.
1325 * @param dbgi debug info
1326 * @param type the type of the dereferenced result (the points_to type)
1327 * @param addr the address to dereference
1329 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1330 ir_node *const addr)
1332 ir_type *irtype = get_ir_type(type);
1333 if (is_compound_type(irtype)
1334 || is_Method_type(irtype)
1335 || is_Array_type(irtype)) {
1339 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1340 ? cons_volatile : cons_none;
1341 ir_mode *const mode = get_type_mode(irtype);
1342 ir_node *const memory = get_store();
1343 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1344 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1345 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1347 set_store(load_mem);
1349 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1350 return create_conv(dbgi, load_res, mode_arithmetic);
1354 * Creates a strict Conv (to the node's mode) if necessary.
1356 * @param dbgi debug info
1357 * @param node the node to strict conv
1359 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1361 ir_mode *mode = get_irn_mode(node);
1363 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1365 if (!mode_is_float(mode))
1368 /* check if there is already a Conv */
1369 if (is_Conv(node)) {
1370 /* convert it into a strict Conv */
1371 set_Conv_strict(node, 1);
1375 /* otherwise create a new one */
1376 return new_d_strictConv(dbgi, node, mode);
1380 * Returns the address of a global variable.
1382 * @param dbgi debug info
1383 * @param variable the variable
1385 static ir_node *get_global_var_address(dbg_info *const dbgi,
1386 const variable_t *const variable)
1388 ir_entity *const irentity = variable->v.entity;
1389 if (variable->thread_local) {
1390 ir_node *const no_mem = new_NoMem();
1391 ir_node *const tls = get_irg_tls(current_ir_graph);
1392 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1394 return create_symconst(dbgi, mode_P_data, irentity);
1399 * Returns the correct base address depending on whether it is a parameter or a
1400 * normal local variable.
1402 static ir_node *get_local_frame(ir_entity *const ent)
1404 ir_graph *const irg = current_ir_graph;
1405 const ir_type *const owner = get_entity_owner(ent);
1406 if (owner == current_outer_frame || owner == current_outer_value_type) {
1407 assert(current_static_link != NULL);
1408 return current_static_link;
1410 return get_irg_frame(irg);
1415 * Keep all memory edges of the given block.
1417 static void keep_all_memory(ir_node *block)
1419 ir_node *old = get_cur_block();
1421 set_cur_block(block);
1422 keep_alive(get_store());
1423 /* TODO: keep all memory edges from restricted pointers */
1427 static ir_node *reference_expression_enum_value_to_firm(
1428 const reference_expression_t *ref)
1430 entity_t *entity = ref->entity;
1431 type_t *type = skip_typeref(entity->enum_value.enum_type);
1432 /* make sure the type is constructed */
1433 (void) get_ir_type(type);
1435 return new_Const(entity->enum_value.tv);
1438 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1440 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1441 entity_t *entity = ref->entity;
1442 assert(is_declaration(entity));
1443 type_t *type = skip_typeref(entity->declaration.type);
1445 /* make sure the type is constructed */
1446 (void) get_ir_type(type);
1448 /* for gcc compatibility we have to produce (dummy) addresses for some
1450 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1451 if (warning.other) {
1452 warningf(&ref->base.source_position,
1453 "taking address of builtin '%Y'", ref->entity->base.symbol);
1456 /* simply create a NULL pointer */
1457 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1458 ir_node *res = new_Const_long(mode, 0);
1463 switch ((declaration_kind_t) entity->declaration.kind) {
1464 case DECLARATION_KIND_UNKNOWN:
1467 case DECLARATION_KIND_LOCAL_VARIABLE: {
1468 ir_mode *const mode = get_ir_mode_storage(type);
1469 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1470 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1472 case DECLARATION_KIND_PARAMETER: {
1473 ir_mode *const mode = get_ir_mode_storage(type);
1474 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1475 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1477 case DECLARATION_KIND_FUNCTION: {
1478 ir_mode *const mode = get_ir_mode_storage(type);
1479 return create_symconst(dbgi, mode, entity->function.irentity);
1481 case DECLARATION_KIND_INNER_FUNCTION: {
1482 ir_mode *const mode = get_ir_mode_storage(type);
1483 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1484 /* inner function not using the closure */
1485 return create_symconst(dbgi, mode, entity->function.irentity);
1487 /* need trampoline here */
1488 return create_trampoline(dbgi, mode, entity->function.irentity);
1491 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1492 const variable_t *variable = &entity->variable;
1493 ir_node *const addr = get_global_var_address(dbgi, variable);
1494 return deref_address(dbgi, variable->base.type, addr);
1497 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1498 ir_entity *irentity = entity->variable.v.entity;
1499 ir_node *frame = get_local_frame(irentity);
1500 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1501 return deref_address(dbgi, entity->declaration.type, sel);
1503 case DECLARATION_KIND_PARAMETER_ENTITY: {
1504 ir_entity *irentity = entity->parameter.v.entity;
1505 ir_node *frame = get_local_frame(irentity);
1506 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1507 return deref_address(dbgi, entity->declaration.type, sel);
1510 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1511 return entity->variable.v.vla_base;
1513 case DECLARATION_KIND_COMPOUND_MEMBER:
1514 panic("not implemented reference type");
1517 panic("reference to declaration with unknown type found");
1520 static ir_node *reference_addr(const reference_expression_t *ref)
1522 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1523 entity_t *entity = ref->entity;
1524 assert(is_declaration(entity));
1526 switch((declaration_kind_t) entity->declaration.kind) {
1527 case DECLARATION_KIND_UNKNOWN:
1529 case DECLARATION_KIND_PARAMETER:
1530 case DECLARATION_KIND_LOCAL_VARIABLE:
1531 /* you can store to a local variable (so we don't panic but return NULL
1532 * as an indicator for no real address) */
1534 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1535 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1538 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1539 ir_entity *irentity = entity->variable.v.entity;
1540 ir_node *frame = get_local_frame(irentity);
1541 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1545 case DECLARATION_KIND_PARAMETER_ENTITY: {
1546 ir_entity *irentity = entity->parameter.v.entity;
1547 ir_node *frame = get_local_frame(irentity);
1548 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1553 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1554 return entity->variable.v.vla_base;
1556 case DECLARATION_KIND_FUNCTION: {
1557 type_t *const type = skip_typeref(entity->declaration.type);
1558 ir_mode *const mode = get_ir_mode_storage(type);
1559 return create_symconst(dbgi, mode, entity->function.irentity);
1562 case DECLARATION_KIND_INNER_FUNCTION: {
1563 type_t *const type = skip_typeref(entity->declaration.type);
1564 ir_mode *const mode = get_ir_mode_storage(type);
1565 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1566 /* inner function not using the closure */
1567 return create_symconst(dbgi, mode, entity->function.irentity);
1569 /* need trampoline here */
1570 return create_trampoline(dbgi, mode, entity->function.irentity);
1574 case DECLARATION_KIND_COMPOUND_MEMBER:
1575 panic("not implemented reference type");
1578 panic("reference to declaration with unknown type found");
1582 * Generate an unary builtin.
1584 * @param kind the builtin kind to generate
1585 * @param op the operand
1586 * @param function_type the function type for the GNU builtin routine
1587 * @param db debug info
1589 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1592 in[0] = expression_to_firm(op);
1594 ir_type *tp = get_ir_type(function_type);
1595 ir_type *res = get_method_res_type(tp, 0);
1596 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1597 set_irn_pinned(irn, op_pin_state_floats);
1598 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1602 * Generate a pinned unary builtin.
1604 * @param kind the builtin kind to generate
1605 * @param op the operand
1606 * @param function_type the function type for the GNU builtin routine
1607 * @param db debug info
1609 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1612 in[0] = expression_to_firm(op);
1614 ir_type *tp = get_ir_type(function_type);
1615 ir_type *res = get_method_res_type(tp, 0);
1616 ir_node *mem = get_store();
1617 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1618 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1619 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1624 * Generate an binary-void-return builtin.
1626 * @param kind the builtin kind to generate
1627 * @param op1 the first operand
1628 * @param op2 the second operand
1629 * @param function_type the function type for the GNU builtin routine
1630 * @param db debug info
1632 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1, expression_t *op2,
1633 type_t *function_type, dbg_info *db)
1636 in[0] = expression_to_firm(op1);
1637 in[1] = expression_to_firm(op2);
1639 ir_type *tp = get_ir_type(function_type);
1640 ir_node *mem = get_store();
1641 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1642 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1647 * Transform calls to builtin functions.
1649 static ir_node *process_builtin_call(const call_expression_t *call)
1651 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1653 assert(call->function->kind == EXPR_REFERENCE);
1654 reference_expression_t *builtin = &call->function->reference;
1656 type_t *type = skip_typeref(builtin->base.type);
1657 assert(is_type_pointer(type));
1659 type_t *function_type = skip_typeref(type->pointer.points_to);
1661 switch (builtin->entity->function.btk) {
1662 case bk_gnu_builtin_alloca: {
1663 if (call->arguments == NULL || call->arguments->next != NULL) {
1664 panic("invalid number of parameters on __builtin_alloca");
1666 expression_t *argument = call->arguments->expression;
1667 ir_node *size = expression_to_firm(argument);
1669 ir_node *store = get_store();
1670 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1672 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1674 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1679 case bk_gnu_builtin_huge_val:
1680 case bk_gnu_builtin_huge_valf:
1681 case bk_gnu_builtin_huge_vall:
1682 case bk_gnu_builtin_inf:
1683 case bk_gnu_builtin_inff:
1684 case bk_gnu_builtin_infl: {
1685 type_t *type = function_type->function.return_type;
1686 ir_mode *mode = get_ir_mode_arithmetic(type);
1687 tarval *tv = get_mode_infinite(mode);
1688 ir_node *res = new_d_Const(dbgi, tv);
1691 case bk_gnu_builtin_nan:
1692 case bk_gnu_builtin_nanf:
1693 case bk_gnu_builtin_nanl: {
1694 /* Ignore string for now... */
1695 assert(is_type_function(function_type));
1696 type_t *type = function_type->function.return_type;
1697 ir_mode *mode = get_ir_mode_arithmetic(type);
1698 tarval *tv = get_mode_NAN(mode);
1699 ir_node *res = new_d_Const(dbgi, tv);
1702 case bk_gnu_builtin_expect: {
1703 expression_t *argument = call->arguments->expression;
1704 return _expression_to_firm(argument);
1706 case bk_gnu_builtin_va_end:
1707 /* evaluate the argument of va_end for its side effects */
1708 _expression_to_firm(call->arguments->expression);
1710 case bk_gnu_builtin_frame_address: {
1711 expression_t *const expression = call->arguments->expression;
1712 bool val = fold_constant_to_bool(expression);
1715 return get_irg_frame(current_ir_graph);
1717 /* get the argument */
1720 in[0] = expression_to_firm(expression);
1721 in[1] = get_irg_frame(current_ir_graph);
1722 ir_type *tp = get_ir_type(function_type);
1723 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1724 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1727 case bk_gnu_builtin_return_address: {
1729 expression_t *const expression = call->arguments->expression;
1732 in[0] = expression_to_firm(expression);
1733 in[1] = get_irg_frame(current_ir_graph);
1734 ir_type *tp = get_ir_type(function_type);
1735 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1736 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1738 case bk_gnu_builtin_ffs:
1739 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1740 case bk_gnu_builtin_clz:
1741 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1742 case bk_gnu_builtin_ctz:
1743 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1744 case bk_gnu_builtin_popcount:
1745 case bk_ms__popcount:
1746 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1747 case bk_gnu_builtin_parity:
1748 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1749 case bk_gnu_builtin_prefetch: {
1750 call_argument_t *const args = call->arguments;
1751 expression_t *const addr = args->expression;
1754 in[0] = _expression_to_firm(addr);
1755 if (args->next != NULL) {
1756 expression_t *const rw = args->next->expression;
1758 in[1] = _expression_to_firm(rw);
1760 if (args->next->next != NULL) {
1761 expression_t *const locality = args->next->next->expression;
1763 in[2] = expression_to_firm(locality);
1765 in[2] = new_Const_long(mode_int, 3);
1768 in[1] = new_Const_long(mode_int, 0);
1769 in[2] = new_Const_long(mode_int, 3);
1771 ir_type *tp = get_ir_type(function_type);
1772 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1773 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1776 case bk_gnu_builtin_trap:
1779 ir_type *tp = get_ir_type(function_type);
1780 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1781 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1784 case bk_ms__debugbreak: {
1785 ir_type *tp = get_ir_type(function_type);
1786 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1787 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1790 case bk_ms_ReturnAddress: {
1793 in[0] = new_Const_long(mode_int, 0);
1794 in[1] = get_irg_frame(current_ir_graph);
1795 ir_type *tp = get_ir_type(function_type);
1796 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1797 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1800 case bk_ms_rotl64: {
1801 ir_node *val = expression_to_firm(call->arguments->expression);
1802 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1803 ir_mode *mode = get_irn_mode(val);
1804 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1807 case bk_ms_rotr64: {
1808 ir_node *val = expression_to_firm(call->arguments->expression);
1809 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1810 ir_mode *mode = get_irn_mode(val);
1811 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1812 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1813 return new_d_Rotl(dbgi, val, sub, mode);
1815 case bk_ms_byteswap_ushort:
1816 case bk_ms_byteswap_ulong:
1817 case bk_ms_byteswap_uint64:
1818 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1821 case bk_ms__indword:
1822 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1823 case bk_ms__outbyte:
1824 case bk_ms__outword:
1825 case bk_ms__outdword:
1826 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1827 call->arguments->next->expression, function_type, dbgi);
1829 panic("unsupported builtin found");
1834 * Transform a call expression.
1835 * Handles some special cases, like alloca() calls, which must be resolved
1836 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1837 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1840 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1842 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1843 assert(get_cur_block() != NULL);
1845 expression_t *function = call->function;
1846 if (function->kind == EXPR_REFERENCE) {
1847 const reference_expression_t *ref = &function->reference;
1848 entity_t *entity = ref->entity;
1850 if (entity->kind == ENTITY_FUNCTION) {
1851 if (entity->function.btk != bk_none) {
1852 return process_builtin_call(call);
1855 ir_entity *irentity = entity->function.irentity;
1856 if (irentity == NULL)
1857 irentity = get_function_entity(entity, NULL);
1859 if (irentity == rts_entities[rts_alloca]) {
1860 /* handle alloca() call */
1861 expression_t *argument = call->arguments->expression;
1862 ir_node *size = expression_to_firm(argument);
1863 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1865 size = create_conv(dbgi, size, mode);
1867 ir_node *store = get_store();
1868 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1869 firm_unknown_type, stack_alloc);
1870 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1872 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1878 ir_node *callee = expression_to_firm(function);
1880 type_t *type = skip_typeref(function->base.type);
1881 assert(is_type_pointer(type));
1882 pointer_type_t *pointer_type = &type->pointer;
1883 type_t *points_to = skip_typeref(pointer_type->points_to);
1884 assert(is_type_function(points_to));
1885 function_type_t *function_type = &points_to->function;
1887 int n_parameters = 0;
1888 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1889 ir_type *new_method_type = NULL;
1890 if (function_type->variadic || function_type->unspecified_parameters) {
1891 const call_argument_t *argument = call->arguments;
1892 for ( ; argument != NULL; argument = argument->next) {
1896 /* we need to construct a new method type matching the call
1898 type_dbg_info *tdbgi = get_type_dbg_info_((const type_t*) function_type);
1899 int n_res = get_method_n_ress(ir_method_type);
1900 new_method_type = new_d_type_method(n_parameters, n_res, tdbgi);
1901 set_method_calling_convention(new_method_type,
1902 get_method_calling_convention(ir_method_type));
1903 set_method_additional_properties(new_method_type,
1904 get_method_additional_properties(ir_method_type));
1905 set_method_variadicity(new_method_type,
1906 get_method_variadicity(ir_method_type));
1908 for (int i = 0; i < n_res; ++i) {
1909 set_method_res_type(new_method_type, i,
1910 get_method_res_type(ir_method_type, i));
1912 argument = call->arguments;
1913 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
1914 expression_t *expression = argument->expression;
1915 ir_type *irtype = get_ir_type(expression->base.type);
1916 set_method_param_type(new_method_type, i, irtype);
1918 ir_method_type = new_method_type;
1920 n_parameters = get_method_n_params(ir_method_type);
1923 ir_node *in[n_parameters];
1925 const call_argument_t *argument = call->arguments;
1926 for (int n = 0; n < n_parameters; ++n) {
1927 expression_t *expression = argument->expression;
1928 ir_node *arg_node = expression_to_firm(expression);
1930 type_t *type = skip_typeref(expression->base.type);
1931 if (!is_type_compound(type)) {
1932 ir_mode *mode = get_ir_mode_storage(expression->base.type);
1933 arg_node = create_conv(dbgi, arg_node, mode);
1934 arg_node = do_strict_conv(dbgi, arg_node);
1939 argument = argument->next;
1942 ir_node *store = get_store();
1943 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1945 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M);
1948 type_t *return_type = skip_typeref(function_type->return_type);
1949 ir_node *result = NULL;
1951 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1952 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1954 if (is_type_scalar(return_type)) {
1955 ir_mode *mode = get_ir_mode_storage(return_type);
1956 result = new_d_Proj(dbgi, resproj, mode, 0);
1957 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
1958 result = create_conv(NULL, result, mode_arith);
1960 ir_mode *mode = mode_P_data;
1961 result = new_d_Proj(dbgi, resproj, mode, 0);
1965 if (function->kind == EXPR_REFERENCE &&
1966 function->reference.entity->declaration.modifiers & DM_NORETURN) {
1967 /* A dead end: Keep the Call and the Block. Also place all further
1968 * nodes into a new and unreachable block. */
1970 keep_alive(get_cur_block());
1977 static void statement_to_firm(statement_t *statement);
1978 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1980 static ir_node *expression_to_addr(const expression_t *expression);
1981 static ir_node *create_condition_evaluation(const expression_t *expression,
1982 ir_node *true_block,
1983 ir_node *false_block);
1985 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1988 if (!is_type_compound(type)) {
1989 ir_mode *mode = get_ir_mode_storage(type);
1990 value = create_conv(dbgi, value, mode);
1991 value = do_strict_conv(dbgi, value);
1994 ir_node *memory = get_store();
1996 if (is_type_scalar(type)) {
1997 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1998 ? cons_volatile : cons_none;
1999 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2000 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2001 set_store(store_mem);
2003 ir_type *irtype = get_ir_type(type);
2004 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2005 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
2006 set_store(copyb_mem);
2010 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2012 tarval *all_one = get_mode_all_one(mode);
2013 int mode_size = get_mode_size_bits(mode);
2015 assert(offset >= 0);
2017 assert(offset + size <= mode_size);
2018 if (size == mode_size) {
2022 long shiftr = get_mode_size_bits(mode) - size;
2023 long shiftl = offset;
2024 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2025 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2026 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2027 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2032 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2033 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2035 ir_type *entity_type = get_entity_type(entity);
2036 ir_type *base_type = get_primitive_base_type(entity_type);
2037 assert(base_type != NULL);
2038 ir_mode *mode = get_type_mode(base_type);
2040 value = create_conv(dbgi, value, mode);
2042 /* kill upper bits of value and shift to right position */
2043 int bitoffset = get_entity_offset_bits_remainder(entity);
2044 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2046 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2047 ir_node *mask_node = new_d_Const(dbgi, mask);
2048 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2049 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2050 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2051 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2053 /* load current value */
2054 ir_node *mem = get_store();
2055 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2056 set_volatile ? cons_volatile : cons_none);
2057 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2058 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2059 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2060 tarval *inv_mask = tarval_not(shift_mask);
2061 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2062 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2064 /* construct new value and store */
2065 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2066 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2067 set_volatile ? cons_volatile : cons_none);
2068 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2069 set_store(store_mem);
2071 return value_masked;
2074 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2077 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2078 type_t *type = expression->base.type;
2079 ir_mode *mode = get_ir_mode_storage(type);
2080 ir_node *mem = get_store();
2081 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2082 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2083 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2085 load_res = create_conv(dbgi, load_res, mode_int);
2087 set_store(load_mem);
2089 /* kill upper bits */
2090 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2091 ir_entity *entity = expression->compound_entry->compound_member.entity;
2092 int bitoffset = get_entity_offset_bits_remainder(entity);
2093 ir_type *entity_type = get_entity_type(entity);
2094 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2095 long shift_bitsl = machine_size - bitoffset - bitsize;
2096 assert(shift_bitsl >= 0);
2097 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2098 ir_node *countl = new_d_Const(dbgi, tvl);
2099 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2101 long shift_bitsr = bitoffset + shift_bitsl;
2102 assert(shift_bitsr <= (long) machine_size);
2103 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2104 ir_node *countr = new_d_Const(dbgi, tvr);
2106 if (mode_is_signed(mode)) {
2107 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2109 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2112 return create_conv(dbgi, shiftr, mode);
2115 /* make sure the selected compound type is constructed */
2116 static void construct_select_compound(const select_expression_t *expression)
2118 type_t *type = skip_typeref(expression->compound->base.type);
2119 if (is_type_pointer(type)) {
2120 type = type->pointer.points_to;
2122 (void) get_ir_type(type);
2125 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2126 ir_node *value, ir_node *addr)
2128 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2129 type_t *type = skip_typeref(expression->base.type);
2131 if (!is_type_compound(type)) {
2132 ir_mode *mode = get_ir_mode_storage(type);
2133 value = create_conv(dbgi, value, mode);
2134 value = do_strict_conv(dbgi, value);
2137 if (expression->kind == EXPR_REFERENCE) {
2138 const reference_expression_t *ref = &expression->reference;
2140 entity_t *entity = ref->entity;
2141 assert(is_declaration(entity));
2142 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2143 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2144 set_value(entity->variable.v.value_number, value);
2146 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2147 set_value(entity->parameter.v.value_number, value);
2153 addr = expression_to_addr(expression);
2154 assert(addr != NULL);
2156 if (expression->kind == EXPR_SELECT) {
2157 const select_expression_t *select = &expression->select;
2159 construct_select_compound(select);
2161 entity_t *entity = select->compound_entry;
2162 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2163 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2164 ir_entity *irentity = entity->compound_member.entity;
2166 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2167 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2173 assign_value(dbgi, addr, type, value);
2177 static void set_value_for_expression(const expression_t *expression,
2180 set_value_for_expression_addr(expression, value, NULL);
2183 static ir_node *get_value_from_lvalue(const expression_t *expression,
2186 if (expression->kind == EXPR_REFERENCE) {
2187 const reference_expression_t *ref = &expression->reference;
2189 entity_t *entity = ref->entity;
2190 assert(entity->kind == ENTITY_VARIABLE
2191 || entity->kind == ENTITY_PARAMETER);
2192 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2194 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2195 value_number = entity->variable.v.value_number;
2196 assert(addr == NULL);
2197 type_t *type = skip_typeref(expression->base.type);
2198 ir_mode *mode = get_ir_mode_storage(type);
2199 ir_node *res = get_value(value_number, mode);
2200 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2201 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2202 value_number = entity->parameter.v.value_number;
2203 assert(addr == NULL);
2204 type_t *type = skip_typeref(expression->base.type);
2205 ir_mode *mode = get_ir_mode_storage(type);
2206 ir_node *res = get_value(value_number, mode);
2207 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2211 assert(addr != NULL);
2212 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2215 if (expression->kind == EXPR_SELECT &&
2216 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2217 construct_select_compound(&expression->select);
2218 value = bitfield_extract_to_firm(&expression->select, addr);
2220 value = deref_address(dbgi, expression->base.type, addr);
2227 static ir_node *create_incdec(const unary_expression_t *expression)
2229 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2230 const expression_t *value_expr = expression->value;
2231 ir_node *addr = expression_to_addr(value_expr);
2232 ir_node *value = get_value_from_lvalue(value_expr, addr);
2234 type_t *type = skip_typeref(expression->base.type);
2235 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2238 if (is_type_pointer(type)) {
2239 pointer_type_t *pointer_type = &type->pointer;
2240 offset = get_type_size_node(pointer_type->points_to);
2242 assert(is_type_arithmetic(type));
2243 offset = new_Const(get_mode_one(mode));
2247 ir_node *store_value;
2248 switch(expression->base.kind) {
2249 case EXPR_UNARY_POSTFIX_INCREMENT:
2251 store_value = new_d_Add(dbgi, value, offset, mode);
2253 case EXPR_UNARY_POSTFIX_DECREMENT:
2255 store_value = new_d_Sub(dbgi, value, offset, mode);
2257 case EXPR_UNARY_PREFIX_INCREMENT:
2258 result = new_d_Add(dbgi, value, offset, mode);
2259 store_value = result;
2261 case EXPR_UNARY_PREFIX_DECREMENT:
2262 result = new_d_Sub(dbgi, value, offset, mode);
2263 store_value = result;
2266 panic("no incdec expr in create_incdec");
2269 set_value_for_expression_addr(value_expr, store_value, addr);
2274 static bool is_local_variable(expression_t *expression)
2276 if (expression->kind != EXPR_REFERENCE)
2278 reference_expression_t *ref_expr = &expression->reference;
2279 entity_t *entity = ref_expr->entity;
2280 if (entity->kind != ENTITY_VARIABLE)
2282 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2283 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2286 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2289 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2290 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2291 case EXPR_BINARY_NOTEQUAL:
2292 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2293 case EXPR_BINARY_ISLESS:
2294 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2295 case EXPR_BINARY_ISLESSEQUAL:
2296 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2297 case EXPR_BINARY_ISGREATER:
2298 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2299 case EXPR_BINARY_ISGREATEREQUAL:
2300 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2301 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2306 panic("trying to get pn_Cmp from non-comparison binexpr type");
2310 * Handle the assume optimizer hint: check if a Confirm
2311 * node can be created.
2313 * @param dbi debug info
2314 * @param expr the IL assume expression
2316 * we support here only some simple cases:
2321 static ir_node *handle_assume_compare(dbg_info *dbi,
2322 const binary_expression_t *expression)
2324 expression_t *op1 = expression->left;
2325 expression_t *op2 = expression->right;
2326 entity_t *var2, *var = NULL;
2327 ir_node *res = NULL;
2330 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2332 if (is_local_variable(op1) && is_local_variable(op2)) {
2333 var = op1->reference.entity;
2334 var2 = op2->reference.entity;
2336 type_t *const type = skip_typeref(var->declaration.type);
2337 ir_mode *const mode = get_ir_mode_storage(type);
2339 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2340 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2342 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2343 set_value(var2->variable.v.value_number, res);
2345 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2346 set_value(var->variable.v.value_number, res);
2352 if (is_local_variable(op1) && is_constant_expression(op2)) {
2353 var = op1->reference.entity;
2355 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2356 cmp_val = get_inversed_pnc(cmp_val);
2357 var = op2->reference.entity;
2362 type_t *const type = skip_typeref(var->declaration.type);
2363 ir_mode *const mode = get_ir_mode_storage(type);
2365 res = get_value(var->variable.v.value_number, mode);
2366 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2367 set_value(var->variable.v.value_number, res);
2373 * Handle the assume optimizer hint.
2375 * @param dbi debug info
2376 * @param expr the IL assume expression
2378 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2380 switch(expression->kind) {
2381 case EXPR_BINARY_EQUAL:
2382 case EXPR_BINARY_NOTEQUAL:
2383 case EXPR_BINARY_LESS:
2384 case EXPR_BINARY_LESSEQUAL:
2385 case EXPR_BINARY_GREATER:
2386 case EXPR_BINARY_GREATEREQUAL:
2387 return handle_assume_compare(dbi, &expression->binary);
2393 static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node,
2394 type_t *from_type, type_t *type)
2396 type = skip_typeref(type);
2397 if (!is_type_scalar(type)) {
2398 /* make sure firm type is constructed */
2399 (void) get_ir_type(type);
2403 from_type = skip_typeref(from_type);
2404 ir_mode *mode = get_ir_mode_storage(type);
2405 /* check for conversion from / to __based types */
2406 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2407 const variable_t *from_var = from_type->pointer.base_variable;
2408 const variable_t *to_var = type->pointer.base_variable;
2409 if (from_var != to_var) {
2410 if (from_var != NULL) {
2411 ir_node *const addr = get_global_var_address(dbgi, from_var);
2412 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2413 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2415 if (to_var != NULL) {
2416 ir_node *const addr = get_global_var_address(dbgi, to_var);
2417 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2418 value_node = new_d_Sub(dbgi, value_node, base, mode);
2423 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2424 /* bool adjustments (we save a mode_Bu, but have to temporarily
2425 * convert to mode_b so we only get a 0/1 value */
2426 value_node = create_conv(dbgi, value_node, mode_b);
2429 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2430 ir_node *node = create_conv(dbgi, value_node, mode);
2431 node = do_strict_conv(dbgi, node);
2432 node = create_conv(dbgi, node, mode_arith);
2437 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2439 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2440 type_t *type = skip_typeref(expression->base.type);
2442 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2443 return expression_to_addr(expression->value);
2445 const expression_t *value = expression->value;
2447 switch(expression->base.kind) {
2448 case EXPR_UNARY_NEGATE: {
2449 ir_node *value_node = expression_to_firm(value);
2450 ir_mode *mode = get_ir_mode_arithmetic(type);
2451 return new_d_Minus(dbgi, value_node, mode);
2453 case EXPR_UNARY_PLUS:
2454 return expression_to_firm(value);
2455 case EXPR_UNARY_BITWISE_NEGATE: {
2456 ir_node *value_node = expression_to_firm(value);
2457 ir_mode *mode = get_ir_mode_arithmetic(type);
2458 return new_d_Not(dbgi, value_node, mode);
2460 case EXPR_UNARY_NOT: {
2461 ir_node *value_node = _expression_to_firm(value);
2462 value_node = create_conv(dbgi, value_node, mode_b);
2463 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2466 case EXPR_UNARY_DEREFERENCE: {
2467 ir_node *value_node = expression_to_firm(value);
2468 type_t *value_type = skip_typeref(value->base.type);
2469 assert(is_type_pointer(value_type));
2471 /* check for __based */
2472 const variable_t *const base_var = value_type->pointer.base_variable;
2473 if (base_var != NULL) {
2474 ir_node *const addr = get_global_var_address(dbgi, base_var);
2475 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2476 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2478 type_t *points_to = value_type->pointer.points_to;
2479 return deref_address(dbgi, points_to, value_node);
2481 case EXPR_UNARY_POSTFIX_INCREMENT:
2482 case EXPR_UNARY_POSTFIX_DECREMENT:
2483 case EXPR_UNARY_PREFIX_INCREMENT:
2484 case EXPR_UNARY_PREFIX_DECREMENT:
2485 return create_incdec(expression);
2486 case EXPR_UNARY_CAST_IMPLICIT:
2487 case EXPR_UNARY_CAST: {
2488 ir_node *value_node = expression_to_firm(value);
2489 type_t *from_type = value->base.type;
2490 return create_cast(dbgi, value_node, from_type, type);
2492 case EXPR_UNARY_ASSUME:
2493 if (firm_opt.confirm)
2494 return handle_assume(dbgi, value);
2501 panic("invalid UNEXPR type found");
2505 * produces a 0/1 depending of the value of a mode_b node
2507 static ir_node *produce_condition_result(const expression_t *expression,
2508 ir_mode *mode, dbg_info *dbgi)
2510 ir_node *cur_block = get_cur_block();
2512 ir_node *one_block = new_immBlock();
2513 set_cur_block(one_block);
2514 ir_node *one = new_Const(get_mode_one(mode));
2515 ir_node *jmp_one = new_d_Jmp(dbgi);
2517 ir_node *zero_block = new_immBlock();
2518 set_cur_block(zero_block);
2519 ir_node *zero = new_Const(get_mode_null(mode));
2520 ir_node *jmp_zero = new_d_Jmp(dbgi);
2522 set_cur_block(cur_block);
2523 create_condition_evaluation(expression, one_block, zero_block);
2524 mature_immBlock(one_block);
2525 mature_immBlock(zero_block);
2527 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2528 new_Block(2, in_cf);
2530 ir_node *in[2] = { one, zero };
2531 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2536 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2537 ir_node *value, type_t *type)
2539 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2540 assert(is_type_pointer(type));
2541 pointer_type_t *const pointer_type = &type->pointer;
2542 type_t *const points_to = skip_typeref(pointer_type->points_to);
2543 ir_node * elem_size = get_type_size_node(points_to);
2544 elem_size = create_conv(dbgi, elem_size, mode);
2545 value = create_conv(dbgi, value, mode);
2546 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2550 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2551 ir_node *left, ir_node *right)
2554 type_t *type_left = skip_typeref(expression->left->base.type);
2555 type_t *type_right = skip_typeref(expression->right->base.type);
2557 expression_kind_t kind = expression->base.kind;
2560 case EXPR_BINARY_SHIFTLEFT:
2561 case EXPR_BINARY_SHIFTRIGHT:
2562 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2563 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2564 mode = get_irn_mode(left);
2565 right = create_conv(dbgi, right, mode_uint);
2568 case EXPR_BINARY_SUB:
2569 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2570 const pointer_type_t *const ptr_type = &type_left->pointer;
2572 mode = get_ir_mode_arithmetic(expression->base.type);
2573 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2574 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2575 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2576 ir_node *const no_mem = new_NoMem();
2577 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2578 mode, op_pin_state_floats);
2579 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2582 case EXPR_BINARY_SUB_ASSIGN:
2583 if (is_type_pointer(type_left)) {
2584 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2585 mode = get_ir_mode_arithmetic(type_left);
2590 case EXPR_BINARY_ADD:
2591 case EXPR_BINARY_ADD_ASSIGN:
2592 if (is_type_pointer(type_left)) {
2593 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2594 mode = get_ir_mode_arithmetic(type_left);
2596 } else if (is_type_pointer(type_right)) {
2597 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2598 mode = get_ir_mode_arithmetic(type_right);
2605 mode = get_ir_mode_arithmetic(type_right);
2606 left = create_conv(dbgi, left, mode);
2611 case EXPR_BINARY_ADD_ASSIGN:
2612 case EXPR_BINARY_ADD:
2613 return new_d_Add(dbgi, left, right, mode);
2614 case EXPR_BINARY_SUB_ASSIGN:
2615 case EXPR_BINARY_SUB:
2616 return new_d_Sub(dbgi, left, right, mode);
2617 case EXPR_BINARY_MUL_ASSIGN:
2618 case EXPR_BINARY_MUL:
2619 return new_d_Mul(dbgi, left, right, mode);
2620 case EXPR_BINARY_BITWISE_AND:
2621 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2622 return new_d_And(dbgi, left, right, mode);
2623 case EXPR_BINARY_BITWISE_OR:
2624 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2625 return new_d_Or(dbgi, left, right, mode);
2626 case EXPR_BINARY_BITWISE_XOR:
2627 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2628 return new_d_Eor(dbgi, left, right, mode);
2629 case EXPR_BINARY_SHIFTLEFT:
2630 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2631 return new_d_Shl(dbgi, left, right, mode);
2632 case EXPR_BINARY_SHIFTRIGHT:
2633 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2634 if (mode_is_signed(mode)) {
2635 return new_d_Shrs(dbgi, left, right, mode);
2637 return new_d_Shr(dbgi, left, right, mode);
2639 case EXPR_BINARY_DIV:
2640 case EXPR_BINARY_DIV_ASSIGN: {
2641 ir_node *pin = new_Pin(new_NoMem());
2644 if (mode_is_float(mode)) {
2645 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2646 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2648 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2649 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2653 case EXPR_BINARY_MOD:
2654 case EXPR_BINARY_MOD_ASSIGN: {
2655 ir_node *pin = new_Pin(new_NoMem());
2656 assert(!mode_is_float(mode));
2657 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2658 op_pin_state_floats);
2659 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2663 panic("unexpected expression kind");
2667 static ir_node *create_lazy_op(const binary_expression_t *expression)
2669 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2670 type_t *type = skip_typeref(expression->base.type);
2671 ir_mode *mode = get_ir_mode_arithmetic(type);
2673 if (is_constant_expression(expression->left)) {
2674 bool val = fold_constant_to_bool(expression->left);
2675 expression_kind_t ekind = expression->base.kind;
2676 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2677 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2679 return new_Const(get_mode_null(mode));
2683 return new_Const(get_mode_one(mode));
2687 if (is_constant_expression(expression->right)) {
2688 bool valr = fold_constant_to_bool(expression->right);
2690 new_Const(get_mode_one(mode)) :
2691 new_Const(get_mode_null(mode));
2694 return produce_condition_result(expression->right, mode, dbgi);
2697 return produce_condition_result((const expression_t*) expression, mode,
2701 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2702 ir_node *right, ir_mode *mode);
2704 static ir_node *create_assign_binop(const binary_expression_t *expression)
2706 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2707 const expression_t *left_expr = expression->left;
2708 type_t *type = skip_typeref(left_expr->base.type);
2709 ir_node *right = expression_to_firm(expression->right);
2710 ir_node *left_addr = expression_to_addr(left_expr);
2711 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2712 ir_node *result = create_op(dbgi, expression, left, right);
2714 result = create_cast(dbgi, result, expression->right->base.type, type);
2715 result = do_strict_conv(dbgi, result);
2717 result = set_value_for_expression_addr(left_expr, result, left_addr);
2719 if (!is_type_compound(type)) {
2720 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2721 result = create_conv(dbgi, result, mode_arithmetic);
2726 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2728 expression_kind_t kind = expression->base.kind;
2731 case EXPR_BINARY_EQUAL:
2732 case EXPR_BINARY_NOTEQUAL:
2733 case EXPR_BINARY_LESS:
2734 case EXPR_BINARY_LESSEQUAL:
2735 case EXPR_BINARY_GREATER:
2736 case EXPR_BINARY_GREATEREQUAL:
2737 case EXPR_BINARY_ISGREATER:
2738 case EXPR_BINARY_ISGREATEREQUAL:
2739 case EXPR_BINARY_ISLESS:
2740 case EXPR_BINARY_ISLESSEQUAL:
2741 case EXPR_BINARY_ISLESSGREATER:
2742 case EXPR_BINARY_ISUNORDERED: {
2743 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2744 ir_node *left = expression_to_firm(expression->left);
2745 ir_node *right = expression_to_firm(expression->right);
2746 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2747 long pnc = get_pnc(kind, expression->left->base.type);
2748 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2751 case EXPR_BINARY_ASSIGN: {
2752 ir_node *addr = expression_to_addr(expression->left);
2753 ir_node *right = expression_to_firm(expression->right);
2755 = set_value_for_expression_addr(expression->left, right, addr);
2757 type_t *type = skip_typeref(expression->base.type);
2758 if (!is_type_compound(type)) {
2759 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2760 res = create_conv(NULL, res, mode_arithmetic);
2764 case EXPR_BINARY_ADD:
2765 case EXPR_BINARY_SUB:
2766 case EXPR_BINARY_MUL:
2767 case EXPR_BINARY_DIV:
2768 case EXPR_BINARY_MOD:
2769 case EXPR_BINARY_BITWISE_AND:
2770 case EXPR_BINARY_BITWISE_OR:
2771 case EXPR_BINARY_BITWISE_XOR:
2772 case EXPR_BINARY_SHIFTLEFT:
2773 case EXPR_BINARY_SHIFTRIGHT:
2775 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2776 ir_node *left = expression_to_firm(expression->left);
2777 ir_node *right = expression_to_firm(expression->right);
2778 return create_op(dbgi, expression, left, right);
2780 case EXPR_BINARY_LOGICAL_AND:
2781 case EXPR_BINARY_LOGICAL_OR:
2782 return create_lazy_op(expression);
2783 case EXPR_BINARY_COMMA:
2784 /* create side effects of left side */
2785 (void) expression_to_firm(expression->left);
2786 return _expression_to_firm(expression->right);
2788 case EXPR_BINARY_ADD_ASSIGN:
2789 case EXPR_BINARY_SUB_ASSIGN:
2790 case EXPR_BINARY_MUL_ASSIGN:
2791 case EXPR_BINARY_MOD_ASSIGN:
2792 case EXPR_BINARY_DIV_ASSIGN:
2793 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2794 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2795 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2796 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2797 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2798 return create_assign_binop(expression);
2800 panic("TODO binexpr type");
2804 static ir_node *array_access_addr(const array_access_expression_t *expression)
2806 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2807 ir_node *base_addr = expression_to_firm(expression->array_ref);
2808 ir_node *offset = expression_to_firm(expression->index);
2809 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2810 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2811 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2816 static ir_node *array_access_to_firm(
2817 const array_access_expression_t *expression)
2819 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2820 ir_node *addr = array_access_addr(expression);
2821 type_t *type = revert_automatic_type_conversion(
2822 (const expression_t*) expression);
2823 type = skip_typeref(type);
2825 return deref_address(dbgi, type, addr);
2828 static long get_offsetof_offset(const offsetof_expression_t *expression)
2830 type_t *orig_type = expression->type;
2833 designator_t *designator = expression->designator;
2834 for ( ; designator != NULL; designator = designator->next) {
2835 type_t *type = skip_typeref(orig_type);
2836 /* be sure the type is constructed */
2837 (void) get_ir_type(type);
2839 if (designator->symbol != NULL) {
2840 assert(is_type_compound(type));
2841 symbol_t *symbol = designator->symbol;
2843 compound_t *compound = type->compound.compound;
2844 entity_t *iter = compound->members.entities;
2845 for ( ; iter != NULL; iter = iter->base.next) {
2846 if (iter->base.symbol == symbol) {
2850 assert(iter != NULL);
2852 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2853 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2854 offset += get_entity_offset(iter->compound_member.entity);
2856 orig_type = iter->declaration.type;
2858 expression_t *array_index = designator->array_index;
2859 assert(designator->array_index != NULL);
2860 assert(is_type_array(type));
2862 long index = fold_constant_to_int(array_index);
2863 ir_type *arr_type = get_ir_type(type);
2864 ir_type *elem_type = get_array_element_type(arr_type);
2865 long elem_size = get_type_size_bytes(elem_type);
2867 offset += index * elem_size;
2869 orig_type = type->array.element_type;
2876 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2878 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2879 long offset = get_offsetof_offset(expression);
2880 tarval *tv = new_tarval_from_long(offset, mode);
2881 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2883 return new_d_Const(dbgi, tv);
2886 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2887 ir_entity *entity, type_t *type);
2889 static ir_node *compound_literal_to_firm(
2890 const compound_literal_expression_t *expression)
2892 type_t *type = expression->type;
2894 /* create an entity on the stack */
2895 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2897 ident *const id = id_unique("CompLit.%u");
2898 ir_type *const irtype = get_ir_type(type);
2899 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2900 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2901 set_entity_ld_ident(entity, id);
2903 /* create initialisation code */
2904 initializer_t *initializer = expression->initializer;
2905 create_local_initializer(initializer, dbgi, entity, type);
2907 /* create a sel for the compound literal address */
2908 ir_node *frame = get_irg_frame(current_ir_graph);
2909 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2914 * Transform a sizeof expression into Firm code.
2916 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2918 type_t *const type = skip_typeref(expression->type);
2919 /* §6.5.3.4:2 if the type is a VLA, evaluate the expression. */
2920 if (is_type_array(type) && type->array.is_vla
2921 && expression->tp_expression != NULL) {
2922 expression_to_firm(expression->tp_expression);
2925 return get_type_size_node(type);
2928 static entity_t *get_expression_entity(const expression_t *expression)
2930 if (expression->kind != EXPR_REFERENCE)
2933 return expression->reference.entity;
2936 static unsigned get_cparser_entity_alignment(const entity_t *entity)
2938 switch(entity->kind) {
2939 DECLARATION_KIND_CASES
2940 return entity->declaration.alignment;
2943 return entity->compound.alignment;
2944 case ENTITY_TYPEDEF:
2945 return entity->typedefe.alignment;
2953 * Transform an alignof expression into Firm code.
2955 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2957 unsigned alignment = 0;
2959 const expression_t *tp_expression = expression->tp_expression;
2960 if (tp_expression != NULL) {
2961 entity_t *entity = get_expression_entity(tp_expression);
2962 if (entity != NULL) {
2963 alignment = get_cparser_entity_alignment(entity);
2967 if (alignment == 0) {
2968 type_t *type = expression->type;
2969 alignment = get_type_alignment(type);
2972 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2973 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2974 tarval *tv = new_tarval_from_long(alignment, mode);
2975 return new_d_Const(dbgi, tv);
2978 static void init_ir_types(void);
2980 static tarval *fold_constant_to_tarval(const expression_t *expression)
2982 assert(is_type_valid(skip_typeref(expression->base.type)));
2984 bool constant_folding_old = constant_folding;
2985 constant_folding = true;
2989 assert(is_constant_expression(expression));
2991 ir_graph *old_current_ir_graph = current_ir_graph;
2992 current_ir_graph = get_const_code_irg();
2994 ir_node *cnst = expression_to_firm(expression);
2995 current_ir_graph = old_current_ir_graph;
2997 if (!is_Const(cnst)) {
2998 panic("couldn't fold constant");
3001 constant_folding = constant_folding_old;
3003 tarval *tv = get_Const_tarval(cnst);
3007 long fold_constant_to_int(const expression_t *expression)
3009 if (expression->kind == EXPR_INVALID)
3012 tarval *tv = fold_constant_to_tarval(expression);
3013 if (!tarval_is_long(tv)) {
3014 panic("result of constant folding is not integer");
3017 return get_tarval_long(tv);
3020 bool fold_constant_to_bool(const expression_t *expression)
3022 if (expression->kind == EXPR_INVALID)
3024 tarval *tv = fold_constant_to_tarval(expression);
3025 return !tarval_is_null(tv);
3028 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3030 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3032 /* first try to fold a constant condition */
3033 if (is_constant_expression(expression->condition)) {
3034 bool val = fold_constant_to_bool(expression->condition);
3036 expression_t *true_expression = expression->true_expression;
3037 if (true_expression == NULL)
3038 true_expression = expression->condition;
3039 return expression_to_firm(true_expression);
3041 return expression_to_firm(expression->false_expression);
3045 ir_node *cur_block = get_cur_block();
3047 /* create the true block */
3048 ir_node *true_block = new_immBlock();
3049 set_cur_block(true_block);
3051 ir_node *true_val = expression->true_expression != NULL ?
3052 expression_to_firm(expression->true_expression) : NULL;
3053 ir_node *true_jmp = new_Jmp();
3055 /* create the false block */
3056 ir_node *false_block = new_immBlock();
3057 set_cur_block(false_block);
3059 ir_node *false_val = expression_to_firm(expression->false_expression);
3060 ir_node *false_jmp = new_Jmp();
3062 /* create the condition evaluation */
3063 set_cur_block(cur_block);
3064 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3065 if (expression->true_expression == NULL) {
3066 if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3067 true_val = cond_expr;
3069 /* Condition ended with a short circuit (&&, ||, !) operation or a
3070 * comparison. Generate a "1" as value for the true branch. */
3071 true_val = new_Const(get_mode_one(mode_Is));
3074 mature_immBlock(true_block);
3075 mature_immBlock(false_block);
3077 /* create the common block */
3078 ir_node *in_cf[2] = { true_jmp, false_jmp };
3079 new_Block(2, in_cf);
3081 /* TODO improve static semantics, so either both or no values are NULL */
3082 if (true_val == NULL || false_val == NULL)
3085 ir_node *in[2] = { true_val, false_val };
3086 ir_mode *mode = get_irn_mode(true_val);
3087 assert(get_irn_mode(false_val) == mode);
3088 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
3094 * Returns an IR-node representing the address of a field.
3096 static ir_node *select_addr(const select_expression_t *expression)
3098 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3100 construct_select_compound(expression);
3102 ir_node *compound_addr = expression_to_firm(expression->compound);
3104 entity_t *entry = expression->compound_entry;
3105 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3106 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3108 if (constant_folding) {
3109 ir_mode *mode = get_irn_mode(compound_addr);
3110 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3111 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3112 return new_d_Add(dbgi, compound_addr, ofs, mode);
3114 ir_entity *irentity = entry->compound_member.entity;
3115 assert(irentity != NULL);
3116 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3120 static ir_node *select_to_firm(const select_expression_t *expression)
3122 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3123 ir_node *addr = select_addr(expression);
3124 type_t *type = revert_automatic_type_conversion(
3125 (const expression_t*) expression);
3126 type = skip_typeref(type);
3128 entity_t *entry = expression->compound_entry;
3129 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3130 type_t *entry_type = skip_typeref(entry->declaration.type);
3132 if (entry_type->kind == TYPE_BITFIELD) {
3133 return bitfield_extract_to_firm(expression, addr);
3136 return deref_address(dbgi, type, addr);
3139 /* Values returned by __builtin_classify_type. */
3140 typedef enum gcc_type_class
3146 enumeral_type_class,
3149 reference_type_class,
3153 function_type_class,
3164 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3166 type_t *type = expr->type_expression->base.type;
3168 /* FIXME gcc returns different values depending on whether compiling C or C++
3169 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3172 type = skip_typeref(type);
3173 switch (type->kind) {
3175 const atomic_type_t *const atomic_type = &type->atomic;
3176 switch (atomic_type->akind) {
3177 /* should not be reached */
3178 case ATOMIC_TYPE_INVALID:
3182 /* gcc cannot do that */
3183 case ATOMIC_TYPE_VOID:
3184 tc = void_type_class;
3187 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3188 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3189 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3190 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3191 case ATOMIC_TYPE_SHORT:
3192 case ATOMIC_TYPE_USHORT:
3193 case ATOMIC_TYPE_INT:
3194 case ATOMIC_TYPE_UINT:
3195 case ATOMIC_TYPE_LONG:
3196 case ATOMIC_TYPE_ULONG:
3197 case ATOMIC_TYPE_LONGLONG:
3198 case ATOMIC_TYPE_ULONGLONG:
3199 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3200 tc = integer_type_class;
3203 case ATOMIC_TYPE_FLOAT:
3204 case ATOMIC_TYPE_DOUBLE:
3205 case ATOMIC_TYPE_LONG_DOUBLE:
3206 tc = real_type_class;
3209 panic("Unexpected atomic type in classify_type_to_firm().");
3212 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3213 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3214 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3215 case TYPE_ARRAY: /* gcc handles this as pointer */
3216 case TYPE_FUNCTION: /* gcc handles this as pointer */
3217 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3218 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3219 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3221 /* gcc handles this as integer */
3222 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3224 /* gcc classifies the referenced type */
3225 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3228 /* typedef/typeof should be skipped already */
3235 panic("unexpected TYPE classify_type_to_firm().");
3239 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3240 tarval *const tv = new_tarval_from_long(tc, mode_int);
3241 return new_d_Const(dbgi, tv);
3244 static ir_node *function_name_to_firm(
3245 const funcname_expression_t *const expr)
3247 switch(expr->kind) {
3248 case FUNCNAME_FUNCTION:
3249 case FUNCNAME_PRETTY_FUNCTION:
3250 case FUNCNAME_FUNCDNAME:
3251 if (current_function_name == NULL) {
3252 const source_position_t *const src_pos = &expr->base.source_position;
3253 const char *name = current_function_entity->base.symbol->string;
3254 const string_t string = { name, strlen(name) + 1 };
3255 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3257 return current_function_name;
3258 case FUNCNAME_FUNCSIG:
3259 if (current_funcsig == NULL) {
3260 const source_position_t *const src_pos = &expr->base.source_position;
3261 ir_entity *ent = get_irg_entity(current_ir_graph);
3262 const char *const name = get_entity_ld_name(ent);
3263 const string_t string = { name, strlen(name) + 1 };
3264 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3266 return current_funcsig;
3268 panic("Unsupported function name");
3271 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3273 statement_t *statement = expr->statement;
3275 assert(statement->kind == STATEMENT_COMPOUND);
3276 return compound_statement_to_firm(&statement->compound);
3279 static ir_node *va_start_expression_to_firm(
3280 const va_start_expression_t *const expr)
3282 type_t *const type = current_function_entity->declaration.type;
3283 ir_type *const method_type = get_ir_type(type);
3284 int const n = get_method_n_params(method_type) - 1;
3285 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3286 ir_node *const frame = get_irg_frame(current_ir_graph);
3287 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3288 ir_node *const no_mem = new_NoMem();
3289 ir_node *const arg_sel =
3290 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3292 type_t *const param_type = expr->parameter->base.type;
3293 ir_node *const cnst = get_type_size_node(param_type);
3294 ir_mode *const mode = get_irn_mode(cnst);
3295 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3296 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3297 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3298 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3299 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3300 set_value_for_expression(expr->ap, add);
3305 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3307 type_t *const type = expr->base.type;
3308 expression_t *const ap_expr = expr->ap;
3309 ir_node *const ap_addr = expression_to_addr(ap_expr);
3310 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3311 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3312 ir_node *const res = deref_address(dbgi, type, ap);
3314 ir_node *const cnst = get_type_size_node(expr->base.type);
3315 ir_mode *const mode = get_irn_mode(cnst);
3316 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3317 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3318 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3319 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3320 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3322 set_value_for_expression_addr(ap_expr, add, ap_addr);
3328 * Generate Firm for a va_copy expression.
3330 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3332 ir_node *const src = expression_to_firm(expr->src);
3333 set_value_for_expression(expr->dst, src);
3337 static ir_node *dereference_addr(const unary_expression_t *const expression)
3339 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3340 return expression_to_firm(expression->value);
3344 * Returns a IR-node representing an lvalue of the given expression.
3346 static ir_node *expression_to_addr(const expression_t *expression)
3348 switch(expression->kind) {
3349 case EXPR_ARRAY_ACCESS:
3350 return array_access_addr(&expression->array_access);
3352 return call_expression_to_firm(&expression->call);
3353 case EXPR_COMPOUND_LITERAL:
3354 return compound_literal_to_firm(&expression->compound_literal);
3355 case EXPR_REFERENCE:
3356 return reference_addr(&expression->reference);
3358 return select_addr(&expression->select);
3359 case EXPR_UNARY_DEREFERENCE:
3360 return dereference_addr(&expression->unary);
3364 panic("trying to get address of non-lvalue");
3367 static ir_node *builtin_constant_to_firm(
3368 const builtin_constant_expression_t *expression)
3370 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3373 if (is_constant_expression(expression->value)) {
3378 return new_Const_long(mode, v);
3381 static ir_node *builtin_types_compatible_to_firm(
3382 const builtin_types_compatible_expression_t *expression)
3384 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3385 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3386 long const value = types_compatible(left, right) ? 1 : 0;
3387 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3388 return new_Const_long(mode, value);
3391 static ir_node *get_label_block(label_t *label)
3393 if (label->block != NULL)
3394 return label->block;
3396 /* beware: might be called from create initializer with current_ir_graph
3397 * set to const_code_irg. */
3398 ir_graph *rem = current_ir_graph;
3399 current_ir_graph = current_function;
3401 ir_node *block = new_immBlock();
3403 label->block = block;
3405 ARR_APP1(label_t *, all_labels, label);
3407 current_ir_graph = rem;
3412 * Pointer to a label. This is used for the
3413 * GNU address-of-label extension.
3415 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3417 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3418 ir_node *block = get_label_block(label->label);
3419 ir_entity *entity = create_Block_entity(block);
3421 symconst_symbol value;
3422 value.entity_p = entity;
3423 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
3427 * creates firm nodes for an expression. The difference between this function
3428 * and expression_to_firm is, that this version might produce mode_b nodes
3429 * instead of mode_Is.
3431 static ir_node *_expression_to_firm(const expression_t *expression)
3434 if (!constant_folding) {
3435 assert(!expression->base.transformed);
3436 ((expression_t*) expression)->base.transformed = true;
3440 switch (expression->kind) {
3441 case EXPR_CHARACTER_CONSTANT:
3442 return character_constant_to_firm(&expression->conste);
3443 case EXPR_WIDE_CHARACTER_CONSTANT:
3444 return wide_character_constant_to_firm(&expression->conste);
3446 return const_to_firm(&expression->conste);
3447 case EXPR_STRING_LITERAL:
3448 return string_literal_to_firm(&expression->string);
3449 case EXPR_WIDE_STRING_LITERAL:
3450 return wide_string_literal_to_firm(&expression->wide_string);
3451 case EXPR_REFERENCE:
3452 return reference_expression_to_firm(&expression->reference);
3453 case EXPR_REFERENCE_ENUM_VALUE:
3454 return reference_expression_enum_value_to_firm(&expression->reference);
3456 return call_expression_to_firm(&expression->call);
3458 return unary_expression_to_firm(&expression->unary);
3460 return binary_expression_to_firm(&expression->binary);
3461 case EXPR_ARRAY_ACCESS:
3462 return array_access_to_firm(&expression->array_access);
3464 return sizeof_to_firm(&expression->typeprop);
3466 return alignof_to_firm(&expression->typeprop);
3467 case EXPR_CONDITIONAL:
3468 return conditional_to_firm(&expression->conditional);
3470 return select_to_firm(&expression->select);
3471 case EXPR_CLASSIFY_TYPE:
3472 return classify_type_to_firm(&expression->classify_type);
3474 return function_name_to_firm(&expression->funcname);
3475 case EXPR_STATEMENT:
3476 return statement_expression_to_firm(&expression->statement);
3478 return va_start_expression_to_firm(&expression->va_starte);
3480 return va_arg_expression_to_firm(&expression->va_arge);
3482 return va_copy_expression_to_firm(&expression->va_copye);
3483 case EXPR_BUILTIN_CONSTANT_P:
3484 return builtin_constant_to_firm(&expression->builtin_constant);
3485 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3486 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3488 return offsetof_to_firm(&expression->offsetofe);
3489 case EXPR_COMPOUND_LITERAL:
3490 return compound_literal_to_firm(&expression->compound_literal);
3491 case EXPR_LABEL_ADDRESS:
3492 return label_address_to_firm(&expression->label_address);
3498 panic("invalid expression found");
3502 * Check if a given expression is a GNU __builtin_expect() call.
3504 static bool is_builtin_expect(const expression_t *expression)
3506 if (expression->kind != EXPR_CALL)
3509 expression_t *function = expression->call.function;
3510 if (function->kind != EXPR_REFERENCE)
3512 reference_expression_t *ref = &function->reference;
3513 if (ref->entity->kind != ENTITY_FUNCTION ||
3514 ref->entity->function.btk != bk_gnu_builtin_expect)
3520 static bool produces_mode_b(const expression_t *expression)
3522 switch (expression->kind) {
3523 case EXPR_BINARY_EQUAL:
3524 case EXPR_BINARY_NOTEQUAL:
3525 case EXPR_BINARY_LESS:
3526 case EXPR_BINARY_LESSEQUAL:
3527 case EXPR_BINARY_GREATER:
3528 case EXPR_BINARY_GREATEREQUAL:
3529 case EXPR_BINARY_ISGREATER:
3530 case EXPR_BINARY_ISGREATEREQUAL:
3531 case EXPR_BINARY_ISLESS:
3532 case EXPR_BINARY_ISLESSEQUAL:
3533 case EXPR_BINARY_ISLESSGREATER:
3534 case EXPR_BINARY_ISUNORDERED:
3535 case EXPR_UNARY_NOT:
3539 if (is_builtin_expect(expression)) {
3540 expression_t *argument = expression->call.arguments->expression;
3541 return produces_mode_b(argument);
3544 case EXPR_BINARY_COMMA:
3545 return produces_mode_b(expression->binary.right);
3552 static ir_node *expression_to_firm(const expression_t *expression)
3554 if (!produces_mode_b(expression)) {
3555 ir_node *res = _expression_to_firm(expression);
3556 assert(res == NULL || get_irn_mode(res) != mode_b);
3560 if (is_constant_expression(expression)) {
3561 ir_node *res = _expression_to_firm(expression);
3562 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3563 assert(is_Const(res));
3564 if (is_Const_null(res)) {
3565 return new_Const_long(mode, 0);
3567 return new_Const_long(mode, 1);
3571 /* we have to produce a 0/1 from the mode_b expression */
3572 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3573 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3574 return produce_condition_result(expression, mode, dbgi);
3578 * create a short-circuit expression evaluation that tries to construct
3579 * efficient control flow structures for &&, || and ! expressions
3581 static ir_node *create_condition_evaluation(const expression_t *expression,
3582 ir_node *true_block,
3583 ir_node *false_block)
3585 switch(expression->kind) {
3586 case EXPR_UNARY_NOT: {
3587 const unary_expression_t *unary_expression = &expression->unary;
3588 create_condition_evaluation(unary_expression->value, false_block,
3592 case EXPR_BINARY_LOGICAL_AND: {
3593 const binary_expression_t *binary_expression = &expression->binary;
3595 ir_node *extra_block = new_immBlock();
3596 create_condition_evaluation(binary_expression->left, extra_block,
3598 mature_immBlock(extra_block);
3599 set_cur_block(extra_block);
3600 create_condition_evaluation(binary_expression->right, true_block,
3604 case EXPR_BINARY_LOGICAL_OR: {
3605 const binary_expression_t *binary_expression = &expression->binary;
3607 ir_node *extra_block = new_immBlock();
3608 create_condition_evaluation(binary_expression->left, true_block,
3610 mature_immBlock(extra_block);
3611 set_cur_block(extra_block);
3612 create_condition_evaluation(binary_expression->right, true_block,
3620 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3621 ir_node *cond_expr = _expression_to_firm(expression);
3622 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3623 ir_node *cond = new_d_Cond(dbgi, condition);
3624 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3625 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3627 /* set branch prediction info based on __builtin_expect */
3628 if (is_builtin_expect(expression) && is_Cond(cond)) {
3629 call_argument_t *argument = expression->call.arguments->next;
3630 if (is_constant_expression(argument->expression)) {
3631 bool cnst = fold_constant_to_bool(argument->expression);
3632 cond_jmp_predicate pred;
3634 if (cnst == false) {
3635 pred = COND_JMP_PRED_FALSE;
3637 pred = COND_JMP_PRED_TRUE;
3639 set_Cond_jmp_pred(cond, pred);
3643 add_immBlock_pred(true_block, true_proj);
3644 add_immBlock_pred(false_block, false_proj);
3646 set_cur_block(NULL);
3650 static void create_variable_entity(entity_t *variable,
3651 declaration_kind_t declaration_kind,
3652 ir_type *parent_type)
3654 assert(variable->kind == ENTITY_VARIABLE);
3655 type_t *type = skip_typeref(variable->declaration.type);
3657 ident *const id = new_id_from_str(variable->base.symbol->string);
3658 ir_type *const irtype = get_ir_type(type);
3659 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3660 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3661 unsigned alignment = variable->declaration.alignment;
3663 set_entity_alignment(irentity, alignment);
3665 handle_decl_modifiers(irentity, variable);
3667 variable->declaration.kind = (unsigned char) declaration_kind;
3668 variable->variable.v.entity = irentity;
3669 set_entity_ld_ident(irentity, create_ld_ident(variable));
3671 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3672 set_entity_volatility(irentity, volatility_is_volatile);
3677 typedef struct type_path_entry_t type_path_entry_t;
3678 struct type_path_entry_t {
3680 ir_initializer_t *initializer;
3682 entity_t *compound_entry;
3685 typedef struct type_path_t type_path_t;
3686 struct type_path_t {
3687 type_path_entry_t *path;
3692 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3694 size_t len = ARR_LEN(path->path);
3696 for (size_t i = 0; i < len; ++i) {
3697 const type_path_entry_t *entry = & path->path[i];
3699 type_t *type = skip_typeref(entry->type);
3700 if (is_type_compound(type)) {
3701 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3702 } else if (is_type_array(type)) {
3703 fprintf(stderr, "[%u]", (unsigned) entry->index);
3705 fprintf(stderr, "-INVALID-");
3708 fprintf(stderr, " (");
3709 print_type(path->top_type);
3710 fprintf(stderr, ")");
3713 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3715 size_t len = ARR_LEN(path->path);
3717 return & path->path[len-1];
3720 static type_path_entry_t *append_to_type_path(type_path_t *path)
3722 size_t len = ARR_LEN(path->path);
3723 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3725 type_path_entry_t *result = & path->path[len];
3726 memset(result, 0, sizeof(result[0]));
3730 static size_t get_compound_member_count(const compound_type_t *type)
3732 compound_t *compound = type->compound;
3733 size_t n_members = 0;
3734 entity_t *member = compound->members.entities;
3735 for ( ; member != NULL; member = member->base.next) {
3742 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3744 type_t *orig_top_type = path->top_type;
3745 type_t *top_type = skip_typeref(orig_top_type);
3747 assert(is_type_compound(top_type) || is_type_array(top_type));
3749 if (ARR_LEN(path->path) == 0) {
3752 type_path_entry_t *top = get_type_path_top(path);
3753 ir_initializer_t *initializer = top->initializer;
3754 return get_initializer_compound_value(initializer, top->index);
3758 static void descend_into_subtype(type_path_t *path)
3760 type_t *orig_top_type = path->top_type;
3761 type_t *top_type = skip_typeref(orig_top_type);
3763 assert(is_type_compound(top_type) || is_type_array(top_type));
3765 ir_initializer_t *initializer = get_initializer_entry(path);
3767 type_path_entry_t *top = append_to_type_path(path);
3768 top->type = top_type;
3772 if (is_type_compound(top_type)) {
3773 compound_t *compound = top_type->compound.compound;
3774 entity_t *entry = compound->members.entities;
3776 top->compound_entry = entry;
3778 len = get_compound_member_count(&top_type->compound);
3779 if (entry != NULL) {
3780 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3781 path->top_type = entry->declaration.type;
3784 assert(is_type_array(top_type));
3785 assert(top_type->array.size > 0);
3788 path->top_type = top_type->array.element_type;
3789 len = top_type->array.size;
3791 if (initializer == NULL
3792 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3793 initializer = create_initializer_compound(len);
3794 /* we have to set the entry at the 2nd latest path entry... */
3795 size_t path_len = ARR_LEN(path->path);
3796 assert(path_len >= 1);
3798 type_path_entry_t *entry = & path->path[path_len-2];
3799 ir_initializer_t *tinitializer = entry->initializer;
3800 set_initializer_compound_value(tinitializer, entry->index,
3804 top->initializer = initializer;
3807 static void ascend_from_subtype(type_path_t *path)
3809 type_path_entry_t *top = get_type_path_top(path);
3811 path->top_type = top->type;
3813 size_t len = ARR_LEN(path->path);
3814 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3817 static void walk_designator(type_path_t *path, const designator_t *designator)
3819 /* designators start at current object type */
3820 ARR_RESIZE(type_path_entry_t, path->path, 1);
3822 for ( ; designator != NULL; designator = designator->next) {
3823 type_path_entry_t *top = get_type_path_top(path);
3824 type_t *orig_type = top->type;
3825 type_t *type = skip_typeref(orig_type);
3827 if (designator->symbol != NULL) {
3828 assert(is_type_compound(type));
3830 symbol_t *symbol = designator->symbol;
3832 compound_t *compound = type->compound.compound;
3833 entity_t *iter = compound->members.entities;
3834 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3835 if (iter->base.symbol == symbol) {
3836 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3840 assert(iter != NULL);
3842 /* revert previous initialisations of other union elements */
3843 if (type->kind == TYPE_COMPOUND_UNION) {
3844 ir_initializer_t *initializer = top->initializer;
3845 if (initializer != NULL
3846 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3847 /* are we writing to a new element? */
3848 ir_initializer_t *oldi
3849 = get_initializer_compound_value(initializer, index);
3850 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3851 /* clear initializer */
3853 = get_initializer_compound_n_entries(initializer);
3854 ir_initializer_t *nulli = get_initializer_null();
3855 for (size_t i = 0; i < len; ++i) {
3856 set_initializer_compound_value(initializer, i,
3863 top->type = orig_type;
3864 top->compound_entry = iter;
3866 orig_type = iter->declaration.type;
3868 expression_t *array_index = designator->array_index;
3869 assert(designator->array_index != NULL);
3870 assert(is_type_array(type));
3872 long index = fold_constant_to_int(array_index);
3875 if (type->array.size_constant) {
3876 long array_size = type->array.size;
3877 assert(index < array_size);
3881 top->type = orig_type;
3882 top->index = (size_t) index;
3883 orig_type = type->array.element_type;
3885 path->top_type = orig_type;
3887 if (designator->next != NULL) {
3888 descend_into_subtype(path);
3892 path->invalid = false;
3895 static void advance_current_object(type_path_t *path)
3897 if (path->invalid) {
3898 /* TODO: handle this... */
3899 panic("invalid initializer in ast2firm (excessive elements)");
3902 type_path_entry_t *top = get_type_path_top(path);
3904 type_t *type = skip_typeref(top->type);
3905 if (is_type_union(type)) {
3906 /* only the first element is initialized in unions */
3907 top->compound_entry = NULL;
3908 } else if (is_type_struct(type)) {
3909 entity_t *entry = top->compound_entry;
3912 entry = entry->base.next;
3913 top->compound_entry = entry;
3914 if (entry != NULL) {
3915 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3916 path->top_type = entry->declaration.type;
3920 assert(is_type_array(type));
3923 if (!type->array.size_constant || top->index < type->array.size) {
3928 /* we're past the last member of the current sub-aggregate, try if we
3929 * can ascend in the type hierarchy and continue with another subobject */
3930 size_t len = ARR_LEN(path->path);
3933 ascend_from_subtype(path);
3934 advance_current_object(path);
3936 path->invalid = true;
3941 static ir_initializer_t *create_ir_initializer(
3942 const initializer_t *initializer, type_t *type);
3944 static ir_initializer_t *create_ir_initializer_value(
3945 const initializer_value_t *initializer)
3947 if (is_type_compound(initializer->value->base.type)) {
3948 panic("initializer creation for compounds not implemented yet");
3950 type_t *type = initializer->value->base.type;
3951 expression_t *expr = initializer->value;
3952 if (initializer_use_bitfield_basetype) {
3953 type_t *skipped = skip_typeref(type);
3954 if (skipped->kind == TYPE_BITFIELD) {
3955 /* remove the bitfield cast... */
3956 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
3957 expr = expr->unary.value;
3958 type = skipped->bitfield.base_type;
3961 ir_node *value = expression_to_firm(expr);
3962 ir_mode *mode = get_ir_mode_storage(type);
3963 value = create_conv(NULL, value, mode);
3964 return create_initializer_const(value);
3967 /** test wether type can be initialized by a string constant */
3968 static bool is_string_type(type_t *type)
3971 if (is_type_pointer(type)) {
3972 inner = skip_typeref(type->pointer.points_to);
3973 } else if(is_type_array(type)) {
3974 inner = skip_typeref(type->array.element_type);
3979 return is_type_integer(inner);
3982 static ir_initializer_t *create_ir_initializer_list(
3983 const initializer_list_t *initializer, type_t *type)
3986 memset(&path, 0, sizeof(path));
3987 path.top_type = type;
3988 path.path = NEW_ARR_F(type_path_entry_t, 0);
3990 descend_into_subtype(&path);
3992 for (size_t i = 0; i < initializer->len; ++i) {
3993 const initializer_t *sub_initializer = initializer->initializers[i];
3995 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3996 walk_designator(&path, sub_initializer->designator.designator);
4000 if (sub_initializer->kind == INITIALIZER_VALUE) {
4001 /* we might have to descend into types until we're at a scalar
4004 type_t *orig_top_type = path.top_type;
4005 type_t *top_type = skip_typeref(orig_top_type);
4007 if (is_type_scalar(top_type))
4009 descend_into_subtype(&path);
4011 } else if (sub_initializer->kind == INITIALIZER_STRING
4012 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4013 /* we might have to descend into types until we're at a scalar
4016 type_t *orig_top_type = path.top_type;
4017 type_t *top_type = skip_typeref(orig_top_type);
4019 if (is_string_type(top_type))
4021 descend_into_subtype(&path);
4025 ir_initializer_t *sub_irinitializer
4026 = create_ir_initializer(sub_initializer, path.top_type);
4028 size_t path_len = ARR_LEN(path.path);
4029 assert(path_len >= 1);
4030 type_path_entry_t *entry = & path.path[path_len-1];
4031 ir_initializer_t *tinitializer = entry->initializer;
4032 set_initializer_compound_value(tinitializer, entry->index,
4035 advance_current_object(&path);
4038 assert(ARR_LEN(path.path) >= 1);
4039 ir_initializer_t *result = path.path[0].initializer;
4040 DEL_ARR_F(path.path);
4045 static ir_initializer_t *create_ir_initializer_string(
4046 const initializer_string_t *initializer, type_t *type)
4048 type = skip_typeref(type);
4050 size_t string_len = initializer->string.size;
4051 assert(type->kind == TYPE_ARRAY);
4052 assert(type->array.size_constant);
4053 size_t len = type->array.size;
4054 ir_initializer_t *irinitializer = create_initializer_compound(len);
4056 const char *string = initializer->string.begin;
4057 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4059 for (size_t i = 0; i < len; ++i) {
4064 tarval *tv = new_tarval_from_long(c, mode);
4065 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4067 set_initializer_compound_value(irinitializer, i, char_initializer);
4070 return irinitializer;
4073 static ir_initializer_t *create_ir_initializer_wide_string(
4074 const initializer_wide_string_t *initializer, type_t *type)
4076 size_t string_len = initializer->string.size;
4077 assert(type->kind == TYPE_ARRAY);
4078 assert(type->array.size_constant);
4079 size_t len = type->array.size;
4080 ir_initializer_t *irinitializer = create_initializer_compound(len);
4082 const wchar_rep_t *string = initializer->string.begin;
4083 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4085 for (size_t i = 0; i < len; ++i) {
4087 if (i < string_len) {
4090 tarval *tv = new_tarval_from_long(c, mode);
4091 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4093 set_initializer_compound_value(irinitializer, i, char_initializer);
4096 return irinitializer;
4099 static ir_initializer_t *create_ir_initializer(
4100 const initializer_t *initializer, type_t *type)
4102 switch(initializer->kind) {
4103 case INITIALIZER_STRING:
4104 return create_ir_initializer_string(&initializer->string, type);
4106 case INITIALIZER_WIDE_STRING:
4107 return create_ir_initializer_wide_string(&initializer->wide_string,
4110 case INITIALIZER_LIST:
4111 return create_ir_initializer_list(&initializer->list, type);
4113 case INITIALIZER_VALUE:
4114 return create_ir_initializer_value(&initializer->value);
4116 case INITIALIZER_DESIGNATOR:
4117 panic("unexpected designator initializer found");
4119 panic("unknown initializer");
4122 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4123 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4125 switch(get_initializer_kind(initializer)) {
4126 case IR_INITIALIZER_NULL: {
4127 /* NULL is undefined for dynamic initializers */
4130 case IR_INITIALIZER_CONST: {
4131 ir_node *node = get_initializer_const_value(initializer);
4132 ir_mode *mode = get_irn_mode(node);
4133 ir_type *ent_type = get_entity_type(entity);
4135 /* is it a bitfield type? */
4136 if (is_Primitive_type(ent_type) &&
4137 get_primitive_base_type(ent_type) != NULL) {
4138 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4142 assert(get_type_mode(type) == mode);
4143 ir_node *mem = get_store();
4144 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4145 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4149 case IR_INITIALIZER_TARVAL: {
4150 tarval *tv = get_initializer_tarval_value(initializer);
4151 ir_mode *mode = get_tarval_mode(tv);
4152 ir_node *cnst = new_d_Const(dbgi, tv);
4153 ir_type *ent_type = get_entity_type(entity);
4155 /* is it a bitfield type? */
4156 if (is_Primitive_type(ent_type) &&
4157 get_primitive_base_type(ent_type) != NULL) {
4158 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4162 assert(get_type_mode(type) == mode);
4163 ir_node *mem = get_store();
4164 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4165 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4169 case IR_INITIALIZER_COMPOUND: {
4170 assert(is_compound_type(type));
4172 if (is_Array_type(type)) {
4173 assert(has_array_upper_bound(type, 0));
4174 n_members = get_array_upper_bound_int(type, 0);
4176 n_members = get_compound_n_members(type);
4179 if (get_initializer_compound_n_entries(initializer)
4180 != (unsigned) n_members)
4181 panic("initializer doesn't match compound type");
4183 for (int i = 0; i < n_members; ++i) {
4186 ir_entity *sub_entity;
4187 if (is_Array_type(type)) {
4188 tarval *index_tv = new_tarval_from_long(i, mode_uint);
4189 ir_node *cnst = new_d_Const(dbgi, index_tv);
4190 ir_node *in[1] = { cnst };
4191 irtype = get_array_element_type(type);
4192 sub_entity = get_array_element_entity(type);
4193 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4196 sub_entity = get_compound_member(type, i);
4197 irtype = get_entity_type(sub_entity);
4198 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4202 ir_initializer_t *sub_init
4203 = get_initializer_compound_value(initializer, i);
4205 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4212 panic("invalid IR_INITIALIZER found");
4215 static void create_dynamic_initializer(ir_initializer_t *initializer,
4216 dbg_info *dbgi, ir_entity *entity)
4218 ir_node *frame = get_irg_frame(current_ir_graph);
4219 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4220 ir_type *type = get_entity_type(entity);
4222 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4225 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4226 ir_entity *entity, type_t *type)
4228 ir_node *memory = get_store();
4229 ir_node *nomem = new_NoMem();
4230 ir_node *frame = get_irg_frame(current_ir_graph);
4231 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4233 if (initializer->kind == INITIALIZER_VALUE) {
4234 initializer_value_t *initializer_value = &initializer->value;
4236 ir_node *value = expression_to_firm(initializer_value->value);
4237 type = skip_typeref(type);
4238 assign_value(dbgi, addr, type, value);
4242 if (!is_constant_initializer(initializer)) {
4243 bool old_initializer_use_bitfield_basetype
4244 = initializer_use_bitfield_basetype;
4245 initializer_use_bitfield_basetype = true;
4246 ir_initializer_t *irinitializer
4247 = create_ir_initializer(initializer, type);
4248 initializer_use_bitfield_basetype
4249 = old_initializer_use_bitfield_basetype;
4251 create_dynamic_initializer(irinitializer, dbgi, entity);
4255 /* create the ir_initializer */
4256 ir_graph *const old_current_ir_graph = current_ir_graph;
4257 current_ir_graph = get_const_code_irg();
4259 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4261 assert(current_ir_graph == get_const_code_irg());
4262 current_ir_graph = old_current_ir_graph;
4264 /* create a "template" entity which is copied to the entity on the stack */
4265 ident *const id = id_unique("initializer.%u");
4266 ir_type *const irtype = get_ir_type(type);
4267 ir_type *const global_type = get_glob_type();
4268 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4269 set_entity_ld_ident(init_entity, id);
4271 set_entity_visibility(init_entity, ir_visibility_local);
4272 add_entity_linkage(init_entity, IR_LINKAGE_CONSTANT);
4274 set_entity_initializer(init_entity, irinitializer);
4276 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4277 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4279 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M);
4280 set_store(copyb_mem);
4283 static void create_initializer_local_variable_entity(entity_t *entity)
4285 assert(entity->kind == ENTITY_VARIABLE);
4286 initializer_t *initializer = entity->variable.initializer;
4287 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4288 ir_entity *irentity = entity->variable.v.entity;
4289 type_t *type = entity->declaration.type;
4291 create_local_initializer(initializer, dbgi, irentity, type);
4294 static void create_variable_initializer(entity_t *entity)
4296 assert(entity->kind == ENTITY_VARIABLE);
4297 initializer_t *initializer = entity->variable.initializer;
4298 if (initializer == NULL)
4301 declaration_kind_t declaration_kind
4302 = (declaration_kind_t) entity->declaration.kind;
4303 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4304 create_initializer_local_variable_entity(entity);
4308 type_t *type = entity->declaration.type;
4309 type_qualifiers_t tq = get_type_qualifier(type, true);
4311 if (initializer->kind == INITIALIZER_VALUE) {
4312 initializer_value_t *initializer_value = &initializer->value;
4313 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4315 ir_node *value = expression_to_firm(initializer_value->value);
4317 type_t *type = initializer_value->value->base.type;
4318 ir_mode *mode = get_ir_mode_storage(type);
4319 value = create_conv(dbgi, value, mode);
4320 value = do_strict_conv(dbgi, value);
4322 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4323 set_value(entity->variable.v.value_number, value);
4325 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4327 ir_entity *irentity = entity->variable.v.entity;
4329 if (tq & TYPE_QUALIFIER_CONST) {
4330 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4332 set_atomic_ent_value(irentity, value);
4335 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4336 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4338 ir_entity *irentity = entity->variable.v.entity;
4339 ir_initializer_t *irinitializer
4340 = create_ir_initializer(initializer, type);
4342 if (tq & TYPE_QUALIFIER_CONST) {
4343 add_entity_linkage(irentity, IR_LINKAGE_CONSTANT);
4345 set_entity_initializer(irentity, irinitializer);
4349 static void create_variable_length_array(entity_t *entity)
4351 assert(entity->kind == ENTITY_VARIABLE);
4352 assert(entity->variable.initializer == NULL);
4354 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4355 entity->variable.v.vla_base = NULL;
4357 /* TODO: record VLA somewhere so we create the free node when we leave
4361 static void allocate_variable_length_array(entity_t *entity)
4363 assert(entity->kind == ENTITY_VARIABLE);
4364 assert(entity->variable.initializer == NULL);
4365 assert(get_cur_block() != NULL);
4367 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4368 type_t *type = entity->declaration.type;
4369 ir_type *el_type = get_ir_type(type->array.element_type);
4371 /* make sure size_node is calculated */
4372 get_type_size_node(type);
4373 ir_node *elems = type->array.size_node;
4374 ir_node *mem = get_store();
4375 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4377 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4378 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4381 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4382 entity->variable.v.vla_base = addr;
4386 * Creates a Firm local variable from a declaration.
4388 static void create_local_variable(entity_t *entity)
4390 assert(entity->kind == ENTITY_VARIABLE);
4391 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4393 bool needs_entity = entity->variable.address_taken;
4394 type_t *type = skip_typeref(entity->declaration.type);
4396 /* is it a variable length array? */
4397 if (is_type_array(type) && !type->array.size_constant) {
4398 create_variable_length_array(entity);
4400 } else if (is_type_array(type) || is_type_compound(type)) {
4401 needs_entity = true;
4402 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4403 needs_entity = true;
4407 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4408 create_variable_entity(entity,
4409 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4412 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4413 entity->variable.v.value_number = next_value_number_function;
4414 set_irg_loc_description(current_ir_graph, next_value_number_function,
4416 ++next_value_number_function;
4420 static void create_local_static_variable(entity_t *entity)
4422 assert(entity->kind == ENTITY_VARIABLE);
4423 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4425 type_t *type = skip_typeref(entity->declaration.type);
4426 ir_type *const var_type = entity->variable.thread_local ?
4427 get_tls_type() : get_glob_type();
4428 ir_type *const irtype = get_ir_type(type);
4429 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4431 size_t l = strlen(entity->base.symbol->string);
4432 char buf[l + sizeof(".%u")];
4433 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4434 ident *const id = id_unique(buf);
4436 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4438 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4439 set_entity_volatility(irentity, volatility_is_volatile);
4442 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4443 entity->variable.v.entity = irentity;
4445 set_entity_ld_ident(irentity, id);
4446 set_entity_visibility(irentity, ir_visibility_local);
4448 ir_graph *const old_current_ir_graph = current_ir_graph;
4449 current_ir_graph = get_const_code_irg();
4451 create_variable_initializer(entity);
4453 assert(current_ir_graph == get_const_code_irg());
4454 current_ir_graph = old_current_ir_graph;
4459 static void return_statement_to_firm(return_statement_t *statement)
4461 if (get_cur_block() == NULL)
4464 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4465 type_t *type = current_function_entity->declaration.type;
4466 ir_type *func_irtype = get_ir_type(type);
4471 if (get_method_n_ress(func_irtype) > 0) {
4472 ir_type *res_type = get_method_res_type(func_irtype, 0);
4474 if (statement->value != NULL) {
4475 ir_node *node = expression_to_firm(statement->value);
4476 if (!is_compound_type(res_type)) {
4477 type_t *type = statement->value->base.type;
4478 ir_mode *mode = get_ir_mode_storage(type);
4479 node = create_conv(dbgi, node, mode);
4480 node = do_strict_conv(dbgi, node);
4485 if (is_compound_type(res_type)) {
4488 mode = get_type_mode(res_type);
4490 in[0] = new_Unknown(mode);
4494 /* build return_value for its side effects */
4495 if (statement->value != NULL) {
4496 expression_to_firm(statement->value);
4501 ir_node *store = get_store();
4502 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4504 ir_node *end_block = get_irg_end_block(current_ir_graph);
4505 add_immBlock_pred(end_block, ret);
4507 set_cur_block(NULL);
4510 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4512 if (get_cur_block() == NULL)
4515 return expression_to_firm(statement->expression);
4518 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4520 entity_t *entity = compound->scope.entities;
4521 for ( ; entity != NULL; entity = entity->base.next) {
4522 if (!is_declaration(entity))
4525 create_local_declaration(entity);
4528 ir_node *result = NULL;
4529 statement_t *statement = compound->statements;
4530 for ( ; statement != NULL; statement = statement->base.next) {
4531 if (statement->base.next == NULL
4532 && statement->kind == STATEMENT_EXPRESSION) {
4533 result = expression_statement_to_firm(
4534 &statement->expression);
4537 statement_to_firm(statement);
4543 static void create_global_variable(entity_t *entity)
4545 ir_linkage linkage = 0;
4546 ir_visibility visibility = ir_visibility_default;
4547 ir_entity *irentity;
4548 assert(entity->kind == ENTITY_VARIABLE);
4550 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4551 case STORAGE_CLASS_EXTERN: visibility = ir_visibility_external; break;
4552 case STORAGE_CLASS_STATIC:
4553 visibility = ir_visibility_local;
4554 /* uninitialized globals get merged in C */
4555 if (entity->variable.initializer == NULL)
4556 linkage |= IR_LINKAGE_MERGE;
4558 case STORAGE_CLASS_NONE:
4559 visibility = ir_visibility_default;
4560 /* uninitialized globals get merged in C */
4561 if (entity->variable.initializer == NULL)
4562 linkage |= IR_LINKAGE_MERGE;
4564 case STORAGE_CLASS_TYPEDEF:
4565 case STORAGE_CLASS_AUTO:
4566 case STORAGE_CLASS_REGISTER:
4567 panic("invalid storage class for global var");
4570 ir_type *var_type = get_glob_type();
4571 if (entity->variable.thread_local) {
4572 var_type = get_tls_type();
4573 /* LINKAGE_MERGE not supported by current linkers */
4574 linkage &= ~IR_LINKAGE_MERGE;
4576 create_variable_entity(entity, DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4577 irentity = entity->variable.v.entity;
4578 add_entity_linkage(irentity, linkage);
4579 set_entity_visibility(irentity, visibility);
4582 static void create_local_declaration(entity_t *entity)
4584 assert(is_declaration(entity));
4586 /* construct type */
4587 (void) get_ir_type(entity->declaration.type);
4588 if (entity->base.symbol == NULL) {
4592 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4593 case STORAGE_CLASS_STATIC:
4594 if (entity->kind == ENTITY_FUNCTION) {
4595 (void)get_function_entity(entity, NULL);
4597 create_local_static_variable(entity);
4600 case STORAGE_CLASS_EXTERN:
4601 if (entity->kind == ENTITY_FUNCTION) {
4602 assert(entity->function.statement == NULL);
4603 (void)get_function_entity(entity, NULL);
4605 create_global_variable(entity);
4606 create_variable_initializer(entity);
4609 case STORAGE_CLASS_NONE:
4610 case STORAGE_CLASS_AUTO:
4611 case STORAGE_CLASS_REGISTER:
4612 if (entity->kind == ENTITY_FUNCTION) {
4613 if (entity->function.statement != NULL) {
4614 ir_type *owner = get_irg_frame_type(current_ir_graph);
4615 (void)get_function_entity(entity, owner);
4616 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4617 enqueue_inner_function(entity);
4619 (void)get_function_entity(entity, NULL);
4622 create_local_variable(entity);
4625 case STORAGE_CLASS_TYPEDEF:
4628 panic("invalid storage class found");
4631 static void initialize_local_declaration(entity_t *entity)
4633 if (entity->base.symbol == NULL)
4636 // no need to emit code in dead blocks
4637 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4638 && get_cur_block() == NULL)
4641 switch ((declaration_kind_t) entity->declaration.kind) {
4642 case DECLARATION_KIND_LOCAL_VARIABLE:
4643 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4644 create_variable_initializer(entity);
4647 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4648 allocate_variable_length_array(entity);
4651 case DECLARATION_KIND_COMPOUND_MEMBER:
4652 case DECLARATION_KIND_GLOBAL_VARIABLE:
4653 case DECLARATION_KIND_FUNCTION:
4654 case DECLARATION_KIND_INNER_FUNCTION:
4657 case DECLARATION_KIND_PARAMETER:
4658 case DECLARATION_KIND_PARAMETER_ENTITY:
4659 panic("can't initialize parameters");
4661 case DECLARATION_KIND_UNKNOWN:
4662 panic("can't initialize unknown declaration");
4664 panic("invalid declaration kind");
4667 static void declaration_statement_to_firm(declaration_statement_t *statement)
4669 entity_t *entity = statement->declarations_begin;
4673 entity_t *const last = statement->declarations_end;
4674 for ( ;; entity = entity->base.next) {
4675 if (is_declaration(entity)) {
4676 initialize_local_declaration(entity);
4677 } else if (entity->kind == ENTITY_TYPEDEF) {
4678 /* §6.7.7:3 Any array size expressions associated with variable length
4679 * array declarators are evaluated each time the declaration of the
4680 * typedef name is reached in the order of execution. */
4681 type_t *const type = skip_typeref(entity->typedefe.type);
4682 if (is_type_array(type) && type->array.is_vla)
4683 get_vla_size(&type->array);
4690 static void if_statement_to_firm(if_statement_t *statement)
4692 ir_node *cur_block = get_cur_block();
4694 ir_node *fallthrough_block = NULL;
4696 /* the true (blocks) */
4697 ir_node *true_block = NULL;
4698 if (statement->true_statement != NULL) {
4699 true_block = new_immBlock();
4700 set_cur_block(true_block);
4701 statement_to_firm(statement->true_statement);
4702 if (get_cur_block() != NULL) {
4703 ir_node *jmp = new_Jmp();
4704 if (fallthrough_block == NULL)
4705 fallthrough_block = new_immBlock();
4706 add_immBlock_pred(fallthrough_block, jmp);
4710 /* the false (blocks) */
4711 ir_node *false_block = NULL;
4712 if (statement->false_statement != NULL) {
4713 false_block = new_immBlock();
4714 set_cur_block(false_block);
4716 statement_to_firm(statement->false_statement);
4717 if (get_cur_block() != NULL) {
4718 ir_node *jmp = new_Jmp();
4719 if (fallthrough_block == NULL)
4720 fallthrough_block = new_immBlock();
4721 add_immBlock_pred(fallthrough_block, jmp);
4725 /* create the condition */
4726 if (cur_block != NULL) {
4727 if (true_block == NULL || false_block == NULL) {
4728 if (fallthrough_block == NULL)
4729 fallthrough_block = new_immBlock();
4730 if (true_block == NULL)
4731 true_block = fallthrough_block;
4732 if (false_block == NULL)
4733 false_block = fallthrough_block;
4736 set_cur_block(cur_block);
4737 create_condition_evaluation(statement->condition, true_block,
4741 mature_immBlock(true_block);
4742 if (false_block != fallthrough_block && false_block != NULL) {
4743 mature_immBlock(false_block);
4745 if (fallthrough_block != NULL) {
4746 mature_immBlock(fallthrough_block);
4749 set_cur_block(fallthrough_block);
4752 static void while_statement_to_firm(while_statement_t *statement)
4754 ir_node *jmp = NULL;
4755 if (get_cur_block() != NULL) {
4759 /* create the header block */
4760 ir_node *header_block = new_immBlock();
4762 add_immBlock_pred(header_block, jmp);
4766 ir_node *old_continue_label = continue_label;
4767 ir_node *old_break_label = break_label;
4768 continue_label = header_block;
4771 ir_node *body_block = new_immBlock();
4772 set_cur_block(body_block);
4773 statement_to_firm(statement->body);
4774 ir_node *false_block = break_label;
4776 assert(continue_label == header_block);
4777 continue_label = old_continue_label;
4778 break_label = old_break_label;
4780 if (get_cur_block() != NULL) {
4782 add_immBlock_pred(header_block, jmp);
4785 /* shortcut for while(true) */
4786 if (is_constant_expression(statement->condition)
4787 && fold_constant_to_bool(statement->condition) != 0) {
4788 set_cur_block(header_block);
4789 ir_node *header_jmp = new_Jmp();
4790 add_immBlock_pred(body_block, header_jmp);
4792 keep_alive(body_block);
4793 keep_all_memory(body_block);
4794 set_cur_block(body_block);
4796 if (false_block == NULL) {
4797 false_block = new_immBlock();
4800 /* create the condition */
4801 set_cur_block(header_block);
4803 create_condition_evaluation(statement->condition, body_block,
4807 mature_immBlock(body_block);
4808 mature_immBlock(header_block);
4809 if (false_block != NULL) {
4810 mature_immBlock(false_block);
4813 set_cur_block(false_block);
4816 static void do_while_statement_to_firm(do_while_statement_t *statement)
4818 ir_node *jmp = NULL;
4819 if (get_cur_block() != NULL) {
4823 /* create the header block */
4824 ir_node *header_block = new_immBlock();
4827 ir_node *body_block = new_immBlock();
4829 add_immBlock_pred(body_block, jmp);
4832 ir_node *old_continue_label = continue_label;
4833 ir_node *old_break_label = break_label;
4834 continue_label = header_block;
4837 set_cur_block(body_block);
4838 statement_to_firm(statement->body);
4839 ir_node *false_block = break_label;
4841 assert(continue_label == header_block);
4842 continue_label = old_continue_label;
4843 break_label = old_break_label;
4845 if (get_cur_block() != NULL) {
4846 ir_node *body_jmp = new_Jmp();
4847 add_immBlock_pred(header_block, body_jmp);
4848 mature_immBlock(header_block);
4851 if (false_block == NULL) {
4852 false_block = new_immBlock();
4855 /* create the condition */
4856 set_cur_block(header_block);
4858 create_condition_evaluation(statement->condition, body_block, false_block);
4859 mature_immBlock(body_block);
4860 mature_immBlock(header_block);
4861 mature_immBlock(false_block);
4863 set_cur_block(false_block);
4866 static void for_statement_to_firm(for_statement_t *statement)
4868 ir_node *jmp = NULL;
4870 /* create declarations */
4871 entity_t *entity = statement->scope.entities;
4872 for ( ; entity != NULL; entity = entity->base.next) {
4873 if (!is_declaration(entity))
4876 create_local_declaration(entity);
4879 if (get_cur_block() != NULL) {
4880 entity = statement->scope.entities;
4881 for ( ; entity != NULL; entity = entity->base.next) {
4882 if (!is_declaration(entity))
4885 initialize_local_declaration(entity);
4888 if (statement->initialisation != NULL) {
4889 expression_to_firm(statement->initialisation);
4896 /* create the step block */
4897 ir_node *const step_block = new_immBlock();
4898 set_cur_block(step_block);
4899 if (statement->step != NULL) {
4900 expression_to_firm(statement->step);
4902 ir_node *const step_jmp = new_Jmp();
4904 /* create the header block */
4905 ir_node *const header_block = new_immBlock();
4906 set_cur_block(header_block);
4908 add_immBlock_pred(header_block, jmp);
4910 add_immBlock_pred(header_block, step_jmp);
4912 /* the false block */
4913 ir_node *const false_block = new_immBlock();
4916 ir_node *body_block;
4917 if (statement->body != NULL) {
4918 ir_node *const old_continue_label = continue_label;
4919 ir_node *const old_break_label = break_label;
4920 continue_label = step_block;
4921 break_label = false_block;
4923 body_block = new_immBlock();
4924 set_cur_block(body_block);
4925 statement_to_firm(statement->body);
4927 assert(continue_label == step_block);
4928 assert(break_label == false_block);
4929 continue_label = old_continue_label;
4930 break_label = old_break_label;
4932 if (get_cur_block() != NULL) {
4934 add_immBlock_pred(step_block, jmp);
4937 body_block = step_block;
4940 /* create the condition */
4941 set_cur_block(header_block);
4942 if (statement->condition != NULL) {
4943 create_condition_evaluation(statement->condition, body_block,
4946 keep_alive(header_block);
4947 keep_all_memory(header_block);
4949 add_immBlock_pred(body_block, jmp);
4952 mature_immBlock(body_block);
4953 mature_immBlock(false_block);
4954 mature_immBlock(step_block);
4955 mature_immBlock(header_block);
4956 mature_immBlock(false_block);
4958 set_cur_block(false_block);
4961 static void create_jump_statement(const statement_t *statement,
4962 ir_node *target_block)
4964 if (get_cur_block() == NULL)
4967 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4968 ir_node *jump = new_d_Jmp(dbgi);
4969 add_immBlock_pred(target_block, jump);
4971 set_cur_block(NULL);
4974 static ir_node *get_break_label(void)
4976 if (break_label == NULL) {
4977 break_label = new_immBlock();
4982 static void switch_statement_to_firm(switch_statement_t *statement)
4984 ir_node *first_block = NULL;
4985 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4986 ir_node *cond = NULL;
4988 if (get_cur_block() != NULL) {
4989 ir_node *expression = expression_to_firm(statement->expression);
4990 cond = new_d_Cond(dbgi, expression);
4991 first_block = get_cur_block();
4994 set_cur_block(NULL);
4996 ir_node *const old_switch_cond = current_switch_cond;
4997 ir_node *const old_break_label = break_label;
4998 const bool old_saw_default_label = saw_default_label;
4999 saw_default_label = false;
5000 current_switch_cond = cond;
5002 switch_statement_t *const old_switch = current_switch;
5003 current_switch = statement;
5005 /* determine a free number for the default label */
5006 unsigned long num_cases = 0;
5008 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5009 if (l->expression == NULL) {
5013 if (l->last_case >= l->first_case)
5014 num_cases += l->last_case - l->first_case + 1;
5015 if (l->last_case > def_nr)
5016 def_nr = l->last_case;
5019 if (def_nr == INT_MAX) {
5020 /* Bad: an overflow will occur, we cannot be sure that the
5021 * maximum + 1 is a free number. Scan the values a second
5022 * time to find a free number.
5024 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5026 memset(bits, 0, (num_cases + 7) >> 3);
5027 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5028 if (l->expression == NULL) {
5032 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5033 if (start < num_cases && l->last_case >= 0) {
5034 unsigned long end = (unsigned long)l->last_case < num_cases ?
5035 (unsigned long)l->last_case : num_cases - 1;
5036 for (unsigned long cns = start; cns <= end; ++cns) {
5037 bits[cns >> 3] |= (1 << (cns & 7));
5041 /* We look at the first num_cases constants:
5042 * Either they are dense, so we took the last (num_cases)
5043 * one, or they are not dense, so we will find one free
5047 for (i = 0; i < num_cases; ++i)
5048 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5056 statement->default_proj_nr = def_nr;
5058 if (statement->body != NULL) {
5059 statement_to_firm(statement->body);
5062 if (get_cur_block() != NULL) {
5063 ir_node *jmp = new_Jmp();
5064 add_immBlock_pred(get_break_label(), jmp);
5067 if (!saw_default_label && first_block != NULL) {
5068 set_cur_block(first_block);
5069 ir_node *const proj = new_d_defaultProj(dbgi, cond,
5070 statement->default_proj_nr);
5071 add_immBlock_pred(get_break_label(), proj);
5074 if (break_label != NULL) {
5075 mature_immBlock(break_label);
5077 set_cur_block(break_label);
5079 assert(current_switch_cond == cond);
5080 current_switch = old_switch;
5081 current_switch_cond = old_switch_cond;
5082 break_label = old_break_label;
5083 saw_default_label = old_saw_default_label;
5086 static void case_label_to_firm(const case_label_statement_t *statement)
5088 if (statement->is_empty_range)
5091 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5093 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
5096 ir_node *block = new_immBlock();
5098 if (current_switch_cond != NULL) {
5099 set_cur_block(get_nodes_block(current_switch_cond));
5100 if (statement->expression != NULL) {
5101 long pn = statement->first_case;
5102 long end_pn = statement->last_case;
5103 assert(pn <= end_pn);
5104 /* create jumps for all cases in the given range */
5106 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5107 add_immBlock_pred(block, proj);
5108 } while (pn++ < end_pn);
5110 saw_default_label = true;
5111 proj = new_d_defaultProj(dbgi, current_switch_cond,
5112 current_switch->default_proj_nr);
5114 add_immBlock_pred(block, proj);
5118 if (fallthrough != NULL) {
5119 add_immBlock_pred(block, fallthrough);
5121 mature_immBlock(block);
5122 set_cur_block(block);
5124 if (statement->statement != NULL) {
5125 statement_to_firm(statement->statement);
5129 static void label_to_firm(const label_statement_t *statement)
5131 ir_node *block = get_label_block(statement->label);
5133 if (get_cur_block() != NULL) {
5134 ir_node *jmp = new_Jmp();
5135 add_immBlock_pred(block, jmp);
5138 set_cur_block(block);
5140 keep_all_memory(block);
5142 if (statement->statement != NULL) {
5143 statement_to_firm(statement->statement);
5147 static void goto_to_firm(const goto_statement_t *statement)
5149 if (get_cur_block() == NULL)
5152 if (statement->expression) {
5153 ir_node *irn = expression_to_firm(statement->expression);
5154 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5155 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5157 set_irn_link(ijmp, ijmp_list);
5160 ir_node *block = get_label_block(statement->label);
5161 ir_node *jmp = new_Jmp();
5162 add_immBlock_pred(block, jmp);
5164 set_cur_block(NULL);
5167 static void asm_statement_to_firm(const asm_statement_t *statement)
5169 bool needs_memory = false;
5171 if (statement->is_volatile) {
5172 needs_memory = true;
5175 size_t n_clobbers = 0;
5176 asm_clobber_t *clobber = statement->clobbers;
5177 for ( ; clobber != NULL; clobber = clobber->next) {
5178 const char *clobber_str = clobber->clobber.begin;
5180 if (!be_is_valid_clobber(clobber_str)) {
5181 errorf(&statement->base.source_position,
5182 "invalid clobber '%s' specified", clobber->clobber);
5186 if (strcmp(clobber_str, "memory") == 0) {
5187 needs_memory = true;
5191 ident *id = new_id_from_str(clobber_str);
5192 obstack_ptr_grow(&asm_obst, id);
5195 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5196 ident **clobbers = NULL;
5197 if (n_clobbers > 0) {
5198 clobbers = obstack_finish(&asm_obst);
5201 size_t n_inputs = 0;
5202 asm_argument_t *argument = statement->inputs;
5203 for ( ; argument != NULL; argument = argument->next)
5205 size_t n_outputs = 0;
5206 argument = statement->outputs;
5207 for ( ; argument != NULL; argument = argument->next)
5210 unsigned next_pos = 0;
5212 ir_node *ins[n_inputs + n_outputs + 1];
5215 ir_asm_constraint tmp_in_constraints[n_outputs];
5217 const expression_t *out_exprs[n_outputs];
5218 ir_node *out_addrs[n_outputs];
5219 size_t out_size = 0;
5221 argument = statement->outputs;
5222 for ( ; argument != NULL; argument = argument->next) {
5223 const char *constraints = argument->constraints.begin;
5224 asm_constraint_flags_t asm_flags
5225 = be_parse_asm_constraints(constraints);
5227 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5228 warningf(&statement->base.source_position,
5229 "some constraints in '%s' are not supported", constraints);
5231 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5232 errorf(&statement->base.source_position,
5233 "some constraints in '%s' are invalid", constraints);
5236 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5237 errorf(&statement->base.source_position,
5238 "no write flag specified for output constraints '%s'",
5243 unsigned pos = next_pos++;
5244 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5245 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5246 expression_t *expr = argument->expression;
5247 ir_node *addr = expression_to_addr(expr);
5248 /* in+output, construct an artifical same_as constraint on the
5250 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5252 ir_node *value = get_value_from_lvalue(expr, addr);
5254 snprintf(buf, sizeof(buf), "%u", (unsigned) out_size);
5256 ir_asm_constraint constraint;
5257 constraint.pos = pos;
5258 constraint.constraint = new_id_from_str(buf);
5259 constraint.mode = get_ir_mode_storage(expr->base.type);
5260 tmp_in_constraints[in_size] = constraint;
5261 ins[in_size] = value;
5266 out_exprs[out_size] = expr;
5267 out_addrs[out_size] = addr;
5269 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5270 /* pure memory ops need no input (but we have to make sure we
5271 * attach to the memory) */
5272 assert(! (asm_flags &
5273 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5274 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5275 needs_memory = true;
5277 /* we need to attach the address to the inputs */
5278 expression_t *expr = argument->expression;
5280 ir_asm_constraint constraint;
5281 constraint.pos = pos;
5282 constraint.constraint = new_id_from_str(constraints);
5283 constraint.mode = NULL;
5284 tmp_in_constraints[in_size] = constraint;
5286 ins[in_size] = expression_to_addr(expr);
5290 errorf(&statement->base.source_position,
5291 "only modifiers but no place set in constraints '%s'",
5296 ir_asm_constraint constraint;
5297 constraint.pos = pos;
5298 constraint.constraint = new_id_from_str(constraints);
5299 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5301 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5303 assert(obstack_object_size(&asm_obst)
5304 == out_size * sizeof(ir_asm_constraint));
5305 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5308 obstack_grow(&asm_obst, tmp_in_constraints,
5309 in_size * sizeof(tmp_in_constraints[0]));
5310 /* find and count input and output arguments */
5311 argument = statement->inputs;
5312 for ( ; argument != NULL; argument = argument->next) {
5313 const char *constraints = argument->constraints.begin;
5314 asm_constraint_flags_t asm_flags
5315 = be_parse_asm_constraints(constraints);
5317 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5318 errorf(&statement->base.source_position,
5319 "some constraints in '%s' are not supported", constraints);
5322 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5323 errorf(&statement->base.source_position,
5324 "some constraints in '%s' are invalid", constraints);
5327 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5328 errorf(&statement->base.source_position,
5329 "write flag specified for input constraints '%s'",
5335 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5336 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5337 /* we can treat this as "normal" input */
5338 input = expression_to_firm(argument->expression);
5339 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5340 /* pure memory ops need no input (but we have to make sure we
5341 * attach to the memory) */
5342 assert(! (asm_flags &
5343 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5344 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5345 needs_memory = true;
5346 input = expression_to_addr(argument->expression);
5348 errorf(&statement->base.source_position,
5349 "only modifiers but no place set in constraints '%s'",
5354 ir_asm_constraint constraint;
5355 constraint.pos = next_pos++;
5356 constraint.constraint = new_id_from_str(constraints);
5357 constraint.mode = get_irn_mode(input);
5359 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5360 ins[in_size++] = input;
5364 ir_asm_constraint constraint;
5365 constraint.pos = next_pos++;
5366 constraint.constraint = new_id_from_str("");
5367 constraint.mode = mode_M;
5369 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5370 ins[in_size++] = get_store();
5373 assert(obstack_object_size(&asm_obst)
5374 == in_size * sizeof(ir_asm_constraint));
5375 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5377 /* create asm node */
5378 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5380 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5382 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5383 out_size, output_constraints,
5384 n_clobbers, clobbers, asm_text);
5386 if (statement->is_volatile) {
5387 set_irn_pinned(node, op_pin_state_pinned);
5389 set_irn_pinned(node, op_pin_state_floats);
5392 /* create output projs & connect them */
5394 ir_node *projm = new_Proj(node, mode_M, out_size+1);
5399 for (i = 0; i < out_size; ++i) {
5400 const expression_t *out_expr = out_exprs[i];
5402 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5403 ir_node *proj = new_Proj(node, mode, pn);
5404 ir_node *addr = out_addrs[i];
5406 set_value_for_expression_addr(out_expr, proj, addr);
5410 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5412 statement_to_firm(statement->try_statement);
5413 warningf(&statement->base.source_position, "structured exception handling ignored");
5416 static void leave_statement_to_firm(leave_statement_t *statement)
5418 errorf(&statement->base.source_position, "__leave not supported yet");
5422 * Transform a statement.
5424 static void statement_to_firm(statement_t *statement)
5427 assert(!statement->base.transformed);
5428 statement->base.transformed = true;
5431 switch (statement->kind) {
5432 case STATEMENT_INVALID:
5433 panic("invalid statement found");
5434 case STATEMENT_EMPTY:
5437 case STATEMENT_COMPOUND:
5438 compound_statement_to_firm(&statement->compound);
5440 case STATEMENT_RETURN:
5441 return_statement_to_firm(&statement->returns);
5443 case STATEMENT_EXPRESSION:
5444 expression_statement_to_firm(&statement->expression);
5447 if_statement_to_firm(&statement->ifs);
5449 case STATEMENT_WHILE:
5450 while_statement_to_firm(&statement->whiles);
5452 case STATEMENT_DO_WHILE:
5453 do_while_statement_to_firm(&statement->do_while);
5455 case STATEMENT_DECLARATION:
5456 declaration_statement_to_firm(&statement->declaration);
5458 case STATEMENT_BREAK:
5459 create_jump_statement(statement, get_break_label());
5461 case STATEMENT_CONTINUE:
5462 create_jump_statement(statement, continue_label);
5464 case STATEMENT_SWITCH:
5465 switch_statement_to_firm(&statement->switchs);
5467 case STATEMENT_CASE_LABEL:
5468 case_label_to_firm(&statement->case_label);
5471 for_statement_to_firm(&statement->fors);
5473 case STATEMENT_LABEL:
5474 label_to_firm(&statement->label);
5476 case STATEMENT_GOTO:
5477 goto_to_firm(&statement->gotos);
5480 asm_statement_to_firm(&statement->asms);
5482 case STATEMENT_MS_TRY:
5483 ms_try_statement_to_firm(&statement->ms_try);
5485 case STATEMENT_LEAVE:
5486 leave_statement_to_firm(&statement->leave);
5489 panic("statement not implemented");
5492 static int count_local_variables(const entity_t *entity,
5493 const entity_t *const last)
5496 entity_t const *const end = last != NULL ? last->base.next : NULL;
5497 for (; entity != end; entity = entity->base.next) {
5501 if (entity->kind == ENTITY_VARIABLE) {
5502 type = skip_typeref(entity->declaration.type);
5503 address_taken = entity->variable.address_taken;
5504 } else if (entity->kind == ENTITY_PARAMETER) {
5505 type = skip_typeref(entity->declaration.type);
5506 address_taken = entity->parameter.address_taken;
5511 if (!address_taken && is_type_scalar(type))
5517 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5519 int *const count = env;
5521 switch (stmt->kind) {
5522 case STATEMENT_DECLARATION: {
5523 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5524 *count += count_local_variables(decl_stmt->declarations_begin,
5525 decl_stmt->declarations_end);
5530 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5539 * Return the number of local (alias free) variables used by a function.
5541 static int get_function_n_local_vars(entity_t *entity)
5543 const function_t *function = &entity->function;
5546 /* count parameters */
5547 count += count_local_variables(function->parameters.entities, NULL);
5549 /* count local variables declared in body */
5550 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5555 * Build Firm code for the parameters of a function.
5557 static void initialize_function_parameters(entity_t *entity)
5559 assert(entity->kind == ENTITY_FUNCTION);
5560 ir_graph *irg = current_ir_graph;
5561 ir_node *args = get_irg_args(irg);
5562 ir_node *start_block = get_irg_start_block(irg);
5563 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5564 int first_param_nr = 0;
5566 if (entity->function.need_closure) {
5567 /* add an extra parameter for the static link */
5568 entity->function.static_link = new_r_Proj(start_block, args, mode_P_data, 0);
5573 entity_t *parameter = entity->function.parameters.entities;
5574 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5575 if (parameter->kind != ENTITY_PARAMETER)
5578 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5579 type_t *type = skip_typeref(parameter->declaration.type);
5581 bool needs_entity = parameter->parameter.address_taken;
5582 assert(!is_type_array(type));
5583 if (is_type_compound(type)) {
5584 needs_entity = true;
5588 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5589 ident *id = new_id_from_str(parameter->base.symbol->string);
5590 set_entity_ident(entity, id);
5592 parameter->declaration.kind
5593 = DECLARATION_KIND_PARAMETER_ENTITY;
5594 parameter->parameter.v.entity = entity;
5598 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5599 ir_mode *param_mode = get_type_mode(param_irtype);
5601 long pn = n + first_param_nr;
5602 ir_node *value = new_r_Proj(start_block, args, param_mode, pn);
5604 ir_mode *mode = get_ir_mode_storage(type);
5605 value = create_conv(NULL, value, mode);
5606 value = do_strict_conv(NULL, value);
5608 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5609 parameter->parameter.v.value_number = next_value_number_function;
5610 set_irg_loc_description(current_ir_graph, next_value_number_function,
5612 ++next_value_number_function;
5614 set_value(parameter->parameter.v.value_number, value);
5619 * Handle additional decl modifiers for IR-graphs
5621 * @param irg the IR-graph
5622 * @param dec_modifiers additional modifiers
5624 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5625 decl_modifiers_t decl_modifiers)
5627 if (decl_modifiers & DM_RETURNS_TWICE) {
5628 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5629 set_irg_additional_property(irg, mtp_property_returns_twice);
5631 if (decl_modifiers & DM_NORETURN) {
5632 /* TRUE if the declaration includes the Microsoft
5633 __declspec(noreturn) specifier. */
5634 set_irg_additional_property(irg, mtp_property_noreturn);
5636 if (decl_modifiers & DM_NOTHROW) {
5637 /* TRUE if the declaration includes the Microsoft
5638 __declspec(nothrow) specifier. */
5639 set_irg_additional_property(irg, mtp_property_nothrow);
5641 if (decl_modifiers & DM_NAKED) {
5642 /* TRUE if the declaration includes the Microsoft
5643 __declspec(naked) specifier. */
5644 set_irg_additional_property(irg, mtp_property_naked);
5646 if (decl_modifiers & DM_FORCEINLINE) {
5647 /* TRUE if the declaration includes the
5648 Microsoft __forceinline specifier. */
5649 set_irg_inline_property(irg, irg_inline_forced);
5651 if (decl_modifiers & DM_NOINLINE) {
5652 /* TRUE if the declaration includes the Microsoft
5653 __declspec(noinline) specifier. */
5654 set_irg_inline_property(irg, irg_inline_forbidden);
5658 static void add_function_pointer(ir_type *segment, ir_entity *method,
5659 const char *unique_template)
5661 ir_type *method_type = get_entity_type(method);
5662 ir_type *ptr_type = new_type_pointer(method_type);
5664 /* these entities don't really have a name but firm only allows
5666 * Note that we mustn't give these entities a name since for example
5667 * Mach-O doesn't allow them. */
5668 ident *ide = id_unique(unique_template);
5669 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5670 ir_graph *irg = get_const_code_irg();
5671 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5674 set_entity_ld_ident(ptr, new_id_from_chars("", 0));
5675 set_entity_compiler_generated(ptr, 1);
5676 set_entity_visibility(ptr, ir_visibility_local);
5677 add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER);
5678 set_atomic_ent_value(ptr, val);
5682 * Generate possible IJmp branches to a given label block.
5684 static void gen_ijmp_branches(ir_node *block)
5687 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5688 add_immBlock_pred(block, ijmp);
5693 * Create code for a function and all inner functions.
5695 * @param entity the function entity
5697 static void create_function(entity_t *entity)
5699 assert(entity->kind == ENTITY_FUNCTION);
5700 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5702 if (entity->function.statement == NULL)
5705 if (is_main(entity) && firm_opt.os_support == OS_SUPPORT_MINGW) {
5706 prepare_main_collect2(entity);
5709 inner_functions = NULL;
5710 current_trampolines = NULL;
5712 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5713 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5714 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5716 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5717 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5718 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5721 current_function_entity = entity;
5722 current_function_name = NULL;
5723 current_funcsig = NULL;
5725 assert(all_labels == NULL);
5726 all_labels = NEW_ARR_F(label_t *, 0);
5729 int n_local_vars = get_function_n_local_vars(entity);
5730 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5732 ir_graph *old_current_function = current_function;
5733 current_function = irg;
5735 set_irg_fp_model(irg, firm_opt.fp_model);
5736 tarval_enable_fp_ops(1);
5737 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5739 ir_node *first_block = get_cur_block();
5741 /* set inline flags */
5742 if (entity->function.is_inline)
5743 set_irg_inline_property(irg, irg_inline_recomended);
5744 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5746 next_value_number_function = 0;
5747 initialize_function_parameters(entity);
5748 current_static_link = entity->function.static_link;
5750 statement_to_firm(entity->function.statement);
5752 ir_node *end_block = get_irg_end_block(irg);
5754 /* do we have a return statement yet? */
5755 if (get_cur_block() != NULL) {
5756 type_t *type = skip_typeref(entity->declaration.type);
5757 assert(is_type_function(type));
5758 const function_type_t *func_type = &type->function;
5759 const type_t *return_type
5760 = skip_typeref(func_type->return_type);
5763 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5764 ret = new_Return(get_store(), 0, NULL);
5767 if (is_type_scalar(return_type)) {
5768 mode = get_ir_mode_storage(func_type->return_type);
5774 /* §5.1.2.2.3 main implicitly returns 0 */
5775 if (is_main(entity)) {
5776 in[0] = new_Const(get_mode_null(mode));
5778 in[0] = new_Unknown(mode);
5780 ret = new_Return(get_store(), 1, in);
5782 add_immBlock_pred(end_block, ret);
5785 bool has_computed_gotos = false;
5786 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5787 label_t *label = all_labels[i];
5788 if (label->address_taken) {
5789 gen_ijmp_branches(label->block);
5790 has_computed_gotos = true;
5792 mature_immBlock(label->block);
5794 if (has_computed_gotos) {
5795 /* if we have computed goto's in the function, we cannot inline it */
5796 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5797 warningf(&entity->base.source_position,
5798 "function '%Y' can never be inlined because it contains a computed goto",
5799 entity->base.symbol);
5801 set_irg_inline_property(irg, irg_inline_forbidden);
5804 DEL_ARR_F(all_labels);
5807 mature_immBlock(first_block);
5808 mature_immBlock(end_block);
5810 irg_finalize_cons(irg);
5812 /* finalize the frame type */
5813 ir_type *frame_type = get_irg_frame_type(irg);
5814 int n = get_compound_n_members(frame_type);
5817 for (int i = 0; i < n; ++i) {
5818 ir_entity *entity = get_compound_member(frame_type, i);
5819 ir_type *entity_type = get_entity_type(entity);
5821 int align = get_type_alignment_bytes(entity_type);
5822 if (align > align_all)
5826 misalign = offset % align;
5828 offset += align - misalign;
5832 set_entity_offset(entity, offset);
5833 offset += get_type_size_bytes(entity_type);
5835 set_type_size_bytes(frame_type, offset);
5836 set_type_alignment_bytes(frame_type, align_all);
5839 current_function = old_current_function;
5841 if (current_trampolines != NULL) {
5842 DEL_ARR_F(current_trampolines);
5843 current_trampolines = NULL;
5846 /* create inner functions if any */
5847 entity_t **inner = inner_functions;
5848 if (inner != NULL) {
5849 ir_type *rem_outer_frame = current_outer_frame;
5850 current_outer_frame = get_irg_frame_type(current_ir_graph);
5851 ir_type *rem_outer_value_type = current_outer_value_type;
5852 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5853 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5854 create_function(inner[i]);
5858 current_outer_value_type = rem_outer_value_type;
5859 current_outer_frame = rem_outer_frame;
5863 static void scope_to_firm(scope_t *scope)
5865 /* first pass: create declarations */
5866 entity_t *entity = scope->entities;
5867 for ( ; entity != NULL; entity = entity->base.next) {
5868 if (entity->base.symbol == NULL)
5871 if (entity->kind == ENTITY_FUNCTION) {
5872 if (entity->function.btk != bk_none) {
5873 /* builtins have no representation */
5876 (void)get_function_entity(entity, NULL);
5877 } else if (entity->kind == ENTITY_VARIABLE) {
5878 create_global_variable(entity);
5879 } else if (entity->kind == ENTITY_NAMESPACE) {
5880 scope_to_firm(&entity->namespacee.members);
5884 /* second pass: create code/initializers */
5885 entity = scope->entities;
5886 for ( ; entity != NULL; entity = entity->base.next) {
5887 if (entity->base.symbol == NULL)
5890 if (entity->kind == ENTITY_FUNCTION) {
5891 if (entity->function.btk != bk_none) {
5892 /* builtins have no representation */
5895 create_function(entity);
5896 } else if (entity->kind == ENTITY_VARIABLE) {
5897 assert(entity->declaration.kind
5898 == DECLARATION_KIND_GLOBAL_VARIABLE);
5899 current_ir_graph = get_const_code_irg();
5900 create_variable_initializer(entity);
5905 void init_ast2firm(void)
5907 obstack_init(&asm_obst);
5908 init_atomic_modes();
5910 ir_set_debug_retrieve(dbg_retrieve);
5911 ir_set_type_debug_retrieve(dbg_print_type_dbg_info);
5913 /* OS option must be set to the backend */
5914 switch (firm_opt.os_support) {
5915 case OS_SUPPORT_MINGW:
5916 create_ld_ident = create_name_win32;
5918 case OS_SUPPORT_LINUX:
5919 create_ld_ident = create_name_linux_elf;
5921 case OS_SUPPORT_MACHO:
5922 create_ld_ident = create_name_macho;
5925 panic("unexpected OS support mode");
5928 /* create idents for all known runtime functions */
5929 for (size_t i = 0; i < lengthof(rts_data); ++i) {
5930 rts_idents[i] = new_id_from_str(rts_data[i].name);
5933 entitymap_init(&entitymap);
5936 static void init_ir_types(void)
5938 static int ir_types_initialized = 0;
5939 if (ir_types_initialized)
5941 ir_types_initialized = 1;
5943 ir_type_int = get_ir_type(type_int);
5944 ir_type_char = get_ir_type(type_char);
5945 ir_type_const_char = get_ir_type(type_const_char);
5946 ir_type_wchar_t = get_ir_type(type_wchar_t);
5947 ir_type_void = get_ir_type(type_void);
5949 be_params = be_get_backend_param();
5950 mode_float_arithmetic = be_params->mode_float_arithmetic;
5952 stack_param_align = be_params->stack_param_align;
5955 void exit_ast2firm(void)
5957 entitymap_destroy(&entitymap);
5958 obstack_free(&asm_obst, NULL);
5961 static void global_asm_to_firm(statement_t *s)
5963 for (; s != NULL; s = s->base.next) {
5964 assert(s->kind == STATEMENT_ASM);
5966 char const *const text = s->asms.asm_text.begin;
5967 size_t size = s->asms.asm_text.size;
5969 /* skip the last \0 */
5970 if (text[size - 1] == '\0')
5973 ident *const id = new_id_from_chars(text, size);
5978 void translation_unit_to_firm(translation_unit_t *unit)
5980 /* just to be sure */
5981 continue_label = NULL;
5983 current_switch_cond = NULL;
5984 current_translation_unit = unit;
5988 scope_to_firm(&unit->scope);
5989 global_asm_to_firm(unit->global_asm);
5991 current_ir_graph = NULL;
5992 current_translation_unit = NULL;