2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include <libfirm/firm.h>
28 #include <libfirm/adt/obst.h>
29 #include <libfirm/be.h>
33 #include "adt/error.h"
34 #include "adt/array.h"
42 #include "diagnostic.h"
43 #include "lang_features.h"
45 #include "type_hash.h"
47 #include "walk_statements.h"
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;
82 static bool initializer_use_bitfield_basetype;
84 extern bool have_const_functions;
86 static const entity_t *current_function_entity;
87 static ir_node *current_function_name;
88 static ir_node *current_funcsig;
89 static switch_statement_t *current_switch;
90 static ir_graph *current_function;
91 static translation_unit_t *current_translation_unit;
92 static trampoline_region *current_trampolines;
93 static ir_type *current_outer_frame;
94 static ir_type *current_outer_value_type;
95 static ir_node *current_static_link;
97 static entitymap_t entitymap;
99 static struct obstack asm_obst;
101 typedef enum declaration_kind_t {
102 DECLARATION_KIND_UNKNOWN,
103 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
104 DECLARATION_KIND_GLOBAL_VARIABLE,
105 DECLARATION_KIND_LOCAL_VARIABLE,
106 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
107 DECLARATION_KIND_PARAMETER,
108 DECLARATION_KIND_PARAMETER_ENTITY,
109 DECLARATION_KIND_FUNCTION,
110 DECLARATION_KIND_COMPOUND_MEMBER,
111 DECLARATION_KIND_INNER_FUNCTION
112 } declaration_kind_t;
114 static ir_mode *get_ir_mode_storage(type_t *type);
116 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
117 * int that it returns bigger modes for floating point on some platforms
118 * (x87 internally does arithemtic with 80bits)
120 static ir_mode *get_ir_mode_arithmetic(type_t *type);
122 static ir_type *get_ir_type_incomplete(type_t *type);
124 static void enqueue_inner_function(entity_t *entity)
126 if (inner_functions == NULL)
127 inner_functions = NEW_ARR_F(entity_t *, 0);
128 ARR_APP1(entity_t*, inner_functions, entity);
131 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
133 const entity_t *entity = get_irg_loc_description(irg, pos);
135 if (entity != NULL) {
136 warningf(&entity->base.source_position,
137 "%s '%#T' might be used uninitialized",
138 get_entity_kind_name(entity->kind),
139 entity->declaration.type, entity->base.symbol);
141 return new_r_Unknown(irg, mode);
144 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
146 const source_position_t *pos = (const source_position_t*) dbg;
149 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
153 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
155 const source_position_t *pos = (const source_position_t*) dbg;
160 return pos->input_name;
163 static dbg_info *get_dbg_info(const source_position_t *pos)
165 return (dbg_info*) pos;
168 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
170 static ir_mode *mode_int, *mode_uint;
172 static ir_node *_expression_to_firm(const expression_t *expression);
173 static ir_node *expression_to_firm(const expression_t *expression);
174 static void create_local_declaration(entity_t *entity);
176 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
178 unsigned flags = get_atomic_type_flags(kind);
179 unsigned size = get_atomic_type_size(kind);
180 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
181 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
184 unsigned bit_size = size * 8;
185 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
186 unsigned modulo_shift;
187 ir_mode_arithmetic arithmetic;
189 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
190 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
191 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
193 sort = irms_int_number;
194 arithmetic = irma_twos_complement;
195 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
197 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
198 snprintf(name, sizeof(name), "F%u", bit_size);
199 sort = irms_float_number;
200 arithmetic = irma_ieee754;
203 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
211 * Initialises the atomic modes depending on the machine size.
213 static void init_atomic_modes(void)
215 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
216 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
218 mode_int = atomic_modes[ATOMIC_TYPE_INT];
219 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
221 /* there's no real void type in firm */
222 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
224 /* initialize pointer modes */
226 ir_mode_sort sort = irms_reference;
227 unsigned bit_size = machine_size;
229 ir_mode_arithmetic arithmetic = irma_twos_complement;
230 unsigned modulo_shift
231 = bit_size < machine_size ? machine_size : bit_size;
233 snprintf(name, sizeof(name), "p%u", machine_size);
234 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
237 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
238 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
240 /* Hmm, pointers should be machine size */
241 set_modeP_data(ptr_mode);
242 set_modeP_code(ptr_mode);
245 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
247 assert(kind <= ATOMIC_TYPE_LAST);
248 return atomic_modes[kind];
251 static ir_node *get_vla_size(array_type_t *const type)
253 ir_node *size_node = type->size_node;
254 if (size_node == NULL) {
255 size_node = expression_to_firm(type->size_expression);
256 type->size_node = size_node;
262 * Return a node representing the size of a type.
264 static ir_node *get_type_size_node(type_t *type)
266 type = skip_typeref(type);
268 if (is_type_array(type) && type->array.is_vla) {
269 ir_node *size_node = get_vla_size(&type->array);
270 ir_node *elem_size = get_type_size_node(type->array.element_type);
271 ir_mode *mode = get_irn_mode(size_node);
272 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
276 ir_mode *mode = get_ir_mode_storage(type_size_t);
278 sym.type_p = get_ir_type(type);
279 return new_SymConst(mode, sym, symconst_type_size);
282 static unsigned count_parameters(const function_type_t *function_type)
286 function_parameter_t *parameter = function_type->parameters;
287 for ( ; parameter != NULL; parameter = parameter->next) {
295 * Creates a Firm type for an atomic type
297 static ir_type *create_atomic_type(atomic_type_kind_t akind)
299 ir_mode *mode = atomic_modes[akind];
300 ident *id = get_mode_ident(mode);
301 ir_type *irtype = new_type_primitive(id, mode);
302 il_alignment_t alignment = get_atomic_type_alignment(akind);
304 set_type_alignment_bytes(irtype, alignment);
310 * Creates a Firm type for a complex type
312 static ir_type *create_complex_type(const complex_type_t *type)
314 atomic_type_kind_t kind = type->akind;
315 ir_mode *mode = atomic_modes[kind];
316 ident *id = get_mode_ident(mode);
320 /* FIXME: finish the array */
325 * Creates a Firm type for an imaginary type
327 static ir_type *create_imaginary_type(imaginary_type_t *type)
329 atomic_type_kind_t kind = type->akind;
330 ir_mode *mode = atomic_modes[kind];
331 ident *id = get_mode_ident(mode);
332 ir_type *irtype = new_type_primitive(id, mode);
333 il_alignment_t alignment = get_type_alignment((type_t*) type);
335 set_type_alignment_bytes(irtype, alignment);
341 * return type of a parameter (and take transparent union gnu extension into
344 static type_t *get_parameter_type(type_t *orig_type)
346 type_t *type = skip_typeref(orig_type);
347 if (is_type_union(type)
348 && get_type_modifiers(orig_type) & DM_TRANSPARENT_UNION) {
349 compound_t *compound = type->compound.compound;
350 type = compound->members.entities->declaration.type;
356 static ir_type *create_method_type(const function_type_t *function_type, bool for_closure)
358 type_t *return_type = skip_typeref(function_type->return_type);
360 ident *id = id_unique("functiontype.%u");
361 int n_parameters = count_parameters(function_type) + (for_closure ? 1 : 0);
362 int n_results = return_type == type_void ? 0 : 1;
363 ir_type *irtype = new_type_method(id, n_parameters, n_results);
365 if (return_type != type_void) {
366 ir_type *restype = get_ir_type(return_type);
367 set_method_res_type(irtype, 0, restype);
370 function_parameter_t *parameter = function_type->parameters;
373 ir_type *p_irtype = get_ir_type(type_void_ptr);
374 set_method_param_type(irtype, n, p_irtype);
377 for ( ; parameter != NULL; parameter = parameter->next) {
378 type_t *type = get_parameter_type(parameter->type);
379 ir_type *p_irtype = get_ir_type(type);
380 set_method_param_type(irtype, n, p_irtype);
384 bool is_variadic = function_type->variadic;
387 set_method_variadicity(irtype, variadicity_variadic);
389 unsigned cc = get_method_calling_convention(irtype);
390 switch (function_type->calling_convention) {
391 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
394 set_method_calling_convention(irtype, SET_CDECL(cc));
401 /* only non-variadic function can use stdcall, else use cdecl */
402 set_method_calling_convention(irtype, SET_STDCALL(cc));
408 /* only non-variadic function can use fastcall, else use cdecl */
409 set_method_calling_convention(irtype, SET_FASTCALL(cc));
413 /* Hmm, leave default, not accepted by the parser yet. */
418 set_method_calling_convention(irtype, get_method_calling_convention(irtype) | cc_this_call);
423 static ir_type *create_pointer_type(pointer_type_t *type)
425 type_t *points_to = type->points_to;
426 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
427 ir_type *ir_type = new_type_pointer(id_unique("pointer.%u"),
428 ir_points_to, mode_P_data);
433 static ir_type *create_reference_type(reference_type_t *type)
435 type_t *refers_to = type->refers_to;
436 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
437 ir_type *ir_type = new_type_pointer(id_unique("reference.%u"),
438 ir_refers_to, mode_P_data);
443 static ir_type *create_array_type(array_type_t *type)
445 type_t *element_type = type->element_type;
446 ir_type *ir_element_type = get_ir_type(element_type);
448 ident *id = id_unique("array.%u");
449 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
451 const int align = get_type_alignment_bytes(ir_element_type);
452 set_type_alignment_bytes(ir_type, align);
454 if (type->size_constant) {
455 int n_elements = type->size;
457 set_array_bounds_int(ir_type, 0, 0, n_elements);
459 size_t elemsize = get_type_size_bytes(ir_element_type);
460 if (elemsize % align > 0) {
461 elemsize += align - (elemsize % align);
463 set_type_size_bytes(ir_type, n_elements * elemsize);
465 set_array_lower_bound_int(ir_type, 0, 0);
467 set_type_state(ir_type, layout_fixed);
473 * Return the signed integer type of size bits.
475 * @param size the size
477 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
480 static ir_mode *s_modes[64 + 1] = {NULL, };
484 if (size <= 0 || size > 64)
487 mode = s_modes[size];
491 snprintf(name, sizeof(name), "bf_I%u", size);
492 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
493 size <= 32 ? 32 : size );
494 s_modes[size] = mode;
498 snprintf(name, sizeof(name), "I%u", size);
499 ident *id = new_id_from_str(name);
500 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
501 res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
502 set_primitive_base_type(res, base_tp);
508 * Return the unsigned integer type of size bits.
510 * @param size the size
512 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
515 static ir_mode *u_modes[64 + 1] = {NULL, };
519 if (size <= 0 || size > 64)
522 mode = u_modes[size];
526 snprintf(name, sizeof(name), "bf_U%u", size);
527 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
528 size <= 32 ? 32 : size );
529 u_modes[size] = mode;
534 snprintf(name, sizeof(name), "U%u", size);
535 ident *id = new_id_from_str(name);
536 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
537 res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
538 set_primitive_base_type(res, base_tp);
543 static ir_type *create_bitfield_type(bitfield_type_t *const type)
545 type_t *base = skip_typeref(type->base_type);
546 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
547 ir_type *irbase = get_ir_type(base);
549 unsigned size = type->bit_size;
551 assert(!is_type_float(base));
552 if (is_type_signed(base)) {
553 return get_signed_int_type_for_bit_size(irbase, size);
555 return get_unsigned_int_type_for_bit_size(irbase, size);
559 #define INVALID_TYPE ((ir_type_ptr)-1)
562 COMPOUND_IS_STRUCT = false,
563 COMPOUND_IS_UNION = true
567 * Construct firm type from ast struct type.
569 static ir_type *create_compound_type(compound_type_t *type,
570 bool incomplete, bool is_union)
572 compound_t *compound = type->compound;
574 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
575 return compound->irtype;
578 symbol_t *symbol = compound->base.symbol;
580 if (symbol != NULL) {
581 id = new_id_from_str(symbol->string);
584 id = id_unique("__anonymous_union.%u");
586 id = id_unique("__anonymous_struct.%u");
589 dbg_info *dbgi = get_dbg_info(&compound->base.source_position);
593 irtype = new_d_type_union(id, dbgi);
595 irtype = new_d_type_struct(id, dbgi);
598 compound->irtype_complete = false;
599 compound->irtype = irtype;
605 layout_union_type(type);
607 layout_struct_type(type);
610 compound->irtype_complete = true;
612 entity_t *entry = compound->members.entities;
613 for ( ; entry != NULL; entry = entry->base.next) {
614 if (entry->kind != ENTITY_COMPOUND_MEMBER)
617 symbol_t *symbol = entry->base.symbol;
618 type_t *entry_type = entry->declaration.type;
620 if (symbol == NULL) {
621 /* anonymous bitfield member, skip */
622 if (entry_type->kind == TYPE_BITFIELD)
624 assert(entry_type->kind == TYPE_COMPOUND_STRUCT
625 || entry_type->kind == TYPE_COMPOUND_UNION);
626 ident = id_unique("anon.%u");
628 ident = new_id_from_str(symbol->string);
631 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
633 ir_type *entry_irtype = get_ir_type(entry_type);
634 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
636 set_entity_offset(entity, entry->compound_member.offset);
637 set_entity_offset_bits_remainder(entity,
638 entry->compound_member.bit_offset);
640 assert(entry->declaration.kind == DECLARATION_KIND_UNKNOWN);
641 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
642 entry->compound_member.entity = entity;
645 set_type_alignment_bytes(irtype, compound->alignment);
646 set_type_size_bytes(irtype, compound->size);
647 set_type_state(irtype, layout_fixed);
652 static ir_type *create_enum_type(enum_type_t *const type)
654 type->base.firm_type = ir_type_int;
656 ir_mode *const mode = mode_int;
657 tarval *const one = get_mode_one(mode);
658 tarval * tv_next = get_tarval_null(mode);
660 bool constant_folding_old = constant_folding;
661 constant_folding = true;
663 enum_t *enume = type->enume;
664 entity_t *entry = enume->base.next;
665 for (; entry != NULL; entry = entry->base.next) {
666 if (entry->kind != ENTITY_ENUM_VALUE)
669 expression_t *const init = entry->enum_value.value;
671 ir_node *const cnst = expression_to_firm(init);
672 if (!is_Const(cnst)) {
673 panic("couldn't fold constant");
675 tv_next = get_Const_tarval(cnst);
677 entry->enum_value.tv = tv_next;
678 tv_next = tarval_add(tv_next, one);
681 constant_folding = constant_folding_old;
683 return create_atomic_type(type->akind);
686 static ir_type *get_ir_type_incomplete(type_t *type)
688 assert(type != NULL);
689 type = skip_typeref(type);
691 if (type->base.firm_type != NULL) {
692 assert(type->base.firm_type != INVALID_TYPE);
693 return type->base.firm_type;
696 switch (type->kind) {
697 case TYPE_COMPOUND_STRUCT:
698 return create_compound_type(&type->compound, true, COMPOUND_IS_STRUCT);
699 case TYPE_COMPOUND_UNION:
700 return create_compound_type(&type->compound, true, COMPOUND_IS_UNION);
702 return get_ir_type(type);
706 ir_type *get_ir_type(type_t *type)
708 assert(type != NULL);
710 type = skip_typeref(type);
712 if (type->base.firm_type != NULL) {
713 assert(type->base.firm_type != INVALID_TYPE);
714 return type->base.firm_type;
717 ir_type *firm_type = NULL;
718 switch (type->kind) {
720 /* Happens while constant folding, when there was an error */
721 return create_atomic_type(ATOMIC_TYPE_VOID);
724 firm_type = create_atomic_type(type->atomic.akind);
727 firm_type = create_complex_type(&type->complex);
730 firm_type = create_imaginary_type(&type->imaginary);
733 firm_type = create_method_type(&type->function, false);
736 firm_type = create_pointer_type(&type->pointer);
739 firm_type = create_reference_type(&type->reference);
742 firm_type = create_array_type(&type->array);
744 case TYPE_COMPOUND_STRUCT:
745 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_STRUCT);
747 case TYPE_COMPOUND_UNION:
748 firm_type = create_compound_type(&type->compound, false, COMPOUND_IS_UNION);
751 firm_type = create_enum_type(&type->enumt);
754 firm_type = get_ir_type(type->builtin.real_type);
757 firm_type = create_bitfield_type(&type->bitfield);
765 if (firm_type == NULL)
766 panic("unknown type found");
768 type->base.firm_type = firm_type;
772 static ir_mode *get_ir_mode_storage(type_t *type)
774 ir_type *irtype = get_ir_type(type);
776 /* firm doesn't report a mode for arrays somehow... */
777 if (is_Array_type(irtype)) {
781 ir_mode *mode = get_type_mode(irtype);
782 assert(mode != NULL);
786 static ir_mode *get_ir_mode_arithmetic(type_t *type)
788 ir_mode *mode = get_ir_mode_storage(type);
789 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
790 return mode_float_arithmetic;
796 /** Names of the runtime functions. */
797 static const struct {
798 int id; /**< the rts id */
799 int n_res; /**< number of return values */
800 const char *name; /**< the name of the rts function */
801 int n_params; /**< number of parameters */
802 unsigned flags; /**< language flags */
804 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
805 { rts_abort, 0, "abort", 0, _C89 },
806 { rts_alloca, 1, "alloca", 1, _ALL },
807 { rts_abs, 1, "abs", 1, _C89 },
808 { rts_labs, 1, "labs", 1, _C89 },
809 { rts_llabs, 1, "llabs", 1, _C99 },
810 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
812 { rts_fabs, 1, "fabs", 1, _C89 },
813 { rts_sqrt, 1, "sqrt", 1, _C89 },
814 { rts_cbrt, 1, "cbrt", 1, _C99 },
815 { rts_exp, 1, "exp", 1, _C89 },
816 { rts_exp2, 1, "exp2", 1, _C89 },
817 { rts_exp10, 1, "exp10", 1, _GNUC },
818 { rts_log, 1, "log", 1, _C89 },
819 { rts_log2, 1, "log2", 1, _C89 },
820 { rts_log10, 1, "log10", 1, _C89 },
821 { rts_pow, 1, "pow", 2, _C89 },
822 { rts_sin, 1, "sin", 1, _C89 },
823 { rts_cos, 1, "cos", 1, _C89 },
824 { rts_tan, 1, "tan", 1, _C89 },
825 { rts_asin, 1, "asin", 1, _C89 },
826 { rts_acos, 1, "acos", 1, _C89 },
827 { rts_atan, 1, "atan", 1, _C89 },
828 { rts_sinh, 1, "sinh", 1, _C89 },
829 { rts_cosh, 1, "cosh", 1, _C89 },
830 { rts_tanh, 1, "tanh", 1, _C89 },
832 { rts_fabsf, 1, "fabsf", 1, _C99 },
833 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
834 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
835 { rts_expf, 1, "expf", 1, _C99 },
836 { rts_exp2f, 1, "exp2f", 1, _C99 },
837 { rts_exp10f, 1, "exp10f", 1, _GNUC },
838 { rts_logf, 1, "logf", 1, _C99 },
839 { rts_log2f, 1, "log2f", 1, _C99 },
840 { rts_log10f, 1, "log10f", 1, _C99 },
841 { rts_powf, 1, "powf", 2, _C99 },
842 { rts_sinf, 1, "sinf", 1, _C99 },
843 { rts_cosf, 1, "cosf", 1, _C99 },
844 { rts_tanf, 1, "tanf", 1, _C99 },
845 { rts_asinf, 1, "asinf", 1, _C99 },
846 { rts_acosf, 1, "acosf", 1, _C99 },
847 { rts_atanf, 1, "atanf", 1, _C99 },
848 { rts_sinhf, 1, "sinhf", 1, _C99 },
849 { rts_coshf, 1, "coshf", 1, _C99 },
850 { rts_tanhf, 1, "tanhf", 1, _C99 },
852 { rts_fabsl, 1, "fabsl", 1, _C99 },
853 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
854 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
855 { rts_expl, 1, "expl", 1, _C99 },
856 { rts_exp2l, 1, "exp2l", 1, _C99 },
857 { rts_exp10l, 1, "exp10l", 1, _GNUC },
858 { rts_logl, 1, "logl", 1, _C99 },
859 { rts_log2l, 1, "log2l", 1, _C99 },
860 { rts_log10l, 1, "log10l", 1, _C99 },
861 { rts_powl, 1, "powl", 2, _C99 },
862 { rts_sinl, 1, "sinl", 1, _C99 },
863 { rts_cosl, 1, "cosl", 1, _C99 },
864 { rts_tanl, 1, "tanl", 1, _C99 },
865 { rts_asinl, 1, "asinl", 1, _C99 },
866 { rts_acosl, 1, "acosl", 1, _C99 },
867 { rts_atanl, 1, "atanl", 1, _C99 },
868 { rts_sinhl, 1, "sinhl", 1, _C99 },
869 { rts_coshl, 1, "coshl", 1, _C99 },
870 { rts_tanhl, 1, "tanhl", 1, _C99 },
872 { rts_strcmp, 1, "strcmp", 2, _C89 },
873 { rts_strncmp, 1, "strncmp", 3, _C89 },
874 { rts_strcpy, 1, "strcpy", 2, _C89 },
875 { rts_strlen, 1, "strlen", 1, _C89 },
876 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
877 { rts_mempcpy, 1, "mempcpy", 3, _GNUC },
878 { rts_memmove, 1, "memmove", 3, _C89 }, /* HMM, man say its C99 */
879 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
880 { rts_memcmp, 1, "memcmp", 3, _C89 }, /* HMM, man say its C99 */
883 static ident *rts_idents[lengthof(rts_data)];
885 static ident* (*create_ld_ident)(entity_t*) = create_name_linux_elf;
888 * Handle GNU attributes for entities
890 * @param ent the entity
891 * @param decl the routine declaration
893 static void handle_decl_modifiers(ir_entity *irentity, entity_t *entity)
895 assert(is_declaration(entity));
896 decl_modifiers_t modifiers = entity->declaration.modifiers;
897 if (modifiers & DM_PURE) {
898 /* TRUE if the declaration includes the GNU
899 __attribute__((pure)) specifier. */
900 set_entity_additional_property(irentity, mtp_property_pure);
902 if (modifiers & DM_CONST) {
903 set_entity_additional_property(irentity, mtp_property_const);
904 have_const_functions = true;
906 if (modifiers & DM_USED) {
907 /* TRUE if the declaration includes the GNU
908 __attribute__((used)) specifier. */
909 set_entity_stickyness(irentity, stickyness_sticky);
913 static bool is_main(entity_t *entity)
915 static symbol_t *sym_main = NULL;
916 if (sym_main == NULL) {
917 sym_main = symbol_table_insert("main");
920 if (entity->base.symbol != sym_main)
922 /* must be in outermost scope */
923 if (entity->base.parent_scope != ¤t_translation_unit->scope)
930 * Creates an entity representing a function.
932 * @param declaration the function declaration
933 * @param owner_type the owner type of this function, NULL
934 * for global functions
936 static ir_entity *get_function_entity(entity_t *entity, ir_type *owner_type)
938 assert(entity->kind == ENTITY_FUNCTION);
939 if (entity->function.irentity != NULL) {
940 return entity->function.irentity;
943 if (is_main(entity)) {
944 /* force main to C linkage */
945 type_t *type = entity->declaration.type;
946 assert(is_type_function(type));
947 if (type->function.linkage != LINKAGE_C) {
948 type_t *new_type = duplicate_type(type);
949 new_type->function.linkage = LINKAGE_C;
950 type = identify_new_type(new_type);
951 entity->declaration.type = type;
955 symbol_t *symbol = entity->base.symbol;
956 ident *id = new_id_from_str(symbol->string);
959 /* already an entity defined? */
960 ir_entity *irentity = entitymap_get(&entitymap, symbol);
961 bool const has_body = entity->function.statement != NULL;
962 if (irentity != NULL) {
963 if (get_entity_visibility(irentity) == visibility_external_allocated
965 set_entity_visibility(irentity, visibility_external_visible);
970 ir_type *ir_type_method;
971 if (entity->function.need_closure)
972 ir_type_method = create_method_type(&entity->declaration.type->function, true);
974 ir_type_method = get_ir_type(entity->declaration.type);
976 bool nested_function = false;
977 if (owner_type == NULL)
978 owner_type = get_glob_type();
980 nested_function = true;
982 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
983 irentity = new_d_entity(owner_type, id, ir_type_method, dbgi);
987 ld_id = id_unique("inner.%u");
989 ld_id = create_ld_ident(entity);
990 set_entity_ld_ident(irentity, ld_id);
992 handle_decl_modifiers(irentity, entity);
994 if (! nested_function) {
995 /* static inline => local
996 * extern inline => local
997 * inline without definition => local
998 * inline with definition => external_visible */
999 storage_class_tag_t const storage_class
1000 = (storage_class_tag_t) entity->declaration.storage_class;
1001 bool const is_inline = entity->function.is_inline;
1003 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1004 set_entity_visibility(irentity, visibility_external_visible);
1005 } else if (storage_class == STORAGE_CLASS_STATIC ||
1006 (is_inline && has_body)) {
1008 /* this entity was declared, but is defined nowhere */
1009 set_entity_peculiarity(irentity, peculiarity_description);
1011 set_entity_visibility(irentity, visibility_local);
1012 } else if (has_body) {
1013 set_entity_visibility(irentity, visibility_external_visible);
1015 set_entity_visibility(irentity, visibility_external_allocated);
1018 /* nested functions are always local */
1019 set_entity_visibility(irentity, visibility_local);
1021 set_entity_allocation(irentity, allocation_static);
1023 /* We should check for file scope here, but as long as we compile C only
1024 this is not needed. */
1025 if (! firm_opt.freestanding && !has_body) {
1026 /* check for a known runtime function */
1027 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1028 if (id != rts_idents[i])
1031 /* ignore those rts functions not necessary needed for current mode */
1032 if ((c_mode & rts_data[i].flags) == 0)
1034 assert(rts_entities[rts_data[i].id] == NULL);
1035 rts_entities[rts_data[i].id] = irentity;
1039 entitymap_insert(&entitymap, symbol, irentity);
1042 entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1043 entity->function.irentity = irentity;
1048 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1050 ir_mode *value_mode = get_irn_mode(value);
1052 if (value_mode == dest_mode || is_Bad(value))
1055 if (dest_mode == mode_b) {
1056 ir_node *zero = new_Const(get_mode_null(value_mode));
1057 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1058 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1062 return new_d_Conv(dbgi, value, dest_mode);
1066 * Creates a Const node representing a constant.
1068 static ir_node *const_to_firm(const const_expression_t *cnst)
1070 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1071 type_t *type = skip_typeref(cnst->base.type);
1072 ir_mode *mode = get_ir_mode_storage(type);
1077 if (mode_is_float(mode)) {
1078 tv = new_tarval_from_double(cnst->v.float_value, mode);
1080 if (mode_is_signed(mode)) {
1081 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1083 len = snprintf(buf, sizeof(buf), "%llu",
1084 (unsigned long long) cnst->v.int_value);
1086 tv = new_tarval_from_str(buf, len, mode);
1089 ir_node *res = new_d_Const(dbgi, tv);
1090 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1091 return create_conv(dbgi, res, mode_arith);
1095 * Creates a Const node representing a character constant.
1097 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1099 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1100 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1103 size_t const size = cnst->v.character.size;
1104 if (size == 1 && char_is_signed) {
1105 v = (signed char)cnst->v.character.begin[0];
1108 for (size_t i = 0; i < size; ++i) {
1109 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1113 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1114 tarval *tv = new_tarval_from_str(buf, len, mode);
1116 return new_d_Const(dbgi, tv);
1120 * Creates a Const node representing a wide character constant.
1122 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1124 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1125 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1127 long long int v = cnst->v.wide_character.begin[0];
1130 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1131 tarval *tv = new_tarval_from_str(buf, len, mode);
1133 return new_d_Const(dbgi, tv);
1137 * Allocate an area of size bytes aligned at alignment
1140 static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment) {
1141 static unsigned area_cnt = 0;
1144 snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
1145 ident *name = new_id_from_str(buf);
1147 ir_type *tp = new_type_array(id_mangle_u(get_type_ident(frame_type), name), 1, ir_type_char);
1148 set_array_bounds_int(tp, 0, 0, size);
1149 set_type_alignment_bytes(tp, alignment);
1151 ir_entity *area = new_entity(frame_type, name, tp);
1153 /* mark this entity as compiler generated */
1154 set_entity_compiler_generated(area, 1);
1159 * Return a node representing a trampoline reagion
1160 * for a given entity.
1162 * @param dbgi debug info
1163 * @param entity the entity
1165 static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
1167 ir_entity *region = NULL;
1170 if (current_trampolines != NULL) {
1171 for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
1172 if (current_trampolines[i].function == entity) {
1173 region = current_trampolines[i].region;
1178 current_trampolines = NEW_ARR_F(trampoline_region, 0);
1180 ir_graph *irg = current_ir_graph;
1181 if (region == NULL) {
1182 /* create a new region */
1183 ir_type *frame_tp = get_irg_frame_type(irg);
1184 trampoline_region reg;
1185 reg.function = entity;
1187 reg.region = alloc_trampoline(frame_tp,
1188 be_params->trampoline_size,
1189 be_params->trampoline_align);
1190 ARR_APP1(trampoline_region, current_trampolines, reg);
1191 region = reg.region;
1193 return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
1199 * Creates a SymConst for a given entity.
1201 * @param dbgi debug info
1202 * @param mode the (reference) mode for the SymConst
1203 * @param entity the entity
1205 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1208 assert(entity != NULL);
1209 union symconst_symbol sym;
1210 sym.entity_p = entity;
1211 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1215 * Creates a SymConst for a given trampoline of an entity.
1217 * @param dbgi debug info
1218 * @param mode the (reference) mode for the SymConst
1219 * @param entity the entity
1221 static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
1224 assert(entity != NULL);
1226 in[0] = get_trampoline_region(dbgi, entity);
1227 in[1] = create_symconst(dbgi, mode, entity);
1228 in[2] = get_irg_frame(current_ir_graph);
1230 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_inner_trampoline, get_unknown_type());
1231 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1232 return new_Proj(irn, mode, pn_Builtin_1_result);
1236 * Creates a SymConst node representing a string constant.
1238 * @param src_pos the source position of the string constant
1239 * @param id_prefix a prefix for the name of the generated string constant
1240 * @param value the value of the string constant
1242 static ir_node *string_to_firm(const source_position_t *const src_pos,
1243 const char *const id_prefix,
1244 const string_t *const value)
1246 ir_type *const global_type = get_glob_type();
1247 dbg_info *const dbgi = get_dbg_info(src_pos);
1248 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1249 ir_type_const_char, dbgi);
1251 ident *const id = id_unique(id_prefix);
1252 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1253 set_entity_ld_ident(entity, id);
1254 set_entity_variability(entity, variability_constant);
1255 set_entity_allocation(entity, allocation_static);
1256 set_entity_visibility(entity, visibility_local);
1258 ir_type *const elem_type = ir_type_const_char;
1259 ir_mode *const mode = get_type_mode(elem_type);
1261 const char* const string = value->begin;
1262 const size_t slen = value->size;
1264 set_array_lower_bound_int(type, 0, 0);
1265 set_array_upper_bound_int(type, 0, slen);
1266 set_type_size_bytes(type, slen);
1267 set_type_state(type, layout_fixed);
1269 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1270 for (size_t i = 0; i < slen; ++i) {
1271 tvs[i] = new_tarval_from_long(string[i], mode);
1274 set_array_entity_values(entity, tvs, slen);
1277 return create_symconst(dbgi, mode_P_data, entity);
1281 * Creates a SymConst node representing a string literal.
1283 * @param literal the string literal
1285 static ir_node *string_literal_to_firm(
1286 const string_literal_expression_t* literal)
1288 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1293 * Creates a SymConst node representing a wide string literal.
1295 * @param literal the wide string literal
1297 static ir_node *wide_string_literal_to_firm(
1298 const wide_string_literal_expression_t* const literal)
1300 ir_type *const global_type = get_glob_type();
1301 ir_type *const elem_type = ir_type_wchar_t;
1302 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1303 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1306 ident *const id = id_unique("Lstr.%u");
1307 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1308 set_entity_ld_ident(entity, id);
1309 set_entity_variability(entity, variability_constant);
1310 set_entity_allocation(entity, allocation_static);
1312 ir_mode *const mode = get_type_mode(elem_type);
1314 const wchar_rep_t *const string = literal->value.begin;
1315 const size_t slen = literal->value.size;
1317 set_array_lower_bound_int(type, 0, 0);
1318 set_array_upper_bound_int(type, 0, slen);
1319 set_type_size_bytes(type, slen);
1320 set_type_state(type, layout_fixed);
1322 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1323 for (size_t i = 0; i < slen; ++i) {
1324 tvs[i] = new_tarval_from_long(string[i], mode);
1327 set_array_entity_values(entity, tvs, slen);
1330 return create_symconst(dbgi, mode_P_data, entity);
1334 * Dereference an address.
1336 * @param dbgi debug info
1337 * @param type the type of the dereferenced result (the points_to type)
1338 * @param addr the address to dereference
1340 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1341 ir_node *const addr)
1343 ir_type *irtype = get_ir_type(type);
1344 if (is_compound_type(irtype)
1345 || is_Method_type(irtype)
1346 || is_Array_type(irtype)) {
1350 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1351 ? cons_volatile : cons_none;
1352 ir_mode *const mode = get_type_mode(irtype);
1353 ir_node *const memory = get_store();
1354 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1355 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1356 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1358 set_store(load_mem);
1360 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1361 return create_conv(dbgi, load_res, mode_arithmetic);
1365 * Creates a strict Conv (to the node's mode) if necessary.
1367 * @param dbgi debug info
1368 * @param node the node to strict conv
1370 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1372 ir_mode *mode = get_irn_mode(node);
1374 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1376 if (!mode_is_float(mode))
1379 /* check if there is already a Conv */
1380 if (is_Conv(node)) {
1381 /* convert it into a strict Conv */
1382 set_Conv_strict(node, 1);
1386 /* otherwise create a new one */
1387 return new_d_strictConv(dbgi, node, mode);
1391 * Returns the address of a global variable.
1393 * @param dbgi debug info
1394 * @param variable the variable
1396 static ir_node *get_global_var_address(dbg_info *const dbgi,
1397 const variable_t *const variable)
1399 ir_entity *const irentity = variable->v.entity;
1400 if (variable->thread_local) {
1401 ir_node *const no_mem = new_NoMem();
1402 ir_node *const tls = get_irg_tls(current_ir_graph);
1403 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1405 return create_symconst(dbgi, mode_P_data, irentity);
1410 * Returns the correct base address depending on whether it is a parameter or a
1411 * normal local variable.
1413 static ir_node *get_local_frame(ir_entity *const ent)
1415 ir_graph *const irg = current_ir_graph;
1416 const ir_type *const owner = get_entity_owner(ent);
1417 if (owner == current_outer_frame || owner == current_outer_value_type) {
1418 assert(current_static_link != NULL);
1419 return current_static_link;
1421 return get_irg_frame(irg);
1426 * Keep all memory edges of the given block.
1428 static void keep_all_memory(ir_node *block)
1430 ir_node *old = get_cur_block();
1432 set_cur_block(block);
1433 keep_alive(get_store());
1434 /* TODO: keep all memory edges from restricted pointers */
1438 static ir_node *reference_expression_enum_value_to_firm(
1439 const reference_expression_t *ref)
1441 entity_t *entity = ref->entity;
1442 type_t *type = skip_typeref(entity->enum_value.enum_type);
1443 /* make sure the type is constructed */
1444 (void) get_ir_type(type);
1446 return new_Const(entity->enum_value.tv);
1449 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1451 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1452 entity_t *entity = ref->entity;
1453 assert(is_declaration(entity));
1454 type_t *type = skip_typeref(entity->declaration.type);
1456 /* make sure the type is constructed */
1457 (void) get_ir_type(type);
1459 /* for gcc compatibility we have to produce (dummy) addresses for some
1461 if (entity->kind == ENTITY_FUNCTION && entity->function.btk != bk_none) {
1462 if (warning.other) {
1463 warningf(&ref->base.source_position,
1464 "taking address of builtin '%Y'", ref->entity->base.symbol);
1467 /* simply create a NULL pointer */
1468 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
1469 ir_node *res = new_Const_long(mode, 0);
1474 switch ((declaration_kind_t) entity->declaration.kind) {
1475 case DECLARATION_KIND_UNKNOWN:
1478 case DECLARATION_KIND_LOCAL_VARIABLE: {
1479 ir_mode *const mode = get_ir_mode_storage(type);
1480 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1481 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1483 case DECLARATION_KIND_PARAMETER: {
1484 ir_mode *const mode = get_ir_mode_storage(type);
1485 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1486 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1488 case DECLARATION_KIND_FUNCTION: {
1489 ir_mode *const mode = get_ir_mode_storage(type);
1490 return create_symconst(dbgi, mode, entity->function.irentity);
1492 case DECLARATION_KIND_INNER_FUNCTION: {
1493 ir_mode *const mode = get_ir_mode_storage(type);
1494 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1495 /* inner function not using the closure */
1496 return create_symconst(dbgi, mode, entity->function.irentity);
1498 /* need trampoline here */
1499 return create_trampoline(dbgi, mode, entity->function.irentity);
1502 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1503 const variable_t *variable = &entity->variable;
1504 ir_node *const addr = get_global_var_address(dbgi, variable);
1505 return deref_address(dbgi, variable->base.type, addr);
1508 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1509 ir_entity *irentity = entity->variable.v.entity;
1510 ir_node *frame = get_local_frame(irentity);
1511 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1512 return deref_address(dbgi, entity->declaration.type, sel);
1514 case DECLARATION_KIND_PARAMETER_ENTITY: {
1515 ir_entity *irentity = entity->parameter.v.entity;
1516 ir_node *frame = get_local_frame(irentity);
1517 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1518 return deref_address(dbgi, entity->declaration.type, sel);
1521 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1522 return entity->variable.v.vla_base;
1524 case DECLARATION_KIND_COMPOUND_MEMBER:
1525 panic("not implemented reference type");
1528 panic("reference to declaration with unknown type found");
1531 static ir_node *reference_addr(const reference_expression_t *ref)
1533 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1534 entity_t *entity = ref->entity;
1535 assert(is_declaration(entity));
1537 switch((declaration_kind_t) entity->declaration.kind) {
1538 case DECLARATION_KIND_UNKNOWN:
1540 case DECLARATION_KIND_PARAMETER:
1541 case DECLARATION_KIND_LOCAL_VARIABLE:
1542 /* you can store to a local variable (so we don't panic but return NULL
1543 * as an indicator for no real address) */
1545 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1546 ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
1549 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1550 ir_entity *irentity = entity->variable.v.entity;
1551 ir_node *frame = get_local_frame(irentity);
1552 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1556 case DECLARATION_KIND_PARAMETER_ENTITY: {
1557 ir_entity *irentity = entity->parameter.v.entity;
1558 ir_node *frame = get_local_frame(irentity);
1559 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1564 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1565 return entity->variable.v.vla_base;
1567 case DECLARATION_KIND_FUNCTION: {
1568 type_t *const type = skip_typeref(entity->declaration.type);
1569 ir_mode *const mode = get_ir_mode_storage(type);
1570 return create_symconst(dbgi, mode, entity->function.irentity);
1573 case DECLARATION_KIND_INNER_FUNCTION: {
1574 type_t *const type = skip_typeref(entity->declaration.type);
1575 ir_mode *const mode = get_ir_mode_storage(type);
1576 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1577 /* inner function not using the closure */
1578 return create_symconst(dbgi, mode, entity->function.irentity);
1580 /* need trampoline here */
1581 return create_trampoline(dbgi, mode, entity->function.irentity);
1585 case DECLARATION_KIND_COMPOUND_MEMBER:
1586 panic("not implemented reference type");
1589 panic("reference to declaration with unknown type found");
1593 * Generate an unary builtin.
1595 * @param kind the builtin kind to generate
1596 * @param op the operand
1597 * @param function_type the function type for the GNU builtin routine
1598 * @param db debug info
1600 static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1603 in[0] = expression_to_firm(op);
1605 ir_type *tp = get_ir_type(function_type);
1606 ir_type *res = get_method_res_type(tp, 0);
1607 ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), 1, in, kind, tp);
1608 set_irn_pinned(irn, op_pin_state_floats);
1609 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1613 * Generate a pinned unary builtin.
1615 * @param kind the builtin kind to generate
1616 * @param op the operand
1617 * @param function_type the function type for the GNU builtin routine
1618 * @param db debug info
1620 static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
1623 in[0] = expression_to_firm(op);
1625 ir_type *tp = get_ir_type(function_type);
1626 ir_type *res = get_method_res_type(tp, 0);
1627 ir_node *mem = get_store();
1628 ir_node *irn = new_d_Builtin(db, mem, 1, in, kind, tp);
1629 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1630 return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
1635 * Generate an binary-void-return builtin.
1637 * @param kind the builtin kind to generate
1638 * @param op1 the first operand
1639 * @param op2 the second operand
1640 * @param function_type the function type for the GNU builtin routine
1641 * @param db debug info
1643 static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1, expression_t *op2,
1644 type_t *function_type, dbg_info *db)
1647 in[0] = expression_to_firm(op1);
1648 in[1] = expression_to_firm(op2);
1650 ir_type *tp = get_ir_type(function_type);
1651 ir_node *mem = get_store();
1652 ir_node *irn = new_d_Builtin(db, mem, 2, in, kind, tp);
1653 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1658 * Transform calls to builtin functions.
1660 static ir_node *process_builtin_call(const call_expression_t *call)
1662 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1664 assert(call->function->kind == EXPR_REFERENCE);
1665 reference_expression_t *builtin = &call->function->reference;
1667 type_t *type = skip_typeref(builtin->base.type);
1668 assert(is_type_pointer(type));
1670 type_t *function_type = skip_typeref(type->pointer.points_to);
1672 switch (builtin->entity->function.btk) {
1673 case bk_gnu_builtin_alloca: {
1674 if (call->arguments == NULL || call->arguments->next != NULL) {
1675 panic("invalid number of parameters on __builtin_alloca");
1677 expression_t *argument = call->arguments->expression;
1678 ir_node *size = expression_to_firm(argument);
1680 ir_node *store = get_store();
1681 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1683 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1685 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1690 case bk_gnu_builtin_huge_val:
1691 case bk_gnu_builtin_inf:
1692 case bk_gnu_builtin_inff:
1693 case bk_gnu_builtin_infl: {
1694 type_t *type = function_type->function.return_type;
1695 ir_mode *mode = get_ir_mode_arithmetic(type);
1696 tarval *tv = get_mode_infinite(mode);
1697 ir_node *res = new_d_Const(dbgi, tv);
1700 case bk_gnu_builtin_nan:
1701 case bk_gnu_builtin_nanf:
1702 case bk_gnu_builtin_nanl: {
1703 /* Ignore string for now... */
1704 assert(is_type_function(function_type));
1705 type_t *type = function_type->function.return_type;
1706 ir_mode *mode = get_ir_mode_arithmetic(type);
1707 tarval *tv = get_mode_NAN(mode);
1708 ir_node *res = new_d_Const(dbgi, tv);
1711 case bk_gnu_builtin_expect: {
1712 expression_t *argument = call->arguments->expression;
1713 return _expression_to_firm(argument);
1715 case bk_gnu_builtin_va_end:
1716 /* evaluate the argument of va_end for its side effects */
1717 _expression_to_firm(call->arguments->expression);
1719 case bk_gnu_builtin_frame_address: {
1720 expression_t *const expression = call->arguments->expression;
1721 bool val = fold_constant_to_bool(expression);
1724 return get_irg_frame(current_ir_graph);
1726 /* get the argument */
1729 in[0] = expression_to_firm(expression);
1730 in[1] = get_irg_frame(current_ir_graph);
1731 ir_type *tp = get_ir_type(function_type);
1732 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_frame_address, tp);
1733 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1736 case bk_gnu_builtin_return_address: {
1738 expression_t *const expression = call->arguments->expression;
1741 in[0] = expression_to_firm(expression);
1742 in[1] = get_irg_frame(current_ir_graph);
1743 ir_type *tp = get_ir_type(function_type);
1744 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1745 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1747 case bk_gnu_builtin_ffs:
1748 return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
1749 case bk_gnu_builtin_clz:
1750 return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
1751 case bk_gnu_builtin_ctz:
1752 return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
1753 case bk_gnu_builtin_popcount:
1754 case bk_ms__popcount:
1755 return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
1756 case bk_gnu_builtin_parity:
1757 return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
1758 case bk_gnu_builtin_prefetch: {
1759 call_argument_t *const args = call->arguments;
1760 expression_t *const addr = args->expression;
1763 in[0] = _expression_to_firm(addr);
1764 if (args->next != NULL) {
1765 expression_t *const rw = args->next->expression;
1767 in[1] = _expression_to_firm(rw);
1769 if (args->next->next != NULL) {
1770 expression_t *const locality = args->next->next->expression;
1772 in[2] = expression_to_firm(locality);
1774 in[2] = new_Const_long(mode_int, 3);
1777 in[1] = new_Const_long(mode_int, 0);
1778 in[2] = new_Const_long(mode_int, 3);
1780 ir_type *tp = get_ir_type(function_type);
1781 ir_node *irn = new_d_Builtin(dbgi, get_store(), 3, in, ir_bk_prefetch, tp);
1782 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1785 case bk_gnu_builtin_trap:
1788 ir_type *tp = get_ir_type(function_type);
1789 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_trap, tp);
1790 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1793 case bk_ms__debugbreak: {
1794 ir_type *tp = get_ir_type(function_type);
1795 ir_node *irn = new_d_Builtin(dbgi, get_store(), 0, NULL, ir_bk_debugbreak, tp);
1796 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1799 case bk_ms_ReturnAddress: {
1802 in[0] = new_Const_long(mode_int, 0);
1803 in[1] = get_irg_frame(current_ir_graph);
1804 ir_type *tp = get_ir_type(function_type);
1805 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), 2, in, ir_bk_return_address, tp);
1806 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1809 case bk_ms_rotl64: {
1810 ir_node *val = expression_to_firm(call->arguments->expression);
1811 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1812 ir_mode *mode = get_irn_mode(val);
1813 return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
1816 case bk_ms_rotr64: {
1817 ir_node *val = expression_to_firm(call->arguments->expression);
1818 ir_node *shf = expression_to_firm(call->arguments->next->expression);
1819 ir_mode *mode = get_irn_mode(val);
1820 ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
1821 ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
1822 return new_d_Rotl(dbgi, val, sub, mode);
1824 case bk_ms_byteswap_ushort:
1825 case bk_ms_byteswap_ulong:
1826 case bk_ms_byteswap_uint64:
1827 return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
1830 case bk_ms__indword:
1831 return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
1832 case bk_ms__outbyte:
1833 case bk_ms__outword:
1834 case bk_ms__outdword:
1835 return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
1836 call->arguments->next->expression, function_type, dbgi);
1838 panic("unsupported builtin found");
1843 * Transform a call expression.
1844 * Handles some special cases, like alloca() calls, which must be resolved
1845 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1846 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1849 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1851 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1852 assert(get_cur_block() != NULL);
1854 expression_t *function = call->function;
1855 if (function->kind == EXPR_REFERENCE) {
1856 const reference_expression_t *ref = &function->reference;
1857 entity_t *entity = ref->entity;
1859 if (entity->kind == ENTITY_FUNCTION) {
1860 if (entity->function.btk != bk_none) {
1861 return process_builtin_call(call);
1864 ir_entity *irentity = entity->function.irentity;
1865 if (irentity == NULL)
1866 irentity = get_function_entity(entity, NULL);
1868 if (irentity == rts_entities[rts_alloca]) {
1869 /* handle alloca() call */
1870 expression_t *argument = call->arguments->expression;
1871 ir_node *size = expression_to_firm(argument);
1872 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1874 size = create_conv(dbgi, size, mode);
1876 ir_node *store = get_store();
1877 ir_node *alloca = new_d_Alloc(dbgi, store, size,
1878 firm_unknown_type, stack_alloc);
1879 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1881 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1887 ir_node *callee = expression_to_firm(function);
1889 type_t *type = skip_typeref(function->base.type);
1890 assert(is_type_pointer(type));
1891 pointer_type_t *pointer_type = &type->pointer;
1892 type_t *points_to = skip_typeref(pointer_type->points_to);
1893 assert(is_type_function(points_to));
1894 function_type_t *function_type = &points_to->function;
1896 int n_parameters = 0;
1897 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1898 ir_type *new_method_type = NULL;
1899 if (function_type->variadic || function_type->unspecified_parameters) {
1900 const call_argument_t *argument = call->arguments;
1901 for ( ; argument != NULL; argument = argument->next) {
1905 /* we need to construct a new method type matching the call
1907 int n_res = get_method_n_ress(ir_method_type);
1908 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1909 n_parameters, n_res, dbgi);
1910 set_method_calling_convention(new_method_type,
1911 get_method_calling_convention(ir_method_type));
1912 set_method_additional_properties(new_method_type,
1913 get_method_additional_properties(ir_method_type));
1914 set_method_variadicity(new_method_type,
1915 get_method_variadicity(ir_method_type));
1917 for (int i = 0; i < n_res; ++i) {
1918 set_method_res_type(new_method_type, i,
1919 get_method_res_type(ir_method_type, i));
1921 argument = call->arguments;
1922 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
1923 expression_t *expression = argument->expression;
1924 ir_type *irtype = get_ir_type(expression->base.type);
1925 set_method_param_type(new_method_type, i, irtype);
1927 ir_method_type = new_method_type;
1929 n_parameters = get_method_n_params(ir_method_type);
1932 ir_node *in[n_parameters];
1934 const call_argument_t *argument = call->arguments;
1935 for (int n = 0; n < n_parameters; ++n) {
1936 expression_t *expression = argument->expression;
1937 ir_node *arg_node = expression_to_firm(expression);
1939 type_t *type = skip_typeref(expression->base.type);
1940 if (!is_type_compound(type)) {
1941 ir_mode *mode = get_ir_mode_storage(expression->base.type);
1942 arg_node = create_conv(dbgi, arg_node, mode);
1943 arg_node = do_strict_conv(dbgi, arg_node);
1948 argument = argument->next;
1951 ir_node *store = get_store();
1952 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1954 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1957 type_t *return_type = skip_typeref(function_type->return_type);
1958 ir_node *result = NULL;
1960 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1961 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1963 if (is_type_scalar(return_type)) {
1964 ir_mode *mode = get_ir_mode_storage(return_type);
1965 result = new_d_Proj(dbgi, resproj, mode, 0);
1966 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
1967 result = create_conv(NULL, result, mode_arith);
1969 ir_mode *mode = mode_P_data;
1970 result = new_d_Proj(dbgi, resproj, mode, 0);
1974 if (function->kind == EXPR_REFERENCE &&
1975 function->reference.entity->declaration.modifiers & DM_NORETURN) {
1976 /* A dead end: Keep the Call and the Block. Also place all further
1977 * nodes into a new and unreachable block. */
1979 keep_alive(get_cur_block());
1986 static void statement_to_firm(statement_t *statement);
1987 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1989 static ir_node *expression_to_addr(const expression_t *expression);
1990 static ir_node *create_condition_evaluation(const expression_t *expression,
1991 ir_node *true_block,
1992 ir_node *false_block);
1994 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1997 if (!is_type_compound(type)) {
1998 ir_mode *mode = get_ir_mode_storage(type);
1999 value = create_conv(dbgi, value, mode);
2000 value = do_strict_conv(dbgi, value);
2003 ir_node *memory = get_store();
2005 if (is_type_scalar(type)) {
2006 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
2007 ? cons_volatile : cons_none;
2008 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
2009 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2010 set_store(store_mem);
2012 ir_type *irtype = get_ir_type(type);
2013 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
2014 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
2015 set_store(copyb_mem);
2019 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
2021 tarval *all_one = get_mode_all_one(mode);
2022 int mode_size = get_mode_size_bits(mode);
2024 assert(offset >= 0);
2026 assert(offset + size <= mode_size);
2027 if (size == mode_size) {
2031 long shiftr = get_mode_size_bits(mode) - size;
2032 long shiftl = offset;
2033 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
2034 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
2035 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
2036 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
2041 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
2042 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
2044 ir_type *entity_type = get_entity_type(entity);
2045 ir_type *base_type = get_primitive_base_type(entity_type);
2046 assert(base_type != NULL);
2047 ir_mode *mode = get_type_mode(base_type);
2049 value = create_conv(dbgi, value, mode);
2051 /* kill upper bits of value and shift to right position */
2052 int bitoffset = get_entity_offset_bits_remainder(entity);
2053 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2055 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
2056 ir_node *mask_node = new_d_Const(dbgi, mask);
2057 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
2058 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
2059 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
2060 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
2062 /* load current value */
2063 ir_node *mem = get_store();
2064 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
2065 set_volatile ? cons_volatile : cons_none);
2066 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2067 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2068 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
2069 tarval *inv_mask = tarval_not(shift_mask);
2070 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
2071 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
2073 /* construct new value and store */
2074 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
2075 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
2076 set_volatile ? cons_volatile : cons_none);
2077 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
2078 set_store(store_mem);
2080 return value_masked;
2083 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
2086 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2087 type_t *type = expression->base.type;
2088 ir_mode *mode = get_ir_mode_storage(type);
2089 ir_node *mem = get_store();
2090 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
2091 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
2092 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
2094 load_res = create_conv(dbgi, load_res, mode_int);
2096 set_store(load_mem);
2098 /* kill upper bits */
2099 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
2100 ir_entity *entity = expression->compound_entry->compound_member.entity;
2101 int bitoffset = get_entity_offset_bits_remainder(entity);
2102 ir_type *entity_type = get_entity_type(entity);
2103 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
2104 long shift_bitsl = machine_size - bitoffset - bitsize;
2105 assert(shift_bitsl >= 0);
2106 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
2107 ir_node *countl = new_d_Const(dbgi, tvl);
2108 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
2110 long shift_bitsr = bitoffset + shift_bitsl;
2111 assert(shift_bitsr <= (long) machine_size);
2112 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
2113 ir_node *countr = new_d_Const(dbgi, tvr);
2115 if (mode_is_signed(mode)) {
2116 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
2118 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
2121 return create_conv(dbgi, shiftr, mode);
2124 /* make sure the selected compound type is constructed */
2125 static void construct_select_compound(const select_expression_t *expression)
2127 type_t *type = skip_typeref(expression->compound->base.type);
2128 if (is_type_pointer(type)) {
2129 type = type->pointer.points_to;
2131 (void) get_ir_type(type);
2134 static ir_node *set_value_for_expression_addr(const expression_t *expression,
2135 ir_node *value, ir_node *addr)
2137 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2138 type_t *type = skip_typeref(expression->base.type);
2140 if (!is_type_compound(type)) {
2141 ir_mode *mode = get_ir_mode_storage(type);
2142 value = create_conv(dbgi, value, mode);
2143 value = do_strict_conv(dbgi, value);
2146 if (expression->kind == EXPR_REFERENCE) {
2147 const reference_expression_t *ref = &expression->reference;
2149 entity_t *entity = ref->entity;
2150 assert(is_declaration(entity));
2151 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2152 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2153 set_value(entity->variable.v.value_number, value);
2155 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2156 set_value(entity->parameter.v.value_number, value);
2162 addr = expression_to_addr(expression);
2163 assert(addr != NULL);
2165 if (expression->kind == EXPR_SELECT) {
2166 const select_expression_t *select = &expression->select;
2168 construct_select_compound(select);
2170 entity_t *entity = select->compound_entry;
2171 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
2172 if (entity->declaration.type->kind == TYPE_BITFIELD) {
2173 ir_entity *irentity = entity->compound_member.entity;
2175 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
2176 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
2182 assign_value(dbgi, addr, type, value);
2186 static void set_value_for_expression(const expression_t *expression,
2189 set_value_for_expression_addr(expression, value, NULL);
2192 static ir_node *get_value_from_lvalue(const expression_t *expression,
2195 if (expression->kind == EXPR_REFERENCE) {
2196 const reference_expression_t *ref = &expression->reference;
2198 entity_t *entity = ref->entity;
2199 assert(entity->kind == ENTITY_VARIABLE
2200 || entity->kind == ENTITY_PARAMETER);
2201 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2203 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2204 value_number = entity->variable.v.value_number;
2205 assert(addr == NULL);
2206 type_t *type = skip_typeref(expression->base.type);
2207 ir_mode *mode = get_ir_mode_storage(type);
2208 ir_node *res = get_value(value_number, mode);
2209 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2210 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2211 value_number = entity->parameter.v.value_number;
2212 assert(addr == NULL);
2213 type_t *type = skip_typeref(expression->base.type);
2214 ir_mode *mode = get_ir_mode_storage(type);
2215 ir_node *res = get_value(value_number, mode);
2216 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2220 assert(addr != NULL);
2221 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2224 if (expression->kind == EXPR_SELECT &&
2225 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2226 construct_select_compound(&expression->select);
2227 value = bitfield_extract_to_firm(&expression->select, addr);
2229 value = deref_address(dbgi, expression->base.type, addr);
2236 static ir_node *create_incdec(const unary_expression_t *expression)
2238 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2239 const expression_t *value_expr = expression->value;
2240 ir_node *addr = expression_to_addr(value_expr);
2241 ir_node *value = get_value_from_lvalue(value_expr, addr);
2243 type_t *type = skip_typeref(expression->base.type);
2244 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2247 if (is_type_pointer(type)) {
2248 pointer_type_t *pointer_type = &type->pointer;
2249 offset = get_type_size_node(pointer_type->points_to);
2251 assert(is_type_arithmetic(type));
2252 offset = new_Const(get_mode_one(mode));
2256 ir_node *store_value;
2257 switch(expression->base.kind) {
2258 case EXPR_UNARY_POSTFIX_INCREMENT:
2260 store_value = new_d_Add(dbgi, value, offset, mode);
2262 case EXPR_UNARY_POSTFIX_DECREMENT:
2264 store_value = new_d_Sub(dbgi, value, offset, mode);
2266 case EXPR_UNARY_PREFIX_INCREMENT:
2267 result = new_d_Add(dbgi, value, offset, mode);
2268 store_value = result;
2270 case EXPR_UNARY_PREFIX_DECREMENT:
2271 result = new_d_Sub(dbgi, value, offset, mode);
2272 store_value = result;
2275 panic("no incdec expr in create_incdec");
2278 set_value_for_expression_addr(value_expr, store_value, addr);
2283 static bool is_local_variable(expression_t *expression)
2285 if (expression->kind != EXPR_REFERENCE)
2287 reference_expression_t *ref_expr = &expression->reference;
2288 entity_t *entity = ref_expr->entity;
2289 if (entity->kind != ENTITY_VARIABLE)
2291 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2292 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2295 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2298 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2299 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2300 case EXPR_BINARY_NOTEQUAL:
2301 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2302 case EXPR_BINARY_ISLESS:
2303 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2304 case EXPR_BINARY_ISLESSEQUAL:
2305 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2306 case EXPR_BINARY_ISGREATER:
2307 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2308 case EXPR_BINARY_ISGREATEREQUAL:
2309 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2310 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2315 panic("trying to get pn_Cmp from non-comparison binexpr type");
2319 * Handle the assume optimizer hint: check if a Confirm
2320 * node can be created.
2322 * @param dbi debug info
2323 * @param expr the IL assume expression
2325 * we support here only some simple cases:
2330 static ir_node *handle_assume_compare(dbg_info *dbi,
2331 const binary_expression_t *expression)
2333 expression_t *op1 = expression->left;
2334 expression_t *op2 = expression->right;
2335 entity_t *var2, *var = NULL;
2336 ir_node *res = NULL;
2339 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2341 if (is_local_variable(op1) && is_local_variable(op2)) {
2342 var = op1->reference.entity;
2343 var2 = op2->reference.entity;
2345 type_t *const type = skip_typeref(var->declaration.type);
2346 ir_mode *const mode = get_ir_mode_storage(type);
2348 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2349 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2351 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2352 set_value(var2->variable.v.value_number, res);
2354 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2355 set_value(var->variable.v.value_number, res);
2361 if (is_local_variable(op1) && is_constant_expression(op2)) {
2362 var = op1->reference.entity;
2364 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2365 cmp_val = get_inversed_pnc(cmp_val);
2366 var = op2->reference.entity;
2371 type_t *const type = skip_typeref(var->declaration.type);
2372 ir_mode *const mode = get_ir_mode_storage(type);
2374 res = get_value(var->variable.v.value_number, mode);
2375 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2376 set_value(var->variable.v.value_number, res);
2382 * Handle the assume optimizer hint.
2384 * @param dbi debug info
2385 * @param expr the IL assume expression
2387 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2389 switch(expression->kind) {
2390 case EXPR_BINARY_EQUAL:
2391 case EXPR_BINARY_NOTEQUAL:
2392 case EXPR_BINARY_LESS:
2393 case EXPR_BINARY_LESSEQUAL:
2394 case EXPR_BINARY_GREATER:
2395 case EXPR_BINARY_GREATEREQUAL:
2396 return handle_assume_compare(dbi, &expression->binary);
2402 static ir_node *cast_to_firm(const unary_expression_t *expression)
2404 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2405 type_t *type = skip_typeref(expression->base.type);
2406 const expression_t *value = expression->value;
2407 ir_node *value_node = expression_to_firm(value);
2409 if (!is_type_scalar(type)) {
2410 /* make sure firm type is constructed */
2411 (void) get_ir_type(type);
2415 type_t *from_type = skip_typeref(value->base.type);
2416 ir_mode *mode = get_ir_mode_storage(type);
2417 /* check for conversion from / to __based types */
2418 if (is_type_pointer(type) && is_type_pointer(from_type)) {
2419 const variable_t *from_var = from_type->pointer.base_variable;
2420 const variable_t *to_var = type->pointer.base_variable;
2421 if (from_var != to_var) {
2422 if (from_var != NULL) {
2423 ir_node *const addr = get_global_var_address(dbgi, from_var);
2424 ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
2425 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
2427 if (to_var != NULL) {
2428 ir_node *const addr = get_global_var_address(dbgi, to_var);
2429 ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
2430 value_node = new_d_Sub(dbgi, value_node, base, mode);
2435 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2436 ir_node *node = create_conv(dbgi, value_node, mode);
2437 node = do_strict_conv(dbgi, node);
2438 node = create_conv(dbgi, node, mode_arith);
2440 if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
2441 /* bool adjustments (have to compare to get 0/1 value */
2442 ir_node *zero = new_Const(get_mode_null(mode_arith));
2443 ir_node *cmp = new_d_Cmp(dbgi, node, zero);
2444 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
2445 ir_node *one = new_Const_long(mode_arith, 1);
2446 node = new_d_Mux(dbgi, proj, zero, one, mode_arith);
2452 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2454 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2455 type_t *type = skip_typeref(expression->base.type);
2457 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2458 return expression_to_addr(expression->value);
2460 const expression_t *value = expression->value;
2462 switch(expression->base.kind) {
2463 case EXPR_UNARY_NEGATE: {
2464 ir_node *value_node = expression_to_firm(value);
2465 ir_mode *mode = get_ir_mode_arithmetic(type);
2466 return new_d_Minus(dbgi, value_node, mode);
2468 case EXPR_UNARY_PLUS:
2469 return expression_to_firm(value);
2470 case EXPR_UNARY_BITWISE_NEGATE: {
2471 ir_node *value_node = expression_to_firm(value);
2472 ir_mode *mode = get_ir_mode_arithmetic(type);
2473 return new_d_Not(dbgi, value_node, mode);
2475 case EXPR_UNARY_NOT: {
2476 ir_node *value_node = _expression_to_firm(value);
2477 value_node = create_conv(dbgi, value_node, mode_b);
2478 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2481 case EXPR_UNARY_DEREFERENCE: {
2482 ir_node *value_node = expression_to_firm(value);
2483 type_t *value_type = skip_typeref(value->base.type);
2484 assert(is_type_pointer(value_type));
2486 /* check for __based */
2487 const variable_t *const base_var = value_type->pointer.base_variable;
2488 if (base_var != NULL) {
2489 ir_node *const addr = get_global_var_address(dbgi, base_var);
2490 ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
2491 value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
2493 type_t *points_to = value_type->pointer.points_to;
2494 return deref_address(dbgi, points_to, value_node);
2496 case EXPR_UNARY_POSTFIX_INCREMENT:
2497 case EXPR_UNARY_POSTFIX_DECREMENT:
2498 case EXPR_UNARY_PREFIX_INCREMENT:
2499 case EXPR_UNARY_PREFIX_DECREMENT:
2500 return create_incdec(expression);
2501 case EXPR_UNARY_CAST_IMPLICIT:
2502 case EXPR_UNARY_CAST:
2503 return cast_to_firm(expression);
2504 case EXPR_UNARY_ASSUME:
2505 if (firm_opt.confirm)
2506 return handle_assume(dbgi, value);
2513 panic("invalid UNEXPR type found");
2517 * produces a 0/1 depending of the value of a mode_b node
2519 static ir_node *produce_condition_result(const expression_t *expression,
2520 ir_mode *mode, dbg_info *dbgi)
2522 ir_node *cur_block = get_cur_block();
2524 ir_node *one_block = new_immBlock();
2525 set_cur_block(one_block);
2526 ir_node *one = new_Const(get_mode_one(mode));
2527 ir_node *jmp_one = new_d_Jmp(dbgi);
2529 ir_node *zero_block = new_immBlock();
2530 set_cur_block(zero_block);
2531 ir_node *zero = new_Const(get_mode_null(mode));
2532 ir_node *jmp_zero = new_d_Jmp(dbgi);
2534 set_cur_block(cur_block);
2535 create_condition_evaluation(expression, one_block, zero_block);
2536 mature_immBlock(one_block);
2537 mature_immBlock(zero_block);
2539 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2540 new_Block(2, in_cf);
2542 ir_node *in[2] = { one, zero };
2543 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2548 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2549 ir_node *value, type_t *type)
2551 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2552 assert(is_type_pointer(type));
2553 pointer_type_t *const pointer_type = &type->pointer;
2554 type_t *const points_to = skip_typeref(pointer_type->points_to);
2555 ir_node * elem_size = get_type_size_node(points_to);
2556 elem_size = create_conv(dbgi, elem_size, mode);
2557 value = create_conv(dbgi, value, mode);
2558 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2562 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2563 ir_node *left, ir_node *right)
2566 type_t *type_left = skip_typeref(expression->left->base.type);
2567 type_t *type_right = skip_typeref(expression->right->base.type);
2569 expression_kind_t kind = expression->base.kind;
2572 case EXPR_BINARY_SHIFTLEFT:
2573 case EXPR_BINARY_SHIFTRIGHT:
2574 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2575 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2576 mode = get_irn_mode(left);
2577 right = create_conv(dbgi, right, mode_uint);
2580 case EXPR_BINARY_SUB:
2581 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2582 const pointer_type_t *const ptr_type = &type_left->pointer;
2584 mode = get_ir_mode_arithmetic(expression->base.type);
2585 ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
2586 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2587 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2588 ir_node *const no_mem = new_NoMem();
2589 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2590 mode, op_pin_state_floats);
2591 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2594 case EXPR_BINARY_SUB_ASSIGN:
2595 if (is_type_pointer(type_left)) {
2596 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2597 mode = get_ir_mode_arithmetic(type_left);
2602 case EXPR_BINARY_ADD:
2603 case EXPR_BINARY_ADD_ASSIGN:
2604 if (is_type_pointer(type_left)) {
2605 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2606 mode = get_ir_mode_arithmetic(type_left);
2608 } else if (is_type_pointer(type_right)) {
2609 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2610 mode = get_ir_mode_arithmetic(type_right);
2617 mode = get_ir_mode_arithmetic(type_right);
2618 left = create_conv(dbgi, left, mode);
2623 case EXPR_BINARY_ADD_ASSIGN:
2624 case EXPR_BINARY_ADD:
2625 return new_d_Add(dbgi, left, right, mode);
2626 case EXPR_BINARY_SUB_ASSIGN:
2627 case EXPR_BINARY_SUB:
2628 return new_d_Sub(dbgi, left, right, mode);
2629 case EXPR_BINARY_MUL_ASSIGN:
2630 case EXPR_BINARY_MUL:
2631 return new_d_Mul(dbgi, left, right, mode);
2632 case EXPR_BINARY_BITWISE_AND:
2633 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2634 return new_d_And(dbgi, left, right, mode);
2635 case EXPR_BINARY_BITWISE_OR:
2636 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2637 return new_d_Or(dbgi, left, right, mode);
2638 case EXPR_BINARY_BITWISE_XOR:
2639 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2640 return new_d_Eor(dbgi, left, right, mode);
2641 case EXPR_BINARY_SHIFTLEFT:
2642 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2643 return new_d_Shl(dbgi, left, right, mode);
2644 case EXPR_BINARY_SHIFTRIGHT:
2645 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2646 if (mode_is_signed(mode)) {
2647 return new_d_Shrs(dbgi, left, right, mode);
2649 return new_d_Shr(dbgi, left, right, mode);
2651 case EXPR_BINARY_DIV:
2652 case EXPR_BINARY_DIV_ASSIGN: {
2653 ir_node *pin = new_Pin(new_NoMem());
2656 if (mode_is_float(mode)) {
2657 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2658 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2660 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2661 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2665 case EXPR_BINARY_MOD:
2666 case EXPR_BINARY_MOD_ASSIGN: {
2667 ir_node *pin = new_Pin(new_NoMem());
2668 assert(!mode_is_float(mode));
2669 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2670 op_pin_state_floats);
2671 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2675 panic("unexpected expression kind");
2679 static ir_node *create_lazy_op(const binary_expression_t *expression)
2681 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2682 type_t *type = skip_typeref(expression->base.type);
2683 ir_mode *mode = get_ir_mode_arithmetic(type);
2685 if (is_constant_expression(expression->left)) {
2686 bool val = fold_constant_to_bool(expression->left);
2687 expression_kind_t ekind = expression->base.kind;
2688 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2689 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2691 return new_Const(get_mode_null(mode));
2695 return new_Const(get_mode_one(mode));
2699 if (is_constant_expression(expression->right)) {
2700 bool valr = fold_constant_to_bool(expression->right);
2702 new_Const(get_mode_one(mode)) :
2703 new_Const(get_mode_null(mode));
2706 return produce_condition_result(expression->right, mode, dbgi);
2709 return produce_condition_result((const expression_t*) expression, mode,
2713 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2714 ir_node *right, ir_mode *mode);
2716 static ir_node *create_assign_binop(const binary_expression_t *expression)
2718 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2719 const expression_t *left_expr = expression->left;
2720 type_t *type = skip_typeref(left_expr->base.type);
2721 ir_mode *left_mode = get_ir_mode_storage(type);
2722 ir_node *right = expression_to_firm(expression->right);
2723 ir_node *left_addr = expression_to_addr(left_expr);
2724 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2725 ir_node *result = create_op(dbgi, expression, left, right);
2727 result = create_conv(dbgi, result, left_mode);
2728 result = do_strict_conv(dbgi, result);
2730 result = set_value_for_expression_addr(left_expr, result, left_addr);
2732 if (!is_type_compound(type)) {
2733 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2734 result = create_conv(dbgi, result, mode_arithmetic);
2739 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2741 expression_kind_t kind = expression->base.kind;
2744 case EXPR_BINARY_EQUAL:
2745 case EXPR_BINARY_NOTEQUAL:
2746 case EXPR_BINARY_LESS:
2747 case EXPR_BINARY_LESSEQUAL:
2748 case EXPR_BINARY_GREATER:
2749 case EXPR_BINARY_GREATEREQUAL:
2750 case EXPR_BINARY_ISGREATER:
2751 case EXPR_BINARY_ISGREATEREQUAL:
2752 case EXPR_BINARY_ISLESS:
2753 case EXPR_BINARY_ISLESSEQUAL:
2754 case EXPR_BINARY_ISLESSGREATER:
2755 case EXPR_BINARY_ISUNORDERED: {
2756 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2757 ir_node *left = expression_to_firm(expression->left);
2758 ir_node *right = expression_to_firm(expression->right);
2759 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2760 long pnc = get_pnc(kind, expression->left->base.type);
2761 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2764 case EXPR_BINARY_ASSIGN: {
2765 ir_node *addr = expression_to_addr(expression->left);
2766 ir_node *right = expression_to_firm(expression->right);
2768 = set_value_for_expression_addr(expression->left, right, addr);
2770 type_t *type = skip_typeref(expression->base.type);
2771 if (!is_type_compound(type)) {
2772 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2773 res = create_conv(NULL, res, mode_arithmetic);
2777 case EXPR_BINARY_ADD:
2778 case EXPR_BINARY_SUB:
2779 case EXPR_BINARY_MUL:
2780 case EXPR_BINARY_DIV:
2781 case EXPR_BINARY_MOD:
2782 case EXPR_BINARY_BITWISE_AND:
2783 case EXPR_BINARY_BITWISE_OR:
2784 case EXPR_BINARY_BITWISE_XOR:
2785 case EXPR_BINARY_SHIFTLEFT:
2786 case EXPR_BINARY_SHIFTRIGHT:
2788 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2789 ir_node *left = expression_to_firm(expression->left);
2790 ir_node *right = expression_to_firm(expression->right);
2791 return create_op(dbgi, expression, left, right);
2793 case EXPR_BINARY_LOGICAL_AND:
2794 case EXPR_BINARY_LOGICAL_OR:
2795 return create_lazy_op(expression);
2796 case EXPR_BINARY_COMMA:
2797 /* create side effects of left side */
2798 (void) expression_to_firm(expression->left);
2799 return _expression_to_firm(expression->right);
2801 case EXPR_BINARY_ADD_ASSIGN:
2802 case EXPR_BINARY_SUB_ASSIGN:
2803 case EXPR_BINARY_MUL_ASSIGN:
2804 case EXPR_BINARY_MOD_ASSIGN:
2805 case EXPR_BINARY_DIV_ASSIGN:
2806 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2807 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2808 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2809 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2810 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2811 return create_assign_binop(expression);
2813 panic("TODO binexpr type");
2817 static ir_node *array_access_addr(const array_access_expression_t *expression)
2819 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2820 ir_node *base_addr = expression_to_firm(expression->array_ref);
2821 ir_node *offset = expression_to_firm(expression->index);
2822 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2823 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2824 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2829 static ir_node *array_access_to_firm(
2830 const array_access_expression_t *expression)
2832 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2833 ir_node *addr = array_access_addr(expression);
2834 type_t *type = revert_automatic_type_conversion(
2835 (const expression_t*) expression);
2836 type = skip_typeref(type);
2838 return deref_address(dbgi, type, addr);
2841 static long get_offsetof_offset(const offsetof_expression_t *expression)
2843 type_t *orig_type = expression->type;
2846 designator_t *designator = expression->designator;
2847 for ( ; designator != NULL; designator = designator->next) {
2848 type_t *type = skip_typeref(orig_type);
2849 /* be sure the type is constructed */
2850 (void) get_ir_type(type);
2852 if (designator->symbol != NULL) {
2853 assert(is_type_compound(type));
2854 symbol_t *symbol = designator->symbol;
2856 compound_t *compound = type->compound.compound;
2857 entity_t *iter = compound->members.entities;
2858 for ( ; iter != NULL; iter = iter->base.next) {
2859 if (iter->base.symbol == symbol) {
2863 assert(iter != NULL);
2865 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2866 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2867 offset += get_entity_offset(iter->compound_member.entity);
2869 orig_type = iter->declaration.type;
2871 expression_t *array_index = designator->array_index;
2872 assert(designator->array_index != NULL);
2873 assert(is_type_array(type));
2875 long index = fold_constant_to_int(array_index);
2876 ir_type *arr_type = get_ir_type(type);
2877 ir_type *elem_type = get_array_element_type(arr_type);
2878 long elem_size = get_type_size_bytes(elem_type);
2880 offset += index * elem_size;
2882 orig_type = type->array.element_type;
2889 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2891 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2892 long offset = get_offsetof_offset(expression);
2893 tarval *tv = new_tarval_from_long(offset, mode);
2894 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2896 return new_d_Const(dbgi, tv);
2899 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2900 ir_entity *entity, type_t *type);
2902 static ir_node *compound_literal_to_firm(
2903 const compound_literal_expression_t *expression)
2905 type_t *type = expression->type;
2907 /* create an entity on the stack */
2908 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2910 ident *const id = id_unique("CompLit.%u");
2911 ir_type *const irtype = get_ir_type(type);
2912 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2913 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2914 set_entity_ld_ident(entity, id);
2916 set_entity_variability(entity, variability_uninitialized);
2918 /* create initialisation code */
2919 initializer_t *initializer = expression->initializer;
2920 create_local_initializer(initializer, dbgi, entity, type);
2922 /* create a sel for the compound literal address */
2923 ir_node *frame = get_irg_frame(current_ir_graph);
2924 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2929 * Transform a sizeof expression into Firm code.
2931 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2933 type_t *const type = skip_typeref(expression->type);
2934 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
2935 if (is_type_array(type) && type->array.is_vla
2936 && expression->tp_expression != NULL) {
2937 expression_to_firm(expression->tp_expression);
2940 return get_type_size_node(type);
2943 static entity_t *get_expression_entity(const expression_t *expression)
2945 if (expression->kind != EXPR_REFERENCE)
2948 return expression->reference.entity;
2951 static unsigned get_cparser_entity_alignment(const entity_t *entity)
2953 switch(entity->kind) {
2954 DECLARATION_KIND_CASES
2955 return entity->declaration.alignment;
2958 return entity->compound.alignment;
2959 case ENTITY_TYPEDEF:
2960 return entity->typedefe.alignment;
2968 * Transform an alignof expression into Firm code.
2970 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2972 unsigned alignment = 0;
2974 const expression_t *tp_expression = expression->tp_expression;
2975 if (tp_expression != NULL) {
2976 entity_t *entity = get_expression_entity(tp_expression);
2977 if (entity != NULL) {
2978 alignment = get_cparser_entity_alignment(entity);
2982 if (alignment == 0) {
2983 type_t *type = expression->type;
2984 alignment = get_type_alignment(type);
2987 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2988 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2989 tarval *tv = new_tarval_from_long(alignment, mode);
2990 return new_d_Const(dbgi, tv);
2993 static void init_ir_types(void);
2995 static tarval *fold_constant_to_tarval(const expression_t *expression)
2997 assert(is_type_valid(skip_typeref(expression->base.type)));
2999 bool constant_folding_old = constant_folding;
3000 constant_folding = true;
3004 assert(is_constant_expression(expression));
3006 ir_graph *old_current_ir_graph = current_ir_graph;
3007 current_ir_graph = get_const_code_irg();
3009 ir_node *cnst = expression_to_firm(expression);
3010 current_ir_graph = old_current_ir_graph;
3012 if (!is_Const(cnst)) {
3013 panic("couldn't fold constant");
3016 constant_folding = constant_folding_old;
3018 tarval *tv = get_Const_tarval(cnst);
3022 long fold_constant_to_int(const expression_t *expression)
3024 if (expression->kind == EXPR_INVALID)
3027 tarval *tv = fold_constant_to_tarval(expression);
3028 if (!tarval_is_long(tv)) {
3029 panic("result of constant folding is not integer");
3032 return get_tarval_long(tv);
3035 bool fold_constant_to_bool(const expression_t *expression)
3037 if (expression->kind == EXPR_INVALID)
3039 tarval *tv = fold_constant_to_tarval(expression);
3040 return !tarval_is_null(tv);
3043 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
3045 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
3047 /* first try to fold a constant condition */
3048 if (is_constant_expression(expression->condition)) {
3049 bool val = fold_constant_to_bool(expression->condition);
3051 expression_t *true_expression = expression->true_expression;
3052 if (true_expression == NULL)
3053 true_expression = expression->condition;
3054 return expression_to_firm(true_expression);
3056 return expression_to_firm(expression->false_expression);
3060 ir_node *cur_block = get_cur_block();
3062 /* create the true block */
3063 ir_node *true_block = new_immBlock();
3064 set_cur_block(true_block);
3066 ir_node *true_val = expression->true_expression != NULL ?
3067 expression_to_firm(expression->true_expression) : NULL;
3068 ir_node *true_jmp = new_Jmp();
3070 /* create the false block */
3071 ir_node *false_block = new_immBlock();
3072 set_cur_block(false_block);
3074 ir_node *false_val = expression_to_firm(expression->false_expression);
3075 ir_node *false_jmp = new_Jmp();
3077 /* create the condition evaluation */
3078 set_cur_block(cur_block);
3079 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
3080 if (expression->true_expression == NULL) {
3081 if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
3082 true_val = cond_expr;
3084 /* Condition ended with a short circuit (&&, ||, !) operation or a
3085 * comparison. Generate a "1" as value for the true branch. */
3086 true_val = new_Const(get_mode_one(mode_Is));
3089 mature_immBlock(true_block);
3090 mature_immBlock(false_block);
3092 /* create the common block */
3093 ir_node *in_cf[2] = { true_jmp, false_jmp };
3094 new_Block(2, in_cf);
3096 /* TODO improve static semantics, so either both or no values are NULL */
3097 if (true_val == NULL || false_val == NULL)
3100 ir_node *in[2] = { true_val, false_val };
3101 ir_mode *mode = get_irn_mode(true_val);
3102 assert(get_irn_mode(false_val) == mode);
3103 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
3109 * Returns an IR-node representing the address of a field.
3111 static ir_node *select_addr(const select_expression_t *expression)
3113 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3115 construct_select_compound(expression);
3117 ir_node *compound_addr = expression_to_firm(expression->compound);
3119 entity_t *entry = expression->compound_entry;
3120 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3121 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
3123 if (constant_folding) {
3124 ir_mode *mode = get_irn_mode(compound_addr);
3125 /* FIXME: here, we need an integer mode with the same number of bits as mode */
3126 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
3127 return new_d_Add(dbgi, compound_addr, ofs, mode);
3129 ir_entity *irentity = entry->compound_member.entity;
3130 assert(irentity != NULL);
3131 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
3135 static ir_node *select_to_firm(const select_expression_t *expression)
3137 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3138 ir_node *addr = select_addr(expression);
3139 type_t *type = revert_automatic_type_conversion(
3140 (const expression_t*) expression);
3141 type = skip_typeref(type);
3143 entity_t *entry = expression->compound_entry;
3144 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3145 type_t *entry_type = skip_typeref(entry->declaration.type);
3147 if (entry_type->kind == TYPE_BITFIELD) {
3148 return bitfield_extract_to_firm(expression, addr);
3151 return deref_address(dbgi, type, addr);
3154 /* Values returned by __builtin_classify_type. */
3155 typedef enum gcc_type_class
3161 enumeral_type_class,
3164 reference_type_class,
3168 function_type_class,
3179 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
3181 type_t *type = expr->type_expression->base.type;
3183 /* FIXME gcc returns different values depending on whether compiling C or C++
3184 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
3187 type = skip_typeref(type);
3188 switch (type->kind) {
3190 const atomic_type_t *const atomic_type = &type->atomic;
3191 switch (atomic_type->akind) {
3192 /* should not be reached */
3193 case ATOMIC_TYPE_INVALID:
3197 /* gcc cannot do that */
3198 case ATOMIC_TYPE_VOID:
3199 tc = void_type_class;
3202 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
3203 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
3204 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
3205 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
3206 case ATOMIC_TYPE_SHORT:
3207 case ATOMIC_TYPE_USHORT:
3208 case ATOMIC_TYPE_INT:
3209 case ATOMIC_TYPE_UINT:
3210 case ATOMIC_TYPE_LONG:
3211 case ATOMIC_TYPE_ULONG:
3212 case ATOMIC_TYPE_LONGLONG:
3213 case ATOMIC_TYPE_ULONGLONG:
3214 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
3215 tc = integer_type_class;
3218 case ATOMIC_TYPE_FLOAT:
3219 case ATOMIC_TYPE_DOUBLE:
3220 case ATOMIC_TYPE_LONG_DOUBLE:
3221 tc = real_type_class;
3224 panic("Unexpected atomic type in classify_type_to_firm().");
3227 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3228 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3229 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3230 case TYPE_ARRAY: /* gcc handles this as pointer */
3231 case TYPE_FUNCTION: /* gcc handles this as pointer */
3232 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3233 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3234 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3236 /* gcc handles this as integer */
3237 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3239 /* gcc classifies the referenced type */
3240 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3243 /* typedef/typeof should be skipped already */
3250 panic("unexpected TYPE classify_type_to_firm().");
3254 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3255 tarval *const tv = new_tarval_from_long(tc, mode_int);
3256 return new_d_Const(dbgi, tv);
3259 static ir_node *function_name_to_firm(
3260 const funcname_expression_t *const expr)
3262 switch(expr->kind) {
3263 case FUNCNAME_FUNCTION:
3264 case FUNCNAME_PRETTY_FUNCTION:
3265 case FUNCNAME_FUNCDNAME:
3266 if (current_function_name == NULL) {
3267 const source_position_t *const src_pos = &expr->base.source_position;
3268 const char *name = current_function_entity->base.symbol->string;
3269 const string_t string = { name, strlen(name) + 1 };
3270 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3272 return current_function_name;
3273 case FUNCNAME_FUNCSIG:
3274 if (current_funcsig == NULL) {
3275 const source_position_t *const src_pos = &expr->base.source_position;
3276 ir_entity *ent = get_irg_entity(current_ir_graph);
3277 const char *const name = get_entity_ld_name(ent);
3278 const string_t string = { name, strlen(name) + 1 };
3279 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3281 return current_funcsig;
3283 panic("Unsupported function name");
3286 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3288 statement_t *statement = expr->statement;
3290 assert(statement->kind == STATEMENT_COMPOUND);
3291 return compound_statement_to_firm(&statement->compound);
3294 static ir_node *va_start_expression_to_firm(
3295 const va_start_expression_t *const expr)
3297 type_t *const type = current_function_entity->declaration.type;
3298 ir_type *const method_type = get_ir_type(type);
3299 int const n = get_method_n_params(method_type) - 1;
3300 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3301 ir_node *const frame = get_irg_frame(current_ir_graph);
3302 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3303 ir_node *const no_mem = new_NoMem();
3304 ir_node *const arg_sel =
3305 new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
3307 type_t *const param_type = expr->parameter->base.type;
3308 ir_node *const cnst = get_type_size_node(param_type);
3309 ir_mode *const mode = get_irn_mode(cnst);
3310 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3311 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3312 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3313 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3314 ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
3315 set_value_for_expression(expr->ap, add);
3320 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3322 type_t *const type = expr->base.type;
3323 expression_t *const ap_expr = expr->ap;
3324 ir_node *const ap_addr = expression_to_addr(ap_expr);
3325 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3326 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3327 ir_node *const res = deref_address(dbgi, type, ap);
3329 ir_node *const cnst = get_type_size_node(expr->base.type);
3330 ir_mode *const mode = get_irn_mode(cnst);
3331 ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
3332 ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
3333 ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
3334 ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
3335 ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
3337 set_value_for_expression_addr(ap_expr, add, ap_addr);
3343 * Generate Firm for a va_copy expression.
3345 static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
3347 ir_node *const src = expression_to_firm(expr->src);
3348 set_value_for_expression(expr->dst, src);
3352 static ir_node *dereference_addr(const unary_expression_t *const expression)
3354 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3355 return expression_to_firm(expression->value);
3359 * Returns a IR-node representing an lvalue of the given expression.
3361 static ir_node *expression_to_addr(const expression_t *expression)
3363 switch(expression->kind) {
3364 case EXPR_ARRAY_ACCESS:
3365 return array_access_addr(&expression->array_access);
3367 return call_expression_to_firm(&expression->call);
3368 case EXPR_COMPOUND_LITERAL:
3369 return compound_literal_to_firm(&expression->compound_literal);
3370 case EXPR_REFERENCE:
3371 return reference_addr(&expression->reference);
3373 return select_addr(&expression->select);
3374 case EXPR_UNARY_DEREFERENCE:
3375 return dereference_addr(&expression->unary);
3379 panic("trying to get address of non-lvalue");
3382 static ir_node *builtin_constant_to_firm(
3383 const builtin_constant_expression_t *expression)
3385 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3388 if (is_constant_expression(expression->value)) {
3393 return new_Const_long(mode, v);
3396 static ir_node *builtin_types_compatible_to_firm(
3397 const builtin_types_compatible_expression_t *expression)
3399 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3400 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3401 long const value = types_compatible(left, right) ? 1 : 0;
3402 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3403 return new_Const_long(mode, value);
3406 static ir_node *get_label_block(label_t *label)
3408 if (label->block != NULL)
3409 return label->block;
3411 /* beware: might be called from create initializer with current_ir_graph
3412 * set to const_code_irg. */
3413 ir_graph *rem = current_ir_graph;
3414 current_ir_graph = current_function;
3416 ir_node *block = new_immBlock();
3418 label->block = block;
3420 ARR_APP1(label_t *, all_labels, label);
3422 current_ir_graph = rem;
3427 * Pointer to a label. This is used for the
3428 * GNU address-of-label extension.
3430 static ir_node *label_address_to_firm(const label_address_expression_t *label)
3432 dbg_info *dbgi = get_dbg_info(&label->base.source_position);
3433 ir_node *block = get_label_block(label->label);
3434 ir_entity *entity = create_Block_entity(block);
3436 symconst_symbol value;
3437 value.entity_p = entity;
3438 return new_d_SymConst(dbgi, mode_P_code, value, symconst_addr_ent);
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 /* revert previous initialisations of other union elements */
3864 if (type->kind == TYPE_COMPOUND_UNION) {
3865 ir_initializer_t *initializer = top->initializer;
3866 if (initializer != NULL
3867 && get_initializer_kind(initializer) == IR_INITIALIZER_COMPOUND) {
3868 /* are we writing to a new element? */
3869 ir_initializer_t *oldi
3870 = get_initializer_compound_value(initializer, index);
3871 if (get_initializer_kind(oldi) == IR_INITIALIZER_NULL) {
3872 /* clear initializer */
3874 = get_initializer_compound_n_entries(initializer);
3875 ir_initializer_t *nulli = get_initializer_null();
3876 for (size_t i = 0; i < len; ++i) {
3877 set_initializer_compound_value(initializer, i,
3884 top->type = orig_type;
3885 top->compound_entry = iter;
3887 orig_type = iter->declaration.type;
3889 expression_t *array_index = designator->array_index;
3890 assert(designator->array_index != NULL);
3891 assert(is_type_array(type));
3893 long index = fold_constant_to_int(array_index);
3896 if (type->array.size_constant) {
3897 long array_size = type->array.size;
3898 assert(index < array_size);
3902 top->type = orig_type;
3903 top->index = (size_t) index;
3904 orig_type = type->array.element_type;
3906 path->top_type = orig_type;
3908 if (designator->next != NULL) {
3909 descend_into_subtype(path);
3913 path->invalid = false;
3916 static void advance_current_object(type_path_t *path)
3918 if (path->invalid) {
3919 /* TODO: handle this... */
3920 panic("invalid initializer in ast2firm (excessive elements)");
3923 type_path_entry_t *top = get_type_path_top(path);
3925 type_t *type = skip_typeref(top->type);
3926 if (is_type_union(type)) {
3927 /* only the first element is initialized in unions */
3928 top->compound_entry = NULL;
3929 } else if (is_type_struct(type)) {
3930 entity_t *entry = top->compound_entry;
3933 entry = entry->base.next;
3934 top->compound_entry = entry;
3935 if (entry != NULL) {
3936 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3937 path->top_type = entry->declaration.type;
3941 assert(is_type_array(type));
3944 if (!type->array.size_constant || top->index < type->array.size) {
3949 /* we're past the last member of the current sub-aggregate, try if we
3950 * can ascend in the type hierarchy and continue with another subobject */
3951 size_t len = ARR_LEN(path->path);
3954 ascend_from_subtype(path);
3955 advance_current_object(path);
3957 path->invalid = true;
3962 static ir_initializer_t *create_ir_initializer(
3963 const initializer_t *initializer, type_t *type);
3965 static ir_initializer_t *create_ir_initializer_value(
3966 const initializer_value_t *initializer)
3968 if (is_type_compound(initializer->value->base.type)) {
3969 panic("initializer creation for compounds not implemented yet");
3971 type_t *type = initializer->value->base.type;
3972 expression_t *expr = initializer->value;
3973 if (initializer_use_bitfield_basetype) {
3974 type_t *skipped = skip_typeref(type);
3975 if (skipped->kind == TYPE_BITFIELD) {
3976 /* remove the bitfield cast... */
3977 assert(expr->kind == EXPR_UNARY_CAST_IMPLICIT);
3978 expr = expr->unary.value;
3979 type = skipped->bitfield.base_type;
3982 ir_node *value = expression_to_firm(expr);
3983 ir_mode *mode = get_ir_mode_storage(type);
3984 value = create_conv(NULL, value, mode);
3985 return create_initializer_const(value);
3988 /** test wether type can be initialized by a string constant */
3989 static bool is_string_type(type_t *type)
3992 if (is_type_pointer(type)) {
3993 inner = skip_typeref(type->pointer.points_to);
3994 } else if(is_type_array(type)) {
3995 inner = skip_typeref(type->array.element_type);
4000 return is_type_integer(inner);
4003 static ir_initializer_t *create_ir_initializer_list(
4004 const initializer_list_t *initializer, type_t *type)
4007 memset(&path, 0, sizeof(path));
4008 path.top_type = type;
4009 path.path = NEW_ARR_F(type_path_entry_t, 0);
4011 descend_into_subtype(&path);
4013 for (size_t i = 0; i < initializer->len; ++i) {
4014 const initializer_t *sub_initializer = initializer->initializers[i];
4016 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
4017 walk_designator(&path, sub_initializer->designator.designator);
4021 if (sub_initializer->kind == INITIALIZER_VALUE) {
4022 /* we might have to descend into types until we're at a scalar
4025 type_t *orig_top_type = path.top_type;
4026 type_t *top_type = skip_typeref(orig_top_type);
4028 if (is_type_scalar(top_type))
4030 descend_into_subtype(&path);
4032 } else if (sub_initializer->kind == INITIALIZER_STRING
4033 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
4034 /* we might have to descend into types until we're at a scalar
4037 type_t *orig_top_type = path.top_type;
4038 type_t *top_type = skip_typeref(orig_top_type);
4040 if (is_string_type(top_type))
4042 descend_into_subtype(&path);
4046 ir_initializer_t *sub_irinitializer
4047 = create_ir_initializer(sub_initializer, path.top_type);
4049 size_t path_len = ARR_LEN(path.path);
4050 assert(path_len >= 1);
4051 type_path_entry_t *entry = & path.path[path_len-1];
4052 ir_initializer_t *tinitializer = entry->initializer;
4053 set_initializer_compound_value(tinitializer, entry->index,
4056 advance_current_object(&path);
4059 assert(ARR_LEN(path.path) >= 1);
4060 ir_initializer_t *result = path.path[0].initializer;
4061 DEL_ARR_F(path.path);
4066 static ir_initializer_t *create_ir_initializer_string(
4067 const initializer_string_t *initializer, type_t *type)
4069 type = skip_typeref(type);
4071 size_t string_len = initializer->string.size;
4072 assert(type->kind == TYPE_ARRAY);
4073 assert(type->array.size_constant);
4074 size_t len = type->array.size;
4075 ir_initializer_t *irinitializer = create_initializer_compound(len);
4077 const char *string = initializer->string.begin;
4078 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
4080 for (size_t i = 0; i < len; ++i) {
4085 tarval *tv = new_tarval_from_long(c, mode);
4086 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4088 set_initializer_compound_value(irinitializer, i, char_initializer);
4091 return irinitializer;
4094 static ir_initializer_t *create_ir_initializer_wide_string(
4095 const initializer_wide_string_t *initializer, type_t *type)
4097 size_t string_len = initializer->string.size;
4098 assert(type->kind == TYPE_ARRAY);
4099 assert(type->array.size_constant);
4100 size_t len = type->array.size;
4101 ir_initializer_t *irinitializer = create_initializer_compound(len);
4103 const wchar_rep_t *string = initializer->string.begin;
4104 ir_mode *mode = get_type_mode(ir_type_wchar_t);
4106 for (size_t i = 0; i < len; ++i) {
4108 if (i < string_len) {
4111 tarval *tv = new_tarval_from_long(c, mode);
4112 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
4114 set_initializer_compound_value(irinitializer, i, char_initializer);
4117 return irinitializer;
4120 static ir_initializer_t *create_ir_initializer(
4121 const initializer_t *initializer, type_t *type)
4123 switch(initializer->kind) {
4124 case INITIALIZER_STRING:
4125 return create_ir_initializer_string(&initializer->string, type);
4127 case INITIALIZER_WIDE_STRING:
4128 return create_ir_initializer_wide_string(&initializer->wide_string,
4131 case INITIALIZER_LIST:
4132 return create_ir_initializer_list(&initializer->list, type);
4134 case INITIALIZER_VALUE:
4135 return create_ir_initializer_value(&initializer->value);
4137 case INITIALIZER_DESIGNATOR:
4138 panic("unexpected designator initializer found");
4140 panic("unknown initializer");
4143 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
4144 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
4146 switch(get_initializer_kind(initializer)) {
4147 case IR_INITIALIZER_NULL: {
4148 /* NULL is undefined for dynamic initializers */
4151 case IR_INITIALIZER_CONST: {
4152 ir_node *node = get_initializer_const_value(initializer);
4153 ir_mode *mode = get_irn_mode(node);
4154 ir_type *ent_type = get_entity_type(entity);
4156 /* is it a bitfield type? */
4157 if (is_Primitive_type(ent_type) &&
4158 get_primitive_base_type(ent_type) != NULL) {
4159 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
4163 assert(get_type_mode(type) == mode);
4164 ir_node *mem = get_store();
4165 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
4166 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4170 case IR_INITIALIZER_TARVAL: {
4171 tarval *tv = get_initializer_tarval_value(initializer);
4172 ir_mode *mode = get_tarval_mode(tv);
4173 ir_node *cnst = new_d_Const(dbgi, tv);
4174 ir_type *ent_type = get_entity_type(entity);
4176 /* is it a bitfield type? */
4177 if (is_Primitive_type(ent_type) &&
4178 get_primitive_base_type(ent_type) != NULL) {
4179 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
4183 assert(get_type_mode(type) == mode);
4184 ir_node *mem = get_store();
4185 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
4186 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
4190 case IR_INITIALIZER_COMPOUND: {
4191 assert(is_compound_type(type));
4193 if (is_Array_type(type)) {
4194 assert(has_array_upper_bound(type, 0));
4195 n_members = get_array_upper_bound_int(type, 0);
4197 n_members = get_compound_n_members(type);
4200 if (get_initializer_compound_n_entries(initializer)
4201 != (unsigned) n_members)
4202 panic("initializer doesn't match compound type");
4204 for (int i = 0; i < n_members; ++i) {
4207 ir_entity *sub_entity;
4208 if (is_Array_type(type)) {
4209 tarval *index_tv = new_tarval_from_long(i, mode_uint);
4210 ir_node *cnst = new_d_Const(dbgi, index_tv);
4211 ir_node *in[1] = { cnst };
4212 irtype = get_array_element_type(type);
4213 sub_entity = get_array_element_entity(type);
4214 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4217 sub_entity = get_compound_member(type, i);
4218 irtype = get_entity_type(sub_entity);
4219 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4223 ir_initializer_t *sub_init
4224 = get_initializer_compound_value(initializer, i);
4226 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4233 panic("invalid IR_INITIALIZER found");
4236 static void create_dynamic_initializer(ir_initializer_t *initializer,
4237 dbg_info *dbgi, ir_entity *entity)
4239 ir_node *frame = get_irg_frame(current_ir_graph);
4240 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4241 ir_type *type = get_entity_type(entity);
4243 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4246 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4247 ir_entity *entity, type_t *type)
4249 ir_node *memory = get_store();
4250 ir_node *nomem = new_NoMem();
4251 ir_node *frame = get_irg_frame(current_ir_graph);
4252 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4254 if (initializer->kind == INITIALIZER_VALUE) {
4255 initializer_value_t *initializer_value = &initializer->value;
4257 ir_node *value = expression_to_firm(initializer_value->value);
4258 type = skip_typeref(type);
4259 assign_value(dbgi, addr, type, value);
4263 if (!is_constant_initializer(initializer)) {
4264 bool old_initializer_use_bitfield_basetype
4265 = initializer_use_bitfield_basetype;
4266 initializer_use_bitfield_basetype = true;
4267 ir_initializer_t *irinitializer
4268 = create_ir_initializer(initializer, type);
4269 initializer_use_bitfield_basetype
4270 = old_initializer_use_bitfield_basetype;
4272 create_dynamic_initializer(irinitializer, dbgi, entity);
4276 /* create the ir_initializer */
4277 ir_graph *const old_current_ir_graph = current_ir_graph;
4278 current_ir_graph = get_const_code_irg();
4280 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4282 assert(current_ir_graph == get_const_code_irg());
4283 current_ir_graph = old_current_ir_graph;
4285 /* create a "template" entity which is copied to the entity on the stack */
4286 ident *const id = id_unique("initializer.%u");
4287 ir_type *const irtype = get_ir_type(type);
4288 ir_type *const global_type = get_glob_type();
4289 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4290 set_entity_ld_ident(init_entity, id);
4292 set_entity_variability(init_entity, variability_initialized);
4293 set_entity_visibility(init_entity, visibility_local);
4294 set_entity_allocation(init_entity, allocation_static);
4296 set_entity_initializer(init_entity, irinitializer);
4298 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4299 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4301 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
4302 set_store(copyb_mem);
4305 static void create_initializer_local_variable_entity(entity_t *entity)
4307 assert(entity->kind == ENTITY_VARIABLE);
4308 initializer_t *initializer = entity->variable.initializer;
4309 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4310 ir_entity *irentity = entity->variable.v.entity;
4311 type_t *type = entity->declaration.type;
4313 create_local_initializer(initializer, dbgi, irentity, type);
4316 static void create_variable_initializer(entity_t *entity)
4318 assert(entity->kind == ENTITY_VARIABLE);
4319 initializer_t *initializer = entity->variable.initializer;
4320 if (initializer == NULL)
4323 declaration_kind_t declaration_kind
4324 = (declaration_kind_t) entity->declaration.kind;
4325 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4326 create_initializer_local_variable_entity(entity);
4330 type_t *type = entity->declaration.type;
4331 type_qualifiers_t tq = get_type_qualifier(type, true);
4333 if (initializer->kind == INITIALIZER_VALUE) {
4334 initializer_value_t *initializer_value = &initializer->value;
4335 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4337 ir_node *value = expression_to_firm(initializer_value->value);
4339 type_t *type = initializer_value->value->base.type;
4340 ir_mode *mode = get_ir_mode_storage(type);
4341 value = create_conv(dbgi, value, mode);
4342 value = do_strict_conv(dbgi, value);
4344 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4345 set_value(entity->variable.v.value_number, value);
4347 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4349 ir_entity *irentity = entity->variable.v.entity;
4351 if (tq & TYPE_QUALIFIER_CONST) {
4352 set_entity_variability(irentity, variability_constant);
4354 set_entity_variability(irentity, variability_initialized);
4356 set_atomic_ent_value(irentity, value);
4359 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4360 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4362 ir_entity *irentity = entity->variable.v.entity;
4363 ir_initializer_t *irinitializer
4364 = create_ir_initializer(initializer, type);
4366 if (tq & TYPE_QUALIFIER_CONST) {
4367 set_entity_variability(irentity, variability_constant);
4369 set_entity_variability(irentity, variability_initialized);
4371 set_entity_initializer(irentity, irinitializer);
4375 static void create_variable_length_array(entity_t *entity)
4377 assert(entity->kind == ENTITY_VARIABLE);
4378 assert(entity->variable.initializer == NULL);
4380 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4381 entity->variable.v.vla_base = NULL;
4383 /* TODO: record VLA somewhere so we create the free node when we leave
4387 static void allocate_variable_length_array(entity_t *entity)
4389 assert(entity->kind == ENTITY_VARIABLE);
4390 assert(entity->variable.initializer == NULL);
4391 assert(get_cur_block() != NULL);
4393 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4394 type_t *type = entity->declaration.type;
4395 ir_type *el_type = get_ir_type(type->array.element_type);
4397 /* make sure size_node is calculated */
4398 get_type_size_node(type);
4399 ir_node *elems = type->array.size_node;
4400 ir_node *mem = get_store();
4401 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4403 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4404 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4407 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4408 entity->variable.v.vla_base = addr;
4412 * Creates a Firm local variable from a declaration.
4414 static void create_local_variable(entity_t *entity)
4416 assert(entity->kind == ENTITY_VARIABLE);
4417 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4419 bool needs_entity = entity->variable.address_taken;
4420 type_t *type = skip_typeref(entity->declaration.type);
4422 /* is it a variable length array? */
4423 if (is_type_array(type) && !type->array.size_constant) {
4424 create_variable_length_array(entity);
4426 } else if (is_type_array(type) || is_type_compound(type)) {
4427 needs_entity = true;
4428 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4429 needs_entity = true;
4433 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4434 create_variable_entity(entity,
4435 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4438 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4439 entity->variable.v.value_number = next_value_number_function;
4440 set_irg_loc_description(current_ir_graph, next_value_number_function,
4442 ++next_value_number_function;
4446 static void create_local_static_variable(entity_t *entity)
4448 assert(entity->kind == ENTITY_VARIABLE);
4449 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4451 type_t *type = skip_typeref(entity->declaration.type);
4452 ir_type *const var_type = entity->variable.thread_local ?
4453 get_tls_type() : get_glob_type();
4454 ir_type *const irtype = get_ir_type(type);
4455 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4457 size_t l = strlen(entity->base.symbol->string);
4458 char buf[l + sizeof(".%u")];
4459 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4460 ident *const id = id_unique(buf);
4462 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4464 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4465 set_entity_volatility(irentity, volatility_is_volatile);
4468 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4469 entity->variable.v.entity = irentity;
4471 set_entity_ld_ident(irentity, id);
4472 set_entity_variability(irentity, variability_uninitialized);
4473 set_entity_visibility(irentity, visibility_local);
4474 set_entity_allocation(irentity, entity->variable.thread_local ?
4475 allocation_automatic : allocation_static);
4477 ir_graph *const old_current_ir_graph = current_ir_graph;
4478 current_ir_graph = get_const_code_irg();
4480 create_variable_initializer(entity);
4482 assert(current_ir_graph == get_const_code_irg());
4483 current_ir_graph = old_current_ir_graph;
4488 static void return_statement_to_firm(return_statement_t *statement)
4490 if (get_cur_block() == NULL)
4493 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4494 type_t *type = current_function_entity->declaration.type;
4495 ir_type *func_irtype = get_ir_type(type);
4500 if (get_method_n_ress(func_irtype) > 0) {
4501 ir_type *res_type = get_method_res_type(func_irtype, 0);
4503 if (statement->value != NULL) {
4504 ir_node *node = expression_to_firm(statement->value);
4505 if (!is_compound_type(res_type)) {
4506 type_t *type = statement->value->base.type;
4507 ir_mode *mode = get_ir_mode_storage(type);
4508 node = create_conv(dbgi, node, mode);
4509 node = do_strict_conv(dbgi, node);
4514 if (is_compound_type(res_type)) {
4517 mode = get_type_mode(res_type);
4519 in[0] = new_Unknown(mode);
4523 /* build return_value for its side effects */
4524 if (statement->value != NULL) {
4525 expression_to_firm(statement->value);
4530 ir_node *store = get_store();
4531 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4533 ir_node *end_block = get_irg_end_block(current_ir_graph);
4534 add_immBlock_pred(end_block, ret);
4536 set_cur_block(NULL);
4539 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4541 if (get_cur_block() == NULL)
4544 return expression_to_firm(statement->expression);
4547 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4549 entity_t *entity = compound->scope.entities;
4550 for ( ; entity != NULL; entity = entity->base.next) {
4551 if (!is_declaration(entity))
4554 create_local_declaration(entity);
4557 ir_node *result = NULL;
4558 statement_t *statement = compound->statements;
4559 for ( ; statement != NULL; statement = statement->base.next) {
4560 if (statement->base.next == NULL
4561 && statement->kind == STATEMENT_EXPRESSION) {
4562 result = expression_statement_to_firm(
4563 &statement->expression);
4566 statement_to_firm(statement);
4572 static void create_global_variable(entity_t *entity)
4574 assert(entity->kind == ENTITY_VARIABLE);
4577 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4578 case STORAGE_CLASS_STATIC: vis = visibility_local; break;
4579 case STORAGE_CLASS_EXTERN: vis = visibility_external_allocated; break;
4580 case STORAGE_CLASS_NONE: vis = visibility_external_visible; break;
4582 default: panic("Invalid storage class for global variable");
4585 ir_type *var_type = entity->variable.thread_local ?
4586 get_tls_type() : get_glob_type();
4587 create_variable_entity(entity,
4588 DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4589 set_entity_visibility(entity->variable.v.entity, vis);
4592 static void create_local_declaration(entity_t *entity)
4594 assert(is_declaration(entity));
4596 /* construct type */
4597 (void) get_ir_type(entity->declaration.type);
4598 if (entity->base.symbol == NULL) {
4602 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4603 case STORAGE_CLASS_STATIC:
4604 if (entity->kind == ENTITY_FUNCTION) {
4605 (void)get_function_entity(entity, NULL);
4607 create_local_static_variable(entity);
4610 case STORAGE_CLASS_EXTERN:
4611 if (entity->kind == ENTITY_FUNCTION) {
4612 assert(entity->function.statement == NULL);
4613 (void)get_function_entity(entity, NULL);
4615 create_global_variable(entity);
4616 create_variable_initializer(entity);
4619 case STORAGE_CLASS_NONE:
4620 case STORAGE_CLASS_AUTO:
4621 case STORAGE_CLASS_REGISTER:
4622 if (entity->kind == ENTITY_FUNCTION) {
4623 if (entity->function.statement != NULL) {
4624 ir_type *owner = get_irg_frame_type(current_ir_graph);
4625 (void)get_function_entity(entity, owner);
4626 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4627 enqueue_inner_function(entity);
4629 (void)get_function_entity(entity, NULL);
4632 create_local_variable(entity);
4635 case STORAGE_CLASS_TYPEDEF:
4638 panic("invalid storage class found");
4641 static void initialize_local_declaration(entity_t *entity)
4643 if (entity->base.symbol == NULL)
4646 // no need to emit code in dead blocks
4647 if (entity->declaration.storage_class != STORAGE_CLASS_STATIC
4648 && get_cur_block() == NULL)
4651 switch ((declaration_kind_t) entity->declaration.kind) {
4652 case DECLARATION_KIND_LOCAL_VARIABLE:
4653 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4654 create_variable_initializer(entity);
4657 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4658 allocate_variable_length_array(entity);
4661 case DECLARATION_KIND_COMPOUND_MEMBER:
4662 case DECLARATION_KIND_GLOBAL_VARIABLE:
4663 case DECLARATION_KIND_FUNCTION:
4664 case DECLARATION_KIND_INNER_FUNCTION:
4667 case DECLARATION_KIND_PARAMETER:
4668 case DECLARATION_KIND_PARAMETER_ENTITY:
4669 panic("can't initialize parameters");
4671 case DECLARATION_KIND_UNKNOWN:
4672 panic("can't initialize unknown declaration");
4674 panic("invalid declaration kind");
4677 static void declaration_statement_to_firm(declaration_statement_t *statement)
4679 entity_t *entity = statement->declarations_begin;
4683 entity_t *const last = statement->declarations_end;
4684 for ( ;; entity = entity->base.next) {
4685 if (is_declaration(entity)) {
4686 initialize_local_declaration(entity);
4687 } else if (entity->kind == ENTITY_TYPEDEF) {
4688 /* ยง6.7.7:3 Any array size expressions associated with variable length
4689 * array declarators are evaluated each time the declaration of the
4690 * typedef name is reached in the order of execution. */
4691 type_t *const type = skip_typeref(entity->typedefe.type);
4692 if (is_type_array(type) && type->array.is_vla)
4693 get_vla_size(&type->array);
4700 static void if_statement_to_firm(if_statement_t *statement)
4702 ir_node *cur_block = get_cur_block();
4704 ir_node *fallthrough_block = NULL;
4706 /* the true (blocks) */
4707 ir_node *true_block = NULL;
4708 if (statement->true_statement != NULL) {
4709 true_block = new_immBlock();
4710 set_cur_block(true_block);
4711 statement_to_firm(statement->true_statement);
4712 if (get_cur_block() != NULL) {
4713 ir_node *jmp = new_Jmp();
4714 if (fallthrough_block == NULL)
4715 fallthrough_block = new_immBlock();
4716 add_immBlock_pred(fallthrough_block, jmp);
4720 /* the false (blocks) */
4721 ir_node *false_block = NULL;
4722 if (statement->false_statement != NULL) {
4723 false_block = new_immBlock();
4724 set_cur_block(false_block);
4726 statement_to_firm(statement->false_statement);
4727 if (get_cur_block() != NULL) {
4728 ir_node *jmp = new_Jmp();
4729 if (fallthrough_block == NULL)
4730 fallthrough_block = new_immBlock();
4731 add_immBlock_pred(fallthrough_block, jmp);
4735 /* create the condition */
4736 if (cur_block != NULL) {
4737 if (true_block == NULL || false_block == NULL) {
4738 if (fallthrough_block == NULL)
4739 fallthrough_block = new_immBlock();
4740 if (true_block == NULL)
4741 true_block = fallthrough_block;
4742 if (false_block == NULL)
4743 false_block = fallthrough_block;
4746 set_cur_block(cur_block);
4747 create_condition_evaluation(statement->condition, true_block,
4751 mature_immBlock(true_block);
4752 if (false_block != fallthrough_block && false_block != NULL) {
4753 mature_immBlock(false_block);
4755 if (fallthrough_block != NULL) {
4756 mature_immBlock(fallthrough_block);
4759 set_cur_block(fallthrough_block);
4762 static void while_statement_to_firm(while_statement_t *statement)
4764 ir_node *jmp = NULL;
4765 if (get_cur_block() != NULL) {
4769 /* create the header block */
4770 ir_node *header_block = new_immBlock();
4772 add_immBlock_pred(header_block, jmp);
4776 ir_node *old_continue_label = continue_label;
4777 ir_node *old_break_label = break_label;
4778 continue_label = header_block;
4781 ir_node *body_block = new_immBlock();
4782 set_cur_block(body_block);
4783 statement_to_firm(statement->body);
4784 ir_node *false_block = break_label;
4786 assert(continue_label == header_block);
4787 continue_label = old_continue_label;
4788 break_label = old_break_label;
4790 if (get_cur_block() != NULL) {
4792 add_immBlock_pred(header_block, jmp);
4795 /* shortcut for while(true) */
4796 if (is_constant_expression(statement->condition)
4797 && fold_constant_to_bool(statement->condition) != 0) {
4798 set_cur_block(header_block);
4799 ir_node *header_jmp = new_Jmp();
4800 add_immBlock_pred(body_block, header_jmp);
4802 keep_alive(body_block);
4803 keep_all_memory(body_block);
4804 set_cur_block(body_block);
4806 if (false_block == NULL) {
4807 false_block = new_immBlock();
4810 /* create the condition */
4811 set_cur_block(header_block);
4813 create_condition_evaluation(statement->condition, body_block,
4817 mature_immBlock(body_block);
4818 mature_immBlock(header_block);
4819 if (false_block != NULL) {
4820 mature_immBlock(false_block);
4823 set_cur_block(false_block);
4826 static void do_while_statement_to_firm(do_while_statement_t *statement)
4828 ir_node *jmp = NULL;
4829 if (get_cur_block() != NULL) {
4833 /* create the header block */
4834 ir_node *header_block = new_immBlock();
4837 ir_node *body_block = new_immBlock();
4839 add_immBlock_pred(body_block, jmp);
4842 ir_node *old_continue_label = continue_label;
4843 ir_node *old_break_label = break_label;
4844 continue_label = header_block;
4847 set_cur_block(body_block);
4848 statement_to_firm(statement->body);
4849 ir_node *false_block = break_label;
4851 assert(continue_label == header_block);
4852 continue_label = old_continue_label;
4853 break_label = old_break_label;
4855 if (get_cur_block() != NULL) {
4856 ir_node *body_jmp = new_Jmp();
4857 add_immBlock_pred(header_block, body_jmp);
4858 mature_immBlock(header_block);
4861 if (false_block == NULL) {
4862 false_block = new_immBlock();
4865 /* create the condition */
4866 set_cur_block(header_block);
4868 create_condition_evaluation(statement->condition, body_block, false_block);
4869 mature_immBlock(body_block);
4870 mature_immBlock(header_block);
4871 mature_immBlock(false_block);
4873 set_cur_block(false_block);
4876 static void for_statement_to_firm(for_statement_t *statement)
4878 ir_node *jmp = NULL;
4880 /* create declarations */
4881 entity_t *entity = statement->scope.entities;
4882 for ( ; entity != NULL; entity = entity->base.next) {
4883 if (!is_declaration(entity))
4886 create_local_declaration(entity);
4889 if (get_cur_block() != NULL) {
4890 entity = statement->scope.entities;
4891 for ( ; entity != NULL; entity = entity->base.next) {
4892 if (!is_declaration(entity))
4895 initialize_local_declaration(entity);
4898 if (statement->initialisation != NULL) {
4899 expression_to_firm(statement->initialisation);
4906 /* create the step block */
4907 ir_node *const step_block = new_immBlock();
4908 set_cur_block(step_block);
4909 if (statement->step != NULL) {
4910 expression_to_firm(statement->step);
4912 ir_node *const step_jmp = new_Jmp();
4914 /* create the header block */
4915 ir_node *const header_block = new_immBlock();
4916 set_cur_block(header_block);
4918 add_immBlock_pred(header_block, jmp);
4920 add_immBlock_pred(header_block, step_jmp);
4922 /* the false block */
4923 ir_node *const false_block = new_immBlock();
4926 ir_node *body_block;
4927 if (statement->body != NULL) {
4928 ir_node *const old_continue_label = continue_label;
4929 ir_node *const old_break_label = break_label;
4930 continue_label = step_block;
4931 break_label = false_block;
4933 body_block = new_immBlock();
4934 set_cur_block(body_block);
4935 statement_to_firm(statement->body);
4937 assert(continue_label == step_block);
4938 assert(break_label == false_block);
4939 continue_label = old_continue_label;
4940 break_label = old_break_label;
4942 if (get_cur_block() != NULL) {
4944 add_immBlock_pred(step_block, jmp);
4947 body_block = step_block;
4950 /* create the condition */
4951 set_cur_block(header_block);
4952 if (statement->condition != NULL) {
4953 create_condition_evaluation(statement->condition, body_block,
4956 keep_alive(header_block);
4957 keep_all_memory(header_block);
4959 add_immBlock_pred(body_block, jmp);
4962 mature_immBlock(body_block);
4963 mature_immBlock(false_block);
4964 mature_immBlock(step_block);
4965 mature_immBlock(header_block);
4966 mature_immBlock(false_block);
4968 set_cur_block(false_block);
4971 static void create_jump_statement(const statement_t *statement,
4972 ir_node *target_block)
4974 if (get_cur_block() == NULL)
4977 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4978 ir_node *jump = new_d_Jmp(dbgi);
4979 add_immBlock_pred(target_block, jump);
4981 set_cur_block(NULL);
4984 static ir_node *get_break_label(void)
4986 if (break_label == NULL) {
4987 break_label = new_immBlock();
4992 static void switch_statement_to_firm(switch_statement_t *statement)
4994 ir_node *first_block = get_cur_block();
4995 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4996 ir_node *cond = NULL;
4997 if (first_block != NULL) {
4998 ir_node *expression = expression_to_firm(statement->expression);
4999 cond = new_d_Cond(dbgi, expression);
5002 set_cur_block(NULL);
5004 ir_node *const old_switch_cond = current_switch_cond;
5005 ir_node *const old_break_label = break_label;
5006 const bool old_saw_default_label = saw_default_label;
5007 saw_default_label = false;
5008 current_switch_cond = cond;
5010 switch_statement_t *const old_switch = current_switch;
5011 current_switch = statement;
5013 /* determine a free number for the default label */
5014 unsigned long num_cases = 0;
5016 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5017 if (l->expression == NULL) {
5021 if (l->last_case >= l->first_case)
5022 num_cases += l->last_case - l->first_case + 1;
5023 if (l->last_case > def_nr)
5024 def_nr = l->last_case;
5027 if (def_nr == INT_MAX) {
5028 /* Bad: an overflow will occur, we cannot be sure that the
5029 * maximum + 1 is a free number. Scan the values a second
5030 * time to find a free number.
5032 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
5034 memset(bits, 0, (num_cases + 7) >> 3);
5035 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
5036 if (l->expression == NULL) {
5040 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
5041 if (start < num_cases && l->last_case >= 0) {
5042 unsigned long end = (unsigned long)l->last_case < num_cases ?
5043 (unsigned long)l->last_case : num_cases - 1;
5044 for (unsigned long cns = start; cns <= end; ++cns) {
5045 bits[cns >> 3] |= (1 << (cns & 7));
5049 /* We look at the first num_cases constants:
5050 * Either they are dense, so we took the last (num_cases)
5051 * one, or they are not dense, so we will find one free
5055 for (i = 0; i < num_cases; ++i)
5056 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
5064 statement->default_proj_nr = def_nr;
5066 if (statement->body != NULL) {
5067 statement_to_firm(statement->body);
5070 if (get_cur_block() != NULL) {
5071 ir_node *jmp = new_Jmp();
5072 add_immBlock_pred(get_break_label(), jmp);
5075 if (!saw_default_label && first_block != NULL) {
5076 set_cur_block(first_block);
5077 ir_node *const proj = new_d_defaultProj(dbgi, cond,
5078 statement->default_proj_nr);
5079 add_immBlock_pred(get_break_label(), proj);
5082 if (break_label != NULL) {
5083 mature_immBlock(break_label);
5085 set_cur_block(break_label);
5087 assert(current_switch_cond == cond);
5088 current_switch = old_switch;
5089 current_switch_cond = old_switch_cond;
5090 break_label = old_break_label;
5091 saw_default_label = old_saw_default_label;
5094 static void case_label_to_firm(const case_label_statement_t *statement)
5096 if (statement->is_empty_range)
5099 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5101 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
5104 ir_node *block = new_immBlock();
5106 if (current_switch_cond != NULL) {
5107 set_cur_block(get_nodes_block(current_switch_cond));
5108 if (statement->expression != NULL) {
5109 long pn = statement->first_case;
5110 long end_pn = statement->last_case;
5111 assert(pn <= end_pn);
5112 /* create jumps for all cases in the given range */
5114 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
5115 add_immBlock_pred(block, proj);
5116 } while (pn++ < end_pn);
5118 saw_default_label = true;
5119 proj = new_d_defaultProj(dbgi, current_switch_cond,
5120 current_switch->default_proj_nr);
5122 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;