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
26 #include <libfirm/firm.h>
27 #include <libfirm/adt/obst.h>
31 #include "adt/error.h"
32 #include "adt/array.h"
38 #include "diagnostic.h"
39 #include "lang_features.h"
42 #include "entitymap_t.h"
43 #include "driver/firm_opt.h"
44 #include "driver/firm_cmdline.h"
46 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
48 /* some idents needed for name mangling */
49 static ident *id_underscore;
52 static ir_type *ir_type_const_char;
53 static ir_type *ir_type_wchar_t;
54 static ir_type *ir_type_void;
55 static ir_type *ir_type_int;
57 static type_t *type_const_char;
59 static int next_value_number_function;
60 static ir_node *continue_label;
61 static ir_node *break_label;
62 static ir_node *current_switch_cond;
63 static bool saw_default_label;
64 static ir_node **imature_blocks;
65 static bool constant_folding;
67 static const declaration_t *current_function_decl;
68 static ir_node *current_function_name;
69 static ir_node *current_funcsig;
71 static entitymap_t entitymap;
73 static struct obstack asm_obst;
75 typedef enum declaration_kind_t {
76 DECLARATION_KIND_UNKNOWN,
77 DECLARATION_KIND_FUNCTION,
78 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
79 DECLARATION_KIND_GLOBAL_VARIABLE,
80 DECLARATION_KIND_LOCAL_VARIABLE,
81 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
82 DECLARATION_KIND_COMPOUND_MEMBER,
83 DECLARATION_KIND_LABEL_BLOCK,
84 DECLARATION_KIND_ENUM_ENTRY,
85 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
86 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
90 static ir_type *get_ir_type(type_t *type);
91 static ir_type *get_ir_type_incomplete(type_t *type);
92 static int count_decls_in_stmts(const statement_t *stmt);
94 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
96 const declaration_t *declaration = get_irg_loc_description(irg, pos);
98 warningf(&declaration->source_position,
99 "variable '%#T' might be used uninitialized",
100 declaration->type, declaration->symbol);
101 return new_r_Unknown(irg, mode);
104 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
106 const source_position_t *pos = (const source_position_t*) dbg;
109 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
113 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
115 const source_position_t *pos = (const source_position_t*) dbg;
120 return pos->input_name;
123 static dbg_info *get_dbg_info(const source_position_t *pos)
125 return (dbg_info*) pos;
128 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
130 static ir_mode *mode_int, *mode_uint;
132 static ir_node *_expression_to_firm(const expression_t *expression);
133 static ir_node *expression_to_firm(const expression_t *expression);
134 static inline ir_mode *get_ir_mode(type_t *type);
135 static void create_local_declaration(declaration_t *declaration);
137 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
139 unsigned flags = get_atomic_type_flags(kind);
140 unsigned size = get_atomic_type_size(kind);
141 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
142 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
145 unsigned bit_size = size * 8;
146 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
147 ir_mode_arithmetic arithmetic;
148 unsigned modulo_shift;
150 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
151 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
152 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
153 sort = irms_int_number;
154 arithmetic = irma_twos_complement;
155 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
157 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
158 snprintf(name, sizeof(name), "f%d", bit_size);
159 sort = irms_float_number;
160 arithmetic = irma_ieee754;
163 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
171 * Initialises the atomic modes depending on the machine size.
173 static void init_atomic_modes(void)
175 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
176 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
178 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
179 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
181 /* there's no real void type in firm */
182 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
184 /* initialize pointer modes */
186 ir_mode_sort sort = irms_reference;
187 unsigned bit_size = machine_size;
189 ir_mode_arithmetic arithmetic = irma_twos_complement;
190 unsigned modulo_shift
191 = bit_size < machine_size ? machine_size : bit_size;
193 snprintf(name, sizeof(name), "p%d", machine_size);
194 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
197 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
198 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
200 /* Hmm, pointers should be machine size */
201 set_modeP_data(ptr_mode);
202 set_modeP_code(ptr_mode);
205 static unsigned get_compound_type_size(compound_type_t *type)
207 ir_type *irtype = get_ir_type((type_t*) type);
208 return get_type_size_bytes(irtype);
211 static unsigned get_array_type_size(array_type_t *type)
213 assert(!type->is_vla);
214 ir_type *irtype = get_ir_type((type_t*) type);
215 return get_type_size_bytes(irtype);
219 static unsigned get_type_size_const(type_t *type)
221 type = skip_typeref(type);
225 panic("error type occurred");
227 return get_atomic_type_size(type->atomic.akind);
229 return 2 * get_atomic_type_size(type->complex.akind);
231 return get_atomic_type_size(type->imaginary.akind);
233 return get_mode_size_bytes(mode_int);
234 case TYPE_COMPOUND_UNION:
235 case TYPE_COMPOUND_STRUCT:
236 return get_compound_type_size(&type->compound);
238 /* just a pointer to the function */
239 return get_mode_size_bytes(mode_P_code);
241 return get_mode_size_bytes(mode_P_data);
243 return get_array_type_size(&type->array);
245 return get_type_size_const(type->builtin.real_type);
247 panic("type size of bitfield request");
253 panic("Trying to determine size of invalid type");
256 static ir_node *get_type_size(type_t *type)
258 type = skip_typeref(type);
260 if (is_type_array(type) && type->array.is_vla) {
261 ir_node *size_node = type->array.size_node;
262 if (size_node == NULL) {
263 size_node = expression_to_firm(type->array.size_expression);
264 assert(!is_Const(size_node));
265 type->array.size_node = size_node;
268 ir_node *elem_size = get_type_size(type->array.element_type);
269 ir_mode *mode = get_irn_mode(size_node);
270 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
274 ir_mode *mode = get_ir_mode(type_size_t);
276 sym.type_p = get_ir_type(type);
277 return new_SymConst(mode, sym, symconst_type_size);
280 static unsigned count_parameters(const function_type_t *function_type)
284 function_parameter_t *parameter = function_type->parameters;
285 for ( ; parameter != NULL; parameter = parameter->next) {
293 * Creates a Firm type for an atomic type
295 static ir_type *create_atomic_type(const atomic_type_t *type)
297 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
298 atomic_type_kind_t kind = type->akind;
299 ir_mode *mode = _atomic_modes[kind];
300 ident *id = get_mode_ident(mode);
301 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
303 set_type_alignment_bytes(irtype, type->base.alignment);
309 * Creates a Firm type for a complex type
311 static ir_type *create_complex_type(const complex_type_t *type)
313 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
314 atomic_type_kind_t kind = type->akind;
315 ir_mode *mode = _atomic_modes[kind];
316 ident *id = get_mode_ident(mode);
321 /* FIXME: finish the array */
326 * Creates a Firm type for an imaginary type
328 static ir_type *create_imaginary_type(const imaginary_type_t *type)
330 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
331 atomic_type_kind_t kind = type->akind;
332 ir_mode *mode = _atomic_modes[kind];
333 ident *id = get_mode_ident(mode);
334 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
336 set_type_alignment_bytes(irtype, type->base.alignment);
341 static ir_type *create_method_type(const function_type_t *function_type)
343 type_t *return_type = function_type->return_type;
345 ident *id = id_unique("functiontype.%u");
346 int n_parameters = count_parameters(function_type);
347 int n_results = return_type == type_void ? 0 : 1;
348 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
349 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
351 if (return_type != type_void) {
352 ir_type *restype = get_ir_type(return_type);
353 set_method_res_type(irtype, 0, restype);
356 function_parameter_t *parameter = function_type->parameters;
358 for ( ; parameter != NULL; parameter = parameter->next) {
359 ir_type *p_irtype = get_ir_type(parameter->type);
360 set_method_param_type(irtype, n, p_irtype);
364 if (function_type->variadic || function_type->unspecified_parameters) {
365 set_method_variadicity(irtype, variadicity_variadic);
371 static ir_type *create_pointer_type(pointer_type_t *type)
373 type_t *points_to = type->points_to;
374 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
375 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
376 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
377 ir_points_to, mode_P_data, dbgi);
382 static ir_type *create_array_type(array_type_t *type)
384 type_t *element_type = type->element_type;
385 ir_type *ir_element_type = get_ir_type(element_type);
387 ident *id = id_unique("array.%u");
388 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
389 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
391 const int align = get_type_alignment_bytes(ir_element_type);
392 set_type_alignment_bytes(ir_type, align);
394 if (type->size_constant) {
395 int n_elements = type->size;
397 set_array_bounds_int(ir_type, 0, 0, n_elements);
399 size_t elemsize = get_type_size_bytes(ir_element_type);
400 if (elemsize % align > 0) {
401 elemsize += align - (elemsize % align);
403 set_type_size_bytes(ir_type, n_elements * elemsize);
405 set_array_lower_bound_int(ir_type, 0, 0);
407 set_type_state(ir_type, layout_fixed);
413 * Return the signed integer type of size bits.
415 * @param size the size
417 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
420 static ir_mode *s_modes[64 + 1] = {NULL, };
424 if (size <= 0 || size > 64)
427 mode = s_modes[size];
431 snprintf(name, sizeof(name), "bf_I%u", size);
432 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
433 size <= 32 ? 32 : size );
434 s_modes[size] = mode;
438 snprintf(name, sizeof(name), "I%u", size);
439 ident *id = new_id_from_str(name);
440 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
441 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
442 set_primitive_base_type(res, base_tp);
448 * Return the unsigned integer type of size bits.
450 * @param size the size
452 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
455 static ir_mode *u_modes[64 + 1] = {NULL, };
459 if (size <= 0 || size > 64)
462 mode = u_modes[size];
466 snprintf(name, sizeof(name), "bf_U%u", size);
467 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
468 size <= 32 ? 32 : size );
469 u_modes[size] = mode;
474 snprintf(name, sizeof(name), "U%u", size);
475 ident *id = new_id_from_str(name);
476 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
477 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
478 set_primitive_base_type(res, base_tp);
483 static ir_type *create_bitfield_type(bitfield_type_t *const type)
485 type_t *base = skip_typeref(type->base_type);
486 assert(base->kind == TYPE_ATOMIC);
487 ir_type *irbase = get_ir_type(base);
489 unsigned size = fold_constant(type->size);
491 assert(!is_type_float(base));
492 if (is_type_signed(base)) {
493 return get_signed_int_type_for_bit_size(irbase, size);
495 return get_unsigned_int_type_for_bit_size(irbase, size);
499 #define INVALID_TYPE ((ir_type_ptr)-1)
502 COMPOUND_IS_STRUCT = false,
503 COMPOUND_IS_UNION = true
507 * Construct firm type from ast struct type.
509 * As anonymous inner structs get flattened to a single firm type, we might get
510 * irtype, outer_offset and out_align passed (they represent the position of
511 * the anonymous inner struct inside the resulting firm struct)
513 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
514 size_t *outer_offset, size_t *outer_align,
515 bool incomplete, bool is_union)
517 declaration_t *declaration = type->declaration;
518 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
520 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
521 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
523 return declaration->v.irtype;
525 size_t align_all = 1;
527 size_t bit_offset = 0;
530 if (irtype == NULL) {
531 symbol_t *symbol = declaration->symbol;
533 if (symbol != NULL) {
534 id = new_id_from_str(symbol->string);
537 id = id_unique("__anonymous_union.%u");
539 id = id_unique("__anonymous_struct.%u");
542 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
545 irtype = new_d_type_union(id, dbgi);
547 irtype = new_d_type_struct(id, dbgi);
550 declaration->declaration_kind
551 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
552 declaration->v.irtype = irtype;
553 //type->base.firm_type = irtype;
555 offset = *outer_offset;
556 align_all = *outer_align;
562 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
564 declaration_t *entry = declaration->scope.declarations;
565 for( ; entry != NULL; entry = entry->next) {
566 if (entry->namespc != NAMESPACE_NORMAL)
569 size_t prev_offset = offset;
571 symbol_t *symbol = entry->symbol;
572 type_t *entry_type = skip_typeref(entry->type);
573 dbg_info *dbgi = get_dbg_info(&entry->source_position);
576 if (symbol != NULL) {
577 ident = new_id_from_str(symbol->string);
579 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
580 create_compound_type(&entry_type->compound, irtype, &offset,
581 &align_all, false, COMPOUND_IS_STRUCT);
582 goto finished_member;
583 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
584 create_compound_type(&entry_type->compound, irtype, &offset,
585 &align_all, false, COMPOUND_IS_UNION);
586 goto finished_member;
588 assert(entry_type->kind == TYPE_BITFIELD);
590 ident = id_unique("anon.%u");
593 ir_type *base_irtype;
594 if (entry_type->kind == TYPE_BITFIELD) {
595 base_irtype = get_ir_type(entry_type->bitfield.base_type);
597 base_irtype = get_ir_type(entry_type);
600 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
601 size_t misalign = offset % entry_alignment;
603 ir_type *entry_irtype = get_ir_type(entry_type);
604 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
607 size_t bits_remainder;
608 if (entry_type->kind == TYPE_BITFIELD) {
609 size_t size_bits = fold_constant(entry_type->bitfield.size);
610 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
612 if (size_bits > rest_size_bits) {
613 /* start a new bucket */
614 offset += entry_alignment - misalign;
620 /* put into current bucket */
621 base = offset - misalign;
622 bits_remainder = misalign * 8 + bit_offset;
625 offset += size_bits / 8;
626 bit_offset = bit_offset + (size_bits % 8);
628 size_t entry_size = get_type_size_bytes(base_irtype);
629 if (misalign > 0 || bit_offset > 0)
630 offset += entry_alignment - misalign;
634 offset += entry_size;
638 if (entry_alignment > align_all) {
639 if (entry_alignment % align_all != 0) {
640 panic("uneven alignments not supported yet");
642 align_all = entry_alignment;
645 set_entity_offset(entity, base);
646 set_entity_offset_bits_remainder(entity,
647 (unsigned char) bits_remainder);
648 //add_struct_member(irtype, entity);
649 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
650 assert(entry->v.entity == NULL);
651 entry->v.entity = entity;
655 size_t entry_size = offset - prev_offset;
656 if (entry_size > size) {
668 size_t misalign = offset % align_all;
669 if (misalign > 0 || bit_offset > 0) {
670 size += align_all - misalign;
673 if (outer_offset != NULL) {
675 *outer_offset = offset;
677 *outer_offset += size;
680 if (align_all > *outer_align) {
681 if (align_all % *outer_align != 0) {
682 panic("uneven alignments not supported yet");
684 *outer_align = align_all;
687 set_type_alignment_bytes(irtype, align_all);
688 set_type_size_bytes(irtype, size);
689 set_type_state(irtype, layout_fixed);
695 static ir_type *create_enum_type(enum_type_t *const type)
697 type->base.firm_type = ir_type_int;
699 ir_mode *const mode = mode_int;
700 tarval *const one = get_mode_one(mode);
701 tarval * tv_next = get_tarval_null(mode);
703 bool constant_folding_old = constant_folding;
704 constant_folding = true;
706 declaration_t *declaration = type->declaration->next;
707 for (; declaration != NULL; declaration = declaration->next) {
708 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
711 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
713 expression_t *const init = declaration->init.enum_value;
715 ir_node *const cnst = expression_to_firm(init);
716 if (!is_Const(cnst)) {
717 panic("couldn't fold constant");
719 tv_next = get_Const_tarval(cnst);
721 declaration->v.enum_val = tv_next;
722 tv_next = tarval_add(tv_next, one);
725 constant_folding = constant_folding_old;
730 static ir_type *get_ir_type_incomplete(type_t *type)
732 assert(type != NULL);
733 type = skip_typeref(type);
735 if (type->base.firm_type != NULL) {
736 assert(type->base.firm_type != INVALID_TYPE);
737 return type->base.firm_type;
740 switch (type->kind) {
741 case TYPE_COMPOUND_STRUCT:
742 return create_compound_type(&type->compound, NULL, NULL, NULL,
743 true, COMPOUND_IS_STRUCT);
745 case TYPE_COMPOUND_UNION:
746 return create_compound_type(&type->compound, NULL, NULL, NULL,
747 true, COMPOUND_IS_UNION);
749 return get_ir_type(type);
753 static ir_type *get_ir_type(type_t *type)
755 assert(type != NULL);
757 type = skip_typeref(type);
759 if (type->base.firm_type != NULL) {
760 assert(type->base.firm_type != INVALID_TYPE);
761 return type->base.firm_type;
764 ir_type *firm_type = NULL;
765 switch (type->kind) {
767 panic("error type occurred");
769 firm_type = create_atomic_type(&type->atomic);
772 firm_type = create_complex_type(&type->complex);
775 firm_type = create_imaginary_type(&type->imaginary);
778 firm_type = create_method_type(&type->function);
781 firm_type = create_pointer_type(&type->pointer);
784 firm_type = create_array_type(&type->array);
786 case TYPE_COMPOUND_STRUCT:
787 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
788 false, COMPOUND_IS_STRUCT);
790 case TYPE_COMPOUND_UNION:
791 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
792 false, COMPOUND_IS_UNION);
795 firm_type = create_enum_type(&type->enumt);
798 firm_type = get_ir_type(type->builtin.real_type);
801 firm_type = create_bitfield_type(&type->bitfield);
809 if (firm_type == NULL)
810 panic("unknown type found");
812 type->base.firm_type = firm_type;
816 static inline ir_mode *get_ir_mode(type_t *type)
818 ir_type *irtype = get_ir_type(type);
820 /* firm doesn't report a mode for arrays somehow... */
821 if (is_Array_type(irtype)) {
825 ir_mode *mode = get_type_mode(irtype);
826 assert(mode != NULL);
830 /** Names of the runtime functions. */
831 static const struct {
832 int id; /**< the rts id */
833 int n_res; /**< number of return values */
834 const char *name; /**< the name of the rts function */
835 int n_params; /**< number of parameters */
836 unsigned flags; /**< language flags */
838 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
839 { rts_abort, 0, "abort", 0, _C89 },
840 { rts_alloca, 1, "alloca", 1, _ALL },
841 { rts_abs, 1, "abs", 1, _C89 },
842 { rts_labs, 1, "labs", 1, _C89 },
843 { rts_llabs, 1, "llabs", 1, _C99 },
844 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
846 { rts_fabs, 1, "fabs", 1, _C89 },
847 { rts_sqrt, 1, "sqrt", 1, _C89 },
848 { rts_cbrt, 1, "cbrt", 1, _C99 },
849 { rts_exp, 1, "exp", 1, _C89 },
850 { rts_exp2, 1, "exp2", 1, _C89 },
851 { rts_exp10, 1, "exp10", 1, _GNUC },
852 { rts_log, 1, "log", 1, _C89 },
853 { rts_log2, 1, "log2", 1, _C89 },
854 { rts_log10, 1, "log10", 1, _C89 },
855 { rts_pow, 1, "pow", 2, _C89 },
856 { rts_sin, 1, "sin", 1, _C89 },
857 { rts_cos, 1, "cos", 1, _C89 },
858 { rts_tan, 1, "tan", 1, _C89 },
859 { rts_asin, 1, "asin", 1, _C89 },
860 { rts_acos, 1, "acos", 1, _C89 },
861 { rts_atan, 1, "atan", 1, _C89 },
862 { rts_sinh, 1, "sinh", 1, _C89 },
863 { rts_cosh, 1, "cosh", 1, _C89 },
864 { rts_tanh, 1, "tanh", 1, _C89 },
866 { rts_fabsf, 1, "fabsf", 1, _C99 },
867 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
868 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
869 { rts_expf, 1, "expf", 1, _C99 },
870 { rts_exp2f, 1, "exp2f", 1, _C99 },
871 { rts_exp10f, 1, "exp10f", 1, _GNUC },
872 { rts_logf, 1, "logf", 1, _C99 },
873 { rts_log2f, 1, "log2f", 1, _C99 },
874 { rts_log10f, 1, "log10f", 1, _C99 },
875 { rts_powf, 1, "powf", 2, _C99 },
876 { rts_sinf, 1, "sinf", 1, _C99 },
877 { rts_cosf, 1, "cosf", 1, _C99 },
878 { rts_tanf, 1, "tanf", 1, _C99 },
879 { rts_asinf, 1, "asinf", 1, _C99 },
880 { rts_acosf, 1, "acosf", 1, _C99 },
881 { rts_atanf, 1, "atanf", 1, _C99 },
882 { rts_sinhf, 1, "sinhf", 1, _C99 },
883 { rts_coshf, 1, "coshf", 1, _C99 },
884 { rts_tanhf, 1, "tanhf", 1, _C99 },
886 { rts_fabsl, 1, "fabsl", 1, _C99 },
887 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
888 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
889 { rts_expl, 1, "expl", 1, _C99 },
890 { rts_exp2l, 1, "exp2l", 1, _C99 },
891 { rts_exp10l, 1, "exp10l", 1, _GNUC },
892 { rts_logl, 1, "logl", 1, _C99 },
893 { rts_log2l, 1, "log2l", 1, _C99 },
894 { rts_log10l, 1, "log10l", 1, _C99 },
895 { rts_powl, 1, "powl", 2, _C99 },
896 { rts_sinl, 1, "sinl", 1, _C99 },
897 { rts_cosl, 1, "cosl", 1, _C99 },
898 { rts_tanl, 1, "tanl", 1, _C99 },
899 { rts_asinl, 1, "asinl", 1, _C99 },
900 { rts_acosl, 1, "acosl", 1, _C99 },
901 { rts_atanl, 1, "atanl", 1, _C99 },
902 { rts_sinhl, 1, "sinhl", 1, _C99 },
903 { rts_coshl, 1, "coshl", 1, _C99 },
904 { rts_tanhl, 1, "tanhl", 1, _C99 },
906 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
907 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
908 { rts_strcmp, 1, "strcmp", 2, _C89 },
909 { rts_strncmp, 1, "strncmp", 3, _C89 }
912 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
915 * Mangles an entity linker (ld) name for win32 usage.
917 * @param ent the entity to be mangled
918 * @param declaration the declaration
920 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
924 if (is_Method_type(get_entity_type(ent)))
925 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
927 /* always add an underscore in win32 */
928 id = mangle(id_underscore, get_entity_ident(ent));
931 decl_modifiers_t decl_modifiers = declaration->decl_modifiers;
932 if (decl_modifiers & DM_DLLIMPORT) {
933 /* add prefix for imported symbols */
934 id = mangle(id_imp, id);
940 * Mangles an entity linker (ld) name for Linux ELF usage.
942 * @param ent the entity to be mangled
943 * @param declaration the declaration
945 static ident *create_ld_ident_linux_elf(ir_entity *entity,
946 declaration_t *declaration)
949 return get_entity_ident(entity);
953 * Mangles an entity linker (ld) name for Mach-O usage.
955 * @param ent the entity to be mangled
956 * @param declaration the declaration
958 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
961 ident *id = mangle(id_underscore, get_entity_ident(ent));
965 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
966 declaration_t *declaration);
967 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
970 * Creates an entity representing a function.
972 * @param declaration the function declaration
974 static ir_entity *get_function_entity(declaration_t *declaration)
976 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
977 return declaration->v.entity;
978 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
980 symbol_t *symbol = declaration->symbol;
981 ident *id = new_id_from_str(symbol->string);
983 ir_type *global_type = get_glob_type();
984 ir_type *ir_type_method = get_ir_type(declaration->type);
985 assert(is_Method_type(ir_type_method));
987 /* already an entity defined? */
988 ir_entity *entity = entitymap_get(&entitymap, symbol);
989 if (entity != NULL) {
990 if (get_entity_visibility(entity) == visibility_external_allocated
991 && declaration->init.statement != NULL) {
992 set_entity_visibility(entity, visibility_external_visible);
997 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
998 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
999 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1000 if (declaration->storage_class == STORAGE_CLASS_STATIC &&
1001 declaration->init.statement == NULL) {
1002 /* this entity was declared, but never defined */
1003 set_entity_peculiarity(entity, peculiarity_description);
1005 if (declaration->storage_class == STORAGE_CLASS_STATIC
1006 || declaration->is_inline) {
1007 set_entity_visibility(entity, visibility_local);
1008 } else if (declaration->init.statement != NULL) {
1009 set_entity_visibility(entity, visibility_external_visible);
1011 set_entity_visibility(entity, visibility_external_allocated);
1013 set_entity_allocation(entity, allocation_static);
1015 /* We should check for file scope here, but as long as we compile C only
1016 this is not needed. */
1017 if (! firm_opt.freestanding) {
1018 /* check for a known runtime function */
1019 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1020 if (id != rts_idents[i])
1023 /* ignore those rts functions not necessary needed for current mode */
1024 if ((c_mode & rts_data[i].flags) == 0)
1026 assert(rts_entities[rts_data[i].id] == NULL);
1027 rts_entities[rts_data[i].id] = entity;
1031 entitymap_insert(&entitymap, symbol, entity);
1034 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1035 declaration->v.entity = entity;
1041 * Creates a Const node representing a constant.
1043 static ir_node *const_to_firm(const const_expression_t *cnst)
1045 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1046 ir_mode *mode = get_ir_mode(cnst->base.type);
1051 if (mode_is_float(mode)) {
1052 tv = new_tarval_from_double(cnst->v.float_value, mode);
1054 if (mode_is_signed(mode)) {
1055 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1057 len = snprintf(buf, sizeof(buf), "%llu",
1058 (unsigned long long) cnst->v.int_value);
1060 tv = new_tarval_from_str(buf, len, mode);
1063 return new_d_Const(dbgi, mode, tv);
1067 * Creates a Const node representing a character constant.
1069 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1071 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1072 ir_mode *mode = get_ir_mode(cnst->base.type);
1074 long long int v = 0;
1075 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1076 if (char_is_signed) {
1077 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1079 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1083 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1084 tarval *tv = new_tarval_from_str(buf, len, mode);
1086 return new_d_Const(dbgi, mode, tv);
1090 * Creates a Const node representing a wide character constant.
1092 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1094 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1095 ir_mode *mode = get_ir_mode(cnst->base.type);
1097 long long int v = cnst->v.wide_character.begin[0];
1100 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1101 tarval *tv = new_tarval_from_str(buf, len, mode);
1103 return new_d_Const(dbgi, mode, tv);
1107 * Creates a SymConst for a given entity.
1109 * @param dbgi debug info
1110 * @param mode the (reference) mode for the SymConst
1111 * @param entity the entity
1113 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1116 assert(entity != NULL);
1117 union symconst_symbol sym;
1118 sym.entity_p = entity;
1119 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1123 * Creates a SymConst node representing a string constant.
1125 * @param src_pos the source position of the string constant
1126 * @param id_prefix a prefix for the name of the generated string constant
1127 * @param value the value of the string constant
1129 static ir_node *string_to_firm(const source_position_t *const src_pos,
1130 const char *const id_prefix,
1131 const string_t *const value)
1133 ir_type *const global_type = get_glob_type();
1134 dbg_info *const dbgi = get_dbg_info(src_pos);
1135 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1136 ir_type_const_char, dbgi);
1138 ident *const id = id_unique(id_prefix);
1139 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1140 set_entity_ld_ident(entity, id);
1141 set_entity_variability(entity, variability_constant);
1142 set_entity_allocation(entity, allocation_static);
1144 ir_type *const elem_type = ir_type_const_char;
1145 ir_mode *const mode = get_type_mode(elem_type);
1147 const char* const string = value->begin;
1148 const size_t slen = value->size;
1150 set_array_lower_bound_int(type, 0, 0);
1151 set_array_upper_bound_int(type, 0, slen);
1152 set_type_size_bytes(type, slen);
1153 set_type_state(type, layout_fixed);
1155 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1156 for(size_t i = 0; i < slen; ++i) {
1157 tvs[i] = new_tarval_from_long(string[i], mode);
1160 set_array_entity_values(entity, tvs, slen);
1163 return create_symconst(dbgi, mode_P_data, entity);
1167 * Creates a SymConst node representing a string literal.
1169 * @param literal the string literal
1171 static ir_node *string_literal_to_firm(
1172 const string_literal_expression_t* literal)
1174 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1179 * Creates a SymConst node representing a wide string literal.
1181 * @param literal the wide string literal
1183 static ir_node *wide_string_literal_to_firm(
1184 const wide_string_literal_expression_t* const literal)
1186 ir_type *const global_type = get_glob_type();
1187 ir_type *const elem_type = ir_type_wchar_t;
1188 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1189 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1192 ident *const id = id_unique("Lstr.%u");
1193 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1194 set_entity_ld_ident(entity, id);
1195 set_entity_variability(entity, variability_constant);
1196 set_entity_allocation(entity, allocation_static);
1198 ir_mode *const mode = get_type_mode(elem_type);
1200 const wchar_rep_t *const string = literal->value.begin;
1201 const size_t slen = literal->value.size;
1203 set_array_lower_bound_int(type, 0, 0);
1204 set_array_upper_bound_int(type, 0, slen);
1205 set_type_size_bytes(type, slen);
1206 set_type_state(type, layout_fixed);
1208 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1209 for(size_t i = 0; i < slen; ++i) {
1210 tvs[i] = new_tarval_from_long(string[i], mode);
1213 set_array_entity_values(entity, tvs, slen);
1216 return create_symconst(dbgi, mode_P_data, entity);
1219 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1220 ir_node *const addr)
1222 ir_type *irtype = get_ir_type(type);
1223 if (is_compound_type(irtype)
1224 || is_Method_type(irtype)
1225 || is_Array_type(irtype)) {
1229 ir_mode *const mode = get_type_mode(irtype);
1230 ir_node *const memory = get_store();
1231 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1232 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1233 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1235 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1236 set_Load_volatility(load, volatility_is_volatile);
1239 set_store(load_mem);
1244 * Creates a strict Conv if neccessary.
1246 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1248 ir_mode *mode = get_irn_mode(node);
1250 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1252 if (!mode_is_float(mode))
1255 /* check if there is already a Conv */
1256 if (is_Conv(node)) {
1257 /* convert it into a strict Conv */
1258 set_Conv_strict(node, 1);
1262 /* otherwise create a new one */
1263 return new_d_strictConv(dbgi, node, mode);
1266 static ir_node *get_global_var_address(dbg_info *const dbgi,
1267 const declaration_t *const decl)
1269 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1271 ir_entity *const entity = decl->v.entity;
1272 switch ((storage_class_tag_t)decl->storage_class) {
1273 case STORAGE_CLASS_THREAD:
1274 case STORAGE_CLASS_THREAD_EXTERN:
1275 case STORAGE_CLASS_THREAD_STATIC: {
1276 ir_node *const no_mem = new_NoMem();
1277 ir_node *const tls = get_irg_tls(current_ir_graph);
1278 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1282 return create_symconst(dbgi, mode_P_data, entity);
1287 * Returns the correct base address depending on whether it is a parameter or a
1288 * normal local variable.
1290 static ir_node *get_local_frame(ir_entity *const ent)
1292 ir_graph *const irg = current_ir_graph;
1293 const ir_type *const owner = get_entity_owner(ent);
1294 if (owner == get_irg_frame_type(irg)) {
1295 return get_irg_frame(irg);
1297 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1298 return get_irg_value_param_base(irg);
1302 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1304 ir_mode *value_mode = get_irn_mode(value);
1306 if (value_mode == dest_mode || is_Bad(value))
1309 if (dest_mode == mode_b) {
1310 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1311 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1312 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1316 return new_d_Conv(dbgi, value, dest_mode);
1319 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1321 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1322 declaration_t *declaration = ref->declaration;
1323 type_t *type = skip_typeref(declaration->type);
1325 /* make sure the type is constructed */
1326 (void) get_ir_type(type);
1328 switch((declaration_kind_t) declaration->declaration_kind) {
1329 case DECLARATION_KIND_TYPE:
1330 case DECLARATION_KIND_UNKNOWN:
1333 case DECLARATION_KIND_ENUM_ENTRY: {
1334 ir_mode *const mode = get_ir_mode(type);
1335 return new_Const(mode, declaration->v.enum_val);
1338 case DECLARATION_KIND_LOCAL_VARIABLE: {
1339 ir_mode *const mode = get_ir_mode(type);
1340 return get_value(declaration->v.value_number, mode);
1342 case DECLARATION_KIND_FUNCTION: {
1343 ir_mode *const mode = get_ir_mode(type);
1344 return create_symconst(dbgi, mode, declaration->v.entity);
1346 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1347 ir_node *const addr = get_global_var_address(dbgi, declaration);
1348 return deref_address(dbgi, declaration->type, addr);
1351 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1352 ir_entity *entity = declaration->v.entity;
1353 ir_node *frame = get_local_frame(entity);
1354 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1355 return deref_address(dbgi, declaration->type, sel);
1358 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1359 return declaration->v.vla_base;
1361 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1362 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1363 case DECLARATION_KIND_COMPOUND_MEMBER:
1364 case DECLARATION_KIND_LABEL_BLOCK:
1365 panic("not implemented reference type");
1368 panic("reference to declaration with unknown type found");
1371 static ir_node *reference_addr(const reference_expression_t *ref)
1373 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1374 declaration_t *declaration = ref->declaration;
1376 switch((declaration_kind_t) declaration->declaration_kind) {
1377 case DECLARATION_KIND_TYPE:
1378 case DECLARATION_KIND_UNKNOWN:
1380 case DECLARATION_KIND_LOCAL_VARIABLE:
1381 /* you can store to a local variable (so we don't panic but return NULL
1382 * as an indicator for no real address) */
1384 case DECLARATION_KIND_FUNCTION: {
1385 type_t *const type = skip_typeref(ref->base.type);
1386 ir_mode *const mode = get_ir_mode(type);
1387 return create_symconst(dbgi, mode, declaration->v.entity);
1389 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1390 ir_node *const addr = get_global_var_address(dbgi, declaration);
1393 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1394 ir_entity *entity = declaration->v.entity;
1395 ir_node *frame = get_local_frame(entity);
1396 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1401 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1402 return declaration->v.vla_base;
1404 case DECLARATION_KIND_ENUM_ENTRY:
1405 panic("trying to reference enum entry");
1407 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1408 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1409 case DECLARATION_KIND_COMPOUND_MEMBER:
1410 case DECLARATION_KIND_LABEL_BLOCK:
1411 panic("not implemented reference type");
1414 panic("reference to declaration with unknown type found");
1418 * Transform calls to builtin functions.
1420 static ir_node *process_builtin_call(const call_expression_t *call)
1422 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1424 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1425 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1427 type_t *type = skip_typeref(builtin->base.type);
1428 assert(is_type_pointer(type));
1430 type_t *function_type = skip_typeref(type->pointer.points_to);
1431 symbol_t *symbol = builtin->symbol;
1433 switch(symbol->ID) {
1434 case T___builtin_alloca: {
1435 if (call->arguments == NULL || call->arguments->next != NULL) {
1436 panic("invalid number of parameters on __builtin_alloca");
1438 expression_t *argument = call->arguments->expression;
1439 ir_node *size = expression_to_firm(argument);
1441 ir_node *store = get_store();
1442 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1444 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1446 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1450 case T___builtin_huge_val: {
1451 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1452 tarval *tv = get_mode_infinite(mode);
1453 ir_node *res = new_d_Const(dbgi, mode, tv);
1456 case T___builtin_nan:
1457 case T___builtin_nanf:
1458 case T___builtin_nand: {
1459 /* Ignore string for now... */
1460 assert(is_type_function(function_type));
1461 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1462 tarval *tv = get_mode_NAN(mode);
1463 ir_node *res = new_d_Const(dbgi, mode, tv);
1466 case T___builtin_va_end:
1469 panic("Unsupported builtin found\n");
1474 * Transform a call expression.
1475 * Handles some special cases, like alloca() calls, which must be resolved BEFORE the inlines runs.
1476 * Inlining routines calling alloca() is dangerous, 176.gcc for instance might allocate 2GB instead of
1477 * 256 MB if alloca is not handled right...
1479 static ir_node *call_expression_to_firm(const call_expression_t *call)
1481 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1482 assert(get_cur_block() != NULL);
1484 expression_t *function = call->function;
1485 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1486 return process_builtin_call(call);
1488 if (function->kind == EXPR_REFERENCE) {
1489 const reference_expression_t *ref = &function->reference;
1490 declaration_t *declaration = ref->declaration;
1492 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1493 if (declaration->v.entity == rts_entities[rts_alloca]) {
1494 /* handle alloca() call */
1495 expression_t *argument = call->arguments->expression;
1496 ir_node *size = expression_to_firm(argument);
1498 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1500 ir_node *store = get_store();
1501 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1502 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1504 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1506 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1512 ir_node *callee = expression_to_firm(function);
1514 type_t *type = skip_typeref(function->base.type);
1515 assert(is_type_pointer(type));
1516 pointer_type_t *pointer_type = &type->pointer;
1517 type_t *points_to = skip_typeref(pointer_type->points_to);
1518 assert(is_type_function(points_to));
1519 function_type_t *function_type = &points_to->function;
1521 int n_parameters = 0;
1522 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1523 ir_type *new_method_type = NULL;
1524 if (function_type->variadic || function_type->unspecified_parameters) {
1525 const call_argument_t *argument = call->arguments;
1526 for( ; argument != NULL; argument = argument->next) {
1530 /* we need to construct a new method type matching the call
1532 int n_res = get_method_n_ress(ir_method_type);
1533 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1534 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1535 n_parameters, n_res, dbgi);
1536 set_method_calling_convention(new_method_type,
1537 get_method_calling_convention(ir_method_type));
1538 set_method_additional_properties(new_method_type,
1539 get_method_additional_properties(ir_method_type));
1540 set_method_variadicity(new_method_type,
1541 get_method_variadicity(ir_method_type));
1543 for(int i = 0; i < n_res; ++i) {
1544 set_method_res_type(new_method_type, i,
1545 get_method_res_type(ir_method_type, i));
1547 argument = call->arguments;
1548 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1549 expression_t *expression = argument->expression;
1550 ir_type *irtype = get_ir_type(expression->base.type);
1551 set_method_param_type(new_method_type, i, irtype);
1553 ir_method_type = new_method_type;
1555 n_parameters = get_method_n_params(ir_method_type);
1558 ir_node *in[n_parameters];
1560 const call_argument_t *argument = call->arguments;
1561 for(int n = 0; n < n_parameters; ++n) {
1562 expression_t *expression = argument->expression;
1563 ir_node *arg_node = expression_to_firm(expression);
1565 arg_node = do_strict_conv(dbgi, arg_node);
1569 argument = argument->next;
1572 ir_node *store = get_store();
1573 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1575 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1578 type_t *return_type = skip_typeref(function_type->return_type);
1579 ir_node *result = NULL;
1581 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1583 if (is_type_scalar(return_type)) {
1584 mode = get_ir_mode(return_type);
1588 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1589 result = new_d_Proj(dbgi, resproj, mode, 0);
1595 static void statement_to_firm(statement_t *statement);
1596 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1598 static ir_node *expression_to_addr(const expression_t *expression);
1599 static void create_condition_evaluation(const expression_t *expression,
1600 ir_node *true_block,
1601 ir_node *false_block);
1603 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1606 value = do_strict_conv(dbgi, value);
1608 ir_node *memory = get_store();
1610 if (is_type_scalar(type)) {
1611 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1612 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1613 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1614 set_Store_volatility(store, volatility_is_volatile);
1615 set_store(store_mem);
1617 ir_type *irtype = get_ir_type(type);
1618 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1619 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1620 set_store(copyb_mem);
1624 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1626 tarval *all_one = get_mode_all_one(mode);
1627 int mode_size = get_mode_size_bits(mode);
1629 assert(offset >= 0 && size >= 0);
1630 assert(offset + size <= mode_size);
1631 if (size == mode_size) {
1635 long shiftr = get_mode_size_bits(mode) - size;
1636 long shiftl = offset;
1637 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1638 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1639 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1640 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1645 static void bitfield_store_to_firm(const select_expression_t *expression,
1646 ir_node *addr, ir_node *value)
1648 type_t *type = expression->base.type;
1649 ir_mode *mode = get_ir_mode(type);
1651 assert(get_irn_mode(value) == mode || is_Bad(value));
1653 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1655 /* kill upper bits of value and shift to right position */
1656 ir_entity *entity = expression->compound_entry->v.entity;
1657 int bitoffset = get_entity_offset_bits_remainder(entity);
1658 ir_type *entity_type = get_entity_type(entity);
1659 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1661 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1662 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1663 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1664 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1665 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1666 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1668 /* load current value */
1669 ir_node *mem = get_store();
1670 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1671 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1672 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1673 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1674 tarval *inv_mask = tarval_not(shift_mask);
1675 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1676 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1678 /* construct new value and store */
1679 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1680 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1681 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1682 set_store(store_mem);
1684 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1685 set_Load_volatility(load, volatility_is_volatile);
1686 set_Store_volatility(store, volatility_is_volatile);
1690 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1693 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1694 type_t *type = expression->base.type;
1695 ir_mode *mode = get_ir_mode(type);
1696 ir_node *mem = get_store();
1697 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1698 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1699 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1701 load_res = create_conv(dbgi, load_res, mode_int);
1703 set_store(load_mem);
1705 /* kill upper bits */
1706 ir_entity *entity = expression->compound_entry->v.entity;
1707 int bitoffset = get_entity_offset_bits_remainder(entity);
1708 ir_type *entity_type = get_entity_type(entity);
1709 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1710 long shift_bitsl = machine_size - bitoffset - bitsize;
1711 assert(shift_bitsl >= 0);
1712 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1713 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1714 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1716 long shift_bitsr = bitoffset + shift_bitsl;
1717 assert(shift_bitsr <= (long) machine_size);
1718 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1719 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1721 if (mode_is_signed(mode)) {
1722 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1724 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1727 return create_conv(dbgi, shiftr, mode);
1730 static void set_value_for_expression_addr(const expression_t *expression,
1731 ir_node *value, ir_node *addr)
1733 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1734 value = do_strict_conv(dbgi, value);
1736 if (expression->kind == EXPR_REFERENCE) {
1737 const reference_expression_t *ref = &expression->reference;
1739 declaration_t *declaration = ref->declaration;
1740 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1741 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1742 set_value(declaration->v.value_number, value);
1748 addr = expression_to_addr(expression);
1750 type_t *type = skip_typeref(expression->base.type);
1752 if (expression->kind == EXPR_SELECT) {
1753 const select_expression_t *select = &expression->select;
1755 declaration_t *declaration = select->compound_entry;
1756 if (declaration->type->kind == TYPE_BITFIELD) {
1757 bitfield_store_to_firm(select, addr, value);
1762 assign_value(dbgi, addr, type, value);
1765 static void set_value_for_expression(const expression_t *expression,
1768 set_value_for_expression_addr(expression, value, NULL);
1771 static ir_node *get_value_from_lvalue(const expression_t *expression,
1774 if (expression->kind == EXPR_REFERENCE) {
1775 const reference_expression_t *ref = &expression->reference;
1777 declaration_t *declaration = ref->declaration;
1778 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1779 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1780 assert(addr == NULL);
1781 ir_mode *mode = get_ir_mode(expression->base.type);
1782 return get_value(declaration->v.value_number, mode);
1786 assert(addr != NULL);
1787 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1790 if (expression->kind == EXPR_SELECT &&
1791 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1792 value = bitfield_extract_to_firm(&expression->select, addr);
1794 value = deref_address(dbgi, expression->base.type, addr);
1801 static ir_node *create_incdec(const unary_expression_t *expression)
1803 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1804 const expression_t *value_expr = expression->value;
1805 ir_node *addr = expression_to_addr(value_expr);
1806 ir_node *value = get_value_from_lvalue(value_expr, addr);
1808 type_t *type = skip_typeref(expression->base.type);
1809 ir_mode *mode = get_ir_mode(expression->base.type);
1812 if (is_type_pointer(type)) {
1813 pointer_type_t *pointer_type = &type->pointer;
1814 offset = get_type_size(pointer_type->points_to);
1816 assert(is_type_arithmetic(type));
1817 offset = new_Const(mode, get_mode_one(mode));
1821 ir_node *store_value;
1822 switch(expression->base.kind) {
1823 case EXPR_UNARY_POSTFIX_INCREMENT:
1825 store_value = new_d_Add(dbgi, value, offset, mode);
1827 case EXPR_UNARY_POSTFIX_DECREMENT:
1829 store_value = new_d_Sub(dbgi, value, offset, mode);
1831 case EXPR_UNARY_PREFIX_INCREMENT:
1832 result = new_d_Add(dbgi, value, offset, mode);
1833 store_value = result;
1835 case EXPR_UNARY_PREFIX_DECREMENT:
1836 result = new_d_Sub(dbgi, value, offset, mode);
1837 store_value = result;
1840 panic("no incdec expr in create_incdec");
1843 set_value_for_expression_addr(value_expr, store_value, addr);
1848 static bool is_local_variable(expression_t *expression)
1850 if (expression->kind != EXPR_REFERENCE)
1852 reference_expression_t *ref_expr = &expression->reference;
1853 declaration_t *declaration = ref_expr->declaration;
1854 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1857 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1860 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1861 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1862 case EXPR_BINARY_NOTEQUAL:
1863 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1864 case EXPR_BINARY_ISLESS:
1865 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1866 case EXPR_BINARY_ISLESSEQUAL:
1867 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1868 case EXPR_BINARY_ISGREATER:
1869 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1870 case EXPR_BINARY_ISGREATEREQUAL:
1871 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1872 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1877 panic("trying to get pn_Cmp from non-comparison binexpr type");
1881 * Handle the assume optimizer hint: check if a Confirm
1882 * node can be created.
1884 * @param dbi debug info
1885 * @param expr the IL assume expression
1887 * we support here only some simple cases:
1892 static ir_node *handle_assume_compare(dbg_info *dbi,
1893 const binary_expression_t *expression)
1895 expression_t *op1 = expression->left;
1896 expression_t *op2 = expression->right;
1897 declaration_t *var2, *var = NULL;
1898 ir_node *res = NULL;
1901 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1903 if (is_local_variable(op1) && is_local_variable(op2)) {
1904 var = op1->reference.declaration;
1905 var2 = op2->reference.declaration;
1907 type_t *const type = skip_typeref(var->type);
1908 ir_mode *const mode = get_ir_mode(type);
1910 ir_node *const irn1 = get_value(var->v.value_number, mode);
1911 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1913 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1914 set_value(var2->v.value_number, res);
1916 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1917 set_value(var->v.value_number, res);
1923 if (is_local_variable(op1) && is_constant_expression(op2)) {
1924 var = op1->reference.declaration;
1926 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1927 cmp_val = get_inversed_pnc(cmp_val);
1928 var = op2->reference.declaration;
1933 type_t *const type = skip_typeref(var->type);
1934 ir_mode *const mode = get_ir_mode(type);
1936 res = get_value(var->v.value_number, mode);
1937 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1938 set_value(var->v.value_number, res);
1944 * Handle the assume optimizer hint.
1946 * @param dbi debug info
1947 * @param expr the IL assume expression
1949 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1950 switch(expression->kind) {
1951 case EXPR_BINARY_EQUAL:
1952 case EXPR_BINARY_NOTEQUAL:
1953 case EXPR_BINARY_LESS:
1954 case EXPR_BINARY_LESSEQUAL:
1955 case EXPR_BINARY_GREATER:
1956 case EXPR_BINARY_GREATEREQUAL:
1957 return handle_assume_compare(dbi, &expression->binary);
1963 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1965 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1966 type_t *type = skip_typeref(expression->base.type);
1968 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1969 return expression_to_addr(expression->value);
1971 const expression_t *value = expression->value;
1973 switch(expression->base.kind) {
1974 case EXPR_UNARY_NEGATE: {
1975 ir_node *value_node = expression_to_firm(value);
1976 ir_mode *mode = get_ir_mode(type);
1977 return new_d_Minus(dbgi, value_node, mode);
1979 case EXPR_UNARY_PLUS:
1980 return expression_to_firm(value);
1981 case EXPR_UNARY_BITWISE_NEGATE: {
1982 ir_node *value_node = expression_to_firm(value);
1983 ir_mode *mode = get_ir_mode(type);
1984 return new_d_Not(dbgi, value_node, mode);
1986 case EXPR_UNARY_NOT: {
1987 ir_node *value_node = _expression_to_firm(value);
1988 value_node = create_conv(dbgi, value_node, mode_b);
1989 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
1992 case EXPR_UNARY_DEREFERENCE: {
1993 ir_node *value_node = expression_to_firm(value);
1994 type_t *value_type = skip_typeref(value->base.type);
1995 assert(is_type_pointer(value_type));
1996 type_t *points_to = value_type->pointer.points_to;
1997 return deref_address(dbgi, points_to, value_node);
1999 case EXPR_UNARY_POSTFIX_INCREMENT:
2000 case EXPR_UNARY_POSTFIX_DECREMENT:
2001 case EXPR_UNARY_PREFIX_INCREMENT:
2002 case EXPR_UNARY_PREFIX_DECREMENT:
2003 return create_incdec(expression);
2004 case EXPR_UNARY_CAST: {
2005 ir_node *value_node = expression_to_firm(value);
2006 if (is_type_scalar(type)) {
2007 ir_mode *mode = get_ir_mode(type);
2008 ir_node *node = create_conv(dbgi, value_node, mode);
2009 node = do_strict_conv(dbgi, node);
2012 /* make sure firm type is constructed */
2013 (void) get_ir_type(type);
2017 case EXPR_UNARY_CAST_IMPLICIT: {
2018 ir_node *value_node = expression_to_firm(value);
2019 if (is_type_scalar(type)) {
2020 ir_mode *mode = get_ir_mode(type);
2021 return create_conv(dbgi, value_node, mode);
2026 case EXPR_UNARY_ASSUME:
2027 if (firm_opt.confirm)
2028 return handle_assume(dbgi, value);
2035 panic("invalid UNEXPR type found");
2039 * produces a 0/1 depending of the value of a mode_b node
2041 static ir_node *produce_condition_result(const expression_t *expression,
2044 ir_mode *mode = get_ir_mode(expression->base.type);
2045 ir_node *cur_block = get_cur_block();
2047 ir_node *one_block = new_immBlock();
2048 ir_node *one = new_Const(mode, get_mode_one(mode));
2049 ir_node *jmp_one = new_d_Jmp(dbgi);
2051 ir_node *zero_block = new_immBlock();
2052 ir_node *zero = new_Const(mode, get_mode_null(mode));
2053 ir_node *jmp_zero = new_d_Jmp(dbgi);
2055 set_cur_block(cur_block);
2056 create_condition_evaluation(expression, one_block, zero_block);
2057 mature_immBlock(one_block);
2058 mature_immBlock(zero_block);
2060 ir_node *common_block = new_immBlock();
2061 add_immBlock_pred(common_block, jmp_one);
2062 add_immBlock_pred(common_block, jmp_zero);
2063 mature_immBlock(common_block);
2065 ir_node *in[2] = { one, zero };
2066 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2071 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2072 ir_node *value, type_t *type)
2074 pointer_type_t *const pointer_type = &type->pointer;
2075 type_t *const points_to = pointer_type->points_to;
2076 const unsigned elem_size = get_type_size_const(points_to);
2078 assert(elem_size >= 1);
2082 value = create_conv(dbgi, value, mode_int);
2083 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2084 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2088 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2089 ir_node *left, ir_node *right)
2092 type_t *type_left = skip_typeref(expression->left->base.type);
2093 type_t *type_right = skip_typeref(expression->right->base.type);
2095 expression_kind_t kind = expression->base.kind;
2098 case EXPR_BINARY_SHIFTLEFT:
2099 case EXPR_BINARY_SHIFTRIGHT:
2100 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2101 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2102 mode = get_irn_mode(left);
2103 right = create_conv(dbgi, right, mode_uint);
2106 case EXPR_BINARY_SUB:
2107 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2108 const pointer_type_t *const ptr_type = &type_left->pointer;
2110 mode = get_ir_mode(expression->base.type);
2111 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2112 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2113 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2114 ir_node *const no_mem = new_NoMem();
2115 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2116 mode, op_pin_state_floats);
2117 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2120 case EXPR_BINARY_SUB_ASSIGN:
2121 if (is_type_pointer(type_left)) {
2122 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2123 mode = get_ir_mode(type_left);
2128 case EXPR_BINARY_ADD:
2129 case EXPR_BINARY_ADD_ASSIGN:
2130 if (is_type_pointer(type_left)) {
2131 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2132 mode = get_ir_mode(type_left);
2134 } else if (is_type_pointer(type_right)) {
2135 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2136 mode = get_ir_mode(type_right);
2143 mode = get_irn_mode(right);
2144 left = create_conv(dbgi, left, mode);
2149 case EXPR_BINARY_ADD_ASSIGN:
2150 case EXPR_BINARY_ADD:
2151 return new_d_Add(dbgi, left, right, mode);
2152 case EXPR_BINARY_SUB_ASSIGN:
2153 case EXPR_BINARY_SUB:
2154 return new_d_Sub(dbgi, left, right, mode);
2155 case EXPR_BINARY_MUL_ASSIGN:
2156 case EXPR_BINARY_MUL:
2157 return new_d_Mul(dbgi, left, right, mode);
2158 case EXPR_BINARY_BITWISE_AND:
2159 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2160 return new_d_And(dbgi, left, right, mode);
2161 case EXPR_BINARY_BITWISE_OR:
2162 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2163 return new_d_Or(dbgi, left, right, mode);
2164 case EXPR_BINARY_BITWISE_XOR:
2165 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2166 return new_d_Eor(dbgi, left, right, mode);
2167 case EXPR_BINARY_SHIFTLEFT:
2168 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2169 return new_d_Shl(dbgi, left, right, mode);
2170 case EXPR_BINARY_SHIFTRIGHT:
2171 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2172 if (mode_is_signed(mode)) {
2173 return new_d_Shrs(dbgi, left, right, mode);
2175 return new_d_Shr(dbgi, left, right, mode);
2177 case EXPR_BINARY_DIV:
2178 case EXPR_BINARY_DIV_ASSIGN: {
2179 ir_node *pin = new_Pin(new_NoMem());
2182 if (mode_is_float(mode)) {
2183 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2184 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2186 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2187 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2191 case EXPR_BINARY_MOD:
2192 case EXPR_BINARY_MOD_ASSIGN: {
2193 ir_node *pin = new_Pin(new_NoMem());
2194 assert(!mode_is_float(mode));
2195 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2196 op_pin_state_floats);
2197 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2201 panic("unexpected expression kind");
2205 static ir_node *create_lazy_op(const binary_expression_t *expression)
2207 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2208 type_t *type = expression->base.type;
2209 ir_mode *mode = get_ir_mode(type);
2211 if (is_constant_expression(expression->left)) {
2212 long val = fold_constant(expression->left);
2213 expression_kind_t ekind = expression->base.kind;
2214 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
2215 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2216 return expression_to_firm(expression->right);
2218 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
2219 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
2220 return new_Const(mode, get_mode_one(mode));
2224 return produce_condition_result((const expression_t*) expression, dbgi);
2227 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2228 ir_node *right, ir_mode *mode);
2230 static ir_node *create_assign_binop(const binary_expression_t *expression)
2232 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2233 const expression_t *left_expr = expression->left;
2234 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2235 ir_node *left_addr = expression_to_addr(left_expr);
2236 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2237 ir_node *right = expression_to_firm(expression->right);
2238 ir_node *result = create_op(dbgi, expression, left, right);
2240 result = create_conv(dbgi, result, left_mode);
2241 result = do_strict_conv(dbgi, result);
2243 set_value_for_expression_addr(left_expr, result, left_addr);
2248 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2250 expression_kind_t kind = expression->base.kind;
2253 case EXPR_BINARY_EQUAL:
2254 case EXPR_BINARY_NOTEQUAL:
2255 case EXPR_BINARY_LESS:
2256 case EXPR_BINARY_LESSEQUAL:
2257 case EXPR_BINARY_GREATER:
2258 case EXPR_BINARY_GREATEREQUAL:
2259 case EXPR_BINARY_ISGREATER:
2260 case EXPR_BINARY_ISGREATEREQUAL:
2261 case EXPR_BINARY_ISLESS:
2262 case EXPR_BINARY_ISLESSEQUAL:
2263 case EXPR_BINARY_ISLESSGREATER:
2264 case EXPR_BINARY_ISUNORDERED: {
2265 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2266 ir_node *left = expression_to_firm(expression->left);
2267 ir_node *right = expression_to_firm(expression->right);
2268 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2269 long pnc = get_pnc(kind, expression->left->base.type);
2270 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2273 case EXPR_BINARY_ASSIGN: {
2274 ir_node *addr = expression_to_addr(expression->left);
2275 ir_node *right = expression_to_firm(expression->right);
2276 set_value_for_expression_addr(expression->left, right, addr);
2280 case EXPR_BINARY_ADD:
2281 case EXPR_BINARY_SUB:
2282 case EXPR_BINARY_MUL:
2283 case EXPR_BINARY_DIV:
2284 case EXPR_BINARY_MOD:
2285 case EXPR_BINARY_BITWISE_AND:
2286 case EXPR_BINARY_BITWISE_OR:
2287 case EXPR_BINARY_BITWISE_XOR:
2288 case EXPR_BINARY_SHIFTLEFT:
2289 case EXPR_BINARY_SHIFTRIGHT:
2291 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2292 ir_node *left = expression_to_firm(expression->left);
2293 ir_node *right = expression_to_firm(expression->right);
2294 return create_op(dbgi, expression, left, right);
2296 case EXPR_BINARY_LOGICAL_AND:
2297 case EXPR_BINARY_LOGICAL_OR:
2298 return create_lazy_op(expression);
2299 case EXPR_BINARY_COMMA:
2300 /* create side effects of left side */
2301 (void) expression_to_firm(expression->left);
2302 return _expression_to_firm(expression->right);
2304 case EXPR_BINARY_ADD_ASSIGN:
2305 case EXPR_BINARY_SUB_ASSIGN:
2306 case EXPR_BINARY_MUL_ASSIGN:
2307 case EXPR_BINARY_MOD_ASSIGN:
2308 case EXPR_BINARY_DIV_ASSIGN:
2309 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2310 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2311 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2312 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2313 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2314 return create_assign_binop(expression);
2315 case EXPR_BINARY_BUILTIN_EXPECT:
2316 return _expression_to_firm(expression->left);
2318 panic("TODO binexpr type");
2322 static ir_node *array_access_addr(const array_access_expression_t *expression)
2324 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2325 ir_node *base_addr = expression_to_firm(expression->array_ref);
2326 ir_node *offset = expression_to_firm(expression->index);
2328 type_t *offset_type = skip_typeref(expression->index->base.type);
2330 if (is_type_signed(offset_type)) {
2331 mode = get_ir_mode(type_ssize_t);
2333 mode = get_ir_mode(type_size_t);
2335 offset = create_conv(dbgi, offset, mode);
2337 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2338 assert(is_type_pointer(ref_type));
2339 pointer_type_t *pointer_type = &ref_type->pointer;
2341 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2342 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2343 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2345 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2350 static ir_node *array_access_to_firm(
2351 const array_access_expression_t *expression)
2353 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2354 ir_node *addr = array_access_addr(expression);
2355 type_t *type = revert_automatic_type_conversion(
2356 (const expression_t*) expression);
2357 type = skip_typeref(type);
2359 return deref_address(dbgi, type, addr);
2362 static long get_offsetof_offset(const offsetof_expression_t *expression)
2364 type_t *orig_type = expression->type;
2367 designator_t *designator = expression->designator;
2368 for( ; designator != NULL; designator = designator->next) {
2369 type_t *type = skip_typeref(orig_type);
2370 /* be sure the type is constructed */
2371 (void) get_ir_type(type);
2373 if (designator->symbol != NULL) {
2374 assert(is_type_compound(type));
2375 symbol_t *symbol = designator->symbol;
2377 declaration_t *declaration = type->compound.declaration;
2378 declaration_t *iter = declaration->scope.declarations;
2379 for( ; iter != NULL; iter = iter->next) {
2380 if (iter->symbol == symbol) {
2384 assert(iter != NULL);
2386 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2387 offset += get_entity_offset(iter->v.entity);
2389 orig_type = iter->type;
2391 expression_t *array_index = designator->array_index;
2392 assert(designator->array_index != NULL);
2393 assert(is_type_array(type));
2394 assert(is_type_valid(array_index->base.type));
2396 long index = fold_constant(array_index);
2397 ir_type *arr_type = get_ir_type(type);
2398 ir_type *elem_type = get_array_element_type(arr_type);
2399 long elem_size = get_type_size_bytes(elem_type);
2401 offset += index * elem_size;
2403 orig_type = type->array.element_type;
2410 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2412 ir_mode *mode = get_ir_mode(expression->base.type);
2413 long offset = get_offsetof_offset(expression);
2414 tarval *tv = new_tarval_from_long(offset, mode);
2415 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2417 return new_d_Const(dbgi, mode, tv);
2420 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2421 ir_entity *entity, type_t *type);
2423 static ir_node *compound_literal_to_firm(
2424 const compound_literal_expression_t *expression)
2426 type_t *type = expression->type;
2428 /* create an entity on the stack */
2429 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2431 ident *const id = id_unique("CompLit.%u");
2432 ir_type *const irtype = get_ir_type(type);
2433 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2434 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2435 set_entity_ld_ident(entity, id);
2437 set_entity_variability(entity, variability_uninitialized);
2439 /* create initialisation code */
2440 initializer_t *initializer = expression->initializer;
2441 create_local_initializer(initializer, dbgi, entity, type);
2443 /* create a sel for the compound literal address */
2444 ir_node *frame = get_local_frame(entity);
2445 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2450 * Transform a sizeof expression into Firm code.
2452 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2454 type_t *type = expression->type;
2456 type = expression->tp_expression->base.type;
2457 assert(type != NULL);
2460 type = skip_typeref(type);
2461 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2462 if (is_type_array(type) && type->array.is_vla
2463 && expression->tp_expression != NULL) {
2464 expression_to_firm(expression->tp_expression);
2467 return get_type_size(type);
2471 * Transform an alignof expression into Firm code.
2473 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2475 type_t *type = expression->type;
2477 /* beware: if expression is a variable reference, return the
2478 alignment of the variable. */
2479 const expression_t *tp_expression = expression->tp_expression;
2480 const declaration_t *declaration = expr_is_variable(tp_expression);
2481 if (declaration != NULL) {
2482 /* TODO: get the alignment of this variable. */
2484 type = tp_expression->base.type;
2485 assert(type != NULL);
2488 ir_mode *const mode = get_ir_mode(expression->base.type);
2489 symconst_symbol sym;
2490 sym.type_p = get_ir_type(type);
2491 return new_SymConst(mode, sym, symconst_type_align);
2494 static void init_ir_types(void);
2495 long fold_constant(const expression_t *expression)
2497 bool constant_folding_old = constant_folding;
2498 constant_folding = true;
2502 assert(is_constant_expression(expression));
2504 ir_graph *old_current_ir_graph = current_ir_graph;
2505 if (current_ir_graph == NULL) {
2506 current_ir_graph = get_const_code_irg();
2509 ir_node *cnst = expression_to_firm(expression);
2510 current_ir_graph = old_current_ir_graph;
2512 if (!is_Const(cnst)) {
2513 panic("couldn't fold constant\n");
2516 tarval *tv = get_Const_tarval(cnst);
2517 if (!tarval_is_long(tv)) {
2518 panic("result of constant folding is not integer\n");
2521 constant_folding = constant_folding_old;
2523 return get_tarval_long(tv);
2526 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2528 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2530 /* first try to fold a constant condition */
2531 if (is_constant_expression(expression->condition)) {
2532 long val = fold_constant(expression->condition);
2534 return expression_to_firm(expression->true_expression);
2536 return expression_to_firm(expression->false_expression);
2540 ir_node *cur_block = get_cur_block();
2542 /* create the true block */
2543 ir_node *true_block = new_immBlock();
2545 ir_node *true_val = expression_to_firm(expression->true_expression);
2546 ir_node *true_jmp = new_Jmp();
2548 /* create the false block */
2549 ir_node *false_block = new_immBlock();
2551 ir_node *false_val = expression_to_firm(expression->false_expression);
2552 ir_node *false_jmp = new_Jmp();
2554 /* create the condition evaluation */
2555 set_cur_block(cur_block);
2556 create_condition_evaluation(expression->condition, true_block, false_block);
2557 mature_immBlock(true_block);
2558 mature_immBlock(false_block);
2560 /* create the common block */
2561 ir_node *common_block = new_immBlock();
2562 add_immBlock_pred(common_block, true_jmp);
2563 add_immBlock_pred(common_block, false_jmp);
2564 mature_immBlock(common_block);
2566 /* TODO improve static semantics, so either both or no values are NULL */
2567 if (true_val == NULL || false_val == NULL)
2570 ir_node *in[2] = { true_val, false_val };
2571 ir_mode *mode = get_irn_mode(true_val);
2572 assert(get_irn_mode(false_val) == mode);
2573 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2579 * Returns an IR-node representing the address of a field.
2581 static ir_node *select_addr(const select_expression_t *expression)
2583 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2585 ir_node *compound_addr = expression_to_firm(expression->compound);
2587 /* make sure the type is constructed */
2588 type_t *type = skip_typeref(expression->compound->base.type);
2589 if (is_type_pointer(type)) {
2590 type = type->pointer.points_to;
2592 (void) get_ir_type(type);
2594 declaration_t *entry = expression->compound_entry;
2595 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2596 ir_entity *entity = entry->v.entity;
2598 assert(entity != NULL);
2600 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2605 static ir_node *select_to_firm(const select_expression_t *expression)
2607 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2608 ir_node *addr = select_addr(expression);
2609 type_t *type = revert_automatic_type_conversion(
2610 (const expression_t*) expression);
2611 type = skip_typeref(type);
2613 declaration_t *entry = expression->compound_entry;
2614 type_t *entry_type = skip_typeref(entry->type);
2616 if (entry_type->kind == TYPE_BITFIELD) {
2617 return bitfield_extract_to_firm(expression, addr);
2620 return deref_address(dbgi, type, addr);
2623 /* Values returned by __builtin_classify_type. */
2624 typedef enum gcc_type_class
2630 enumeral_type_class,
2633 reference_type_class,
2637 function_type_class,
2648 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2650 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2656 const atomic_type_t *const atomic_type = &type->atomic;
2657 switch (atomic_type->akind) {
2658 /* should not be reached */
2659 case ATOMIC_TYPE_INVALID:
2663 /* gcc cannot do that */
2664 case ATOMIC_TYPE_VOID:
2665 tc = void_type_class;
2668 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2669 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2670 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2671 case ATOMIC_TYPE_SHORT:
2672 case ATOMIC_TYPE_USHORT:
2673 case ATOMIC_TYPE_INT:
2674 case ATOMIC_TYPE_UINT:
2675 case ATOMIC_TYPE_LONG:
2676 case ATOMIC_TYPE_ULONG:
2677 case ATOMIC_TYPE_LONGLONG:
2678 case ATOMIC_TYPE_ULONGLONG:
2679 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2680 tc = integer_type_class;
2683 case ATOMIC_TYPE_FLOAT:
2684 case ATOMIC_TYPE_DOUBLE:
2685 case ATOMIC_TYPE_LONG_DOUBLE:
2686 tc = real_type_class;
2689 panic("Unexpected atomic type in classify_type_to_firm().");
2692 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2693 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2694 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2695 case TYPE_ARRAY: /* gcc handles this as pointer */
2696 case TYPE_FUNCTION: /* gcc handles this as pointer */
2697 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2698 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2699 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2701 /* gcc handles this as integer */
2702 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2705 /* typedef/typeof should be skipped already */
2712 panic("unexpected TYPE classify_type_to_firm().");
2715 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2716 ir_mode *const mode = mode_int;
2717 tarval *const tv = new_tarval_from_long(tc, mode);
2718 return new_d_Const(dbgi, mode, tv);
2721 static ir_node *function_name_to_firm(
2722 const funcname_expression_t *const expr)
2724 switch(expr->kind) {
2725 case FUNCNAME_FUNCTION:
2726 case FUNCNAME_PRETTY_FUNCTION:
2727 case FUNCNAME_FUNCDNAME:
2728 if (current_function_name == NULL) {
2729 const source_position_t *const src_pos = &expr->base.source_position;
2730 const char *const name = current_function_decl->symbol->string;
2731 const string_t string = { name, strlen(name) + 1 };
2732 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2734 return current_function_name;
2735 case FUNCNAME_FUNCSIG:
2736 if (current_funcsig == NULL) {
2737 const source_position_t *const src_pos = &expr->base.source_position;
2738 ir_entity *ent = get_irg_entity(current_ir_graph);
2739 const char *const name = get_entity_ld_name(ent);
2740 const string_t string = { name, strlen(name) + 1 };
2741 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2743 return current_funcsig;
2745 panic("Unsupported function name");
2748 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2750 statement_t *statement = expr->statement;
2752 assert(statement->kind == STATEMENT_COMPOUND);
2753 return compound_statement_to_firm(&statement->compound);
2756 static ir_node *va_start_expression_to_firm(
2757 const va_start_expression_t *const expr)
2759 ir_type *const method_type = get_ir_type(current_function_decl->type);
2760 int const n = get_method_n_params(method_type) - 1;
2761 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2762 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2763 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2764 ir_node *const no_mem = new_NoMem();
2765 ir_node *const arg_sel =
2766 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2768 ir_node *const cnst = get_type_size(expr->parameter->type);
2769 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2770 set_value_for_expression(expr->ap, add);
2775 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2777 type_t *const type = expr->base.type;
2778 expression_t *const ap_expr = expr->ap;
2779 ir_node *const ap_addr = expression_to_addr(ap_expr);
2780 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2781 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2782 ir_node *const res = deref_address(dbgi, type, ap);
2784 ir_node *const cnst = get_type_size(expr->base.type);
2785 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2787 set_value_for_expression_addr(ap_expr, add, ap_addr);
2792 static ir_node *dereference_addr(const unary_expression_t *const expression)
2794 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2795 return expression_to_firm(expression->value);
2799 * Returns a IR-node representing an lvalue of the given expression.
2801 static ir_node *expression_to_addr(const expression_t *expression)
2803 switch(expression->kind) {
2804 case EXPR_REFERENCE:
2805 return reference_addr(&expression->reference);
2806 case EXPR_ARRAY_ACCESS:
2807 return array_access_addr(&expression->array_access);
2809 return select_addr(&expression->select);
2811 return call_expression_to_firm(&expression->call);
2812 case EXPR_UNARY_DEREFERENCE: {
2813 return dereference_addr(&expression->unary);
2818 panic("trying to get address of non-lvalue");
2821 static ir_node *builtin_constant_to_firm(
2822 const builtin_constant_expression_t *expression)
2824 ir_mode *mode = get_ir_mode(expression->base.type);
2827 if (is_constant_expression(expression->value)) {
2832 return new_Const_long(mode, v);
2835 static ir_node *builtin_prefetch_to_firm(
2836 const builtin_prefetch_expression_t *expression)
2838 ir_node *adr = expression_to_firm(expression->adr);
2839 /* no Firm support for prefetch yet */
2845 * creates firm nodes for an expression. The difference between this function
2846 * and expression_to_firm is, that this version might produce mode_b nodes
2847 * instead of mode_Is.
2849 static ir_node *_expression_to_firm(const expression_t *expression)
2852 if (!constant_folding) {
2853 assert(!expression->base.transformed);
2854 ((expression_t*) expression)->base.transformed = true;
2858 switch(expression->kind) {
2859 case EXPR_CHARACTER_CONSTANT:
2860 return character_constant_to_firm(&expression->conste);
2861 case EXPR_WIDE_CHARACTER_CONSTANT:
2862 return wide_character_constant_to_firm(&expression->conste);
2864 return const_to_firm(&expression->conste);
2865 case EXPR_STRING_LITERAL:
2866 return string_literal_to_firm(&expression->string);
2867 case EXPR_WIDE_STRING_LITERAL:
2868 return wide_string_literal_to_firm(&expression->wide_string);
2869 case EXPR_REFERENCE:
2870 return reference_expression_to_firm(&expression->reference);
2872 return call_expression_to_firm(&expression->call);
2874 return unary_expression_to_firm(&expression->unary);
2876 return binary_expression_to_firm(&expression->binary);
2877 case EXPR_ARRAY_ACCESS:
2878 return array_access_to_firm(&expression->array_access);
2880 return sizeof_to_firm(&expression->typeprop);
2882 return alignof_to_firm(&expression->typeprop);
2883 case EXPR_CONDITIONAL:
2884 return conditional_to_firm(&expression->conditional);
2886 return select_to_firm(&expression->select);
2887 case EXPR_CLASSIFY_TYPE:
2888 return classify_type_to_firm(&expression->classify_type);
2890 return function_name_to_firm(&expression->funcname);
2891 case EXPR_STATEMENT:
2892 return statement_expression_to_firm(&expression->statement);
2894 return va_start_expression_to_firm(&expression->va_starte);
2896 return va_arg_expression_to_firm(&expression->va_arge);
2897 case EXPR_BUILTIN_SYMBOL:
2898 panic("unimplemented expression found");
2899 case EXPR_BUILTIN_CONSTANT_P:
2900 return builtin_constant_to_firm(&expression->builtin_constant);
2901 case EXPR_BUILTIN_PREFETCH:
2902 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2904 return offsetof_to_firm(&expression->offsetofe);
2905 case EXPR_COMPOUND_LITERAL:
2906 return compound_literal_to_firm(&expression->compound_literal);
2912 panic("invalid expression found");
2915 static bool produces_mode_b(const expression_t *expression)
2917 switch (expression->kind) {
2918 case EXPR_BINARY_EQUAL:
2919 case EXPR_BINARY_NOTEQUAL:
2920 case EXPR_BINARY_LESS:
2921 case EXPR_BINARY_LESSEQUAL:
2922 case EXPR_BINARY_GREATER:
2923 case EXPR_BINARY_GREATEREQUAL:
2924 case EXPR_BINARY_ISGREATER:
2925 case EXPR_BINARY_ISGREATEREQUAL:
2926 case EXPR_BINARY_ISLESS:
2927 case EXPR_BINARY_ISLESSEQUAL:
2928 case EXPR_BINARY_ISLESSGREATER:
2929 case EXPR_BINARY_ISUNORDERED:
2930 case EXPR_UNARY_NOT:
2933 case EXPR_BINARY_BUILTIN_EXPECT:
2934 return produces_mode_b(expression->binary.left);
2935 case EXPR_BINARY_COMMA:
2936 return produces_mode_b(expression->binary.right);
2943 static ir_node *expression_to_firm(const expression_t *expression)
2945 if (!produces_mode_b(expression)) {
2946 ir_node *res = _expression_to_firm(expression);
2947 assert(res == NULL || get_irn_mode(res) != mode_b);
2951 if (is_constant_expression(expression)) {
2952 ir_node *res = _expression_to_firm(expression);
2953 ir_mode *mode = get_ir_mode(expression->base.type);
2954 assert(is_Const(res));
2955 if (is_Const_null(res)) {
2956 return new_Const_long(mode, 0);
2958 return new_Const_long(mode, 1);
2962 /* we have to produce a 0/1 from the mode_b expression */
2963 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2964 return produce_condition_result(expression, dbgi);
2967 static ir_node *expression_to_modeb(const expression_t *expression)
2969 ir_node *res = _expression_to_firm(expression);
2970 res = create_conv(NULL, res, mode_b);
2976 * create a short-circuit expression evaluation that tries to construct
2977 * efficient control flow structures for &&, || and ! expressions
2979 static void create_condition_evaluation(const expression_t *expression,
2980 ir_node *true_block,
2981 ir_node *false_block)
2983 switch(expression->kind) {
2984 case EXPR_UNARY_NOT: {
2985 const unary_expression_t *unary_expression = &expression->unary;
2986 create_condition_evaluation(unary_expression->value, false_block,
2990 case EXPR_BINARY_LOGICAL_AND: {
2991 const binary_expression_t *binary_expression = &expression->binary;
2993 ir_node *cur_block = get_cur_block();
2994 ir_node *extra_block = new_immBlock();
2995 set_cur_block(cur_block);
2996 create_condition_evaluation(binary_expression->left, extra_block,
2998 mature_immBlock(extra_block);
2999 set_cur_block(extra_block);
3000 create_condition_evaluation(binary_expression->right, true_block,
3004 case EXPR_BINARY_LOGICAL_OR: {
3005 const binary_expression_t *binary_expression = &expression->binary;
3007 ir_node *cur_block = get_cur_block();
3008 ir_node *extra_block = new_immBlock();
3009 set_cur_block(cur_block);
3010 create_condition_evaluation(binary_expression->left, true_block,
3012 mature_immBlock(extra_block);
3013 set_cur_block(extra_block);
3014 create_condition_evaluation(binary_expression->right, true_block,
3022 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3023 ir_node *condition = expression_to_modeb(expression);
3024 ir_node *cond = new_d_Cond(dbgi, condition);
3025 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3026 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3028 /* set branch prediction info based on __builtin_expect */
3029 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3030 long cnst = fold_constant(expression->binary.right);
3031 cond_jmp_predicate pred;
3034 pred = COND_JMP_PRED_FALSE;
3036 pred = COND_JMP_PRED_TRUE;
3038 set_Cond_jmp_pred(cond, pred);
3041 add_immBlock_pred(true_block, true_proj);
3042 if (false_block != NULL) {
3043 add_immBlock_pred(false_block, false_proj);
3046 set_cur_block(NULL);
3051 static void create_declaration_entity(declaration_t *declaration,
3052 declaration_kind_t declaration_kind,
3053 ir_type *parent_type)
3055 type_t *const type = skip_typeref(declaration->type);
3056 if (is_type_function(type)) {
3057 (void) get_function_entity(declaration);
3061 ident *const id = new_id_from_str(declaration->symbol->string);
3062 ir_type *const irtype = get_ir_type(type);
3063 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3064 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3066 declaration->declaration_kind = (unsigned char) declaration_kind;
3067 declaration->v.entity = entity;
3068 set_entity_variability(entity, variability_uninitialized);
3069 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3070 if (parent_type == get_tls_type())
3071 set_entity_allocation(entity, allocation_automatic);
3072 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3073 set_entity_allocation(entity, allocation_static);
3075 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3076 set_entity_volatility(entity, volatility_is_volatile);
3081 typedef struct type_path_entry_t type_path_entry_t;
3082 struct type_path_entry_t {
3084 ir_initializer_t *initializer;
3086 declaration_t *compound_entry;
3089 typedef struct type_path_t type_path_t;
3090 struct type_path_t {
3091 type_path_entry_t *path;
3096 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3098 size_t len = ARR_LEN(path->path);
3100 for(size_t i = 0; i < len; ++i) {
3101 const type_path_entry_t *entry = & path->path[i];
3103 type_t *type = skip_typeref(entry->type);
3104 if (is_type_compound(type)) {
3105 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3106 } else if (is_type_array(type)) {
3107 fprintf(stderr, "[%zd]", entry->index);
3109 fprintf(stderr, "-INVALID-");
3112 fprintf(stderr, " (");
3113 print_type(path->top_type);
3114 fprintf(stderr, ")");
3117 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3119 size_t len = ARR_LEN(path->path);
3121 return & path->path[len-1];
3124 static type_path_entry_t *append_to_type_path(type_path_t *path)
3126 size_t len = ARR_LEN(path->path);
3127 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3129 type_path_entry_t *result = & path->path[len];
3130 memset(result, 0, sizeof(result[0]));
3134 static size_t get_compound_size(const compound_type_t *type)
3136 declaration_t *declaration = type->declaration;
3137 declaration_t *member = declaration->scope.declarations;
3139 for( ; member != NULL; member = member->next) {
3142 /* TODO: cache results? */
3147 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3149 type_t *orig_top_type = path->top_type;
3150 type_t *top_type = skip_typeref(orig_top_type);
3152 assert(is_type_compound(top_type) || is_type_array(top_type));
3154 if (ARR_LEN(path->path) == 0) {
3157 type_path_entry_t *top = get_type_path_top(path);
3158 ir_initializer_t *initializer = top->initializer;
3159 return get_initializer_compound_value(initializer, top->index);
3163 static void descend_into_subtype(type_path_t *path)
3165 type_t *orig_top_type = path->top_type;
3166 type_t *top_type = skip_typeref(orig_top_type);
3168 assert(is_type_compound(top_type) || is_type_array(top_type));
3170 ir_initializer_t *initializer = get_initializer_entry(path);
3172 type_path_entry_t *top = append_to_type_path(path);
3173 top->type = top_type;
3177 if (is_type_compound(top_type)) {
3178 declaration_t *declaration = top_type->compound.declaration;
3179 declaration_t *entry = declaration->scope.declarations;
3181 top->compound_entry = entry;
3183 len = get_compound_size(&top_type->compound);
3185 path->top_type = entry->type;
3187 assert(is_type_array(top_type));
3188 assert(top_type->array.size > 0);
3191 path->top_type = top_type->array.element_type;
3192 len = top_type->array.size;
3194 if (initializer == NULL
3195 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3196 initializer = create_initializer_compound(len);
3197 /* we have to set the entry at the 2nd latest path entry... */
3198 size_t path_len = ARR_LEN(path->path);
3199 assert(path_len >= 1);
3201 type_path_entry_t *entry = & path->path[path_len-2];
3202 ir_initializer_t *tinitializer = entry->initializer;
3203 set_initializer_compound_value(tinitializer, entry->index,
3207 top->initializer = initializer;
3210 static void ascend_from_subtype(type_path_t *path)
3212 type_path_entry_t *top = get_type_path_top(path);
3214 path->top_type = top->type;
3216 size_t len = ARR_LEN(path->path);
3217 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3220 static void walk_designator(type_path_t *path, const designator_t *designator)
3222 /* designators start at current object type */
3223 ARR_RESIZE(type_path_entry_t, path->path, 1);
3225 for( ; designator != NULL; designator = designator->next) {
3226 type_path_entry_t *top = get_type_path_top(path);
3227 type_t *orig_type = top->type;
3228 type_t *type = skip_typeref(orig_type);
3230 if (designator->symbol != NULL) {
3231 assert(is_type_compound(type));
3233 symbol_t *symbol = designator->symbol;
3235 declaration_t *declaration = type->compound.declaration;
3236 declaration_t *iter = declaration->scope.declarations;
3237 for( ; iter != NULL; iter = iter->next, ++index) {
3238 if (iter->symbol == symbol) {
3242 assert(iter != NULL);
3244 top->type = orig_type;
3245 top->compound_entry = iter;
3247 orig_type = iter->type;
3249 expression_t *array_index = designator->array_index;
3250 assert(designator->array_index != NULL);
3251 assert(is_type_array(type));
3252 assert(is_type_valid(array_index->base.type));
3254 long index = fold_constant(array_index);
3257 if (type->array.size_constant == 1) {
3258 long array_size = type->array.size;
3259 assert(index < array_size);
3263 top->type = orig_type;
3264 top->index = (size_t) index;
3265 orig_type = type->array.element_type;
3267 path->top_type = orig_type;
3269 if (designator->next != NULL) {
3270 descend_into_subtype(path);
3274 path->invalid = false;
3277 static void advance_current_object(type_path_t *path)
3279 if (path->invalid) {
3280 /* TODO: handle this... */
3281 panic("invalid initializer in ast2firm (excessive elements)");
3285 type_path_entry_t *top = get_type_path_top(path);
3287 type_t *type = skip_typeref(top->type);
3288 if (is_type_union(type)) {
3289 top->compound_entry = NULL;
3290 } else if (is_type_struct(type)) {
3291 declaration_t *entry = top->compound_entry;
3294 entry = entry->next;
3295 top->compound_entry = entry;
3296 if (entry != NULL) {
3297 path->top_type = entry->type;
3301 assert(is_type_array(type));
3304 if (!type->array.size_constant || top->index < type->array.size) {
3309 /* we're past the last member of the current sub-aggregate, try if we
3310 * can ascend in the type hierarchy and continue with another subobject */
3311 size_t len = ARR_LEN(path->path);
3314 ascend_from_subtype(path);
3315 advance_current_object(path);
3317 path->invalid = true;
3322 static ir_initializer_t *create_ir_initializer(
3323 const initializer_t *initializer, type_t *type);
3325 static ir_initializer_t *create_ir_initializer_value(
3326 const initializer_value_t *initializer)
3328 if (is_type_compound(initializer->value->base.type)) {
3329 panic("initializer creation for compounds not implemented yet");
3331 ir_node *value = expression_to_firm(initializer->value);
3332 return create_initializer_const(value);
3335 static ir_initializer_t *create_ir_initializer_list(
3336 const initializer_list_t *initializer, type_t *type)
3339 memset(&path, 0, sizeof(path));
3340 path.top_type = type;
3341 path.path = NEW_ARR_F(type_path_entry_t, 0);
3343 descend_into_subtype(&path);
3345 for(size_t i = 0; i < initializer->len; ++i) {
3346 const initializer_t *sub_initializer = initializer->initializers[i];
3348 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3349 walk_designator(&path, sub_initializer->designator.designator);
3353 if (sub_initializer->kind == INITIALIZER_VALUE) {
3354 /* we might have to descend into types until we're at a scalar
3357 type_t *orig_top_type = path.top_type;
3358 type_t *top_type = skip_typeref(orig_top_type);
3360 if (is_type_scalar(top_type))
3362 descend_into_subtype(&path);
3366 ir_initializer_t *sub_irinitializer
3367 = create_ir_initializer(sub_initializer, path.top_type);
3369 size_t path_len = ARR_LEN(path.path);
3370 assert(path_len >= 1);
3371 type_path_entry_t *entry = & path.path[path_len-1];
3372 ir_initializer_t *tinitializer = entry->initializer;
3373 set_initializer_compound_value(tinitializer, entry->index,
3376 advance_current_object(&path);
3379 assert(ARR_LEN(path.path) >= 1);
3380 ir_initializer_t *result = path.path[0].initializer;
3381 DEL_ARR_F(path.path);
3386 static ir_initializer_t *create_ir_initializer_string(
3387 const initializer_string_t *initializer, type_t *type)
3389 type = skip_typeref(type);
3391 size_t string_len = initializer->string.size;
3392 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3393 size_t len = type->array.size;
3394 ir_initializer_t *irinitializer = create_initializer_compound(len);
3396 const char *string = initializer->string.begin;
3397 ir_mode *mode = get_type_mode(ir_type_const_char);
3399 for(size_t i = 0; i < len; ++i) {
3404 tarval *tv = new_tarval_from_long(c, mode);
3405 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3407 set_initializer_compound_value(irinitializer, i, char_initializer);
3410 return irinitializer;
3413 static ir_initializer_t *create_ir_initializer_wide_string(
3414 const initializer_wide_string_t *initializer, type_t *type)
3416 size_t string_len = initializer->string.size;
3417 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3418 size_t len = type->array.size;
3419 ir_initializer_t *irinitializer = create_initializer_compound(len);
3421 const wchar_rep_t *string = initializer->string.begin;
3422 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3424 for(size_t i = 0; i < len; ++i) {
3426 if (i < string_len) {
3429 tarval *tv = new_tarval_from_long(c, mode);
3430 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3432 set_initializer_compound_value(irinitializer, i, char_initializer);
3435 return irinitializer;
3438 static ir_initializer_t *create_ir_initializer(
3439 const initializer_t *initializer, type_t *type)
3441 switch(initializer->kind) {
3442 case INITIALIZER_STRING:
3443 return create_ir_initializer_string(&initializer->string, type);
3445 case INITIALIZER_WIDE_STRING:
3446 return create_ir_initializer_wide_string(&initializer->wide_string,
3449 case INITIALIZER_LIST:
3450 return create_ir_initializer_list(&initializer->list, type);
3452 case INITIALIZER_VALUE:
3453 return create_ir_initializer_value(&initializer->value);
3455 case INITIALIZER_DESIGNATOR:
3456 panic("unexpected designator initializer found");
3458 panic("unknown initializer");
3461 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3464 if (is_atomic_type(type)) {
3465 ir_mode *mode = get_type_mode(type);
3466 tarval *zero = get_mode_null(mode);
3467 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3469 /* TODO: bitfields */
3470 ir_node *mem = get_store();
3471 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3472 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3475 assert(is_compound_type(type));
3478 if (is_Array_type(type)) {
3479 assert(has_array_upper_bound(type, 0));
3480 n_members = get_array_upper_bound_int(type, 0);
3482 n_members = get_compound_n_members(type);
3485 for(int i = 0; i < n_members; ++i) {
3488 if (is_Array_type(type)) {
3489 ir_entity *entity = get_array_element_entity(type);
3490 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3491 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3492 ir_node *in[1] = { cnst };
3493 irtype = get_array_element_type(type);
3494 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3496 ir_entity *member = get_compound_member(type, i);
3498 irtype = get_entity_type(member);
3499 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3502 create_dynamic_null_initializer(irtype, dbgi, addr);
3507 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3508 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3510 switch(get_initializer_kind(initializer)) {
3511 case IR_INITIALIZER_NULL: {
3512 create_dynamic_null_initializer(type, dbgi, base_addr);
3515 case IR_INITIALIZER_CONST: {
3516 ir_node *node = get_initializer_const_value(initializer);
3517 ir_mode *mode = get_irn_mode(node);
3518 assert(get_type_mode(type) == mode);
3520 /* TODO: bitfields... */
3521 ir_node *mem = get_store();
3522 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3523 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3527 case IR_INITIALIZER_TARVAL: {
3528 tarval *tv = get_initializer_tarval_value(initializer);
3529 ir_mode *mode = get_tarval_mode(tv);
3530 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3531 assert(get_type_mode(type) == mode);
3533 /* TODO: bitfields... */
3534 ir_node *mem = get_store();
3535 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3536 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3540 case IR_INITIALIZER_COMPOUND: {
3541 assert(is_compound_type(type));
3543 if (is_Array_type(type)) {
3544 assert(has_array_upper_bound(type, 0));
3545 n_members = get_array_upper_bound_int(type, 0);
3547 n_members = get_compound_n_members(type);
3550 if (get_initializer_compound_n_entries(initializer)
3551 != (unsigned) n_members)
3552 panic("initializer doesn't match compound type");
3554 for(int i = 0; i < n_members; ++i) {
3557 if (is_Array_type(type)) {
3558 ir_entity *entity = get_array_element_entity(type);
3559 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3560 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3561 ir_node *in[1] = { cnst };
3562 irtype = get_array_element_type(type);
3563 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3565 ir_entity *member = get_compound_member(type, i);
3567 irtype = get_entity_type(member);
3568 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3571 ir_initializer_t *sub_init
3572 = get_initializer_compound_value(initializer, i);
3574 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3580 panic("invalid IR_INITIALIZER found");
3583 static void create_dynamic_initializer(ir_initializer_t *initializer,
3584 dbg_info *dbgi, ir_entity *entity)
3586 ir_node *frame = get_local_frame(entity);
3587 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3588 ir_type *type = get_entity_type(entity);
3590 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3593 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3594 ir_entity *entity, type_t *type)
3596 ir_node *memory = get_store();
3597 ir_node *nomem = new_NoMem();
3598 ir_node *frame = get_irg_frame(current_ir_graph);
3599 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3601 if (initializer->kind == INITIALIZER_VALUE) {
3602 initializer_value_t *initializer_value = &initializer->value;
3604 ir_node *value = expression_to_firm(initializer_value->value);
3605 type = skip_typeref(type);
3606 assign_value(dbgi, addr, type, value);
3610 if (!is_constant_initializer(initializer)) {
3611 ir_initializer_t *irinitializer
3612 = create_ir_initializer(initializer, type);
3614 create_dynamic_initializer(irinitializer, dbgi, entity);
3618 /* create the ir_initializer */
3619 ir_graph *const old_current_ir_graph = current_ir_graph;
3620 current_ir_graph = get_const_code_irg();
3622 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3624 assert(current_ir_graph == get_const_code_irg());
3625 current_ir_graph = old_current_ir_graph;
3627 /* create a "template" entity which is copied to the entity on the stack */
3628 ident *const id = id_unique("initializer.%u");
3629 ir_type *const irtype = get_ir_type(type);
3630 ir_type *const global_type = get_glob_type();
3631 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3632 set_entity_ld_ident(init_entity, id);
3634 set_entity_variability(init_entity, variability_initialized);
3635 set_entity_visibility(init_entity, visibility_local);
3636 set_entity_allocation(init_entity, allocation_static);
3638 set_entity_initializer(init_entity, irinitializer);
3640 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3641 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3643 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3644 set_store(copyb_mem);
3647 static void create_initializer_local_variable_entity(declaration_t *declaration)
3649 initializer_t *initializer = declaration->init.initializer;
3650 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3651 ir_entity *entity = declaration->v.entity;
3652 type_t *type = declaration->type;
3653 create_local_initializer(initializer, dbgi, entity, type);
3656 static void create_declaration_initializer(declaration_t *declaration)
3658 initializer_t *initializer = declaration->init.initializer;
3659 if (initializer == NULL)
3662 declaration_kind_t declaration_kind
3663 = (declaration_kind_t) declaration->declaration_kind;
3664 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3665 create_initializer_local_variable_entity(declaration);
3669 if (initializer->kind == INITIALIZER_VALUE) {
3670 initializer_value_t *initializer_value = &initializer->value;
3672 = get_dbg_info(&declaration->source_position);
3674 ir_node *value = expression_to_firm(initializer_value->value);
3675 value = do_strict_conv(dbgi, value);
3677 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3678 set_value(declaration->v.value_number, value);
3680 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3682 ir_entity *entity = declaration->v.entity;
3684 set_entity_variability(entity, variability_initialized);
3685 set_atomic_ent_value(entity, value);
3688 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3689 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3691 ir_entity *entity = declaration->v.entity;
3692 ir_initializer_t *irinitializer
3693 = create_ir_initializer(initializer, declaration->type);
3695 set_entity_variability(entity, variability_initialized);
3696 set_entity_initializer(entity, irinitializer);
3700 static void create_variable_length_array(declaration_t *declaration)
3702 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3703 type_t *type = declaration->type;
3704 ir_node *mem = get_store();
3705 ir_type *el_type = get_ir_type(type->array.element_type);
3707 /* make sure size_node is calculated */
3708 get_type_size(type);
3709 ir_node *elems = type->array.size_node;
3710 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3712 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3713 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3716 /* initializers are not allowed for VLAs */
3717 assert(declaration->init.initializer == NULL);
3719 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3720 declaration->v.vla_base = addr;
3722 /* TODO: record VLA somewhere so we create the free node when we leave
3727 * Creates a Firm local variable from a declaration.
3729 static void create_local_variable(declaration_t *declaration)
3731 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3733 bool needs_entity = declaration->address_taken;
3734 type_t *type = skip_typeref(declaration->type);
3736 /* is it a variable length array? */
3737 if (is_type_array(type) && !type->array.size_constant) {
3738 create_variable_length_array(declaration);
3740 } else if (is_type_array(type) || is_type_compound(type)) {
3741 needs_entity = true;
3742 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3743 needs_entity = true;
3747 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3748 create_declaration_entity(declaration,
3749 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3752 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3753 declaration->v.value_number = next_value_number_function;
3754 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3755 ++next_value_number_function;
3759 static void create_local_static_variable(declaration_t *declaration)
3761 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3763 type_t *const type = skip_typeref(declaration->type);
3764 ir_type *const global_type = get_glob_type();
3765 ir_type *const irtype = get_ir_type(type);
3766 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3768 size_t l = strlen(declaration->symbol->string);
3769 char buf[l + sizeof(".%u")];
3770 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3771 ident *const id = id_unique(buf);
3773 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3775 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3776 set_entity_volatility(entity, volatility_is_volatile);
3779 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3780 declaration->v.entity = entity;
3781 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3782 set_entity_variability(entity, variability_uninitialized);
3783 set_entity_visibility(entity, visibility_local);
3784 set_entity_allocation(entity, allocation_static);
3786 ir_graph *const old_current_ir_graph = current_ir_graph;
3787 current_ir_graph = get_const_code_irg();
3789 create_declaration_initializer(declaration);
3791 assert(current_ir_graph == get_const_code_irg());
3792 current_ir_graph = old_current_ir_graph;
3797 static void return_statement_to_firm(return_statement_t *statement)
3799 if (get_cur_block() == NULL)
3802 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3803 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3808 if (get_method_n_ress(func_irtype) > 0) {
3809 ir_type *res_type = get_method_res_type(func_irtype, 0);
3811 if (statement->value != NULL) {
3812 ir_node *node = expression_to_firm(statement->value);
3813 node = do_strict_conv(dbgi, node);
3817 if (is_compound_type(res_type)) {
3820 mode = get_type_mode(res_type);
3822 in[0] = new_Unknown(mode);
3826 /* build return_value for its side effects */
3827 if (statement->value != NULL) {
3828 expression_to_firm(statement->value);
3833 ir_node *store = get_store();
3834 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3836 ir_node *end_block = get_irg_end_block(current_ir_graph);
3837 add_immBlock_pred(end_block, ret);
3839 set_cur_block(NULL);
3842 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3844 if (get_cur_block() == NULL)
3847 return expression_to_firm(statement->expression);
3850 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3852 declaration_t *declaration = compound->scope.declarations;
3853 for( ; declaration != NULL; declaration = declaration->next) {
3854 create_local_declaration(declaration);
3857 ir_node *result = NULL;
3858 statement_t *statement = compound->statements;
3859 for( ; statement != NULL; statement = statement->base.next) {
3860 if (statement->base.next == NULL
3861 && statement->kind == STATEMENT_EXPRESSION) {
3862 result = expression_statement_to_firm(
3863 &statement->expression);
3866 statement_to_firm(statement);
3872 static void create_global_variable(declaration_t *declaration)
3876 switch ((storage_class_tag_t)declaration->storage_class) {
3877 case STORAGE_CLASS_STATIC:
3878 vis = visibility_local;
3881 case STORAGE_CLASS_EXTERN:
3882 vis = visibility_external_allocated;
3885 case STORAGE_CLASS_NONE:
3886 vis = visibility_external_visible;
3889 case STORAGE_CLASS_THREAD:
3890 vis = visibility_external_visible;
3893 case STORAGE_CLASS_THREAD_EXTERN:
3894 vis = visibility_external_allocated;
3897 case STORAGE_CLASS_THREAD_STATIC:
3898 vis = visibility_local;
3902 var_type = get_tls_type();
3906 var_type = get_glob_type();
3910 create_declaration_entity(declaration,
3911 DECLARATION_KIND_GLOBAL_VARIABLE,
3913 if (!is_type_function(skip_typeref(declaration->type))) {
3914 set_entity_visibility(declaration->v.entity, vis);
3919 case STORAGE_CLASS_TYPEDEF:
3920 case STORAGE_CLASS_AUTO:
3921 case STORAGE_CLASS_REGISTER:
3922 case STORAGE_CLASS_ENUM_ENTRY:
3925 panic("Invalid storage class for global variable");
3928 static void create_local_declaration(declaration_t *declaration)
3930 if (declaration->namespc != NAMESPACE_NORMAL)
3932 /* construct type */
3933 (void) get_ir_type(declaration->type);
3934 if (declaration->symbol == NULL) {
3938 type_t *type = skip_typeref(declaration->type);
3940 switch ((storage_class_tag_t) declaration->storage_class) {
3941 case STORAGE_CLASS_STATIC:
3942 create_local_static_variable(declaration);
3944 case STORAGE_CLASS_EXTERN:
3945 create_global_variable(declaration);
3946 create_declaration_initializer(declaration);
3948 case STORAGE_CLASS_NONE:
3949 case STORAGE_CLASS_AUTO:
3950 case STORAGE_CLASS_REGISTER:
3951 if (is_type_function(type)) {
3952 if (declaration->init.statement != NULL) {
3953 panic("nested functions not supported yet");
3955 get_function_entity(declaration);
3958 create_local_variable(declaration);
3961 case STORAGE_CLASS_ENUM_ENTRY:
3962 /* should already be handled */
3963 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
3965 case STORAGE_CLASS_TYPEDEF:
3966 declaration->declaration_kind = DECLARATION_KIND_TYPE;
3968 case STORAGE_CLASS_THREAD:
3969 case STORAGE_CLASS_THREAD_EXTERN:
3970 case STORAGE_CLASS_THREAD_STATIC:
3973 panic("invalid storage class found");
3976 static void initialize_local_declaration(declaration_t *declaration)
3978 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
3981 switch ((declaration_kind_t) declaration->declaration_kind) {
3982 case DECLARATION_KIND_LOCAL_VARIABLE:
3983 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
3984 create_declaration_initializer(declaration);
3987 case DECLARATION_KIND_LABEL_BLOCK:
3988 case DECLARATION_KIND_COMPOUND_MEMBER:
3989 case DECLARATION_KIND_GLOBAL_VARIABLE:
3990 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
3991 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
3992 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
3993 case DECLARATION_KIND_FUNCTION:
3994 case DECLARATION_KIND_TYPE:
3995 case DECLARATION_KIND_ENUM_ENTRY:
3998 case DECLARATION_KIND_UNKNOWN:
3999 panic("can't initialize unknwon declaration");
4001 panic("invalid declaration kind");
4004 static void declaration_statement_to_firm(declaration_statement_t *statement)
4006 declaration_t *declaration = statement->declarations_begin;
4007 declaration_t *end = statement->declarations_end->next;
4008 for( ; declaration != end; declaration = declaration->next) {
4009 if (declaration->namespc != NAMESPACE_NORMAL)
4011 initialize_local_declaration(declaration);
4015 static void if_statement_to_firm(if_statement_t *statement)
4017 ir_node *cur_block = get_cur_block();
4019 ir_node *fallthrough_block = NULL;
4021 /* the true (blocks) */
4022 ir_node *true_block = NULL;
4023 if (statement->true_statement != NULL) {
4024 true_block = new_immBlock();
4025 statement_to_firm(statement->true_statement);
4026 if (get_cur_block() != NULL) {
4027 ir_node *jmp = new_Jmp();
4028 if (fallthrough_block == NULL)
4029 fallthrough_block = new_immBlock();
4030 add_immBlock_pred(fallthrough_block, jmp);
4034 /* the false (blocks) */
4035 ir_node *false_block = NULL;
4036 if (statement->false_statement != NULL) {
4037 false_block = new_immBlock();
4039 statement_to_firm(statement->false_statement);
4040 if (get_cur_block() != NULL) {
4041 ir_node *jmp = new_Jmp();
4042 if (fallthrough_block == NULL)
4043 fallthrough_block = new_immBlock();
4044 add_immBlock_pred(fallthrough_block, jmp);
4048 /* create the condition */
4049 if (cur_block != NULL) {
4050 if (true_block == NULL || false_block == NULL) {
4051 if (fallthrough_block == NULL)
4052 fallthrough_block = new_immBlock();
4053 if (true_block == NULL)
4054 true_block = fallthrough_block;
4055 if (false_block == NULL)
4056 false_block = fallthrough_block;
4059 set_cur_block(cur_block);
4060 create_condition_evaluation(statement->condition, true_block,
4064 mature_immBlock(true_block);
4065 if (false_block != fallthrough_block) {
4066 mature_immBlock(false_block);
4068 if (fallthrough_block != NULL) {
4069 mature_immBlock(fallthrough_block);
4072 set_cur_block(fallthrough_block);
4075 static void while_statement_to_firm(while_statement_t *statement)
4077 ir_node *jmp = NULL;
4078 if (get_cur_block() != NULL) {
4082 /* create the header block */
4083 ir_node *header_block = new_immBlock();
4085 add_immBlock_pred(header_block, jmp);
4089 ir_node *old_continue_label = continue_label;
4090 ir_node *old_break_label = break_label;
4091 continue_label = header_block;
4094 ir_node *body_block = new_immBlock();
4095 statement_to_firm(statement->body);
4096 ir_node *false_block = break_label;
4098 assert(continue_label == header_block);
4099 continue_label = old_continue_label;
4100 break_label = old_break_label;
4102 if (get_cur_block() != NULL) {
4104 add_immBlock_pred(header_block, jmp);
4107 /* shortcut for while(true) */
4108 if (is_constant_expression(statement->condition)
4109 && fold_constant(statement->condition) != 0) {
4110 set_cur_block(header_block);
4111 ir_node *header_jmp = new_Jmp();
4112 add_immBlock_pred(body_block, header_jmp);
4114 keep_alive(body_block);
4116 if (false_block == NULL) {
4117 false_block = new_immBlock();
4120 /* create the condition */
4121 set_cur_block(header_block);
4123 create_condition_evaluation(statement->condition, body_block,
4127 mature_immBlock(body_block);
4128 mature_immBlock(header_block);
4129 if (false_block != NULL) {
4130 mature_immBlock(false_block);
4133 set_cur_block(false_block);
4136 static void do_while_statement_to_firm(do_while_statement_t *statement)
4138 ir_node *jmp = NULL;
4139 if (get_cur_block() != NULL) {
4143 /* create the header block */
4144 ir_node *header_block = new_immBlock();
4147 ir_node *body_block = new_immBlock();
4149 add_immBlock_pred(body_block, jmp);
4152 ir_node *false_block = NULL;
4154 if (statement->body != NULL) {
4155 ir_node *old_continue_label = continue_label;
4156 ir_node *old_break_label = break_label;
4157 continue_label = header_block;
4158 break_label = false_block;
4160 statement_to_firm(statement->body);
4161 false_block = break_label;
4163 assert(continue_label == header_block);
4164 continue_label = old_continue_label;
4165 break_label = old_break_label;
4168 if (get_cur_block() != NULL) {
4169 ir_node *body_jmp = new_Jmp();
4170 add_immBlock_pred(header_block, body_jmp);
4171 mature_immBlock(header_block);
4174 /* create the condition */
4175 set_cur_block(header_block);
4177 create_condition_evaluation(statement->condition, body_block, false_block);
4178 mature_immBlock(body_block);
4179 mature_immBlock(header_block);
4180 if (false_block != NULL) {
4181 mature_immBlock(false_block);
4184 set_cur_block(false_block);
4187 static void for_statement_to_firm(for_statement_t *statement)
4189 ir_node *jmp = NULL;
4191 /* create declarations */
4192 declaration_t *declaration = statement->scope.declarations;
4193 for( ; declaration != NULL; declaration = declaration->next) {
4194 create_local_declaration(declaration);
4196 declaration = statement->scope.declarations;
4197 for( ; declaration != NULL; declaration = declaration->next) {
4198 initialize_local_declaration(declaration);
4201 if (get_cur_block() != NULL) {
4202 if (statement->initialisation != NULL) {
4203 expression_to_firm(statement->initialisation);
4210 /* create the step block */
4211 ir_node *const step_block = new_immBlock();
4212 if (statement->step != NULL) {
4213 expression_to_firm(statement->step);
4215 ir_node *const step_jmp = new_Jmp();
4217 /* create the header block */
4218 ir_node *const header_block = new_immBlock();
4220 add_immBlock_pred(header_block, jmp);
4222 add_immBlock_pred(header_block, step_jmp);
4224 /* the false block */
4225 ir_node *const false_block = new_immBlock();
4228 ir_node * body_block;
4229 if (statement->body != NULL) {
4230 ir_node *const old_continue_label = continue_label;
4231 ir_node *const old_break_label = break_label;
4232 continue_label = step_block;
4233 break_label = false_block;
4235 body_block = new_immBlock();
4236 statement_to_firm(statement->body);
4238 assert(continue_label == step_block);
4239 assert(break_label == false_block);
4240 continue_label = old_continue_label;
4241 break_label = old_break_label;
4243 if (get_cur_block() != NULL) {
4245 add_immBlock_pred(step_block, jmp);
4248 body_block = step_block;
4251 /* create the condition */
4252 set_cur_block(header_block);
4253 if (statement->condition != NULL) {
4254 create_condition_evaluation(statement->condition, body_block,
4257 keep_alive(header_block);
4259 add_immBlock_pred(body_block, jmp);
4262 mature_immBlock(body_block);
4263 mature_immBlock(false_block);
4264 mature_immBlock(step_block);
4265 mature_immBlock(header_block);
4266 mature_immBlock(false_block);
4268 set_cur_block(false_block);
4271 static void create_jump_statement(const statement_t *statement,
4272 ir_node *target_block)
4274 if (get_cur_block() == NULL)
4277 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4278 ir_node *jump = new_d_Jmp(dbgi);
4279 add_immBlock_pred(target_block, jump);
4281 set_cur_block(NULL);
4284 static ir_node *get_break_label(void)
4286 if (break_label == NULL) {
4287 ir_node *cur_block = get_cur_block();
4288 break_label = new_immBlock();
4289 set_cur_block(cur_block);
4294 static void switch_statement_to_firm(const switch_statement_t *statement)
4296 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4298 ir_node *expression = expression_to_firm(statement->expression);
4299 ir_node *cond = new_d_Cond(dbgi, expression);
4301 set_cur_block(NULL);
4303 ir_node *const old_switch_cond = current_switch_cond;
4304 ir_node *const old_break_label = break_label;
4305 const bool old_saw_default_label = saw_default_label;
4306 saw_default_label = false;
4307 current_switch_cond = cond;
4310 if (statement->body != NULL) {
4311 statement_to_firm(statement->body);
4314 if (get_cur_block() != NULL) {
4315 ir_node *jmp = new_Jmp();
4316 add_immBlock_pred(get_break_label(), jmp);
4319 if (!saw_default_label) {
4320 set_cur_block(get_nodes_block(cond));
4321 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4322 MAGIC_DEFAULT_PN_NUMBER);
4323 add_immBlock_pred(get_break_label(), proj);
4326 if (break_label != NULL) {
4327 mature_immBlock(break_label);
4329 set_cur_block(break_label);
4331 assert(current_switch_cond == cond);
4332 current_switch_cond = old_switch_cond;
4333 break_label = old_break_label;
4334 saw_default_label = old_saw_default_label;
4337 static void case_label_to_firm(const case_label_statement_t *statement)
4339 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4341 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4343 /* let's create a node and hope firm constant folding creates a Const
4346 ir_node *old_block = get_nodes_block(current_switch_cond);
4347 ir_node *block = new_immBlock();
4349 set_cur_block(old_block);
4350 if (statement->expression != NULL) {
4351 long start_pn = fold_constant(statement->expression);
4352 long end_pn = start_pn;
4353 if (statement->end_range != NULL) {
4354 end_pn = fold_constant(statement->end_range);
4356 assert(start_pn <= end_pn);
4357 /* create jumps for all cases in the given range */
4358 for (long pn = start_pn; pn <= end_pn; ++pn) {
4359 if (pn == MAGIC_DEFAULT_PN_NUMBER) {
4360 /* oops someone detected our cheating... */
4361 panic("magic default pn used");
4363 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4364 add_immBlock_pred(block, proj);
4367 saw_default_label = true;
4368 proj = new_d_defaultProj(dbgi, current_switch_cond,
4369 MAGIC_DEFAULT_PN_NUMBER);
4371 add_immBlock_pred(block, proj);
4374 if (fallthrough != NULL) {
4375 add_immBlock_pred(block, fallthrough);
4377 mature_immBlock(block);
4378 set_cur_block(block);
4380 if (statement->statement != NULL) {
4381 statement_to_firm(statement->statement);
4385 static ir_node *get_label_block(declaration_t *label)
4387 assert(label->namespc == NAMESPACE_LABEL);
4389 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4390 return label->v.block;
4392 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4394 ir_node *old_cur_block = get_cur_block();
4395 ir_node *block = new_immBlock();
4396 set_cur_block(old_cur_block);
4398 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4399 label->v.block = block;
4401 ARR_APP1(ir_node *, imature_blocks, block);
4406 static void label_to_firm(const label_statement_t *statement)
4408 ir_node *block = get_label_block(statement->label);
4410 if (get_cur_block() != NULL) {
4411 ir_node *jmp = new_Jmp();
4412 add_immBlock_pred(block, jmp);
4415 set_cur_block(block);
4418 if (statement->statement != NULL) {
4419 statement_to_firm(statement->statement);
4423 static void goto_to_firm(const goto_statement_t *statement)
4425 if (get_cur_block() == NULL)
4428 ir_node *block = get_label_block(statement->label);
4429 ir_node *jmp = new_Jmp();
4430 add_immBlock_pred(block, jmp);
4432 set_cur_block(NULL);
4435 typedef enum modifier_t {
4436 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4437 ASM_MODIFIER_READ_WRITE = 1 << 1,
4438 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4439 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4442 static void asm_statement_to_firm(const asm_statement_t *statement)
4445 fprintf(stderr, "WARNING asm not implemented yet!\n");
4447 bool needs_memory = false;
4449 size_t n_clobbers = 0;
4450 asm_clobber_t *clobber = statement->clobbers;
4451 for( ; clobber != NULL; clobber = clobber->next) {
4452 if (strcmp(clobber->clobber, "memory") == 0) {
4453 needs_memory = true;
4457 ident *id = new_id_from_str(clobber->clobber);
4458 obstack_ptr_grow(&asm_obst, id);
4461 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4462 ident **clobbers = NULL;
4463 if (n_clobbers > 0) {
4464 clobbers = obstack_finish(&asm_obst);
4467 /* find and count input and output constraints */
4468 asm_constraint_t *constraint = statement->inputs;
4469 for( ; constraint != NULL; constraint = constraint->next) {
4471 bool supports_memop = false;
4472 for(const char *c = constraint->constraints; *c != 0; ++c) {
4473 /* TODO: improve error messages */
4477 panic("multiple alternative assembler constraints not "
4485 supports_memop = true;
4486 obstack_1grow(&asm_obst, *c);
4489 if (modifiers & ASM_MODIFIER_READ_WRITE)
4490 panic("inconsistent register constraints");
4491 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4494 if (modifiers & ASM_MODIFIER_WRITE_ONLY)
4495 panic("inconsistent register constraints");
4496 modifiers |= ASM_MODIFIER_READ_WRITE;
4499 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4500 panic("early clobber assembler constraint not supported yet");
4503 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4504 panic("commutative assembler constraint not supported yet");
4507 /* skip register preferences stuff... */
4508 while(*c != 0 && *c != ',')
4512 /* skip register preferences stuff... */
4516 obstack_1grow(&asm_obst, *c);
4520 obstack_1grow(&asm_obst, '\0');
4521 const char *constraint_string = obstack_finish(&asm_obst);
4523 needs_memory |= supports_memop;
4524 if (supports_memop) {
4531 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4532 statement_to_firm(statement->try_statement);
4533 warningf(&statement->base.source_position, "structured exception handling ignored");
4536 static void leave_statement_to_firm(leave_statement_t *statement) {
4537 errorf(&statement->base.source_position, "__leave not supported yet");
4541 * Transform a statement.
4543 static void statement_to_firm(statement_t *statement)
4546 assert(!statement->base.transformed);
4547 statement->base.transformed = true;
4550 switch(statement->kind) {
4551 case STATEMENT_INVALID:
4552 panic("invalid statement found");
4554 case STATEMENT_EMPTY:
4557 case STATEMENT_COMPOUND:
4558 compound_statement_to_firm(&statement->compound);
4560 case STATEMENT_RETURN:
4561 return_statement_to_firm(&statement->returns);
4563 case STATEMENT_EXPRESSION:
4564 expression_statement_to_firm(&statement->expression);
4567 if_statement_to_firm(&statement->ifs);
4569 case STATEMENT_WHILE:
4570 while_statement_to_firm(&statement->whiles);
4572 case STATEMENT_DO_WHILE:
4573 do_while_statement_to_firm(&statement->do_while);
4575 case STATEMENT_DECLARATION:
4576 declaration_statement_to_firm(&statement->declaration);
4578 case STATEMENT_BREAK:
4579 create_jump_statement(statement, get_break_label());
4581 case STATEMENT_CONTINUE:
4582 create_jump_statement(statement, continue_label);
4584 case STATEMENT_SWITCH:
4585 switch_statement_to_firm(&statement->switchs);
4587 case STATEMENT_CASE_LABEL:
4588 case_label_to_firm(&statement->case_label);
4591 for_statement_to_firm(&statement->fors);
4593 case STATEMENT_LABEL:
4594 label_to_firm(&statement->label);
4596 case STATEMENT_GOTO:
4597 goto_to_firm(&statement->gotos);
4600 asm_statement_to_firm(&statement->asms);
4602 case STATEMENT_MS_TRY:
4603 ms_try_statement_to_firm(&statement->ms_try);
4605 case STATEMENT_LEAVE:
4606 leave_statement_to_firm(&statement->leave);
4609 panic("Statement not implemented\n");
4612 static int count_decls_in_expression(const expression_t *expression);
4614 static int count_local_declarations(const declaration_t * decl,
4615 const declaration_t *const end)
4618 for (; decl != end; decl = decl->next) {
4619 if (decl->namespc != NAMESPACE_NORMAL)
4621 const type_t *type = skip_typeref(decl->type);
4622 if (!decl->address_taken && is_type_scalar(type))
4624 const initializer_t *initializer = decl->init.initializer;
4625 /* FIXME: should walk initializer hierarchies... */
4626 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4627 count += count_decls_in_expression(initializer->value.value);
4633 static int count_decls_in_expression(const expression_t *expression) {
4636 if (expression == NULL)
4639 switch((expression_kind_t) expression->base.kind) {
4640 case EXPR_STATEMENT:
4641 return count_decls_in_stmts(expression->statement.statement);
4643 int count_left = count_decls_in_expression(expression->binary.left);
4644 int count_right = count_decls_in_expression(expression->binary.right);
4645 return count_left + count_right;
4648 return count_decls_in_expression(expression->unary.value);
4650 call_argument_t *argument = expression->call.arguments;
4651 for( ; argument != NULL; argument = argument->next) {
4652 count += count_decls_in_expression(argument->expression);
4659 panic("unexpected expression kind");
4661 case EXPR_COMPOUND_LITERAL:
4665 case EXPR_CONDITIONAL:
4666 count += count_decls_in_expression(expression->conditional.condition);
4667 count += count_decls_in_expression(expression->conditional.true_expression);
4668 count += count_decls_in_expression(expression->conditional.false_expression);
4671 case EXPR_BUILTIN_PREFETCH:
4672 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4673 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4674 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4677 case EXPR_BUILTIN_CONSTANT_P:
4678 count += count_decls_in_expression(expression->builtin_constant.value);
4682 count += count_decls_in_expression(expression->select.compound);
4685 case EXPR_ARRAY_ACCESS:
4686 count += count_decls_in_expression(expression->array_access.array_ref);
4687 count += count_decls_in_expression(expression->array_access.index);
4690 case EXPR_CLASSIFY_TYPE:
4691 count += count_decls_in_expression(expression->classify_type.type_expression);
4695 case EXPR_ALIGNOF: {
4696 expression_t *tp_expression = expression->typeprop.tp_expression;
4697 if (tp_expression != NULL) {
4698 count += count_decls_in_expression(tp_expression);
4704 case EXPR_REFERENCE:
4706 case EXPR_CHARACTER_CONSTANT:
4707 case EXPR_WIDE_CHARACTER_CONSTANT:
4708 case EXPR_STRING_LITERAL:
4709 case EXPR_WIDE_STRING_LITERAL:
4711 case EXPR_BUILTIN_SYMBOL:
4717 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4718 * (or implement all the missing expressions here/implement a walker)
4724 static int count_decls_in_stmts(const statement_t *stmt)
4727 for (; stmt != NULL; stmt = stmt->base.next) {
4728 switch (stmt->kind) {
4729 case STATEMENT_EMPTY:
4732 case STATEMENT_DECLARATION: {
4733 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4734 count += count_local_declarations(decl_stmt->declarations_begin,
4735 decl_stmt->declarations_end->next);
4739 case STATEMENT_COMPOUND: {
4740 const compound_statement_t *const comp =
4742 count += count_decls_in_stmts(comp->statements);
4746 case STATEMENT_IF: {
4747 const if_statement_t *const if_stmt = &stmt->ifs;
4748 count += count_decls_in_expression(if_stmt->condition);
4749 count += count_decls_in_stmts(if_stmt->true_statement);
4750 count += count_decls_in_stmts(if_stmt->false_statement);
4754 case STATEMENT_SWITCH: {
4755 const switch_statement_t *const switch_stmt = &stmt->switchs;
4756 count += count_decls_in_expression(switch_stmt->expression);
4757 count += count_decls_in_stmts(switch_stmt->body);
4761 case STATEMENT_LABEL: {
4762 const label_statement_t *const label_stmt = &stmt->label;
4763 if (label_stmt->statement != NULL) {
4764 count += count_decls_in_stmts(label_stmt->statement);
4769 case STATEMENT_WHILE: {
4770 const while_statement_t *const while_stmt = &stmt->whiles;
4771 count += count_decls_in_expression(while_stmt->condition);
4772 count += count_decls_in_stmts(while_stmt->body);
4776 case STATEMENT_DO_WHILE: {
4777 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4778 count += count_decls_in_expression(do_while_stmt->condition);
4779 count += count_decls_in_stmts(do_while_stmt->body);
4783 case STATEMENT_FOR: {
4784 const for_statement_t *const for_stmt = &stmt->fors;
4785 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4786 count += count_decls_in_expression(for_stmt->initialisation);
4787 count += count_decls_in_expression(for_stmt->condition);
4788 count += count_decls_in_expression(for_stmt->step);
4789 count += count_decls_in_stmts(for_stmt->body);
4793 case STATEMENT_CASE_LABEL: {
4794 const case_label_statement_t *label = &stmt->case_label;
4795 count += count_decls_in_expression(label->expression);
4796 if (label->statement != NULL) {
4797 count += count_decls_in_stmts(label->statement);
4803 case STATEMENT_BREAK:
4804 case STATEMENT_CONTINUE:
4807 case STATEMENT_EXPRESSION: {
4808 const expression_statement_t *expr_stmt = &stmt->expression;
4809 count += count_decls_in_expression(expr_stmt->expression);
4813 case STATEMENT_GOTO:
4814 case STATEMENT_LEAVE:
4815 case STATEMENT_INVALID:
4818 case STATEMENT_RETURN: {
4819 const return_statement_t *ret_stmt = &stmt->returns;
4820 count += count_decls_in_expression(ret_stmt->value);
4824 case STATEMENT_MS_TRY: {
4825 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
4826 count += count_decls_in_stmts(try_stmt->try_statement);
4827 if (try_stmt->except_expression != NULL)
4828 count += count_decls_in_expression(try_stmt->except_expression);
4829 count += count_decls_in_stmts(try_stmt->final_statement);
4837 static int get_function_n_local_vars(declaration_t *declaration)
4841 /* count parameters */
4842 count += count_local_declarations(declaration->scope.declarations, NULL);
4844 /* count local variables declared in body */
4845 count += count_decls_in_stmts(declaration->init.statement);
4850 static void initialize_function_parameters(declaration_t *declaration)
4852 ir_graph *irg = current_ir_graph;
4853 ir_node *args = get_irg_args(irg);
4854 ir_node *start_block = get_irg_start_block(irg);
4855 ir_type *function_irtype = get_ir_type(declaration->type);
4858 declaration_t *parameter = declaration->scope.declarations;
4859 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4860 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4861 type_t *type = skip_typeref(parameter->type);
4863 bool needs_entity = parameter->address_taken;
4864 assert(!is_type_array(type));
4865 if (is_type_compound(type)) {
4866 needs_entity = true;
4870 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4871 ident *id = new_id_from_str(parameter->symbol->string);
4872 set_entity_ident(entity, id);
4874 parameter->declaration_kind
4875 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4876 parameter->v.entity = entity;
4880 ir_type *param_irtype = get_method_param_type(function_irtype, n);
4881 ir_mode *param_mode = get_type_mode(param_irtype);
4884 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
4886 ir_mode *mode = get_ir_mode(parameter->type);
4887 value = create_conv(NULL, value, mode);
4888 value = do_strict_conv(NULL, value);
4890 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4891 parameter->v.value_number = next_value_number_function;
4892 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4893 ++next_value_number_function;
4895 set_value(parameter->v.value_number, value);
4900 * Handle additional decl modifiers for IR-graphs
4902 * @param irg the IR-graph
4903 * @param dec_modifiers additional modifiers
4905 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4907 if (decl_modifiers & DM_NORETURN) {
4908 /* TRUE if the declaration includes the Microsoft
4909 __declspec(noreturn) specifier. */
4910 set_irg_additional_property(irg, mtp_property_noreturn);
4912 if (decl_modifiers & DM_NOTHROW) {
4913 /* TRUE if the declaration includes the Microsoft
4914 __declspec(nothrow) specifier. */
4915 set_irg_additional_property(irg, mtp_property_nothrow);
4917 if (decl_modifiers & DM_NAKED) {
4918 /* TRUE if the declaration includes the Microsoft
4919 __declspec(naked) specifier. */
4920 set_irg_additional_property(irg, mtp_property_naked);
4922 if (decl_modifiers & DM_FORCEINLINE) {
4923 /* TRUE if the declaration includes the
4924 Microsoft __forceinline specifier. */
4925 set_irg_inline_property(irg, irg_inline_forced);
4927 if (decl_modifiers & DM_NOINLINE) {
4928 /* TRUE if the declaration includes the Microsoft
4929 __declspec(noinline) specifier. */
4930 set_irg_inline_property(irg, irg_inline_forbidden);
4935 * Create code for a function.
4937 static void create_function(declaration_t *declaration)
4939 ir_entity *function_entity = get_function_entity(declaration);
4941 if (declaration->init.statement == NULL)
4944 current_function_decl = declaration;
4945 current_function_name = NULL;
4946 current_funcsig = NULL;
4948 assert(imature_blocks == NULL);
4949 imature_blocks = NEW_ARR_F(ir_node*, 0);
4951 int n_local_vars = get_function_n_local_vars(declaration);
4952 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4954 set_irg_fp_model(irg, firm_opt.fp_model);
4955 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
4956 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
4958 ir_node *first_block = get_cur_block();
4960 /* set inline flags */
4961 if (declaration->is_inline)
4962 set_irg_inline_property(irg, irg_inline_recomended);
4963 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
4965 next_value_number_function = 0;
4966 initialize_function_parameters(declaration);
4968 statement_to_firm(declaration->init.statement);
4970 ir_node *end_block = get_irg_end_block(irg);
4972 /* do we have a return statement yet? */
4973 if (get_cur_block() != NULL) {
4974 type_t *type = skip_typeref(declaration->type);
4975 assert(is_type_function(type));
4976 const function_type_t *func_type = &type->function;
4977 const type_t *return_type
4978 = skip_typeref(func_type->return_type);
4981 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4982 ret = new_Return(get_store(), 0, NULL);
4985 if (is_type_scalar(return_type)) {
4986 mode = get_ir_mode(func_type->return_type);
4992 /* ยง5.1.2.2.3 main implicitly returns 0 */
4993 if (strcmp(declaration->symbol->string, "main") == 0) {
4994 in[0] = new_Const(mode, get_mode_null(mode));
4996 in[0] = new_Unknown(mode);
4997 if (warning.return_type) {
4998 warningf(&declaration->source_position,
4999 "missing return statement at end of non-void function '%Y'",
5000 declaration->symbol);
5003 ret = new_Return(get_store(), 1, in);
5005 add_immBlock_pred(end_block, ret);
5008 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
5009 mature_immBlock(imature_blocks[i]);
5011 DEL_ARR_F(imature_blocks);
5012 imature_blocks = NULL;
5014 mature_immBlock(first_block);
5015 mature_immBlock(end_block);
5017 irg_finalize_cons(irg);
5019 /* finalize the frame type */
5020 ir_type *frame_type = get_irg_frame_type(irg);
5021 int n = get_compound_n_members(frame_type);
5024 for(int i = 0; i < n; ++i) {
5025 ir_entity *entity = get_compound_member(frame_type, i);
5026 ir_type *entity_type = get_entity_type(entity);
5028 int align = get_type_alignment_bytes(entity_type);
5029 if (align > align_all)
5033 misalign = offset % align;
5035 offset += align - misalign;
5039 set_entity_offset(entity, offset);
5040 offset += get_type_size_bytes(entity_type);
5042 set_type_size_bytes(frame_type, offset);
5043 set_type_alignment_bytes(frame_type, align_all);
5048 static void scope_to_firm(scope_t *scope)
5050 /* first pass: create declarations */
5051 declaration_t *declaration = scope->declarations;
5052 for( ; declaration != NULL; declaration = declaration->next) {
5053 if (declaration->namespc != NAMESPACE_NORMAL)
5055 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5056 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5058 if (declaration->symbol == NULL)
5061 type_t *type = skip_typeref(declaration->type);
5062 if (is_type_function(type)) {
5063 get_function_entity(declaration);
5065 create_global_variable(declaration);
5069 /* second pass: create code/initializers */
5070 declaration = scope->declarations;
5071 for( ; declaration != NULL; declaration = declaration->next) {
5072 if (declaration->namespc != NAMESPACE_NORMAL)
5074 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5075 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5077 if (declaration->symbol == NULL)
5080 type_t *type = declaration->type;
5081 if (type->kind == TYPE_FUNCTION) {
5082 create_function(declaration);
5084 assert(declaration->declaration_kind
5085 == DECLARATION_KIND_GLOBAL_VARIABLE);
5086 current_ir_graph = get_const_code_irg();
5087 create_declaration_initializer(declaration);
5092 void init_ast2firm(void)
5094 obstack_init(&asm_obst);
5095 init_atomic_modes();
5097 id_underscore = new_id_from_chars("_", 1);
5098 id_imp = new_id_from_chars("__imp_", 6);
5100 /* OS option must be set to the backend */
5101 const char *s = "ia32-gasmode=linux";
5102 switch (firm_opt.os_support) {
5103 case OS_SUPPORT_MINGW:
5104 create_ld_ident = create_ld_ident_win32;
5105 s = "ia32-gasmode=mingw";
5107 case OS_SUPPORT_LINUX:
5108 create_ld_ident = create_ld_ident_linux_elf;
5109 s = "ia32-gasmode=linux"; break;
5111 case OS_SUPPORT_MACHO:
5112 create_ld_ident = create_ld_ident_macho;
5113 s = "ia32-gasmode=macho"; break;
5118 /* create idents for all known runtime functions */
5119 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5120 rts_idents[i] = new_id_from_str(rts_data[i].name);
5123 entitymap_init(&entitymap);
5126 static void init_ir_types(void)
5128 static int ir_types_initialized = 0;
5129 if (ir_types_initialized)
5131 ir_types_initialized = 1;
5133 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
5134 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5135 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5137 ir_type_int = get_ir_type(type_int);
5138 ir_type_const_char = get_ir_type(type_const_char);
5139 ir_type_wchar_t = get_ir_type(type_wchar_t);
5140 ir_type_void = get_ir_type(type_void);
5143 void exit_ast2firm(void)
5145 entitymap_destroy(&entitymap);
5146 obstack_free(&asm_obst, NULL);
5149 void translation_unit_to_firm(translation_unit_t *unit)
5151 /* just to be sure */
5152 continue_label = NULL;
5154 current_switch_cond = NULL;
5158 scope_to_firm(&unit->scope);