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;
66 static const declaration_t *current_function_decl;
67 static ir_node *current_function_name;
68 static ir_node *current_funcsig;
70 static entitymap_t entitymap;
72 static struct obstack asm_obst;
74 typedef enum declaration_kind_t {
75 DECLARATION_KIND_UNKNOWN,
76 DECLARATION_KIND_FUNCTION,
77 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
78 DECLARATION_KIND_GLOBAL_VARIABLE,
79 DECLARATION_KIND_LOCAL_VARIABLE,
80 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
81 DECLARATION_KIND_COMPOUND_MEMBER,
82 DECLARATION_KIND_LABEL_BLOCK,
83 DECLARATION_KIND_ENUM_ENTRY,
84 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
85 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
89 static ir_type *get_ir_type(type_t *type);
90 static ir_type *get_ir_type_incomplete(type_t *type);
91 static int count_decls_in_stmts(const statement_t *stmt);
93 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
95 const declaration_t *declaration = get_irg_loc_description(irg, pos);
97 warningf(&declaration->source_position,
98 "variable '%#T' might be used uninitialized",
99 declaration->type, declaration->symbol);
100 return new_r_Unknown(irg, mode);
103 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
105 const source_position_t *pos = (const source_position_t*) dbg;
108 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
112 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
114 const source_position_t *pos = (const source_position_t*) dbg;
119 return pos->input_name;
122 static dbg_info *get_dbg_info(const source_position_t *pos)
124 return (dbg_info*) pos;
127 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
129 static ir_mode *mode_int, *mode_uint;
131 static ir_node *_expression_to_firm(const expression_t *expression);
132 static ir_node *expression_to_firm(const expression_t *expression);
133 static inline ir_mode *get_ir_mode(type_t *type);
134 static void create_local_declaration(declaration_t *declaration);
136 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
138 unsigned flags = get_atomic_type_flags(kind);
139 unsigned size = get_atomic_type_size(kind);
140 if( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
141 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
144 unsigned bit_size = size * 8;
145 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
146 ir_mode_arithmetic arithmetic;
147 unsigned modulo_shift;
149 if(flags & ATOMIC_TYPE_FLAG_INTEGER) {
150 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
151 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
152 sort = irms_int_number;
153 arithmetic = irma_twos_complement;
154 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
156 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
157 snprintf(name, sizeof(name), "f%d", bit_size);
158 sort = irms_float_number;
159 arithmetic = irma_ieee754;
162 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
170 * Initialises the atomic modes depending on the machine size.
172 static void init_atomic_modes(void)
174 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
175 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
177 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
178 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
180 /* there's no real void type in firm */
181 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
183 /* initialize pointer modes */
185 ir_mode_sort sort = irms_reference;
186 unsigned bit_size = machine_size;
188 ir_mode_arithmetic arithmetic = irma_twos_complement;
189 unsigned modulo_shift
190 = bit_size < machine_size ? machine_size : bit_size;
192 snprintf(name, sizeof(name), "p%d", machine_size);
193 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
196 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
197 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
199 /* Hmm, pointers should be machine size */
200 set_modeP_data(ptr_mode);
201 set_modeP_code(ptr_mode);
204 static unsigned get_compound_type_size(compound_type_t *type)
206 ir_type *irtype = get_ir_type((type_t*) type);
207 return get_type_size_bytes(irtype);
210 static unsigned get_array_type_size(array_type_t *type)
212 assert(!type->is_vla);
213 ir_type *irtype = get_ir_type((type_t*) type);
214 return get_type_size_bytes(irtype);
218 static unsigned get_type_size_const(type_t *type)
220 type = skip_typeref(type);
224 panic("error type occurred");
226 return get_atomic_type_size(type->atomic.akind);
228 return 2 * get_atomic_type_size(type->complex.akind);
230 return get_atomic_type_size(type->imaginary.akind);
232 return get_mode_size_bytes(mode_int);
233 case TYPE_COMPOUND_UNION:
234 case TYPE_COMPOUND_STRUCT:
235 return get_compound_type_size(&type->compound);
237 /* just a pointer to the function */
238 return get_mode_size_bytes(mode_P_code);
240 return get_mode_size_bytes(mode_P_data);
242 return get_array_type_size(&type->array);
244 return get_type_size_const(type->builtin.real_type);
246 panic("type size of bitfield request");
252 panic("Trying to determine size of invalid type");
255 static ir_node *get_type_size(type_t *type)
257 type = skip_typeref(type);
259 if(is_type_array(type) && type->array.is_vla) {
260 ir_node *size_node = type->array.size_node;
261 if(size_node == NULL) {
262 size_node = expression_to_firm(type->array.size_expression);
263 assert(!is_Const(size_node));
264 type->array.size_node = size_node;
267 ir_node *elem_size = get_type_size(type->array.element_type);
268 ir_mode *mode = get_irn_mode(size_node);
269 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
273 ir_mode *mode = get_ir_mode(type_size_t);
275 sym.type_p = get_ir_type(type);
276 return new_SymConst(mode, sym, symconst_type_size);
279 static unsigned count_parameters(const function_type_t *function_type)
283 function_parameter_t *parameter = function_type->parameters;
284 for ( ; parameter != NULL; parameter = parameter->next) {
292 * Creates a Firm type for an atomic type
294 static ir_type *create_atomic_type(const atomic_type_t *type)
296 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
297 atomic_type_kind_t kind = type->akind;
298 ir_mode *mode = _atomic_modes[kind];
299 ident *id = get_mode_ident(mode);
300 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
302 set_type_alignment_bytes(irtype, type->base.alignment);
308 * Creates a Firm type for a complex type
310 static ir_type *create_complex_type(const complex_type_t *type)
312 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
313 atomic_type_kind_t kind = type->akind;
314 ir_mode *mode = _atomic_modes[kind];
315 ident *id = get_mode_ident(mode);
320 /* FIXME: finish the array */
325 * Creates a Firm type for an imaginary type
327 static ir_type *create_imaginary_type(const imaginary_type_t *type)
329 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
330 atomic_type_kind_t kind = type->akind;
331 ir_mode *mode = _atomic_modes[kind];
332 ident *id = get_mode_ident(mode);
333 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
335 set_type_alignment_bytes(irtype, type->base.alignment);
340 static ir_type *create_method_type(const function_type_t *function_type)
342 type_t *return_type = function_type->return_type;
344 ident *id = id_unique("functiontype.%u");
345 int n_parameters = count_parameters(function_type);
346 int n_results = return_type == type_void ? 0 : 1;
347 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
348 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
350 if (return_type != type_void) {
351 ir_type *restype = get_ir_type(return_type);
352 set_method_res_type(irtype, 0, restype);
355 function_parameter_t *parameter = function_type->parameters;
357 for ( ; parameter != NULL; parameter = parameter->next) {
358 ir_type *p_irtype = get_ir_type(parameter->type);
359 set_method_param_type(irtype, n, p_irtype);
363 if (function_type->variadic || function_type->unspecified_parameters) {
364 set_method_variadicity(irtype, variadicity_variadic);
370 static ir_type *create_pointer_type(pointer_type_t *type)
372 type_t *points_to = type->points_to;
373 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
374 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
375 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
376 ir_points_to, mode_P_data, dbgi);
381 static ir_type *create_array_type(array_type_t *type)
383 type_t *element_type = type->element_type;
384 ir_type *ir_element_type = get_ir_type(element_type);
386 ident *id = id_unique("array.%u");
387 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
388 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
390 const int align = get_type_alignment_bytes(ir_element_type);
391 set_type_alignment_bytes(ir_type, align);
393 if(type->size_constant) {
394 int n_elements = type->size;
396 set_array_bounds_int(ir_type, 0, 0, n_elements);
398 size_t elemsize = get_type_size_bytes(ir_element_type);
399 if(elemsize % align > 0) {
400 elemsize += align - (elemsize % align);
402 set_type_size_bytes(ir_type, n_elements * elemsize);
404 set_array_lower_bound_int(ir_type, 0, 0);
406 set_type_state(ir_type, layout_fixed);
412 * Return the signed integer type of size bits.
414 * @param size the size
416 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
419 static ir_mode *s_modes[64 + 1] = {NULL, };
423 if (size <= 0 || size > 64)
426 mode = s_modes[size];
430 snprintf(name, sizeof(name), "bf_I%u", size);
431 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
432 size <= 32 ? 32 : size );
433 s_modes[size] = mode;
437 snprintf(name, sizeof(name), "I%u", size);
438 ident *id = new_id_from_str(name);
439 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
440 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
441 set_primitive_base_type(res, base_tp);
447 * Return the unsigned integer type of size bits.
449 * @param size the size
451 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
454 static ir_mode *u_modes[64 + 1] = {NULL, };
458 if (size <= 0 || size > 64)
461 mode = u_modes[size];
465 snprintf(name, sizeof(name), "bf_U%u", size);
466 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
467 size <= 32 ? 32 : size );
468 u_modes[size] = mode;
473 snprintf(name, sizeof(name), "U%u", size);
474 ident *id = new_id_from_str(name);
475 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
476 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
477 set_primitive_base_type(res, base_tp);
482 static ir_type *create_bitfield_type(bitfield_type_t *const type)
484 type_t *base = skip_typeref(type->base_type);
485 assert(base->kind == TYPE_ATOMIC);
486 ir_type *irbase = get_ir_type(base);
488 unsigned size = fold_constant(type->size);
490 assert(!is_type_float(base));
491 if(is_type_signed(base)) {
492 return get_signed_int_type_for_bit_size(irbase, size);
494 return get_unsigned_int_type_for_bit_size(irbase, size);
498 #define INVALID_TYPE ((ir_type_ptr)-1)
501 COMPOUND_IS_STRUCT = false,
502 COMPOUND_IS_UNION = true
506 * Construct firm type from ast struct type.
508 * As anonymous inner structs get flattened to a single firm type, we might get
509 * irtype, outer_offset and out_align passed (they represent the position of
510 * the anonymous inner struct inside the resulting firm struct)
512 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
513 size_t *outer_offset, size_t *outer_align,
514 bool incomplete, bool is_union)
516 declaration_t *declaration = type->declaration;
517 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
519 if(kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
520 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
522 return declaration->v.irtype;
524 size_t align_all = 1;
526 size_t bit_offset = 0;
530 symbol_t *symbol = declaration->symbol;
533 id = new_id_from_str(symbol->string);
536 id = id_unique("__anonymous_union.%u");
538 id = id_unique("__anonymous_struct.%u");
541 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
544 irtype = new_d_type_union(id, dbgi);
546 irtype = new_d_type_struct(id, dbgi);
549 declaration->declaration_kind
550 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
551 declaration->v.irtype = irtype;
552 //type->base.firm_type = irtype;
554 offset = *outer_offset;
555 align_all = *outer_align;
561 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
563 declaration_t *entry = declaration->scope.declarations;
564 for( ; entry != NULL; entry = entry->next) {
565 if(entry->namespc != NAMESPACE_NORMAL)
568 size_t prev_offset = offset;
570 symbol_t *symbol = entry->symbol;
571 type_t *entry_type = skip_typeref(entry->type);
572 dbg_info *dbgi = get_dbg_info(&entry->source_position);
576 ident = new_id_from_str(symbol->string);
578 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
579 create_compound_type(&entry_type->compound, irtype, &offset,
580 &align_all, false, COMPOUND_IS_STRUCT);
581 goto finished_member;
582 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
583 create_compound_type(&entry_type->compound, irtype, &offset,
584 &align_all, false, COMPOUND_IS_UNION);
585 goto finished_member;
587 assert(entry_type->kind == TYPE_BITFIELD);
589 ident = id_unique("anon.%u");
592 ir_type *base_irtype;
593 if(entry_type->kind == TYPE_BITFIELD) {
594 base_irtype = get_ir_type(entry_type->bitfield.base_type);
596 base_irtype = get_ir_type(entry_type);
599 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
600 size_t misalign = offset % entry_alignment;
602 ir_type *entry_irtype = get_ir_type(entry_type);
603 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
606 size_t bits_remainder;
607 if(entry_type->kind == TYPE_BITFIELD) {
608 size_t size_bits = fold_constant(entry_type->bitfield.size);
609 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
611 if(size_bits > rest_size_bits) {
612 /* start a new bucket */
613 offset += entry_alignment - misalign;
619 /* put into current bucket */
620 base = offset - misalign;
621 bits_remainder = misalign * 8 + bit_offset;
624 offset += size_bits / 8;
625 bit_offset = bit_offset + (size_bits % 8);
627 size_t entry_size = get_type_size_bytes(base_irtype);
628 if(misalign > 0 || bit_offset > 0)
629 offset += entry_alignment - misalign;
633 offset += entry_size;
637 if(entry_alignment > align_all) {
638 if(entry_alignment % align_all != 0) {
639 panic("uneven alignments not supported yet");
641 align_all = entry_alignment;
644 set_entity_offset(entity, base);
645 set_entity_offset_bits_remainder(entity,
646 (unsigned char) bits_remainder);
647 //add_struct_member(irtype, entity);
648 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
649 assert(entry->v.entity == NULL);
650 entry->v.entity = entity;
654 size_t entry_size = offset - prev_offset;
655 if (entry_size > size) {
667 size_t misalign = offset % align_all;
668 if(misalign > 0 || bit_offset > 0) {
669 size += align_all - misalign;
672 if(outer_offset != NULL) {
674 *outer_offset = offset;
676 *outer_offset += size;
679 if (align_all > *outer_align) {
680 if(align_all % *outer_align != 0) {
681 panic("uneven alignments not supported yet");
683 *outer_align = align_all;
686 set_type_alignment_bytes(irtype, align_all);
687 set_type_size_bytes(irtype, size);
688 set_type_state(irtype, layout_fixed);
694 static ir_type *create_enum_type(enum_type_t *const type)
696 type->base.firm_type = ir_type_int;
698 ir_mode *const mode = mode_int;
699 tarval *const one = get_mode_one(mode);
700 tarval * tv_next = get_tarval_null(mode);
702 declaration_t *declaration = type->declaration->next;
703 for (; declaration != NULL; declaration = declaration->next) {
704 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
707 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
709 expression_t *const init = declaration->init.enum_value;
711 ir_node *const cnst = expression_to_firm(init);
712 if (!is_Const(cnst)) {
713 panic("couldn't fold constant");
715 tv_next = get_Const_tarval(cnst);
717 declaration->v.enum_val = tv_next;
718 tv_next = tarval_add(tv_next, one);
724 static ir_type *get_ir_type_incomplete(type_t *type)
726 assert(type != NULL);
727 type = skip_typeref(type);
729 if (type->base.firm_type != NULL) {
730 assert(type->base.firm_type != INVALID_TYPE);
731 return type->base.firm_type;
734 switch (type->kind) {
735 case TYPE_COMPOUND_STRUCT:
736 return create_compound_type(&type->compound, NULL, NULL, NULL,
737 true, COMPOUND_IS_STRUCT);
739 case TYPE_COMPOUND_UNION:
740 return create_compound_type(&type->compound, NULL, NULL, NULL,
741 true, COMPOUND_IS_UNION);
743 return get_ir_type(type);
747 static ir_type *get_ir_type(type_t *type)
749 assert(type != NULL);
751 type = skip_typeref(type);
753 if (type->base.firm_type != NULL) {
754 assert(type->base.firm_type != INVALID_TYPE);
755 return type->base.firm_type;
758 ir_type *firm_type = NULL;
759 switch (type->kind) {
761 panic("error type occurred");
763 firm_type = create_atomic_type(&type->atomic);
766 firm_type = create_complex_type(&type->complex);
769 firm_type = create_imaginary_type(&type->imaginary);
772 firm_type = create_method_type(&type->function);
775 firm_type = create_pointer_type(&type->pointer);
778 firm_type = create_array_type(&type->array);
780 case TYPE_COMPOUND_STRUCT:
781 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
782 false, COMPOUND_IS_STRUCT);
784 case TYPE_COMPOUND_UNION:
785 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
786 false, COMPOUND_IS_UNION);
789 firm_type = create_enum_type(&type->enumt);
792 firm_type = get_ir_type(type->builtin.real_type);
795 firm_type = create_bitfield_type(&type->bitfield);
803 if(firm_type == NULL)
804 panic("unknown type found");
806 type->base.firm_type = firm_type;
810 static inline ir_mode *get_ir_mode(type_t *type)
812 ir_type *irtype = get_ir_type(type);
814 /* firm doesn't report a mode for arrays somehow... */
815 if(is_Array_type(irtype)) {
819 ir_mode *mode = get_type_mode(irtype);
820 assert(mode != NULL);
824 /** Names of the runtime functions. */
825 static const struct {
826 int id; /**< the rts id */
827 int n_res; /**< number of return values */
828 const char *name; /**< the name of the rts function */
829 int n_params; /**< number of parameters */
830 unsigned flags; /**< language flags */
832 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
833 { rts_abort, 0, "abort", 0, _C89 },
834 { rts_alloca, 1, "alloca", 1, _ALL },
835 { rts_abs, 1, "abs", 1, _C89 },
836 { rts_labs, 1, "labs", 1, _C89 },
837 { rts_llabs, 1, "llabs", 1, _C99 },
838 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
840 { rts_fabs, 1, "fabs", 1, _C89 },
841 { rts_sqrt, 1, "sqrt", 1, _C89 },
842 { rts_cbrt, 1, "cbrt", 1, _C99 },
843 { rts_exp, 1, "exp", 1, _C89 },
844 { rts_exp2, 1, "exp2", 1, _C89 },
845 { rts_exp10, 1, "exp10", 1, _GNUC },
846 { rts_log, 1, "log", 1, _C89 },
847 { rts_log2, 1, "log2", 1, _C89 },
848 { rts_log10, 1, "log10", 1, _C89 },
849 { rts_pow, 1, "pow", 2, _C89 },
850 { rts_sin, 1, "sin", 1, _C89 },
851 { rts_cos, 1, "cos", 1, _C89 },
852 { rts_tan, 1, "tan", 1, _C89 },
853 { rts_asin, 1, "asin", 1, _C89 },
854 { rts_acos, 1, "acos", 1, _C89 },
855 { rts_atan, 1, "atan", 1, _C89 },
856 { rts_sinh, 1, "sinh", 1, _C89 },
857 { rts_cosh, 1, "cosh", 1, _C89 },
858 { rts_tanh, 1, "tanh", 1, _C89 },
860 { rts_fabsf, 1, "fabsf", 1, _C99 },
861 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
862 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
863 { rts_expf, 1, "expf", 1, _C99 },
864 { rts_exp2f, 1, "exp2f", 1, _C99 },
865 { rts_exp10f, 1, "exp10f", 1, _GNUC },
866 { rts_logf, 1, "logf", 1, _C99 },
867 { rts_log2f, 1, "log2f", 1, _C99 },
868 { rts_log10f, 1, "log10f", 1, _C99 },
869 { rts_powf, 1, "powf", 2, _C99 },
870 { rts_sinf, 1, "sinf", 1, _C99 },
871 { rts_cosf, 1, "cosf", 1, _C99 },
872 { rts_tanf, 1, "tanf", 1, _C99 },
873 { rts_asinf, 1, "asinf", 1, _C99 },
874 { rts_acosf, 1, "acosf", 1, _C99 },
875 { rts_atanf, 1, "atanf", 1, _C99 },
876 { rts_sinhf, 1, "sinhf", 1, _C99 },
877 { rts_coshf, 1, "coshf", 1, _C99 },
878 { rts_tanhf, 1, "tanhf", 1, _C99 },
880 { rts_fabsl, 1, "fabsl", 1, _C99 },
881 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
882 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
883 { rts_expl, 1, "expl", 1, _C99 },
884 { rts_exp2l, 1, "exp2l", 1, _C99 },
885 { rts_exp10l, 1, "exp10l", 1, _GNUC },
886 { rts_logl, 1, "logl", 1, _C99 },
887 { rts_log2l, 1, "log2l", 1, _C99 },
888 { rts_log10l, 1, "log10l", 1, _C99 },
889 { rts_powl, 1, "powl", 2, _C99 },
890 { rts_sinl, 1, "sinl", 1, _C99 },
891 { rts_cosl, 1, "cosl", 1, _C99 },
892 { rts_tanl, 1, "tanl", 1, _C99 },
893 { rts_asinl, 1, "asinl", 1, _C99 },
894 { rts_acosl, 1, "acosl", 1, _C99 },
895 { rts_atanl, 1, "atanl", 1, _C99 },
896 { rts_sinhl, 1, "sinhl", 1, _C99 },
897 { rts_coshl, 1, "coshl", 1, _C99 },
898 { rts_tanhl, 1, "tanhl", 1, _C99 },
900 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
901 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
902 { rts_strcmp, 1, "strcmp", 2, _C89 },
903 { rts_strncmp, 1, "strncmp", 3, _C89 }
906 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
909 * Mangles an entity linker (ld) name for win32 usage.
911 * @param ent the entity to be mangled
912 * @param declaration the declaration
914 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
918 if (is_Method_type(get_entity_type(ent)))
919 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
921 /* always add an underscore in win32 */
922 id = mangle(id_underscore, get_entity_ident(ent));
925 decl_modifiers_t decl_modifiers = declaration->decl_modifiers;
926 if (decl_modifiers & DM_DLLIMPORT) {
927 /* add prefix for imported symbols */
928 id = mangle(id_imp, id);
934 * Mangles an entity linker (ld) name for Linux ELF usage.
936 * @param ent the entity to be mangled
937 * @param declaration the declaration
939 static ident *create_ld_ident_linux_elf(ir_entity *entity,
940 declaration_t *declaration)
943 return get_entity_ident(entity);
947 * Mangles an entity linker (ld) name for Mach-O usage.
949 * @param ent the entity to be mangled
950 * @param declaration the declaration
952 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
955 ident *id = mangle(id_underscore, get_entity_ident(ent));
959 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
960 declaration_t *declaration);
961 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
964 * Creates an entity representing a function.
966 * @param declaration the function declaration
968 static ir_entity *get_function_entity(declaration_t *declaration)
970 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
971 return declaration->v.entity;
972 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
974 symbol_t *symbol = declaration->symbol;
975 ident *id = new_id_from_str(symbol->string);
977 ir_type *global_type = get_glob_type();
978 ir_type *ir_type_method = get_ir_type(declaration->type);
979 assert(is_Method_type(ir_type_method));
981 /* already an entity defined? */
982 ir_entity *entity = entitymap_get(&entitymap, symbol);
986 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
987 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
988 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
989 if(declaration->storage_class == STORAGE_CLASS_STATIC &&
990 declaration->init.statement == NULL) {
991 /* this entity was declared, but never defined */
992 set_entity_peculiarity(entity, peculiarity_description);
994 if(declaration->storage_class == STORAGE_CLASS_STATIC
995 || declaration->is_inline) {
996 set_entity_visibility(entity, visibility_local);
997 } else if(declaration->init.statement != NULL) {
998 set_entity_visibility(entity, visibility_external_visible);
1000 set_entity_visibility(entity, visibility_external_allocated);
1002 set_entity_allocation(entity, allocation_static);
1004 /* We should check for file scope here, but as long as we compile C only
1005 this is not needed. */
1006 if (! firm_opt.freestanding) {
1007 /* check for a known runtime function */
1008 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1009 if (id != rts_idents[i])
1012 /* ignore those rts functions not necessary needed for current mode */
1013 if ((c_mode & rts_data[i].flags) == 0)
1015 assert(rts_entities[rts_data[i].id] == NULL);
1016 rts_entities[rts_data[i].id] = entity;
1020 entitymap_insert(&entitymap, symbol, entity);
1023 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1024 declaration->v.entity = entity;
1030 * Creates a Const node representing a constant.
1032 static ir_node *const_to_firm(const const_expression_t *cnst)
1034 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1035 ir_mode *mode = get_ir_mode(cnst->base.type);
1040 if(mode_is_float(mode)) {
1041 tv = new_tarval_from_double(cnst->v.float_value, mode);
1043 if(mode_is_signed(mode)) {
1044 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1046 len = snprintf(buf, sizeof(buf), "%llu",
1047 (unsigned long long) cnst->v.int_value);
1049 tv = new_tarval_from_str(buf, len, mode);
1052 return new_d_Const(dbgi, mode, tv);
1056 * Creates a Const node representing a character constant.
1058 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1060 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1061 ir_mode *mode = get_ir_mode(cnst->base.type);
1063 long long int v = 0;
1064 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1065 if (char_is_signed) {
1066 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1068 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1072 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1073 tarval *tv = new_tarval_from_str(buf, len, mode);
1075 return new_d_Const(dbgi, mode, tv);
1079 * Creates a Const node representing a wide character constant.
1081 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1083 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1084 ir_mode *mode = get_ir_mode(cnst->base.type);
1086 long long int v = cnst->v.wide_character.begin[0];
1089 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1090 tarval *tv = new_tarval_from_str(buf, len, mode);
1092 return new_d_Const(dbgi, mode, tv);
1096 * Creates a SymConst for a given entity.
1098 * @param dbgi debug info
1099 * @param mode the (reference) mode for the SymConst
1100 * @param entity the entity
1102 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1105 assert(entity != NULL);
1106 union symconst_symbol sym;
1107 sym.entity_p = entity;
1108 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1112 * Creates a SymConst node representing a string constant.
1114 * @param src_pos the source position of the string constant
1115 * @param id_prefix a prefix for the name of the generated string constant
1116 * @param value the value of the string constant
1118 static ir_node *string_to_firm(const source_position_t *const src_pos,
1119 const char *const id_prefix,
1120 const string_t *const value)
1122 ir_type *const global_type = get_glob_type();
1123 dbg_info *const dbgi = get_dbg_info(src_pos);
1124 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1125 ir_type_const_char, dbgi);
1127 ident *const id = id_unique(id_prefix);
1128 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1129 set_entity_ld_ident(entity, id);
1130 set_entity_variability(entity, variability_constant);
1131 set_entity_allocation(entity, allocation_static);
1133 ir_type *const elem_type = ir_type_const_char;
1134 ir_mode *const mode = get_type_mode(elem_type);
1136 const char* const string = value->begin;
1137 const size_t slen = value->size;
1139 set_array_lower_bound_int(type, 0, 0);
1140 set_array_upper_bound_int(type, 0, slen);
1141 set_type_size_bytes(type, slen);
1142 set_type_state(type, layout_fixed);
1144 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1145 for(size_t i = 0; i < slen; ++i) {
1146 tvs[i] = new_tarval_from_long(string[i], mode);
1149 set_array_entity_values(entity, tvs, slen);
1152 return create_symconst(dbgi, mode_P_data, entity);
1156 * Creates a SymConst node representing a string literal.
1158 * @param literal the string literal
1160 static ir_node *string_literal_to_firm(
1161 const string_literal_expression_t* literal)
1163 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1168 * Creates a SymConst node representing a wide string literal.
1170 * @param literal the wide string literal
1172 static ir_node *wide_string_literal_to_firm(
1173 const wide_string_literal_expression_t* const literal)
1175 ir_type *const global_type = get_glob_type();
1176 ir_type *const elem_type = ir_type_wchar_t;
1177 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1178 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1181 ident *const id = id_unique("Lstr.%u");
1182 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1183 set_entity_ld_ident(entity, id);
1184 set_entity_variability(entity, variability_constant);
1185 set_entity_allocation(entity, allocation_static);
1187 ir_mode *const mode = get_type_mode(elem_type);
1189 const wchar_rep_t *const string = literal->value.begin;
1190 const size_t slen = literal->value.size;
1192 set_array_lower_bound_int(type, 0, 0);
1193 set_array_upper_bound_int(type, 0, slen);
1194 set_type_size_bytes(type, slen);
1195 set_type_state(type, layout_fixed);
1197 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1198 for(size_t i = 0; i < slen; ++i) {
1199 tvs[i] = new_tarval_from_long(string[i], mode);
1202 set_array_entity_values(entity, tvs, slen);
1205 return create_symconst(dbgi, mode_P_data, entity);
1208 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1209 ir_node *const addr)
1211 ir_type *irtype = get_ir_type(type);
1212 if (is_compound_type(irtype)
1213 || is_Method_type(irtype)
1214 || is_Array_type(irtype)) {
1218 ir_mode *const mode = get_type_mode(irtype);
1219 ir_node *const memory = get_store();
1220 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1221 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1222 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1224 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1225 set_Load_volatility(load, volatility_is_volatile);
1228 set_store(load_mem);
1233 * Creates a strict Conv if neccessary.
1235 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1237 ir_mode *mode = get_irn_mode(node);
1239 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1241 if (!mode_is_float(mode))
1244 /* check if there is already a Conv */
1245 if (is_Conv(node)) {
1246 /* convert it into a strict Conv */
1247 set_Conv_strict(node, 1);
1251 /* otherwise create a new one */
1252 return new_d_strictConv(dbgi, node, mode);
1255 static ir_node *get_global_var_address(dbg_info *const dbgi,
1256 const declaration_t *const decl)
1258 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1260 ir_entity *const entity = decl->v.entity;
1261 switch ((storage_class_tag_t)decl->storage_class) {
1262 case STORAGE_CLASS_THREAD:
1263 case STORAGE_CLASS_THREAD_EXTERN:
1264 case STORAGE_CLASS_THREAD_STATIC: {
1265 ir_node *const no_mem = new_NoMem();
1266 ir_node *const tls = get_irg_tls(current_ir_graph);
1267 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1271 return create_symconst(dbgi, mode_P_data, entity);
1276 * Returns the correct base address depending on whether it is a parameter or a
1277 * normal local variable.
1279 static ir_node *get_local_frame(ir_entity *const ent)
1281 ir_graph *const irg = current_ir_graph;
1282 const ir_type *const owner = get_entity_owner(ent);
1283 if (owner == get_irg_frame_type(irg)) {
1284 return get_irg_frame(irg);
1286 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1287 return get_irg_value_param_base(irg);
1291 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1293 ir_mode *value_mode = get_irn_mode(value);
1295 if (value_mode == dest_mode || is_Bad(value))
1298 if(dest_mode == mode_b) {
1299 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1300 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1301 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1305 return new_d_Conv(dbgi, value, dest_mode);
1308 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1310 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1311 declaration_t *declaration = ref->declaration;
1312 type_t *type = skip_typeref(declaration->type);
1314 /* make sure the type is constructed */
1315 (void) get_ir_type(type);
1317 switch((declaration_kind_t) declaration->declaration_kind) {
1318 case DECLARATION_KIND_TYPE:
1319 case DECLARATION_KIND_UNKNOWN:
1322 case DECLARATION_KIND_ENUM_ENTRY: {
1323 ir_mode *const mode = get_ir_mode(type);
1324 return new_Const(mode, declaration->v.enum_val);
1327 case DECLARATION_KIND_LOCAL_VARIABLE: {
1328 ir_mode *const mode = get_ir_mode(type);
1329 return get_value(declaration->v.value_number, mode);
1331 case DECLARATION_KIND_FUNCTION: {
1332 ir_mode *const mode = get_ir_mode(type);
1333 return create_symconst(dbgi, mode, declaration->v.entity);
1335 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1336 ir_node *const addr = get_global_var_address(dbgi, declaration);
1337 return deref_address(dbgi, declaration->type, addr);
1340 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1341 ir_entity *entity = declaration->v.entity;
1342 ir_node *frame = get_local_frame(entity);
1343 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1344 return deref_address(dbgi, declaration->type, sel);
1347 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1348 return declaration->v.vla_base;
1350 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1351 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1352 case DECLARATION_KIND_COMPOUND_MEMBER:
1353 case DECLARATION_KIND_LABEL_BLOCK:
1354 panic("not implemented reference type");
1357 panic("reference to declaration with unknown type found");
1360 static ir_node *reference_addr(const reference_expression_t *ref)
1362 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1363 declaration_t *declaration = ref->declaration;
1365 switch((declaration_kind_t) declaration->declaration_kind) {
1366 case DECLARATION_KIND_TYPE:
1367 case DECLARATION_KIND_UNKNOWN:
1369 case DECLARATION_KIND_LOCAL_VARIABLE:
1370 /* you can store to a local variable (so we don't panic but return NULL
1371 * as an indicator for no real address) */
1373 case DECLARATION_KIND_FUNCTION: {
1374 type_t *const type = skip_typeref(ref->base.type);
1375 ir_mode *const mode = get_ir_mode(type);
1376 return create_symconst(dbgi, mode, declaration->v.entity);
1378 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1379 ir_node *const addr = get_global_var_address(dbgi, declaration);
1382 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1383 ir_entity *entity = declaration->v.entity;
1384 ir_node *frame = get_local_frame(entity);
1385 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1390 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1391 return declaration->v.vla_base;
1393 case DECLARATION_KIND_ENUM_ENTRY:
1394 panic("trying to reference enum entry");
1396 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1397 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1398 case DECLARATION_KIND_COMPOUND_MEMBER:
1399 case DECLARATION_KIND_LABEL_BLOCK:
1400 panic("not implemented reference type");
1403 panic("reference to declaration with unknown type found");
1407 * Transform calls to builtin functions.
1409 static ir_node *process_builtin_call(const call_expression_t *call)
1411 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1413 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1414 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1416 type_t *type = skip_typeref(builtin->base.type);
1417 assert(is_type_pointer(type));
1419 type_t *function_type = skip_typeref(type->pointer.points_to);
1420 symbol_t *symbol = builtin->symbol;
1422 switch(symbol->ID) {
1423 case T___builtin_alloca: {
1424 if(call->arguments == NULL || call->arguments->next != NULL) {
1425 panic("invalid number of parameters on __builtin_alloca");
1427 expression_t *argument = call->arguments->expression;
1428 ir_node *size = expression_to_firm(argument);
1430 ir_node *store = get_store();
1431 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1433 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1435 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1439 case T___builtin_huge_val: {
1440 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1441 tarval *tv = get_mode_infinite(mode);
1442 ir_node *res = new_d_Const(dbgi, mode, tv);
1445 case T___builtin_nan:
1446 case T___builtin_nanf:
1447 case T___builtin_nand: {
1448 /* Ignore string for now... */
1449 assert(is_type_function(function_type));
1450 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1451 tarval *tv = get_mode_NAN(mode);
1452 ir_node *res = new_d_Const(dbgi, mode, tv);
1455 case T___builtin_va_end:
1458 panic("Unsupported builtin found\n");
1463 * Transform a call expression.
1464 * Handles some special cases, like alloca() calls, which must be resolved BEFORE the inlines runs.
1465 * Inlining routines calling alloca() is dangerous, 176.gcc for instance might allocate 2GB instead of
1466 * 256 MB if alloca is not handled right...
1468 static ir_node *call_expression_to_firm(const call_expression_t *call)
1470 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1471 assert(get_cur_block() != NULL);
1473 expression_t *function = call->function;
1474 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1475 return process_builtin_call(call);
1477 if(function->kind == EXPR_REFERENCE) {
1478 const reference_expression_t *ref = &function->reference;
1479 declaration_t *declaration = ref->declaration;
1481 if((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1482 if (declaration->v.entity == rts_entities[rts_alloca]) {
1483 /* handle alloca() call */
1484 expression_t *argument = call->arguments->expression;
1485 ir_node *size = expression_to_firm(argument);
1487 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1489 ir_node *store = get_store();
1490 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1491 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1493 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1495 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1501 ir_node *callee = expression_to_firm(function);
1503 type_t *type = skip_typeref(function->base.type);
1504 assert(is_type_pointer(type));
1505 pointer_type_t *pointer_type = &type->pointer;
1506 type_t *points_to = skip_typeref(pointer_type->points_to);
1507 assert(is_type_function(points_to));
1508 function_type_t *function_type = &points_to->function;
1510 int n_parameters = 0;
1511 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1512 ir_type *new_method_type = NULL;
1513 if(function_type->variadic || function_type->unspecified_parameters) {
1514 const call_argument_t *argument = call->arguments;
1515 for( ; argument != NULL; argument = argument->next) {
1519 /* we need to construct a new method type matching the call
1521 int n_res = get_method_n_ress(ir_method_type);
1522 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1523 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1524 n_parameters, n_res, dbgi);
1525 set_method_calling_convention(new_method_type,
1526 get_method_calling_convention(ir_method_type));
1527 set_method_additional_properties(new_method_type,
1528 get_method_additional_properties(ir_method_type));
1529 set_method_variadicity(new_method_type,
1530 get_method_variadicity(ir_method_type));
1532 for(int i = 0; i < n_res; ++i) {
1533 set_method_res_type(new_method_type, i,
1534 get_method_res_type(ir_method_type, i));
1536 argument = call->arguments;
1537 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1538 expression_t *expression = argument->expression;
1539 ir_type *irtype = get_ir_type(expression->base.type);
1540 set_method_param_type(new_method_type, i, irtype);
1542 ir_method_type = new_method_type;
1544 n_parameters = get_method_n_params(ir_method_type);
1547 ir_node *in[n_parameters];
1549 const call_argument_t *argument = call->arguments;
1550 for(int n = 0; n < n_parameters; ++n) {
1551 expression_t *expression = argument->expression;
1552 ir_node *arg_node = expression_to_firm(expression);
1554 arg_node = do_strict_conv(dbgi, arg_node);
1558 argument = argument->next;
1561 ir_node *store = get_store();
1562 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1564 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1567 type_t *return_type = skip_typeref(function_type->return_type);
1568 ir_node *result = NULL;
1570 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1572 if(is_type_scalar(return_type)) {
1573 mode = get_ir_mode(return_type);
1577 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1578 result = new_d_Proj(dbgi, resproj, mode, 0);
1584 static void statement_to_firm(statement_t *statement);
1585 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1587 static ir_node *expression_to_addr(const expression_t *expression);
1588 static void create_condition_evaluation(const expression_t *expression,
1589 ir_node *true_block,
1590 ir_node *false_block);
1592 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1595 value = do_strict_conv(dbgi, value);
1597 ir_node *memory = get_store();
1599 if(is_type_scalar(type)) {
1600 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1601 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1602 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1603 set_Store_volatility(store, volatility_is_volatile);
1604 set_store(store_mem);
1606 ir_type *irtype = get_ir_type(type);
1607 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1608 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1609 set_store(copyb_mem);
1613 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1615 tarval *all_one = get_mode_all_one(mode);
1616 int mode_size = get_mode_size_bits(mode);
1618 assert(offset >= 0 && size >= 0);
1619 assert(offset + size <= mode_size);
1620 if(size == mode_size) {
1624 long shiftr = get_mode_size_bits(mode) - size;
1625 long shiftl = offset;
1626 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1627 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1628 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1629 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1634 static void bitfield_store_to_firm(const select_expression_t *expression,
1635 ir_node *addr, ir_node *value)
1637 type_t *type = expression->base.type;
1638 ir_mode *mode = get_ir_mode(type);
1640 assert(get_irn_mode(value) == mode || is_Bad(value));
1642 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1644 /* kill upper bits of value and shift to right position */
1645 ir_entity *entity = expression->compound_entry->v.entity;
1646 int bitoffset = get_entity_offset_bits_remainder(entity);
1647 ir_type *entity_type = get_entity_type(entity);
1648 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1650 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1651 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1652 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1653 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1654 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1655 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1657 /* load current value */
1658 ir_node *mem = get_store();
1659 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1660 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1661 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1662 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1663 tarval *inv_mask = tarval_not(shift_mask);
1664 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1665 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1667 /* construct new value and store */
1668 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1669 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1670 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1671 set_store(store_mem);
1673 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1674 set_Load_volatility(load, volatility_is_volatile);
1675 set_Store_volatility(store, volatility_is_volatile);
1679 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1682 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1683 type_t *type = expression->base.type;
1684 ir_mode *mode = get_ir_mode(type);
1685 ir_node *mem = get_store();
1686 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1687 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1688 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1690 load_res = create_conv(dbgi, load_res, mode_int);
1692 set_store(load_mem);
1694 /* kill upper bits */
1695 ir_entity *entity = expression->compound_entry->v.entity;
1696 int bitoffset = get_entity_offset_bits_remainder(entity);
1697 ir_type *entity_type = get_entity_type(entity);
1698 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1699 long shift_bitsl = machine_size - bitoffset - bitsize;
1700 assert(shift_bitsl >= 0);
1701 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1702 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1703 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1705 long shift_bitsr = bitoffset + shift_bitsl;
1706 assert(shift_bitsr <= (long) machine_size);
1707 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1708 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1710 if(mode_is_signed(mode)) {
1711 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1713 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1716 return create_conv(dbgi, shiftr, mode);
1719 static void set_value_for_expression_addr(const expression_t *expression,
1720 ir_node *value, ir_node *addr)
1722 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1723 value = do_strict_conv(dbgi, value);
1725 if (expression->kind == EXPR_REFERENCE) {
1726 const reference_expression_t *ref = &expression->reference;
1728 declaration_t *declaration = ref->declaration;
1729 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1730 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1731 set_value(declaration->v.value_number, value);
1737 addr = expression_to_addr(expression);
1739 type_t *type = skip_typeref(expression->base.type);
1741 if (expression->kind == EXPR_SELECT) {
1742 const select_expression_t *select = &expression->select;
1744 declaration_t *declaration = select->compound_entry;
1745 if (declaration->type->kind == TYPE_BITFIELD) {
1746 bitfield_store_to_firm(select, addr, value);
1751 assign_value(dbgi, addr, type, value);
1754 static void set_value_for_expression(const expression_t *expression,
1757 set_value_for_expression_addr(expression, value, NULL);
1760 static ir_node *get_value_from_lvalue(const expression_t *expression,
1763 if (expression->kind == EXPR_REFERENCE) {
1764 const reference_expression_t *ref = &expression->reference;
1766 declaration_t *declaration = ref->declaration;
1767 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1768 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1769 assert(addr == NULL);
1770 ir_mode *mode = get_ir_mode(expression->base.type);
1771 return get_value(declaration->v.value_number, mode);
1775 assert(addr != NULL);
1776 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1779 if (expression->kind == EXPR_SELECT &&
1780 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1781 value = bitfield_extract_to_firm(&expression->select, addr);
1783 value = deref_address(dbgi, expression->base.type, addr);
1790 static ir_node *create_incdec(const unary_expression_t *expression)
1792 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1793 const expression_t *value_expr = expression->value;
1794 ir_node *addr = expression_to_addr(value_expr);
1795 ir_node *value = get_value_from_lvalue(value_expr, addr);
1797 type_t *type = skip_typeref(expression->base.type);
1798 ir_mode *mode = get_ir_mode(expression->base.type);
1801 if(is_type_pointer(type)) {
1802 pointer_type_t *pointer_type = &type->pointer;
1803 offset = get_type_size(pointer_type->points_to);
1805 assert(is_type_arithmetic(type));
1806 offset = new_Const(mode, get_mode_one(mode));
1810 ir_node *store_value;
1811 switch(expression->base.kind) {
1812 case EXPR_UNARY_POSTFIX_INCREMENT:
1814 store_value = new_d_Add(dbgi, value, offset, mode);
1816 case EXPR_UNARY_POSTFIX_DECREMENT:
1818 store_value = new_d_Sub(dbgi, value, offset, mode);
1820 case EXPR_UNARY_PREFIX_INCREMENT:
1821 result = new_d_Add(dbgi, value, offset, mode);
1822 store_value = result;
1824 case EXPR_UNARY_PREFIX_DECREMENT:
1825 result = new_d_Sub(dbgi, value, offset, mode);
1826 store_value = result;
1829 panic("no incdec expr in create_incdec");
1832 set_value_for_expression_addr(value_expr, store_value, addr);
1837 static bool is_local_variable(expression_t *expression)
1839 if (expression->kind != EXPR_REFERENCE)
1841 reference_expression_t *ref_expr = &expression->reference;
1842 declaration_t *declaration = ref_expr->declaration;
1843 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1846 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1849 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1850 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1851 case EXPR_BINARY_NOTEQUAL:
1852 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1853 case EXPR_BINARY_ISLESS:
1854 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1855 case EXPR_BINARY_ISLESSEQUAL:
1856 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1857 case EXPR_BINARY_ISGREATER:
1858 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1859 case EXPR_BINARY_ISGREATEREQUAL:
1860 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1861 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1866 panic("trying to get pn_Cmp from non-comparison binexpr type");
1870 * Handle the assume optimizer hint: check if a Confirm
1871 * node can be created.
1873 * @param dbi debug info
1874 * @param expr the IL assume expression
1876 * we support here only some simple cases:
1881 static ir_node *handle_assume_compare(dbg_info *dbi,
1882 const binary_expression_t *expression)
1884 expression_t *op1 = expression->left;
1885 expression_t *op2 = expression->right;
1886 declaration_t *var2, *var = NULL;
1887 ir_node *res = NULL;
1890 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1892 if (is_local_variable(op1) && is_local_variable(op2)) {
1893 var = op1->reference.declaration;
1894 var2 = op2->reference.declaration;
1896 type_t *const type = skip_typeref(var->type);
1897 ir_mode *const mode = get_ir_mode(type);
1899 ir_node *const irn1 = get_value(var->v.value_number, mode);
1900 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1902 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1903 set_value(var2->v.value_number, res);
1905 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1906 set_value(var->v.value_number, res);
1912 if (is_local_variable(op1) && is_constant_expression(op2)) {
1913 var = op1->reference.declaration;
1915 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1916 cmp_val = get_inversed_pnc(cmp_val);
1917 var = op2->reference.declaration;
1922 type_t *const type = skip_typeref(var->type);
1923 ir_mode *const mode = get_ir_mode(type);
1925 res = get_value(var->v.value_number, mode);
1926 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1927 set_value(var->v.value_number, res);
1933 * Handle the assume optimizer hint.
1935 * @param dbi debug info
1936 * @param expr the IL assume expression
1938 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1939 switch(expression->kind) {
1940 case EXPR_BINARY_EQUAL:
1941 case EXPR_BINARY_NOTEQUAL:
1942 case EXPR_BINARY_LESS:
1943 case EXPR_BINARY_LESSEQUAL:
1944 case EXPR_BINARY_GREATER:
1945 case EXPR_BINARY_GREATEREQUAL:
1946 return handle_assume_compare(dbi, &expression->binary);
1952 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1954 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1955 type_t *type = skip_typeref(expression->base.type);
1957 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1958 return expression_to_addr(expression->value);
1960 const expression_t *value = expression->value;
1962 switch(expression->base.kind) {
1963 case EXPR_UNARY_NEGATE: {
1964 ir_node *value_node = expression_to_firm(value);
1965 ir_mode *mode = get_ir_mode(type);
1966 return new_d_Minus(dbgi, value_node, mode);
1968 case EXPR_UNARY_PLUS:
1969 return expression_to_firm(value);
1970 case EXPR_UNARY_BITWISE_NEGATE: {
1971 ir_node *value_node = expression_to_firm(value);
1972 ir_mode *mode = get_ir_mode(type);
1973 return new_d_Not(dbgi, value_node, mode);
1975 case EXPR_UNARY_NOT: {
1976 ir_node *value_node = _expression_to_firm(value);
1977 value_node = create_conv(dbgi, value_node, mode_b);
1978 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
1981 case EXPR_UNARY_DEREFERENCE: {
1982 ir_node *value_node = expression_to_firm(value);
1983 type_t *value_type = skip_typeref(value->base.type);
1984 assert(is_type_pointer(value_type));
1985 type_t *points_to = value_type->pointer.points_to;
1986 return deref_address(dbgi, points_to, value_node);
1988 case EXPR_UNARY_POSTFIX_INCREMENT:
1989 case EXPR_UNARY_POSTFIX_DECREMENT:
1990 case EXPR_UNARY_PREFIX_INCREMENT:
1991 case EXPR_UNARY_PREFIX_DECREMENT:
1992 return create_incdec(expression);
1993 case EXPR_UNARY_CAST: {
1994 ir_node *value_node = expression_to_firm(value);
1995 if(is_type_scalar(type)) {
1996 ir_mode *mode = get_ir_mode(type);
1997 ir_node *node = create_conv(dbgi, value_node, mode);
1998 node = do_strict_conv(dbgi, node);
2001 /* make sure firm type is constructed */
2002 (void) get_ir_type(type);
2006 case EXPR_UNARY_CAST_IMPLICIT: {
2007 ir_node *value_node = expression_to_firm(value);
2008 if(is_type_scalar(type)) {
2009 ir_mode *mode = get_ir_mode(type);
2010 return create_conv(dbgi, value_node, mode);
2015 case EXPR_UNARY_ASSUME:
2016 if(firm_opt.confirm)
2017 return handle_assume(dbgi, value);
2024 panic("invalid UNEXPR type found");
2028 * produces a 0/1 depending of the value of a mode_b node
2030 static ir_node *produce_condition_result(const expression_t *expression,
2033 ir_mode *mode = get_ir_mode(expression->base.type);
2034 ir_node *cur_block = get_cur_block();
2036 ir_node *one_block = new_immBlock();
2037 ir_node *one = new_Const(mode, get_mode_one(mode));
2038 ir_node *jmp_one = new_d_Jmp(dbgi);
2040 ir_node *zero_block = new_immBlock();
2041 ir_node *zero = new_Const(mode, get_mode_null(mode));
2042 ir_node *jmp_zero = new_d_Jmp(dbgi);
2044 set_cur_block(cur_block);
2045 create_condition_evaluation(expression, one_block, zero_block);
2046 mature_immBlock(one_block);
2047 mature_immBlock(zero_block);
2049 ir_node *common_block = new_immBlock();
2050 add_immBlock_pred(common_block, jmp_one);
2051 add_immBlock_pred(common_block, jmp_zero);
2052 mature_immBlock(common_block);
2054 ir_node *in[2] = { one, zero };
2055 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2060 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2061 ir_node *value, type_t *type)
2063 pointer_type_t *const pointer_type = &type->pointer;
2064 type_t *const points_to = pointer_type->points_to;
2065 const unsigned elem_size = get_type_size_const(points_to);
2067 assert(elem_size >= 1);
2071 value = create_conv(dbgi, value, mode_int);
2072 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2073 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2077 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2078 ir_node *left, ir_node *right)
2081 type_t *type_left = skip_typeref(expression->left->base.type);
2082 type_t *type_right = skip_typeref(expression->right->base.type);
2084 expression_kind_t kind = expression->base.kind;
2087 case EXPR_BINARY_SHIFTLEFT:
2088 case EXPR_BINARY_SHIFTRIGHT:
2089 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2090 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2091 mode = get_irn_mode(left);
2092 right = create_conv(dbgi, right, mode_uint);
2095 case EXPR_BINARY_SUB:
2096 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2097 const pointer_type_t *const ptr_type = &type_left->pointer;
2099 mode = get_ir_mode(expression->base.type);
2100 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2101 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2102 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2103 ir_node *const no_mem = new_NoMem();
2104 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2105 mode, op_pin_state_floats);
2106 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2109 case EXPR_BINARY_SUB_ASSIGN:
2110 if (is_type_pointer(type_left)) {
2111 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2112 mode = get_ir_mode(type_left);
2117 case EXPR_BINARY_ADD:
2118 case EXPR_BINARY_ADD_ASSIGN:
2119 if (is_type_pointer(type_left)) {
2120 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2121 mode = get_ir_mode(type_left);
2123 } else if (is_type_pointer(type_right)) {
2124 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2125 mode = get_ir_mode(type_right);
2132 mode = get_irn_mode(right);
2133 left = create_conv(dbgi, left, mode);
2138 case EXPR_BINARY_ADD_ASSIGN:
2139 case EXPR_BINARY_ADD:
2140 return new_d_Add(dbgi, left, right, mode);
2141 case EXPR_BINARY_SUB_ASSIGN:
2142 case EXPR_BINARY_SUB:
2143 return new_d_Sub(dbgi, left, right, mode);
2144 case EXPR_BINARY_MUL_ASSIGN:
2145 case EXPR_BINARY_MUL:
2146 return new_d_Mul(dbgi, left, right, mode);
2147 case EXPR_BINARY_BITWISE_AND:
2148 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2149 return new_d_And(dbgi, left, right, mode);
2150 case EXPR_BINARY_BITWISE_OR:
2151 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2152 return new_d_Or(dbgi, left, right, mode);
2153 case EXPR_BINARY_BITWISE_XOR:
2154 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2155 return new_d_Eor(dbgi, left, right, mode);
2156 case EXPR_BINARY_SHIFTLEFT:
2157 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2158 return new_d_Shl(dbgi, left, right, mode);
2159 case EXPR_BINARY_SHIFTRIGHT:
2160 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2161 if (mode_is_signed(mode)) {
2162 return new_d_Shrs(dbgi, left, right, mode);
2164 return new_d_Shr(dbgi, left, right, mode);
2166 case EXPR_BINARY_DIV:
2167 case EXPR_BINARY_DIV_ASSIGN: {
2168 ir_node *pin = new_Pin(new_NoMem());
2171 if(mode_is_float(mode)) {
2172 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2173 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2175 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2176 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2180 case EXPR_BINARY_MOD:
2181 case EXPR_BINARY_MOD_ASSIGN: {
2182 ir_node *pin = new_Pin(new_NoMem());
2183 assert(!mode_is_float(mode));
2184 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2185 op_pin_state_floats);
2186 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2190 panic("unexpected expression kind");
2194 static ir_node *create_lazy_op(const binary_expression_t *expression)
2196 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2197 type_t *type = expression->base.type;
2198 ir_mode *mode = get_ir_mode(type);
2200 if(is_constant_expression(expression->left)) {
2201 long val = fold_constant(expression->left);
2202 expression_kind_t ekind = expression->base.kind;
2203 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
2204 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2205 return expression_to_firm(expression->right);
2207 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
2208 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
2209 return new_Const(mode, get_mode_one(mode));
2213 return produce_condition_result((const expression_t*) expression, dbgi);
2216 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2217 ir_node *right, ir_mode *mode);
2219 static ir_node *create_assign_binop(const binary_expression_t *expression)
2221 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2222 const expression_t *left_expr = expression->left;
2223 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2224 ir_node *left_addr = expression_to_addr(left_expr);
2225 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2226 ir_node *right = expression_to_firm(expression->right);
2227 ir_node *result = create_op(dbgi, expression, left, right);
2229 result = create_conv(dbgi, result, left_mode);
2230 result = do_strict_conv(dbgi, result);
2232 set_value_for_expression_addr(left_expr, result, left_addr);
2237 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2239 expression_kind_t kind = expression->base.kind;
2242 case EXPR_BINARY_EQUAL:
2243 case EXPR_BINARY_NOTEQUAL:
2244 case EXPR_BINARY_LESS:
2245 case EXPR_BINARY_LESSEQUAL:
2246 case EXPR_BINARY_GREATER:
2247 case EXPR_BINARY_GREATEREQUAL:
2248 case EXPR_BINARY_ISGREATER:
2249 case EXPR_BINARY_ISGREATEREQUAL:
2250 case EXPR_BINARY_ISLESS:
2251 case EXPR_BINARY_ISLESSEQUAL:
2252 case EXPR_BINARY_ISLESSGREATER:
2253 case EXPR_BINARY_ISUNORDERED: {
2254 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2255 ir_node *left = expression_to_firm(expression->left);
2256 ir_node *right = expression_to_firm(expression->right);
2257 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2258 long pnc = get_pnc(kind, expression->left->base.type);
2259 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2262 case EXPR_BINARY_ASSIGN: {
2263 ir_node *addr = expression_to_addr(expression->left);
2264 ir_node *right = expression_to_firm(expression->right);
2265 set_value_for_expression_addr(expression->left, right, addr);
2269 case EXPR_BINARY_ADD:
2270 case EXPR_BINARY_SUB:
2271 case EXPR_BINARY_MUL:
2272 case EXPR_BINARY_DIV:
2273 case EXPR_BINARY_MOD:
2274 case EXPR_BINARY_BITWISE_AND:
2275 case EXPR_BINARY_BITWISE_OR:
2276 case EXPR_BINARY_BITWISE_XOR:
2277 case EXPR_BINARY_SHIFTLEFT:
2278 case EXPR_BINARY_SHIFTRIGHT:
2280 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2281 ir_node *left = expression_to_firm(expression->left);
2282 ir_node *right = expression_to_firm(expression->right);
2283 return create_op(dbgi, expression, left, right);
2285 case EXPR_BINARY_LOGICAL_AND:
2286 case EXPR_BINARY_LOGICAL_OR:
2287 return create_lazy_op(expression);
2288 case EXPR_BINARY_COMMA:
2289 /* create side effects of left side */
2290 (void) expression_to_firm(expression->left);
2291 return _expression_to_firm(expression->right);
2293 case EXPR_BINARY_ADD_ASSIGN:
2294 case EXPR_BINARY_SUB_ASSIGN:
2295 case EXPR_BINARY_MUL_ASSIGN:
2296 case EXPR_BINARY_MOD_ASSIGN:
2297 case EXPR_BINARY_DIV_ASSIGN:
2298 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2299 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2300 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2301 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2302 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2303 return create_assign_binop(expression);
2304 case EXPR_BINARY_BUILTIN_EXPECT:
2305 return _expression_to_firm(expression->left);
2307 panic("TODO binexpr type");
2311 static ir_node *array_access_addr(const array_access_expression_t *expression)
2313 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2314 ir_node *base_addr = expression_to_firm(expression->array_ref);
2315 ir_node *offset = expression_to_firm(expression->index);
2317 type_t *offset_type = skip_typeref(expression->index->base.type);
2319 if (is_type_signed(offset_type)) {
2320 mode = get_ir_mode(type_ssize_t);
2322 mode = get_ir_mode(type_size_t);
2324 offset = create_conv(dbgi, offset, mode);
2326 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2327 assert(is_type_pointer(ref_type));
2328 pointer_type_t *pointer_type = &ref_type->pointer;
2330 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2331 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2332 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2334 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2339 static ir_node *array_access_to_firm(
2340 const array_access_expression_t *expression)
2342 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2343 ir_node *addr = array_access_addr(expression);
2344 type_t *type = revert_automatic_type_conversion(
2345 (const expression_t*) expression);
2346 type = skip_typeref(type);
2348 return deref_address(dbgi, type, addr);
2351 static long get_offsetof_offset(const offsetof_expression_t *expression)
2353 type_t *orig_type = expression->type;
2356 designator_t *designator = expression->designator;
2357 for( ; designator != NULL; designator = designator->next) {
2358 type_t *type = skip_typeref(orig_type);
2359 /* be sure the type is constructed */
2360 (void) get_ir_type(type);
2362 if(designator->symbol != NULL) {
2363 assert(is_type_compound(type));
2364 symbol_t *symbol = designator->symbol;
2366 declaration_t *declaration = type->compound.declaration;
2367 declaration_t *iter = declaration->scope.declarations;
2368 for( ; iter != NULL; iter = iter->next) {
2369 if(iter->symbol == symbol) {
2373 assert(iter != NULL);
2375 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2376 offset += get_entity_offset(iter->v.entity);
2378 orig_type = iter->type;
2380 expression_t *array_index = designator->array_index;
2381 assert(designator->array_index != NULL);
2382 assert(is_type_array(type));
2383 assert(is_type_valid(array_index->base.type));
2385 long index = fold_constant(array_index);
2386 ir_type *arr_type = get_ir_type(type);
2387 ir_type *elem_type = get_array_element_type(arr_type);
2388 long elem_size = get_type_size_bytes(elem_type);
2390 offset += index * elem_size;
2392 orig_type = type->array.element_type;
2399 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2401 ir_mode *mode = get_ir_mode(expression->base.type);
2402 long offset = get_offsetof_offset(expression);
2403 tarval *tv = new_tarval_from_long(offset, mode);
2404 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2406 return new_d_Const(dbgi, mode, tv);
2409 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2410 ir_entity *entity, type_t *type);
2412 static ir_node *compound_literal_to_firm(
2413 const compound_literal_expression_t *expression)
2415 type_t *type = expression->type;
2417 /* create an entity on the stack */
2418 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2420 ident *const id = id_unique("CompLit.%u");
2421 ir_type *const irtype = get_ir_type(type);
2422 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2423 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2424 set_entity_ld_ident(entity, id);
2426 set_entity_variability(entity, variability_uninitialized);
2428 /* create initialisation code */
2429 initializer_t *initializer = expression->initializer;
2430 create_local_initializer(initializer, dbgi, entity, type);
2432 /* create a sel for the compound literal address */
2433 ir_node *frame = get_local_frame(entity);
2434 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2439 * Transform a sizeof expression into Firm code.
2441 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2443 type_t *type = expression->type;
2445 type = expression->tp_expression->base.type;
2446 assert(type != NULL);
2449 type = skip_typeref(type);
2450 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2451 if(is_type_array(type) && type->array.is_vla
2452 && expression->tp_expression != NULL) {
2453 expression_to_firm(expression->tp_expression);
2456 return get_type_size(type);
2460 * Transform an alignof expression into Firm code.
2462 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2464 type_t *type = expression->type;
2466 /* beware: if expression is a variable reference, return the
2467 alignment of the variable. */
2468 const expression_t *tp_expression = expression->tp_expression;
2469 const declaration_t *declaration = expr_is_variable(tp_expression);
2470 if (declaration != NULL) {
2471 /* TODO: get the alignment of this variable. */
2473 type = tp_expression->base.type;
2474 assert(type != NULL);
2477 ir_mode *const mode = get_ir_mode(expression->base.type);
2478 symconst_symbol sym;
2479 sym.type_p = get_ir_type(type);
2480 return new_SymConst(mode, sym, symconst_type_align);
2483 static bool constant_folding;
2485 static void init_ir_types(void);
2486 long fold_constant(const expression_t *expression)
2488 bool constant_folding_old = constant_folding;
2489 constant_folding = true;
2493 assert(is_constant_expression(expression));
2495 ir_graph *old_current_ir_graph = current_ir_graph;
2496 if(current_ir_graph == NULL) {
2497 current_ir_graph = get_const_code_irg();
2500 ir_node *cnst = expression_to_firm(expression);
2501 current_ir_graph = old_current_ir_graph;
2503 if(!is_Const(cnst)) {
2504 panic("couldn't fold constant\n");
2507 tarval *tv = get_Const_tarval(cnst);
2508 if(!tarval_is_long(tv)) {
2509 panic("result of constant folding is not integer\n");
2512 constant_folding = constant_folding_old;
2514 return get_tarval_long(tv);
2517 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2519 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2521 /* first try to fold a constant condition */
2522 if(is_constant_expression(expression->condition)) {
2523 long val = fold_constant(expression->condition);
2525 return expression_to_firm(expression->true_expression);
2527 return expression_to_firm(expression->false_expression);
2531 ir_node *cur_block = get_cur_block();
2533 /* create the true block */
2534 ir_node *true_block = new_immBlock();
2536 ir_node *true_val = expression_to_firm(expression->true_expression);
2537 ir_node *true_jmp = new_Jmp();
2539 /* create the false block */
2540 ir_node *false_block = new_immBlock();
2542 ir_node *false_val = expression_to_firm(expression->false_expression);
2543 ir_node *false_jmp = new_Jmp();
2545 /* create the condition evaluation */
2546 set_cur_block(cur_block);
2547 create_condition_evaluation(expression->condition, true_block, false_block);
2548 mature_immBlock(true_block);
2549 mature_immBlock(false_block);
2551 /* create the common block */
2552 ir_node *common_block = new_immBlock();
2553 add_immBlock_pred(common_block, true_jmp);
2554 add_immBlock_pred(common_block, false_jmp);
2555 mature_immBlock(common_block);
2557 /* TODO improve static semantics, so either both or no values are NULL */
2558 if (true_val == NULL || false_val == NULL)
2561 ir_node *in[2] = { true_val, false_val };
2562 ir_mode *mode = get_irn_mode(true_val);
2563 assert(get_irn_mode(false_val) == mode);
2564 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2570 * Returns an IR-node representing the address of a field.
2572 static ir_node *select_addr(const select_expression_t *expression)
2574 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2576 ir_node *compound_addr = expression_to_firm(expression->compound);
2578 /* make sure the type is constructed */
2579 type_t *type = skip_typeref(expression->compound->base.type);
2580 if (is_type_pointer(type)) {
2581 type = type->pointer.points_to;
2583 (void) get_ir_type(type);
2585 declaration_t *entry = expression->compound_entry;
2586 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2587 ir_entity *entity = entry->v.entity;
2589 assert(entity != NULL);
2591 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2596 static ir_node *select_to_firm(const select_expression_t *expression)
2598 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2599 ir_node *addr = select_addr(expression);
2600 type_t *type = revert_automatic_type_conversion(
2601 (const expression_t*) expression);
2602 type = skip_typeref(type);
2604 declaration_t *entry = expression->compound_entry;
2605 type_t *entry_type = skip_typeref(entry->type);
2607 if (entry_type->kind == TYPE_BITFIELD) {
2608 return bitfield_extract_to_firm(expression, addr);
2611 return deref_address(dbgi, type, addr);
2614 /* Values returned by __builtin_classify_type. */
2615 typedef enum gcc_type_class
2621 enumeral_type_class,
2624 reference_type_class,
2628 function_type_class,
2639 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2641 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2647 const atomic_type_t *const atomic_type = &type->atomic;
2648 switch (atomic_type->akind) {
2649 /* should not be reached */
2650 case ATOMIC_TYPE_INVALID:
2654 /* gcc cannot do that */
2655 case ATOMIC_TYPE_VOID:
2656 tc = void_type_class;
2659 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2660 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2661 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2662 case ATOMIC_TYPE_SHORT:
2663 case ATOMIC_TYPE_USHORT:
2664 case ATOMIC_TYPE_INT:
2665 case ATOMIC_TYPE_UINT:
2666 case ATOMIC_TYPE_LONG:
2667 case ATOMIC_TYPE_ULONG:
2668 case ATOMIC_TYPE_LONGLONG:
2669 case ATOMIC_TYPE_ULONGLONG:
2670 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2671 tc = integer_type_class;
2674 case ATOMIC_TYPE_FLOAT:
2675 case ATOMIC_TYPE_DOUBLE:
2676 case ATOMIC_TYPE_LONG_DOUBLE:
2677 tc = real_type_class;
2680 panic("Unexpected atomic type in classify_type_to_firm().");
2683 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2684 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2685 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2686 case TYPE_ARRAY: /* gcc handles this as pointer */
2687 case TYPE_FUNCTION: /* gcc handles this as pointer */
2688 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2689 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2690 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2692 /* gcc handles this as integer */
2693 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2696 /* typedef/typeof should be skipped already */
2703 panic("unexpected TYPE classify_type_to_firm().");
2706 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2707 ir_mode *const mode = mode_int;
2708 tarval *const tv = new_tarval_from_long(tc, mode);
2709 return new_d_Const(dbgi, mode, tv);
2712 static ir_node *function_name_to_firm(
2713 const funcname_expression_t *const expr)
2715 switch(expr->kind) {
2716 case FUNCNAME_FUNCTION:
2717 case FUNCNAME_PRETTY_FUNCTION:
2718 case FUNCNAME_FUNCDNAME:
2719 if (current_function_name == NULL) {
2720 const source_position_t *const src_pos = &expr->base.source_position;
2721 const char *const name = current_function_decl->symbol->string;
2722 const string_t string = { name, strlen(name) + 1 };
2723 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2725 return current_function_name;
2726 case FUNCNAME_FUNCSIG:
2727 if (current_funcsig == NULL) {
2728 const source_position_t *const src_pos = &expr->base.source_position;
2729 ir_entity *ent = get_irg_entity(current_ir_graph);
2730 const char *const name = get_entity_ld_name(ent);
2731 const string_t string = { name, strlen(name) + 1 };
2732 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2734 return current_funcsig;
2736 panic("Unsupported function name");
2739 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2741 statement_t *statement = expr->statement;
2743 assert(statement->kind == STATEMENT_COMPOUND);
2744 return compound_statement_to_firm(&statement->compound);
2747 static ir_node *va_start_expression_to_firm(
2748 const va_start_expression_t *const expr)
2750 ir_type *const method_type = get_ir_type(current_function_decl->type);
2751 int const n = get_method_n_params(method_type) - 1;
2752 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2753 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2754 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2755 ir_node *const no_mem = new_NoMem();
2756 ir_node *const arg_sel =
2757 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2759 ir_node *const cnst = get_type_size(expr->parameter->type);
2760 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2761 set_value_for_expression(expr->ap, add);
2766 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2768 type_t *const type = expr->base.type;
2769 expression_t *const ap_expr = expr->ap;
2770 ir_node *const ap_addr = expression_to_addr(ap_expr);
2771 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2772 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2773 ir_node *const res = deref_address(dbgi, type, ap);
2775 ir_node *const cnst = get_type_size(expr->base.type);
2776 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2778 set_value_for_expression_addr(ap_expr, add, ap_addr);
2783 static ir_node *dereference_addr(const unary_expression_t *const expression)
2785 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2786 return expression_to_firm(expression->value);
2790 * Returns a IR-node representing an lvalue of the given expression.
2792 static ir_node *expression_to_addr(const expression_t *expression)
2794 switch(expression->kind) {
2795 case EXPR_REFERENCE:
2796 return reference_addr(&expression->reference);
2797 case EXPR_ARRAY_ACCESS:
2798 return array_access_addr(&expression->array_access);
2800 return select_addr(&expression->select);
2802 return call_expression_to_firm(&expression->call);
2803 case EXPR_UNARY_DEREFERENCE: {
2804 return dereference_addr(&expression->unary);
2809 panic("trying to get address of non-lvalue");
2812 static ir_node *builtin_constant_to_firm(
2813 const builtin_constant_expression_t *expression)
2815 ir_mode *mode = get_ir_mode(expression->base.type);
2818 if (is_constant_expression(expression->value)) {
2823 return new_Const_long(mode, v);
2826 static ir_node *builtin_prefetch_to_firm(
2827 const builtin_prefetch_expression_t *expression)
2829 ir_node *adr = expression_to_firm(expression->adr);
2830 /* no Firm support for prefetch yet */
2836 * creates firm nodes for an expression. The difference between this function
2837 * and expression_to_firm is, that this version might produce mode_b nodes
2838 * instead of mode_Is.
2840 static ir_node *_expression_to_firm(const expression_t *expression)
2843 if (!constant_folding) {
2844 assert(!expression->base.transformed);
2845 ((expression_t*) expression)->base.transformed = true;
2849 switch(expression->kind) {
2850 case EXPR_CHARACTER_CONSTANT:
2851 return character_constant_to_firm(&expression->conste);
2852 case EXPR_WIDE_CHARACTER_CONSTANT:
2853 return wide_character_constant_to_firm(&expression->conste);
2855 return const_to_firm(&expression->conste);
2856 case EXPR_STRING_LITERAL:
2857 return string_literal_to_firm(&expression->string);
2858 case EXPR_WIDE_STRING_LITERAL:
2859 return wide_string_literal_to_firm(&expression->wide_string);
2860 case EXPR_REFERENCE:
2861 return reference_expression_to_firm(&expression->reference);
2863 return call_expression_to_firm(&expression->call);
2865 return unary_expression_to_firm(&expression->unary);
2867 return binary_expression_to_firm(&expression->binary);
2868 case EXPR_ARRAY_ACCESS:
2869 return array_access_to_firm(&expression->array_access);
2871 return sizeof_to_firm(&expression->typeprop);
2873 return alignof_to_firm(&expression->typeprop);
2874 case EXPR_CONDITIONAL:
2875 return conditional_to_firm(&expression->conditional);
2877 return select_to_firm(&expression->select);
2878 case EXPR_CLASSIFY_TYPE:
2879 return classify_type_to_firm(&expression->classify_type);
2881 return function_name_to_firm(&expression->funcname);
2882 case EXPR_STATEMENT:
2883 return statement_expression_to_firm(&expression->statement);
2885 return va_start_expression_to_firm(&expression->va_starte);
2887 return va_arg_expression_to_firm(&expression->va_arge);
2888 case EXPR_BUILTIN_SYMBOL:
2889 panic("unimplemented expression found");
2890 case EXPR_BUILTIN_CONSTANT_P:
2891 return builtin_constant_to_firm(&expression->builtin_constant);
2892 case EXPR_BUILTIN_PREFETCH:
2893 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2895 return offsetof_to_firm(&expression->offsetofe);
2896 case EXPR_COMPOUND_LITERAL:
2897 return compound_literal_to_firm(&expression->compound_literal);
2903 panic("invalid expression found");
2906 static bool produces_mode_b(const expression_t *expression)
2908 switch (expression->kind) {
2909 case EXPR_BINARY_EQUAL:
2910 case EXPR_BINARY_NOTEQUAL:
2911 case EXPR_BINARY_LESS:
2912 case EXPR_BINARY_LESSEQUAL:
2913 case EXPR_BINARY_GREATER:
2914 case EXPR_BINARY_GREATEREQUAL:
2915 case EXPR_BINARY_ISGREATER:
2916 case EXPR_BINARY_ISGREATEREQUAL:
2917 case EXPR_BINARY_ISLESS:
2918 case EXPR_BINARY_ISLESSEQUAL:
2919 case EXPR_BINARY_ISLESSGREATER:
2920 case EXPR_BINARY_ISUNORDERED:
2921 case EXPR_UNARY_NOT:
2924 case EXPR_BINARY_BUILTIN_EXPECT:
2925 return produces_mode_b(expression->binary.left);
2926 case EXPR_BINARY_COMMA:
2927 return produces_mode_b(expression->binary.right);
2934 static ir_node *expression_to_firm(const expression_t *expression)
2936 if (!produces_mode_b(expression)) {
2937 ir_node *res = _expression_to_firm(expression);
2938 assert(res == NULL || get_irn_mode(res) != mode_b);
2942 if (is_constant_expression(expression)) {
2943 ir_node *res = _expression_to_firm(expression);
2944 ir_mode *mode = get_ir_mode(expression->base.type);
2945 assert(is_Const(res));
2946 if (is_Const_null(res)) {
2947 return new_Const_long(mode, 0);
2949 return new_Const_long(mode, 1);
2953 /* we have to produce a 0/1 from the mode_b expression */
2954 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2955 return produce_condition_result(expression, dbgi);
2958 static ir_node *expression_to_modeb(const expression_t *expression)
2960 ir_node *res = _expression_to_firm(expression);
2961 res = create_conv(NULL, res, mode_b);
2967 * create a short-circuit expression evaluation that tries to construct
2968 * efficient control flow structures for &&, || and ! expressions
2970 static void create_condition_evaluation(const expression_t *expression,
2971 ir_node *true_block,
2972 ir_node *false_block)
2974 switch(expression->kind) {
2975 case EXPR_UNARY_NOT: {
2976 const unary_expression_t *unary_expression = &expression->unary;
2977 create_condition_evaluation(unary_expression->value, false_block,
2981 case EXPR_BINARY_LOGICAL_AND: {
2982 const binary_expression_t *binary_expression = &expression->binary;
2984 ir_node *cur_block = get_cur_block();
2985 ir_node *extra_block = new_immBlock();
2986 set_cur_block(cur_block);
2987 create_condition_evaluation(binary_expression->left, extra_block,
2989 mature_immBlock(extra_block);
2990 set_cur_block(extra_block);
2991 create_condition_evaluation(binary_expression->right, true_block,
2995 case EXPR_BINARY_LOGICAL_OR: {
2996 const binary_expression_t *binary_expression = &expression->binary;
2998 ir_node *cur_block = get_cur_block();
2999 ir_node *extra_block = new_immBlock();
3000 set_cur_block(cur_block);
3001 create_condition_evaluation(binary_expression->left, true_block,
3003 mature_immBlock(extra_block);
3004 set_cur_block(extra_block);
3005 create_condition_evaluation(binary_expression->right, true_block,
3013 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3014 ir_node *condition = expression_to_modeb(expression);
3015 ir_node *cond = new_d_Cond(dbgi, condition);
3016 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3017 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3019 /* set branch prediction info based on __builtin_expect */
3020 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3021 long cnst = fold_constant(expression->binary.right);
3022 cond_jmp_predicate pred;
3025 pred = COND_JMP_PRED_FALSE;
3027 pred = COND_JMP_PRED_TRUE;
3029 set_Cond_jmp_pred(cond, pred);
3032 add_immBlock_pred(true_block, true_proj);
3033 add_immBlock_pred(false_block, false_proj);
3035 set_cur_block(NULL);
3040 static void create_declaration_entity(declaration_t *declaration,
3041 declaration_kind_t declaration_kind,
3042 ir_type *parent_type)
3044 type_t *const type = skip_typeref(declaration->type);
3045 if (is_type_function(type)) {
3046 (void) get_function_entity(declaration);
3050 ident *const id = new_id_from_str(declaration->symbol->string);
3051 ir_type *const irtype = get_ir_type(type);
3052 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3053 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3055 declaration->declaration_kind = (unsigned char) declaration_kind;
3056 declaration->v.entity = entity;
3057 set_entity_variability(entity, variability_uninitialized);
3058 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3059 if(parent_type == get_tls_type())
3060 set_entity_allocation(entity, allocation_automatic);
3061 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3062 set_entity_allocation(entity, allocation_static);
3064 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3065 set_entity_volatility(entity, volatility_is_volatile);
3070 typedef struct type_path_entry_t type_path_entry_t;
3071 struct type_path_entry_t {
3073 ir_initializer_t *initializer;
3075 declaration_t *compound_entry;
3078 typedef struct type_path_t type_path_t;
3079 struct type_path_t {
3080 type_path_entry_t *path;
3085 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3087 size_t len = ARR_LEN(path->path);
3089 for(size_t i = 0; i < len; ++i) {
3090 const type_path_entry_t *entry = & path->path[i];
3092 type_t *type = skip_typeref(entry->type);
3093 if(is_type_compound(type)) {
3094 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3095 } else if(is_type_array(type)) {
3096 fprintf(stderr, "[%zd]", entry->index);
3098 fprintf(stderr, "-INVALID-");
3101 fprintf(stderr, " (");
3102 print_type(path->top_type);
3103 fprintf(stderr, ")");
3106 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3108 size_t len = ARR_LEN(path->path);
3110 return & path->path[len-1];
3113 static type_path_entry_t *append_to_type_path(type_path_t *path)
3115 size_t len = ARR_LEN(path->path);
3116 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3118 type_path_entry_t *result = & path->path[len];
3119 memset(result, 0, sizeof(result[0]));
3123 static size_t get_compound_size(const compound_type_t *type)
3125 declaration_t *declaration = type->declaration;
3126 declaration_t *member = declaration->scope.declarations;
3128 for( ; member != NULL; member = member->next) {
3131 /* TODO: cache results? */
3136 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3138 type_t *orig_top_type = path->top_type;
3139 type_t *top_type = skip_typeref(orig_top_type);
3141 assert(is_type_compound(top_type) || is_type_array(top_type));
3143 if(ARR_LEN(path->path) == 0) {
3146 type_path_entry_t *top = get_type_path_top(path);
3147 ir_initializer_t *initializer = top->initializer;
3148 return get_initializer_compound_value(initializer, top->index);
3152 static void descend_into_subtype(type_path_t *path)
3154 type_t *orig_top_type = path->top_type;
3155 type_t *top_type = skip_typeref(orig_top_type);
3157 assert(is_type_compound(top_type) || is_type_array(top_type));
3159 ir_initializer_t *initializer = get_initializer_entry(path);
3161 type_path_entry_t *top = append_to_type_path(path);
3162 top->type = top_type;
3166 if(is_type_compound(top_type)) {
3167 declaration_t *declaration = top_type->compound.declaration;
3168 declaration_t *entry = declaration->scope.declarations;
3170 top->compound_entry = entry;
3172 len = get_compound_size(&top_type->compound);
3174 path->top_type = entry->type;
3176 assert(is_type_array(top_type));
3177 assert(top_type->array.size > 0);
3180 path->top_type = top_type->array.element_type;
3181 len = top_type->array.size;
3183 if(initializer == NULL
3184 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3185 initializer = create_initializer_compound(len);
3186 /* we have to set the entry at the 2nd latest path entry... */
3187 size_t path_len = ARR_LEN(path->path);
3188 assert(path_len >= 1);
3190 type_path_entry_t *entry = & path->path[path_len-2];
3191 ir_initializer_t *tinitializer = entry->initializer;
3192 set_initializer_compound_value(tinitializer, entry->index,
3196 top->initializer = initializer;
3199 static void ascend_from_subtype(type_path_t *path)
3201 type_path_entry_t *top = get_type_path_top(path);
3203 path->top_type = top->type;
3205 size_t len = ARR_LEN(path->path);
3206 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3209 static void walk_designator(type_path_t *path, const designator_t *designator)
3211 /* designators start at current object type */
3212 ARR_RESIZE(type_path_entry_t, path->path, 1);
3214 for( ; designator != NULL; designator = designator->next) {
3215 type_path_entry_t *top = get_type_path_top(path);
3216 type_t *orig_type = top->type;
3217 type_t *type = skip_typeref(orig_type);
3219 if(designator->symbol != NULL) {
3220 assert(is_type_compound(type));
3222 symbol_t *symbol = designator->symbol;
3224 declaration_t *declaration = type->compound.declaration;
3225 declaration_t *iter = declaration->scope.declarations;
3226 for( ; iter != NULL; iter = iter->next, ++index) {
3227 if(iter->symbol == symbol) {
3231 assert(iter != NULL);
3233 top->type = orig_type;
3234 top->compound_entry = iter;
3236 orig_type = iter->type;
3238 expression_t *array_index = designator->array_index;
3239 assert(designator->array_index != NULL);
3240 assert(is_type_array(type));
3241 assert(is_type_valid(array_index->base.type));
3243 long index = fold_constant(array_index);
3246 if(type->array.size_constant == 1) {
3247 long array_size = type->array.size;
3248 assert(index < array_size);
3252 top->type = orig_type;
3253 top->index = (size_t) index;
3254 orig_type = type->array.element_type;
3256 path->top_type = orig_type;
3258 if(designator->next != NULL) {
3259 descend_into_subtype(path);
3263 path->invalid = false;
3266 static void advance_current_object(type_path_t *path)
3269 /* TODO: handle this... */
3270 panic("invalid initializer in ast2firm (excessive elements)");
3274 type_path_entry_t *top = get_type_path_top(path);
3276 type_t *type = skip_typeref(top->type);
3277 if(is_type_union(type)) {
3278 top->compound_entry = NULL;
3279 } else if(is_type_struct(type)) {
3280 declaration_t *entry = top->compound_entry;
3283 entry = entry->next;
3284 top->compound_entry = entry;
3286 path->top_type = entry->type;
3290 assert(is_type_array(type));
3293 if(!type->array.size_constant || top->index < type->array.size) {
3298 /* we're past the last member of the current sub-aggregate, try if we
3299 * can ascend in the type hierarchy and continue with another subobject */
3300 size_t len = ARR_LEN(path->path);
3303 ascend_from_subtype(path);
3304 advance_current_object(path);
3306 path->invalid = true;
3311 static ir_initializer_t *create_ir_initializer(
3312 const initializer_t *initializer, type_t *type);
3314 static ir_initializer_t *create_ir_initializer_value(
3315 const initializer_value_t *initializer)
3317 if (is_type_compound(initializer->value->base.type)) {
3318 panic("initializer creation for compounds not implemented yet");
3320 ir_node *value = expression_to_firm(initializer->value);
3321 return create_initializer_const(value);
3324 static ir_initializer_t *create_ir_initializer_list(
3325 const initializer_list_t *initializer, type_t *type)
3328 memset(&path, 0, sizeof(path));
3329 path.top_type = type;
3330 path.path = NEW_ARR_F(type_path_entry_t, 0);
3332 descend_into_subtype(&path);
3334 for(size_t i = 0; i < initializer->len; ++i) {
3335 const initializer_t *sub_initializer = initializer->initializers[i];
3337 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3338 walk_designator(&path, sub_initializer->designator.designator);
3342 if(sub_initializer->kind == INITIALIZER_VALUE) {
3343 /* we might have to descend into types until we're at a scalar
3346 type_t *orig_top_type = path.top_type;
3347 type_t *top_type = skip_typeref(orig_top_type);
3349 if(is_type_scalar(top_type))
3351 descend_into_subtype(&path);
3355 ir_initializer_t *sub_irinitializer
3356 = create_ir_initializer(sub_initializer, path.top_type);
3358 size_t path_len = ARR_LEN(path.path);
3359 assert(path_len >= 1);
3360 type_path_entry_t *entry = & path.path[path_len-1];
3361 ir_initializer_t *tinitializer = entry->initializer;
3362 set_initializer_compound_value(tinitializer, entry->index,
3365 advance_current_object(&path);
3368 assert(ARR_LEN(path.path) >= 1);
3369 ir_initializer_t *result = path.path[0].initializer;
3370 DEL_ARR_F(path.path);
3375 static ir_initializer_t *create_ir_initializer_string(
3376 const initializer_string_t *initializer, type_t *type)
3378 type = skip_typeref(type);
3380 size_t string_len = initializer->string.size;
3381 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3382 size_t len = type->array.size;
3383 ir_initializer_t *irinitializer = create_initializer_compound(len);
3385 const char *string = initializer->string.begin;
3386 ir_mode *mode = get_type_mode(ir_type_const_char);
3388 for(size_t i = 0; i < len; ++i) {
3393 tarval *tv = new_tarval_from_long(c, mode);
3394 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3396 set_initializer_compound_value(irinitializer, i, char_initializer);
3399 return irinitializer;
3402 static ir_initializer_t *create_ir_initializer_wide_string(
3403 const initializer_wide_string_t *initializer, type_t *type)
3405 size_t string_len = initializer->string.size;
3406 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3407 size_t len = type->array.size;
3408 ir_initializer_t *irinitializer = create_initializer_compound(len);
3410 const wchar_rep_t *string = initializer->string.begin;
3411 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3413 for(size_t i = 0; i < len; ++i) {
3415 if(i < string_len) {
3418 tarval *tv = new_tarval_from_long(c, mode);
3419 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3421 set_initializer_compound_value(irinitializer, i, char_initializer);
3424 return irinitializer;
3427 static ir_initializer_t *create_ir_initializer(
3428 const initializer_t *initializer, type_t *type)
3430 switch(initializer->kind) {
3431 case INITIALIZER_STRING:
3432 return create_ir_initializer_string(&initializer->string, type);
3434 case INITIALIZER_WIDE_STRING:
3435 return create_ir_initializer_wide_string(&initializer->wide_string,
3438 case INITIALIZER_LIST:
3439 return create_ir_initializer_list(&initializer->list, type);
3441 case INITIALIZER_VALUE:
3442 return create_ir_initializer_value(&initializer->value);
3444 case INITIALIZER_DESIGNATOR:
3445 panic("unexpected designator initializer found");
3447 panic("unknown initializer");
3450 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3453 if (is_atomic_type(type)) {
3454 ir_mode *mode = get_type_mode(type);
3455 tarval *zero = get_mode_null(mode);
3456 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3458 /* TODO: bitfields */
3459 ir_node *mem = get_store();
3460 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3461 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3464 assert(is_compound_type(type));
3467 if(is_Array_type(type)) {
3468 assert(has_array_upper_bound(type, 0));
3469 n_members = get_array_upper_bound_int(type, 0);
3471 n_members = get_compound_n_members(type);
3474 for(int i = 0; i < n_members; ++i) {
3477 if(is_Array_type(type)) {
3478 ir_entity *entity = get_array_element_entity(type);
3479 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3480 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3481 ir_node *in[1] = { cnst };
3482 irtype = get_array_element_type(type);
3483 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3485 ir_entity *member = get_compound_member(type, i);
3487 irtype = get_entity_type(member);
3488 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3491 create_dynamic_null_initializer(irtype, dbgi, addr);
3496 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3497 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3499 switch(get_initializer_kind(initializer)) {
3500 case IR_INITIALIZER_NULL: {
3501 create_dynamic_null_initializer(type, dbgi, base_addr);
3504 case IR_INITIALIZER_CONST: {
3505 ir_node *node = get_initializer_const_value(initializer);
3506 ir_mode *mode = get_irn_mode(node);
3507 assert(get_type_mode(type) == mode);
3509 /* TODO: bitfields... */
3510 ir_node *mem = get_store();
3511 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3512 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3516 case IR_INITIALIZER_TARVAL: {
3517 tarval *tv = get_initializer_tarval_value(initializer);
3518 ir_mode *mode = get_tarval_mode(tv);
3519 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3520 assert(get_type_mode(type) == mode);
3522 /* TODO: bitfields... */
3523 ir_node *mem = get_store();
3524 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3525 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3529 case IR_INITIALIZER_COMPOUND: {
3530 assert(is_compound_type(type));
3532 if(is_Array_type(type)) {
3533 assert(has_array_upper_bound(type, 0));
3534 n_members = get_array_upper_bound_int(type, 0);
3536 n_members = get_compound_n_members(type);
3539 if(get_initializer_compound_n_entries(initializer)
3540 != (unsigned) n_members)
3541 panic("initializer doesn't match compound type");
3543 for(int i = 0; i < n_members; ++i) {
3546 if(is_Array_type(type)) {
3547 ir_entity *entity = get_array_element_entity(type);
3548 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3549 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3550 ir_node *in[1] = { cnst };
3551 irtype = get_array_element_type(type);
3552 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3554 ir_entity *member = get_compound_member(type, i);
3556 irtype = get_entity_type(member);
3557 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3560 ir_initializer_t *sub_init
3561 = get_initializer_compound_value(initializer, i);
3563 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3569 panic("invalid IR_INITIALIZER found");
3572 static void create_dynamic_initializer(ir_initializer_t *initializer,
3573 dbg_info *dbgi, ir_entity *entity)
3575 ir_node *frame = get_local_frame(entity);
3576 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3577 ir_type *type = get_entity_type(entity);
3579 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3582 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3583 ir_entity *entity, type_t *type)
3585 ir_node *memory = get_store();
3586 ir_node *nomem = new_NoMem();
3587 ir_node *frame = get_irg_frame(current_ir_graph);
3588 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3590 if(initializer->kind == INITIALIZER_VALUE) {
3591 initializer_value_t *initializer_value = &initializer->value;
3593 ir_node *value = expression_to_firm(initializer_value->value);
3594 type = skip_typeref(type);
3595 assign_value(dbgi, addr, type, value);
3599 if(!is_constant_initializer(initializer)) {
3600 ir_initializer_t *irinitializer
3601 = create_ir_initializer(initializer, type);
3603 create_dynamic_initializer(irinitializer, dbgi, entity);
3607 /* create the ir_initializer */
3608 ir_graph *const old_current_ir_graph = current_ir_graph;
3609 current_ir_graph = get_const_code_irg();
3611 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3613 assert(current_ir_graph == get_const_code_irg());
3614 current_ir_graph = old_current_ir_graph;
3616 /* create a "template" entity which is copied to the entity on the stack */
3617 ident *const id = id_unique("initializer.%u");
3618 ir_type *const irtype = get_ir_type(type);
3619 ir_type *const global_type = get_glob_type();
3620 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3621 set_entity_ld_ident(init_entity, id);
3623 set_entity_variability(init_entity, variability_initialized);
3624 set_entity_visibility(init_entity, visibility_local);
3625 set_entity_allocation(init_entity, allocation_static);
3627 set_entity_initializer(init_entity, irinitializer);
3629 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3630 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3632 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3633 set_store(copyb_mem);
3636 static void create_initializer_local_variable_entity(declaration_t *declaration)
3638 initializer_t *initializer = declaration->init.initializer;
3639 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3640 ir_entity *entity = declaration->v.entity;
3641 type_t *type = declaration->type;
3642 create_local_initializer(initializer, dbgi, entity, type);
3645 static void create_declaration_initializer(declaration_t *declaration)
3647 initializer_t *initializer = declaration->init.initializer;
3648 if(initializer == NULL)
3651 declaration_kind_t declaration_kind
3652 = (declaration_kind_t) declaration->declaration_kind;
3653 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3654 create_initializer_local_variable_entity(declaration);
3658 if(initializer->kind == INITIALIZER_VALUE) {
3659 initializer_value_t *initializer_value = &initializer->value;
3661 = get_dbg_info(&declaration->source_position);
3663 ir_node *value = expression_to_firm(initializer_value->value);
3664 value = do_strict_conv(dbgi, value);
3666 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3667 set_value(declaration->v.value_number, value);
3669 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3671 ir_entity *entity = declaration->v.entity;
3673 set_entity_variability(entity, variability_initialized);
3674 set_atomic_ent_value(entity, value);
3677 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3678 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3680 ir_entity *entity = declaration->v.entity;
3681 ir_initializer_t *irinitializer
3682 = create_ir_initializer(initializer, declaration->type);
3684 set_entity_variability(entity, variability_initialized);
3685 set_entity_initializer(entity, irinitializer);
3689 static void create_variable_length_array(declaration_t *declaration)
3691 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3692 type_t *type = declaration->type;
3693 ir_node *mem = get_store();
3694 ir_type *el_type = get_ir_type(type->array.element_type);
3696 /* make sure size_node is calculated */
3697 get_type_size(type);
3698 ir_node *elems = type->array.size_node;
3699 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3701 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3702 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3705 /* initializers are not allowed for VLAs */
3706 assert(declaration->init.initializer == NULL);
3708 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3709 declaration->v.vla_base = addr;
3711 /* TODO: record VLA somewhere so we create the free node when we leave
3716 * Creates a Firm local variable from a declaration.
3718 static void create_local_variable(declaration_t *declaration)
3720 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3722 bool needs_entity = declaration->address_taken;
3723 type_t *type = skip_typeref(declaration->type);
3725 /* is it a variable length array? */
3726 if(is_type_array(type) && !type->array.size_constant) {
3727 create_variable_length_array(declaration);
3729 } else if(is_type_array(type) || is_type_compound(type)) {
3730 needs_entity = true;
3731 } else if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3732 needs_entity = true;
3736 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3737 create_declaration_entity(declaration,
3738 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3741 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3742 declaration->v.value_number = next_value_number_function;
3743 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3744 ++next_value_number_function;
3748 static void create_local_static_variable(declaration_t *declaration)
3750 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3752 type_t *const type = skip_typeref(declaration->type);
3753 ir_type *const global_type = get_glob_type();
3754 ir_type *const irtype = get_ir_type(type);
3755 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3757 size_t l = strlen(declaration->symbol->string);
3758 char buf[l + sizeof(".%u")];
3759 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3760 ident *const id = id_unique(buf);
3762 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3764 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3765 set_entity_volatility(entity, volatility_is_volatile);
3768 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3769 declaration->v.entity = entity;
3770 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3771 set_entity_variability(entity, variability_uninitialized);
3772 set_entity_visibility(entity, visibility_local);
3773 set_entity_allocation(entity, allocation_static);
3775 ir_graph *const old_current_ir_graph = current_ir_graph;
3776 current_ir_graph = get_const_code_irg();
3778 create_declaration_initializer(declaration);
3780 assert(current_ir_graph == get_const_code_irg());
3781 current_ir_graph = old_current_ir_graph;
3786 static void return_statement_to_firm(return_statement_t *statement)
3788 if(get_cur_block() == NULL)
3791 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3792 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3797 if(get_method_n_ress(func_irtype) > 0) {
3798 ir_type *res_type = get_method_res_type(func_irtype, 0);
3800 if(statement->value != NULL) {
3801 ir_node *node = expression_to_firm(statement->value);
3802 node = do_strict_conv(dbgi, node);
3806 if(is_compound_type(res_type)) {
3809 mode = get_type_mode(res_type);
3811 in[0] = new_Unknown(mode);
3815 /* build return_value for its side effects */
3816 if(statement->value != NULL) {
3817 expression_to_firm(statement->value);
3822 ir_node *store = get_store();
3823 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3825 ir_node *end_block = get_irg_end_block(current_ir_graph);
3826 add_immBlock_pred(end_block, ret);
3828 set_cur_block(NULL);
3831 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3833 if(get_cur_block() == NULL)
3836 return expression_to_firm(statement->expression);
3839 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3841 declaration_t *declaration = compound->scope.declarations;
3842 for( ; declaration != NULL; declaration = declaration->next) {
3843 create_local_declaration(declaration);
3846 ir_node *result = NULL;
3847 statement_t *statement = compound->statements;
3848 for( ; statement != NULL; statement = statement->base.next) {
3849 if(statement->base.next == NULL
3850 && statement->kind == STATEMENT_EXPRESSION) {
3851 result = expression_statement_to_firm(
3852 &statement->expression);
3855 statement_to_firm(statement);
3861 static void create_global_variable(declaration_t *declaration)
3865 switch ((storage_class_tag_t)declaration->storage_class) {
3866 case STORAGE_CLASS_STATIC:
3867 vis = visibility_local;
3870 case STORAGE_CLASS_EXTERN:
3871 vis = visibility_external_allocated;
3874 case STORAGE_CLASS_NONE:
3875 vis = visibility_external_visible;
3878 case STORAGE_CLASS_THREAD:
3879 vis = visibility_external_visible;
3882 case STORAGE_CLASS_THREAD_EXTERN:
3883 vis = visibility_external_allocated;
3886 case STORAGE_CLASS_THREAD_STATIC:
3887 vis = visibility_local;
3891 var_type = get_tls_type();
3895 var_type = get_glob_type();
3899 create_declaration_entity(declaration,
3900 DECLARATION_KIND_GLOBAL_VARIABLE,
3902 set_entity_visibility(declaration->v.entity, vis);
3906 case STORAGE_CLASS_TYPEDEF:
3907 case STORAGE_CLASS_AUTO:
3908 case STORAGE_CLASS_REGISTER:
3909 case STORAGE_CLASS_ENUM_ENTRY:
3912 panic("Invalid storage class for global variable");
3915 static void create_local_declaration(declaration_t *declaration)
3917 if (declaration->namespc != NAMESPACE_NORMAL)
3919 /* construct type */
3920 (void) get_ir_type(declaration->type);
3921 if (declaration->symbol == NULL) {
3925 type_t *type = skip_typeref(declaration->type);
3927 switch ((storage_class_tag_t) declaration->storage_class) {
3928 case STORAGE_CLASS_STATIC:
3929 create_local_static_variable(declaration);
3931 case STORAGE_CLASS_EXTERN:
3932 create_global_variable(declaration);
3933 create_declaration_initializer(declaration);
3935 case STORAGE_CLASS_NONE:
3936 case STORAGE_CLASS_AUTO:
3937 case STORAGE_CLASS_REGISTER:
3938 if(is_type_function(type)) {
3939 if(declaration->init.statement != NULL) {
3940 panic("nested functions not supported yet");
3942 get_function_entity(declaration);
3945 create_local_variable(declaration);
3948 case STORAGE_CLASS_ENUM_ENTRY:
3949 /* should already be handled */
3950 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
3952 case STORAGE_CLASS_TYPEDEF:
3953 declaration->declaration_kind = DECLARATION_KIND_TYPE;
3955 case STORAGE_CLASS_THREAD:
3956 case STORAGE_CLASS_THREAD_EXTERN:
3957 case STORAGE_CLASS_THREAD_STATIC:
3960 panic("invalid storage class found");
3963 static void initialize_local_declaration(declaration_t *declaration)
3965 if(declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
3968 switch ((declaration_kind_t) declaration->declaration_kind) {
3969 case DECLARATION_KIND_LOCAL_VARIABLE:
3970 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
3971 create_declaration_initializer(declaration);
3974 case DECLARATION_KIND_LABEL_BLOCK:
3975 case DECLARATION_KIND_COMPOUND_MEMBER:
3976 case DECLARATION_KIND_GLOBAL_VARIABLE:
3977 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
3978 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
3979 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
3980 case DECLARATION_KIND_FUNCTION:
3981 case DECLARATION_KIND_TYPE:
3982 case DECLARATION_KIND_ENUM_ENTRY:
3985 case DECLARATION_KIND_UNKNOWN:
3986 panic("can't initialize unknwon declaration");
3988 panic("invalid declaration kind");
3991 static void declaration_statement_to_firm(declaration_statement_t *statement)
3993 declaration_t *declaration = statement->declarations_begin;
3994 declaration_t *end = statement->declarations_end->next;
3995 for( ; declaration != end; declaration = declaration->next) {
3996 if(declaration->namespc != NAMESPACE_NORMAL)
3998 initialize_local_declaration(declaration);
4002 static void if_statement_to_firm(if_statement_t *statement)
4004 ir_node *cur_block = get_cur_block();
4006 ir_node *fallthrough_block = NULL;
4008 /* the true (blocks) */
4009 ir_node *true_block = NULL;
4010 if (statement->true_statement != NULL) {
4011 true_block = new_immBlock();
4012 statement_to_firm(statement->true_statement);
4013 if (get_cur_block() != NULL) {
4014 ir_node *jmp = new_Jmp();
4015 if (fallthrough_block == NULL)
4016 fallthrough_block = new_immBlock();
4017 add_immBlock_pred(fallthrough_block, jmp);
4021 /* the false (blocks) */
4022 ir_node *false_block = NULL;
4023 if (statement->false_statement != NULL) {
4024 false_block = new_immBlock();
4026 statement_to_firm(statement->false_statement);
4027 if (get_cur_block() != NULL) {
4028 ir_node *jmp = new_Jmp();
4029 if (fallthrough_block == NULL)
4030 fallthrough_block = new_immBlock();
4031 add_immBlock_pred(fallthrough_block, jmp);
4035 /* create the condition */
4036 if (cur_block != NULL) {
4037 if (true_block == NULL || false_block == NULL) {
4038 if (fallthrough_block == NULL)
4039 fallthrough_block = new_immBlock();
4040 if (true_block == NULL)
4041 true_block = fallthrough_block;
4042 if (false_block == NULL)
4043 false_block = fallthrough_block;
4046 set_cur_block(cur_block);
4047 create_condition_evaluation(statement->condition, true_block,
4051 mature_immBlock(true_block);
4052 if (false_block != fallthrough_block) {
4053 mature_immBlock(false_block);
4055 if (fallthrough_block != NULL) {
4056 mature_immBlock(fallthrough_block);
4059 set_cur_block(fallthrough_block);
4062 static void while_statement_to_firm(while_statement_t *statement)
4064 ir_node *jmp = NULL;
4065 if(get_cur_block() != NULL) {
4069 /* create the header block */
4070 ir_node *header_block = new_immBlock();
4072 add_immBlock_pred(header_block, jmp);
4075 /* the false block */
4076 ir_node *false_block = new_immBlock();
4079 ir_node *body_block;
4080 if (statement->body != NULL) {
4081 ir_node *old_continue_label = continue_label;
4082 ir_node *old_break_label = break_label;
4083 continue_label = header_block;
4084 break_label = false_block;
4086 body_block = new_immBlock();
4087 statement_to_firm(statement->body);
4089 assert(continue_label == header_block);
4090 assert(break_label == false_block);
4091 continue_label = old_continue_label;
4092 break_label = old_break_label;
4094 if(get_cur_block() != NULL) {
4096 add_immBlock_pred(header_block, jmp);
4099 body_block = header_block;
4102 /* create the condition */
4103 set_cur_block(header_block);
4105 create_condition_evaluation(statement->condition, body_block, false_block);
4106 mature_immBlock(body_block);
4107 mature_immBlock(false_block);
4108 mature_immBlock(header_block);
4110 set_cur_block(false_block);
4113 static void do_while_statement_to_firm(do_while_statement_t *statement)
4115 ir_node *jmp = NULL;
4116 if(get_cur_block() != NULL) {
4120 /* create the header block */
4121 ir_node *header_block = new_immBlock();
4123 /* the false block */
4124 ir_node *false_block = new_immBlock();
4127 ir_node *body_block = new_immBlock();
4129 add_immBlock_pred(body_block, jmp);
4132 if (statement->body != NULL) {
4133 ir_node *old_continue_label = continue_label;
4134 ir_node *old_break_label = break_label;
4135 continue_label = header_block;
4136 break_label = false_block;
4138 statement_to_firm(statement->body);
4140 assert(continue_label == header_block);
4141 assert(break_label == false_block);
4142 continue_label = old_continue_label;
4143 break_label = old_break_label;
4145 if (get_cur_block() == NULL) {
4146 mature_immBlock(header_block);
4147 mature_immBlock(body_block);
4148 mature_immBlock(false_block);
4153 ir_node *body_jmp = new_Jmp();
4154 add_immBlock_pred(header_block, body_jmp);
4155 mature_immBlock(header_block);
4157 /* create the condition */
4158 set_cur_block(header_block);
4160 create_condition_evaluation(statement->condition, body_block, false_block);
4161 mature_immBlock(body_block);
4162 mature_immBlock(false_block);
4163 mature_immBlock(header_block);
4165 set_cur_block(false_block);
4168 static void for_statement_to_firm(for_statement_t *statement)
4170 ir_node *jmp = NULL;
4172 /* create declarations */
4173 declaration_t *declaration = statement->scope.declarations;
4174 for( ; declaration != NULL; declaration = declaration->next) {
4175 create_local_declaration(declaration);
4177 declaration = statement->scope.declarations;
4178 for( ; declaration != NULL; declaration = declaration->next) {
4179 initialize_local_declaration(declaration);
4182 if (get_cur_block() != NULL) {
4183 if(statement->initialisation != NULL) {
4184 expression_to_firm(statement->initialisation);
4191 /* create the step block */
4192 ir_node *const step_block = new_immBlock();
4193 if (statement->step != NULL) {
4194 expression_to_firm(statement->step);
4196 ir_node *const step_jmp = new_Jmp();
4198 /* create the header block */
4199 ir_node *const header_block = new_immBlock();
4201 add_immBlock_pred(header_block, jmp);
4203 add_immBlock_pred(header_block, step_jmp);
4205 /* the false block */
4206 ir_node *const false_block = new_immBlock();
4209 ir_node * body_block;
4210 if (statement->body != NULL) {
4211 ir_node *const old_continue_label = continue_label;
4212 ir_node *const old_break_label = break_label;
4213 continue_label = step_block;
4214 break_label = false_block;
4216 body_block = new_immBlock();
4217 statement_to_firm(statement->body);
4219 assert(continue_label == step_block);
4220 assert(break_label == false_block);
4221 continue_label = old_continue_label;
4222 break_label = old_break_label;
4224 if (get_cur_block() != NULL) {
4226 add_immBlock_pred(step_block, jmp);
4229 body_block = step_block;
4232 /* create the condition */
4233 set_cur_block(header_block);
4234 if (statement->condition != NULL) {
4235 create_condition_evaluation(statement->condition, body_block,
4238 keep_alive(header_block);
4240 add_immBlock_pred(body_block, jmp);
4243 mature_immBlock(body_block);
4244 mature_immBlock(false_block);
4245 mature_immBlock(step_block);
4246 mature_immBlock(header_block);
4247 mature_immBlock(false_block);
4249 set_cur_block(false_block);
4252 static void create_jump_statement(const statement_t *statement,
4253 ir_node *target_block)
4255 if(get_cur_block() == NULL)
4258 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4259 ir_node *jump = new_d_Jmp(dbgi);
4260 add_immBlock_pred(target_block, jump);
4262 set_cur_block(NULL);
4265 static ir_node *get_break_label(void)
4267 if (break_label == NULL) {
4268 ir_node *cur_block = get_cur_block();
4269 break_label = new_immBlock();
4270 set_cur_block(cur_block);
4275 static void switch_statement_to_firm(const switch_statement_t *statement)
4277 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4279 ir_node *expression = expression_to_firm(statement->expression);
4280 ir_node *cond = new_d_Cond(dbgi, expression);
4282 set_cur_block(NULL);
4284 ir_node *const old_switch_cond = current_switch_cond;
4285 ir_node *const old_break_label = break_label;
4286 const bool old_saw_default_label = saw_default_label;
4287 saw_default_label = false;
4288 current_switch_cond = cond;
4291 if (statement->body != NULL) {
4292 statement_to_firm(statement->body);
4295 if (get_cur_block() != NULL) {
4296 ir_node *jmp = new_Jmp();
4297 add_immBlock_pred(get_break_label(), jmp);
4300 if (!saw_default_label) {
4301 set_cur_block(get_nodes_block(cond));
4302 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4303 MAGIC_DEFAULT_PN_NUMBER);
4304 add_immBlock_pred(get_break_label(), proj);
4307 if (break_label != NULL) {
4308 mature_immBlock(break_label);
4310 set_cur_block(break_label);
4312 assert(current_switch_cond == cond);
4313 current_switch_cond = old_switch_cond;
4314 break_label = old_break_label;
4315 saw_default_label = old_saw_default_label;
4318 static void case_label_to_firm(const case_label_statement_t *statement)
4320 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4322 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4324 /* let's create a node and hope firm constant folding creates a Const
4327 ir_node *old_block = get_nodes_block(current_switch_cond);
4328 ir_node *block = new_immBlock();
4330 set_cur_block(old_block);
4331 if(statement->expression != NULL) {
4332 long start_pn = fold_constant(statement->expression);
4333 long end_pn = start_pn;
4334 if (statement->end_range != NULL) {
4335 end_pn = fold_constant(statement->end_range);
4337 assert(start_pn <= end_pn);
4338 /* create jumps for all cases in the given range */
4339 for (long pn = start_pn; pn <= end_pn; ++pn) {
4340 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
4341 /* oops someone detected our cheating... */
4342 panic("magic default pn used");
4344 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4345 add_immBlock_pred(block, proj);
4348 saw_default_label = true;
4349 proj = new_d_defaultProj(dbgi, current_switch_cond,
4350 MAGIC_DEFAULT_PN_NUMBER);
4352 add_immBlock_pred(block, proj);
4355 if (fallthrough != NULL) {
4356 add_immBlock_pred(block, fallthrough);
4358 mature_immBlock(block);
4359 set_cur_block(block);
4361 if(statement->statement != NULL) {
4362 statement_to_firm(statement->statement);
4366 static ir_node *get_label_block(declaration_t *label)
4368 assert(label->namespc == NAMESPACE_LABEL);
4370 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4371 return label->v.block;
4373 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4375 ir_node *old_cur_block = get_cur_block();
4376 ir_node *block = new_immBlock();
4377 set_cur_block(old_cur_block);
4379 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4380 label->v.block = block;
4382 ARR_APP1(ir_node *, imature_blocks, block);
4387 static void label_to_firm(const label_statement_t *statement)
4389 ir_node *block = get_label_block(statement->label);
4391 if(get_cur_block() != NULL) {
4392 ir_node *jmp = new_Jmp();
4393 add_immBlock_pred(block, jmp);
4396 set_cur_block(block);
4399 if(statement->statement != NULL) {
4400 statement_to_firm(statement->statement);
4404 static void goto_to_firm(const goto_statement_t *statement)
4406 if(get_cur_block() == NULL)
4409 ir_node *block = get_label_block(statement->label);
4410 ir_node *jmp = new_Jmp();
4411 add_immBlock_pred(block, jmp);
4413 set_cur_block(NULL);
4416 typedef enum modifier_t {
4417 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4418 ASM_MODIFIER_READ_WRITE = 1 << 1,
4419 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4420 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4423 static void asm_statement_to_firm(const asm_statement_t *statement)
4426 fprintf(stderr, "WARNING asm not implemented yet!\n");
4428 bool needs_memory = false;
4430 size_t n_clobbers = 0;
4431 asm_clobber_t *clobber = statement->clobbers;
4432 for( ; clobber != NULL; clobber = clobber->next) {
4433 if(strcmp(clobber->clobber, "memory") == 0) {
4434 needs_memory = true;
4438 ident *id = new_id_from_str(clobber->clobber);
4439 obstack_ptr_grow(&asm_obst, id);
4442 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4443 ident **clobbers = NULL;
4444 if(n_clobbers > 0) {
4445 clobbers = obstack_finish(&asm_obst);
4448 /* find and count input and output constraints */
4449 asm_constraint_t *constraint = statement->inputs;
4450 for( ; constraint != NULL; constraint = constraint->next) {
4452 bool supports_memop = false;
4453 for(const char *c = constraint->constraints; *c != 0; ++c) {
4454 /* TODO: improve error messages */
4458 panic("multiple alternative assembler constraints not "
4466 supports_memop = true;
4467 obstack_1grow(&asm_obst, *c);
4470 if(modifiers & ASM_MODIFIER_READ_WRITE)
4471 panic("inconsistent register constraints");
4472 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4475 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4476 panic("inconsistent register constraints");
4477 modifiers |= ASM_MODIFIER_READ_WRITE;
4480 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4481 panic("early clobber assembler constraint not supported yet");
4484 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4485 panic("commutative assembler constraint not supported yet");
4488 /* skip register preferences stuff... */
4489 while(*c != 0 && *c != ',')
4493 /* skip register preferences stuff... */
4497 obstack_1grow(&asm_obst, *c);
4501 obstack_1grow(&asm_obst, '\0');
4502 const char *constraint_string = obstack_finish(&asm_obst);
4504 needs_memory |= supports_memop;
4505 if(supports_memop) {
4512 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4513 statement_to_firm(statement->try_statement);
4514 warningf(&statement->base.source_position, "structured exception handling ignored");
4517 static void leave_statement_to_firm(leave_statement_t *statement) {
4518 errorf(&statement->base.source_position, "__leave not supported yet");
4522 * Transform a statement.
4524 static void statement_to_firm(statement_t *statement)
4527 assert(!statement->base.transformed);
4528 statement->base.transformed = true;
4531 switch(statement->kind) {
4532 case STATEMENT_INVALID:
4533 panic("invalid statement found");
4535 case STATEMENT_EMPTY:
4538 case STATEMENT_COMPOUND:
4539 compound_statement_to_firm(&statement->compound);
4541 case STATEMENT_RETURN:
4542 return_statement_to_firm(&statement->returns);
4544 case STATEMENT_EXPRESSION:
4545 expression_statement_to_firm(&statement->expression);
4548 if_statement_to_firm(&statement->ifs);
4550 case STATEMENT_WHILE:
4551 while_statement_to_firm(&statement->whiles);
4553 case STATEMENT_DO_WHILE:
4554 do_while_statement_to_firm(&statement->do_while);
4556 case STATEMENT_DECLARATION:
4557 declaration_statement_to_firm(&statement->declaration);
4559 case STATEMENT_BREAK:
4560 create_jump_statement(statement, get_break_label());
4562 case STATEMENT_CONTINUE:
4563 create_jump_statement(statement, continue_label);
4565 case STATEMENT_SWITCH:
4566 switch_statement_to_firm(&statement->switchs);
4568 case STATEMENT_CASE_LABEL:
4569 case_label_to_firm(&statement->case_label);
4572 for_statement_to_firm(&statement->fors);
4574 case STATEMENT_LABEL:
4575 label_to_firm(&statement->label);
4577 case STATEMENT_GOTO:
4578 goto_to_firm(&statement->gotos);
4581 asm_statement_to_firm(&statement->asms);
4583 case STATEMENT_MS_TRY:
4584 ms_try_statement_to_firm(&statement->ms_try);
4586 case STATEMENT_LEAVE:
4587 leave_statement_to_firm(&statement->leave);
4590 panic("Statement not implemented\n");
4593 static int count_decls_in_expression(const expression_t *expression);
4595 static int count_local_declarations(const declaration_t * decl,
4596 const declaration_t *const end)
4599 for (; decl != end; decl = decl->next) {
4600 if(decl->namespc != NAMESPACE_NORMAL)
4602 const type_t *type = skip_typeref(decl->type);
4603 if (!decl->address_taken && is_type_scalar(type))
4605 const initializer_t *initializer = decl->init.initializer;
4606 /* FIXME: should walk initializer hierarchies... */
4607 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4608 count += count_decls_in_expression(initializer->value.value);
4614 static int count_decls_in_expression(const expression_t *expression) {
4617 if(expression == NULL)
4620 switch((expression_kind_t) expression->base.kind) {
4621 case EXPR_STATEMENT:
4622 return count_decls_in_stmts(expression->statement.statement);
4624 int count_left = count_decls_in_expression(expression->binary.left);
4625 int count_right = count_decls_in_expression(expression->binary.right);
4626 return count_left + count_right;
4629 return count_decls_in_expression(expression->unary.value);
4631 call_argument_t *argument = expression->call.arguments;
4632 for( ; argument != NULL; argument = argument->next) {
4633 count += count_decls_in_expression(argument->expression);
4640 panic("unexpected expression kind");
4642 case EXPR_COMPOUND_LITERAL:
4646 case EXPR_CONDITIONAL:
4647 count += count_decls_in_expression(expression->conditional.condition);
4648 count += count_decls_in_expression(expression->conditional.true_expression);
4649 count += count_decls_in_expression(expression->conditional.false_expression);
4652 case EXPR_BUILTIN_PREFETCH:
4653 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4654 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4655 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4658 case EXPR_BUILTIN_CONSTANT_P:
4659 count += count_decls_in_expression(expression->builtin_constant.value);
4663 count += count_decls_in_expression(expression->select.compound);
4666 case EXPR_ARRAY_ACCESS:
4667 count += count_decls_in_expression(expression->array_access.array_ref);
4668 count += count_decls_in_expression(expression->array_access.index);
4671 case EXPR_CLASSIFY_TYPE:
4672 count += count_decls_in_expression(expression->classify_type.type_expression);
4676 case EXPR_ALIGNOF: {
4677 expression_t *tp_expression = expression->typeprop.tp_expression;
4678 if (tp_expression != NULL) {
4679 count += count_decls_in_expression(tp_expression);
4685 case EXPR_REFERENCE:
4687 case EXPR_CHARACTER_CONSTANT:
4688 case EXPR_WIDE_CHARACTER_CONSTANT:
4689 case EXPR_STRING_LITERAL:
4690 case EXPR_WIDE_STRING_LITERAL:
4692 case EXPR_BUILTIN_SYMBOL:
4698 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4699 * (or implement all the missing expressions here/implement a walker)
4705 static int count_decls_in_stmts(const statement_t *stmt)
4708 for (; stmt != NULL; stmt = stmt->base.next) {
4709 switch (stmt->kind) {
4710 case STATEMENT_EMPTY:
4713 case STATEMENT_DECLARATION: {
4714 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4715 count += count_local_declarations(decl_stmt->declarations_begin,
4716 decl_stmt->declarations_end->next);
4720 case STATEMENT_COMPOUND: {
4721 const compound_statement_t *const comp =
4723 count += count_decls_in_stmts(comp->statements);
4727 case STATEMENT_IF: {
4728 const if_statement_t *const if_stmt = &stmt->ifs;
4729 count += count_decls_in_expression(if_stmt->condition);
4730 count += count_decls_in_stmts(if_stmt->true_statement);
4731 count += count_decls_in_stmts(if_stmt->false_statement);
4735 case STATEMENT_SWITCH: {
4736 const switch_statement_t *const switch_stmt = &stmt->switchs;
4737 count += count_decls_in_expression(switch_stmt->expression);
4738 count += count_decls_in_stmts(switch_stmt->body);
4742 case STATEMENT_LABEL: {
4743 const label_statement_t *const label_stmt = &stmt->label;
4744 if(label_stmt->statement != NULL) {
4745 count += count_decls_in_stmts(label_stmt->statement);
4750 case STATEMENT_WHILE: {
4751 const while_statement_t *const while_stmt = &stmt->whiles;
4752 count += count_decls_in_expression(while_stmt->condition);
4753 count += count_decls_in_stmts(while_stmt->body);
4757 case STATEMENT_DO_WHILE: {
4758 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4759 count += count_decls_in_expression(do_while_stmt->condition);
4760 count += count_decls_in_stmts(do_while_stmt->body);
4764 case STATEMENT_FOR: {
4765 const for_statement_t *const for_stmt = &stmt->fors;
4766 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4767 count += count_decls_in_expression(for_stmt->initialisation);
4768 count += count_decls_in_expression(for_stmt->condition);
4769 count += count_decls_in_expression(for_stmt->step);
4770 count += count_decls_in_stmts(for_stmt->body);
4774 case STATEMENT_CASE_LABEL: {
4775 const case_label_statement_t *label = &stmt->case_label;
4776 count += count_decls_in_expression(label->expression);
4777 if(label->statement != NULL) {
4778 count += count_decls_in_stmts(label->statement);
4784 case STATEMENT_BREAK:
4785 case STATEMENT_CONTINUE:
4788 case STATEMENT_EXPRESSION: {
4789 const expression_statement_t *expr_stmt = &stmt->expression;
4790 count += count_decls_in_expression(expr_stmt->expression);
4794 case STATEMENT_GOTO:
4795 case STATEMENT_LEAVE:
4796 case STATEMENT_INVALID:
4799 case STATEMENT_RETURN: {
4800 const return_statement_t *ret_stmt = &stmt->returns;
4801 count += count_decls_in_expression(ret_stmt->value);
4805 case STATEMENT_MS_TRY: {
4806 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
4807 count += count_decls_in_stmts(try_stmt->try_statement);
4808 if(try_stmt->except_expression != NULL)
4809 count += count_decls_in_expression(try_stmt->except_expression);
4810 count += count_decls_in_stmts(try_stmt->final_statement);
4818 static int get_function_n_local_vars(declaration_t *declaration)
4822 /* count parameters */
4823 count += count_local_declarations(declaration->scope.declarations, NULL);
4825 /* count local variables declared in body */
4826 count += count_decls_in_stmts(declaration->init.statement);
4831 static void initialize_function_parameters(declaration_t *declaration)
4833 ir_graph *irg = current_ir_graph;
4834 ir_node *args = get_irg_args(irg);
4835 ir_node *start_block = get_irg_start_block(irg);
4836 ir_type *function_irtype = get_ir_type(declaration->type);
4839 declaration_t *parameter = declaration->scope.declarations;
4840 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4841 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4842 type_t *type = skip_typeref(parameter->type);
4844 bool needs_entity = parameter->address_taken;
4845 assert(!is_type_array(type));
4846 if(is_type_compound(type)) {
4847 needs_entity = true;
4851 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4852 ident *id = new_id_from_str(parameter->symbol->string);
4853 set_entity_ident(entity, id);
4855 parameter->declaration_kind
4856 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4857 parameter->v.entity = entity;
4861 ir_type *param_irtype = get_method_param_type(function_irtype, n);
4862 ir_mode *param_mode = get_type_mode(param_irtype);
4865 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
4867 ir_mode *mode = get_ir_mode(parameter->type);
4868 value = create_conv(NULL, value, mode);
4869 value = do_strict_conv(NULL, value);
4871 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4872 parameter->v.value_number = next_value_number_function;
4873 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4874 ++next_value_number_function;
4876 set_value(parameter->v.value_number, value);
4881 * Handle additional decl modifiers for IR-graphs
4883 * @param irg the IR-graph
4884 * @param dec_modifiers additional modifiers
4886 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4888 if (decl_modifiers & DM_NORETURN) {
4889 /* TRUE if the declaration includes the Microsoft
4890 __declspec(noreturn) specifier. */
4891 set_irg_additional_property(irg, mtp_property_noreturn);
4893 if (decl_modifiers & DM_NOTHROW) {
4894 /* TRUE if the declaration includes the Microsoft
4895 __declspec(nothrow) specifier. */
4896 set_irg_additional_property(irg, mtp_property_nothrow);
4898 if (decl_modifiers & DM_NAKED) {
4899 /* TRUE if the declaration includes the Microsoft
4900 __declspec(naked) specifier. */
4901 set_irg_additional_property(irg, mtp_property_naked);
4903 if (decl_modifiers & DM_FORCEINLINE) {
4904 /* TRUE if the declaration includes the
4905 Microsoft __forceinline specifier. */
4906 set_irg_inline_property(irg, irg_inline_forced);
4908 if (decl_modifiers & DM_NOINLINE) {
4909 /* TRUE if the declaration includes the Microsoft
4910 __declspec(noinline) specifier. */
4911 set_irg_inline_property(irg, irg_inline_forbidden);
4916 * Create code for a function.
4918 static void create_function(declaration_t *declaration)
4920 ir_entity *function_entity = get_function_entity(declaration);
4922 if(declaration->init.statement == NULL)
4925 current_function_decl = declaration;
4926 current_function_name = NULL;
4927 current_funcsig = NULL;
4929 assert(imature_blocks == NULL);
4930 imature_blocks = NEW_ARR_F(ir_node*, 0);
4932 int n_local_vars = get_function_n_local_vars(declaration);
4933 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4935 set_irg_fp_model(irg, firm_opt.fp_model);
4936 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
4937 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
4939 ir_node *first_block = get_cur_block();
4941 /* set inline flags */
4942 if (declaration->is_inline)
4943 set_irg_inline_property(irg, irg_inline_recomended);
4944 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
4946 next_value_number_function = 0;
4947 initialize_function_parameters(declaration);
4949 statement_to_firm(declaration->init.statement);
4951 ir_node *end_block = get_irg_end_block(irg);
4953 /* do we have a return statement yet? */
4954 if(get_cur_block() != NULL) {
4955 type_t *type = skip_typeref(declaration->type);
4956 assert(is_type_function(type));
4957 const function_type_t *func_type = &type->function;
4958 const type_t *return_type
4959 = skip_typeref(func_type->return_type);
4962 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4963 ret = new_Return(get_store(), 0, NULL);
4966 if(is_type_scalar(return_type)) {
4967 mode = get_ir_mode(func_type->return_type);
4973 /* ยง5.1.2.2.3 main implicitly returns 0 */
4974 if (strcmp(declaration->symbol->string, "main") == 0) {
4975 in[0] = new_Const(mode, get_mode_null(mode));
4977 in[0] = new_Unknown(mode);
4978 if(warning.return_type) {
4979 warningf(&declaration->source_position,
4980 "missing return statement at end of non-void function '%Y'",
4981 declaration->symbol);
4984 ret = new_Return(get_store(), 1, in);
4986 add_immBlock_pred(end_block, ret);
4989 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4990 mature_immBlock(imature_blocks[i]);
4992 DEL_ARR_F(imature_blocks);
4993 imature_blocks = NULL;
4995 mature_immBlock(first_block);
4996 mature_immBlock(end_block);
4998 irg_finalize_cons(irg);
5000 /* finalize the frame type */
5001 ir_type *frame_type = get_irg_frame_type(irg);
5002 int n = get_compound_n_members(frame_type);
5005 for(int i = 0; i < n; ++i) {
5006 ir_entity *entity = get_compound_member(frame_type, i);
5007 ir_type *entity_type = get_entity_type(entity);
5009 int align = get_type_alignment_bytes(entity_type);
5010 if(align > align_all)
5014 misalign = offset % align;
5016 offset += align - misalign;
5020 set_entity_offset(entity, offset);
5021 offset += get_type_size_bytes(entity_type);
5023 set_type_size_bytes(frame_type, offset);
5024 set_type_alignment_bytes(frame_type, align_all);
5029 static void scope_to_firm(scope_t *scope)
5031 /* first pass: create declarations */
5032 declaration_t *declaration = scope->declarations;
5033 for( ; declaration != NULL; declaration = declaration->next) {
5034 if(declaration->namespc != NAMESPACE_NORMAL)
5036 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5037 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5039 if(declaration->symbol == NULL)
5042 type_t *type = skip_typeref(declaration->type);
5043 if(is_type_function(type)) {
5044 get_function_entity(declaration);
5046 create_global_variable(declaration);
5050 /* second pass: create code/initializers */
5051 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 = declaration->type;
5062 if(type->kind == TYPE_FUNCTION) {
5063 create_function(declaration);
5065 assert(declaration->declaration_kind
5066 == DECLARATION_KIND_GLOBAL_VARIABLE);
5067 current_ir_graph = get_const_code_irg();
5068 create_declaration_initializer(declaration);
5073 void init_ast2firm(void)
5075 obstack_init(&asm_obst);
5076 init_atomic_modes();
5078 id_underscore = new_id_from_chars("_", 1);
5079 id_imp = new_id_from_chars("__imp_", 6);
5081 /* OS option must be set to the backend */
5082 const char *s = "ia32-gasmode=linux";
5083 switch (firm_opt.os_support) {
5084 case OS_SUPPORT_MINGW:
5085 create_ld_ident = create_ld_ident_win32;
5086 s = "ia32-gasmode=mingw";
5088 case OS_SUPPORT_LINUX:
5089 create_ld_ident = create_ld_ident_linux_elf;
5090 s = "ia32-gasmode=linux"; break;
5092 case OS_SUPPORT_MACHO:
5093 create_ld_ident = create_ld_ident_macho;
5094 s = "ia32-gasmode=macho"; break;
5099 /* create idents for all known runtime functions */
5100 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5101 rts_idents[i] = new_id_from_str(rts_data[i].name);
5104 entitymap_init(&entitymap);
5107 static void init_ir_types(void)
5109 static int ir_types_initialized = 0;
5110 if(ir_types_initialized)
5112 ir_types_initialized = 1;
5114 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
5115 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5116 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5118 ir_type_int = get_ir_type(type_int);
5119 ir_type_const_char = get_ir_type(type_const_char);
5120 ir_type_wchar_t = get_ir_type(type_wchar_t);
5121 ir_type_void = get_ir_type(type_void);
5124 void exit_ast2firm(void)
5126 entitymap_destroy(&entitymap);
5127 obstack_free(&asm_obst, NULL);
5130 void translation_unit_to_firm(translation_unit_t *unit)
5132 /* just to be sure */
5133 continue_label = NULL;
5135 current_switch_cond = NULL;
5139 scope_to_firm(&unit->scope);