2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include <libfirm/firm.h>
28 #include <libfirm/adt/obst.h>
29 #include <libfirm/be.h>
33 #include "adt/error.h"
34 #include "adt/array.h"
42 #include "diagnostic.h"
43 #include "lang_features.h"
45 #include "type_hash.h"
47 #include "walk_statements.h"
49 #include "entitymap_t.h"
50 #include "driver/firm_opt.h"
51 #include "driver/firm_cmdline.h"
53 static ir_type *ir_type_const_char;
54 static ir_type *ir_type_wchar_t;
55 static ir_type *ir_type_void;
56 static ir_type *ir_type_int;
58 /* architecture specific floating point arithmetic mode (if any) */
59 static ir_mode *mode_float_arithmetic;
61 static int next_value_number_function;
62 static ir_node *continue_label;
63 static ir_node *break_label;
64 static ir_node *current_switch_cond;
65 static bool saw_default_label;
66 static label_t **all_labels;
67 static entity_t **inner_functions;
68 static ir_node *ijmp_list;
69 static bool constant_folding;
71 extern bool have_const_functions;
73 static const entity_t *current_function_entity;
74 static ir_node *current_function_name;
75 static ir_node *current_funcsig;
76 static switch_statement_t *current_switch;
77 static ir_graph *current_function;
78 static translation_unit_t *current_translation_unit;
80 static entitymap_t entitymap;
82 static struct obstack asm_obst;
84 typedef enum declaration_kind_t {
85 DECLARATION_KIND_UNKNOWN,
86 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
87 DECLARATION_KIND_GLOBAL_VARIABLE,
88 DECLARATION_KIND_LOCAL_VARIABLE,
89 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
90 DECLARATION_KIND_PARAMETER,
91 DECLARATION_KIND_PARAMETER_ENTITY,
92 DECLARATION_KIND_FUNCTION,
93 DECLARATION_KIND_COMPOUND_MEMBER,
94 DECLARATION_KIND_INNER_FUNCTION
97 static ir_mode *get_ir_mode_storage(type_t *type);
99 * get arithmetic mode for a type. This is different from get_ir_mode_storage,
100 * int that it returns bigger modes for floating point on some platforms
101 * (x87 internally does arithemtic with 80bits)
103 static ir_mode *get_ir_mode_arithmetic(type_t *type);
105 static ir_type *get_ir_type_incomplete(type_t *type);
107 static void enqueue_inner_function(entity_t *entity)
109 ARR_APP1(entity_t*, inner_functions, entity);
112 static entity_t *next_inner_function(void)
114 int len = ARR_LEN(inner_functions);
118 entity_t *entity = inner_functions[len-1];
119 ARR_SHRINKLEN(inner_functions, len-1);
124 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
126 const entity_t *entity = get_irg_loc_description(irg, pos);
128 if (entity != NULL) {
129 warningf(&entity->base.source_position,
130 "%s '%#T' might be used uninitialized",
131 get_entity_kind_name(entity->kind),
132 entity->declaration.type, entity->base.symbol);
134 return new_r_Unknown(irg, mode);
137 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
139 const source_position_t *pos = (const source_position_t*) dbg;
142 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
146 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
148 const source_position_t *pos = (const source_position_t*) dbg;
153 return pos->input_name;
156 static dbg_info *get_dbg_info(const source_position_t *pos)
158 return (dbg_info*) pos;
161 static ir_mode *atomic_modes[ATOMIC_TYPE_LAST+1];
163 static ir_mode *mode_int, *mode_uint;
165 static ir_node *_expression_to_firm(const expression_t *expression);
166 static ir_node *expression_to_firm(const expression_t *expression);
167 static void create_local_declaration(entity_t *entity);
169 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
171 unsigned flags = get_atomic_type_flags(kind);
172 unsigned size = get_atomic_type_size(kind);
173 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
174 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
177 unsigned bit_size = size * 8;
178 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
179 unsigned modulo_shift;
180 ir_mode_arithmetic arithmetic;
182 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
183 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
184 snprintf(name, sizeof(name), "%s%u", is_signed ? "I" : "U",
186 sort = irms_int_number;
187 arithmetic = irma_twos_complement;
188 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
190 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
191 snprintf(name, sizeof(name), "F%u", bit_size);
192 sort = irms_float_number;
193 arithmetic = irma_ieee754;
196 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
204 * Initialises the atomic modes depending on the machine size.
206 static void init_atomic_modes(void)
208 for (int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
209 atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
211 mode_int = atomic_modes[ATOMIC_TYPE_INT];
212 mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
214 /* there's no real void type in firm */
215 atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
217 /* initialize pointer modes */
219 ir_mode_sort sort = irms_reference;
220 unsigned bit_size = machine_size;
222 ir_mode_arithmetic arithmetic = irma_twos_complement;
223 unsigned modulo_shift
224 = bit_size < machine_size ? machine_size : bit_size;
226 snprintf(name, sizeof(name), "p%u", machine_size);
227 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
230 set_reference_mode_signed_eq(ptr_mode, atomic_modes[get_intptr_kind()]);
231 set_reference_mode_unsigned_eq(ptr_mode, atomic_modes[get_uintptr_kind()]);
233 /* Hmm, pointers should be machine size */
234 set_modeP_data(ptr_mode);
235 set_modeP_code(ptr_mode);
238 ir_mode *get_atomic_mode(atomic_type_kind_t kind)
240 assert(kind <= ATOMIC_TYPE_LAST);
241 return atomic_modes[kind];
244 static ir_node *get_vla_size(array_type_t *const type)
246 ir_node *size_node = type->size_node;
247 if (size_node == NULL) {
248 size_node = expression_to_firm(type->size_expression);
249 type->size_node = size_node;
254 static ir_node *get_type_size(type_t *type)
256 type = skip_typeref(type);
258 if (is_type_array(type) && type->array.is_vla) {
259 ir_node *size_node = get_vla_size(&type->array);
260 ir_node *elem_size = get_type_size(type->array.element_type);
261 ir_mode *mode = get_irn_mode(size_node);
262 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
266 ir_mode *mode = get_ir_mode_storage(type_size_t);
268 sym.type_p = get_ir_type(type);
269 return new_SymConst(mode, sym, symconst_type_size);
272 static unsigned count_parameters(const function_type_t *function_type)
276 function_parameter_t *parameter = function_type->parameters;
277 for ( ; parameter != NULL; parameter = parameter->next) {
284 static type_t *get_aligned_type(type_t *type, int alignment)
289 type = skip_typeref(type);
290 if (alignment > type->base.alignment) {
291 type_t *copy = duplicate_type(type);
292 copy->base.alignment = alignment;
293 type = identify_new_type(copy);
299 * Creates a Firm type for an atomic type
301 static ir_type *create_atomic_type(atomic_type_kind_t akind, int alignment)
303 ir_mode *mode = atomic_modes[akind];
304 ident *id = get_mode_ident(mode);
305 ir_type *irtype = new_type_primitive(id, mode);
307 set_type_alignment_bytes(irtype, alignment);
313 * Creates a Firm type for a complex type
315 static ir_type *create_complex_type(const complex_type_t *type)
317 atomic_type_kind_t kind = type->akind;
318 ir_mode *mode = atomic_modes[kind];
319 ident *id = get_mode_ident(mode);
323 /* FIXME: finish the array */
328 * Creates a Firm type for an imaginary type
330 static ir_type *create_imaginary_type(const imaginary_type_t *type)
332 atomic_type_kind_t kind = type->akind;
333 ir_mode *mode = atomic_modes[kind];
334 ident *id = get_mode_ident(mode);
335 ir_type *irtype = new_type_primitive(id, mode);
337 set_type_alignment_bytes(irtype, type->base.alignment);
343 * return type of a parameter (and take transparent union gnu extension into
346 static type_t *get_parameter_type(type_t *type)
348 type = skip_typeref(type);
349 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
350 compound_t *compound = type->compound.compound;
351 type = compound->members.entities->declaration.type;
357 static ir_type *create_method_type(const function_type_t *function_type)
359 type_t *return_type = skip_typeref(function_type->return_type);
361 ident *id = id_unique("functiontype.%u");
362 int n_parameters = count_parameters(function_type);
363 int n_results = return_type == type_void ? 0 : 1;
364 ir_type *irtype = new_type_method(id, n_parameters, n_results);
366 if (return_type != type_void) {
367 ir_type *restype = get_ir_type(return_type);
368 set_method_res_type(irtype, 0, restype);
371 function_parameter_t *parameter = function_type->parameters;
373 for ( ; parameter != NULL; parameter = parameter->next) {
374 type_t *type = get_parameter_type(parameter->type);
375 ir_type *p_irtype = get_ir_type(type);
376 set_method_param_type(irtype, n, p_irtype);
380 if (function_type->variadic || function_type->unspecified_parameters) {
381 set_method_variadicity(irtype, variadicity_variadic);
384 unsigned cc = get_method_calling_convention(irtype);
385 switch (function_type->calling_convention) {
386 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
389 set_method_calling_convention(irtype, SET_CDECL(cc));
393 if (function_type->variadic || function_type->unspecified_parameters)
396 /* only non-variadic function can use stdcall, else use cdecl */
397 set_method_calling_convention(irtype, SET_STDCALL(cc));
401 if (function_type->variadic || function_type->unspecified_parameters)
403 /* only non-variadic function can use fastcall, else use cdecl */
404 set_method_calling_convention(irtype, SET_FASTCALL(cc));
408 /* Hmm, leave default, not accepted by the parser yet. */
415 static ir_type *create_pointer_type(pointer_type_t *type)
417 type_t *points_to = type->points_to;
418 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
419 ir_type *ir_type = new_type_pointer(id_unique("pointer.%u"),
420 ir_points_to, mode_P_data);
425 static ir_type *create_reference_type(reference_type_t *type)
427 type_t *refers_to = type->refers_to;
428 ir_type *ir_refers_to = get_ir_type_incomplete(refers_to);
429 ir_type *ir_type = new_type_pointer(id_unique("reference.%u"),
430 ir_refers_to, mode_P_data);
435 static ir_type *create_array_type(array_type_t *type)
437 type_t *element_type = type->element_type;
438 ir_type *ir_element_type = get_ir_type(element_type);
440 ident *id = id_unique("array.%u");
441 ir_type *ir_type = new_type_array(id, 1, ir_element_type);
443 const int align = get_type_alignment_bytes(ir_element_type);
444 set_type_alignment_bytes(ir_type, align);
446 if (type->size_constant) {
447 int n_elements = type->size;
449 set_array_bounds_int(ir_type, 0, 0, n_elements);
451 size_t elemsize = get_type_size_bytes(ir_element_type);
452 if (elemsize % align > 0) {
453 elemsize += align - (elemsize % align);
455 set_type_size_bytes(ir_type, n_elements * elemsize);
457 set_array_lower_bound_int(ir_type, 0, 0);
459 set_type_state(ir_type, layout_fixed);
465 * Return the signed integer type of size bits.
467 * @param size the size
469 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
472 static ir_mode *s_modes[64 + 1] = {NULL, };
476 if (size <= 0 || size > 64)
479 mode = s_modes[size];
483 snprintf(name, sizeof(name), "bf_I%u", size);
484 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
485 size <= 32 ? 32 : size );
486 s_modes[size] = mode;
490 snprintf(name, sizeof(name), "I%u", size);
491 ident *id = new_id_from_str(name);
492 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
493 res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
494 set_primitive_base_type(res, base_tp);
500 * Return the unsigned integer type of size bits.
502 * @param size the size
504 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
507 static ir_mode *u_modes[64 + 1] = {NULL, };
511 if (size <= 0 || size > 64)
514 mode = u_modes[size];
518 snprintf(name, sizeof(name), "bf_U%u", size);
519 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
520 size <= 32 ? 32 : size );
521 u_modes[size] = mode;
526 snprintf(name, sizeof(name), "U%u", size);
527 ident *id = new_id_from_str(name);
528 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
529 res = new_d_type_primitive(id_mangle_u(get_type_ident(base_tp), id), mode, dbgi);
530 set_primitive_base_type(res, base_tp);
535 static ir_type *create_bitfield_type(bitfield_type_t *const type)
537 type_t *base = skip_typeref(type->base_type);
538 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
539 ir_type *irbase = get_ir_type(base);
541 unsigned size = type->bit_size;
543 assert(!is_type_float(base));
544 if (is_type_signed(base)) {
545 return get_signed_int_type_for_bit_size(irbase, size);
547 return get_unsigned_int_type_for_bit_size(irbase, size);
551 #define INVALID_TYPE ((ir_type_ptr)-1)
554 COMPOUND_IS_STRUCT = false,
555 COMPOUND_IS_UNION = true
559 * Construct firm type from ast struct type.
561 * As anonymous inner structs get flattened to a single firm type, we might get
562 * irtype, outer_offset and out_align passed (they represent the position of
563 * the anonymous inner struct inside the resulting firm struct)
565 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
566 size_t *outer_offset, size_t *outer_align,
567 bool incomplete, bool is_union)
569 compound_t *compound = type->compound;
571 if (compound->irtype != NULL && (compound->irtype_complete || incomplete)) {
572 return compound->irtype;
575 size_t align_all = 1;
577 size_t bit_offset = 0;
580 if (irtype == NULL) {
581 symbol_t *symbol = compound->base.symbol;
583 if (symbol != NULL) {
584 id = new_id_from_str(symbol->string);
587 id = id_unique("__anonymous_union.%u");
589 id = id_unique("__anonymous_struct.%u");
592 dbg_info *dbgi = get_dbg_info(&compound->base.source_position);
595 irtype = new_d_type_union(id, dbgi);
597 irtype = new_d_type_struct(id, dbgi);
600 compound->irtype_complete = false;
601 compound->irtype = irtype;
603 offset = *outer_offset;
604 align_all = *outer_align;
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 size_t prev_offset = offset;
619 symbol_t *symbol = entry->base.symbol;
620 type_t *entry_type = skip_typeref(entry->declaration.type);
622 = get_aligned_type(entry_type, entry->compound_member.alignment);
623 dbg_info *dbgi = get_dbg_info(&entry->base.source_position);
626 if (symbol != NULL) {
627 ident = new_id_from_str(symbol->string);
629 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
630 create_compound_type(&entry_type->compound, irtype, &offset,
631 &align_all, false, COMPOUND_IS_STRUCT);
632 goto finished_member;
633 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
634 create_compound_type(&entry_type->compound, irtype, &offset,
635 &align_all, false, COMPOUND_IS_UNION);
636 goto finished_member;
638 assert(entry_type->kind == TYPE_BITFIELD);
640 ident = id_unique("anon.%u");
643 ir_type *base_irtype;
644 if (entry_type->kind == TYPE_BITFIELD) {
645 base_irtype = get_ir_type(entry_type->bitfield.base_type);
647 base_irtype = get_ir_type(entry_type);
650 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
651 size_t misalign = offset % entry_alignment;
653 ir_type *entry_irtype = get_ir_type(entry_type);
654 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
657 size_t bits_remainder;
658 if (entry_type->kind == TYPE_BITFIELD) {
659 size_t size_bits = entry_type->bitfield.bit_size;
660 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
662 if (size_bits > rest_size_bits) {
663 /* start a new bucket */
664 offset += entry_alignment - misalign;
670 /* put into current bucket */
671 base = offset - misalign;
672 bits_remainder = misalign * 8 + bit_offset;
675 offset += size_bits / 8;
676 bit_offset = bit_offset + (size_bits % 8);
678 size_t entry_size = get_type_size_bytes(base_irtype);
679 if (misalign > 0 || bit_offset > 0)
680 offset += entry_alignment - misalign;
684 offset += entry_size;
688 if (entry_alignment > align_all) {
689 if (entry_alignment % align_all != 0) {
690 panic("uneven alignments not supported yet");
692 align_all = entry_alignment;
695 set_entity_offset(entity, base);
696 set_entity_offset_bits_remainder(entity,
697 (unsigned char) bits_remainder);
698 //add_struct_member(irtype, entity);
699 entry->declaration.kind = DECLARATION_KIND_COMPOUND_MEMBER;
700 assert(entry->compound_member.entity == NULL);
701 entry->compound_member.entity = entity;
705 size_t entry_size = offset - prev_offset;
706 if (entry_size > size) {
718 size_t misalign = offset % align_all;
719 if (misalign > 0 || bit_offset > 0) {
720 size += align_all - misalign;
723 if (outer_offset != NULL) {
725 *outer_offset = offset;
727 *outer_offset += size;
730 if (align_all > *outer_align) {
731 if (align_all % *outer_align != 0) {
732 panic("uneven alignments not supported yet");
734 *outer_align = align_all;
737 set_type_alignment_bytes(irtype, align_all);
738 set_type_size_bytes(irtype, size);
739 set_type_state(irtype, layout_fixed);
745 static ir_type *create_enum_type(enum_type_t *const type)
747 type->base.firm_type = ir_type_int;
749 ir_mode *const mode = mode_int;
750 tarval *const one = get_mode_one(mode);
751 tarval * tv_next = get_tarval_null(mode);
753 bool constant_folding_old = constant_folding;
754 constant_folding = true;
756 enum_t *enume = type->enume;
757 entity_t *entry = enume->base.next;
758 for (; entry != NULL; entry = entry->base.next) {
759 if (entry->kind != ENTITY_ENUM_VALUE)
762 expression_t *const init = entry->enum_value.value;
764 ir_node *const cnst = expression_to_firm(init);
765 if (!is_Const(cnst)) {
766 panic("couldn't fold constant");
768 tv_next = get_Const_tarval(cnst);
770 entry->enum_value.tv = tv_next;
771 tv_next = tarval_add(tv_next, one);
774 constant_folding = constant_folding_old;
776 return create_atomic_type(type->akind, type->base.alignment);
779 static ir_type *get_ir_type_incomplete(type_t *type)
781 assert(type != NULL);
782 type = skip_typeref(type);
784 if (type->base.firm_type != NULL) {
785 assert(type->base.firm_type != INVALID_TYPE);
786 return type->base.firm_type;
789 switch (type->kind) {
790 case TYPE_COMPOUND_STRUCT:
791 return create_compound_type(&type->compound, NULL, NULL, NULL,
792 true, COMPOUND_IS_STRUCT);
793 case TYPE_COMPOUND_UNION:
794 return create_compound_type(&type->compound, NULL, NULL, NULL,
795 true, COMPOUND_IS_UNION);
797 return get_ir_type(type);
801 ir_type *get_ir_type(type_t *type)
803 assert(type != NULL);
805 type = skip_typeref(type);
807 if (type->base.firm_type != NULL) {
808 assert(type->base.firm_type != INVALID_TYPE);
809 return type->base.firm_type;
812 ir_type *firm_type = NULL;
813 switch (type->kind) {
815 /* Happens while constant folding, when there was an error */
816 return create_atomic_type(ATOMIC_TYPE_VOID, 0);
819 firm_type = create_atomic_type(type->atomic.akind,
820 type->base.alignment);
823 firm_type = create_complex_type(&type->complex);
826 firm_type = create_imaginary_type(&type->imaginary);
829 firm_type = create_method_type(&type->function);
832 firm_type = create_pointer_type(&type->pointer);
835 firm_type = create_reference_type(&type->reference);
838 firm_type = create_array_type(&type->array);
840 case TYPE_COMPOUND_STRUCT:
841 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
842 false, COMPOUND_IS_STRUCT);
844 case TYPE_COMPOUND_UNION:
845 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
846 false, COMPOUND_IS_UNION);
849 firm_type = create_enum_type(&type->enumt);
852 firm_type = get_ir_type(type->builtin.real_type);
855 firm_type = create_bitfield_type(&type->bitfield);
863 if (firm_type == NULL)
864 panic("unknown type found");
866 type->base.firm_type = firm_type;
870 static ir_mode *get_ir_mode_storage(type_t *type)
872 ir_type *irtype = get_ir_type(type);
874 /* firm doesn't report a mode for arrays somehow... */
875 if (is_Array_type(irtype)) {
879 ir_mode *mode = get_type_mode(irtype);
880 assert(mode != NULL);
884 static ir_mode *get_ir_mode_arithmetic(type_t *type)
886 ir_mode *mode = get_ir_mode_storage(type);
887 if (mode_is_float(mode) && mode_float_arithmetic != NULL) {
888 return mode_float_arithmetic;
894 /** Names of the runtime functions. */
895 static const struct {
896 int id; /**< the rts id */
897 int n_res; /**< number of return values */
898 const char *name; /**< the name of the rts function */
899 int n_params; /**< number of parameters */
900 unsigned flags; /**< language flags */
902 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
903 { rts_abort, 0, "abort", 0, _C89 },
904 { rts_alloca, 1, "alloca", 1, _ALL },
905 { rts_abs, 1, "abs", 1, _C89 },
906 { rts_labs, 1, "labs", 1, _C89 },
907 { rts_llabs, 1, "llabs", 1, _C99 },
908 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
910 { rts_fabs, 1, "fabs", 1, _C89 },
911 { rts_sqrt, 1, "sqrt", 1, _C89 },
912 { rts_cbrt, 1, "cbrt", 1, _C99 },
913 { rts_exp, 1, "exp", 1, _C89 },
914 { rts_exp2, 1, "exp2", 1, _C89 },
915 { rts_exp10, 1, "exp10", 1, _GNUC },
916 { rts_log, 1, "log", 1, _C89 },
917 { rts_log2, 1, "log2", 1, _C89 },
918 { rts_log10, 1, "log10", 1, _C89 },
919 { rts_pow, 1, "pow", 2, _C89 },
920 { rts_sin, 1, "sin", 1, _C89 },
921 { rts_cos, 1, "cos", 1, _C89 },
922 { rts_tan, 1, "tan", 1, _C89 },
923 { rts_asin, 1, "asin", 1, _C89 },
924 { rts_acos, 1, "acos", 1, _C89 },
925 { rts_atan, 1, "atan", 1, _C89 },
926 { rts_sinh, 1, "sinh", 1, _C89 },
927 { rts_cosh, 1, "cosh", 1, _C89 },
928 { rts_tanh, 1, "tanh", 1, _C89 },
930 { rts_fabsf, 1, "fabsf", 1, _C99 },
931 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
932 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
933 { rts_expf, 1, "expf", 1, _C99 },
934 { rts_exp2f, 1, "exp2f", 1, _C99 },
935 { rts_exp10f, 1, "exp10f", 1, _GNUC },
936 { rts_logf, 1, "logf", 1, _C99 },
937 { rts_log2f, 1, "log2f", 1, _C99 },
938 { rts_log10f, 1, "log10f", 1, _C99 },
939 { rts_powf, 1, "powf", 2, _C99 },
940 { rts_sinf, 1, "sinf", 1, _C99 },
941 { rts_cosf, 1, "cosf", 1, _C99 },
942 { rts_tanf, 1, "tanf", 1, _C99 },
943 { rts_asinf, 1, "asinf", 1, _C99 },
944 { rts_acosf, 1, "acosf", 1, _C99 },
945 { rts_atanf, 1, "atanf", 1, _C99 },
946 { rts_sinhf, 1, "sinhf", 1, _C99 },
947 { rts_coshf, 1, "coshf", 1, _C99 },
948 { rts_tanhf, 1, "tanhf", 1, _C99 },
950 { rts_fabsl, 1, "fabsl", 1, _C99 },
951 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
952 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
953 { rts_expl, 1, "expl", 1, _C99 },
954 { rts_exp2l, 1, "exp2l", 1, _C99 },
955 { rts_exp10l, 1, "exp10l", 1, _GNUC },
956 { rts_logl, 1, "logl", 1, _C99 },
957 { rts_log2l, 1, "log2l", 1, _C99 },
958 { rts_log10l, 1, "log10l", 1, _C99 },
959 { rts_powl, 1, "powl", 2, _C99 },
960 { rts_sinl, 1, "sinl", 1, _C99 },
961 { rts_cosl, 1, "cosl", 1, _C99 },
962 { rts_tanl, 1, "tanl", 1, _C99 },
963 { rts_asinl, 1, "asinl", 1, _C99 },
964 { rts_acosl, 1, "acosl", 1, _C99 },
965 { rts_atanl, 1, "atanl", 1, _C99 },
966 { rts_sinhl, 1, "sinhl", 1, _C99 },
967 { rts_coshl, 1, "coshl", 1, _C99 },
968 { rts_tanhl, 1, "tanhl", 1, _C99 },
970 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
971 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
972 { rts_strcmp, 1, "strcmp", 2, _C89 },
973 { rts_strncmp, 1, "strncmp", 3, _C89 }
976 static ident *rts_idents[lengthof(rts_data)];
978 static ident* (*create_ld_ident)(entity_t*) = create_name_linux_elf;
981 * Handle GNU attributes for entities
983 * @param ent the entity
984 * @param decl the routine declaration
986 static void handle_gnu_attributes_ent(ir_entity *irentity, entity_t *entity)
988 assert(is_declaration(entity));
989 decl_modifiers_t modifiers = entity->declaration.modifiers;
990 if (modifiers & DM_PURE) {
991 /* TRUE if the declaration includes the GNU
992 __attribute__((pure)) specifier. */
993 set_entity_additional_property(irentity, mtp_property_pure);
995 if (modifiers & DM_CONST) {
996 set_entity_additional_property(irentity, mtp_property_const);
997 have_const_functions = true;
999 if (modifiers & DM_USED) {
1000 /* TRUE if the declaration includes the GNU
1001 __attribute__((used)) specifier. */
1002 set_entity_stickyness(irentity, stickyness_sticky);
1006 static bool is_main(entity_t *entity)
1008 static symbol_t *sym_main = NULL;
1009 if (sym_main == NULL) {
1010 sym_main = symbol_table_insert("main");
1013 if (entity->base.symbol != sym_main)
1015 /* must be in outermost scope */
1016 if (entity->base.parent_scope != ¤t_translation_unit->scope)
1023 * Creates an entity representing a function.
1025 * @param declaration the function declaration
1027 static ir_entity *get_function_entity(entity_t *entity)
1029 assert(entity->kind == ENTITY_FUNCTION);
1030 if (entity->function.entity != NULL) {
1031 return entity->function.entity;
1034 if (is_main(entity)) {
1035 /* force main to C linkage */
1036 type_t *type = entity->declaration.type;
1037 assert(is_type_function(type));
1038 if (type->function.linkage != LINKAGE_C) {
1039 type_t *new_type = duplicate_type(type);
1040 new_type->function.linkage = LINKAGE_C;
1041 type = identify_new_type(new_type);
1042 entity->declaration.type = type;
1046 symbol_t *symbol = entity->base.symbol;
1047 ident *id = new_id_from_str(symbol->string);
1049 ir_type *global_type = get_glob_type();
1050 ir_type *ir_type_method = get_ir_type(entity->declaration.type);
1051 bool const has_body = entity->function.statement != NULL;
1053 /* already an entity defined? */
1054 ir_entity *irentity = entitymap_get(&entitymap, symbol);
1055 if (irentity != NULL) {
1056 if (get_entity_visibility(irentity) == visibility_external_allocated
1058 set_entity_visibility(irentity, visibility_external_visible);
1060 goto entity_created;
1063 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
1064 irentity = new_d_entity(global_type, id, ir_type_method, dbgi);
1065 set_entity_ld_ident(irentity, create_ld_ident(entity));
1067 handle_gnu_attributes_ent(irentity, entity);
1069 /* static inline => local
1070 * extern inline => local
1071 * inline without definition => local
1072 * inline with definition => external_visible */
1073 storage_class_tag_t const storage_class
1074 = (storage_class_tag_t) entity->declaration.storage_class;
1075 bool const is_inline = entity->function.is_inline;
1076 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1077 set_entity_visibility(irentity, visibility_external_visible);
1078 } else if (storage_class == STORAGE_CLASS_STATIC ||
1079 (is_inline && has_body)) {
1081 /* this entity was declared, but is defined nowhere */
1082 set_entity_peculiarity(irentity, peculiarity_description);
1084 set_entity_visibility(irentity, visibility_local);
1085 } else if (has_body) {
1086 set_entity_visibility(irentity, visibility_external_visible);
1088 set_entity_visibility(irentity, visibility_external_allocated);
1090 set_entity_allocation(irentity, allocation_static);
1092 /* We should check for file scope here, but as long as we compile C only
1093 this is not needed. */
1094 if (! firm_opt.freestanding) {
1095 /* check for a known runtime function */
1096 for (size_t i = 0; i < lengthof(rts_data); ++i) {
1097 if (id != rts_idents[i])
1100 /* ignore those rts functions not necessary needed for current mode */
1101 if ((c_mode & rts_data[i].flags) == 0)
1103 assert(rts_entities[rts_data[i].id] == NULL);
1104 rts_entities[rts_data[i].id] = irentity;
1108 entitymap_insert(&entitymap, symbol, irentity);
1111 entity->declaration.kind = DECLARATION_KIND_FUNCTION;
1112 entity->function.entity = irentity;
1117 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1119 ir_mode *value_mode = get_irn_mode(value);
1121 if (value_mode == dest_mode || is_Bad(value))
1124 if (dest_mode == mode_b) {
1125 ir_node *zero = new_Const(get_mode_null(value_mode));
1126 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1127 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1131 return new_d_Conv(dbgi, value, dest_mode);
1135 * Creates a Const node representing a constant.
1137 static ir_node *const_to_firm(const const_expression_t *cnst)
1139 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1140 type_t *type = skip_typeref(cnst->base.type);
1141 ir_mode *mode = get_ir_mode_storage(type);
1146 if (mode_is_float(mode)) {
1147 tv = new_tarval_from_double(cnst->v.float_value, mode);
1149 if (mode_is_signed(mode)) {
1150 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1152 len = snprintf(buf, sizeof(buf), "%llu",
1153 (unsigned long long) cnst->v.int_value);
1155 tv = new_tarval_from_str(buf, len, mode);
1158 ir_node *res = new_d_Const(dbgi, tv);
1159 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
1160 return create_conv(dbgi, res, mode_arith);
1164 * Creates a Const node representing a character constant.
1166 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1168 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1169 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1172 size_t const size = cnst->v.character.size;
1173 if (size == 1 && char_is_signed) {
1174 v = (signed char)cnst->v.character.begin[0];
1177 for (size_t i = 0; i < size; ++i) {
1178 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1182 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1183 tarval *tv = new_tarval_from_str(buf, len, mode);
1185 return new_d_Const(dbgi, tv);
1189 * Creates a Const node representing a wide character constant.
1191 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1193 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1194 ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
1196 long long int v = cnst->v.wide_character.begin[0];
1199 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1200 tarval *tv = new_tarval_from_str(buf, len, mode);
1202 return new_d_Const(dbgi, tv);
1206 * Creates a SymConst for a given entity.
1208 * @param dbgi debug info
1209 * @param mode the (reference) mode for the SymConst
1210 * @param entity the entity
1212 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1215 assert(entity != NULL);
1216 union symconst_symbol sym;
1217 sym.entity_p = entity;
1218 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1222 * Creates a SymConst node representing a string constant.
1224 * @param src_pos the source position of the string constant
1225 * @param id_prefix a prefix for the name of the generated string constant
1226 * @param value the value of the string constant
1228 static ir_node *string_to_firm(const source_position_t *const src_pos,
1229 const char *const id_prefix,
1230 const string_t *const value)
1232 ir_type *const global_type = get_glob_type();
1233 dbg_info *const dbgi = get_dbg_info(src_pos);
1234 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1235 ir_type_const_char, dbgi);
1237 ident *const id = id_unique(id_prefix);
1238 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1239 set_entity_ld_ident(entity, id);
1240 set_entity_variability(entity, variability_constant);
1241 set_entity_allocation(entity, allocation_static);
1243 ir_type *const elem_type = ir_type_const_char;
1244 ir_mode *const mode = get_type_mode(elem_type);
1246 const char* const string = value->begin;
1247 const size_t slen = value->size;
1249 set_array_lower_bound_int(type, 0, 0);
1250 set_array_upper_bound_int(type, 0, slen);
1251 set_type_size_bytes(type, slen);
1252 set_type_state(type, layout_fixed);
1254 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1255 for (size_t i = 0; i < slen; ++i) {
1256 tvs[i] = new_tarval_from_long(string[i], mode);
1259 set_array_entity_values(entity, tvs, slen);
1262 return create_symconst(dbgi, mode_P_data, entity);
1266 * Creates a SymConst node representing a string literal.
1268 * @param literal the string literal
1270 static ir_node *string_literal_to_firm(
1271 const string_literal_expression_t* literal)
1273 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1278 * Creates a SymConst node representing a wide string literal.
1280 * @param literal the wide string literal
1282 static ir_node *wide_string_literal_to_firm(
1283 const wide_string_literal_expression_t* const literal)
1285 ir_type *const global_type = get_glob_type();
1286 ir_type *const elem_type = ir_type_wchar_t;
1287 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1288 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1291 ident *const id = id_unique("Lstr.%u");
1292 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1293 set_entity_ld_ident(entity, id);
1294 set_entity_variability(entity, variability_constant);
1295 set_entity_allocation(entity, allocation_static);
1297 ir_mode *const mode = get_type_mode(elem_type);
1299 const wchar_rep_t *const string = literal->value.begin;
1300 const size_t slen = literal->value.size;
1302 set_array_lower_bound_int(type, 0, 0);
1303 set_array_upper_bound_int(type, 0, slen);
1304 set_type_size_bytes(type, slen);
1305 set_type_state(type, layout_fixed);
1307 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1308 for (size_t i = 0; i < slen; ++i) {
1309 tvs[i] = new_tarval_from_long(string[i], mode);
1312 set_array_entity_values(entity, tvs, slen);
1315 return create_symconst(dbgi, mode_P_data, entity);
1318 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1319 ir_node *const addr)
1321 ir_type *irtype = get_ir_type(type);
1322 if (is_compound_type(irtype)
1323 || is_Method_type(irtype)
1324 || is_Array_type(irtype)) {
1328 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1329 ? cons_volatile : cons_none;
1330 ir_mode *const mode = get_type_mode(irtype);
1331 ir_node *const memory = get_store();
1332 ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
1333 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1334 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1336 set_store(load_mem);
1338 ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
1339 return create_conv(dbgi, load_res, mode_arithmetic);
1343 * Creates a strict Conv if necessary.
1345 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1347 ir_mode *mode = get_irn_mode(node);
1349 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1351 if (!mode_is_float(mode))
1354 /* check if there is already a Conv */
1355 if (is_Conv(node)) {
1356 /* convert it into a strict Conv */
1357 set_Conv_strict(node, 1);
1361 /* otherwise create a new one */
1362 return new_d_strictConv(dbgi, node, mode);
1365 static ir_node *get_global_var_address(dbg_info *const dbgi,
1366 const entity_t *const entity)
1368 assert(entity->kind == ENTITY_VARIABLE);
1369 assert(entity->declaration.kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1371 ir_entity *const irentity = entity->variable.v.entity;
1372 if (entity->variable.thread_local) {
1373 ir_node *const no_mem = new_NoMem();
1374 ir_node *const tls = get_irg_tls(current_ir_graph);
1375 return new_d_simpleSel(dbgi, no_mem, tls, irentity);
1377 return create_symconst(dbgi, mode_P_data, irentity);
1382 * Returns the correct base address depending on whether it is a parameter or a
1383 * normal local variable.
1385 static ir_node *get_local_frame(ir_entity *const ent)
1387 ir_graph *const irg = current_ir_graph;
1388 const ir_type *const owner = get_entity_owner(ent);
1389 if (owner == get_irg_frame_type(irg)) {
1390 return get_irg_frame(irg);
1392 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1393 return get_irg_value_param_base(irg);
1398 * Keep all memory edges of the given block.
1400 static void keep_all_memory(ir_node *block)
1402 ir_node *old = get_cur_block();
1404 set_cur_block(block);
1405 keep_alive(get_store());
1406 /* TODO: keep all memory edges from restricted pointers */
1410 static ir_node *reference_expression_enum_value_to_firm(
1411 const reference_expression_t *ref)
1413 entity_t *entity = ref->entity;
1414 type_t *type = skip_typeref(entity->enum_value.enum_type);
1415 /* make sure the type is constructed */
1416 (void) get_ir_type(type);
1418 return new_Const(entity->enum_value.tv);
1421 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1423 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1424 entity_t *entity = ref->entity;
1425 assert(is_declaration(entity));
1426 type_t *type = skip_typeref(entity->declaration.type);
1428 /* make sure the type is constructed */
1429 (void) get_ir_type(type);
1431 switch ((declaration_kind_t) entity->declaration.kind) {
1432 case DECLARATION_KIND_UNKNOWN:
1435 case DECLARATION_KIND_LOCAL_VARIABLE: {
1436 ir_mode *const mode = get_ir_mode_storage(type);
1437 ir_node *const value = get_value(entity->variable.v.value_number, mode);
1438 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1440 case DECLARATION_KIND_PARAMETER: {
1441 ir_mode *const mode = get_ir_mode_storage(type);
1442 ir_node *const value = get_value(entity->parameter.v.value_number,mode);
1443 return create_conv(NULL, value, get_ir_mode_arithmetic(type));
1445 case DECLARATION_KIND_FUNCTION: {
1446 ir_mode *const mode = get_ir_mode_storage(type);
1447 return create_symconst(dbgi, mode, entity->function.entity);
1449 case DECLARATION_KIND_INNER_FUNCTION: {
1450 ir_mode *const mode = get_ir_mode_storage(type);
1451 if (!entity->function.goto_to_outer && !entity->function.need_closure) {
1452 /* inner function not using the closure */
1453 return create_symconst(dbgi, mode, entity->function.entity);
1455 /* TODO: need trampoline here */
1456 panic("Trampoline code not implemented");
1457 return create_symconst(dbgi, mode, entity->function.entity);
1460 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1461 ir_node *const addr = get_global_var_address(dbgi, entity);
1462 return deref_address(dbgi, entity->declaration.type, addr);
1465 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1466 ir_entity *irentity = entity->variable.v.entity;
1467 ir_node *frame = get_local_frame(irentity);
1468 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1469 return deref_address(dbgi, entity->declaration.type, sel);
1471 case DECLARATION_KIND_PARAMETER_ENTITY: {
1472 ir_entity *irentity = entity->parameter.v.entity;
1473 ir_node *frame = get_local_frame(irentity);
1474 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1475 return deref_address(dbgi, entity->declaration.type, sel);
1478 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1479 return entity->variable.v.vla_base;
1481 case DECLARATION_KIND_COMPOUND_MEMBER:
1482 panic("not implemented reference type");
1485 panic("reference to declaration with unknown type found");
1488 static ir_node *reference_addr(const reference_expression_t *ref)
1490 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1491 entity_t *entity = ref->entity;
1492 assert(is_declaration(entity));
1494 switch((declaration_kind_t) entity->declaration.kind) {
1495 case DECLARATION_KIND_UNKNOWN:
1497 case DECLARATION_KIND_PARAMETER:
1498 case DECLARATION_KIND_LOCAL_VARIABLE:
1499 /* you can store to a local variable (so we don't panic but return NULL
1500 * as an indicator for no real address) */
1502 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1503 ir_node *const addr = get_global_var_address(dbgi, entity);
1506 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1507 ir_entity *irentity = entity->variable.v.entity;
1508 ir_node *frame = get_local_frame(irentity);
1509 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1513 case DECLARATION_KIND_PARAMETER_ENTITY: {
1514 ir_entity *irentity = entity->parameter.v.entity;
1515 ir_node *frame = get_local_frame(irentity);
1516 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
1521 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1522 return entity->variable.v.vla_base;
1524 case DECLARATION_KIND_FUNCTION: {
1525 type_t *const type = skip_typeref(entity->declaration.type);
1526 ir_mode *const mode = get_ir_mode_storage(type);
1527 return create_symconst(dbgi, mode, entity->function.entity);
1530 case DECLARATION_KIND_INNER_FUNCTION:
1531 case DECLARATION_KIND_COMPOUND_MEMBER:
1532 panic("not implemented reference type");
1535 panic("reference to declaration with unknown type found");
1539 * Transform calls to builtin functions.
1541 static ir_node *process_builtin_call(const call_expression_t *call)
1543 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1545 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1546 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1548 type_t *type = skip_typeref(builtin->base.type);
1549 assert(is_type_pointer(type));
1551 type_t *function_type = skip_typeref(type->pointer.points_to);
1552 symbol_t *symbol = builtin->symbol;
1554 switch(symbol->ID) {
1555 case T___builtin_alloca: {
1556 if (call->arguments == NULL || call->arguments->next != NULL) {
1557 panic("invalid number of parameters on __builtin_alloca");
1559 expression_t *argument = call->arguments->expression;
1560 ir_node *size = expression_to_firm(argument);
1562 ir_node *store = get_store();
1563 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1565 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1567 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1572 case T___builtin_huge_val:
1573 case T___builtin_inf:
1574 case T___builtin_inff:
1575 case T___builtin_infl: {
1576 type_t *type = function_type->function.return_type;
1577 ir_mode *mode = get_ir_mode_arithmetic(type);
1578 tarval *tv = get_mode_infinite(mode);
1579 ir_node *res = new_d_Const(dbgi, tv);
1582 case T___builtin_nan:
1583 case T___builtin_nanf:
1584 case T___builtin_nanl: {
1585 /* Ignore string for now... */
1586 assert(is_type_function(function_type));
1587 type_t *type = function_type->function.return_type;
1588 ir_mode *mode = get_ir_mode_arithmetic(type);
1589 tarval *tv = get_mode_NAN(mode);
1590 ir_node *res = new_d_Const(dbgi, tv);
1593 case T___builtin_expect: {
1594 expression_t *argument = call->arguments->expression;
1595 return _expression_to_firm(argument);
1597 case T___builtin_va_end:
1598 /* evaluate the argument of va_end for its side effects */
1599 _expression_to_firm(call->arguments->expression);
1601 case T___builtin_frame_address: {
1602 expression_t *const expression = call->arguments->expression;
1603 long val = fold_constant(expression);
1606 return get_irg_frame(current_ir_graph);
1608 /* get the argument */
1611 in[0] = _expression_to_firm(expression);
1612 in[1] = get_irg_frame(current_ir_graph);
1613 ir_type *tp = get_ir_type((type_t*) function_type);
1614 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_frame_addess, 2, in, tp);
1615 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1618 case T___builtin_return_address: {
1619 expression_t *const expression = call->arguments->expression;
1622 in[0] = _expression_to_firm(expression);
1623 in[1] = get_irg_frame(current_ir_graph);
1624 ir_type *tp = get_ir_type((type_t*) function_type);
1625 ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_return_address, 2, in, tp);
1626 return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
1628 case T___builtin_prefetch: {
1629 call_argument_t *const args = call->arguments;
1630 expression_t *const addr = args->expression;
1633 in[0] = _expression_to_firm(addr);
1634 if (args->next != NULL) {
1635 expression_t *const rw = args->next->expression;
1637 in[1] = _expression_to_firm(rw);
1639 if (args->next->next != NULL) {
1640 expression_t *const locality = args->next->next->expression;
1642 in[2] = _expression_to_firm(locality);
1644 in[2] = new_Const_long(mode_int, 3);
1647 in[1] = new_Const_long(mode_int, 0);
1648 in[2] = new_Const_long(mode_int, 3);
1650 ir_type *tp = get_ir_type((type_t*) function_type);
1651 ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_prefetch, 3, in, tp);
1652 set_store(new_Proj(irn, mode_M, pn_Builtin_M));
1656 panic("unsupported builtin found");
1661 * Transform a call expression.
1662 * Handles some special cases, like alloca() calls, which must be resolved
1663 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1664 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1667 static ir_node *call_expression_to_firm(const call_expression_t *const call)
1669 dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
1670 assert(get_cur_block() != NULL);
1672 expression_t *function = call->function;
1673 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1674 return process_builtin_call(call);
1676 if (function->kind == EXPR_REFERENCE) {
1677 const reference_expression_t *ref = &function->reference;
1678 entity_t *entity = ref->entity;
1680 if (entity->kind == ENTITY_FUNCTION
1681 && entity->function.entity == rts_entities[rts_alloca]) {
1682 /* handle alloca() call */
1683 expression_t *argument = call->arguments->expression;
1684 ir_node *size = expression_to_firm(argument);
1685 ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
1687 size = create_conv(dbgi, size, mode);
1689 ir_node *store = get_store();
1690 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1692 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1694 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1699 ir_node *callee = expression_to_firm(function);
1701 type_t *type = skip_typeref(function->base.type);
1702 assert(is_type_pointer(type));
1703 pointer_type_t *pointer_type = &type->pointer;
1704 type_t *points_to = skip_typeref(pointer_type->points_to);
1705 assert(is_type_function(points_to));
1706 function_type_t *function_type = &points_to->function;
1708 int n_parameters = 0;
1709 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1710 ir_type *new_method_type = NULL;
1711 if (function_type->variadic || function_type->unspecified_parameters) {
1712 const call_argument_t *argument = call->arguments;
1713 for ( ; argument != NULL; argument = argument->next) {
1717 /* we need to construct a new method type matching the call
1719 int n_res = get_method_n_ress(ir_method_type);
1720 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1721 n_parameters, n_res, dbgi);
1722 set_method_calling_convention(new_method_type,
1723 get_method_calling_convention(ir_method_type));
1724 set_method_additional_properties(new_method_type,
1725 get_method_additional_properties(ir_method_type));
1726 set_method_variadicity(new_method_type,
1727 get_method_variadicity(ir_method_type));
1729 for (int i = 0; i < n_res; ++i) {
1730 set_method_res_type(new_method_type, i,
1731 get_method_res_type(ir_method_type, i));
1733 argument = call->arguments;
1734 for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
1735 expression_t *expression = argument->expression;
1736 ir_type *irtype = get_ir_type(expression->base.type);
1737 set_method_param_type(new_method_type, i, irtype);
1739 ir_method_type = new_method_type;
1741 n_parameters = get_method_n_params(ir_method_type);
1744 ir_node *in[n_parameters];
1746 const call_argument_t *argument = call->arguments;
1747 for (int n = 0; n < n_parameters; ++n) {
1748 expression_t *expression = argument->expression;
1749 ir_node *arg_node = expression_to_firm(expression);
1751 type_t *type = skip_typeref(expression->base.type);
1752 if (!is_type_compound(type)) {
1753 ir_mode *mode = get_ir_mode_storage(expression->base.type);
1754 arg_node = create_conv(dbgi, arg_node, mode);
1755 arg_node = do_strict_conv(dbgi, arg_node);
1760 argument = argument->next;
1763 ir_node *store = get_store();
1764 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1766 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1769 type_t *return_type = skip_typeref(function_type->return_type);
1770 ir_node *result = NULL;
1772 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1773 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1775 if (is_type_scalar(return_type)) {
1776 ir_mode *mode = get_ir_mode_storage(return_type);
1777 result = new_d_Proj(dbgi, resproj, mode, 0);
1778 ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
1779 result = create_conv(NULL, result, mode_arith);
1781 ir_mode *mode = mode_P_data;
1782 result = new_d_Proj(dbgi, resproj, mode, 0);
1786 if (function->kind == EXPR_REFERENCE &&
1787 function->reference.entity->declaration.modifiers & DM_NORETURN) {
1788 /* A dead end: Keep the Call and the Block. Also place all further
1789 * nodes into a new and unreachable block. */
1791 keep_alive(get_cur_block());
1798 static void statement_to_firm(statement_t *statement);
1799 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1801 static ir_node *expression_to_addr(const expression_t *expression);
1802 static ir_node *create_condition_evaluation(const expression_t *expression,
1803 ir_node *true_block,
1804 ir_node *false_block);
1806 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1809 if (!is_type_compound(type)) {
1810 ir_mode *mode = get_ir_mode_storage(type);
1811 value = create_conv(dbgi, value, mode);
1812 value = do_strict_conv(dbgi, value);
1815 ir_node *memory = get_store();
1817 if (is_type_scalar(type)) {
1818 ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
1819 ? cons_volatile : cons_none;
1820 ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
1821 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1822 set_store(store_mem);
1824 ir_type *irtype = get_ir_type(type);
1825 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1826 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1827 set_store(copyb_mem);
1831 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1833 tarval *all_one = get_mode_all_one(mode);
1834 int mode_size = get_mode_size_bits(mode);
1836 assert(offset >= 0);
1838 assert(offset + size <= mode_size);
1839 if (size == mode_size) {
1843 long shiftr = get_mode_size_bits(mode) - size;
1844 long shiftl = offset;
1845 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1846 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1847 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1848 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1853 static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
1854 ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
1856 ir_type *entity_type = get_entity_type(entity);
1857 ir_type *base_type = get_primitive_base_type(entity_type);
1858 assert(base_type != NULL);
1859 ir_mode *mode = get_type_mode(base_type);
1861 value = create_conv(dbgi, value, mode);
1863 /* kill upper bits of value and shift to right position */
1864 int bitoffset = get_entity_offset_bits_remainder(entity);
1865 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1867 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1868 ir_node *mask_node = new_d_Const(dbgi, mask);
1869 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1870 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1871 ir_node *shiftcount = new_d_Const(dbgi, shiftl);
1872 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1874 /* load current value */
1875 ir_node *mem = get_store();
1876 ir_node *load = new_d_Load(dbgi, mem, addr, mode,
1877 set_volatile ? cons_volatile : cons_none);
1878 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1879 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1880 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1881 tarval *inv_mask = tarval_not(shift_mask);
1882 ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
1883 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1885 /* construct new value and store */
1886 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1887 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
1888 set_volatile ? cons_volatile : cons_none);
1889 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1890 set_store(store_mem);
1892 return value_masked;
1895 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1898 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1899 type_t *type = expression->base.type;
1900 ir_mode *mode = get_ir_mode_storage(type);
1901 ir_node *mem = get_store();
1902 ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
1903 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1904 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1906 load_res = create_conv(dbgi, load_res, mode_int);
1908 set_store(load_mem);
1910 /* kill upper bits */
1911 assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
1912 ir_entity *entity = expression->compound_entry->compound_member.entity;
1913 int bitoffset = get_entity_offset_bits_remainder(entity);
1914 ir_type *entity_type = get_entity_type(entity);
1915 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1916 long shift_bitsl = machine_size - bitoffset - bitsize;
1917 assert(shift_bitsl >= 0);
1918 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1919 ir_node *countl = new_d_Const(dbgi, tvl);
1920 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1922 long shift_bitsr = bitoffset + shift_bitsl;
1923 assert(shift_bitsr <= (long) machine_size);
1924 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1925 ir_node *countr = new_d_Const(dbgi, tvr);
1927 if (mode_is_signed(mode)) {
1928 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1930 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1933 return create_conv(dbgi, shiftr, mode);
1936 /* make sure the selected compound type is constructed */
1937 static void construct_select_compound(const select_expression_t *expression)
1939 type_t *type = skip_typeref(expression->compound->base.type);
1940 if (is_type_pointer(type)) {
1941 type = type->pointer.points_to;
1943 (void) get_ir_type(type);
1946 static ir_node *set_value_for_expression_addr(const expression_t *expression,
1947 ir_node *value, ir_node *addr)
1949 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1950 type_t *type = skip_typeref(expression->base.type);
1952 if (!is_type_compound(type)) {
1953 ir_mode *mode = get_ir_mode_storage(type);
1954 value = create_conv(dbgi, value, mode);
1955 value = do_strict_conv(dbgi, value);
1958 if (expression->kind == EXPR_REFERENCE) {
1959 const reference_expression_t *ref = &expression->reference;
1961 entity_t *entity = ref->entity;
1962 assert(is_declaration(entity));
1963 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
1964 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1965 set_value(entity->variable.v.value_number, value);
1967 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
1968 set_value(entity->parameter.v.value_number, value);
1974 addr = expression_to_addr(expression);
1975 assert(addr != NULL);
1977 if (expression->kind == EXPR_SELECT) {
1978 const select_expression_t *select = &expression->select;
1980 construct_select_compound(select);
1982 entity_t *entity = select->compound_entry;
1983 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
1984 if (entity->declaration.type->kind == TYPE_BITFIELD) {
1985 ir_entity *irentity = entity->compound_member.entity;
1987 = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
1988 value = bitfield_store_to_firm(dbgi, irentity, addr, value,
1994 assign_value(dbgi, addr, type, value);
1998 static void set_value_for_expression(const expression_t *expression,
2001 set_value_for_expression_addr(expression, value, NULL);
2004 static ir_node *get_value_from_lvalue(const expression_t *expression,
2007 if (expression->kind == EXPR_REFERENCE) {
2008 const reference_expression_t *ref = &expression->reference;
2010 entity_t *entity = ref->entity;
2011 assert(entity->kind == ENTITY_VARIABLE
2012 || entity->kind == ENTITY_PARAMETER);
2013 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2015 if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
2016 value_number = entity->variable.v.value_number;
2017 assert(addr == NULL);
2018 type_t *type = skip_typeref(expression->base.type);
2019 ir_mode *mode = get_ir_mode_storage(type);
2020 ir_node *res = get_value(value_number, mode);
2021 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2022 } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
2023 value_number = entity->parameter.v.value_number;
2024 assert(addr == NULL);
2025 type_t *type = skip_typeref(expression->base.type);
2026 ir_mode *mode = get_ir_mode_storage(type);
2027 ir_node *res = get_value(value_number, mode);
2028 return create_conv(NULL, res, get_ir_mode_arithmetic(type));
2032 assert(addr != NULL);
2033 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2036 if (expression->kind == EXPR_SELECT &&
2037 expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
2038 construct_select_compound(&expression->select);
2039 value = bitfield_extract_to_firm(&expression->select, addr);
2041 value = deref_address(dbgi, expression->base.type, addr);
2048 static ir_node *create_incdec(const unary_expression_t *expression)
2050 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2051 const expression_t *value_expr = expression->value;
2052 ir_node *addr = expression_to_addr(value_expr);
2053 ir_node *value = get_value_from_lvalue(value_expr, addr);
2055 type_t *type = skip_typeref(expression->base.type);
2056 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2059 if (is_type_pointer(type)) {
2060 pointer_type_t *pointer_type = &type->pointer;
2061 offset = get_type_size(pointer_type->points_to);
2063 assert(is_type_arithmetic(type));
2064 offset = new_Const(get_mode_one(mode));
2068 ir_node *store_value;
2069 switch(expression->base.kind) {
2070 case EXPR_UNARY_POSTFIX_INCREMENT:
2072 store_value = new_d_Add(dbgi, value, offset, mode);
2074 case EXPR_UNARY_POSTFIX_DECREMENT:
2076 store_value = new_d_Sub(dbgi, value, offset, mode);
2078 case EXPR_UNARY_PREFIX_INCREMENT:
2079 result = new_d_Add(dbgi, value, offset, mode);
2080 store_value = result;
2082 case EXPR_UNARY_PREFIX_DECREMENT:
2083 result = new_d_Sub(dbgi, value, offset, mode);
2084 store_value = result;
2087 panic("no incdec expr in create_incdec");
2090 set_value_for_expression_addr(value_expr, store_value, addr);
2095 static bool is_local_variable(expression_t *expression)
2097 if (expression->kind != EXPR_REFERENCE)
2099 reference_expression_t *ref_expr = &expression->reference;
2100 entity_t *entity = ref_expr->entity;
2101 if (entity->kind != ENTITY_VARIABLE)
2103 assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
2104 return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
2107 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2110 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2111 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2112 case EXPR_BINARY_NOTEQUAL:
2113 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2114 case EXPR_BINARY_ISLESS:
2115 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2116 case EXPR_BINARY_ISLESSEQUAL:
2117 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2118 case EXPR_BINARY_ISGREATER:
2119 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2120 case EXPR_BINARY_ISGREATEREQUAL:
2121 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2122 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2127 panic("trying to get pn_Cmp from non-comparison binexpr type");
2131 * Handle the assume optimizer hint: check if a Confirm
2132 * node can be created.
2134 * @param dbi debug info
2135 * @param expr the IL assume expression
2137 * we support here only some simple cases:
2142 static ir_node *handle_assume_compare(dbg_info *dbi,
2143 const binary_expression_t *expression)
2145 expression_t *op1 = expression->left;
2146 expression_t *op2 = expression->right;
2147 entity_t *var2, *var = NULL;
2148 ir_node *res = NULL;
2151 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2153 if (is_local_variable(op1) && is_local_variable(op2)) {
2154 var = op1->reference.entity;
2155 var2 = op2->reference.entity;
2157 type_t *const type = skip_typeref(var->declaration.type);
2158 ir_mode *const mode = get_ir_mode_storage(type);
2160 ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
2161 ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
2163 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2164 set_value(var2->variable.v.value_number, res);
2166 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2167 set_value(var->variable.v.value_number, res);
2173 if (is_local_variable(op1) && is_constant_expression(op2)) {
2174 var = op1->reference.entity;
2176 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2177 cmp_val = get_inversed_pnc(cmp_val);
2178 var = op2->reference.entity;
2183 type_t *const type = skip_typeref(var->declaration.type);
2184 ir_mode *const mode = get_ir_mode_storage(type);
2186 res = get_value(var->variable.v.value_number, mode);
2187 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2188 set_value(var->variable.v.value_number, res);
2194 * Handle the assume optimizer hint.
2196 * @param dbi debug info
2197 * @param expr the IL assume expression
2199 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
2201 switch(expression->kind) {
2202 case EXPR_BINARY_EQUAL:
2203 case EXPR_BINARY_NOTEQUAL:
2204 case EXPR_BINARY_LESS:
2205 case EXPR_BINARY_LESSEQUAL:
2206 case EXPR_BINARY_GREATER:
2207 case EXPR_BINARY_GREATEREQUAL:
2208 return handle_assume_compare(dbi, &expression->binary);
2214 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2216 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2217 type_t *type = skip_typeref(expression->base.type);
2219 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2220 return expression_to_addr(expression->value);
2222 const expression_t *value = expression->value;
2224 switch(expression->base.kind) {
2225 case EXPR_UNARY_NEGATE: {
2226 ir_node *value_node = expression_to_firm(value);
2227 ir_mode *mode = get_ir_mode_arithmetic(type);
2228 return new_d_Minus(dbgi, value_node, mode);
2230 case EXPR_UNARY_PLUS:
2231 return expression_to_firm(value);
2232 case EXPR_UNARY_BITWISE_NEGATE: {
2233 ir_node *value_node = expression_to_firm(value);
2234 ir_mode *mode = get_ir_mode_arithmetic(type);
2235 return new_d_Not(dbgi, value_node, mode);
2237 case EXPR_UNARY_NOT: {
2238 ir_node *value_node = _expression_to_firm(value);
2239 value_node = create_conv(dbgi, value_node, mode_b);
2240 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2243 case EXPR_UNARY_DEREFERENCE: {
2244 ir_node *value_node = expression_to_firm(value);
2245 type_t *value_type = skip_typeref(value->base.type);
2246 assert(is_type_pointer(value_type));
2247 type_t *points_to = value_type->pointer.points_to;
2248 return deref_address(dbgi, points_to, value_node);
2250 case EXPR_UNARY_POSTFIX_INCREMENT:
2251 case EXPR_UNARY_POSTFIX_DECREMENT:
2252 case EXPR_UNARY_PREFIX_INCREMENT:
2253 case EXPR_UNARY_PREFIX_DECREMENT:
2254 return create_incdec(expression);
2255 case EXPR_UNARY_CAST: {
2256 ir_node *value_node = expression_to_firm(value);
2257 if (is_type_scalar(type)) {
2258 ir_mode *mode = get_ir_mode_storage(type);
2259 ir_node *node = create_conv(dbgi, value_node, mode);
2260 node = do_strict_conv(dbgi, node);
2261 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2262 node = create_conv(dbgi, node, mode_arith);
2265 /* make sure firm type is constructed */
2266 (void) get_ir_type(type);
2270 case EXPR_UNARY_CAST_IMPLICIT: {
2271 ir_node *value_node = expression_to_firm(value);
2272 if (is_type_scalar(type)) {
2273 ir_mode *mode = get_ir_mode_storage(type);
2274 ir_node *res = create_conv(dbgi, value_node, mode);
2275 ir_mode *mode_arith = get_ir_mode_arithmetic(type);
2276 res = create_conv(dbgi, res, mode_arith);
2282 case EXPR_UNARY_ASSUME:
2283 if (firm_opt.confirm)
2284 return handle_assume(dbgi, value);
2291 panic("invalid UNEXPR type found");
2295 * produces a 0/1 depending of the value of a mode_b node
2297 static ir_node *produce_condition_result(const expression_t *expression,
2298 ir_mode *mode, dbg_info *dbgi)
2300 ir_node *cur_block = get_cur_block();
2302 ir_node *one_block = new_immBlock();
2303 set_cur_block(one_block);
2304 ir_node *one = new_Const(get_mode_one(mode));
2305 ir_node *jmp_one = new_d_Jmp(dbgi);
2307 ir_node *zero_block = new_immBlock();
2308 set_cur_block(zero_block);
2309 ir_node *zero = new_Const(get_mode_null(mode));
2310 ir_node *jmp_zero = new_d_Jmp(dbgi);
2312 set_cur_block(cur_block);
2313 create_condition_evaluation(expression, one_block, zero_block);
2314 mature_immBlock(one_block);
2315 mature_immBlock(zero_block);
2317 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2318 new_Block(2, in_cf);
2320 ir_node *in[2] = { one, zero };
2321 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2326 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2327 ir_node *value, type_t *type)
2329 ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
2330 assert(is_type_pointer(type));
2331 pointer_type_t *const pointer_type = &type->pointer;
2332 type_t *const points_to = skip_typeref(pointer_type->points_to);
2333 ir_node * elem_size = get_type_size(points_to);
2334 elem_size = create_conv(dbgi, elem_size, mode);
2335 value = create_conv(dbgi, value, mode);
2336 ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
2340 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2341 ir_node *left, ir_node *right)
2344 type_t *type_left = skip_typeref(expression->left->base.type);
2345 type_t *type_right = skip_typeref(expression->right->base.type);
2347 expression_kind_t kind = expression->base.kind;
2350 case EXPR_BINARY_SHIFTLEFT:
2351 case EXPR_BINARY_SHIFTRIGHT:
2352 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2353 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2354 mode = get_irn_mode(left);
2355 right = create_conv(dbgi, right, mode_uint);
2358 case EXPR_BINARY_SUB:
2359 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2360 const pointer_type_t *const ptr_type = &type_left->pointer;
2362 mode = get_ir_mode_arithmetic(expression->base.type);
2363 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2364 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2365 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2366 ir_node *const no_mem = new_NoMem();
2367 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2368 mode, op_pin_state_floats);
2369 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2372 case EXPR_BINARY_SUB_ASSIGN:
2373 if (is_type_pointer(type_left)) {
2374 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2375 mode = get_ir_mode_arithmetic(type_left);
2380 case EXPR_BINARY_ADD:
2381 case EXPR_BINARY_ADD_ASSIGN:
2382 if (is_type_pointer(type_left)) {
2383 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2384 mode = get_ir_mode_arithmetic(type_left);
2386 } else if (is_type_pointer(type_right)) {
2387 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2388 mode = get_ir_mode_arithmetic(type_right);
2395 mode = get_ir_mode_arithmetic(type_right);
2396 left = create_conv(dbgi, left, mode);
2401 case EXPR_BINARY_ADD_ASSIGN:
2402 case EXPR_BINARY_ADD:
2403 return new_d_Add(dbgi, left, right, mode);
2404 case EXPR_BINARY_SUB_ASSIGN:
2405 case EXPR_BINARY_SUB:
2406 return new_d_Sub(dbgi, left, right, mode);
2407 case EXPR_BINARY_MUL_ASSIGN:
2408 case EXPR_BINARY_MUL:
2409 return new_d_Mul(dbgi, left, right, mode);
2410 case EXPR_BINARY_BITWISE_AND:
2411 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2412 return new_d_And(dbgi, left, right, mode);
2413 case EXPR_BINARY_BITWISE_OR:
2414 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2415 return new_d_Or(dbgi, left, right, mode);
2416 case EXPR_BINARY_BITWISE_XOR:
2417 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2418 return new_d_Eor(dbgi, left, right, mode);
2419 case EXPR_BINARY_SHIFTLEFT:
2420 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2421 return new_d_Shl(dbgi, left, right, mode);
2422 case EXPR_BINARY_SHIFTRIGHT:
2423 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2424 if (mode_is_signed(mode)) {
2425 return new_d_Shrs(dbgi, left, right, mode);
2427 return new_d_Shr(dbgi, left, right, mode);
2429 case EXPR_BINARY_DIV:
2430 case EXPR_BINARY_DIV_ASSIGN: {
2431 ir_node *pin = new_Pin(new_NoMem());
2434 if (mode_is_float(mode)) {
2435 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2436 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2438 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2439 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2443 case EXPR_BINARY_MOD:
2444 case EXPR_BINARY_MOD_ASSIGN: {
2445 ir_node *pin = new_Pin(new_NoMem());
2446 assert(!mode_is_float(mode));
2447 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2448 op_pin_state_floats);
2449 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2453 panic("unexpected expression kind");
2457 static ir_node *create_lazy_op(const binary_expression_t *expression)
2459 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2460 type_t *type = skip_typeref(expression->base.type);
2461 ir_mode *mode = get_ir_mode_arithmetic(type);
2463 if (is_constant_expression(expression->left)) {
2464 long val = fold_constant(expression->left);
2465 expression_kind_t ekind = expression->base.kind;
2466 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2467 if (ekind == EXPR_BINARY_LOGICAL_AND) {
2469 return new_Const(get_mode_null(mode));
2473 return new_Const(get_mode_one(mode));
2477 if (is_constant_expression(expression->right)) {
2478 long const valr = fold_constant(expression->right);
2480 new_Const(get_mode_one(mode)) :
2481 new_Const(get_mode_null(mode));
2484 return produce_condition_result(expression->right, mode, dbgi);
2487 return produce_condition_result((const expression_t*) expression, mode,
2491 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2492 ir_node *right, ir_mode *mode);
2494 static ir_node *create_assign_binop(const binary_expression_t *expression)
2496 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2497 const expression_t *left_expr = expression->left;
2498 type_t *type = skip_typeref(left_expr->base.type);
2499 ir_mode *left_mode = get_ir_mode_storage(type);
2500 ir_node *right = expression_to_firm(expression->right);
2501 ir_node *left_addr = expression_to_addr(left_expr);
2502 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2503 ir_node *result = create_op(dbgi, expression, left, right);
2505 result = create_conv(dbgi, result, left_mode);
2506 result = do_strict_conv(dbgi, result);
2508 result = set_value_for_expression_addr(left_expr, result, left_addr);
2510 if (!is_type_compound(type)) {
2511 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2512 result = create_conv(dbgi, result, mode_arithmetic);
2517 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2519 expression_kind_t kind = expression->base.kind;
2522 case EXPR_BINARY_EQUAL:
2523 case EXPR_BINARY_NOTEQUAL:
2524 case EXPR_BINARY_LESS:
2525 case EXPR_BINARY_LESSEQUAL:
2526 case EXPR_BINARY_GREATER:
2527 case EXPR_BINARY_GREATEREQUAL:
2528 case EXPR_BINARY_ISGREATER:
2529 case EXPR_BINARY_ISGREATEREQUAL:
2530 case EXPR_BINARY_ISLESS:
2531 case EXPR_BINARY_ISLESSEQUAL:
2532 case EXPR_BINARY_ISLESSGREATER:
2533 case EXPR_BINARY_ISUNORDERED: {
2534 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2535 ir_node *left = expression_to_firm(expression->left);
2536 ir_node *right = expression_to_firm(expression->right);
2537 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2538 long pnc = get_pnc(kind, expression->left->base.type);
2539 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2542 case EXPR_BINARY_ASSIGN: {
2543 ir_node *addr = expression_to_addr(expression->left);
2544 ir_node *right = expression_to_firm(expression->right);
2546 = set_value_for_expression_addr(expression->left, right, addr);
2548 type_t *type = skip_typeref(expression->base.type);
2549 if (!is_type_compound(type)) {
2550 ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
2551 res = create_conv(NULL, res, mode_arithmetic);
2555 case EXPR_BINARY_ADD:
2556 case EXPR_BINARY_SUB:
2557 case EXPR_BINARY_MUL:
2558 case EXPR_BINARY_DIV:
2559 case EXPR_BINARY_MOD:
2560 case EXPR_BINARY_BITWISE_AND:
2561 case EXPR_BINARY_BITWISE_OR:
2562 case EXPR_BINARY_BITWISE_XOR:
2563 case EXPR_BINARY_SHIFTLEFT:
2564 case EXPR_BINARY_SHIFTRIGHT:
2566 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2567 ir_node *left = expression_to_firm(expression->left);
2568 ir_node *right = expression_to_firm(expression->right);
2569 return create_op(dbgi, expression, left, right);
2571 case EXPR_BINARY_LOGICAL_AND:
2572 case EXPR_BINARY_LOGICAL_OR:
2573 return create_lazy_op(expression);
2574 case EXPR_BINARY_COMMA:
2575 /* create side effects of left side */
2576 (void) expression_to_firm(expression->left);
2577 return _expression_to_firm(expression->right);
2579 case EXPR_BINARY_ADD_ASSIGN:
2580 case EXPR_BINARY_SUB_ASSIGN:
2581 case EXPR_BINARY_MUL_ASSIGN:
2582 case EXPR_BINARY_MOD_ASSIGN:
2583 case EXPR_BINARY_DIV_ASSIGN:
2584 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2585 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2586 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2587 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2588 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2589 return create_assign_binop(expression);
2591 panic("TODO binexpr type");
2595 static ir_node *array_access_addr(const array_access_expression_t *expression)
2597 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2598 ir_node *base_addr = expression_to_firm(expression->array_ref);
2599 ir_node *offset = expression_to_firm(expression->index);
2600 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2601 ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
2602 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2607 static ir_node *array_access_to_firm(
2608 const array_access_expression_t *expression)
2610 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2611 ir_node *addr = array_access_addr(expression);
2612 type_t *type = revert_automatic_type_conversion(
2613 (const expression_t*) expression);
2614 type = skip_typeref(type);
2616 return deref_address(dbgi, type, addr);
2619 static long get_offsetof_offset(const offsetof_expression_t *expression)
2621 type_t *orig_type = expression->type;
2624 designator_t *designator = expression->designator;
2625 for ( ; designator != NULL; designator = designator->next) {
2626 type_t *type = skip_typeref(orig_type);
2627 /* be sure the type is constructed */
2628 (void) get_ir_type(type);
2630 if (designator->symbol != NULL) {
2631 assert(is_type_compound(type));
2632 symbol_t *symbol = designator->symbol;
2634 compound_t *compound = type->compound.compound;
2635 entity_t *iter = compound->members.entities;
2636 for ( ; iter != NULL; iter = iter->base.next) {
2637 if (iter->base.symbol == symbol) {
2641 assert(iter != NULL);
2643 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2644 assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2645 offset += get_entity_offset(iter->compound_member.entity);
2647 orig_type = iter->declaration.type;
2649 expression_t *array_index = designator->array_index;
2650 assert(designator->array_index != NULL);
2651 assert(is_type_array(type));
2653 long index = fold_constant(array_index);
2654 ir_type *arr_type = get_ir_type(type);
2655 ir_type *elem_type = get_array_element_type(arr_type);
2656 long elem_size = get_type_size_bytes(elem_type);
2658 offset += index * elem_size;
2660 orig_type = type->array.element_type;
2667 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2669 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
2670 long offset = get_offsetof_offset(expression);
2671 tarval *tv = new_tarval_from_long(offset, mode);
2672 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2674 return new_d_Const(dbgi, tv);
2677 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2678 ir_entity *entity, type_t *type);
2680 static ir_node *compound_literal_to_firm(
2681 const compound_literal_expression_t *expression)
2683 type_t *type = expression->type;
2685 /* create an entity on the stack */
2686 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2688 ident *const id = id_unique("CompLit.%u");
2689 ir_type *const irtype = get_ir_type(type);
2690 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2691 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2692 set_entity_ld_ident(entity, id);
2694 set_entity_variability(entity, variability_uninitialized);
2696 /* create initialisation code */
2697 initializer_t *initializer = expression->initializer;
2698 create_local_initializer(initializer, dbgi, entity, type);
2700 /* create a sel for the compound literal address */
2701 ir_node *frame = get_local_frame(entity);
2702 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2707 * Transform a sizeof expression into Firm code.
2709 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2711 type_t *type = expression->type;
2713 type = expression->tp_expression->base.type;
2714 assert(type != NULL);
2717 type = skip_typeref(type);
2718 /* ยง6.5.3.4:2 if the type is a VLA, evaluate the expression. */
2719 if (is_type_array(type) && type->array.is_vla
2720 && expression->tp_expression != NULL) {
2721 expression_to_firm(expression->tp_expression);
2724 return get_type_size(type);
2727 static entity_t *get_expression_entity(const expression_t *expression)
2729 if (expression->kind != EXPR_REFERENCE)
2732 return expression->reference.entity;
2736 * Transform an alignof expression into Firm code.
2738 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2740 ir_entity *irentity = NULL;
2742 const expression_t *tp_expression = expression->tp_expression;
2743 if (tp_expression != NULL) {
2744 entity_t *entity = get_expression_entity(tp_expression);
2745 if (entity != NULL && is_declaration(entity)) {
2746 switch (entity->declaration.kind) {
2747 case DECLARATION_KIND_UNKNOWN:
2748 panic("unknown entity reference found");
2749 case DECLARATION_KIND_COMPOUND_MEMBER:
2750 irentity = entity->compound_member.entity;
2752 case DECLARATION_KIND_GLOBAL_VARIABLE:
2753 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
2754 irentity = entity->variable.v.entity;
2756 case DECLARATION_KIND_PARAMETER_ENTITY:
2757 irentity = entity->parameter.v.entity;
2759 case DECLARATION_KIND_FUNCTION:
2760 case DECLARATION_KIND_INNER_FUNCTION:
2761 irentity = entity->function.entity;
2763 case DECLARATION_KIND_PARAMETER:
2764 case DECLARATION_KIND_LOCAL_VARIABLE:
2765 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
2772 if (irentity != NULL) {
2773 irtype = get_entity_type(irentity);
2775 type_t *type = expression->type;
2776 irtype = get_ir_type(type);
2779 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
2780 symconst_symbol sym;
2781 sym.type_p = irtype;
2782 return new_SymConst(mode, sym, symconst_type_align);
2785 static void init_ir_types(void);
2787 long fold_constant(const expression_t *expression)
2789 assert(is_type_valid(skip_typeref(expression->base.type)));
2791 bool constant_folding_old = constant_folding;
2792 constant_folding = true;
2796 assert(is_constant_expression(expression));
2798 ir_graph *old_current_ir_graph = current_ir_graph;
2799 current_ir_graph = get_const_code_irg();
2801 ir_node *cnst = expression_to_firm(expression);
2802 current_ir_graph = old_current_ir_graph;
2804 if (!is_Const(cnst)) {
2805 panic("couldn't fold constant");
2808 tarval *tv = get_Const_tarval(cnst);
2809 if (!tarval_is_long(tv)) {
2810 panic("result of constant folding is not integer");
2813 constant_folding = constant_folding_old;
2815 return get_tarval_long(tv);
2818 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2820 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2822 /* first try to fold a constant condition */
2823 if (is_constant_expression(expression->condition)) {
2824 long val = fold_constant(expression->condition);
2826 expression_t *true_expression = expression->true_expression;
2827 if (true_expression == NULL)
2828 true_expression = expression->condition;
2829 return expression_to_firm(true_expression);
2831 return expression_to_firm(expression->false_expression);
2835 ir_node *cur_block = get_cur_block();
2837 /* create the true block */
2838 ir_node *true_block = new_immBlock();
2839 set_cur_block(true_block);
2841 ir_node *true_val = expression->true_expression != NULL ?
2842 expression_to_firm(expression->true_expression) : NULL;
2843 ir_node *true_jmp = new_Jmp();
2845 /* create the false block */
2846 ir_node *false_block = new_immBlock();
2847 set_cur_block(false_block);
2849 ir_node *false_val = expression_to_firm(expression->false_expression);
2850 ir_node *false_jmp = new_Jmp();
2852 /* create the condition evaluation */
2853 set_cur_block(cur_block);
2854 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
2855 if (expression->true_expression == NULL) {
2856 if (cond_expr != NULL) {
2857 true_val = cond_expr;
2859 /* Condition ended with a short circuit (&&, ||, !) operation.
2860 * Generate a "1" as value for the true branch. */
2861 true_val = new_Const(get_mode_one(mode_Is));
2864 mature_immBlock(true_block);
2865 mature_immBlock(false_block);
2867 /* create the common block */
2868 ir_node *in_cf[2] = { true_jmp, false_jmp };
2869 new_Block(2, in_cf);
2871 /* TODO improve static semantics, so either both or no values are NULL */
2872 if (true_val == NULL || false_val == NULL)
2875 ir_node *in[2] = { true_val, false_val };
2876 ir_mode *mode = get_irn_mode(true_val);
2877 assert(get_irn_mode(false_val) == mode);
2878 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2884 * Returns an IR-node representing the address of a field.
2886 static ir_node *select_addr(const select_expression_t *expression)
2888 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2890 construct_select_compound(expression);
2892 ir_node *compound_addr = expression_to_firm(expression->compound);
2894 entity_t *entry = expression->compound_entry;
2895 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
2896 assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
2898 if (constant_folding) {
2899 ir_mode *mode = get_irn_mode(compound_addr);
2900 /* FIXME: here, we need an integer mode with the same number of bits as mode */
2901 ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
2902 return new_d_Add(dbgi, compound_addr, ofs, mode);
2904 ir_entity *irentity = entry->compound_member.entity;
2905 assert(irentity != NULL);
2906 return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
2910 static ir_node *select_to_firm(const select_expression_t *expression)
2912 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2913 ir_node *addr = select_addr(expression);
2914 type_t *type = revert_automatic_type_conversion(
2915 (const expression_t*) expression);
2916 type = skip_typeref(type);
2918 entity_t *entry = expression->compound_entry;
2919 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
2920 type_t *entry_type = skip_typeref(entry->declaration.type);
2922 if (entry_type->kind == TYPE_BITFIELD) {
2923 return bitfield_extract_to_firm(expression, addr);
2926 return deref_address(dbgi, type, addr);
2929 /* Values returned by __builtin_classify_type. */
2930 typedef enum gcc_type_class
2936 enumeral_type_class,
2939 reference_type_class,
2943 function_type_class,
2954 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2956 type_t *type = expr->type_expression->base.type;
2958 /* FIXME gcc returns different values depending on whether compiling C or C++
2959 * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
2962 type = skip_typeref(type);
2963 switch (type->kind) {
2965 const atomic_type_t *const atomic_type = &type->atomic;
2966 switch (atomic_type->akind) {
2967 /* should not be reached */
2968 case ATOMIC_TYPE_INVALID:
2972 /* gcc cannot do that */
2973 case ATOMIC_TYPE_VOID:
2974 tc = void_type_class;
2977 case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
2978 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2979 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2980 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2981 case ATOMIC_TYPE_SHORT:
2982 case ATOMIC_TYPE_USHORT:
2983 case ATOMIC_TYPE_INT:
2984 case ATOMIC_TYPE_UINT:
2985 case ATOMIC_TYPE_LONG:
2986 case ATOMIC_TYPE_ULONG:
2987 case ATOMIC_TYPE_LONGLONG:
2988 case ATOMIC_TYPE_ULONGLONG:
2989 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2990 tc = integer_type_class;
2993 case ATOMIC_TYPE_FLOAT:
2994 case ATOMIC_TYPE_DOUBLE:
2995 case ATOMIC_TYPE_LONG_DOUBLE:
2996 tc = real_type_class;
2999 panic("Unexpected atomic type in classify_type_to_firm().");
3002 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
3003 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
3004 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
3005 case TYPE_ARRAY: /* gcc handles this as pointer */
3006 case TYPE_FUNCTION: /* gcc handles this as pointer */
3007 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
3008 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
3009 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
3011 /* gcc handles this as integer */
3012 case TYPE_ENUM: tc = integer_type_class; goto make_const;
3014 /* gcc classifies the referenced type */
3015 case TYPE_REFERENCE: type = type->reference.refers_to; continue;
3018 /* typedef/typeof should be skipped already */
3025 panic("unexpected TYPE classify_type_to_firm().");
3029 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3030 tarval *const tv = new_tarval_from_long(tc, mode_int);
3031 return new_d_Const(dbgi, tv);
3034 static ir_node *function_name_to_firm(
3035 const funcname_expression_t *const expr)
3037 switch(expr->kind) {
3038 case FUNCNAME_FUNCTION:
3039 case FUNCNAME_PRETTY_FUNCTION:
3040 case FUNCNAME_FUNCDNAME:
3041 if (current_function_name == NULL) {
3042 const source_position_t *const src_pos = &expr->base.source_position;
3043 const char *name = current_function_entity->base.symbol->string;
3044 const string_t string = { name, strlen(name) + 1 };
3045 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
3047 return current_function_name;
3048 case FUNCNAME_FUNCSIG:
3049 if (current_funcsig == NULL) {
3050 const source_position_t *const src_pos = &expr->base.source_position;
3051 ir_entity *ent = get_irg_entity(current_ir_graph);
3052 const char *const name = get_entity_ld_name(ent);
3053 const string_t string = { name, strlen(name) + 1 };
3054 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
3056 return current_funcsig;
3058 panic("Unsupported function name");
3061 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
3063 statement_t *statement = expr->statement;
3065 assert(statement->kind == STATEMENT_COMPOUND);
3066 return compound_statement_to_firm(&statement->compound);
3069 static ir_node *va_start_expression_to_firm(
3070 const va_start_expression_t *const expr)
3072 type_t *const type = current_function_entity->declaration.type;
3073 ir_type *const method_type = get_ir_type(type);
3074 int const n = get_method_n_params(method_type) - 1;
3075 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
3076 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
3077 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3078 ir_node *const no_mem = new_NoMem();
3079 ir_node *const arg_sel =
3080 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
3082 ir_node *const cnst = get_type_size(expr->parameter->base.type);
3083 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
3084 set_value_for_expression(expr->ap, add);
3089 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
3091 type_t *const type = expr->base.type;
3092 expression_t *const ap_expr = expr->ap;
3093 ir_node *const ap_addr = expression_to_addr(ap_expr);
3094 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
3095 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
3096 ir_node *const res = deref_address(dbgi, type, ap);
3098 ir_node *const cnst = get_type_size(expr->base.type);
3099 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
3101 set_value_for_expression_addr(ap_expr, add, ap_addr);
3106 static ir_node *dereference_addr(const unary_expression_t *const expression)
3108 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
3109 return expression_to_firm(expression->value);
3113 * Returns a IR-node representing an lvalue of the given expression.
3115 static ir_node *expression_to_addr(const expression_t *expression)
3117 switch(expression->kind) {
3118 case EXPR_ARRAY_ACCESS:
3119 return array_access_addr(&expression->array_access);
3121 return call_expression_to_firm(&expression->call);
3122 case EXPR_COMPOUND_LITERAL:
3123 return compound_literal_to_firm(&expression->compound_literal);
3124 case EXPR_REFERENCE:
3125 return reference_addr(&expression->reference);
3127 return select_addr(&expression->select);
3128 case EXPR_UNARY_DEREFERENCE:
3129 return dereference_addr(&expression->unary);
3133 panic("trying to get address of non-lvalue");
3136 static ir_node *builtin_constant_to_firm(
3137 const builtin_constant_expression_t *expression)
3139 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3142 if (is_constant_expression(expression->value)) {
3147 return new_Const_long(mode, v);
3150 static ir_node *builtin_types_compatible_to_firm(
3151 const builtin_types_compatible_expression_t *expression)
3153 type_t *const left = get_unqualified_type(skip_typeref(expression->left));
3154 type_t *const right = get_unqualified_type(skip_typeref(expression->right));
3155 long const value = types_compatible(left, right) ? 1 : 0;
3156 ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
3157 return new_Const_long(mode, value);
3160 static ir_node *get_label_block(label_t *label)
3162 if (label->block != NULL)
3163 return label->block;
3165 /* beware: might be called from create initializer with current_ir_graph
3166 * set to const_code_irg. */
3167 ir_graph *rem = current_ir_graph;
3168 current_ir_graph = current_function;
3170 ir_node *block = new_immBlock();
3172 label->block = block;
3174 ARR_APP1(label_t *, all_labels, label);
3176 current_ir_graph = rem;
3181 * Pointer to a label. This is used for the
3182 * GNU address-of-label extension.
3184 static ir_node *label_address_to_firm(
3185 const label_address_expression_t *label)
3187 ir_node *block = get_label_block(label->label);
3188 ir_label_t nr = get_Block_label(block);
3191 nr = get_irp_next_label_nr();
3192 set_Block_label(block, nr);
3194 symconst_symbol value;
3196 return new_SymConst(mode_P_code, value, symconst_label);
3199 static ir_node *builtin_symbol_to_firm(
3200 const builtin_symbol_expression_t *expression)
3202 /* for gcc compatibility we have to produce (dummy) addresses for some
3204 if (warning.other) {
3205 warningf(&expression->base.source_position,
3206 "taking address of builtin '%Y'", expression->symbol);
3209 /* simply create a NULL pointer */
3210 ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
3211 ir_node *res = new_Const_long(mode, 0);
3217 * creates firm nodes for an expression. The difference between this function
3218 * and expression_to_firm is, that this version might produce mode_b nodes
3219 * instead of mode_Is.
3221 static ir_node *_expression_to_firm(const expression_t *expression)
3224 if (!constant_folding) {
3225 assert(!expression->base.transformed);
3226 ((expression_t*) expression)->base.transformed = true;
3230 switch (expression->kind) {
3231 case EXPR_CHARACTER_CONSTANT:
3232 return character_constant_to_firm(&expression->conste);
3233 case EXPR_WIDE_CHARACTER_CONSTANT:
3234 return wide_character_constant_to_firm(&expression->conste);
3236 return const_to_firm(&expression->conste);
3237 case EXPR_STRING_LITERAL:
3238 return string_literal_to_firm(&expression->string);
3239 case EXPR_WIDE_STRING_LITERAL:
3240 return wide_string_literal_to_firm(&expression->wide_string);
3241 case EXPR_REFERENCE:
3242 return reference_expression_to_firm(&expression->reference);
3243 case EXPR_REFERENCE_ENUM_VALUE:
3244 return reference_expression_enum_value_to_firm(&expression->reference);
3246 return call_expression_to_firm(&expression->call);
3248 return unary_expression_to_firm(&expression->unary);
3250 return binary_expression_to_firm(&expression->binary);
3251 case EXPR_ARRAY_ACCESS:
3252 return array_access_to_firm(&expression->array_access);
3254 return sizeof_to_firm(&expression->typeprop);
3256 return alignof_to_firm(&expression->typeprop);
3257 case EXPR_CONDITIONAL:
3258 return conditional_to_firm(&expression->conditional);
3260 return select_to_firm(&expression->select);
3261 case EXPR_CLASSIFY_TYPE:
3262 return classify_type_to_firm(&expression->classify_type);
3264 return function_name_to_firm(&expression->funcname);
3265 case EXPR_STATEMENT:
3266 return statement_expression_to_firm(&expression->statement);
3268 return va_start_expression_to_firm(&expression->va_starte);
3270 return va_arg_expression_to_firm(&expression->va_arge);
3271 case EXPR_BUILTIN_SYMBOL:
3272 return builtin_symbol_to_firm(&expression->builtin_symbol);
3273 case EXPR_BUILTIN_CONSTANT_P:
3274 return builtin_constant_to_firm(&expression->builtin_constant);
3275 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
3276 return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
3278 return offsetof_to_firm(&expression->offsetofe);
3279 case EXPR_COMPOUND_LITERAL:
3280 return compound_literal_to_firm(&expression->compound_literal);
3281 case EXPR_LABEL_ADDRESS:
3282 return label_address_to_firm(&expression->label_address);
3288 panic("invalid expression found");
3291 static bool is_builtin_expect(const expression_t *expression)
3293 if (expression->kind != EXPR_CALL)
3296 expression_t *function = expression->call.function;
3297 if (function->kind != EXPR_BUILTIN_SYMBOL)
3299 if (function->builtin_symbol.symbol->ID != T___builtin_expect)
3305 static bool produces_mode_b(const expression_t *expression)
3307 switch (expression->kind) {
3308 case EXPR_BINARY_EQUAL:
3309 case EXPR_BINARY_NOTEQUAL:
3310 case EXPR_BINARY_LESS:
3311 case EXPR_BINARY_LESSEQUAL:
3312 case EXPR_BINARY_GREATER:
3313 case EXPR_BINARY_GREATEREQUAL:
3314 case EXPR_BINARY_ISGREATER:
3315 case EXPR_BINARY_ISGREATEREQUAL:
3316 case EXPR_BINARY_ISLESS:
3317 case EXPR_BINARY_ISLESSEQUAL:
3318 case EXPR_BINARY_ISLESSGREATER:
3319 case EXPR_BINARY_ISUNORDERED:
3320 case EXPR_UNARY_NOT:
3324 if (is_builtin_expect(expression)) {
3325 expression_t *argument = expression->call.arguments->expression;
3326 return produces_mode_b(argument);
3329 case EXPR_BINARY_COMMA:
3330 return produces_mode_b(expression->binary.right);
3337 static ir_node *expression_to_firm(const expression_t *expression)
3339 if (!produces_mode_b(expression)) {
3340 ir_node *res = _expression_to_firm(expression);
3341 assert(res == NULL || get_irn_mode(res) != mode_b);
3345 if (is_constant_expression(expression)) {
3346 ir_node *res = _expression_to_firm(expression);
3347 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3348 assert(is_Const(res));
3349 if (is_Const_null(res)) {
3350 return new_Const_long(mode, 0);
3352 return new_Const_long(mode, 1);
3356 /* we have to produce a 0/1 from the mode_b expression */
3357 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3358 ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
3359 return produce_condition_result(expression, mode, dbgi);
3363 * create a short-circuit expression evaluation that tries to construct
3364 * efficient control flow structures for &&, || and ! expressions
3366 static ir_node *create_condition_evaluation(const expression_t *expression,
3367 ir_node *true_block,
3368 ir_node *false_block)
3370 switch(expression->kind) {
3371 case EXPR_UNARY_NOT: {
3372 const unary_expression_t *unary_expression = &expression->unary;
3373 create_condition_evaluation(unary_expression->value, false_block,
3377 case EXPR_BINARY_LOGICAL_AND: {
3378 const binary_expression_t *binary_expression = &expression->binary;
3380 ir_node *extra_block = new_immBlock();
3381 create_condition_evaluation(binary_expression->left, extra_block,
3383 mature_immBlock(extra_block);
3384 set_cur_block(extra_block);
3385 create_condition_evaluation(binary_expression->right, true_block,
3389 case EXPR_BINARY_LOGICAL_OR: {
3390 const binary_expression_t *binary_expression = &expression->binary;
3392 ir_node *extra_block = new_immBlock();
3393 create_condition_evaluation(binary_expression->left, true_block,
3395 mature_immBlock(extra_block);
3396 set_cur_block(extra_block);
3397 create_condition_evaluation(binary_expression->right, true_block,
3405 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3406 ir_node *cond_expr = _expression_to_firm(expression);
3407 ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
3408 ir_node *cond = new_d_Cond(dbgi, condition);
3409 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3410 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3412 /* set branch prediction info based on __builtin_expect */
3413 if (is_builtin_expect(expression) && is_Cond(cond)) {
3414 call_argument_t *argument = expression->call.arguments->next;
3415 if (is_constant_expression(argument->expression)) {
3416 long cnst = fold_constant(argument->expression);
3417 cond_jmp_predicate pred;
3420 pred = COND_JMP_PRED_FALSE;
3422 pred = COND_JMP_PRED_TRUE;
3424 set_Cond_jmp_pred(cond, pred);
3428 add_immBlock_pred(true_block, true_proj);
3429 add_immBlock_pred(false_block, false_proj);
3431 set_cur_block(NULL);
3436 static void create_variable_entity(entity_t *variable,
3437 declaration_kind_t declaration_kind,
3438 ir_type *parent_type)
3440 assert(variable->kind == ENTITY_VARIABLE);
3441 type_t *type = skip_typeref(variable->declaration.type);
3442 type = get_aligned_type(type, variable->variable.alignment);
3444 ident *const id = new_id_from_str(variable->base.symbol->string);
3445 ir_type *const irtype = get_ir_type(type);
3446 dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
3448 ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
3450 handle_gnu_attributes_ent(irentity, variable);
3452 variable->declaration.kind = (unsigned char) declaration_kind;
3453 variable->variable.v.entity = irentity;
3454 set_entity_variability(irentity, variability_uninitialized);
3455 set_entity_ld_ident(irentity, create_ld_ident(variable));
3457 if (parent_type == get_tls_type())
3458 set_entity_allocation(irentity, allocation_automatic);
3459 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3460 set_entity_allocation(irentity, allocation_static);
3462 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3463 set_entity_volatility(irentity, volatility_is_volatile);
3468 typedef struct type_path_entry_t type_path_entry_t;
3469 struct type_path_entry_t {
3471 ir_initializer_t *initializer;
3473 entity_t *compound_entry;
3476 typedef struct type_path_t type_path_t;
3477 struct type_path_t {
3478 type_path_entry_t *path;
3483 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3485 size_t len = ARR_LEN(path->path);
3487 for (size_t i = 0; i < len; ++i) {
3488 const type_path_entry_t *entry = & path->path[i];
3490 type_t *type = skip_typeref(entry->type);
3491 if (is_type_compound(type)) {
3492 fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
3493 } else if (is_type_array(type)) {
3494 fprintf(stderr, "[%u]", (unsigned) entry->index);
3496 fprintf(stderr, "-INVALID-");
3499 fprintf(stderr, " (");
3500 print_type(path->top_type);
3501 fprintf(stderr, ")");
3504 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3506 size_t len = ARR_LEN(path->path);
3508 return & path->path[len-1];
3511 static type_path_entry_t *append_to_type_path(type_path_t *path)
3513 size_t len = ARR_LEN(path->path);
3514 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3516 type_path_entry_t *result = & path->path[len];
3517 memset(result, 0, sizeof(result[0]));
3521 static size_t get_compound_member_count(const compound_type_t *type)
3523 compound_t *compound = type->compound;
3524 size_t n_members = 0;
3525 entity_t *member = compound->members.entities;
3526 for ( ; member != NULL; member = member->base.next) {
3533 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3535 type_t *orig_top_type = path->top_type;
3536 type_t *top_type = skip_typeref(orig_top_type);
3538 assert(is_type_compound(top_type) || is_type_array(top_type));
3540 if (ARR_LEN(path->path) == 0) {
3543 type_path_entry_t *top = get_type_path_top(path);
3544 ir_initializer_t *initializer = top->initializer;
3545 return get_initializer_compound_value(initializer, top->index);
3549 static void descend_into_subtype(type_path_t *path)
3551 type_t *orig_top_type = path->top_type;
3552 type_t *top_type = skip_typeref(orig_top_type);
3554 assert(is_type_compound(top_type) || is_type_array(top_type));
3556 ir_initializer_t *initializer = get_initializer_entry(path);
3558 type_path_entry_t *top = append_to_type_path(path);
3559 top->type = top_type;
3563 if (is_type_compound(top_type)) {
3564 compound_t *compound = top_type->compound.compound;
3565 entity_t *entry = compound->members.entities;
3567 top->compound_entry = entry;
3569 len = get_compound_member_count(&top_type->compound);
3570 if (entry != NULL) {
3571 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3572 path->top_type = entry->declaration.type;
3575 assert(is_type_array(top_type));
3576 assert(top_type->array.size > 0);
3579 path->top_type = top_type->array.element_type;
3580 len = top_type->array.size;
3582 if (initializer == NULL
3583 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3584 initializer = create_initializer_compound(len);
3585 /* we have to set the entry at the 2nd latest path entry... */
3586 size_t path_len = ARR_LEN(path->path);
3587 assert(path_len >= 1);
3589 type_path_entry_t *entry = & path->path[path_len-2];
3590 ir_initializer_t *tinitializer = entry->initializer;
3591 set_initializer_compound_value(tinitializer, entry->index,
3595 top->initializer = initializer;
3598 static void ascend_from_subtype(type_path_t *path)
3600 type_path_entry_t *top = get_type_path_top(path);
3602 path->top_type = top->type;
3604 size_t len = ARR_LEN(path->path);
3605 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3608 static void walk_designator(type_path_t *path, const designator_t *designator)
3610 /* designators start at current object type */
3611 ARR_RESIZE(type_path_entry_t, path->path, 1);
3613 for ( ; designator != NULL; designator = designator->next) {
3614 type_path_entry_t *top = get_type_path_top(path);
3615 type_t *orig_type = top->type;
3616 type_t *type = skip_typeref(orig_type);
3618 if (designator->symbol != NULL) {
3619 assert(is_type_compound(type));
3621 symbol_t *symbol = designator->symbol;
3623 compound_t *compound = type->compound.compound;
3624 entity_t *iter = compound->members.entities;
3625 for ( ; iter != NULL; iter = iter->base.next, ++index) {
3626 if (iter->base.symbol == symbol) {
3627 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
3631 assert(iter != NULL);
3633 top->type = orig_type;
3634 top->compound_entry = iter;
3636 orig_type = iter->declaration.type;
3638 expression_t *array_index = designator->array_index;
3639 assert(designator->array_index != NULL);
3640 assert(is_type_array(type));
3642 long index = fold_constant(array_index);
3645 if (type->array.size_constant) {
3646 long array_size = type->array.size;
3647 assert(index < array_size);
3651 top->type = orig_type;
3652 top->index = (size_t) index;
3653 orig_type = type->array.element_type;
3655 path->top_type = orig_type;
3657 if (designator->next != NULL) {
3658 descend_into_subtype(path);
3662 path->invalid = false;
3665 static void advance_current_object(type_path_t *path)
3667 if (path->invalid) {
3668 /* TODO: handle this... */
3669 panic("invalid initializer in ast2firm (excessive elements)");
3672 type_path_entry_t *top = get_type_path_top(path);
3674 type_t *type = skip_typeref(top->type);
3675 if (is_type_union(type)) {
3676 top->compound_entry = NULL;
3677 } else if (is_type_struct(type)) {
3678 entity_t *entry = top->compound_entry;
3681 entry = entry->base.next;
3682 top->compound_entry = entry;
3683 if (entry != NULL) {
3684 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
3685 path->top_type = entry->declaration.type;
3689 assert(is_type_array(type));
3692 if (!type->array.size_constant || top->index < type->array.size) {
3697 /* we're past the last member of the current sub-aggregate, try if we
3698 * can ascend in the type hierarchy and continue with another subobject */
3699 size_t len = ARR_LEN(path->path);
3702 ascend_from_subtype(path);
3703 advance_current_object(path);
3705 path->invalid = true;
3710 static ir_initializer_t *create_ir_initializer(
3711 const initializer_t *initializer, type_t *type);
3713 static ir_initializer_t *create_ir_initializer_value(
3714 const initializer_value_t *initializer)
3716 if (is_type_compound(initializer->value->base.type)) {
3717 panic("initializer creation for compounds not implemented yet");
3719 ir_node *value = expression_to_firm(initializer->value);
3720 type_t *type = initializer->value->base.type;
3721 ir_mode *mode = get_ir_mode_storage(type);
3722 value = create_conv(NULL, value, mode);
3723 return create_initializer_const(value);
3726 /** test wether type can be initialized by a string constant */
3727 static bool is_string_type(type_t *type)
3730 if (is_type_pointer(type)) {
3731 inner = skip_typeref(type->pointer.points_to);
3732 } else if(is_type_array(type)) {
3733 inner = skip_typeref(type->array.element_type);
3738 return is_type_integer(inner);
3741 static ir_initializer_t *create_ir_initializer_list(
3742 const initializer_list_t *initializer, type_t *type)
3745 memset(&path, 0, sizeof(path));
3746 path.top_type = type;
3747 path.path = NEW_ARR_F(type_path_entry_t, 0);
3749 descend_into_subtype(&path);
3751 for (size_t i = 0; i < initializer->len; ++i) {
3752 const initializer_t *sub_initializer = initializer->initializers[i];
3754 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3755 walk_designator(&path, sub_initializer->designator.designator);
3759 if (sub_initializer->kind == INITIALIZER_VALUE) {
3760 /* we might have to descend into types until we're at a scalar
3763 type_t *orig_top_type = path.top_type;
3764 type_t *top_type = skip_typeref(orig_top_type);
3766 if (is_type_scalar(top_type))
3768 descend_into_subtype(&path);
3770 } else if (sub_initializer->kind == INITIALIZER_STRING
3771 || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
3772 /* we might have to descend into types until we're at a scalar
3775 type_t *orig_top_type = path.top_type;
3776 type_t *top_type = skip_typeref(orig_top_type);
3778 if (is_string_type(top_type))
3780 descend_into_subtype(&path);
3784 ir_initializer_t *sub_irinitializer
3785 = create_ir_initializer(sub_initializer, path.top_type);
3787 size_t path_len = ARR_LEN(path.path);
3788 assert(path_len >= 1);
3789 type_path_entry_t *entry = & path.path[path_len-1];
3790 ir_initializer_t *tinitializer = entry->initializer;
3791 set_initializer_compound_value(tinitializer, entry->index,
3794 advance_current_object(&path);
3797 assert(ARR_LEN(path.path) >= 1);
3798 ir_initializer_t *result = path.path[0].initializer;
3799 DEL_ARR_F(path.path);
3804 static ir_initializer_t *create_ir_initializer_string(
3805 const initializer_string_t *initializer, type_t *type)
3807 type = skip_typeref(type);
3809 size_t string_len = initializer->string.size;
3810 assert(type->kind == TYPE_ARRAY);
3811 assert(type->array.size_constant);
3812 size_t len = type->array.size;
3813 ir_initializer_t *irinitializer = create_initializer_compound(len);
3815 const char *string = initializer->string.begin;
3816 ir_mode *mode = get_ir_mode_storage(type->array.element_type);
3818 for (size_t i = 0; i < len; ++i) {
3823 tarval *tv = new_tarval_from_long(c, mode);
3824 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3826 set_initializer_compound_value(irinitializer, i, char_initializer);
3829 return irinitializer;
3832 static ir_initializer_t *create_ir_initializer_wide_string(
3833 const initializer_wide_string_t *initializer, type_t *type)
3835 size_t string_len = initializer->string.size;
3836 assert(type->kind == TYPE_ARRAY);
3837 assert(type->array.size_constant);
3838 size_t len = type->array.size;
3839 ir_initializer_t *irinitializer = create_initializer_compound(len);
3841 const wchar_rep_t *string = initializer->string.begin;
3842 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3844 for (size_t i = 0; i < len; ++i) {
3846 if (i < string_len) {
3849 tarval *tv = new_tarval_from_long(c, mode);
3850 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3852 set_initializer_compound_value(irinitializer, i, char_initializer);
3855 return irinitializer;
3858 static ir_initializer_t *create_ir_initializer(
3859 const initializer_t *initializer, type_t *type)
3861 switch(initializer->kind) {
3862 case INITIALIZER_STRING:
3863 return create_ir_initializer_string(&initializer->string, type);
3865 case INITIALIZER_WIDE_STRING:
3866 return create_ir_initializer_wide_string(&initializer->wide_string,
3869 case INITIALIZER_LIST:
3870 return create_ir_initializer_list(&initializer->list, type);
3872 case INITIALIZER_VALUE:
3873 return create_ir_initializer_value(&initializer->value);
3875 case INITIALIZER_DESIGNATOR:
3876 panic("unexpected designator initializer found");
3878 panic("unknown initializer");
3881 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3884 if (is_atomic_type(type)) {
3885 ir_mode *mode = get_type_mode(type);
3886 tarval *zero = get_mode_null(mode);
3887 ir_node *cnst = new_d_Const(dbgi, zero);
3889 /* TODO: bitfields */
3890 ir_node *mem = get_store();
3891 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
3892 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3895 assert(is_compound_type(type));
3898 if (is_Array_type(type)) {
3899 assert(has_array_upper_bound(type, 0));
3900 n_members = get_array_upper_bound_int(type, 0);
3902 n_members = get_compound_n_members(type);
3905 for (int i = 0; i < n_members; ++i) {
3908 if (is_Array_type(type)) {
3909 ir_entity *entity = get_array_element_entity(type);
3910 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3911 ir_node *cnst = new_d_Const(dbgi, index_tv);
3912 ir_node *in[1] = { cnst };
3913 irtype = get_array_element_type(type);
3914 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3916 ir_entity *member = get_compound_member(type, i);
3918 irtype = get_entity_type(member);
3919 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3922 create_dynamic_null_initializer(irtype, dbgi, addr);
3927 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3928 ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3930 switch(get_initializer_kind(initializer)) {
3931 case IR_INITIALIZER_NULL: {
3932 create_dynamic_null_initializer(type, dbgi, base_addr);
3935 case IR_INITIALIZER_CONST: {
3936 ir_node *node = get_initializer_const_value(initializer);
3937 ir_mode *mode = get_irn_mode(node);
3938 ir_type *ent_type = get_entity_type(entity);
3940 /* is it a bitfield type? */
3941 if (is_Primitive_type(ent_type) &&
3942 get_primitive_base_type(ent_type) != NULL) {
3943 bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
3947 assert(get_type_mode(type) == mode);
3948 ir_node *mem = get_store();
3949 ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
3950 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3954 case IR_INITIALIZER_TARVAL: {
3955 tarval *tv = get_initializer_tarval_value(initializer);
3956 ir_mode *mode = get_tarval_mode(tv);
3957 ir_node *cnst = new_d_Const(dbgi, tv);
3958 ir_type *ent_type = get_entity_type(entity);
3960 /* is it a bitfield type? */
3961 if (is_Primitive_type(ent_type) &&
3962 get_primitive_base_type(ent_type) != NULL) {
3963 bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
3967 assert(get_type_mode(type) == mode);
3968 ir_node *mem = get_store();
3969 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
3970 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3974 case IR_INITIALIZER_COMPOUND: {
3975 assert(is_compound_type(type));
3977 if (is_Array_type(type)) {
3978 assert(has_array_upper_bound(type, 0));
3979 n_members = get_array_upper_bound_int(type, 0);
3981 n_members = get_compound_n_members(type);
3984 if (get_initializer_compound_n_entries(initializer)
3985 != (unsigned) n_members)
3986 panic("initializer doesn't match compound type");
3988 for (int i = 0; i < n_members; ++i) {
3991 ir_entity *sub_entity;
3992 if (is_Array_type(type)) {
3993 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3994 ir_node *cnst = new_d_Const(dbgi, index_tv);
3995 ir_node *in[1] = { cnst };
3996 irtype = get_array_element_type(type);
3997 sub_entity = get_array_element_entity(type);
3998 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
4001 sub_entity = get_compound_member(type, i);
4002 irtype = get_entity_type(sub_entity);
4003 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
4007 ir_initializer_t *sub_init
4008 = get_initializer_compound_value(initializer, i);
4010 create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
4017 panic("invalid IR_INITIALIZER found");
4020 static void create_dynamic_initializer(ir_initializer_t *initializer,
4021 dbg_info *dbgi, ir_entity *entity)
4023 ir_node *frame = get_local_frame(entity);
4024 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
4025 ir_type *type = get_entity_type(entity);
4027 create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
4030 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
4031 ir_entity *entity, type_t *type)
4033 ir_node *memory = get_store();
4034 ir_node *nomem = new_NoMem();
4035 ir_node *frame = get_irg_frame(current_ir_graph);
4036 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
4038 if (initializer->kind == INITIALIZER_VALUE) {
4039 initializer_value_t *initializer_value = &initializer->value;
4041 ir_node *value = expression_to_firm(initializer_value->value);
4042 type = skip_typeref(type);
4043 assign_value(dbgi, addr, type, value);
4047 if (!is_constant_initializer(initializer)) {
4048 ir_initializer_t *irinitializer
4049 = create_ir_initializer(initializer, type);
4051 create_dynamic_initializer(irinitializer, dbgi, entity);
4055 /* create the ir_initializer */
4056 ir_graph *const old_current_ir_graph = current_ir_graph;
4057 current_ir_graph = get_const_code_irg();
4059 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
4061 assert(current_ir_graph == get_const_code_irg());
4062 current_ir_graph = old_current_ir_graph;
4064 /* create a "template" entity which is copied to the entity on the stack */
4065 ident *const id = id_unique("initializer.%u");
4066 ir_type *const irtype = get_ir_type(type);
4067 ir_type *const global_type = get_glob_type();
4068 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
4069 set_entity_ld_ident(init_entity, id);
4071 set_entity_variability(init_entity, variability_initialized);
4072 set_entity_visibility(init_entity, visibility_local);
4073 set_entity_allocation(init_entity, allocation_static);
4075 set_entity_initializer(init_entity, irinitializer);
4077 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
4078 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
4080 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
4081 set_store(copyb_mem);
4084 static void create_initializer_local_variable_entity(entity_t *entity)
4086 assert(entity->kind == ENTITY_VARIABLE);
4087 initializer_t *initializer = entity->variable.initializer;
4088 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4089 ir_entity *irentity = entity->variable.v.entity;
4090 type_t *type = entity->declaration.type;
4092 type = get_aligned_type(type, entity->variable.alignment);
4093 create_local_initializer(initializer, dbgi, irentity, type);
4096 static void create_variable_initializer(entity_t *entity)
4098 assert(entity->kind == ENTITY_VARIABLE);
4099 initializer_t *initializer = entity->variable.initializer;
4100 if (initializer == NULL)
4103 declaration_kind_t declaration_kind
4104 = (declaration_kind_t) entity->declaration.kind;
4105 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
4106 create_initializer_local_variable_entity(entity);
4110 type_t *type = entity->declaration.type;
4111 type_qualifiers_t tq = get_type_qualifier(type, true);
4113 if (initializer->kind == INITIALIZER_VALUE) {
4114 initializer_value_t *initializer_value = &initializer->value;
4115 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4117 ir_node *value = expression_to_firm(initializer_value->value);
4119 type_t *type = initializer_value->value->base.type;
4120 ir_mode *mode = get_ir_mode_storage(type);
4121 value = create_conv(dbgi, value, mode);
4122 value = do_strict_conv(dbgi, value);
4124 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
4125 set_value(entity->variable.v.value_number, value);
4127 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4129 ir_entity *irentity = entity->variable.v.entity;
4131 if (tq & TYPE_QUALIFIER_CONST) {
4132 set_entity_variability(irentity, variability_constant);
4134 set_entity_variability(irentity, variability_initialized);
4136 set_atomic_ent_value(irentity, value);
4139 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
4140 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
4142 ir_entity *irentity = entity->variable.v.entity;
4143 ir_initializer_t *irinitializer
4144 = create_ir_initializer(initializer, type);
4146 if (tq & TYPE_QUALIFIER_CONST) {
4147 set_entity_variability(irentity, variability_constant);
4149 set_entity_variability(irentity, variability_initialized);
4151 set_entity_initializer(irentity, irinitializer);
4155 static void create_variable_length_array(entity_t *entity)
4157 assert(entity->kind == ENTITY_VARIABLE);
4158 assert(entity->variable.initializer == NULL);
4160 entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
4161 entity->variable.v.vla_base = NULL;
4163 /* TODO: record VLA somewhere so we create the free node when we leave
4167 static void allocate_variable_length_array(entity_t *entity)
4169 assert(entity->kind == ENTITY_VARIABLE);
4170 assert(entity->variable.initializer == NULL);
4171 assert(get_cur_block() != NULL);
4173 dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
4174 type_t *type = entity->declaration.type;
4175 ir_type *el_type = get_ir_type(type->array.element_type);
4177 /* make sure size_node is calculated */
4178 get_type_size(type);
4179 ir_node *elems = type->array.size_node;
4180 ir_node *mem = get_store();
4181 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
4183 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
4184 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
4187 assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
4188 entity->variable.v.vla_base = addr;
4192 * Creates a Firm local variable from a declaration.
4194 static void create_local_variable(entity_t *entity)
4196 assert(entity->kind == ENTITY_VARIABLE);
4197 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4199 bool needs_entity = entity->variable.address_taken;
4200 type_t *type = skip_typeref(entity->declaration.type);
4202 /* is it a variable length array? */
4203 if (is_type_array(type) && !type->array.size_constant) {
4204 create_variable_length_array(entity);
4206 } else if (is_type_array(type) || is_type_compound(type)) {
4207 needs_entity = true;
4208 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4209 needs_entity = true;
4213 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
4214 create_variable_entity(entity,
4215 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
4218 entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
4219 entity->variable.v.value_number = next_value_number_function;
4220 set_irg_loc_description(current_ir_graph, next_value_number_function,
4222 ++next_value_number_function;
4226 static void create_local_static_variable(entity_t *entity)
4228 assert(entity->kind == ENTITY_VARIABLE);
4229 assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
4231 type_t *type = skip_typeref(entity->declaration.type);
4232 type = get_aligned_type(type, entity->variable.alignment);
4234 ir_type *const var_type = entity->variable.thread_local ?
4235 get_tls_type() : get_glob_type();
4236 ir_type *const irtype = get_ir_type(type);
4237 dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
4239 size_t l = strlen(entity->base.symbol->string);
4240 char buf[l + sizeof(".%u")];
4241 snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
4242 ident *const id = id_unique(buf);
4244 ir_entity *const irentity = new_d_entity(var_type, id, irtype, dbgi);
4246 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4247 set_entity_volatility(irentity, volatility_is_volatile);
4250 entity->declaration.kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4251 entity->variable.v.entity = irentity;
4253 set_entity_ld_ident(irentity, id);
4254 set_entity_variability(irentity, variability_uninitialized);
4255 set_entity_visibility(irentity, visibility_local);
4256 set_entity_allocation(irentity, entity->variable.thread_local ?
4257 allocation_automatic : allocation_static);
4259 ir_graph *const old_current_ir_graph = current_ir_graph;
4260 current_ir_graph = get_const_code_irg();
4262 create_variable_initializer(entity);
4264 assert(current_ir_graph == get_const_code_irg());
4265 current_ir_graph = old_current_ir_graph;
4270 static void return_statement_to_firm(return_statement_t *statement)
4272 if (get_cur_block() == NULL)
4275 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4276 type_t *type = current_function_entity->declaration.type;
4277 ir_type *func_irtype = get_ir_type(type);
4282 if (get_method_n_ress(func_irtype) > 0) {
4283 ir_type *res_type = get_method_res_type(func_irtype, 0);
4285 if (statement->value != NULL) {
4286 ir_node *node = expression_to_firm(statement->value);
4287 if (!is_compound_type(res_type)) {
4288 type_t *type = statement->value->base.type;
4289 ir_mode *mode = get_ir_mode_storage(type);
4290 node = create_conv(dbgi, node, mode);
4291 node = do_strict_conv(dbgi, node);
4296 if (is_compound_type(res_type)) {
4299 mode = get_type_mode(res_type);
4301 in[0] = new_Unknown(mode);
4305 /* build return_value for its side effects */
4306 if (statement->value != NULL) {
4307 expression_to_firm(statement->value);
4312 ir_node *store = get_store();
4313 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4315 ir_node *end_block = get_irg_end_block(current_ir_graph);
4316 add_immBlock_pred(end_block, ret);
4318 set_cur_block(NULL);
4321 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4323 if (get_cur_block() == NULL)
4326 return expression_to_firm(statement->expression);
4329 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4331 entity_t *entity = compound->scope.entities;
4332 for ( ; entity != NULL; entity = entity->base.next) {
4333 if (!is_declaration(entity))
4336 create_local_declaration(entity);
4339 ir_node *result = NULL;
4340 statement_t *statement = compound->statements;
4341 for ( ; statement != NULL; statement = statement->base.next) {
4342 if (statement->base.next == NULL
4343 && statement->kind == STATEMENT_EXPRESSION) {
4344 result = expression_statement_to_firm(
4345 &statement->expression);
4348 statement_to_firm(statement);
4354 static void create_global_variable(entity_t *entity)
4356 assert(entity->kind == ENTITY_VARIABLE);
4359 switch ((storage_class_tag_t)entity->declaration.storage_class) {
4360 case STORAGE_CLASS_STATIC: vis = visibility_local; break;
4361 case STORAGE_CLASS_EXTERN: vis = visibility_external_allocated; break;
4362 case STORAGE_CLASS_NONE: vis = visibility_external_visible; break;
4364 default: panic("Invalid storage class for global variable");
4367 ir_type *var_type = entity->variable.thread_local ?
4368 get_tls_type() : get_glob_type();
4369 create_variable_entity(entity,
4370 DECLARATION_KIND_GLOBAL_VARIABLE, var_type);
4371 set_entity_visibility(entity->variable.v.entity, vis);
4374 static void create_local_declaration(entity_t *entity)
4376 assert(is_declaration(entity));
4378 /* construct type */
4379 (void) get_ir_type(entity->declaration.type);
4380 if (entity->base.symbol == NULL) {
4384 switch ((storage_class_tag_t) entity->declaration.storage_class) {
4385 case STORAGE_CLASS_STATIC:
4386 create_local_static_variable(entity);
4388 case STORAGE_CLASS_EXTERN:
4389 if (entity->kind == ENTITY_FUNCTION) {
4390 assert(entity->function.statement == NULL);
4391 get_function_entity(entity);
4393 create_global_variable(entity);
4394 create_variable_initializer(entity);
4397 case STORAGE_CLASS_NONE:
4398 case STORAGE_CLASS_AUTO:
4399 case STORAGE_CLASS_REGISTER:
4400 if (entity->kind == ENTITY_FUNCTION) {
4401 if (entity->function.statement != NULL) {
4402 get_function_entity(entity);
4403 entity->declaration.kind = DECLARATION_KIND_INNER_FUNCTION;
4404 enqueue_inner_function(entity);
4406 get_function_entity(entity);
4409 create_local_variable(entity);
4412 case STORAGE_CLASS_TYPEDEF:
4415 panic("invalid storage class found");
4418 static void initialize_local_declaration(entity_t *entity)
4420 if (entity->base.symbol == NULL)
4423 switch ((declaration_kind_t) entity->declaration.kind) {
4424 case DECLARATION_KIND_LOCAL_VARIABLE:
4425 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4426 create_variable_initializer(entity);
4429 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4430 allocate_variable_length_array(entity);
4433 case DECLARATION_KIND_COMPOUND_MEMBER:
4434 case DECLARATION_KIND_GLOBAL_VARIABLE:
4435 case DECLARATION_KIND_FUNCTION:
4436 case DECLARATION_KIND_INNER_FUNCTION:
4439 case DECLARATION_KIND_PARAMETER:
4440 case DECLARATION_KIND_PARAMETER_ENTITY:
4441 panic("can't initialize parameters");
4443 case DECLARATION_KIND_UNKNOWN:
4444 panic("can't initialize unknown declaration");
4446 panic("invalid declaration kind");
4449 static void declaration_statement_to_firm(declaration_statement_t *statement)
4451 entity_t *entity = statement->declarations_begin;
4455 entity_t *const last = statement->declarations_end;
4456 for ( ;; entity = entity->base.next) {
4457 if (is_declaration(entity)) {
4458 initialize_local_declaration(entity);
4459 } else if (entity->kind == ENTITY_TYPEDEF) {
4460 type_t *const type = skip_typeref(entity->typedefe.type);
4461 if (is_type_array(type) && type->array.is_vla)
4462 get_vla_size(&type->array);
4469 static void if_statement_to_firm(if_statement_t *statement)
4471 ir_node *cur_block = get_cur_block();
4473 ir_node *fallthrough_block = NULL;
4475 /* the true (blocks) */
4476 ir_node *true_block = NULL;
4477 if (statement->true_statement != NULL) {
4478 true_block = new_immBlock();
4479 set_cur_block(true_block);
4480 statement_to_firm(statement->true_statement);
4481 if (get_cur_block() != NULL) {
4482 ir_node *jmp = new_Jmp();
4483 if (fallthrough_block == NULL)
4484 fallthrough_block = new_immBlock();
4485 add_immBlock_pred(fallthrough_block, jmp);
4489 /* the false (blocks) */
4490 ir_node *false_block = NULL;
4491 if (statement->false_statement != NULL) {
4492 false_block = new_immBlock();
4493 set_cur_block(false_block);
4495 statement_to_firm(statement->false_statement);
4496 if (get_cur_block() != NULL) {
4497 ir_node *jmp = new_Jmp();
4498 if (fallthrough_block == NULL)
4499 fallthrough_block = new_immBlock();
4500 add_immBlock_pred(fallthrough_block, jmp);
4504 /* create the condition */
4505 if (cur_block != NULL) {
4506 if (true_block == NULL || false_block == NULL) {
4507 if (fallthrough_block == NULL)
4508 fallthrough_block = new_immBlock();
4509 if (true_block == NULL)
4510 true_block = fallthrough_block;
4511 if (false_block == NULL)
4512 false_block = fallthrough_block;
4515 set_cur_block(cur_block);
4516 create_condition_evaluation(statement->condition, true_block,
4520 mature_immBlock(true_block);
4521 if (false_block != fallthrough_block && false_block != NULL) {
4522 mature_immBlock(false_block);
4524 if (fallthrough_block != NULL) {
4525 mature_immBlock(fallthrough_block);
4528 set_cur_block(fallthrough_block);
4531 static void while_statement_to_firm(while_statement_t *statement)
4533 ir_node *jmp = NULL;
4534 if (get_cur_block() != NULL) {
4538 /* create the header block */
4539 ir_node *header_block = new_immBlock();
4541 add_immBlock_pred(header_block, jmp);
4545 ir_node *old_continue_label = continue_label;
4546 ir_node *old_break_label = break_label;
4547 continue_label = header_block;
4550 ir_node *body_block = new_immBlock();
4551 set_cur_block(body_block);
4552 statement_to_firm(statement->body);
4553 ir_node *false_block = break_label;
4555 assert(continue_label == header_block);
4556 continue_label = old_continue_label;
4557 break_label = old_break_label;
4559 if (get_cur_block() != NULL) {
4561 add_immBlock_pred(header_block, jmp);
4564 /* shortcut for while(true) */
4565 if (is_constant_expression(statement->condition)
4566 && fold_constant(statement->condition) != 0) {
4567 set_cur_block(header_block);
4568 ir_node *header_jmp = new_Jmp();
4569 add_immBlock_pred(body_block, header_jmp);
4571 keep_alive(body_block);
4572 keep_all_memory(body_block);
4573 set_cur_block(body_block);
4575 if (false_block == NULL) {
4576 false_block = new_immBlock();
4579 /* create the condition */
4580 set_cur_block(header_block);
4582 create_condition_evaluation(statement->condition, body_block,
4586 mature_immBlock(body_block);
4587 mature_immBlock(header_block);
4588 if (false_block != NULL) {
4589 mature_immBlock(false_block);
4592 set_cur_block(false_block);
4595 static void do_while_statement_to_firm(do_while_statement_t *statement)
4597 ir_node *jmp = NULL;
4598 if (get_cur_block() != NULL) {
4602 /* create the header block */
4603 ir_node *header_block = new_immBlock();
4606 ir_node *body_block = new_immBlock();
4608 add_immBlock_pred(body_block, jmp);
4611 ir_node *old_continue_label = continue_label;
4612 ir_node *old_break_label = break_label;
4613 continue_label = header_block;
4616 set_cur_block(body_block);
4617 statement_to_firm(statement->body);
4618 ir_node *false_block = break_label;
4620 assert(continue_label == header_block);
4621 continue_label = old_continue_label;
4622 break_label = old_break_label;
4624 if (get_cur_block() != NULL) {
4625 ir_node *body_jmp = new_Jmp();
4626 add_immBlock_pred(header_block, body_jmp);
4627 mature_immBlock(header_block);
4630 if (false_block == NULL) {
4631 false_block = new_immBlock();
4634 /* create the condition */
4635 set_cur_block(header_block);
4637 create_condition_evaluation(statement->condition, body_block, false_block);
4638 mature_immBlock(body_block);
4639 mature_immBlock(header_block);
4640 mature_immBlock(false_block);
4642 set_cur_block(false_block);
4645 static void for_statement_to_firm(for_statement_t *statement)
4647 ir_node *jmp = NULL;
4649 /* create declarations */
4650 entity_t *entity = statement->scope.entities;
4651 for ( ; entity != NULL; entity = entity->base.next) {
4652 if (!is_declaration(entity))
4655 create_local_declaration(entity);
4658 if (get_cur_block() != NULL) {
4659 entity = statement->scope.entities;
4660 for ( ; entity != NULL; entity = entity->base.next) {
4661 if (!is_declaration(entity))
4664 initialize_local_declaration(entity);
4667 if (statement->initialisation != NULL) {
4668 expression_to_firm(statement->initialisation);
4675 /* create the step block */
4676 ir_node *const step_block = new_immBlock();
4677 set_cur_block(step_block);
4678 if (statement->step != NULL) {
4679 expression_to_firm(statement->step);
4681 ir_node *const step_jmp = new_Jmp();
4683 /* create the header block */
4684 ir_node *const header_block = new_immBlock();
4685 set_cur_block(header_block);
4687 add_immBlock_pred(header_block, jmp);
4689 add_immBlock_pred(header_block, step_jmp);
4691 /* the false block */
4692 ir_node *const false_block = new_immBlock();
4695 ir_node *body_block;
4696 if (statement->body != NULL) {
4697 ir_node *const old_continue_label = continue_label;
4698 ir_node *const old_break_label = break_label;
4699 continue_label = step_block;
4700 break_label = false_block;
4702 body_block = new_immBlock();
4703 set_cur_block(body_block);
4704 statement_to_firm(statement->body);
4706 assert(continue_label == step_block);
4707 assert(break_label == false_block);
4708 continue_label = old_continue_label;
4709 break_label = old_break_label;
4711 if (get_cur_block() != NULL) {
4713 add_immBlock_pred(step_block, jmp);
4716 body_block = step_block;
4719 /* create the condition */
4720 set_cur_block(header_block);
4721 if (statement->condition != NULL) {
4722 create_condition_evaluation(statement->condition, body_block,
4725 keep_alive(header_block);
4726 keep_all_memory(header_block);
4728 add_immBlock_pred(body_block, jmp);
4731 mature_immBlock(body_block);
4732 mature_immBlock(false_block);
4733 mature_immBlock(step_block);
4734 mature_immBlock(header_block);
4735 mature_immBlock(false_block);
4737 set_cur_block(false_block);
4740 static void create_jump_statement(const statement_t *statement,
4741 ir_node *target_block)
4743 if (get_cur_block() == NULL)
4746 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4747 ir_node *jump = new_d_Jmp(dbgi);
4748 add_immBlock_pred(target_block, jump);
4750 set_cur_block(NULL);
4753 static ir_node *get_break_label(void)
4755 if (break_label == NULL) {
4756 break_label = new_immBlock();
4761 static void switch_statement_to_firm(switch_statement_t *statement)
4763 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4765 ir_node *expression = expression_to_firm(statement->expression);
4766 ir_node *cond = new_d_Cond(dbgi, expression);
4768 set_cur_block(NULL);
4770 ir_node *const old_switch_cond = current_switch_cond;
4771 ir_node *const old_break_label = break_label;
4772 const bool old_saw_default_label = saw_default_label;
4773 saw_default_label = false;
4774 current_switch_cond = cond;
4776 switch_statement_t *const old_switch = current_switch;
4777 current_switch = statement;
4779 /* determine a free number for the default label */
4780 unsigned long num_cases = 0;
4782 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4783 if (l->expression == NULL) {
4787 if (l->last_case >= l->first_case)
4788 num_cases += l->last_case - l->first_case + 1;
4789 if (l->last_case > def_nr)
4790 def_nr = l->last_case;
4793 if (def_nr == INT_MAX) {
4794 /* Bad: an overflow will occurr, we cannot be sure that the
4795 * maximum + 1 is a free number. Scan the values a second
4796 * time to find a free number.
4798 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
4800 memset(bits, 0, (num_cases + 7) >> 3);
4801 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4802 if (l->expression == NULL) {
4806 unsigned long start = l->first_case > 0 ? (unsigned long)l->first_case : 0;
4807 if (start < num_cases && l->last_case >= 0) {
4808 unsigned long end = (unsigned long)l->last_case < num_cases ?
4809 (unsigned long)l->last_case : num_cases - 1;
4810 for (unsigned long cns = start; cns <= end; ++cns) {
4811 bits[cns >> 3] |= (1 << (cns & 7));
4815 /* We look at the first num_cases constants:
4816 * Either they are densed, so we took the last (num_cases)
4817 * one, or they are non densed, so we will find one free
4821 for (i = 0; i < num_cases; ++i)
4822 if ((bits[i >> 3] & (1 << (i & 7))) == 0)
4830 statement->default_proj_nr = def_nr;
4832 if (statement->body != NULL) {
4833 statement_to_firm(statement->body);
4836 if (get_cur_block() != NULL) {
4837 ir_node *jmp = new_Jmp();
4838 add_immBlock_pred(get_break_label(), jmp);
4841 if (!saw_default_label) {
4842 set_cur_block(get_nodes_block(cond));
4843 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4844 statement->default_proj_nr);
4845 add_immBlock_pred(get_break_label(), proj);
4848 if (break_label != NULL) {
4849 mature_immBlock(break_label);
4851 set_cur_block(break_label);
4853 assert(current_switch_cond == cond);
4854 current_switch = old_switch;
4855 current_switch_cond = old_switch_cond;
4856 break_label = old_break_label;
4857 saw_default_label = old_saw_default_label;
4860 static void case_label_to_firm(const case_label_statement_t *statement)
4862 if (statement->is_empty_range)
4865 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4867 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4870 ir_node *block = new_immBlock();
4872 set_cur_block(get_nodes_block(current_switch_cond));
4873 if (statement->expression != NULL) {
4874 long pn = statement->first_case;
4875 long end_pn = statement->last_case;
4876 assert(pn <= end_pn);
4877 /* create jumps for all cases in the given range */
4879 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4880 add_immBlock_pred(block, proj);
4881 } while(pn++ < end_pn);
4883 saw_default_label = true;
4884 proj = new_d_defaultProj(dbgi, current_switch_cond,
4885 current_switch->default_proj_nr);
4887 add_immBlock_pred(block, proj);
4890 if (fallthrough != NULL) {
4891 add_immBlock_pred(block, fallthrough);
4893 mature_immBlock(block);
4894 set_cur_block(block);
4896 if (statement->statement != NULL) {
4897 statement_to_firm(statement->statement);
4901 static void label_to_firm(const label_statement_t *statement)
4903 ir_node *block = get_label_block(statement->label);
4905 if (get_cur_block() != NULL) {
4906 ir_node *jmp = new_Jmp();
4907 add_immBlock_pred(block, jmp);
4910 set_cur_block(block);
4912 keep_all_memory(block);
4914 if (statement->statement != NULL) {
4915 statement_to_firm(statement->statement);
4919 static void goto_to_firm(const goto_statement_t *statement)
4921 if (get_cur_block() == NULL)
4924 if (statement->expression) {
4925 ir_node *irn = expression_to_firm(statement->expression);
4926 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4927 ir_node *ijmp = new_d_IJmp(dbgi, irn);
4929 set_irn_link(ijmp, ijmp_list);
4932 ir_node *block = get_label_block(statement->label);
4933 ir_node *jmp = new_Jmp();
4934 add_immBlock_pred(block, jmp);
4936 set_cur_block(NULL);
4939 static void asm_statement_to_firm(const asm_statement_t *statement)
4941 bool needs_memory = false;
4943 if (statement->is_volatile) {
4944 needs_memory = true;
4947 size_t n_clobbers = 0;
4948 asm_clobber_t *clobber = statement->clobbers;
4949 for ( ; clobber != NULL; clobber = clobber->next) {
4950 const char *clobber_str = clobber->clobber.begin;
4952 if (!be_is_valid_clobber(clobber_str)) {
4953 errorf(&statement->base.source_position,
4954 "invalid clobber '%s' specified", clobber->clobber);
4958 if (strcmp(clobber_str, "memory") == 0) {
4959 needs_memory = true;
4963 ident *id = new_id_from_str(clobber_str);
4964 obstack_ptr_grow(&asm_obst, id);
4967 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4968 ident **clobbers = NULL;
4969 if (n_clobbers > 0) {
4970 clobbers = obstack_finish(&asm_obst);
4973 size_t n_inputs = 0;
4974 asm_argument_t *argument = statement->inputs;
4975 for ( ; argument != NULL; argument = argument->next)
4977 size_t n_outputs = 0;
4978 argument = statement->outputs;
4979 for ( ; argument != NULL; argument = argument->next)
4982 unsigned next_pos = 0;
4984 ir_node *ins[n_inputs + n_outputs + 1];
4987 ir_asm_constraint tmp_in_constraints[n_outputs];
4989 const expression_t *out_exprs[n_outputs];
4990 ir_node *out_addrs[n_outputs];
4991 size_t out_size = 0;
4993 argument = statement->outputs;
4994 for ( ; argument != NULL; argument = argument->next) {
4995 const char *constraints = argument->constraints.begin;
4996 asm_constraint_flags_t asm_flags
4997 = be_parse_asm_constraints(constraints);
4999 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5000 warningf(&statement->base.source_position,
5001 "some constraints in '%s' are not supported", constraints);
5003 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5004 errorf(&statement->base.source_position,
5005 "some constraints in '%s' are invalid", constraints);
5008 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
5009 errorf(&statement->base.source_position,
5010 "no write flag specified for output constraints '%s'",
5015 unsigned pos = next_pos++;
5016 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5017 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5018 expression_t *expr = argument->expression;
5019 ir_node *addr = expression_to_addr(expr);
5020 /* in+output, construct an artifical same_as constraint on the
5022 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
5024 ir_node *value = get_value_from_lvalue(expr, addr);
5026 snprintf(buf, sizeof(buf), "%u", pos);
5028 ir_asm_constraint constraint;
5029 constraint.pos = pos;
5030 constraint.constraint = new_id_from_str(buf);
5031 constraint.mode = get_ir_mode_storage(expr->base.type);
5032 tmp_in_constraints[in_size] = constraint;
5033 ins[in_size] = value;
5038 out_exprs[out_size] = expr;
5039 out_addrs[out_size] = addr;
5041 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5042 /* pure memory ops need no input (but we have to make sure we
5043 * attach to the memory) */
5044 assert(! (asm_flags &
5045 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5046 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5047 needs_memory = true;
5049 /* we need to attach the address to the inputs */
5050 expression_t *expr = argument->expression;
5052 ir_asm_constraint constraint;
5053 constraint.pos = pos;
5054 constraint.constraint = new_id_from_str(constraints);
5055 constraint.mode = NULL;
5056 tmp_in_constraints[in_size] = constraint;
5058 ins[in_size] = expression_to_addr(expr);
5062 errorf(&statement->base.source_position,
5063 "only modifiers but no place set in constraints '%s'",
5068 ir_asm_constraint constraint;
5069 constraint.pos = pos;
5070 constraint.constraint = new_id_from_str(constraints);
5071 constraint.mode = get_ir_mode_storage(argument->expression->base.type);
5073 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5075 assert(obstack_object_size(&asm_obst)
5076 == out_size * sizeof(ir_asm_constraint));
5077 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
5080 obstack_grow(&asm_obst, tmp_in_constraints,
5081 in_size * sizeof(tmp_in_constraints[0]));
5082 /* find and count input and output arguments */
5083 argument = statement->inputs;
5084 for ( ; argument != NULL; argument = argument->next) {
5085 const char *constraints = argument->constraints.begin;
5086 asm_constraint_flags_t asm_flags
5087 = be_parse_asm_constraints(constraints);
5089 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
5090 errorf(&statement->base.source_position,
5091 "some constraints in '%s' are not supported", constraints);
5094 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
5095 errorf(&statement->base.source_position,
5096 "some constraints in '%s' are invalid", constraints);
5099 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
5100 errorf(&statement->base.source_position,
5101 "write flag specified for input constraints '%s'",
5107 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
5108 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
5109 /* we can treat this as "normal" input */
5110 input = expression_to_firm(argument->expression);
5111 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
5112 /* pure memory ops need no input (but we have to make sure we
5113 * attach to the memory) */
5114 assert(! (asm_flags &
5115 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
5116 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
5117 needs_memory = true;
5118 input = expression_to_addr(argument->expression);
5120 errorf(&statement->base.source_position,
5121 "only modifiers but no place set in constraints '%s'",
5126 ir_asm_constraint constraint;
5127 constraint.pos = next_pos++;
5128 constraint.constraint = new_id_from_str(constraints);
5129 constraint.mode = get_irn_mode(input);
5131 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5132 ins[in_size++] = input;
5136 ir_asm_constraint constraint;
5137 constraint.pos = next_pos++;
5138 constraint.constraint = new_id_from_str("");
5139 constraint.mode = mode_M;
5141 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
5142 ins[in_size++] = get_store();
5145 assert(obstack_object_size(&asm_obst)
5146 == in_size * sizeof(ir_asm_constraint));
5147 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
5149 /* create asm node */
5150 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
5152 ident *asm_text = new_id_from_str(statement->asm_text.begin);
5154 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
5155 out_size, output_constraints,
5156 n_clobbers, clobbers, asm_text);
5158 if (statement->is_volatile) {
5159 set_irn_pinned(node, op_pin_state_pinned);
5161 set_irn_pinned(node, op_pin_state_floats);
5164 /* create output projs & connect them */
5166 ir_node *projm = new_Proj(node, mode_M, out_size+1);
5171 for (i = 0; i < out_size; ++i) {
5172 const expression_t *out_expr = out_exprs[i];
5174 ir_mode *mode = get_ir_mode_storage(out_expr->base.type);
5175 ir_node *proj = new_Proj(node, mode, pn);
5176 ir_node *addr = out_addrs[i];
5178 set_value_for_expression_addr(out_expr, proj, addr);
5182 static void ms_try_statement_to_firm(ms_try_statement_t *statement)
5184 statement_to_firm(statement->try_statement);
5185 warningf(&statement->base.source_position, "structured exception handling ignored");
5188 static void leave_statement_to_firm(leave_statement_t *statement)
5190 errorf(&statement->base.source_position, "__leave not supported yet");
5194 * Transform a statement.
5196 static void statement_to_firm(statement_t *statement)
5199 assert(!statement->base.transformed);
5200 statement->base.transformed = true;
5203 switch (statement->kind) {
5204 case STATEMENT_INVALID:
5205 panic("invalid statement found");
5206 case STATEMENT_EMPTY:
5209 case STATEMENT_COMPOUND:
5210 compound_statement_to_firm(&statement->compound);
5212 case STATEMENT_RETURN:
5213 return_statement_to_firm(&statement->returns);
5215 case STATEMENT_EXPRESSION:
5216 expression_statement_to_firm(&statement->expression);
5219 if_statement_to_firm(&statement->ifs);
5221 case STATEMENT_WHILE:
5222 while_statement_to_firm(&statement->whiles);
5224 case STATEMENT_DO_WHILE:
5225 do_while_statement_to_firm(&statement->do_while);
5227 case STATEMENT_DECLARATION:
5228 declaration_statement_to_firm(&statement->declaration);
5230 case STATEMENT_BREAK:
5231 create_jump_statement(statement, get_break_label());
5233 case STATEMENT_CONTINUE:
5234 create_jump_statement(statement, continue_label);
5236 case STATEMENT_SWITCH:
5237 switch_statement_to_firm(&statement->switchs);
5239 case STATEMENT_CASE_LABEL:
5240 case_label_to_firm(&statement->case_label);
5243 for_statement_to_firm(&statement->fors);
5245 case STATEMENT_LABEL:
5246 label_to_firm(&statement->label);
5248 case STATEMENT_GOTO:
5249 goto_to_firm(&statement->gotos);
5252 asm_statement_to_firm(&statement->asms);
5254 case STATEMENT_MS_TRY:
5255 ms_try_statement_to_firm(&statement->ms_try);
5257 case STATEMENT_LEAVE:
5258 leave_statement_to_firm(&statement->leave);
5261 panic("statement not implemented");
5264 static int count_local_variables(const entity_t *entity,
5265 const entity_t *const last)
5268 entity_t const *const end = last != NULL ? last->base.next : NULL;
5269 for (; entity != end; entity = entity->base.next) {
5273 if (entity->kind == ENTITY_VARIABLE) {
5274 type = skip_typeref(entity->declaration.type);
5275 address_taken = entity->variable.address_taken;
5276 } else if (entity->kind == ENTITY_PARAMETER) {
5277 type = skip_typeref(entity->declaration.type);
5278 address_taken = entity->parameter.address_taken;
5283 if (!address_taken && is_type_scalar(type))
5289 static void count_local_variables_in_stmt(statement_t *stmt, void *const env)
5291 int *const count = env;
5293 switch (stmt->kind) {
5294 case STATEMENT_DECLARATION: {
5295 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5296 *count += count_local_variables(decl_stmt->declarations_begin,
5297 decl_stmt->declarations_end);
5302 *count += count_local_variables(stmt->fors.scope.entities, NULL);
5310 static int get_function_n_local_vars(entity_t *entity)
5314 /* count parameters */
5315 count += count_local_variables(entity->function.parameters.entities, NULL);
5317 /* count local variables declared in body */
5318 walk_statements(entity->function.statement, count_local_variables_in_stmt,
5323 static void initialize_function_parameters(entity_t *entity)
5325 assert(entity->kind == ENTITY_FUNCTION);
5326 ir_graph *irg = current_ir_graph;
5327 ir_node *args = get_irg_args(irg);
5328 ir_node *start_block = get_irg_start_block(irg);
5329 ir_type *function_irtype = get_ir_type(entity->declaration.type);
5332 entity_t *parameter = entity->function.parameters.entities;
5333 for ( ; parameter != NULL; parameter = parameter->base.next, ++n) {
5334 if (parameter->kind != ENTITY_PARAMETER)
5337 assert(parameter->declaration.kind == DECLARATION_KIND_UNKNOWN);
5338 type_t *type = skip_typeref(parameter->declaration.type);
5340 bool needs_entity = parameter->parameter.address_taken;
5341 assert(!is_type_array(type));
5342 if (is_type_compound(type)) {
5343 needs_entity = true;
5347 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5348 ident *id = new_id_from_str(parameter->base.symbol->string);
5349 set_entity_ident(entity, id);
5351 parameter->declaration.kind
5352 = DECLARATION_KIND_PARAMETER_ENTITY;
5353 parameter->parameter.v.entity = entity;
5357 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5358 ir_mode *param_mode = get_type_mode(param_irtype);
5361 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5363 ir_mode *mode = get_ir_mode_storage(type);
5364 value = create_conv(NULL, value, mode);
5365 value = do_strict_conv(NULL, value);
5367 parameter->declaration.kind = DECLARATION_KIND_PARAMETER;
5368 parameter->parameter.v.value_number = next_value_number_function;
5369 set_irg_loc_description(current_ir_graph, next_value_number_function,
5371 ++next_value_number_function;
5373 set_value(parameter->parameter.v.value_number, value);
5378 * Handle additional decl modifiers for IR-graphs
5380 * @param irg the IR-graph
5381 * @param dec_modifiers additional modifiers
5383 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5385 if (decl_modifiers & DM_RETURNS_TWICE) {
5386 /* TRUE if the declaration includes __attribute__((returns_twice)) */
5387 set_irg_additional_property(irg, mtp_property_returns_twice);
5389 if (decl_modifiers & DM_NORETURN) {
5390 /* TRUE if the declaration includes the Microsoft
5391 __declspec(noreturn) specifier. */
5392 set_irg_additional_property(irg, mtp_property_noreturn);
5394 if (decl_modifiers & DM_NOTHROW) {
5395 /* TRUE if the declaration includes the Microsoft
5396 __declspec(nothrow) specifier. */
5397 set_irg_additional_property(irg, mtp_property_nothrow);
5399 if (decl_modifiers & DM_NAKED) {
5400 /* TRUE if the declaration includes the Microsoft
5401 __declspec(naked) specifier. */
5402 set_irg_additional_property(irg, mtp_property_naked);
5404 if (decl_modifiers & DM_FORCEINLINE) {
5405 /* TRUE if the declaration includes the
5406 Microsoft __forceinline specifier. */
5407 set_irg_inline_property(irg, irg_inline_forced);
5409 if (decl_modifiers & DM_NOINLINE) {
5410 /* TRUE if the declaration includes the Microsoft
5411 __declspec(noinline) specifier. */
5412 set_irg_inline_property(irg, irg_inline_forbidden);
5416 static void add_function_pointer(ir_type *segment, ir_entity *method,
5417 const char *unique_template)
5419 ir_type *method_type = get_entity_type(method);
5420 ident *id = id_unique(unique_template);
5421 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5423 ident *ide = id_unique(unique_template);
5424 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5425 ir_graph *irg = get_const_code_irg();
5426 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5429 set_entity_compiler_generated(ptr, 1);
5430 set_entity_variability(ptr, variability_constant);
5431 set_atomic_ent_value(ptr, val);
5435 * Generate possible IJmp branches to a given label block.
5437 static void gen_ijmp_branches(ir_node *block)
5440 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5441 add_immBlock_pred(block, ijmp);
5446 * Create code for a function.
5448 static void create_function(entity_t *entity)
5450 assert(entity->kind == ENTITY_FUNCTION);
5451 ir_entity *function_entity = get_function_entity(entity);
5453 if (entity->function.statement == NULL)
5456 if (entity->declaration.modifiers & DM_CONSTRUCTOR) {
5457 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5458 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5460 if (entity->declaration.modifiers & DM_DESTRUCTOR) {
5461 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5462 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5465 current_function_entity = entity;
5466 current_function_name = NULL;
5467 current_funcsig = NULL;
5469 assert(all_labels == NULL);
5470 all_labels = NEW_ARR_F(label_t *, 0);
5473 int n_local_vars = get_function_n_local_vars(entity);
5474 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5476 ir_graph *old_current_function = current_function;
5477 current_function = irg;
5479 set_irg_fp_model(irg, firm_opt.fp_model);
5480 tarval_enable_fp_ops(1);
5481 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5483 ir_node *first_block = get_cur_block();
5485 /* set inline flags */
5486 if (entity->function.is_inline)
5487 set_irg_inline_property(irg, irg_inline_recomended);
5488 handle_decl_modifier_irg(irg, entity->declaration.modifiers);
5490 next_value_number_function = 0;
5491 initialize_function_parameters(entity);
5493 statement_to_firm(entity->function.statement);
5495 ir_node *end_block = get_irg_end_block(irg);
5497 /* do we have a return statement yet? */
5498 if (get_cur_block() != NULL) {
5499 type_t *type = skip_typeref(entity->declaration.type);
5500 assert(is_type_function(type));
5501 const function_type_t *func_type = &type->function;
5502 const type_t *return_type
5503 = skip_typeref(func_type->return_type);
5506 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5507 ret = new_Return(get_store(), 0, NULL);
5510 if (is_type_scalar(return_type)) {
5511 mode = get_ir_mode_storage(func_type->return_type);
5517 /* ยง5.1.2.2.3 main implicitly returns 0 */
5518 if (is_main(entity)) {
5519 in[0] = new_Const(get_mode_null(mode));
5521 in[0] = new_Unknown(mode);
5523 ret = new_Return(get_store(), 1, in);
5525 add_immBlock_pred(end_block, ret);
5528 bool has_computed_gotos = false;
5529 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5530 label_t *label = all_labels[i];
5531 if (label->address_taken) {
5532 gen_ijmp_branches(label->block);
5533 has_computed_gotos = true;
5535 mature_immBlock(label->block);
5537 if (has_computed_gotos) {
5538 /* if we have computed goto's in the function, we cannot inline it */
5539 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5540 warningf(&entity->base.source_position,
5541 "function '%Y' can never be inlined because it contains a computed goto",
5542 entity->base.symbol);
5544 set_irg_inline_property(irg, irg_inline_forbidden);
5547 DEL_ARR_F(all_labels);
5550 mature_immBlock(first_block);
5551 mature_immBlock(end_block);
5553 irg_finalize_cons(irg);
5555 /* finalize the frame type */
5556 ir_type *frame_type = get_irg_frame_type(irg);
5557 int n = get_compound_n_members(frame_type);
5560 for (int i = 0; i < n; ++i) {
5561 ir_entity *entity = get_compound_member(frame_type, i);
5562 ir_type *entity_type = get_entity_type(entity);
5564 int align = get_type_alignment_bytes(entity_type);
5565 if (align > align_all)
5569 misalign = offset % align;
5571 offset += align - misalign;
5575 set_entity_offset(entity, offset);
5576 offset += get_type_size_bytes(entity_type);
5578 set_type_size_bytes(frame_type, offset);
5579 set_type_alignment_bytes(frame_type, align_all);
5582 current_function = old_current_function;
5584 /* create inner functions */
5586 for (inner = next_inner_function(); inner != NULL;
5587 inner = next_inner_function()) {
5588 create_function(inner);
5592 static void scope_to_firm(scope_t *scope)
5594 /* first pass: create declarations */
5595 entity_t *entity = scope->entities;
5596 for ( ; entity != NULL; entity = entity->base.next) {
5597 if (entity->base.symbol == NULL)
5600 if (entity->kind == ENTITY_FUNCTION) {
5601 get_function_entity(entity);
5602 } else if (entity->kind == ENTITY_VARIABLE) {
5603 create_global_variable(entity);
5607 /* second pass: create code/initializers */
5608 entity = scope->entities;
5609 for ( ; entity != NULL; entity = entity->base.next) {
5610 if (entity->base.symbol == NULL)
5613 if (entity->kind == ENTITY_FUNCTION) {
5614 create_function(entity);
5615 } else if (entity->kind == ENTITY_VARIABLE) {
5616 assert(entity->declaration.kind
5617 == DECLARATION_KIND_GLOBAL_VARIABLE);
5618 current_ir_graph = get_const_code_irg();
5619 create_variable_initializer(entity);
5624 void init_ast2firm(void)
5626 obstack_init(&asm_obst);
5627 init_atomic_modes();
5629 /* OS option must be set to the backend */
5630 switch (firm_opt.os_support) {
5631 case OS_SUPPORT_MINGW:
5632 create_ld_ident = create_name_win32;
5634 case OS_SUPPORT_LINUX:
5635 create_ld_ident = create_name_linux_elf;
5637 case OS_SUPPORT_MACHO:
5638 create_ld_ident = create_name_macho;
5641 panic("unexpected OS support mode");
5644 /* create idents for all known runtime functions */
5645 for (size_t i = 0; i < lengthof(rts_data); ++i) {
5646 rts_idents[i] = new_id_from_str(rts_data[i].name);
5649 entitymap_init(&entitymap);
5652 static void init_ir_types(void)
5654 static int ir_types_initialized = 0;
5655 if (ir_types_initialized)
5657 ir_types_initialized = 1;
5659 ir_type_int = get_ir_type(type_int);
5660 ir_type_const_char = get_ir_type(type_const_char);
5661 ir_type_wchar_t = get_ir_type(type_wchar_t);
5662 ir_type_void = get_ir_type(type_void);
5664 const backend_params *be_params = be_get_backend_param();
5665 mode_float_arithmetic = be_params->mode_float_arithmetic;
5668 void exit_ast2firm(void)
5670 entitymap_destroy(&entitymap);
5671 obstack_free(&asm_obst, NULL);
5674 static void global_asm_to_firm(statement_t *s)
5676 for (; s != NULL; s = s->base.next) {
5677 assert(s->kind == STATEMENT_ASM);
5679 char const *const text = s->asms.asm_text.begin;
5680 size_t size = s->asms.asm_text.size;
5682 /* skip the last \0 */
5683 if (text[size - 1] == '\0')
5686 ident *const id = new_id_from_chars(text, size);
5691 void translation_unit_to_firm(translation_unit_t *unit)
5693 /* just to be sure */
5694 continue_label = NULL;
5696 current_switch_cond = NULL;
5697 current_translation_unit = unit;
5700 inner_functions = NEW_ARR_F(entity_t *, 0);
5702 scope_to_firm(&unit->scope);
5703 global_asm_to_firm(unit->global_asm);
5705 DEL_ARR_F(inner_functions);
5706 inner_functions = NULL;
5708 current_ir_graph = NULL;
5709 current_translation_unit = NULL;