2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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"
49 #include "entitymap_t.h"
50 #include "driver/firm_opt.h"
51 #include "driver/firm_cmdline.h"
53 typedef struct trampoline_region trampoline_region;
54 struct trampoline_region {
55 ir_entity *function; /**< The function that is called by this trampoline */
56 ir_entity *region; /**< created region for the trampoline */
59 static const backend_params *be_params;
61 static ir_type *ir_type_char;
62 static ir_type *ir_type_const_char;
63 static ir_type *ir_type_wchar_t;
64 static ir_type *ir_type_void;
65 static ir_type *ir_type_int;
67 /* architecture specific floating point arithmetic mode (if any) */
68 static ir_mode *mode_float_arithmetic;
70 /* alignment of stack parameters */
71 static unsigned stack_param_align;
73 static int next_value_number_function;
74 static ir_node *continue_label;
75 static ir_node *break_label;
76 static ir_node *current_switch_cond;
77 static bool saw_default_label;
78 static label_t **all_labels;
79 static entity_t **inner_functions;
80 static ir_node *ijmp_list;
81 static bool constant_folding;
83 extern bool have_const_functions;
85 static const entity_t *current_function_entity;
86 static ir_node *current_function_name;
87 static ir_node *current_funcsig;
88 static switch_statement_t *current_switch;
89 static ir_graph *current_function;
90 static translation_unit_t *current_translation_unit;
91 static trampoline_region *current_trampolines;
92 static ir_type *current_outer_frame;
93 static ir_type *current_outer_value_type;
94 static ir_node *current_static_link;
96 static entitymap_t entitymap;
98 static struct obstack asm_obst;
100 typedef enum declaration_kind_t {
101 DECLARATION_KIND_UNKNOWN,
102 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
103 DECLARATION_KIND_GLOBAL_VARIABLE,
104 DECLARATION_KIND_LOCAL_VARIABLE,
105 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
106 DECLARATION_KIND_PARAMETER,
107 DECLARATION_KIND_PARAMETER_ENTITY,
108 DECLARATION_KIND_FUNCTION,
109 DECLARATION_KIND_COMPOUND_MEMBER,
110 DECLARATION_KIND_INNER_FUNCTION
111 } declaration_kind_t;
113 static ir_mode *get_ir_mode_storage(type_t *type);
115 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
116 * int that it returns bigger modes for floating point on some platforms
117 * (x87 internally does arithemtic with 80bits)
119 static ir_mode *get_ir_mode_arithmetic(type_t *type);
121 static ir_type *get_ir_type_incomplete(type_t *type);
123 static void enqueue_inner_function(entity_t *entity)
125 if (inner_functions == NULL)
126 inner_functions = NEW_ARR_F(entity_t *, 0);
127 ARR_APP1(entity_t*, inner_functions, entity);
130 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
132 const entity_t *entity = get_irg_loc_description(irg, pos);
134 if (entity != NULL) {
135 warningf(&entity->base.source_position,
136 "%s '%#T' might be used uninitialized",
137 get_entity_kind_name(entity->kind),
138 entity->declaration.type, entity->base.symbol);
140 return new_r_Unknown(irg, mode);
143 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
145 const source_position_t *pos = (const source_position_t*) dbg;
148 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
152 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
154 const source_position_t *pos = (const source_position_t*) dbg;
159 return pos->input_name;
162 static dbg_info *get_dbg_info(const source_position_t *pos)
164 return (dbg_info*) pos;
167 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
169 static ir_mode *mode_int, *mode_uint;
171 static ir_node *_expression_to_firm(const expression_t *expression);
172 static ir_node *expression_to_firm(const expression_t *expression);
173 static void create_local_declaration(entity_t *entity);
175 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
177 unsigned flags = get_atomic_type_flags(kind);
178 unsigned size = get_atomic_type_size(kind);
179 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
180 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
183 unsigned bit_size = size * 8;
184 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
185 unsigned modulo_shift;
186 ir_mode_arithmetic arithmetic;
188 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
189 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
190 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
192 sort = irms_int_number;
193 arithmetic = irma_twos_complement;
194 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
196 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
197 snprintf(name, sizeof(name), "F%u", bit_size);
198 sort = irms_float_number;
199 arithmetic = irma_ieee754;
202 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
210 * Initialises the atomic modes depending on the machine size.
212 static void init_atomic_modes(void)
214 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
215 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
217 mode_int = atomic_modes[ATOMIC_TYPE_INT];
218 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
220 /* there's no real void type in firm */
221 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
223 /* initialize pointer modes */
225 ir_mode_sort sort = irms_reference;
226 unsigned bit_size = machine_size;
228 ir_mode_arithmetic arithmetic = irma_twos_complement;
229 unsigned modulo_shift
230 = bit_size < machine_size ? machine_size : bit_size;
232 snprintf(name, sizeof(name), "p%u", machine_size);
233 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
236 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
237 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
239 /* Hmm, pointers should be machine size */
240 set_modeP_data(ptr_mode);
241 set_modeP_code(ptr_mode);
244 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
246 assert(kind <= ATOMIC_TYPE_LAST);
247 return atomic_modes[kind];
250 static ir_node *get_vla_size(array_type_t *const type)
252 ir_node *size_node = type->size_node;
253 if (size_node == NULL) {
254 size_node = expression_to_firm(type->size_expression);
255 type->size_node = size_node;
261 * Return a node representing the size of a type.
263 static ir_node *get_type_size_node(type_t *type)
265 type = skip_typeref(type);
267 if (is_type_array(type) && type->array.is_vla) {
268 ir_node *size_node = get_vla_size(&type->array);
269 ir_node *elem_size = get_type_size_node(type->array.element_type);
270 ir_mode *mode = get_irn_mode(size_node);
271 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
275 ir_mode *mode = get_ir_mode_storage(type_size_t);
277 sym.type_p = get_ir_type(type);
278 return new_SymConst(mode, sym, symconst_type_size);
281 static unsigned count_parameters(const function_type_t *function_type)
285 function_parameter_t *parameter = function_type->parameters;
286 for ( ; parameter != NULL; parameter = parameter->next) {
294 * Creates a Firm type for an atomic type
296 static ir_type *create_atomic_type(atomic_type_kind_t akind)
298 ir_mode *mode = atomic_modes[akind];
299 ident *id = get_mode_ident(mode);
300 ir_type *irtype = new_type_primitive(id, mode);
301 il_alignment_t alignment = get_atomic_type_alignment(akind);
303 set_type_alignment_bytes(irtype, alignment);
309 * Creates a Firm type for a complex type
311 static ir_type *create_complex_type(const complex_type_t *type)
313 atomic_type_kind_t kind = type->akind;
314 ir_mode *mode = atomic_modes[kind];
315 ident *id = get_mode_ident(mode);
319 /* FIXME: finish the array */
324 * Creates a Firm type for an imaginary type
326 static ir_type *create_imaginary_type(imaginary_type_t *type)
328 atomic_type_kind_t kind = type->akind;
329 ir_mode *mode = atomic_modes[kind];
330 ident *id = get_mode_ident(mode);
331 ir_type *irtype = new_type_primitive(id, mode);
332 il_alignment_t alignment = get_type_alignment((type_t*) type);
334 set_type_alignment_bytes(irtype, alignment);
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 ident *id = id_unique("functiontype.%u");
360 int n_parameters = count_parameters(function_type) + (for_closure ? 1 : 0);
361 int n_results = return_type == type_void ? 0 : 1;
362 ir_type *irtype = new_type_method(id, n_parameters, n_results);
364 if (return_type != type_void) {
365 ir_type *restype = get_ir_type(return_type);
366 set_method_res_type(irtype, 0, restype);
369 function_parameter_t *parameter = function_type->parameters;
372 ir_type *p_irtype = get_ir_type(type_void_ptr);
373 set_method_param_type(irtype, n, p_irtype);
376 for ( ; parameter != NULL; parameter = parameter->next) {
377 type_t *type = get_parameter_type(parameter->type);
378 ir_type *p_irtype = get_ir_type(type);
379 set_method_param_type(irtype, n, p_irtype);
383 if (function_type->variadic || function_type->unspecified_parameters) {
384 set_method_variadicity(irtype, variadicity_variadic);
387 unsigned cc = get_method_calling_convention(irtype);
388 switch (function_type->calling_convention) {
389 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
392 set_method_calling_convention(irtype, SET_CDECL(cc));
396 if (function_type->variadic || function_type->unspecified_parameters)
399 /* only non-variadic function can use stdcall, else use cdecl */
400 set_method_calling_convention(irtype, SET_STDCALL(cc));
404 if (function_type->variadic || function_type->unspecified_parameters)
406 /* only non-variadic function can use fastcall, else use cdecl */
407 set_method_calling_convention(irtype, SET_FASTCALL(cc));
411 /* Hmm, leave default, not accepted by the parser yet. */
416 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
421 static ir_type *create_pointer_type(pointer_type_t *type)
423 type_t *points_to = type->points_to;
424 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
425 ir_type *ir_type = new_type_pointer(id_unique("pointer.%u"),
426 ir_points_to, mode_P_data);
431 static ir_type *create_reference_type(reference_type_t *type)
433 type_t *refers_to = type->refers_to;
434 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
435 ir_type *ir_type = new_type_pointer(id_unique("reference.%u"),
436 ir_refers_to, mode_P_data);
441 static ir_type *create_array_type(array_type_t *type)
443 type_t *element_type = type->element_type;
444 ir_type *ir_element_type = get_ir_type(element_type);
446 ident *id = id_unique("array.%u");
447 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
449 const int align = get_type_alignment_bytes(ir_element_type);
450 set_type_alignment_bytes(ir_type, align);
452 if (type->size_constant) {
453 int n_elements = type->size;
455 set_array_bounds_int(ir_type, 0, 0, n_elements);
457 size_t elemsize = get_type_size_bytes(ir_element_type);
458 if (elemsize % align > 0) {
459 elemsize += align - (elemsize % align);
461 set_type_size_bytes(ir_type, n_elements * elemsize);
463 set_array_lower_bound_int(ir_type, 0, 0);
465 set_type_state(ir_type, layout_fixed);
471 * Return the signed integer type of size bits.
473 * @param size the size
475 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
478 static ir_mode *s_modes[64 + 1] = {NULL, };
482 if (size <= 0 || size > 64)
485 mode = s_modes[size];
489 snprintf(name, sizeof(name), "bf_I%u", size);
490 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
491 size <= 32 ? 32 : size );
492 s_modes[size] = mode;
496 snprintf(name, sizeof(name), "I%u", size);
497 ident *id = new_id_from_str(name);
498 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
499 res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
500 set_primitive_base_type(res, base_tp);
506 * Return the unsigned integer type of size bits.
508 * @param size the size
510 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;
532 snprintf(name, sizeof(name), "U%u", size);
533 ident *id = new_id_from_str(name);
534 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
535 res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
536 set_primitive_base_type(res, base_tp);
541 static ir_type *create_bitfield_type(bitfield_type_t *const type)
543 type_t *base = skip_typeref(type->base_type);
544 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
545 ir_type *irbase = get_ir_type(base);
547 unsigned size = type->bit_size;
549 assert(!is_type_float(base));
550 if (is_type_signed(base)) {
551 return get_signed_int_type_for_bit_size(irbase, size);
553 return get_unsigned_int_type_for_bit_size(irbase, size);
557 #define INVALID_TYPE ((ir_type_ptr)-1)
560 COMPOUND_IS_STRUCT = false,
561 COMPOUND_IS_UNION = true
565 * Construct firm type from ast struct type.
567 static ir_type *create_compound_type(compound_type_t *type,
568 bool incomplete, bool is_union)
570 compound_t *compound = type->compound;
572 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
573 return compound->irtype;
576 symbol_t *symbol = compound->base.symbol;
578 if (symbol != NULL) {
579 id = new_id_from_str(symbol->string);
582 id = id_unique("__anonymous_union.%u");
584 id = id_unique("__anonymous_struct.%u");
587 dbg_info *dbgi = get_dbg_info(&compound->base.source_position);
591 irtype = new_d_type_union(id, dbgi);
593 irtype = new_d_type_struct(id, dbgi);
596 compound->irtype_complete = false;
597 compound->irtype = irtype;
603 layout_union_type(type);
605 layout_struct_type(type);
608 compound->irtype_complete = true;
610 entity_t *entry = compound->members.entities;
611 for ( ; entry != NULL; entry = entry->base.next) {
612 if (entry->kind != ENTITY_COMPOUND_MEMBER)
615 symbol_t *symbol = entry->base.symbol;
616 type_t *entry_type = entry->declaration.type;
618 if (symbol == NULL) {
619 /* anonymous bitfield member, skip */
620 if (entry_type->kind == TYPE_BITFIELD)
622 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
623 || entry_type->kind == TYPE_COMPOUND_UNION);
624 ident = id_unique("anon.%u");
626 ident = new_id_from_str(symbol->string);
629 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
631 ir_type *entry_irtype = get_ir_type(entry_type);
632 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
634 set_entity_offset(entity, entry->compound_member.offset);
635 set_entity_offset_bits_remainder(entity,
636 entry->compound_member.bit_offset);
638 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
639 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
640 entry->compound_member.entity = entity;
643 set_type_alignment_bytes(irtype, compound->alignment);
644 set_type_size_bytes(irtype, compound->size);
645 set_type_state(irtype, layout_fixed);
650 static ir_type *create_enum_type(enum_type_t *const type)
652 type->base.firm_type = ir_type_int;
654 ir_mode *const mode = mode_int;
655 tarval *const one = get_mode_one(mode);
656 tarval * tv_next = get_tarval_null(mode);
658 bool constant_folding_old = constant_folding;
659 constant_folding = true;
661 enum_t *enume = type->enume;
662 entity_t *entry = enume->base.next;
663 for (; entry != NULL; entry = entry->base.next) {
664 if (entry->kind != ENTITY_ENUM_VALUE)
667 expression_t *const init = entry->enum_value.value;
669 ir_node *const cnst = expression_to_firm(init);
670 if (!is_Const(cnst)) {
671 panic("couldn't fold constant");
673 tv_next = get_Const_tarval(cnst);
675 entry->enum_value.tv = tv_next;
676 tv_next = tarval_add(tv_next, one);
679 constant_folding = constant_folding_old;
681 return create_atomic_type(type->akind);
684 static ir_type *get_ir_type_incomplete(type_t *type)
686 assert(type != NULL);
687 type = skip_typeref(type);
689 if (type->base.firm_type != NULL) {
690 assert(type->base.firm_type != INVALID_TYPE);
691 return type->base.firm_type;
694 switch (type->kind) {
695 case TYPE_COMPOUND_STRUCT:
696 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
697 case TYPE_COMPOUND_UNION:
698 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
700 return get_ir_type(type);
704 ir_type *get_ir_type(type_t *type)
706 assert(type != NULL);
708 type = skip_typeref(type);
710 if (type->base.firm_type != NULL) {
711 assert(type->base.firm_type != INVALID_TYPE);
712 return type->base.firm_type;
715 ir_type *firm_type = NULL;
716 switch (type->kind) {
718 /* Happens while constant folding, when there was an error */
719 return create_atomic_type(ATOMIC_TYPE_VOID);
722 firm_type = create_atomic_type(type->atomic.akind);
725 firm_type = create_complex_type(&type->complex);
728 firm_type = create_imaginary_type(&type->imaginary);
731 firm_type = create_method_type(&type->function, false);
734 firm_type = create_pointer_type(&type->pointer);
737 firm_type = create_reference_type(&type->reference);
740 firm_type = create_array_type(&type->array);
742 case TYPE_COMPOUND_STRUCT:
743 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
745 case TYPE_COMPOUND_UNION:
746 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
749 firm_type = create_enum_type(&type->enumt);
752 firm_type = get_ir_type(type->builtin.real_type);
755 firm_type = create_bitfield_type(&type->bitfield);
763 if (firm_type == NULL)
764 panic("unknown type found");
766 type->base.firm_type = firm_type;
770 static ir_mode *get_ir_mode_storage(type_t *type)
772 ir_type *irtype = get_ir_type(type);
774 /* firm doesn't report a mode for arrays somehow... */
775 if (is_Array_type(irtype)) {
779 ir_mode *mode = get_type_mode(irtype);
780 assert(mode != NULL);
784 static ir_mode *get_ir_mode_arithmetic(type_t *type)
786 ir_mode *mode = get_ir_mode_storage(type);
787 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
788 return mode_float_arithmetic;
794 /** Names of the runtime functions. */
795 static const struct {
796 int id; /**< the rts id */
797 int n_res; /**< number of return values */
798 const char *name; /**< the name of the rts function */
799 int n_params; /**< number of parameters */
800 unsigned flags; /**< language flags */
802 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
803 { rts_abort, 0, "abort", 0, _C89 },
804 { rts_alloca, 1, "alloca", 1, _ALL },
805 { rts_abs, 1, "abs", 1, _C89 },
806 { rts_labs, 1, "labs", 1, _C89 },
807 { rts_llabs, 1, "llabs", 1, _C99 },
808 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
810 { rts_fabs, 1, "fabs", 1, _C89 },
811 { rts_sqrt, 1, "sqrt", 1, _C89 },
812 { rts_cbrt, 1, "cbrt", 1, _C99 },
813 { rts_exp, 1, "exp", 1, _C89 },
814 { rts_exp2, 1, "exp2", 1, _C89 },
815 { rts_exp10, 1, "exp10", 1, _GNUC },
816 { rts_log, 1, "log", 1, _C89 },
817 { rts_log2, 1, "log2", 1, _C89 },
818 { rts_log10, 1, "log10", 1, _C89 },
819 { rts_pow, 1, "pow", 2, _C89 },
820 { rts_sin, 1, "sin", 1, _C89 },
821 { rts_cos, 1, "cos", 1, _C89 },
822 { rts_tan, 1, "tan", 1, _C89 },
823 { rts_asin, 1, "asin", 1, _C89 },
824 { rts_acos, 1, "acos", 1, _C89 },
825 { rts_atan, 1, "atan", 1, _C89 },
826 { rts_sinh, 1, "sinh", 1, _C89 },
827 { rts_cosh, 1, "cosh", 1, _C89 },
828 { rts_tanh, 1, "tanh", 1, _C89 },
830 { rts_fabsf, 1, "fabsf", 1, _C99 },
831 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
832 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
833 { rts_expf, 1, "expf", 1, _C99 },
834 { rts_exp2f, 1, "exp2f", 1, _C99 },
835 { rts_exp10f, 1, "exp10f", 1, _GNUC },
836 { rts_logf, 1, "logf", 1, _C99 },
837 { rts_log2f, 1, "log2f", 1, _C99 },
838 { rts_log10f, 1, "log10f", 1, _C99 },
839 { rts_powf, 1, "powf", 2, _C99 },
840 { rts_sinf, 1, "sinf", 1, _C99 },
841 { rts_cosf, 1, "cosf", 1, _C99 },
842 { rts_tanf, 1, "tanf", 1, _C99 },
843 { rts_asinf, 1, "asinf", 1, _C99 },
844 { rts_acosf, 1, "acosf", 1, _C99 },
845 { rts_atanf, 1, "atanf", 1, _C99 },
846 { rts_sinhf, 1, "sinhf", 1, _C99 },
847 { rts_coshf, 1, "coshf", 1, _C99 },
848 { rts_tanhf, 1, "tanhf", 1, _C99 },
850 { rts_fabsl, 1, "fabsl", 1, _C99 },
851 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
852 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
853 { rts_expl, 1, "expl", 1, _C99 },
854 { rts_exp2l, 1, "exp2l", 1, _C99 },
855 { rts_exp10l, 1, "exp10l", 1, _GNUC },
856 { rts_logl, 1, "logl", 1, _C99 },
857 { rts_log2l, 1, "log2l", 1, _C99 },
858 { rts_log10l, 1, "log10l", 1, _C99 },
859 { rts_powl, 1, "powl", 2, _C99 },
860 { rts_sinl, 1, "sinl", 1, _C99 },
861 { rts_cosl, 1, "cosl", 1, _C99 },
862 { rts_tanl, 1, "tanl", 1, _C99 },
863 { rts_asinl, 1, "asinl", 1, _C99 },
864 { rts_acosl, 1, "acosl", 1, _C99 },
865 { rts_atanl, 1, "atanl", 1, _C99 },
866 { rts_sinhl, 1, "sinhl", 1, _C99 },
867 { rts_coshl, 1, "coshl", 1, _C99 },
868 { rts_tanhl, 1, "tanhl", 1, _C99 },
870 { rts_strcmp, 1, "strcmp", 2, _C89 },
871 { rts_strncmp, 1, "strncmp", 3, _C89 },
872 { rts_strcpy, 1, "strcpy", 2, _C89 },
873 { rts_strlen, 1, "strlen", 1, _C89 },
874 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
875 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
876 { rts_memmove, 1, "memmove", 3, _C89 }, /* HMM, man say its C99 */
877 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
878 { rts_memcmp, 1, "memcmp", 3, _C89 }, /* HMM, man say its C99 */
881 static ident *rts_idents[lengthof(rts_data)];
883 static ident* (*create_ld_ident)(entity_t*) = create_name_linux_elf;
886 * Handle GNU attributes for entities
888 * @param ent the entity
889 * @param decl the routine declaration
891 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
893 assert(is_declaration(entity));
894 decl_modifiers_t modifiers = entity->declaration.modifiers;
895 if (modifiers & DM_PURE) {
896 /* TRUE if the declaration includes the GNU
897 __attribute__((pure)) specifier. */
898 set_entity_additional_property(irentity, mtp_property_pure);
900 if (modifiers & DM_CONST) {
901 set_entity_additional_property(irentity, mtp_property_const);
902 have_const_functions = true;
904 if (modifiers & DM_USED) {
905 /* TRUE if the declaration includes the GNU
906 __attribute__((used)) specifier. */
907 set_entity_stickyness(irentity, stickyness_sticky);
911 static bool is_main(entity_t *entity)
913 static symbol_t *sym_main = NULL;
914 if (sym_main == NULL) {
915 sym_main = symbol_table_insert("main");
918 if (entity->base.symbol != sym_main)
920 /* must be in outermost scope */
921 if (entity->base.parent_scope != ¤t_translation_unit->scope)
928 * Creates an entity representing a function.
930 * @param declaration the function declaration
931 * @param owner_type the owner type of this function, NULL
932 * for global functions
934 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
936 assert(entity->kind == ENTITY_FUNCTION);
937 if (entity->function.irentity != NULL) {
938 return entity->function.irentity;
941 if (is_main(entity)) {
942 /* force main to C linkage */
943 type_t *type = entity->declaration.type;
944 assert(is_type_function(type));
945 if (type->function.linkage != LINKAGE_C) {
946 type_t *new_type = duplicate_type(type);
947 new_type->function.linkage = LINKAGE_C;
948 type = identify_new_type(new_type);
949 entity->declaration.type = type;
953 symbol_t *symbol = entity->base.symbol;
954 ident *id = new_id_from_str(symbol->string);
957 /* already an entity defined? */
958 ir_entity *irentity = entitymap_get(&entitymap, symbol);
959 bool const has_body = entity->function.statement != NULL;
960 if (irentity != NULL) {
961 if (get_entity_visibility(irentity) == visibility_external_allocated
963 set_entity_visibility(irentity, visibility_external_visible);
968 ir_type *ir_type_method;
969 if (entity->function.need_closure)
970 ir_type_method = create_method_type(&entity->declaration.type->function, true);
972 ir_type_method = get_ir_type(entity->declaration.type);
974 bool nested_function = false;
975 if (owner_type == NULL)
976 owner_type = get_glob_type();
978 nested_function = true;
980 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
981 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
985 ld_id = id_unique("inner.%u");
987 ld_id = create_ld_ident(entity);
988 set_entity_ld_ident(irentity, ld_id);
990 handle_decl_modifiers(irentity, entity);
992 if (! nested_function) {
993 /* static inline => local
994 * extern inline => local
995 * inline without definition => local
996 * inline with definition => external_visible */
997 storage_class_tag_t const storage_class
998 = (storage_class_tag_t) entity->declaration.storage_class;
999 bool const is_inline = entity->function.is_inline;
1001 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1002 set_entity_visibility(irentity, visibility_external_visible);
1003 } else if (storage_class == STORAGE_CLASS_STATIC ||
1004 (is_inline && has_body)) {
1006 /* this entity was declared, but is defined nowhere */
1007 set_entity_peculiarity(irentity, peculiarity_description);
1009 set_entity_visibility(irentity, visibility_local);
1010 } else if (has_body) {
1011 set_entity_visibility(irentity, visibility_external_visible);
1013 set_entity_visibility(irentity, visibility_external_allocated);
1016 /* nested functions are always local */
1017 set_entity_visibility(irentity, visibility_local);
1019 set_entity_allocation(irentity, allocation_static);
1021 /* We should check for file scope here, but as long as we compile C only
1022 this is not needed. */
1023 if (! firm_opt.freestanding && !has_body) {
1024 /* check for a known runtime function */
1025 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1026 if (id != rts_idents[i])
1029 /* ignore those rts functions not necessary needed for current mode */
1030 if ((c_mode & rts_data[i].flags) == 0)
1032 assert(rts_entities[rts_data[i].id] == NULL);
1033 rts_entities[rts_data[i].id] = irentity;
1037 entitymap_insert(&entitymap, symbol, irentity);
1040 entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1041 entity->function.irentity = irentity;
1046 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1048 ir_mode *value_mode = get_irn_mode(value);
1050 if (value_mode == dest_mode || is_Bad(value))
1053 if (dest_mode == mode_b) {
1054 ir_node *zero = new_Const(get_mode_null(value_mode));
1055 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1056 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1060 return new_d_Conv(dbgi, value, dest_mode);
1064 * Creates a Const node representing a constant.
1066 static ir_node *const_to_firm(const const_expression_t *cnst)
1068 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1069 type_t *type = skip_typeref(cnst->base.type);
1070 ir_mode *mode = get_ir_mode_storage(type);
1075 if (mode_is_float(mode)) {
1076 tv = new_tarval_from_double(cnst->v.float_value, mode);
1078 if (mode_is_signed(mode)) {
1079 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1081 len = snprintf(buf, sizeof(buf), "%llu",
1082 (unsigned long long) cnst->v.int_value);
1084 tv = new_tarval_from_str(buf, len, mode);
1087 ir_node *res = new_d_Const(dbgi, tv);
1088 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1089 return create_conv(dbgi, res, mode_arith);
1093 * Creates a Const node representing a character constant.
1095 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1097 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1098 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1101 size_t const size = cnst->v.character.size;
1102 if (size == 1 && char_is_signed) {
1103 v = (signed char)cnst->v.character.begin[0];
1106 for (size_t i = 0; i < size; ++i) {
1107 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1111 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1112 tarval *tv = new_tarval_from_str(buf, len, mode);
1114 return new_d_Const(dbgi, tv);
1118 * Creates a Const node representing a wide character constant.
1120 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1122 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1123 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1125 long long int v = cnst->v.wide_character.begin[0];
1128 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1129 tarval *tv = new_tarval_from_str(buf, len, mode);
1131 return new_d_Const(dbgi, tv);
1135 * Allocate an area of size bytes aligned at alignment
1138 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment) {
1139 static unsigned area_cnt = 0;
1142 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1143 ident *name = new_id_from_str(buf);
1145 ir_type *tp = new_type_array(id_mangle_u(get_type_ident(frame_type), name), 1, ir_type_char);
1146 set_array_bounds_int(tp, 0, 0, size);
1147 set_type_alignment_bytes(tp, alignment);
1149 ir_entity *area = new_entity(frame_type, name, tp);
1151 /* mark this entity as compiler generated */
1152 set_entity_compiler_generated(area, 1);
1157 * Return a node representing a trampoline reagion
1158 * for a given entity.
1160 * @param dbgi debug info
1161 * @param entity the entity
1163 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1165 ir_entity *region = NULL;
1168 if (current_trampolines != NULL) {
1169 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1170 if (current_trampolines[i].function == entity) {
1171 region = current_trampolines[i].region;
1176 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1178 ir_graph *irg = current_ir_graph;
1179 if (region == NULL) {
1180 /* create a new region */
1181 ir_type *frame_tp = get_irg_frame_type(irg);
1182 trampoline_region reg;
1183 reg.function = entity;
1185 reg.region = alloc_trampoline(frame_tp,
1186 be_params->trampoline_size,
1187 be_params->trampoline_align);
1188 ARR_APP1(trampoline_region, current_trampolines, reg);
1189 region = reg.region;
1191 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1197 * Creates a SymConst for a given entity.
1199 * @param dbgi debug info
1200 * @param mode the (reference) mode for the SymConst
1201 * @param entity the entity
1203 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1206 assert(entity != NULL);
1207 union symconst_symbol sym;
1208 sym.entity_p = entity;
1209 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1213 * Creates a SymConst for a given trampoline of an entity.
1215 * @param dbgi debug info
1216 * @param mode the (reference) mode for the SymConst
1217 * @param entity the entity
1219 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1222 assert(entity != NULL);
1224 in[0] = get_trampoline_region(dbgi, entity);
1225 in[1] = create_symconst(dbgi, mode, entity);
1226 in[2] = get_irg_frame(current_ir_graph);
1228 ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_inner_trampoline, 3, in, get_unknown_type());
1229 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1230 return new_Proj(irn, mode, pn_Builtin_1_result);
1234 * Creates a SymConst node representing a string constant.
1236 * @param src_pos the source position of the string constant
1237 * @param id_prefix a prefix for the name of the generated string constant
1238 * @param value the value of the string constant
1240 static ir_node *string_to_firm(const source_position_t *const src_pos,
1241 const char *const id_prefix,
1242 const string_t *const value)
1244 ir_type *const global_type = get_glob_type();
1245 dbg_info *const dbgi = get_dbg_info(src_pos);
1246 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1247 ir_type_const_char, dbgi);
1249 ident *const id = id_unique(id_prefix);
1250 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1251 set_entity_ld_ident(entity, id);
1252 set_entity_variability(entity, variability_constant);
1253 set_entity_allocation(entity, allocation_static);
1254 set_entity_visibility(entity, visibility_local);
1256 ir_type *const elem_type = ir_type_const_char;
1257 ir_mode *const mode = get_type_mode(elem_type);
1259 const char* const string = value->begin;
1260 const size_t slen = value->size;
1262 set_array_lower_bound_int(type, 0, 0);
1263 set_array_upper_bound_int(type, 0, slen);
1264 set_type_size_bytes(type, slen);
1265 set_type_state(type, layout_fixed);
1267 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1268 for (size_t i = 0; i < slen; ++i) {
1269 tvs[i] = new_tarval_from_long(string[i], mode);
1272 set_array_entity_values(entity, tvs, slen);
1275 return create_symconst(dbgi, mode_P_data, entity);
1279 * Creates a SymConst node representing a string literal.
1281 * @param literal the string literal
1283 static ir_node *string_literal_to_firm(
1284 const string_literal_expression_t* literal)
1286 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1291 * Creates a SymConst node representing a wide string literal.
1293 * @param literal the wide string literal
1295 static ir_node *wide_string_literal_to_firm(
1296 const wide_string_literal_expression_t* const literal)
1298 ir_type *const global_type = get_glob_type();
1299 ir_type *const elem_type = ir_type_wchar_t;
1300 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1301 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1304 ident *const id = id_unique("Lstr.%u");
1305 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1306 set_entity_ld_ident(entity, id);
1307 set_entity_variability(entity, variability_constant);
1308 set_entity_allocation(entity, allocation_static);
1310 ir_mode *const mode = get_type_mode(elem_type);
1312 const wchar_rep_t *const string = literal->value.begin;
1313 const size_t slen = literal->value.size;
1315 set_array_lower_bound_int(type, 0, 0);
1316 set_array_upper_bound_int(type, 0, slen);
1317 set_type_size_bytes(type, slen);
1318 set_type_state(type, layout_fixed);
1320 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1321 for (size_t i = 0; i < slen; ++i) {
1322 tvs[i] = new_tarval_from_long(string[i], mode);
1325 set_array_entity_values(entity, tvs, slen);
1328 return create_symconst(dbgi, mode_P_data, entity);
1332 * Dereference an address.
1334 * @param dbgi debug info
1335 * @param type the type of the dereferenced result (the points_to type)
1336 * @param addr the address to dereference
1338 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1339 ir_node *const addr)
1341 ir_type *irtype = get_ir_type(type);
1342 if (is_compound_type(irtype)
1343 || is_Method_type(irtype)
1344 || is_Array_type(irtype)) {
1348 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1349 ? cons_volatile : cons_none;
1350 ir_mode *const mode = get_type_mode(irtype);
1351 ir_node *const memory = get_store();
1352 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1353 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1354 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1356 set_store(load_mem);
1358 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1359 return create_conv(dbgi, load_res, mode_arithmetic);
1363 * Creates a strict Conv (to the node's mode) if necessary.
1365 * @param dbgi debug info
1366 * @param node the node to strict conv
1368 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1370 ir_mode *mode = get_irn_mode(node);
1372 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1374 if (!mode_is_float(mode))
1377 /* check if there is already a Conv */
1378 if (is_Conv(node)) {
1379 /* convert it into a strict Conv */
1380 set_Conv_strict(node, 1);
1384 /* otherwise create a new one */
1385 return new_d_strictConv(dbgi, node, mode);
1389 * Returns the address of a global variable.
1391 * @param dbgi debug info
1392 * @param variable the variable
1394 static ir_node *get_global_var_address(dbg_info *const dbgi,
1395 const variable_t *const variable)
1397 ir_entity *const irentity = variable->v.entity;
1398 if (variable->thread_local) {
1399 ir_node *const no_mem = new_NoMem();
1400 ir_node *const tls = get_irg_tls(current_ir_graph);
1401 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1403 return create_symconst(dbgi, mode_P_data, irentity);
1408 * Returns the correct base address depending on whether it is a parameter or a
1409 * normal local variable.
1411 static ir_node *get_local_frame(ir_entity *const ent)
1413 ir_graph *const irg = current_ir_graph;
1414 const ir_type *const owner = get_entity_owner(ent);
1415 if (owner == current_outer_frame || owner == current_outer_value_type) {
1416 assert(current_static_link != NULL);
1417 return current_static_link;
1419 return get_irg_frame(irg);
1424 * Keep all memory edges of the given block.
1426 static void keep_all_memory(ir_node *block)
1428 ir_node *old = get_cur_block();
1430 set_cur_block(block);
1431 keep_alive(get_store());
1432 /* TODO: keep all memory edges from restricted pointers */
1436 static ir_node *reference_expression_enum_value_to_firm(
1437 const reference_expression_t *ref)
1439 entity_t *entity = ref->entity;
1440 type_t *type = skip_typeref(entity->enum_value.enum_type);
1441 /* make sure the type is constructed */
1442 (void) get_ir_type(type);
1444 return new_Const(entity->enum_value.tv);
1447 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1449 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1450 entity_t *entity = ref->entity;
1451 assert(is_declaration(entity));
1452 type_t *type = skip_typeref(entity->declaration.type);
1454 /* make sure the type is constructed */
1455 (void) get_ir_type(type);
1457 switch ((declaration_kind_t) entity->declaration.kind) {
1458 case DECLARATION_KIND_UNKNOWN:
1461 case DECLARATION_KIND_LOCAL_VARIABLE: {
1462 ir_mode *const mode = get_ir_mode_storage(type);
1463 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1464 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1466 case DECLARATION_KIND_PARAMETER: {
1467 ir_mode *const mode = get_ir_mode_storage(type);
1468 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1469 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1471 case DECLARATION_KIND_FUNCTION: {
1472 ir_mode *const mode = get_ir_mode_storage(type);
1474 if (entity->function.btk != bk_none) {
1475 /* for gcc compatibility we have to produce (dummy) addresses for some
1477 if (warning.other) {
1478 warningf(&ref->base.source_position,
1479 "taking address of builtin '%Y'", ref->entity->base.symbol);
1482 /* simply create a NULL pointer */
1483 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1484 ir_node *res = new_Const_long(mode, 0);
1488 return create_symconst(dbgi, mode, entity->function.irentity);
1490 case DECLARATION_KIND_INNER_FUNCTION: {
1491 ir_mode *const mode = get_ir_mode_storage(type);
1492 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1493 /* inner function not using the closure */
1494 return create_symconst(dbgi, mode, entity->function.irentity);
1496 /* need trampoline here */
1497 return create_trampoline(dbgi, mode, entity->function.irentity);
1500 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1501 const variable_t *variable = &entity->variable;
1502 ir_node *const addr = get_global_var_address(dbgi, variable);
1503 return deref_address(dbgi, variable->base.type, addr);
1506 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1507 ir_entity *irentity = entity->variable.v.entity;
1508 ir_node *frame = get_local_frame(irentity);
1509 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1510 return deref_address(dbgi, entity->declaration.type, sel);
1512 case DECLARATION_KIND_PARAMETER_ENTITY: {
1513 ir_entity *irentity = entity->parameter.v.entity;
1514 ir_node *frame = get_local_frame(irentity);
1515 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1516 return deref_address(dbgi, entity->declaration.type, sel);
1519 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1520 return entity->variable.v.vla_base;
1522 case DECLARATION_KIND_COMPOUND_MEMBER:
1523 panic("not implemented reference type");
1526 panic("reference to declaration with unknown type found");
1529 static ir_node *reference_addr(const reference_expression_t *ref)
1531 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1532 entity_t *entity = ref->entity;
1533 assert(is_declaration(entity));
1535 switch((declaration_kind_t) entity->declaration.kind) {
1536 case DECLARATION_KIND_UNKNOWN:
1538 case DECLARATION_KIND_PARAMETER:
1539 case DECLARATION_KIND_LOCAL_VARIABLE:
1540 /* you can store to a local variable (so we don't panic but return NULL
1541 * as an indicator for no real address) */
1543 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1544 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1547 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1548 ir_entity *irentity = entity->variable.v.entity;
1549 ir_node *frame = get_local_frame(irentity);
1550 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1554 case DECLARATION_KIND_PARAMETER_ENTITY: {
1555 ir_entity *irentity = entity->parameter.v.entity;
1556 ir_node *frame = get_local_frame(irentity);
1557 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1562 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1563 return entity->variable.v.vla_base;
1565 case DECLARATION_KIND_FUNCTION: {
1566 type_t *const type = skip_typeref(entity->declaration.type);
1567 ir_mode *const mode = get_ir_mode_storage(type);
1568 return create_symconst(dbgi, mode, entity->function.irentity);
1571 case DECLARATION_KIND_INNER_FUNCTION: {
1572 type_t *const type = skip_typeref(entity->declaration.type);
1573 ir_mode *const mode = get_ir_mode_storage(type);
1574 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1575 /* inner function not using the closure */
1576 return create_symconst(dbgi, mode, entity->function.irentity);
1578 /* need trampoline here */
1579 return create_trampoline(dbgi, mode, entity->function.irentity);
1583 case DECLARATION_KIND_COMPOUND_MEMBER:
1584 panic("not implemented reference type");
1587 panic("reference to declaration with unknown type found");
1591 * Generate an unary builtin.
1593 * @param kind the builtin kind to generate
1594 * @param op the operand
1595 * @param function_type the function type for the GNU builtin routine
1596 * @param db debug info
1598 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1601 in[0] = expression_to_firm(op);
1603 ir_type *tp = get_ir_type(function_type);
1604 ir_type *res = get_method_res_type(tp, 0);
1605 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), kind, 1, in, tp);
1606 set_irn_pinned(irn, op_pin_state_floats);
1607 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1611 * Generate a pinned unary builtin.
1613 * @param kind the builtin kind to generate
1614 * @param op the operand
1615 * @param function_type the function type for the GNU builtin routine
1616 * @param db debug info
1618 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1621 in[0] = expression_to_firm(op);
1623 ir_type *tp = get_ir_type(function_type);
1624 ir_type *res = get_method_res_type(tp, 0);
1625 ir_node *mem = get_store();
1626 ir_node *irn = new_d_Builtin(db, mem, kind, 1, in, tp);
1627 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1628 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1633 * Generate an binary-void-return builtin.
1635 * @param kind the builtin kind to generate
1636 * @param op1 the first operand
1637 * @param op2 the second operand
1638 * @param function_type the function type for the GNU builtin routine
1639 * @param db debug info
1641 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1, expression_t *op2,
1642 type_t *function_type, dbg_info *db)
1645 in[0] = expression_to_firm(op1);
1646 in[1] = expression_to_firm(op2);
1648 ir_type *tp = get_ir_type(function_type);
1649 ir_node *mem = get_store();
1650 ir_node *irn = new_d_Builtin(db, mem, kind, 2, in, tp);
1651 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1656 * Transform calls to builtin functions.
1658 static ir_node *process_builtin_call(const call_expression_t *call)
1660 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1662 assert(call->function->kind == EXPR_REFERENCE);
1663 reference_expression_t *builtin = &call->function->reference;
1665 type_t *type = skip_typeref(builtin->base.type);
1666 assert(is_type_pointer(type));
1668 type_t *function_type = skip_typeref(type->pointer.points_to);
1670 switch (builtin->entity->function.btk) {
1671 case bk_gnu_builtin_alloca: {
1672 if (call->arguments == NULL || call->arguments->next != NULL) {
1673 panic("invalid number of parameters on __builtin_alloca");
1675 expression_t *argument = call->arguments->expression;
1676 ir_node *size = expression_to_firm(argument);
1678 ir_node *store = get_store();
1679 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1681 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1683 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1688 case bk_gnu_builtin_huge_val:
1689 case bk_gnu_builtin_inf:
1690 case bk_gnu_builtin_inff:
1691 case bk_gnu_builtin_infl: {
1692 type_t *type = function_type->function.return_type;
1693 ir_mode *mode = get_ir_mode_arithmetic(type);
1694 tarval *tv = get_mode_infinite(mode);
1695 ir_node *res = new_d_Const(dbgi, tv);
1698 case bk_gnu_builtin_nan:
1699 case bk_gnu_builtin_nanf:
1700 case bk_gnu_builtin_nanl: {
1701 /* Ignore string for now... */
1702 assert(is_type_function(function_type));
1703 type_t *type = function_type->function.return_type;
1704 ir_mode *mode = get_ir_mode_arithmetic(type);
1705 tarval *tv = get_mode_NAN(mode);
1706 ir_node *res = new_d_Const(dbgi, tv);
1709 case bk_gnu_builtin_expect: {
1710 expression_t *argument = call->arguments->expression;
1711 return _expression_to_firm(argument);
1713 case bk_gnu_builtin_va_end:
1714 /* evaluate the argument of va_end for its side effects */
1715 _expression_to_firm(call->arguments->expression);
1717 case bk_gnu_builtin_frame_address: {
1718 expression_t *const expression = call->arguments->expression;
1719 bool val = fold_constant_to_bool(expression);
1722 return get_irg_frame(current_ir_graph);
1724 /* get the argument */
1727 in[0] = expression_to_firm(expression);
1728 in[1] = get_irg_frame(current_ir_graph);
1729 ir_type *tp = get_ir_type(function_type);
1730 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_frame_addess, 2, in, tp);
1731 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1734 case bk_gnu_builtin_return_address: {
1736 expression_t *const expression = call->arguments->expression;
1739 in[0] = expression_to_firm(expression);
1740 in[1] = get_irg_frame(current_ir_graph);
1741 ir_type *tp = get_ir_type(function_type);
1742 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_return_address, 2, in, tp);
1743 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1745 case bk_gnu_builtin_ffs:
1746 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1747 case bk_gnu_builtin_clz:
1748 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1749 case bk_gnu_builtin_ctz:
1750 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1751 case bk_gnu_builtin_popcount:
1752 case bk_ms__popcount:
1753 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1754 case bk_gnu_builtin_parity:
1755 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1756 case bk_gnu_builtin_prefetch: {
1757 call_argument_t *const args = call->arguments;
1758 expression_t *const addr = args->expression;
1761 in[0] = _expression_to_firm(addr);
1762 if (args->next != NULL) {
1763 expression_t *const rw = args->next->expression;
1765 in[1] = _expression_to_firm(rw);
1767 if (args->next->next != NULL) {
1768 expression_t *const locality = args->next->next->expression;
1770 in[2] = expression_to_firm(locality);
1772 in[2] = new_Const_long(mode_int, 3);
1775 in[1] = new_Const_long(mode_int, 0);
1776 in[2] = new_Const_long(mode_int, 3);
1778 ir_type *tp = get_ir_type(function_type);
1779 ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_prefetch, 3, in, tp);
1780 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1783 case bk_gnu_builtin_trap:
1786 ir_type *tp = get_ir_type(function_type);
1787 ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_trap, 0, NULL, tp);
1788 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1791 case bk_ms__debugbreak: {
1792 ir_type *tp = get_ir_type(function_type);
1793 ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_debugbreak, 0, NULL, tp);
1794 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1797 case bk_ms_ReturnAddress: {
1800 in[0] = new_Const_long(mode_int, 0);
1801 in[1] = get_irg_frame(current_ir_graph);
1802 ir_type *tp = get_ir_type(function_type);
1803 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_return_address, 2, in, tp);
1804 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1807 case bk_ms_rotl64: {
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 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1814 case bk_ms_rotr64: {
1815 ir_node *val = expression_to_firm(call->arguments->expression);
1816 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1817 ir_mode *mode = get_irn_mode(val);
1818 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1819 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1820 return new_d_Rotl(dbgi, val, sub, mode);
1822 case bk_ms_byteswap_ushort:
1823 case bk_ms_byteswap_ulong:
1824 case bk_ms_byteswap_uint64:
1825 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1828 case bk_ms__indword:
1829 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1830 case bk_ms__outbyte:
1831 case bk_ms__outword:
1832 case bk_ms__outdword:
1833 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1834 call->arguments->next->expression, function_type, dbgi);
1836 panic("unsupported builtin found");
1841 * Transform a call expression.
1842 * Handles some special cases, like alloca() calls, which must be resolved
1843 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1844 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1847 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1849 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1850 assert(get_cur_block() != NULL);
1852 expression_t *function = call->function;
1853 if (function->kind == EXPR_REFERENCE) {
1854 const reference_expression_t *ref = &function->reference;
1855 entity_t *entity = ref->entity;
1857 if (entity->kind == ENTITY_FUNCTION) {
1858 if (entity->function.btk != bk_none) {
1859 return process_builtin_call(call);
1862 ir_entity *irentity = entity->function.irentity;
1863 if (irentity == NULL)
1864 irentity = get_function_entity(entity, NULL);
1866 if (irentity == rts_entities[rts_alloca]) {
1867 /* handle alloca() call */
1868 expression_t *argument = call->arguments->expression;
1869 ir_node *size = expression_to_firm(argument);
1870 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1872 size = create_conv(dbgi, size, mode);
1874 ir_node *store = get_store();
1875 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1876 firm_unknown_type, stack_alloc);
1877 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1879 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1885 ir_node *callee = expression_to_firm(function);
1887 type_t *type = skip_typeref(function->base.type);
1888 assert(is_type_pointer(type));
1889 pointer_type_t *pointer_type = &type->pointer;
1890 type_t *points_to = skip_typeref(pointer_type->points_to);
1891 assert(is_type_function(points_to));
1892 function_type_t *function_type = &points_to->function;
1894 int n_parameters = 0;
1895 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1896 ir_type *new_method_type = NULL;
1897 if (function_type->variadic || function_type->unspecified_parameters) {
1898 const call_argument_t *argument = call->arguments;
1899 for ( ; argument != NULL; argument = argument->next) {
1903 /* we need to construct a new method type matching the call
1905 int n_res = get_method_n_ress(ir_method_type);
1906 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1907 n_parameters, n_res, dbgi);
1908 set_method_calling_convention(new_method_type,
1909 get_method_calling_convention(ir_method_type));
1910 set_method_additional_properties(new_method_type,
1911 get_method_additional_properties(ir_method_type));
1912 set_method_variadicity(new_method_type,
1913 get_method_variadicity(ir_method_type));
1915 for (int i = 0; i < n_res; ++i) {
1916 set_method_res_type(new_method_type, i,
1917 get_method_res_type(ir_method_type, i));
1919 argument = call->arguments;
1920 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
1921 expression_t *expression = argument->expression;
1922 ir_type *irtype = get_ir_type(expression->base.type);
1923 set_method_param_type(new_method_type, i, irtype);
1925 ir_method_type = new_method_type;
1927 n_parameters = get_method_n_params(ir_method_type);
1930 ir_node *in[n_parameters];
1932 const call_argument_t *argument = call->arguments;
1933 for (int n = 0; n < n_parameters; ++n) {
1934 expression_t *expression = argument->expression;
1935 ir_node *arg_node = expression_to_firm(expression);
1937 type_t *type = skip_typeref(expression->base.type);
1938 if (!is_type_compound(type)) {
1939 ir_mode *mode = get_ir_mode_storage(expression->base.type);
1940 arg_node = create_conv(dbgi, arg_node, mode);
1941 arg_node = do_strict_conv(dbgi, arg_node);
1946 argument = argument->next;
1949 ir_node *store = get_store();
1950 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1952 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1955 type_t *return_type = skip_typeref(function_type->return_type);
1956 ir_node *result = NULL;
1958 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1959 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1961 if (is_type_scalar(return_type)) {
1962 ir_mode *mode = get_ir_mode_storage(return_type);
1963 result = new_d_Proj(dbgi, resproj, mode, 0);
1964 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
1965 result = create_conv(NULL, result, mode_arith);
1967 ir_mode *mode = mode_P_data;
1968 result = new_d_Proj(dbgi, resproj, mode, 0);
1972 if (function->kind == EXPR_REFERENCE &&
1973 function->reference.entity->declaration.modifiers & DM_NORETURN) {
1974 /* A dead end: Keep the Call and the Block. Also place all further
1975 * nodes into a new and unreachable block. */
1977 keep_alive(get_cur_block());
1984 static void statement_to_firm(statement_t *statement);
1985 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1987 static ir_node *expression_to_addr(const expression_t *expression);
1988 static ir_node *create_condition_evaluation(const expression_t *expression,
1989 ir_node *true_block,
1990 ir_node *false_block);
1992 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1995 if (!is_type_compound(type)) {
1996 ir_mode *mode = get_ir_mode_storage(type);
1997 value = create_conv(dbgi, value, mode);
1998 value = do_strict_conv(dbgi, value);
2001 ir_node *memory = get_store();
2003 if (is_type_scalar(type)) {
2004 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2005 ? cons_volatile : cons_none;
2006 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2007 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2008 set_store(store_mem);
2010 ir_type *irtype = get_ir_type(type);
2011 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2012 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2013 set_store(copyb_mem);
2017 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2019 tarval *all_one = get_mode_all_one(mode);
2020 int mode_size = get_mode_size_bits(mode);
2022 assert(offset >= 0);
2024 assert(offset + size <= mode_size);
2025 if (size == mode_size) {
2029 long shiftr = get_mode_size_bits(mode) - size;
2030 long shiftl = offset;
2031 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2032 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2033 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2034 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2039 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2040 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2042 ir_type *entity_type = get_entity_type(entity);
2043 ir_type *base_type = get_primitive_base_type(entity_type);
2044 assert(base_type != NULL);
2045 ir_mode *mode = get_type_mode(base_type);
2047 value = create_conv(dbgi, value, mode);
2049 /* kill upper bits of value and shift to right position */
2050 int bitoffset = get_entity_offset_bits_remainder(entity);
2051 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2053 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2054 ir_node *mask_node = new_d_Const(dbgi, mask);
2055 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2056 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2057 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2058 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2060 /* load current value */
2061 ir_node *mem = get_store();
2062 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2063 set_volatile ? cons_volatile : cons_none);
2064 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2065 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2066 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2067 tarval *inv_mask = tarval_not(shift_mask);
2068 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2069 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2071 /* construct new value and store */
2072 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2073 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2074 set_volatile ? cons_volatile : cons_none);
2075 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2076 set_store(store_mem);
2078 return value_masked;
2081 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2084 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2085 type_t *type = expression->base.type;
2086 ir_mode *mode = get_ir_mode_storage(type);
2087 ir_node *mem = get_store();
2088 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2089 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2090 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2092 load_res = create_conv(dbgi, load_res, mode_int);
2094 set_store(load_mem);
2096 /* kill upper bits */
2097 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2098 ir_entity *entity = expression->compound_entry->compound_member.entity;
2099 int bitoffset = get_entity_offset_bits_remainder(entity);
2100 ir_type *entity_type = get_entity_type(entity);
2101 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2102 long shift_bitsl = machine_size - bitoffset - bitsize;
2103 assert(shift_bitsl >= 0);
2104 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2105 ir_node *countl = new_d_Const(dbgi, tvl);
2106 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2108 long shift_bitsr = bitoffset + shift_bitsl;
2109 assert(shift_bitsr <= (long) machine_size);
2110 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2111 ir_node *countr = new_d_Const(dbgi, tvr);
2113 if (mode_is_signed(mode)) {
2114 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2116 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2119 return create_conv(dbgi, shiftr, mode);
2122 /* make sure the selected compound type is constructed */
2123 static void construct_select_compound(const select_expression_t *expression)
2125 type_t *type = skip_typeref(expression->compound->base.type);
2126 if (is_type_pointer(type)) {
2127 type = type->pointer.points_to;
2129 (void) get_ir_type(type);
2132 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2133 ir_node *value, ir_node *addr)
2135 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2136 type_t *type = skip_typeref(expression->base.type);
2138 if (!is_type_compound(type)) {
2139 ir_mode *mode = get_ir_mode_storage(type);
2140 value = create_conv(dbgi, value, mode);
2141 value = do_strict_conv(dbgi, value);
2144 if (expression->kind == EXPR_REFERENCE) {
2145 const reference_expression_t *ref = &expression->reference;
2147 entity_t *entity = ref->entity;
2148 assert(is_declaration(entity));
2149 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2150 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2151 set_value(entity->variable.v.value_number, value);
2153 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2154 set_value(entity->parameter.v.value_number, value);
2160 addr = expression_to_addr(expression);
2161 assert(addr != NULL);
2163 if (expression->kind == EXPR_SELECT) {
2164 const select_expression_t *select = &expression->select;
2166 construct_select_compound(select);
2168 entity_t *entity = select->compound_entry;
2169 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2170 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2171 ir_entity *irentity = entity->compound_member.entity;
2173 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2174 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2180 assign_value(dbgi, addr, type, value);
2184 static void set_value_for_expression(const expression_t *expression,
2187 set_value_for_expression_addr(expression, value, NULL);
2190 static ir_node *get_value_from_lvalue(const expression_t *expression,
2193 if (expression->kind == EXPR_REFERENCE) {
2194 const reference_expression_t *ref = &expression->reference;
2196 entity_t *entity = ref->entity;
2197 assert(entity->kind == ENTITY_VARIABLE
2198 || entity->kind == ENTITY_PARAMETER);
2199 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2201 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2202 value_number = entity->variable.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));
2208 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2209 value_number = entity->parameter.v.value_number;
2210 assert(addr == NULL);
2211 type_t *type = skip_typeref(expression->base.type);
2212 ir_mode *mode = get_ir_mode_storage(type);
2213 ir_node *res = get_value(value_number, mode);
2214 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2218 assert(addr != NULL);
2219 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2222 if (expression->kind == EXPR_SELECT &&
2223 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2224 construct_select_compound(&expression->select);
2225 value = bitfield_extract_to_firm(&expression->select, addr);
2227 value = deref_address(dbgi, expression->base.type, addr);
2234 static ir_node *create_incdec(const unary_expression_t *expression)
2236 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2237 const expression_t *value_expr = expression->value;
2238 ir_node *addr = expression_to_addr(value_expr);
2239 ir_node *value = get_value_from_lvalue(value_expr, addr);
2241 type_t *type = skip_typeref(expression->base.type);
2242 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2245 if (is_type_pointer(type)) {
2246 pointer_type_t *pointer_type = &type->pointer;
2247 offset = get_type_size_node(pointer_type->points_to);
2249 assert(is_type_arithmetic(type));
2250 offset = new_Const(get_mode_one(mode));
2254 ir_node *store_value;
2255 switch(expression->base.kind) {
2256 case EXPR_UNARY_POSTFIX_INCREMENT:
2258 store_value = new_d_Add(dbgi, value, offset, mode);
2260 case EXPR_UNARY_POSTFIX_DECREMENT:
2262 store_value = new_d_Sub(dbgi, value, offset, mode);
2264 case EXPR_UNARY_PREFIX_INCREMENT:
2265 result = new_d_Add(dbgi, value, offset, mode);
2266 store_value = result;
2268 case EXPR_UNARY_PREFIX_DECREMENT:
2269 result = new_d_Sub(dbgi, value, offset, mode);
2270 store_value = result;
2273 panic("no incdec expr in create_incdec");
2276 set_value_for_expression_addr(value_expr, store_value, addr);
2281 static bool is_local_variable(expression_t *expression)
2283 if (expression->kind != EXPR_REFERENCE)
2285 reference_expression_t *ref_expr = &expression->reference;
2286 entity_t *entity = ref_expr->entity;
2287 if (entity->kind != ENTITY_VARIABLE)
2289 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2290 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2293 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2296 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2297 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2298 case EXPR_BINARY_NOTEQUAL:
2299 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2300 case EXPR_BINARY_ISLESS:
2301 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2302 case EXPR_BINARY_ISLESSEQUAL:
2303 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2304 case EXPR_BINARY_ISGREATER:
2305 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2306 case EXPR_BINARY_ISGREATEREQUAL:
2307 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2308 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2313 panic("trying to get pn_Cmp from non-comparison binexpr type");
2317 * Handle the assume optimizer hint: check if a Confirm
2318 * node can be created.
2320 * @param dbi debug info
2321 * @param expr the IL assume expression
2323 * we support here only some simple cases:
2328 static ir_node *handle_assume_compare(dbg_info *dbi,
2329 const binary_expression_t *expression)
2331 expression_t *op1 = expression->left;
2332 expression_t *op2 = expression->right;
2333 entity_t *var2, *var = NULL;
2334 ir_node *res = NULL;
2337 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2339 if (is_local_variable(op1) && is_local_variable(op2)) {
2340 var = op1->reference.entity;
2341 var2 = op2->reference.entity;
2343 type_t *const type = skip_typeref(var->declaration.type);
2344 ir_mode *const mode = get_ir_mode_storage(type);
2346 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2347 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2349 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2350 set_value(var2->variable.v.value_number, res);
2352 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2353 set_value(var->variable.v.value_number, res);
2359 if (is_local_variable(op1) && is_constant_expression(op2)) {
2360 var = op1->reference.entity;
2362 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2363 cmp_val = get_inversed_pnc(cmp_val);
2364 var = op2->reference.entity;
2369 type_t *const type = skip_typeref(var->declaration.type);
2370 ir_mode *const mode = get_ir_mode_storage(type);
2372 res = get_value(var->variable.v.value_number, mode);
2373 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2374 set_value(var->variable.v.value_number, res);
2380 * Handle the assume optimizer hint.
2382 * @param dbi debug info
2383 * @param expr the IL assume expression
2385 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2387 switch(expression->kind) {
2388 case EXPR_BINARY_EQUAL:
2389 case EXPR_BINARY_NOTEQUAL:
2390 case EXPR_BINARY_LESS:
2391 case EXPR_BINARY_LESSEQUAL:
2392 case EXPR_BINARY_GREATER:
2393 case EXPR_BINARY_GREATEREQUAL:
2394 return handle_assume_compare(dbi, &expression->binary);
2400 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2402 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2403 type_t *type = skip_typeref(expression->base.type);
2405 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2406 return expression_to_addr(expression->value);
2408 const expression_t *value = expression->value;
2410 switch(expression->base.kind) {
2411 case EXPR_UNARY_NEGATE: {
2412 ir_node *value_node = expression_to_firm(value);
2413 ir_mode *mode = get_ir_mode_arithmetic(type);
2414 return new_d_Minus(dbgi, value_node, mode);
2416 case EXPR_UNARY_PLUS:
2417 return expression_to_firm(value);
2418 case EXPR_UNARY_BITWISE_NEGATE: {
2419 ir_node *value_node = expression_to_firm(value);
2420 ir_mode *mode = get_ir_mode_arithmetic(type);
2421 return new_d_Not(dbgi, value_node, mode);
2423 case EXPR_UNARY_NOT: {
2424 ir_node *value_node = _expression_to_firm(value);
2425 value_node = create_conv(dbgi, value_node, mode_b);
2426 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2429 case EXPR_UNARY_DEREFERENCE: {
2430 ir_node *value_node = expression_to_firm(value);
2431 type_t *value_type = skip_typeref(value->base.type);
2432 assert(is_type_pointer(value_type));
2434 /* check for __based */
2435 const variable_t *const base_var = value_type->pointer.base_variable;
2436 if (base_var != NULL) {
2437 ir_node *const addr = get_global_var_address(dbgi, base_var);
2438 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2439 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2441 type_t *points_to = value_type->pointer.points_to;
2442 return deref_address(dbgi, points_to, value_node);
2444 case EXPR_UNARY_POSTFIX_INCREMENT:
2445 case EXPR_UNARY_POSTFIX_DECREMENT:
2446 case EXPR_UNARY_PREFIX_INCREMENT:
2447 case EXPR_UNARY_PREFIX_DECREMENT:
2448 return create_incdec(expression);
2449 case EXPR_UNARY_CAST: {
2450 ir_node *value_node = expression_to_firm(value);
2451 if (is_type_scalar(type)) {
2452 ir_mode *mode = get_ir_mode_storage(type);
2453 type_t *from_type = skip_typeref(value->base.type);
2454 /* check for conversion from / to __based types */
2455 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2456 const variable_t *from_var = from_type->pointer.base_variable;
2457 const variable_t *to_var = type->pointer.base_variable;
2458 if (from_var != to_var) {
2459 if (from_var != NULL) {
2460 ir_node *const addr = get_global_var_address(dbgi, from_var);
2461 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2462 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2464 if (to_var != NULL) {
2465 ir_node *const addr = get_global_var_address(dbgi, to_var);
2466 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2467 value_node = new_d_Sub(dbgi, value_node, base, mode);
2471 ir_node *node = create_conv(dbgi, value_node, mode);
2472 node = do_strict_conv(dbgi, node);
2473 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2474 node = create_conv(dbgi, node, mode_arith);
2477 /* make sure firm type is constructed */
2478 (void) get_ir_type(type);
2482 case EXPR_UNARY_CAST_IMPLICIT: {
2483 ir_node *value_node = expression_to_firm(value);
2484 if (is_type_scalar(type)) {
2485 ir_mode *mode = get_ir_mode_storage(type);
2486 ir_node *res = create_conv(dbgi, value_node, mode);
2487 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2488 res = create_conv(dbgi, res, mode_arith);
2494 case EXPR_UNARY_ASSUME:
2495 if (firm_opt.confirm)
2496 return handle_assume(dbgi, value);
2503 panic("invalid UNEXPR type found");
2507 * produces a 0/1 depending of the value of a mode_b node
2509 static ir_node *produce_condition_result(const expression_t *expression,
2510 ir_mode *mode, dbg_info *dbgi)
2512 ir_node *cur_block = get_cur_block();
2514 ir_node *one_block = new_immBlock();
2515 set_cur_block(one_block);
2516 ir_node *one = new_Const(get_mode_one(mode));
2517 ir_node *jmp_one = new_d_Jmp(dbgi);
2519 ir_node *zero_block = new_immBlock();
2520 set_cur_block(zero_block);
2521 ir_node *zero = new_Const(get_mode_null(mode));
2522 ir_node *jmp_zero = new_d_Jmp(dbgi);
2524 set_cur_block(cur_block);
2525 create_condition_evaluation(expression, one_block, zero_block);
2526 mature_immBlock(one_block);
2527 mature_immBlock(zero_block);
2529 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2530 new_Block(2, in_cf);
2532 ir_node *in[2] = { one, zero };
2533 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2538 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2539 ir_node *value, type_t *type)
2541 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2542 assert(is_type_pointer(type));
2543 pointer_type_t *const pointer_type = &type->pointer;
2544 type_t *const points_to = skip_typeref(pointer_type->points_to);
2545 ir_node * elem_size = get_type_size_node(points_to);
2546 elem_size = create_conv(dbgi, elem_size, mode);
2547 value = create_conv(dbgi, value, mode);
2548 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2552 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2553 ir_node *left, ir_node *right)
2556 type_t *type_left = skip_typeref(expression->left->base.type);
2557 type_t *type_right = skip_typeref(expression->right->base.type);
2559 expression_kind_t kind = expression->base.kind;
2562 case EXPR_BINARY_SHIFTLEFT:
2563 case EXPR_BINARY_SHIFTRIGHT:
2564 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2565 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2566 mode = get_irn_mode(left);
2567 right = create_conv(dbgi, right, mode_uint);
2570 case EXPR_BINARY_SUB:
2571 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2572 const pointer_type_t *const ptr_type = &type_left->pointer;
2574 mode = get_ir_mode_arithmetic(expression->base.type);
2575 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2576 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2577 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2578 ir_node *const no_mem = new_NoMem();
2579 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2580 mode, op_pin_state_floats);
2581 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2584 case EXPR_BINARY_SUB_ASSIGN:
2585 if (is_type_pointer(type_left)) {
2586 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2587 mode = get_ir_mode_arithmetic(type_left);
2592 case EXPR_BINARY_ADD:
2593 case EXPR_BINARY_ADD_ASSIGN:
2594 if (is_type_pointer(type_left)) {
2595 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2596 mode = get_ir_mode_arithmetic(type_left);
2598 } else if (is_type_pointer(type_right)) {
2599 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2600 mode = get_ir_mode_arithmetic(type_right);
2607 mode = get_ir_mode_arithmetic(type_right);
2608 left = create_conv(dbgi, left, mode);
2613 case EXPR_BINARY_ADD_ASSIGN:
2614 case EXPR_BINARY_ADD:
2615 return new_d_Add(dbgi, left, right, mode);
2616 case EXPR_BINARY_SUB_ASSIGN:
2617 case EXPR_BINARY_SUB:
2618 return new_d_Sub(dbgi, left, right, mode);
2619 case EXPR_BINARY_MUL_ASSIGN:
2620 case EXPR_BINARY_MUL:
2621 return new_d_Mul(dbgi, left, right, mode);
2622 case EXPR_BINARY_BITWISE_AND:
2623 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2624 return new_d_And(dbgi, left, right, mode);
2625 case EXPR_BINARY_BITWISE_OR:
2626 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2627 return new_d_Or(dbgi, left, right, mode);
2628 case EXPR_BINARY_BITWISE_XOR:
2629 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2630 return new_d_Eor(dbgi, left, right, mode);
2631 case EXPR_BINARY_SHIFTLEFT:
2632 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2633 return new_d_Shl(dbgi, left, right, mode);
2634 case EXPR_BINARY_SHIFTRIGHT:
2635 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2636 if (mode_is_signed(mode)) {
2637 return new_d_Shrs(dbgi, left, right, mode);
2639 return new_d_Shr(dbgi, left, right, mode);
2641 case EXPR_BINARY_DIV:
2642 case EXPR_BINARY_DIV_ASSIGN: {
2643 ir_node *pin = new_Pin(new_NoMem());
2646 if (mode_is_float(mode)) {
2647 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2648 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2650 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2651 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2655 case EXPR_BINARY_MOD:
2656 case EXPR_BINARY_MOD_ASSIGN: {
2657 ir_node *pin = new_Pin(new_NoMem());
2658 assert(!mode_is_float(mode));
2659 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2660 op_pin_state_floats);
2661 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2665 panic("unexpected expression kind");
2669 static ir_node *create_lazy_op(const binary_expression_t *expression)
2671 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2672 type_t *type = skip_typeref(expression->base.type);
2673 ir_mode *mode = get_ir_mode_arithmetic(type);
2675 if (is_constant_expression(expression->left)) {
2676 bool val = fold_constant_to_bool(expression->left);
2677 expression_kind_t ekind = expression->base.kind;
2678 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2679 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2681 return new_Const(get_mode_null(mode));
2685 return new_Const(get_mode_one(mode));
2689 if (is_constant_expression(expression->right)) {
2690 bool valr = fold_constant_to_bool(expression->right);
2692 new_Const(get_mode_one(mode)) :
2693 new_Const(get_mode_null(mode));
2696 return produce_condition_result(expression->right, mode, dbgi);
2699 return produce_condition_result((const expression_t*) expression, mode,
2703 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2704 ir_node *right, ir_mode *mode);
2706 static ir_node *create_assign_binop(const binary_expression_t *expression)
2708 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2709 const expression_t *left_expr = expression->left;
2710 type_t *type = skip_typeref(left_expr->base.type);
2711 ir_mode *left_mode = get_ir_mode_storage(type);
2712 ir_node *right = expression_to_firm(expression->right);
2713 ir_node *left_addr = expression_to_addr(left_expr);
2714 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2715 ir_node *result = create_op(dbgi, expression, left, right);
2717 result = create_conv(dbgi, result, left_mode);
2718 result = do_strict_conv(dbgi, result);
2720 result = set_value_for_expression_addr(left_expr, result, left_addr);
2722 if (!is_type_compound(type)) {
2723 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2724 result = create_conv(dbgi, result, mode_arithmetic);
2729 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2731 expression_kind_t kind = expression->base.kind;
2734 case EXPR_BINARY_EQUAL:
2735 case EXPR_BINARY_NOTEQUAL:
2736 case EXPR_BINARY_LESS:
2737 case EXPR_BINARY_LESSEQUAL:
2738 case EXPR_BINARY_GREATER:
2739 case EXPR_BINARY_GREATEREQUAL:
2740 case EXPR_BINARY_ISGREATER:
2741 case EXPR_BINARY_ISGREATEREQUAL:
2742 case EXPR_BINARY_ISLESS:
2743 case EXPR_BINARY_ISLESSEQUAL:
2744 case EXPR_BINARY_ISLESSGREATER:
2745 case EXPR_BINARY_ISUNORDERED: {
2746 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2747 ir_node *left = expression_to_firm(expression->left);
2748 ir_node *right = expression_to_firm(expression->right);
2749 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2750 long pnc = get_pnc(kind, expression->left->base.type);
2751 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2754 case EXPR_BINARY_ASSIGN: {
2755 ir_node *addr = expression_to_addr(expression->left);
2756 ir_node *right = expression_to_firm(expression->right);
2758 = set_value_for_expression_addr(expression->left, right, addr);
2760 type_t *type = skip_typeref(expression->base.type);
2761 if (!is_type_compound(type)) {
2762 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2763 res = create_conv(NULL, res, mode_arithmetic);
2767 case EXPR_BINARY_ADD:
2768 case EXPR_BINARY_SUB:
2769 case EXPR_BINARY_MUL:
2770 case EXPR_BINARY_DIV:
2771 case EXPR_BINARY_MOD:
2772 case EXPR_BINARY_BITWISE_AND:
2773 case EXPR_BINARY_BITWISE_OR:
2774 case EXPR_BINARY_BITWISE_XOR:
2775 case EXPR_BINARY_SHIFTLEFT:
2776 case EXPR_BINARY_SHIFTRIGHT:
2778 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2779 ir_node *left = expression_to_firm(expression->left);
2780 ir_node *right = expression_to_firm(expression->right);
2781 return create_op(dbgi, expression, left, right);
2783 case EXPR_BINARY_LOGICAL_AND:
2784 case EXPR_BINARY_LOGICAL_OR:
2785 return create_lazy_op(expression);
2786 case EXPR_BINARY_COMMA:
2787 /* create side effects of left side */
2788 (void) expression_to_firm(expression->left);
2789 return _expression_to_firm(expression->right);
2791 case EXPR_BINARY_ADD_ASSIGN:
2792 case EXPR_BINARY_SUB_ASSIGN:
2793 case EXPR_BINARY_MUL_ASSIGN:
2794 case EXPR_BINARY_MOD_ASSIGN:
2795 case EXPR_BINARY_DIV_ASSIGN:
2796 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2797 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2798 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2799 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2800 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2801 return create_assign_binop(expression);
2803 panic("TODO binexpr type");
2807 static ir_node *array_access_addr(const array_access_expression_t *expression)
2809 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2810 ir_node *base_addr = expression_to_firm(expression->array_ref);
2811 ir_node *offset = expression_to_firm(expression->index);
2812 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2813 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2814 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2819 static ir_node *array_access_to_firm(
2820 const array_access_expression_t *expression)
2822 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2823 ir_node *addr = array_access_addr(expression);
2824 type_t *type = revert_automatic_type_conversion(
2825 (const expression_t*) expression);
2826 type = skip_typeref(type);
2828 return deref_address(dbgi, type, addr);
2831 static long get_offsetof_offset(const offsetof_expression_t *expression)
2833 type_t *orig_type = expression->type;
2836 designator_t *designator = expression->designator;
2837 for ( ; designator != NULL; designator = designator->next) {
2838 type_t *type = skip_typeref(orig_type);
2839 /* be sure the type is constructed */
2840 (void) get_ir_type(type);
2842 if (designator->symbol != NULL) {
2843 assert(is_type_compound(type));
2844 symbol_t *symbol = designator->symbol;
2846 compound_t *compound = type->compound.compound;
2847 entity_t *iter = compound->members.entities;
2848 for ( ; iter != NULL; iter = iter->base.next) {
2849 if (iter->base.symbol == symbol) {
2853 assert(iter != NULL);
2855 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2856 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2857 offset += get_entity_offset(iter->compound_member.entity);
2859 orig_type = iter->declaration.type;
2861 expression_t *array_index = designator->array_index;
2862 assert(designator->array_index != NULL);
2863 assert(is_type_array(type));
2865 long index = fold_constant_to_int(array_index);
2866 ir_type *arr_type = get_ir_type(type);
2867 ir_type *elem_type = get_array_element_type(arr_type);
2868 long elem_size = get_type_size_bytes(elem_type);
2870 offset += index * elem_size;
2872 orig_type = type->array.element_type;
2879 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2881 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2882 long offset = get_offsetof_offset(expression);
2883 tarval *tv = new_tarval_from_long(offset, mode);
2884 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2886 return new_d_Const(dbgi, tv);
2889 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2890 ir_entity *entity, type_t *type);
2892 static ir_node *compound_literal_to_firm(
2893 const compound_literal_expression_t *expression)
2895 type_t *type = expression->type;
2897 /* create an entity on the stack */
2898 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2900 ident *const id = id_unique("CompLit.%u");
2901 ir_type *const irtype = get_ir_type(type);
2902 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2903 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2904 set_entity_ld_ident(entity, id);
2906 set_entity_variability(entity, variability_uninitialized);
2908 /* create initialisation code */
2909 initializer_t *initializer = expression->initializer;
2910 create_local_initializer(initializer, dbgi, entity, type);
2912 /* create a sel for the compound literal address */
2913 ir_node *frame = get_irg_frame(current_ir_graph);
2914 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2919 * Transform a sizeof expression into Firm code.
2921 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2923 type_t *type = expression->type;
2925 type = expression->tp_expression->base.type;
2926 assert(type != NULL);
2929 type = skip_typeref(type);
2930 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
2931 if (is_type_array(type) && type->array.is_vla
2932 && expression->tp_expression != NULL) {
2933 expression_to_firm(expression->tp_expression);
2936 return get_type_size_node(type);
2939 static entity_t *get_expression_entity(const expression_t *expression)
2941 if (expression->kind != EXPR_REFERENCE)
2944 return expression->reference.entity;
2947 static unsigned get_cparser_entity_alignment(const entity_t *entity)
2949 switch(entity->kind) {
2950 DECLARATION_KIND_CASES
2951 return entity->declaration.alignment;
2954 return entity->compound.alignment;
2955 case ENTITY_TYPEDEF:
2956 return entity->typedefe.alignment;
2964 * Transform an alignof expression into Firm code.
2966 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2968 unsigned alignment = 0;
2970 const expression_t *tp_expression = expression->tp_expression;
2971 if (tp_expression != NULL) {
2972 entity_t *entity = get_expression_entity(tp_expression);
2973 if (entity != NULL) {
2974 alignment = get_cparser_entity_alignment(entity);
2978 if (alignment == 0) {
2979 type_t *type = expression->type;
2980 alignment = get_type_alignment(type);
2983 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2984 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2985 tarval *tv = new_tarval_from_long(alignment, mode);
2986 return new_d_Const(dbgi, tv);
2989 static void init_ir_types(void);
2991 static tarval *fold_constant_to_tarval(const expression_t *expression)
2993 assert(is_type_valid(skip_typeref(expression->base.type)));
2995 bool constant_folding_old = constant_folding;
2996 constant_folding = true;
3000 assert(is_constant_expression(expression));
3002 ir_graph *old_current_ir_graph = current_ir_graph;
3003 current_ir_graph = get_const_code_irg();
3005 ir_node *cnst = expression_to_firm(expression);
3006 current_ir_graph = old_current_ir_graph;
3008 if (!is_Const(cnst)) {
3009 panic("couldn't fold constant");
3012 constant_folding = constant_folding_old;
3014 tarval *tv = get_Const_tarval(cnst);
3018 long fold_constant_to_int(const expression_t *expression)
3020 if (expression->kind == EXPR_INVALID)
3023 tarval *tv = fold_constant_to_tarval(expression);
3024 if (!tarval_is_long(tv)) {
3025 panic("result of constant folding is not integer");
3028 return get_tarval_long(tv);
3031 bool fold_constant_to_bool(const expression_t *expression)
3033 if (expression->kind == EXPR_INVALID)
3035 tarval *tv = fold_constant_to_tarval(expression);
3036 return !tarval_is_null(tv);
3039 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3041 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3043 /* first try to fold a constant condition */
3044 if (is_constant_expression(expression->condition)) {
3045 bool val = fold_constant_to_bool(expression->condition);
3047 expression_t *true_expression = expression->true_expression;
3048 if (true_expression == NULL)
3049 true_expression = expression->condition;
3050 return expression_to_firm(true_expression);
3052 return expression_to_firm(expression->false_expression);
3056 ir_node *cur_block = get_cur_block();
3058 /* create the true block */
3059 ir_node *true_block = new_immBlock();
3060 set_cur_block(true_block);
3062 ir_node *true_val = expression->true_expression != NULL ?
3063 expression_to_firm(expression->true_expression) : NULL;
3064 ir_node *true_jmp = new_Jmp();
3066 /* create the false block */
3067 ir_node *false_block = new_immBlock();
3068 set_cur_block(false_block);
3070 ir_node *false_val = expression_to_firm(expression->false_expression);
3071 ir_node *false_jmp = new_Jmp();
3073 /* create the condition evaluation */
3074 set_cur_block(cur_block);
3075 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3076 if (expression->true_expression == NULL) {
3077 if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3078 true_val = cond_expr;
3080 /* Condition ended with a short circuit (&&, ||, !) operation or a
3081 * comparison. Generate a "1" as value for the true branch. */
3082 true_val = new_Const(get_mode_one(mode_Is));
3085 mature_immBlock(true_block);
3086 mature_immBlock(false_block);
3088 /* create the common block */
3089 ir_node *in_cf[2] = { true_jmp, false_jmp };
3090 new_Block(2, in_cf);
3092 /* TODO improve static semantics, so either both or no values are NULL */
3093 if (true_val == NULL || false_val == NULL)
3096 ir_node *in[2] = { true_val, false_val };
3097 ir_mode *mode = get_irn_mode(true_val);
3098 assert(get_irn_mode(false_val) == mode);
3099 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
3105 * Returns an IR-node representing the address of a field.
3107 static ir_node *select_addr(const select_expression_t *expression)
3109 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3111 construct_select_compound(expression);
3113 ir_node *compound_addr = expression_to_firm(expression->compound);
3115 entity_t *entry = expression->compound_entry;
3116 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3117 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3119 if (constant_folding) {
3120 ir_mode *mode = get_irn_mode(compound_addr);
3121 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3122 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3123 return new_d_Add(dbgi, compound_addr, ofs, mode);
3125 ir_entity *irentity = entry->compound_member.entity;
3126 assert(irentity != NULL);
3127 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3131 static ir_node *select_to_firm(const select_expression_t *expression)
3133 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3134 ir_node *addr = select_addr(expression);
3135 type_t *type = revert_automatic_type_conversion(
3136 (const expression_t*) expression);
3137 type = skip_typeref(type);
3139 entity_t *entry = expression->compound_entry;
3140 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3141 type_t *entry_type = skip_typeref(entry->declaration.type);
3143 if (entry_type->kind == TYPE_BITFIELD) {
3144 return bitfield_extract_to_firm(expression, addr);
3147 return deref_address(dbgi, type, addr);
3150 /* Values returned by __builtin_classify_type. */
3151 typedef enum gcc_type_class
3157 enumeral_type_class,
3160 reference_type_class,
3164 function_type_class,
3175 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3177 type_t *type = expr->type_expression->base.type;
3179 /* FIXME gcc returns different values depending on whether compiling C or C++
3180 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3183 type = skip_typeref(type);
3184 switch (type->kind) {
3186 const atomic_type_t *const atomic_type = &type->atomic;
3187 switch (atomic_type->akind) {
3188 /* should not be reached */
3189 case ATOMIC_TYPE_INVALID:
3193 /* gcc cannot do that */
3194 case ATOMIC_TYPE_VOID:
3195 tc = void_type_class;
3198 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3199 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3200 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3201 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3202 case ATOMIC_TYPE_SHORT:
3203 case ATOMIC_TYPE_USHORT:
3204 case ATOMIC_TYPE_INT:
3205 case ATOMIC_TYPE_UINT:
3206 case ATOMIC_TYPE_LONG:
3207 case ATOMIC_TYPE_ULONG:
3208 case ATOMIC_TYPE_LONGLONG:
3209 case ATOMIC_TYPE_ULONGLONG:
3210 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3211 tc = integer_type_class;
3214 case ATOMIC_TYPE_FLOAT:
3215 case ATOMIC_TYPE_DOUBLE:
3216 case ATOMIC_TYPE_LONG_DOUBLE:
3217 tc = real_type_class;
3220 panic("Unexpected atomic type in classify_type_to_firm().");
3223 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3224 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3225 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3226 case TYPE_ARRAY: /* gcc handles this as pointer */
3227 case TYPE_FUNCTION: /* gcc handles this as pointer */
3228 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3229 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3230 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3232 /* gcc handles this as integer */
3233 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3235 /* gcc classifies the referenced type */
3236 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3239 /* typedef/typeof should be skipped already */
3246 panic("unexpected TYPE classify_type_to_firm().");
3250 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3251 tarval *const tv = new_tarval_from_long(tc, mode_int);
3252 return new_d_Const(dbgi, tv);
3255 static ir_node *function_name_to_firm(
3256 const funcname_expression_t *const expr)
3258 switch(expr->kind) {
3259 case FUNCNAME_FUNCTION:
3260 case FUNCNAME_PRETTY_FUNCTION:
3261 case FUNCNAME_FUNCDNAME:
3262 if (current_function_name == NULL) {
3263 const source_position_t *const src_pos = &expr->base.source_position;
3264 const char *name = current_function_entity->base.symbol->string;
3265 const string_t string = { name, strlen(name) + 1 };
3266 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3268 return current_function_name;
3269 case FUNCNAME_FUNCSIG:
3270 if (current_funcsig == NULL) {
3271 const source_position_t *const src_pos = &expr->base.source_position;
3272 ir_entity *ent = get_irg_entity(current_ir_graph);
3273 const char *const name = get_entity_ld_name(ent);
3274 const string_t string = { name, strlen(name) + 1 };
3275 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3277 return current_funcsig;
3279 panic("Unsupported function name");
3282 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3284 statement_t *statement = expr->statement;
3286 assert(statement->kind == STATEMENT_COMPOUND);
3287 return compound_statement_to_firm(&statement->compound);
3290 static ir_node *va_start_expression_to_firm(
3291 const va_start_expression_t *const expr)
3293 type_t *const type = current_function_entity->declaration.type;
3294 ir_type *const method_type = get_ir_type(type);
3295 int const n = get_method_n_params(method_type) - 1;
3296 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3297 ir_node *const frame = get_irg_frame(current_ir_graph);
3298 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3299 ir_node *const no_mem = new_NoMem();
3300 ir_node *const arg_sel =
3301 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3303 type_t *const param_type = expr->parameter->base.type;
3304 ir_node *const cnst = get_type_size_node(param_type);
3305 ir_mode *const mode = get_irn_mode(cnst);
3306 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3307 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3308 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3309 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3310 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3311 set_value_for_expression(expr->ap, add);
3316 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3318 type_t *const type = expr->base.type;
3319 expression_t *const ap_expr = expr->ap;
3320 ir_node *const ap_addr = expression_to_addr(ap_expr);
3321 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3322 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3323 ir_node *const res = deref_address(dbgi, type, ap);
3325 ir_node *const cnst = get_type_size_node(expr->base.type);
3326 ir_mode *const mode = get_irn_mode(cnst);
3327 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3328 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3329 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3330 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3331 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3333 set_value_for_expression_addr(ap_expr, add, ap_addr);
3339 * Generate Firm for a va_copy expression.
3341 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3343 ir_node *const src = expression_to_firm(expr->src);
3344 set_value_for_expression(expr->dst, src);
3348 static ir_node *dereference_addr(const unary_expression_t *const expression)
3350 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3351 return expression_to_firm(expression->value);
3355 * Returns a IR-node representing an lvalue of the given expression.
3357 static ir_node *expression_to_addr(const expression_t *expression)
3359 switch(expression->kind) {
3360 case EXPR_ARRAY_ACCESS:
3361 return array_access_addr(&expression->array_access);
3363 return call_expression_to_firm(&expression->call);
3364 case EXPR_COMPOUND_LITERAL:
3365 return compound_literal_to_firm(&expression->compound_literal);
3366 case EXPR_REFERENCE:
3367 return reference_addr(&expression->reference);
3369 return select_addr(&expression->select);
3370 case EXPR_UNARY_DEREFERENCE:
3371 return dereference_addr(&expression->unary);
3375 panic("trying to get address of non-lvalue");
3378 static ir_node *builtin_constant_to_firm(
3379 const builtin_constant_expression_t *expression)
3381 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3384 if (is_constant_expression(expression->value)) {
3389 return new_Const_long(mode, v);
3392 static ir_node *builtin_types_compatible_to_firm(
3393 const builtin_types_compatible_expression_t *expression)
3395 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3396 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3397 long const value = types_compatible(left, right) ? 1 : 0;
3398 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3399 return new_Const_long(mode, value);
3402 static ir_node *get_label_block(label_t *label)
3404 if (label->block != NULL)
3405 return label->block;
3407 /* beware: might be called from create initializer with current_ir_graph
3408 * set to const_code_irg. */
3409 ir_graph *rem = current_ir_graph;
3410 current_ir_graph = current_function;
3412 ir_node *block = new_immBlock();
3414 label->block = block;
3416 ARR_APP1(label_t *, all_labels, label);
3418 current_ir_graph = rem;
3423 * Pointer to a label. This is used for the
3424 * GNU address-of-label extension.
3426 static ir_node *label_address_to_firm(
3427 const label_address_expression_t *label)
3429 ir_node *block = get_label_block(label->label);
3430 ir_label_t nr = get_Block_label(block);
3433 nr = get_irp_next_label_nr();
3434 set_Block_label(block, nr);
3436 symconst_symbol value;
3438 return new_SymConst(mode_P_code, value, symconst_label);
3442 * creates firm nodes for an expression. The difference between this function
3443 * and expression_to_firm is, that this version might produce mode_b nodes
3444 * instead of mode_Is.
3446 static ir_node *_expression_to_firm(const expression_t *expression)
3449 if (!constant_folding) {
3450 assert(!expression->base.transformed);
3451 ((expression_t*) expression)->base.transformed = true;
3455 switch (expression->kind) {
3456 case EXPR_CHARACTER_CONSTANT:
3457 return character_constant_to_firm(&expression->conste);
3458 case EXPR_WIDE_CHARACTER_CONSTANT:
3459 return wide_character_constant_to_firm(&expression->conste);
3461 return const_to_firm(&expression->conste);
3462 case EXPR_STRING_LITERAL:
3463 return string_literal_to_firm(&expression->string);
3464 case EXPR_WIDE_STRING_LITERAL:
3465 return wide_string_literal_to_firm(&expression->wide_string);
3466 case EXPR_REFERENCE:
3467 return reference_expression_to_firm(&expression->reference);
3468 case EXPR_REFERENCE_ENUM_VALUE:
3469 return reference_expression_enum_value_to_firm(&expression->reference);
3471 return call_expression_to_firm(&expression->call);
3473 return unary_expression_to_firm(&expression->unary);
3475 return binary_expression_to_firm(&expression->binary);
3476 case EXPR_ARRAY_ACCESS:
3477 return array_access_to_firm(&expression->array_access);
3479 return sizeof_to_firm(&expression->typeprop);
3481 return alignof_to_firm(&expression->typeprop);
3482 case EXPR_CONDITIONAL:
3483 return conditional_to_firm(&expression->conditional);
3485 return select_to_firm(&expression->select);
3486 case EXPR_CLASSIFY_TYPE:
3487 return classify_type_to_firm(&expression->classify_type);
3489 return function_name_to_firm(&expression->funcname);
3490 case EXPR_STATEMENT:
3491 return statement_expression_to_firm(&expression->statement);
3493 return va_start_expression_to_firm(&expression->va_starte);
3495 return va_arg_expression_to_firm(&expression->va_arge);
3497 return va_copy_expression_to_firm(&expression->va_copye);
3498 case EXPR_BUILTIN_CONSTANT_P:
3499 return builtin_constant_to_firm(&expression->builtin_constant);
3500 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3501 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3503 return offsetof_to_firm(&expression->offsetofe);
3504 case EXPR_COMPOUND_LITERAL:
3505 return compound_literal_to_firm(&expression->compound_literal);
3506 case EXPR_LABEL_ADDRESS:
3507 return label_address_to_firm(&expression->label_address);
3513 panic("invalid expression found");
3517 * Check if a given expression is a GNU __builtin_expect() call.
3519 static bool is_builtin_expect(const expression_t *expression)
3521 if (expression->kind != EXPR_CALL)
3524 expression_t *function = expression->call.function;
3525 if (function->kind != EXPR_REFERENCE)
3527 reference_expression_t *ref = &function->reference;
3528 if (ref->entity->kind != ENTITY_FUNCTION ||
3529 ref->entity->function.btk != bk_gnu_builtin_expect)
3535 static bool produces_mode_b(const expression_t *expression)
3537 switch (expression->kind) {
3538 case EXPR_BINARY_EQUAL:
3539 case EXPR_BINARY_NOTEQUAL:
3540 case EXPR_BINARY_LESS:
3541 case EXPR_BINARY_LESSEQUAL:
3542 case EXPR_BINARY_GREATER:
3543 case EXPR_BINARY_GREATEREQUAL:
3544 case EXPR_BINARY_ISGREATER:
3545 case EXPR_BINARY_ISGREATEREQUAL:
3546 case EXPR_BINARY_ISLESS:
3547 case EXPR_BINARY_ISLESSEQUAL:
3548 case EXPR_BINARY_ISLESSGREATER:
3549 case EXPR_BINARY_ISUNORDERED:
3550 case EXPR_UNARY_NOT:
3554 if (is_builtin_expect(expression)) {
3555 expression_t *argument = expression->call.arguments->expression;
3556 return produces_mode_b(argument);
3559 case EXPR_BINARY_COMMA:
3560 return produces_mode_b(expression->binary.right);
3567 static ir_node *expression_to_firm(const expression_t *expression)
3569 if (!produces_mode_b(expression)) {
3570 ir_node *res = _expression_to_firm(expression);
3571 assert(res == NULL || get_irn_mode(res) != mode_b);
3575 if (is_constant_expression(expression)) {
3576 ir_node *res = _expression_to_firm(expression);
3577 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3578 assert(is_Const(res));
3579 if (is_Const_null(res)) {
3580 return new_Const_long(mode, 0);
3582 return new_Const_long(mode, 1);
3586 /* we have to produce a 0/1 from the mode_b expression */
3587 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3588 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3589 return produce_condition_result(expression, mode, dbgi);
3593 * create a short-circuit expression evaluation that tries to construct
3594 * efficient control flow structures for &&, || and ! expressions
3596 static ir_node *create_condition_evaluation(const expression_t *expression,
3597 ir_node *true_block,
3598 ir_node *false_block)
3600 switch(expression->kind) {
3601 case EXPR_UNARY_NOT: {
3602 const unary_expression_t *unary_expression = &expression->unary;
3603 create_condition_evaluation(unary_expression->value, false_block,
3607 case EXPR_BINARY_LOGICAL_AND: {
3608 const binary_expression_t *binary_expression = &expression->binary;
3610 ir_node *extra_block = new_immBlock();
3611 create_condition_evaluation(binary_expression->left, extra_block,
3613 mature_immBlock(extra_block);
3614 set_cur_block(extra_block);
3615 create_condition_evaluation(binary_expression->right, true_block,
3619 case EXPR_BINARY_LOGICAL_OR: {
3620 const binary_expression_t *binary_expression = &expression->binary;
3622 ir_node *extra_block = new_immBlock();
3623 create_condition_evaluation(binary_expression->left, true_block,
3625 mature_immBlock(extra_block);
3626 set_cur_block(extra_block);
3627 create_condition_evaluation(binary_expression->right, true_block,
3635 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3636 ir_node *cond_expr = _expression_to_firm(expression);
3637 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3638 ir_node *cond = new_d_Cond(dbgi, condition);
3639 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3640 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3642 /* set branch prediction info based on __builtin_expect */
3643 if (is_builtin_expect(expression) && is_Cond(cond)) {
3644 call_argument_t *argument = expression->call.arguments->next;
3645 if (is_constant_expression(argument->expression)) {
3646 bool cnst = fold_constant_to_bool(argument->expression);
3647 cond_jmp_predicate pred;
3649 if (cnst == false) {
3650 pred = COND_JMP_PRED_FALSE;
3652 pred = COND_JMP_PRED_TRUE;
3654 set_Cond_jmp_pred(cond, pred);
3658 add_immBlock_pred(true_block, true_proj);
3659 add_immBlock_pred(false_block, false_proj);
3661 set_cur_block(NULL);
3665 static void create_variable_entity(entity_t *variable,
3666 declaration_kind_t declaration_kind,
3667 ir_type *parent_type)
3669 assert(variable->kind == ENTITY_VARIABLE);
3670 type_t *type = skip_typeref(variable->declaration.type);
3672 ident *const id = new_id_from_str(variable->base.symbol->string);
3673 ir_type *const irtype = get_ir_type(type);
3674 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3675 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3676 unsigned alignment = variable->declaration.alignment;
3678 set_entity_alignment(irentity, alignment);
3680 handle_decl_modifiers(irentity, variable);
3682 variable->declaration.kind = (unsigned char) declaration_kind;
3683 variable->variable.v.entity = irentity;
3684 set_entity_variability(irentity, variability_uninitialized);
3685 set_entity_ld_ident(irentity, create_ld_ident(variable));
3687 if (parent_type == get_tls_type())
3688 set_entity_allocation(irentity, allocation_automatic);
3689 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3690 set_entity_allocation(irentity, allocation_static);
3692 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3693 set_entity_volatility(irentity, volatility_is_volatile);
3698 typedef struct type_path_entry_t type_path_entry_t;
3699 struct type_path_entry_t {
3701 ir_initializer_t *initializer;
3703 entity_t *compound_entry;
3706 typedef struct type_path_t type_path_t;
3707 struct type_path_t {
3708 type_path_entry_t *path;
3713 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3715 size_t len = ARR_LEN(path->path);
3717 for (size_t i = 0; i < len; ++i) {
3718 const type_path_entry_t *entry = & path->path[i];
3720 type_t *type = skip_typeref(entry->type);
3721 if (is_type_compound(type)) {
3722 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3723 } else if (is_type_array(type)) {
3724 fprintf(stderr, "[%u]", (unsigned) entry->index);
3726 fprintf(stderr, "-INVALID-");
3729 fprintf(stderr, " (");
3730 print_type(path->top_type);
3731 fprintf(stderr, ")");
3734 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3736 size_t len = ARR_LEN(path->path);
3738 return & path->path[len-1];
3741 static type_path_entry_t *append_to_type_path(type_path_t *path)
3743 size_t len = ARR_LEN(path->path);
3744 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3746 type_path_entry_t *result = & path->path[len];
3747 memset(result, 0, sizeof(result[0]));
3751 static size_t get_compound_member_count(const compound_type_t *type)
3753 compound_t *compound = type->compound;
3754 size_t n_members = 0;
3755 entity_t *member = compound->members.entities;
3756 for ( ; member != NULL; member = member->base.next) {
3763 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3765 type_t *orig_top_type = path->top_type;
3766 type_t *top_type = skip_typeref(orig_top_type);
3768 assert(is_type_compound(top_type) || is_type_array(top_type));
3770 if (ARR_LEN(path->path) == 0) {
3773 type_path_entry_t *top = get_type_path_top(path);
3774 ir_initializer_t *initializer = top->initializer;
3775 return get_initializer_compound_value(initializer, top->index);
3779 static void descend_into_subtype(type_path_t *path)
3781 type_t *orig_top_type = path->top_type;
3782 type_t *top_type = skip_typeref(orig_top_type);
3784 assert(is_type_compound(top_type) || is_type_array(top_type));
3786 ir_initializer_t *initializer = get_initializer_entry(path);
3788 type_path_entry_t *top = append_to_type_path(path);
3789 top->type = top_type;
3793 if (is_type_compound(top_type)) {
3794 compound_t *compound = top_type->compound.compound;
3795 entity_t *entry = compound->members.entities;
3797 top->compound_entry = entry;
3799 len = get_compound_member_count(&top_type->compound);
3800 if (entry != NULL) {
3801 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3802 path->top_type = entry->declaration.type;
3805 assert(is_type_array(top_type));
3806 assert(top_type->array.size > 0);
3809 path->top_type = top_type->array.element_type;
3810 len = top_type->array.size;
3812 if (initializer == NULL
3813 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3814 initializer = create_initializer_compound(len);
3815 /* we have to set the entry at the 2nd latest path entry... */
3816 size_t path_len = ARR_LEN(path->path);
3817 assert(path_len >= 1);
3819 type_path_entry_t *entry = & path->path[path_len-2];
3820 ir_initializer_t *tinitializer = entry->initializer;
3821 set_initializer_compound_value(tinitializer, entry->index,
3825 top->initializer = initializer;
3828 static void ascend_from_subtype(type_path_t *path)
3830 type_path_entry_t *top = get_type_path_top(path);
3832 path->top_type = top->type;
3834 size_t len = ARR_LEN(path->path);
3835 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3838 static void walk_designator(type_path_t *path, const designator_t *designator)
3840 /* designators start at current object type */
3841 ARR_RESIZE(type_path_entry_t, path->path, 1);
3843 for ( ; designator != NULL; designator = designator->next) {
3844 type_path_entry_t *top = get_type_path_top(path);
3845 type_t *orig_type = top->type;
3846 type_t *type = skip_typeref(orig_type);
3848 if (designator->symbol != NULL) {
3849 assert(is_type_compound(type));
3851 symbol_t *symbol = designator->symbol;
3853 compound_t *compound = type->compound.compound;
3854 entity_t *iter = compound->members.entities;
3855 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3856 if (iter->base.symbol == symbol) {
3857 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3861 assert(iter != NULL);
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 top->compound_entry = NULL;
3907 } else if (is_type_struct(type)) {
3908 entity_t *entry = top->compound_entry;
3911 entry = entry->base.next;
3912 top->compound_entry = entry;
3913 if (entry != NULL) {
3914 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3915 path->top_type = entry->declaration.type;
3919 assert(is_type_array(type));
3922 if (!type->array.size_constant || top->index < type->array.size) {
3927 /* we're past the last member of the current sub-aggregate, try if we
3928 * can ascend in the type hierarchy and continue with another subobject */
3929 size_t len = ARR_LEN(path->path);
3932 ascend_from_subtype(path);
3933 advance_current_object(path);
3935 path->invalid = true;
3940 static ir_initializer_t *create_ir_initializer(
3941 const initializer_t *initializer, type_t *type);
3943 static ir_initializer_t *create_ir_initializer_value(
3944 const initializer_value_t *initializer)
3946 if (is_type_compound(initializer->value->base.type)) {
3947 panic("initializer creation for compounds not implemented yet");
3949 ir_node *value = expression_to_firm(initializer->value);
3950 type_t *type = initializer->value->base.type;
3951 ir_mode *mode = get_ir_mode_storage(type);
3952 value = create_conv(NULL, value, mode);
3953 return create_initializer_const(value);
3956 /** test wether type can be initialized by a string constant */
3957 static bool is_string_type(type_t *type)
3960 if (is_type_pointer(type)) {
3961 inner = skip_typeref(type->pointer.points_to);
3962 } else if(is_type_array(type)) {
3963 inner = skip_typeref(type->array.element_type);
3968 return is_type_integer(inner);
3971 static ir_initializer_t *create_ir_initializer_list(
3972 const initializer_list_t *initializer, type_t *type)
3975 memset(&path, 0, sizeof(path));
3976 path.top_type = type;
3977 path.path = NEW_ARR_F(type_path_entry_t, 0);
3979 descend_into_subtype(&path);
3981 for (size_t i = 0; i < initializer->len; ++i) {
3982 const initializer_t *sub_initializer = initializer->initializers[i];
3984 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3985 walk_designator(&path, sub_initializer->designator.designator);
3989 if (sub_initializer->kind == INITIALIZER_VALUE) {
3990 /* we might have to descend into types until we're at a scalar
3993 type_t *orig_top_type = path.top_type;
3994 type_t *top_type = skip_typeref(orig_top_type);
3996 if (is_type_scalar(top_type))
3998 descend_into_subtype(&path);
4000 } else if (sub_initializer->kind == INITIALIZER_STRING
4001 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4002 /* we might have to descend into types until we're at a scalar
4005 type_t *orig_top_type = path.top_type;
4006 type_t *top_type = skip_typeref(orig_top_type);
4008 if (is_string_type(top_type))
4010 descend_into_subtype(&path);
4014 ir_initializer_t *sub_irinitializer
4015 = create_ir_initializer(sub_initializer, path.top_type);
4017 size_t path_len = ARR_LEN(path.path);
4018 assert(path_len >= 1);
4019 type_path_entry_t *entry = & path.path[path_len-1];
4020 ir_initializer_t *tinitializer = entry->initializer;
4021 set_initializer_compound_value(tinitializer, entry->index,
4024 advance_current_object(&path);
4027 assert(ARR_LEN(path.path) >= 1);
4028 ir_initializer_t *result = path.path[0].initializer;
4029 DEL_ARR_F(path.path);
4034 static ir_initializer_t *create_ir_initializer_string(
4035 const initializer_string_t *initializer, type_t *type)
4037 type = skip_typeref(type);
4039 size_t string_len = initializer->string.size;
4040 assert(type->kind == TYPE_ARRAY);
4041 assert(type->array.size_constant);
4042 size_t len = type->array.size;
4043 ir_initializer_t *irinitializer = create_initializer_compound(len);
4045 const char *string = initializer->string.begin;
4046 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4048 for (size_t i = 0; i < len; ++i) {
4053 tarval *tv = new_tarval_from_long(c, mode);
4054 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4056 set_initializer_compound_value(irinitializer, i, char_initializer);
4059 return irinitializer;
4062 static ir_initializer_t *create_ir_initializer_wide_string(
4063 const initializer_wide_string_t *initializer, type_t *type)
4065 size_t string_len = initializer->string.size;
4066 assert(type->kind == TYPE_ARRAY);
4067 assert(type->array.size_constant);
4068 size_t len = type->array.size;
4069 ir_initializer_t *irinitializer = create_initializer_compound(len);
4071 const wchar_rep_t *string = initializer->string.begin;
4072 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4074 for (size_t i = 0; i < len; ++i) {
4076 if (i < string_len) {
4079 tarval *tv = new_tarval_from_long(c, mode);
4080 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4082 set_initializer_compound_value(irinitializer, i, char_initializer);
4085 return irinitializer;
4088 static ir_initializer_t *create_ir_initializer(
4089 const initializer_t *initializer, type_t *type)
4091 switch(initializer->kind) {
4092 case INITIALIZER_STRING:
4093 return create_ir_initializer_string(&initializer->string, type);
4095 case INITIALIZER_WIDE_STRING:
4096 return create_ir_initializer_wide_string(&initializer->wide_string,
4099 case INITIALIZER_LIST:
4100 return create_ir_initializer_list(&initializer->list, type);
4102 case INITIALIZER_VALUE:
4103 return create_ir_initializer_value(&initializer->value);
4105 case INITIALIZER_DESIGNATOR:
4106 panic("unexpected designator initializer found");
4108 panic("unknown initializer");
4111 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
4114 if (is_atomic_type(type)) {
4115 ir_mode *mode = get_type_mode(type);
4116 tarval *zero = get_mode_null(mode);
4117 ir_node *cnst = new_d_Const(dbgi, zero);
4119 /* TODO: bitfields */
4120 ir_node *mem = get_store();
4121 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4122 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4125 assert(is_compound_type(type));
4128 if (is_Array_type(type)) {
4129 assert(has_array_upper_bound(type, 0));
4130 n_members = get_array_upper_bound_int(type, 0);
4132 n_members = get_compound_n_members(type);
4135 for (int i = 0; i < n_members; ++i) {
4138 if (is_Array_type(type)) {
4139 ir_entity *entity = get_array_element_entity(type);
4140 tarval *index_tv = new_tarval_from_long(i, mode_uint);
4141 ir_node *cnst = new_d_Const(dbgi, index_tv);
4142 ir_node *in[1] = { cnst };
4143 irtype = get_array_element_type(type);
4144 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
4146 ir_entity *member = get_compound_member(type, i);
4148 irtype = get_entity_type(member);
4149 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
4152 create_dynamic_null_initializer(irtype, dbgi, addr);
4157 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4158 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4160 switch(get_initializer_kind(initializer)) {
4161 case IR_INITIALIZER_NULL: {
4162 create_dynamic_null_initializer(type, dbgi, base_addr);
4165 case IR_INITIALIZER_CONST: {
4166 ir_node *node = get_initializer_const_value(initializer);
4167 ir_mode *mode = get_irn_mode(node);
4168 ir_type *ent_type = get_entity_type(entity);
4170 /* is it a bitfield type? */
4171 if (is_Primitive_type(ent_type) &&
4172 get_primitive_base_type(ent_type) != NULL) {
4173 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4177 assert(get_type_mode(type) == mode);
4178 ir_node *mem = get_store();
4179 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4180 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4184 case IR_INITIALIZER_TARVAL: {
4185 tarval *tv = get_initializer_tarval_value(initializer);
4186 ir_mode *mode = get_tarval_mode(tv);
4187 ir_node *cnst = new_d_Const(dbgi, tv);
4188 ir_type *ent_type = get_entity_type(entity);
4190 /* is it a bitfield type? */
4191 if (is_Primitive_type(ent_type) &&
4192 get_primitive_base_type(ent_type) != NULL) {
4193 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4197 assert(get_type_mode(type) == mode);
4198 ir_node *mem = get_store();
4199 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4200 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4204 case IR_INITIALIZER_COMPOUND: {
4205 assert(is_compound_type(type));
4207 if (is_Array_type(type)) {
4208 assert(has_array_upper_bound(type, 0));
4209 n_members = get_array_upper_bound_int(type, 0);
4211 n_members = get_compound_n_members(type);
4214 if (get_initializer_compound_n_entries(initializer)
4215 != (unsigned) n_members)
4216 panic("initializer doesn't match compound type");
4218 for (int i = 0; i < n_members; ++i) {
4221 ir_entity *sub_entity;
4222 if (is_Array_type(type)) {
4223 tarval *index_tv = new_tarval_from_long(i, mode_uint);
4224 ir_node *cnst = new_d_Const(dbgi, index_tv);
4225 ir_node *in[1] = { cnst };
4226 irtype = get_array_element_type(type);
4227 sub_entity = get_array_element_entity(type);
4228 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4231 sub_entity = get_compound_member(type, i);
4232 irtype = get_entity_type(sub_entity);
4233 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4237 ir_initializer_t *sub_init
4238 = get_initializer_compound_value(initializer, i);
4240 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4247 panic("invalid IR_INITIALIZER found");
4250 static void create_dynamic_initializer(ir_initializer_t *initializer,
4251 dbg_info *dbgi, ir_entity *entity)
4253 ir_node *frame = get_irg_frame(current_ir_graph);
4254 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4255 ir_type *type = get_entity_type(entity);
4257 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4260 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4261 ir_entity *entity, type_t *type)
4263 ir_node *memory = get_store();
4264 ir_node *nomem = new_NoMem();
4265 ir_node *frame = get_irg_frame(current_ir_graph);
4266 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4268 if (initializer->kind == INITIALIZER_VALUE) {
4269 initializer_value_t *initializer_value = &initializer->value;
4271 ir_node *value = expression_to_firm(initializer_value->value);
4272 type = skip_typeref(type);
4273 assign_value(dbgi, addr, type, value);
4277 if (!is_constant_initializer(initializer)) {
4278 ir_initializer_t *irinitializer
4279 = create_ir_initializer(initializer, type);
4281 create_dynamic_initializer(irinitializer, dbgi, entity);
4285 /* create the ir_initializer */
4286 ir_graph *const old_current_ir_graph = current_ir_graph;
4287 current_ir_graph = get_const_code_irg();
4289 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4291 assert(current_ir_graph == get_const_code_irg());
4292 current_ir_graph = old_current_ir_graph;
4294 /* create a "template" entity which is copied to the entity on the stack */
4295 ident *const id = id_unique("initializer.%u");
4296 ir_type *const irtype = get_ir_type(type);
4297 ir_type *const global_type = get_glob_type();
4298 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4299 set_entity_ld_ident(init_entity, id);
4301 set_entity_variability(init_entity, variability_initialized);
4302 set_entity_visibility(init_entity, visibility_local);
4303 set_entity_allocation(init_entity, allocation_static);
4305 set_entity_initializer(init_entity, irinitializer);
4307 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4308 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4310 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
4311 set_store(copyb_mem);
4314 static void create_initializer_local_variable_entity(entity_t *entity)
4316 assert(entity->kind == ENTITY_VARIABLE);
4317 initializer_t *initializer = entity->variable.initializer;
4318 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4319 ir_entity *irentity = entity->variable.v.entity;
4320 type_t *type = entity->declaration.type;
4322 create_local_initializer(initializer, dbgi, irentity, type);
4325 static void create_variable_initializer(entity_t *entity)
4327 assert(entity->kind == ENTITY_VARIABLE);
4328 initializer_t *initializer = entity->variable.initializer;
4329 if (initializer == NULL)
4332 declaration_kind_t declaration_kind
4333 = (declaration_kind_t) entity->declaration.kind;
4334 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4335 create_initializer_local_variable_entity(entity);
4339 type_t *type = entity->declaration.type;
4340 type_qualifiers_t tq = get_type_qualifier(type, true);
4342 if (initializer->kind == INITIALIZER_VALUE) {
4343 initializer_value_t *initializer_value = &initializer->value;
4344 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4346 ir_node *value = expression_to_firm(initializer_value->value);
4348 type_t *type = initializer_value->value->base.type;
4349 ir_mode *mode = get_ir_mode_storage(type);
4350 value = create_conv(dbgi, value, mode);
4351 value = do_strict_conv(dbgi, value);
4353 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4354 set_value(entity->variable.v.value_number, value);
4356 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4358 ir_entity *irentity = entity->variable.v.entity;
4360 if (tq & TYPE_QUALIFIER_CONST) {
4361 set_entity_variability(irentity, variability_constant);
4363 set_entity_variability(irentity, variability_initialized);
4365 set_atomic_ent_value(irentity, value);
4368 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4369 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4371 ir_entity *irentity = entity->variable.v.entity;
4372 ir_initializer_t *irinitializer
4373 = create_ir_initializer(initializer, type);
4375 if (tq & TYPE_QUALIFIER_CONST) {
4376 set_entity_variability(irentity, variability_constant);
4378 set_entity_variability(irentity, variability_initialized);
4380 set_entity_initializer(irentity, irinitializer);
4384 static void create_variable_length_array(entity_t *entity)
4386 assert(entity->kind == ENTITY_VARIABLE);
4387 assert(entity->variable.initializer == NULL);
4389 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4390 entity->variable.v.vla_base = NULL;
4392 /* TODO: record VLA somewhere so we create the free node when we leave
4396 static void allocate_variable_length_array(entity_t *entity)
4398 assert(entity->kind == ENTITY_VARIABLE);
4399 assert(entity->variable.initializer == NULL);
4400 assert(get_cur_block() != NULL);
4402 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4403 type_t *type = entity->declaration.type;
4404 ir_type *el_type = get_ir_type(type->array.element_type);
4406 /* make sure size_node is calculated */
4407 get_type_size_node(type);
4408 ir_node *elems = type->array.size_node;
4409 ir_node *mem = get_store();
4410 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4412 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4413 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4416 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4417 entity->variable.v.vla_base = addr;
4421 * Creates a Firm local variable from a declaration.
4423 static void create_local_variable(entity_t *entity)
4425 assert(entity->kind == ENTITY_VARIABLE);
4426 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4428 bool needs_entity = entity->variable.address_taken;
4429 type_t *type = skip_typeref(entity->declaration.type);
4431 /* is it a variable length array? */
4432 if (is_type_array(type) && !type->array.size_constant) {
4433 create_variable_length_array(entity);
4435 } else if (is_type_array(type) || is_type_compound(type)) {
4436 needs_entity = true;
4437 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4438 needs_entity = true;
4442 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4443 create_variable_entity(entity,
4444 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4447 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4448 entity->variable.v.value_number = next_value_number_function;
4449 set_irg_loc_description(current_ir_graph, next_value_number_function,
4451 ++next_value_number_function;
4455 static void create_local_static_variable(entity_t *entity)
4457 assert(entity->kind == ENTITY_VARIABLE);
4458 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4460 type_t *type = skip_typeref(entity->declaration.type);
4461 ir_type *const var_type = entity->variable.thread_local ?
4462 get_tls_type() : get_glob_type();
4463 ir_type *const irtype = get_ir_type(type);
4464 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4466 size_t l = strlen(entity->base.symbol->string);
4467 char buf[l + sizeof(".%u")];
4468 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4469 ident *const id = id_unique(buf);
4471 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4473 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4474 set_entity_volatility(irentity, volatility_is_volatile);
4477 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4478 entity->variable.v.entity = irentity;
4480 set_entity_ld_ident(irentity, id);
4481 set_entity_variability(irentity, variability_uninitialized);
4482 set_entity_visibility(irentity, visibility_local);
4483 set_entity_allocation(irentity, entity->variable.thread_local ?
4484 allocation_automatic : allocation_static);
4486 ir_graph *const old_current_ir_graph = current_ir_graph;
4487 current_ir_graph = get_const_code_irg();
4489 create_variable_initializer(entity);
4491 assert(current_ir_graph == get_const_code_irg());
4492 current_ir_graph = old_current_ir_graph;
4497 static void return_statement_to_firm(return_statement_t *statement)
4499 if (get_cur_block() == NULL)
4502 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4503 type_t *type = current_function_entity->declaration.type;
4504 ir_type *func_irtype = get_ir_type(type);
4509 if (get_method_n_ress(func_irtype) > 0) {
4510 ir_type *res_type = get_method_res_type(func_irtype, 0);
4512 if (statement->value != NULL) {
4513 ir_node *node = expression_to_firm(statement->value);
4514 if (!is_compound_type(res_type)) {
4515 type_t *type = statement->value->base.type;
4516 ir_mode *mode = get_ir_mode_storage(type);
4517 node = create_conv(dbgi, node, mode);
4518 node = do_strict_conv(dbgi, node);
4523 if (is_compound_type(res_type)) {
4526 mode = get_type_mode(res_type);
4528 in[0] = new_Unknown(mode);
4532 /* build return_value for its side effects */
4533 if (statement->value != NULL) {
4534 expression_to_firm(statement->value);
4539 ir_node *store = get_store();
4540 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4542 ir_node *end_block = get_irg_end_block(current_ir_graph);
4543 add_immBlock_pred(end_block, ret);
4545 set_cur_block(NULL);
4548 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4550 if (get_cur_block() == NULL)
4553 return expression_to_firm(statement->expression);
4556 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4558 entity_t *entity = compound->scope.entities;
4559 for ( ; entity != NULL; entity = entity->base.next) {
4560 if (!is_declaration(entity))
4563 create_local_declaration(entity);
4566 ir_node *result = NULL;
4567 statement_t *statement = compound->statements;
4568 for ( ; statement != NULL; statement = statement->base.next) {
4569 if (statement->base.next == NULL
4570 && statement->kind == STATEMENT_EXPRESSION) {
4571 result = expression_statement_to_firm(
4572 &statement->expression);
4575 statement_to_firm(statement);
4581 static void create_global_variable(entity_t *entity)
4583 assert(entity->kind == ENTITY_VARIABLE);
4586 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4587 case STORAGE_CLASS_STATIC: vis = visibility_local; break;
4588 case STORAGE_CLASS_EXTERN: vis = visibility_external_allocated; break;
4589 case STORAGE_CLASS_NONE: vis = visibility_external_visible; break;
4591 default: panic("Invalid storage class for global variable");
4594 ir_type *var_type = entity->variable.thread_local ?
4595 get_tls_type() : get_glob_type();
4596 create_variable_entity(entity,
4597 DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4598 set_entity_visibility(entity->variable.v.entity, vis);
4601 static void create_local_declaration(entity_t *entity)
4603 assert(is_declaration(entity));
4605 /* construct type */
4606 (void) get_ir_type(entity->declaration.type);
4607 if (entity->base.symbol == NULL) {
4611 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4612 case STORAGE_CLASS_STATIC:
4613 create_local_static_variable(entity);
4615 case STORAGE_CLASS_EXTERN:
4616 if (entity->kind == ENTITY_FUNCTION) {
4617 assert(entity->function.statement == NULL);
4618 (void)get_function_entity(entity, NULL);
4620 create_global_variable(entity);
4621 create_variable_initializer(entity);
4624 case STORAGE_CLASS_NONE:
4625 case STORAGE_CLASS_AUTO:
4626 case STORAGE_CLASS_REGISTER:
4627 if (entity->kind == ENTITY_FUNCTION) {
4628 if (entity->function.statement != NULL) {
4629 ir_type *owner = get_irg_frame_type(current_ir_graph);
4630 (void)get_function_entity(entity, owner);
4631 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4632 enqueue_inner_function(entity);
4634 (void)get_function_entity(entity, NULL);
4637 create_local_variable(entity);
4640 case STORAGE_CLASS_TYPEDEF:
4643 panic("invalid storage class found");
4646 static void initialize_local_declaration(entity_t *entity)
4648 if (entity->base.symbol == NULL)
4651 // no need to emit code in dead blocks
4652 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4653 && get_cur_block() == NULL)
4656 switch ((declaration_kind_t) entity->declaration.kind) {
4657 case DECLARATION_KIND_LOCAL_VARIABLE:
4658 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4659 create_variable_initializer(entity);
4662 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4663 allocate_variable_length_array(entity);
4666 case DECLARATION_KIND_COMPOUND_MEMBER:
4667 case DECLARATION_KIND_GLOBAL_VARIABLE:
4668 case DECLARATION_KIND_FUNCTION:
4669 case DECLARATION_KIND_INNER_FUNCTION:
4672 case DECLARATION_KIND_PARAMETER:
4673 case DECLARATION_KIND_PARAMETER_ENTITY:
4674 panic("can't initialize parameters");
4676 case DECLARATION_KIND_UNKNOWN:
4677 panic("can't initialize unknown declaration");
4679 panic("invalid declaration kind");
4682 static void declaration_statement_to_firm(declaration_statement_t *statement)
4684 entity_t *entity = statement->declarations_begin;
4688 entity_t *const last = statement->declarations_end;
4689 for ( ;; entity = entity->base.next) {
4690 if (is_declaration(entity)) {
4691 initialize_local_declaration(entity);
4692 } else if (entity->kind == ENTITY_TYPEDEF) {
4693 /* ยง6.7.7:3 Any array size expressions associated with variable length
4694 * array declarators are evaluated each time the declaration of the
4695 * typedef name is reached in the order of execution. */
4696 type_t *const type = skip_typeref(entity->typedefe.type);
4697 if (is_type_array(type) && type->array.is_vla)
4698 get_vla_size(&type->array);
4705 static void if_statement_to_firm(if_statement_t *statement)
4707 ir_node *cur_block = get_cur_block();
4709 ir_node *fallthrough_block = NULL;
4711 /* the true (blocks) */
4712 ir_node *true_block = NULL;
4713 if (statement->true_statement != NULL) {
4714 true_block = new_immBlock();
4715 set_cur_block(true_block);
4716 statement_to_firm(statement->true_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 /* the false (blocks) */
4726 ir_node *false_block = NULL;
4727 if (statement->false_statement != NULL) {
4728 false_block = new_immBlock();
4729 set_cur_block(false_block);
4731 statement_to_firm(statement->false_statement);
4732 if (get_cur_block() != NULL) {
4733 ir_node *jmp = new_Jmp();
4734 if (fallthrough_block == NULL)
4735 fallthrough_block = new_immBlock();
4736 add_immBlock_pred(fallthrough_block, jmp);
4740 /* create the condition */
4741 if (cur_block != NULL) {
4742 if (true_block == NULL || false_block == NULL) {
4743 if (fallthrough_block == NULL)
4744 fallthrough_block = new_immBlock();
4745 if (true_block == NULL)
4746 true_block = fallthrough_block;
4747 if (false_block == NULL)
4748 false_block = fallthrough_block;
4751 set_cur_block(cur_block);
4752 create_condition_evaluation(statement->condition, true_block,
4756 mature_immBlock(true_block);
4757 if (false_block != fallthrough_block && false_block != NULL) {
4758 mature_immBlock(false_block);
4760 if (fallthrough_block != NULL) {
4761 mature_immBlock(fallthrough_block);
4764 set_cur_block(fallthrough_block);
4767 static void while_statement_to_firm(while_statement_t *statement)
4769 ir_node *jmp = NULL;
4770 if (get_cur_block() != NULL) {
4774 /* create the header block */
4775 ir_node *header_block = new_immBlock();
4777 add_immBlock_pred(header_block, jmp);
4781 ir_node *old_continue_label = continue_label;
4782 ir_node *old_break_label = break_label;
4783 continue_label = header_block;
4786 ir_node *body_block = new_immBlock();
4787 set_cur_block(body_block);
4788 statement_to_firm(statement->body);
4789 ir_node *false_block = break_label;
4791 assert(continue_label == header_block);
4792 continue_label = old_continue_label;
4793 break_label = old_break_label;
4795 if (get_cur_block() != NULL) {
4797 add_immBlock_pred(header_block, jmp);
4800 /* shortcut for while(true) */
4801 if (is_constant_expression(statement->condition)
4802 && fold_constant_to_bool(statement->condition) != 0) {
4803 set_cur_block(header_block);
4804 ir_node *header_jmp = new_Jmp();
4805 add_immBlock_pred(body_block, header_jmp);
4807 keep_alive(body_block);
4808 keep_all_memory(body_block);
4809 set_cur_block(body_block);
4811 if (false_block == NULL) {
4812 false_block = new_immBlock();
4815 /* create the condition */
4816 set_cur_block(header_block);
4818 create_condition_evaluation(statement->condition, body_block,
4822 mature_immBlock(body_block);
4823 mature_immBlock(header_block);
4824 if (false_block != NULL) {
4825 mature_immBlock(false_block);
4828 set_cur_block(false_block);
4831 static void do_while_statement_to_firm(do_while_statement_t *statement)
4833 ir_node *jmp = NULL;
4834 if (get_cur_block() != NULL) {
4838 /* create the header block */
4839 ir_node *header_block = new_immBlock();
4842 ir_node *body_block = new_immBlock();
4844 add_immBlock_pred(body_block, jmp);
4847 ir_node *old_continue_label = continue_label;
4848 ir_node *old_break_label = break_label;
4849 continue_label = header_block;
4852 set_cur_block(body_block);
4853 statement_to_firm(statement->body);
4854 ir_node *false_block = break_label;
4856 assert(continue_label == header_block);
4857 continue_label = old_continue_label;
4858 break_label = old_break_label;
4860 if (get_cur_block() != NULL) {
4861 ir_node *body_jmp = new_Jmp();
4862 add_immBlock_pred(header_block, body_jmp);
4863 mature_immBlock(header_block);
4866 if (false_block == NULL) {
4867 false_block = new_immBlock();
4870 /* create the condition */
4871 set_cur_block(header_block);
4873 create_condition_evaluation(statement->condition, body_block, false_block);
4874 mature_immBlock(body_block);
4875 mature_immBlock(header_block);
4876 mature_immBlock(false_block);
4878 set_cur_block(false_block);
4881 static void for_statement_to_firm(for_statement_t *statement)
4883 ir_node *jmp = NULL;
4885 /* create declarations */
4886 entity_t *entity = statement->scope.entities;
4887 for ( ; entity != NULL; entity = entity->base.next) {
4888 if (!is_declaration(entity))
4891 create_local_declaration(entity);
4894 if (get_cur_block() != NULL) {
4895 entity = statement->scope.entities;
4896 for ( ; entity != NULL; entity = entity->base.next) {
4897 if (!is_declaration(entity))
4900 initialize_local_declaration(entity);
4903 if (statement->initialisation != NULL) {
4904 expression_to_firm(statement->initialisation);
4911 /* create the step block */
4912 ir_node *const step_block = new_immBlock();
4913 set_cur_block(step_block);
4914 if (statement->step != NULL) {
4915 expression_to_firm(statement->step);
4917 ir_node *const step_jmp = new_Jmp();
4919 /* create the header block */
4920 ir_node *const header_block = new_immBlock();
4921 set_cur_block(header_block);
4923 add_immBlock_pred(header_block, jmp);
4925 add_immBlock_pred(header_block, step_jmp);
4927 /* the false block */
4928 ir_node *const false_block = new_immBlock();
4931 ir_node *body_block;
4932 if (statement->body != NULL) {
4933 ir_node *const old_continue_label = continue_label;
4934 ir_node *const old_break_label = break_label;
4935 continue_label = step_block;
4936 break_label = false_block;
4938 body_block = new_immBlock();
4939 set_cur_block(body_block);
4940 statement_to_firm(statement->body);
4942 assert(continue_label == step_block);
4943 assert(break_label == false_block);
4944 continue_label = old_continue_label;
4945 break_label = old_break_label;
4947 if (get_cur_block() != NULL) {
4949 add_immBlock_pred(step_block, jmp);
4952 body_block = step_block;
4955 /* create the condition */
4956 set_cur_block(header_block);
4957 if (statement->condition != NULL) {
4958 create_condition_evaluation(statement->condition, body_block,
4961 keep_alive(header_block);
4962 keep_all_memory(header_block);
4964 add_immBlock_pred(body_block, jmp);
4967 mature_immBlock(body_block);
4968 mature_immBlock(false_block);
4969 mature_immBlock(step_block);
4970 mature_immBlock(header_block);
4971 mature_immBlock(false_block);
4973 set_cur_block(false_block);
4976 static void create_jump_statement(const statement_t *statement,
4977 ir_node *target_block)
4979 if (get_cur_block() == NULL)
4982 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4983 ir_node *jump = new_d_Jmp(dbgi);
4984 add_immBlock_pred(target_block, jump);
4986 set_cur_block(NULL);
4989 static ir_node *get_break_label(void)
4991 if (break_label == NULL) {
4992 break_label = new_immBlock();
4997 static void switch_statement_to_firm(switch_statement_t *statement)
4999 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5001 ir_node *expression = expression_to_firm(statement->expression);
5002 ir_node *cond = new_d_Cond(dbgi, expression);
5004 set_cur_block(NULL);
5006 ir_node *const old_switch_cond = current_switch_cond;
5007 ir_node *const old_break_label = break_label;
5008 const bool old_saw_default_label = saw_default_label;
5009 saw_default_label = false;
5010 current_switch_cond = cond;
5012 switch_statement_t *const old_switch = current_switch;
5013 current_switch = statement;
5015 /* determine a free number for the default label */
5016 unsigned long num_cases = 0;
5018 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5019 if (l->expression == NULL) {
5023 if (l->last_case >= l->first_case)
5024 num_cases += l->last_case - l->first_case + 1;
5025 if (l->last_case > def_nr)
5026 def_nr = l->last_case;
5029 if (def_nr == INT_MAX) {
5030 /* Bad: an overflow will occur, we cannot be sure that the
5031 * maximum + 1 is a free number. Scan the values a second
5032 * time to find a free number.
5034 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5036 memset(bits, 0, (num_cases + 7) >> 3);
5037 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5038 if (l->expression == NULL) {
5042 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5043 if (start < num_cases && l->last_case >= 0) {
5044 unsigned long end = (unsigned long)l->last_case < num_cases ?
5045 (unsigned long)l->last_case : num_cases - 1;
5046 for (unsigned long cns = start; cns <= end; ++cns) {
5047 bits[cns >> 3] |= (1 << (cns & 7));
5051 /* We look at the first num_cases constants:
5052 * Either they are dense, so we took the last (num_cases)
5053 * one, or they are not dense, so we will find one free
5057 for (i = 0; i < num_cases; ++i)
5058 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5066 statement->default_proj_nr = def_nr;
5068 if (statement->body != NULL) {
5069 statement_to_firm(statement->body);
5072 if (get_cur_block() != NULL) {
5073 ir_node *jmp = new_Jmp();
5074 add_immBlock_pred(get_break_label(), jmp);
5077 if (!saw_default_label) {
5078 set_cur_block(get_nodes_block(cond));
5079 ir_node *const proj = new_d_defaultProj(dbgi, cond,
5080 statement->default_proj_nr);
5081 add_immBlock_pred(get_break_label(), proj);
5084 if (break_label != NULL) {
5085 mature_immBlock(break_label);
5087 set_cur_block(break_label);
5089 assert(current_switch_cond == cond);
5090 current_switch = old_switch;
5091 current_switch_cond = old_switch_cond;
5092 break_label = old_break_label;
5093 saw_default_label = old_saw_default_label;
5096 static void case_label_to_firm(const case_label_statement_t *statement)
5098 if (statement->is_empty_range)
5101 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5103 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
5106 ir_node *block = new_immBlock();
5108 set_cur_block(get_nodes_block(current_switch_cond));
5109 if (statement->expression != NULL) {
5110 long pn = statement->first_case;
5111 long end_pn = statement->last_case;
5112 assert(pn <= end_pn);
5113 /* create jumps for all cases in the given range */
5115 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5116 add_immBlock_pred(block, proj);
5117 } while (pn++ < end_pn);
5119 saw_default_label = true;
5120 proj = new_d_defaultProj(dbgi, current_switch_cond,
5121 current_switch->default_proj_nr);
5123 add_immBlock_pred(block, proj);
5126 if (fallthrough != NULL) {
5127 add_immBlock_pred(block, fallthrough);
5129 mature_immBlock(block);
5130 set_cur_block(block);
5132 if (statement->statement != NULL) {
5133 statement_to_firm(statement->statement);
5137 static void label_to_firm(const label_statement_t *statement)
5139 ir_node *block = get_label_block(statement->label);
5141 if (get_cur_block() != NULL) {
5142 ir_node *jmp = new_Jmp();
5143 add_immBlock_pred(block, jmp);
5146 set_cur_block(block);
5148 keep_all_memory(block);
5150 if (statement->statement != NULL) {
5151 statement_to_firm(statement->statement);
5155 static void goto_to_firm(const goto_statement_t *statement)
5157 if (get_cur_block() == NULL)
5160 if (statement->expression) {
5161 ir_node *irn = expression_to_firm(statement->expression);
5162 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5163 ir_node *ijmp = new_d_IJmp(dbgi, irn);
5165 set_irn_link(ijmp, ijmp_list);
5168 ir_node *block = get_label_block(statement->label);
5169 ir_node *jmp = new_Jmp();
5170 add_immBlock_pred(block, jmp);
5172 set_cur_block(NULL);
5175 static void asm_statement_to_firm(const asm_statement_t *statement)
5177 bool needs_memory = false;
5179 if (statement->is_volatile) {
5180 needs_memory = true;
5183 size_t n_clobbers = 0;
5184 asm_clobber_t *clobber = statement->clobbers;
5185 for ( ; clobber != NULL; clobber = clobber->next) {
5186 const char *clobber_str = clobber->clobber.begin;
5188 if (!be_is_valid_clobber(clobber_str)) {
5189 errorf(&statement->base.source_position,
5190 "invalid clobber '%s' specified", clobber->clobber);
5194 if (strcmp(clobber_str, "memory") == 0) {
5195 needs_memory = true;
5199 ident *id = new_id_from_str(clobber_str);
5200 obstack_ptr_grow(&asm_obst, id);
5203 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
5204 ident **clobbers = NULL;
5205 if (n_clobbers > 0) {
5206 clobbers = obstack_finish(&asm_obst);
5209 size_t n_inputs = 0;
5210 asm_argument_t *argument = statement->inputs;
5211 for ( ; argument != NULL; argument = argument->next)
5213 size_t n_outputs = 0;
5214 argument = statement->outputs;
5215 for ( ; argument != NULL; argument = argument->next)
5218 unsigned next_pos = 0;
5220 ir_node *ins[n_inputs + n_outputs + 1];
5223 ir_asm_constraint tmp_in_constraints[n_outputs];
5225 const expression_t *out_exprs[n_outputs];
5226 ir_node *out_addrs[n_outputs];
5227 size_t out_size = 0;
5229 argument = statement->outputs;
5230 for ( ; argument != NULL; argument = argument->next) {
5231 const char *constraints = argument->constraints.begin;
5232 asm_constraint_flags_t asm_flags
5233 = be_parse_asm_constraints(constraints);
5235 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5236 warningf(&statement->base.source_position,
5237 "some constraints in '%s' are not supported", constraints);
5239 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5240 errorf(&statement->base.source_position,
5241 "some constraints in '%s' are invalid", constraints);
5244 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5245 errorf(&statement->base.source_position,
5246 "no write flag specified for output constraints '%s'",
5251 unsigned pos = next_pos++;
5252 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5253 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5254 expression_t *expr = argument->expression;
5255 ir_node *addr = expression_to_addr(expr);
5256 /* in+output, construct an artifical same_as constraint on the
5258 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5260 ir_node *value = get_value_from_lvalue(expr, addr);
5262 snprintf(buf, sizeof(buf), "%u", pos);
5264 ir_asm_constraint constraint;
5265 constraint.pos = pos;
5266 constraint.constraint = new_id_from_str(buf);
5267 constraint.mode = get_ir_mode_storage(expr->base.type);
5268 tmp_in_constraints[in_size] = constraint;
5269 ins[in_size] = value;
5274 out_exprs[out_size] = expr;
5275 out_addrs[out_size] = addr;
5277 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5278 /* pure memory ops need no input (but we have to make sure we
5279 * attach to the memory) */
5280 assert(! (asm_flags &
5281 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5282 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5283 needs_memory = true;
5285 /* we need to attach the address to the inputs */
5286 expression_t *expr = argument->expression;
5288 ir_asm_constraint constraint;
5289 constraint.pos = pos;
5290 constraint.constraint = new_id_from_str(constraints);
5291 constraint.mode = NULL;
5292 tmp_in_constraints[in_size] = constraint;
5294 ins[in_size] = expression_to_addr(expr);
5298 errorf(&statement->base.source_position,
5299 "only modifiers but no place set in constraints '%s'",
5304 ir_asm_constraint constraint;
5305 constraint.pos = pos;
5306 constraint.constraint = new_id_from_str(constraints);
5307 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5309 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5311 assert(obstack_object_size(&asm_obst)
5312 == out_size * sizeof(ir_asm_constraint));
5313 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5316 obstack_grow(&asm_obst, tmp_in_constraints,
5317 in_size * sizeof(tmp_in_constraints[0]));
5318 /* find and count input and output arguments */
5319 argument = statement->inputs;
5320 for ( ; argument != NULL; argument = argument->next) {
5321 const char *constraints = argument->constraints.begin;
5322 asm_constraint_flags_t asm_flags
5323 = be_parse_asm_constraints(constraints);
5325 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5326 errorf(&statement->base.source_position,
5327 "some constraints in '%s' are not supported", constraints);
5330 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5331 errorf(&statement->base.source_position,
5332 "some constraints in '%s' are invalid", constraints);
5335 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5336 errorf(&statement->base.source_position,
5337 "write flag specified for input constraints '%s'",
5343 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5344 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5345 /* we can treat this as "normal" input */
5346 input = expression_to_firm(argument->expression);
5347 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5348 /* pure memory ops need no input (but we have to make sure we
5349 * attach to the memory) */
5350 assert(! (asm_flags &
5351 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5352 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5353 needs_memory = true;
5354 input = expression_to_addr(argument->expression);
5356 errorf(&statement->base.source_position,
5357 "only modifiers but no place set in constraints '%s'",
5362 ir_asm_constraint constraint;
5363 constraint.pos = next_pos++;
5364 constraint.constraint = new_id_from_str(constraints);
5365 constraint.mode = get_irn_mode(input);
5367 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5368 ins[in_size++] = input;
5372 ir_asm_constraint constraint;
5373 constraint.pos = next_pos++;
5374 constraint.constraint = new_id_from_str("");
5375 constraint.mode = mode_M;
5377 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5378 ins[in_size++] = get_store();
5381 assert(obstack_object_size(&asm_obst)
5382 == in_size * sizeof(ir_asm_constraint));
5383 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5385 /* create asm node */
5386 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5388 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5390 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5391 out_size, output_constraints,
5392 n_clobbers, clobbers, asm_text);
5394 if (statement->is_volatile) {
5395 set_irn_pinned(node, op_pin_state_pinned);
5397 set_irn_pinned(node, op_pin_state_floats);
5400 /* create output projs & connect them */
5402 ir_node *projm = new_Proj(node, mode_M, out_size+1);
5407 for (i = 0; i < out_size; ++i) {
5408 const expression_t *out_expr = out_exprs[i];
5410 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5411 ir_node *proj = new_Proj(node, mode, pn);
5412 ir_node *addr = out_addrs[i];
5414 set_value_for_expression_addr(out_expr, proj, addr);
5418 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5420 statement_to_firm(statement->try_statement);
5421 warningf(&statement->base.source_position, "structured exception handling ignored");
5424 static void leave_statement_to_firm(leave_statement_t *statement)
5426 errorf(&statement->base.source_position, "__leave not supported yet");
5430 * Transform a statement.
5432 static void statement_to_firm(statement_t *statement)
5435 assert(!statement->base.transformed);
5436 statement->base.transformed = true;
5439 switch (statement->kind) {
5440 case STATEMENT_INVALID:
5441 panic("invalid statement found");
5442 case STATEMENT_EMPTY:
5445 case STATEMENT_COMPOUND:
5446 compound_statement_to_firm(&statement->compound);
5448 case STATEMENT_RETURN:
5449 return_statement_to_firm(&statement->returns);
5451 case STATEMENT_EXPRESSION:
5452 expression_statement_to_firm(&statement->expression);
5455 if_statement_to_firm(&statement->ifs);
5457 case STATEMENT_WHILE:
5458 while_statement_to_firm(&statement->whiles);
5460 case STATEMENT_DO_WHILE:
5461 do_while_statement_to_firm(&statement->do_while);
5463 case STATEMENT_DECLARATION:
5464 declaration_statement_to_firm(&statement->declaration);
5466 case STATEMENT_BREAK:
5467 create_jump_statement(statement, get_break_label());
5469 case STATEMENT_CONTINUE:
5470 create_jump_statement(statement, continue_label);
5472 case STATEMENT_SWITCH:
5473 switch_statement_to_firm(&statement->switchs);
5475 case STATEMENT_CASE_LABEL:
5476 case_label_to_firm(&statement->case_label);
5479 for_statement_to_firm(&statement->fors);
5481 case STATEMENT_LABEL:
5482 label_to_firm(&statement->label);
5484 case STATEMENT_GOTO:
5485 goto_to_firm(&statement->gotos);
5488 asm_statement_to_firm(&statement->asms);
5490 case STATEMENT_MS_TRY:
5491 ms_try_statement_to_firm(&statement->ms_try);
5493 case STATEMENT_LEAVE:
5494 leave_statement_to_firm(&statement->leave);
5497 panic("statement not implemented");
5500 static int count_local_variables(const entity_t *entity,
5501 const entity_t *const last)
5504 entity_t const *const end = last != NULL ? last->base.next : NULL;
5505 for (; entity != end; entity = entity->base.next) {
5509 if (entity->kind == ENTITY_VARIABLE) {
5510 type = skip_typeref(entity->declaration.type);
5511 address_taken = entity->variable.address_taken;
5512 } else if (entity->kind == ENTITY_PARAMETER) {
5513 type = skip_typeref(entity->declaration.type);
5514 address_taken = entity->parameter.address_taken;
5519 if (!address_taken && is_type_scalar(type))
5525 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5527 int *const count = env;
5529 switch (stmt->kind) {
5530 case STATEMENT_DECLARATION: {
5531 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5532 *count += count_local_variables(decl_stmt->declarations_begin,
5533 decl_stmt->declarations_end);
5538 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5547 * Return the number of local (alias free) variables used by a function.
5549 static int get_function_n_local_vars(entity_t *entity)
5551 const function_t *function = &entity->function;
5554 /* count parameters */
5555 count += count_local_variables(function->parameters.entities, NULL);
5557 /* count local variables declared in body */
5558 walk_statements(function->statement, count_local_variables_in_stmt, &count);
5563 * Build Firm code for the parameters of a function.
5565 static void initialize_function_parameters(entity_t *entity)
5567 assert(entity->kind == ENTITY_FUNCTION);
5568 ir_graph *irg = current_ir_graph;
5569 ir_node *args = get_irg_args(irg);
5570 ir_node *start_block = get_irg_start_block(irg);
5571 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5572 int first_param_nr = 0;
5574 if (entity->function.need_closure) {
5575 /* add an extra parameter for the static link */
5576 entity->function.static_link = new_r_Proj(irg, start_block, args, mode_P_data, 0);
5581 entity_t *parameter = entity->function.parameters.entities;
5582 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5583 if (parameter->kind != ENTITY_PARAMETER)
5586 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5587 type_t *type = skip_typeref(parameter->declaration.type);
5589 bool needs_entity = parameter->parameter.address_taken;
5590 assert(!is_type_array(type));
5591 if (is_type_compound(type)) {
5592 needs_entity = true;
5596 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5597 ident *id = new_id_from_str(parameter->base.symbol->string);
5598 set_entity_ident(entity, id);
5600 parameter->declaration.kind
5601 = DECLARATION_KIND_PARAMETER_ENTITY;
5602 parameter->parameter.v.entity = entity;
5606 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5607 ir_mode *param_mode = get_type_mode(param_irtype);
5609 long pn = n + first_param_nr;
5610 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5612 ir_mode *mode = get_ir_mode_storage(type);
5613 value = create_conv(NULL, value, mode);
5614 value = do_strict_conv(NULL, value);
5616 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5617 parameter->parameter.v.value_number = next_value_number_function;
5618 set_irg_loc_description(current_ir_graph, next_value_number_function,
5620 ++next_value_number_function;
5622 set_value(parameter->parameter.v.value_number, value);
5627 * Handle additional decl modifiers for IR-graphs
5629 * @param irg the IR-graph
5630 * @param dec_modifiers additional modifiers
5632 static void handle_decl_modifier_irg(ir_graph_ptr irg,
5633 decl_modifiers_t decl_modifiers)
5635 if (decl_modifiers & DM_RETURNS_TWICE) {
5636 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5637 set_irg_additional_property(irg, mtp_property_returns_twice);
5639 if (decl_modifiers & DM_NORETURN) {
5640 /* TRUE if the declaration includes the Microsoft
5641 __declspec(noreturn) specifier. */
5642 set_irg_additional_property(irg, mtp_property_noreturn);
5644 if (decl_modifiers & DM_NOTHROW) {
5645 /* TRUE if the declaration includes the Microsoft
5646 __declspec(nothrow) specifier. */
5647 set_irg_additional_property(irg, mtp_property_nothrow);
5649 if (decl_modifiers & DM_NAKED) {
5650 /* TRUE if the declaration includes the Microsoft
5651 __declspec(naked) specifier. */
5652 set_irg_additional_property(irg, mtp_property_naked);
5654 if (decl_modifiers & DM_FORCEINLINE) {
5655 /* TRUE if the declaration includes the
5656 Microsoft __forceinline specifier. */
5657 set_irg_inline_property(irg, irg_inline_forced);
5659 if (decl_modifiers & DM_NOINLINE) {
5660 /* TRUE if the declaration includes the Microsoft
5661 __declspec(noinline) specifier. */
5662 set_irg_inline_property(irg, irg_inline_forbidden);
5666 static void add_function_pointer(ir_type *segment, ir_entity *method,
5667 const char *unique_template)
5669 ir_type *method_type = get_entity_type(method);
5670 ident *id = id_unique(unique_template);
5671 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5673 ident *ide = id_unique(unique_template);
5674 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5675 ir_graph *irg = get_const_code_irg();
5676 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5679 set_entity_compiler_generated(ptr, 1);
5680 set_entity_variability(ptr, variability_constant);
5681 set_atomic_ent_value(ptr, val);
5685 * Generate possible IJmp branches to a given label block.
5687 static void gen_ijmp_branches(ir_node *block)
5690 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5691 add_immBlock_pred(block, ijmp);
5696 * Create code for a function and all inner functions.
5698 * @param entity the function entity
5700 static void create_function(entity_t *entity)
5702 assert(entity->kind == ENTITY_FUNCTION);
5703 ir_entity *function_entity = get_function_entity(entity, current_outer_frame);
5705 if (entity->function.statement == NULL)
5708 if (is_main(entity) && firm_opt.os_support == OS_SUPPORT_MINGW) {
5709 prepare_main_collect2(entity);
5712 inner_functions = NULL;
5713 current_trampolines = NULL;
5715 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5716 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5717 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5719 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5720 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5721 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5724 current_function_entity = entity;
5725 current_function_name = NULL;
5726 current_funcsig = NULL;
5728 assert(all_labels == NULL);
5729 all_labels = NEW_ARR_F(label_t *, 0);
5732 int n_local_vars = get_function_n_local_vars(entity);
5733 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5735 ir_graph *old_current_function = current_function;
5736 current_function = irg;
5738 set_irg_fp_model(irg, firm_opt.fp_model);
5739 tarval_enable_fp_ops(1);
5740 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5742 ir_node *first_block = get_cur_block();
5744 /* set inline flags */
5745 if (entity->function.is_inline)
5746 set_irg_inline_property(irg, irg_inline_recomended);
5747 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5749 next_value_number_function = 0;
5750 initialize_function_parameters(entity);
5751 current_static_link = entity->function.static_link;
5753 statement_to_firm(entity->function.statement);
5755 ir_node *end_block = get_irg_end_block(irg);
5757 /* do we have a return statement yet? */
5758 if (get_cur_block() != NULL) {
5759 type_t *type = skip_typeref(entity->declaration.type);
5760 assert(is_type_function(type));
5761 const function_type_t *func_type = &type->function;
5762 const type_t *return_type
5763 = skip_typeref(func_type->return_type);
5766 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5767 ret = new_Return(get_store(), 0, NULL);
5770 if (is_type_scalar(return_type)) {
5771 mode = get_ir_mode_storage(func_type->return_type);
5777 /* ยง5.1.2.2.3 main implicitly returns 0 */
5778 if (is_main(entity)) {
5779 in[0] = new_Const(get_mode_null(mode));
5781 in[0] = new_Unknown(mode);
5783 ret = new_Return(get_store(), 1, in);
5785 add_immBlock_pred(end_block, ret);
5788 bool has_computed_gotos = false;
5789 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5790 label_t *label = all_labels[i];
5791 if (label->address_taken) {
5792 gen_ijmp_branches(label->block);
5793 has_computed_gotos = true;
5795 mature_immBlock(label->block);
5797 if (has_computed_gotos) {
5798 /* if we have computed goto's in the function, we cannot inline it */
5799 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5800 warningf(&entity->base.source_position,
5801 "function '%Y' can never be inlined because it contains a computed goto",
5802 entity->base.symbol);
5804 set_irg_inline_property(irg, irg_inline_forbidden);
5807 DEL_ARR_F(all_labels);
5810 mature_immBlock(first_block);
5811 mature_immBlock(end_block);
5813 irg_finalize_cons(irg);
5815 /* finalize the frame type */
5816 ir_type *frame_type = get_irg_frame_type(irg);
5817 int n = get_compound_n_members(frame_type);
5820 for (int i = 0; i < n; ++i) {
5821 ir_entity *entity = get_compound_member(frame_type, i);
5822 ir_type *entity_type = get_entity_type(entity);
5824 int align = get_type_alignment_bytes(entity_type);
5825 if (align > align_all)
5829 misalign = offset % align;
5831 offset += align - misalign;
5835 set_entity_offset(entity, offset);
5836 offset += get_type_size_bytes(entity_type);
5838 set_type_size_bytes(frame_type, offset);
5839 set_type_alignment_bytes(frame_type, align_all);
5842 current_function = old_current_function;
5844 if (current_trampolines != NULL) {
5845 DEL_ARR_F(current_trampolines);
5846 current_trampolines = NULL;
5849 /* create inner functions if any */
5850 entity_t **inner = inner_functions;
5851 if (inner != NULL) {
5852 ir_type *rem_outer_frame = current_outer_frame;
5853 current_outer_frame = get_irg_frame_type(current_ir_graph);
5854 ir_type *rem_outer_value_type = current_outer_value_type;
5855 current_outer_value_type = get_irg_value_param_type(current_ir_graph);
5856 for (int i = ARR_LEN(inner) - 1; i >= 0; --i) {
5857 create_function(inner[i]);
5861 current_outer_value_type = rem_outer_value_type;
5862 current_outer_frame = rem_outer_frame;
5866 static void scope_to_firm(scope_t *scope)
5868 /* first pass: create declarations */
5869 entity_t *entity = scope->entities;
5870 for ( ; entity != NULL; entity = entity->base.next) {
5871 if (entity->base.symbol == NULL)
5874 if (entity->kind == ENTITY_FUNCTION) {
5875 if (entity->function.btk != bk_none) {
5876 /* builtins have no representation */
5879 (void)get_function_entity(entity, NULL);
5880 } else if (entity->kind == ENTITY_VARIABLE) {
5881 create_global_variable(entity);
5885 /* second pass: create code/initializers */
5886 entity = scope->entities;
5887 for ( ; entity != NULL; entity = entity->base.next) {
5888 if (entity->base.symbol == NULL)
5891 if (entity->kind == ENTITY_FUNCTION) {
5892 if (entity->function.btk != bk_none) {
5893 /* builtins have no representation */
5896 create_function(entity);
5897 } else if (entity->kind == ENTITY_VARIABLE) {
5898 assert(entity->declaration.kind
5899 == DECLARATION_KIND_GLOBAL_VARIABLE);
5900 current_ir_graph = get_const_code_irg();
5901 create_variable_initializer(entity);
5906 void init_ast2firm(void)
5908 obstack_init(&asm_obst);
5909 init_atomic_modes();
5911 /* OS option must be set to the backend */
5912 switch (firm_opt.os_support) {
5913 case OS_SUPPORT_MINGW:
5914 create_ld_ident = create_name_win32;
5916 case OS_SUPPORT_LINUX:
5917 create_ld_ident = create_name_linux_elf;
5919 case OS_SUPPORT_MACHO:
5920 create_ld_ident = create_name_macho;
5923 panic("unexpected OS support mode");
5926 /* create idents for all known runtime functions */
5927 for (size_t i = 0; i < lengthof(rts_data); ++i) {
5928 rts_idents[i] = new_id_from_str(rts_data[i].name);
5931 entitymap_init(&entitymap);
5934 static void init_ir_types(void)
5936 static int ir_types_initialized = 0;
5937 if (ir_types_initialized)
5939 ir_types_initialized = 1;
5941 ir_type_int = get_ir_type(type_int);
5942 ir_type_char = get_ir_type(type_char);
5943 ir_type_const_char = get_ir_type(type_const_char);
5944 ir_type_wchar_t = get_ir_type(type_wchar_t);
5945 ir_type_void = get_ir_type(type_void);
5947 be_params = be_get_backend_param();
5948 mode_float_arithmetic = be_params->mode_float_arithmetic;
5950 stack_param_align = be_params->stack_param_align;
5953 void exit_ast2firm(void)
5955 entitymap_destroy(&entitymap);
5956 obstack_free(&asm_obst, NULL);
5959 static void global_asm_to_firm(statement_t *s)
5961 for (; s != NULL; s = s->base.next) {
5962 assert(s->kind == STATEMENT_ASM);
5964 char const *const text = s->asms.asm_text.begin;
5965 size_t size = s->asms.asm_text.size;
5967 /* skip the last \0 */
5968 if (text[size - 1] == '\0')
5971 ident *const id = new_id_from_chars(text, size);
5976 void translation_unit_to_firm(translation_unit_t *unit)
5978 /* just to be sure */
5979 continue_label = NULL;
5981 current_switch_cond = NULL;
5982 current_translation_unit = unit;
5986 scope_to_firm(&unit->scope);
5987 global_asm_to_firm(unit->global_asm);
5989 current_ir_graph = NULL;
5990 current_translation_unit = NULL;