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>
28 #include <libfirm/be.h>
32 #include "adt/error.h"
33 #include "adt/array.h"
39 #include "diagnostic.h"
40 #include "lang_features.h"
43 #include "entitymap_t.h"
44 #include "driver/firm_opt.h"
45 #include "driver/firm_cmdline.h"
47 /* some idents needed for name mangling */
48 static ident *id_underscore;
51 static ir_type *ir_type_const_char;
52 static ir_type *ir_type_wchar_t;
53 static ir_type *ir_type_void;
54 static ir_type *ir_type_int;
56 static int next_value_number_function;
57 static ir_node *continue_label;
58 static ir_node *break_label;
59 static ir_node *current_switch_cond;
60 static bool saw_default_label;
61 static ir_node **imature_blocks;
62 static bool constant_folding;
64 static const declaration_t *current_function_decl;
65 static ir_node *current_function_name;
66 static ir_node *current_funcsig;
67 static switch_statement_t *current_switch;
69 static entitymap_t entitymap;
71 static struct obstack asm_obst;
73 typedef enum declaration_kind_t {
74 DECLARATION_KIND_UNKNOWN,
75 DECLARATION_KIND_FUNCTION,
76 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
77 DECLARATION_KIND_GLOBAL_VARIABLE,
78 DECLARATION_KIND_LOCAL_VARIABLE,
79 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
80 DECLARATION_KIND_COMPOUND_MEMBER,
81 DECLARATION_KIND_LABEL_BLOCK,
82 DECLARATION_KIND_ENUM_ENTRY,
83 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
84 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
88 static ir_type *get_ir_type(type_t *type);
89 static ir_type *get_ir_type_incomplete(type_t *type);
90 static int count_decls_in_stmts(const statement_t *stmt);
92 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
94 const declaration_t *declaration = get_irg_loc_description(irg, pos);
96 if (declaration != NULL) {
97 warningf(&declaration->source_position,
98 "variable '%#T' might be used uninitialized",
99 declaration->type, declaration->symbol);
101 return new_r_Unknown(irg, mode);
104 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
106 const source_position_t *pos = (const source_position_t*) dbg;
109 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
113 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
115 const source_position_t *pos = (const source_position_t*) dbg;
120 return pos->input_name;
123 static dbg_info *get_dbg_info(const source_position_t *pos)
125 return (dbg_info*) pos;
128 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
130 static ir_mode *mode_int, *mode_uint;
132 static ir_node *_expression_to_firm(const expression_t *expression);
133 static ir_node *expression_to_firm(const expression_t *expression);
134 static inline ir_mode *get_ir_mode(type_t *type);
135 static void create_local_declaration(declaration_t *declaration);
137 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
139 unsigned flags = get_atomic_type_flags(kind);
140 unsigned size = get_atomic_type_size(kind);
141 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
142 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
145 unsigned bit_size = size * 8;
146 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
147 ir_mode_arithmetic arithmetic;
148 unsigned modulo_shift;
150 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
151 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
152 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
153 sort = irms_int_number;
154 arithmetic = irma_twos_complement;
155 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
157 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
158 snprintf(name, sizeof(name), "f%d", bit_size);
159 sort = irms_float_number;
160 arithmetic = irma_ieee754;
163 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
171 * Initialises the atomic modes depending on the machine size.
173 static void init_atomic_modes(void)
175 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
176 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
178 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
179 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
181 /* there's no real void type in firm */
182 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
184 /* initialize pointer modes */
186 ir_mode_sort sort = irms_reference;
187 unsigned bit_size = machine_size;
189 ir_mode_arithmetic arithmetic = irma_twos_complement;
190 unsigned modulo_shift
191 = bit_size < machine_size ? machine_size : bit_size;
193 snprintf(name, sizeof(name), "p%d", machine_size);
194 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
197 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
198 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
200 /* Hmm, pointers should be machine size */
201 set_modeP_data(ptr_mode);
202 set_modeP_code(ptr_mode);
205 static unsigned get_compound_type_size(compound_type_t *type)
207 ir_type *irtype = get_ir_type((type_t*) type);
208 return get_type_size_bytes(irtype);
211 static unsigned get_array_type_size(array_type_t *type)
213 assert(!type->is_vla);
214 ir_type *irtype = get_ir_type((type_t*) type);
215 return get_type_size_bytes(irtype);
219 static unsigned get_type_size_const(type_t *type)
223 panic("error type occurred");
225 return get_atomic_type_size(type->atomic.akind);
227 return 2 * get_atomic_type_size(type->complex.akind);
229 return get_atomic_type_size(type->imaginary.akind);
231 return get_mode_size_bytes(mode_int);
232 case TYPE_COMPOUND_UNION:
233 case TYPE_COMPOUND_STRUCT:
234 return get_compound_type_size(&type->compound);
236 /* just a pointer to the function */
237 return get_mode_size_bytes(mode_P_code);
239 return get_mode_size_bytes(mode_P_data);
241 return get_array_type_size(&type->array);
243 return get_type_size_const(type->builtin.real_type);
245 panic("type size of bitfield request");
251 panic("Trying to determine size of invalid type");
254 static ir_node *get_type_size(type_t *type)
256 type = skip_typeref(type);
258 if (is_type_array(type) && type->array.is_vla) {
259 ir_node *size_node = type->array.size_node;
260 if (size_node == NULL) {
261 size_node = expression_to_firm(type->array.size_expression);
262 assert(!is_Const(size_node));
263 type->array.size_node = size_node;
266 ir_node *elem_size = get_type_size(type->array.element_type);
267 ir_mode *mode = get_irn_mode(size_node);
268 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
272 ir_mode *mode = get_ir_mode(type_size_t);
274 sym.type_p = get_ir_type(type);
275 return new_SymConst(mode, sym, symconst_type_size);
278 static unsigned count_parameters(const function_type_t *function_type)
282 function_parameter_t *parameter = function_type->parameters;
283 for ( ; parameter != NULL; parameter = parameter->next) {
291 * Creates a Firm type for an atomic type
293 static ir_type *create_atomic_type(const atomic_type_t *type)
295 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
296 atomic_type_kind_t kind = type->akind;
297 ir_mode *mode = _atomic_modes[kind];
298 ident *id = get_mode_ident(mode);
299 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
301 set_type_alignment_bytes(irtype, type->base.alignment);
307 * Creates a Firm type for a complex type
309 static ir_type *create_complex_type(const complex_type_t *type)
311 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
312 atomic_type_kind_t kind = type->akind;
313 ir_mode *mode = _atomic_modes[kind];
314 ident *id = get_mode_ident(mode);
319 /* FIXME: finish the array */
324 * Creates a Firm type for an imaginary type
326 static ir_type *create_imaginary_type(const imaginary_type_t *type)
328 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
329 atomic_type_kind_t kind = type->akind;
330 ir_mode *mode = _atomic_modes[kind];
331 ident *id = get_mode_ident(mode);
332 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
334 set_type_alignment_bytes(irtype, type->base.alignment);
340 * return type of a parameter (and take transparent union gnu extension into
343 static type_t *get_parameter_type(type_t *type)
345 type = skip_typeref(type);
346 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
347 declaration_t *decl = type->compound.declaration;
348 type = decl->scope.declarations->type;
354 static ir_type *create_method_type(const function_type_t *function_type)
356 type_t *return_type = function_type->return_type;
358 ident *id = id_unique("functiontype.%u");
359 int n_parameters = count_parameters(function_type);
360 int n_results = return_type == type_void ? 0 : 1;
361 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
362 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
364 if (return_type != type_void) {
365 ir_type *restype = get_ir_type(return_type);
366 set_method_res_type(irtype, 0, restype);
369 function_parameter_t *parameter = function_type->parameters;
371 for ( ; parameter != NULL; parameter = parameter->next) {
372 type_t *type = get_parameter_type(parameter->type);
373 ir_type *p_irtype = get_ir_type(type);
374 set_method_param_type(irtype, n, p_irtype);
378 if (function_type->variadic || function_type->unspecified_parameters) {
379 set_method_variadicity(irtype, variadicity_variadic);
382 unsigned cc = get_method_calling_convention(irtype);
383 switch (function_type->calling_convention) {
384 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
387 set_method_calling_convention(irtype, SET_CDECL(cc));
391 if (function_type->variadic || function_type->unspecified_parameters)
394 /* only non-variadic function can use stdcall, else use cdecl */
395 set_method_calling_convention(irtype, SET_STDCALL(cc));
399 if (function_type->variadic || function_type->unspecified_parameters)
401 /* only non-variadic function can use fastcall, else use cdecl */
402 set_method_calling_convention(irtype, SET_FASTCALL(cc));
406 /* Hmm, leave default, not accepted by the parser yet. */
407 warningf(&function_type->base.source_position, "THISCALL calling convention not supported yet");
413 static ir_type *create_pointer_type(pointer_type_t *type)
415 type_t *points_to = type->points_to;
416 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
417 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
418 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
419 ir_points_to, mode_P_data, dbgi);
424 static ir_type *create_array_type(array_type_t *type)
426 type_t *element_type = type->element_type;
427 ir_type *ir_element_type = get_ir_type(element_type);
429 ident *id = id_unique("array.%u");
430 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
431 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
433 const int align = get_type_alignment_bytes(ir_element_type);
434 set_type_alignment_bytes(ir_type, align);
436 if (type->size_constant) {
437 int n_elements = type->size;
439 set_array_bounds_int(ir_type, 0, 0, n_elements);
441 size_t elemsize = get_type_size_bytes(ir_element_type);
442 if (elemsize % align > 0) {
443 elemsize += align - (elemsize % align);
445 set_type_size_bytes(ir_type, n_elements * elemsize);
447 set_array_lower_bound_int(ir_type, 0, 0);
449 set_type_state(ir_type, layout_fixed);
455 * Return the signed integer type of size bits.
457 * @param size the size
459 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
462 static ir_mode *s_modes[64 + 1] = {NULL, };
466 if (size <= 0 || size > 64)
469 mode = s_modes[size];
473 snprintf(name, sizeof(name), "bf_I%u", size);
474 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
475 size <= 32 ? 32 : size );
476 s_modes[size] = mode;
480 snprintf(name, sizeof(name), "I%u", size);
481 ident *id = new_id_from_str(name);
482 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
483 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
484 set_primitive_base_type(res, base_tp);
490 * Return the unsigned integer type of size bits.
492 * @param size the size
494 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
497 static ir_mode *u_modes[64 + 1] = {NULL, };
501 if (size <= 0 || size > 64)
504 mode = u_modes[size];
508 snprintf(name, sizeof(name), "bf_U%u", size);
509 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
510 size <= 32 ? 32 : size );
511 u_modes[size] = mode;
516 snprintf(name, sizeof(name), "U%u", size);
517 ident *id = new_id_from_str(name);
518 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
519 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
520 set_primitive_base_type(res, base_tp);
525 static ir_type *create_bitfield_type(bitfield_type_t *const type)
527 type_t *base = skip_typeref(type->base_type);
528 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
529 ir_type *irbase = get_ir_type(base);
531 unsigned size = fold_constant(type->size);
533 assert(!is_type_float(base));
534 if (is_type_signed(base)) {
535 return get_signed_int_type_for_bit_size(irbase, size);
537 return get_unsigned_int_type_for_bit_size(irbase, size);
541 #define INVALID_TYPE ((ir_type_ptr)-1)
544 COMPOUND_IS_STRUCT = false,
545 COMPOUND_IS_UNION = true
549 * Construct firm type from ast struct type.
551 * As anonymous inner structs get flattened to a single firm type, we might get
552 * irtype, outer_offset and out_align passed (they represent the position of
553 * the anonymous inner struct inside the resulting firm struct)
555 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
556 size_t *outer_offset, size_t *outer_align,
557 bool incomplete, bool is_union)
559 declaration_t *declaration = type->declaration;
560 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
562 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
563 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
565 return declaration->v.irtype;
567 size_t align_all = 1;
569 size_t bit_offset = 0;
572 if (irtype == NULL) {
573 symbol_t *symbol = declaration->symbol;
575 if (symbol != NULL) {
576 id = new_id_from_str(symbol->string);
579 id = id_unique("__anonymous_union.%u");
581 id = id_unique("__anonymous_struct.%u");
584 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
587 irtype = new_d_type_union(id, dbgi);
589 irtype = new_d_type_struct(id, dbgi);
592 declaration->declaration_kind
593 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
594 declaration->v.irtype = irtype;
595 //type->base.firm_type = irtype;
597 offset = *outer_offset;
598 align_all = *outer_align;
604 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
606 declaration_t *entry = declaration->scope.declarations;
607 for( ; entry != NULL; entry = entry->next) {
608 if (entry->namespc != NAMESPACE_NORMAL)
611 size_t prev_offset = offset;
613 symbol_t *symbol = entry->symbol;
614 type_t *entry_type = skip_typeref(entry->type);
615 dbg_info *dbgi = get_dbg_info(&entry->source_position);
618 if (symbol != NULL) {
619 ident = new_id_from_str(symbol->string);
621 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
622 create_compound_type(&entry_type->compound, irtype, &offset,
623 &align_all, false, COMPOUND_IS_STRUCT);
624 goto finished_member;
625 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
626 create_compound_type(&entry_type->compound, irtype, &offset,
627 &align_all, false, COMPOUND_IS_UNION);
628 goto finished_member;
630 assert(entry_type->kind == TYPE_BITFIELD);
632 ident = id_unique("anon.%u");
635 ir_type *base_irtype;
636 if (entry_type->kind == TYPE_BITFIELD) {
637 base_irtype = get_ir_type(entry_type->bitfield.base_type);
639 base_irtype = get_ir_type(entry_type);
642 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
643 size_t misalign = offset % entry_alignment;
645 ir_type *entry_irtype = get_ir_type(entry_type);
646 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
649 size_t bits_remainder;
650 if (entry_type->kind == TYPE_BITFIELD) {
651 size_t size_bits = fold_constant(entry_type->bitfield.size);
652 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
654 if (size_bits > rest_size_bits) {
655 /* start a new bucket */
656 offset += entry_alignment - misalign;
662 /* put into current bucket */
663 base = offset - misalign;
664 bits_remainder = misalign * 8 + bit_offset;
667 offset += size_bits / 8;
668 bit_offset = bit_offset + (size_bits % 8);
670 size_t entry_size = get_type_size_bytes(base_irtype);
671 if (misalign > 0 || bit_offset > 0)
672 offset += entry_alignment - misalign;
676 offset += entry_size;
680 if (entry_alignment > align_all) {
681 if (entry_alignment % align_all != 0) {
682 panic("uneven alignments not supported yet");
684 align_all = entry_alignment;
687 set_entity_offset(entity, base);
688 set_entity_offset_bits_remainder(entity,
689 (unsigned char) bits_remainder);
690 //add_struct_member(irtype, entity);
691 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
692 assert(entry->v.entity == NULL);
693 entry->v.entity = entity;
697 size_t entry_size = offset - prev_offset;
698 if (entry_size > size) {
710 size_t misalign = offset % align_all;
711 if (misalign > 0 || bit_offset > 0) {
712 size += align_all - misalign;
715 if (outer_offset != NULL) {
717 *outer_offset = offset;
719 *outer_offset += size;
722 if (align_all > *outer_align) {
723 if (align_all % *outer_align != 0) {
724 panic("uneven alignments not supported yet");
726 *outer_align = align_all;
729 set_type_alignment_bytes(irtype, align_all);
730 set_type_size_bytes(irtype, size);
731 set_type_state(irtype, layout_fixed);
737 static ir_type *create_enum_type(enum_type_t *const type)
739 type->base.firm_type = ir_type_int;
741 ir_mode *const mode = mode_int;
742 tarval *const one = get_mode_one(mode);
743 tarval * tv_next = get_tarval_null(mode);
745 bool constant_folding_old = constant_folding;
746 constant_folding = true;
748 declaration_t *declaration = type->declaration->next;
749 for (; declaration != NULL; declaration = declaration->next) {
750 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
753 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
755 expression_t *const init = declaration->init.enum_value;
757 ir_node *const cnst = expression_to_firm(init);
758 if (!is_Const(cnst)) {
759 panic("couldn't fold constant");
761 tv_next = get_Const_tarval(cnst);
763 declaration->v.enum_val = tv_next;
764 tv_next = tarval_add(tv_next, one);
767 constant_folding = constant_folding_old;
772 static ir_type *get_ir_type_incomplete(type_t *type)
774 assert(type != NULL);
775 type = skip_typeref(type);
777 if (type->base.firm_type != NULL) {
778 assert(type->base.firm_type != INVALID_TYPE);
779 return type->base.firm_type;
782 switch (type->kind) {
783 case TYPE_COMPOUND_STRUCT:
784 return create_compound_type(&type->compound, NULL, NULL, NULL,
785 true, COMPOUND_IS_STRUCT);
786 case TYPE_COMPOUND_UNION:
787 return create_compound_type(&type->compound, NULL, NULL, NULL,
788 true, COMPOUND_IS_UNION);
790 return get_ir_type(type);
794 static ir_type *get_ir_type(type_t *type)
796 assert(type != NULL);
798 type = skip_typeref(type);
800 if (type->base.firm_type != NULL) {
801 assert(type->base.firm_type != INVALID_TYPE);
802 return type->base.firm_type;
805 ir_type *firm_type = NULL;
806 switch (type->kind) {
808 panic("error type occurred");
810 firm_type = create_atomic_type(&type->atomic);
813 firm_type = create_complex_type(&type->complex);
816 firm_type = create_imaginary_type(&type->imaginary);
819 firm_type = create_method_type(&type->function);
822 firm_type = create_pointer_type(&type->pointer);
825 firm_type = create_array_type(&type->array);
827 case TYPE_COMPOUND_STRUCT:
828 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
829 false, COMPOUND_IS_STRUCT);
831 case TYPE_COMPOUND_UNION:
832 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
833 false, COMPOUND_IS_UNION);
836 firm_type = create_enum_type(&type->enumt);
839 firm_type = get_ir_type(type->builtin.real_type);
842 firm_type = create_bitfield_type(&type->bitfield);
850 if (firm_type == NULL)
851 panic("unknown type found");
853 type->base.firm_type = firm_type;
857 static inline ir_mode *get_ir_mode(type_t *type)
859 ir_type *irtype = get_ir_type(type);
861 /* firm doesn't report a mode for arrays somehow... */
862 if (is_Array_type(irtype)) {
866 ir_mode *mode = get_type_mode(irtype);
867 assert(mode != NULL);
871 /** Names of the runtime functions. */
872 static const struct {
873 int id; /**< the rts id */
874 int n_res; /**< number of return values */
875 const char *name; /**< the name of the rts function */
876 int n_params; /**< number of parameters */
877 unsigned flags; /**< language flags */
879 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
880 { rts_abort, 0, "abort", 0, _C89 },
881 { rts_alloca, 1, "alloca", 1, _ALL },
882 { rts_abs, 1, "abs", 1, _C89 },
883 { rts_labs, 1, "labs", 1, _C89 },
884 { rts_llabs, 1, "llabs", 1, _C99 },
885 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
887 { rts_fabs, 1, "fabs", 1, _C89 },
888 { rts_sqrt, 1, "sqrt", 1, _C89 },
889 { rts_cbrt, 1, "cbrt", 1, _C99 },
890 { rts_exp, 1, "exp", 1, _C89 },
891 { rts_exp2, 1, "exp2", 1, _C89 },
892 { rts_exp10, 1, "exp10", 1, _GNUC },
893 { rts_log, 1, "log", 1, _C89 },
894 { rts_log2, 1, "log2", 1, _C89 },
895 { rts_log10, 1, "log10", 1, _C89 },
896 { rts_pow, 1, "pow", 2, _C89 },
897 { rts_sin, 1, "sin", 1, _C89 },
898 { rts_cos, 1, "cos", 1, _C89 },
899 { rts_tan, 1, "tan", 1, _C89 },
900 { rts_asin, 1, "asin", 1, _C89 },
901 { rts_acos, 1, "acos", 1, _C89 },
902 { rts_atan, 1, "atan", 1, _C89 },
903 { rts_sinh, 1, "sinh", 1, _C89 },
904 { rts_cosh, 1, "cosh", 1, _C89 },
905 { rts_tanh, 1, "tanh", 1, _C89 },
907 { rts_fabsf, 1, "fabsf", 1, _C99 },
908 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
909 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
910 { rts_expf, 1, "expf", 1, _C99 },
911 { rts_exp2f, 1, "exp2f", 1, _C99 },
912 { rts_exp10f, 1, "exp10f", 1, _GNUC },
913 { rts_logf, 1, "logf", 1, _C99 },
914 { rts_log2f, 1, "log2f", 1, _C99 },
915 { rts_log10f, 1, "log10f", 1, _C99 },
916 { rts_powf, 1, "powf", 2, _C99 },
917 { rts_sinf, 1, "sinf", 1, _C99 },
918 { rts_cosf, 1, "cosf", 1, _C99 },
919 { rts_tanf, 1, "tanf", 1, _C99 },
920 { rts_asinf, 1, "asinf", 1, _C99 },
921 { rts_acosf, 1, "acosf", 1, _C99 },
922 { rts_atanf, 1, "atanf", 1, _C99 },
923 { rts_sinhf, 1, "sinhf", 1, _C99 },
924 { rts_coshf, 1, "coshf", 1, _C99 },
925 { rts_tanhf, 1, "tanhf", 1, _C99 },
927 { rts_fabsl, 1, "fabsl", 1, _C99 },
928 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
929 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
930 { rts_expl, 1, "expl", 1, _C99 },
931 { rts_exp2l, 1, "exp2l", 1, _C99 },
932 { rts_exp10l, 1, "exp10l", 1, _GNUC },
933 { rts_logl, 1, "logl", 1, _C99 },
934 { rts_log2l, 1, "log2l", 1, _C99 },
935 { rts_log10l, 1, "log10l", 1, _C99 },
936 { rts_powl, 1, "powl", 2, _C99 },
937 { rts_sinl, 1, "sinl", 1, _C99 },
938 { rts_cosl, 1, "cosl", 1, _C99 },
939 { rts_tanl, 1, "tanl", 1, _C99 },
940 { rts_asinl, 1, "asinl", 1, _C99 },
941 { rts_acosl, 1, "acosl", 1, _C99 },
942 { rts_atanl, 1, "atanl", 1, _C99 },
943 { rts_sinhl, 1, "sinhl", 1, _C99 },
944 { rts_coshl, 1, "coshl", 1, _C99 },
945 { rts_tanhl, 1, "tanhl", 1, _C99 },
947 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
948 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
949 { rts_strcmp, 1, "strcmp", 2, _C89 },
950 { rts_strncmp, 1, "strncmp", 3, _C89 }
953 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
956 * Mangles an entity linker (ld) name for win32 usage.
958 * @param ent the entity to be mangled
959 * @param declaration the declaration
961 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
965 if (is_Method_type(get_entity_type(ent)))
966 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
968 /* always add an underscore in win32 */
969 id = mangle(id_underscore, get_entity_ident(ent));
972 decl_modifiers_t decl_modifiers = declaration->modifiers;
973 if (decl_modifiers & DM_DLLIMPORT) {
974 /* add prefix for imported symbols */
975 id = mangle(id_imp, id);
981 * Mangles an entity linker (ld) name for Linux ELF usage.
983 * @param ent the entity to be mangled
984 * @param declaration the declaration
986 static ident *create_ld_ident_linux_elf(ir_entity *entity,
987 declaration_t *declaration)
990 return get_entity_ident(entity);
994 * Mangles an entity linker (ld) name for Mach-O usage.
996 * @param ent the entity to be mangled
997 * @param declaration the declaration
999 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
1002 ident *id = mangle(id_underscore, get_entity_ident(ent));
1006 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
1007 declaration_t *declaration);
1008 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
1011 * Handle GNU attributes for entities
1013 * @param ent the entity
1014 * @param decl the routine declaration
1016 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
1018 if (decl->modifiers & DM_PURE) {
1019 /* TRUE if the declaration includes the GNU
1020 __attribute__((pure)) specifier. */
1021 set_entity_additional_property(ent, mtp_property_pure);
1023 if (decl->modifiers & DM_USED) {
1024 /* TRUE if the declaration includes the GNU
1025 __attribute__((used)) specifier. */
1026 set_entity_stickyness(ent, stickyness_sticky);
1031 * Creates an entity representing a function.
1033 * @param declaration the function declaration
1035 static ir_entity *get_function_entity(declaration_t *declaration)
1037 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
1038 return declaration->v.entity;
1039 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1041 symbol_t *symbol = declaration->symbol;
1042 ident *id = new_id_from_str(symbol->string);
1044 ir_type *global_type = get_glob_type();
1045 ir_type *ir_type_method = get_ir_type(declaration->type);
1046 assert(is_Method_type(ir_type_method));
1048 /* already an entity defined? */
1049 ir_entity *entity = entitymap_get(&entitymap, symbol);
1050 if (entity != NULL) {
1051 if (get_entity_visibility(entity) == visibility_external_allocated
1052 && declaration->init.statement != NULL) {
1053 set_entity_visibility(entity, visibility_external_visible);
1055 goto entity_created;
1058 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1059 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1060 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1062 handle_gnu_attributes_ent(entity, declaration);
1064 /* static inline => local
1065 * extern inline => local
1066 * inline without definition => local
1067 * inline with definition => external_visible */
1068 storage_class_tag_t const storage_class = (storage_class_tag_t)declaration->storage_class;
1069 bool const is_inline = declaration->is_inline;
1070 bool const has_body = declaration->init.statement != NULL;
1071 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1072 set_entity_visibility(entity, visibility_external_visible);
1073 } else if (storage_class == STORAGE_CLASS_STATIC ||
1074 (is_inline && has_body)) {
1076 /* this entity was declared, but is defined nowhere */
1077 set_entity_peculiarity(entity, peculiarity_description);
1079 set_entity_visibility(entity, visibility_local);
1080 } else if (has_body) {
1081 set_entity_visibility(entity, visibility_external_visible);
1083 set_entity_visibility(entity, visibility_external_allocated);
1085 set_entity_allocation(entity, allocation_static);
1087 /* We should check for file scope here, but as long as we compile C only
1088 this is not needed. */
1089 if (! firm_opt.freestanding) {
1090 /* check for a known runtime function */
1091 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1092 if (id != rts_idents[i])
1095 /* ignore those rts functions not necessary needed for current mode */
1096 if ((c_mode & rts_data[i].flags) == 0)
1098 assert(rts_entities[rts_data[i].id] == NULL);
1099 rts_entities[rts_data[i].id] = entity;
1103 entitymap_insert(&entitymap, symbol, entity);
1106 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1107 declaration->v.entity = entity;
1113 * Creates a Const node representing a constant.
1115 static ir_node *const_to_firm(const const_expression_t *cnst)
1117 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1118 ir_mode *mode = get_ir_mode(cnst->base.type);
1123 if (mode_is_float(mode)) {
1124 tv = new_tarval_from_double(cnst->v.float_value, mode);
1126 if (mode_is_signed(mode)) {
1127 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1129 len = snprintf(buf, sizeof(buf), "%llu",
1130 (unsigned long long) cnst->v.int_value);
1132 tv = new_tarval_from_str(buf, len, mode);
1135 return new_d_Const(dbgi, mode, tv);
1139 * Creates a Const node representing a character constant.
1141 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1143 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1144 ir_mode *mode = get_ir_mode(cnst->base.type);
1146 long long int v = 0;
1147 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1148 if (char_is_signed) {
1149 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1151 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1155 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1156 tarval *tv = new_tarval_from_str(buf, len, mode);
1158 return new_d_Const(dbgi, mode, tv);
1162 * Creates a Const node representing a wide character constant.
1164 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1166 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1167 ir_mode *mode = get_ir_mode(cnst->base.type);
1169 long long int v = cnst->v.wide_character.begin[0];
1172 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1173 tarval *tv = new_tarval_from_str(buf, len, mode);
1175 return new_d_Const(dbgi, mode, tv);
1179 * Creates a SymConst for a given entity.
1181 * @param dbgi debug info
1182 * @param mode the (reference) mode for the SymConst
1183 * @param entity the entity
1185 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1188 assert(entity != NULL);
1189 union symconst_symbol sym;
1190 sym.entity_p = entity;
1191 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1195 * Creates a SymConst node representing a string constant.
1197 * @param src_pos the source position of the string constant
1198 * @param id_prefix a prefix for the name of the generated string constant
1199 * @param value the value of the string constant
1201 static ir_node *string_to_firm(const source_position_t *const src_pos,
1202 const char *const id_prefix,
1203 const string_t *const value)
1205 ir_type *const global_type = get_glob_type();
1206 dbg_info *const dbgi = get_dbg_info(src_pos);
1207 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1208 ir_type_const_char, dbgi);
1210 ident *const id = id_unique(id_prefix);
1211 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1212 set_entity_ld_ident(entity, id);
1213 set_entity_variability(entity, variability_constant);
1214 set_entity_allocation(entity, allocation_static);
1216 ir_type *const elem_type = ir_type_const_char;
1217 ir_mode *const mode = get_type_mode(elem_type);
1219 const char* const string = value->begin;
1220 const size_t slen = value->size;
1222 set_array_lower_bound_int(type, 0, 0);
1223 set_array_upper_bound_int(type, 0, slen);
1224 set_type_size_bytes(type, slen);
1225 set_type_state(type, layout_fixed);
1227 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1228 for(size_t i = 0; i < slen; ++i) {
1229 tvs[i] = new_tarval_from_long(string[i], mode);
1232 set_array_entity_values(entity, tvs, slen);
1235 return create_symconst(dbgi, mode_P_data, entity);
1239 * Creates a SymConst node representing a string literal.
1241 * @param literal the string literal
1243 static ir_node *string_literal_to_firm(
1244 const string_literal_expression_t* literal)
1246 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1251 * Creates a SymConst node representing a wide string literal.
1253 * @param literal the wide string literal
1255 static ir_node *wide_string_literal_to_firm(
1256 const wide_string_literal_expression_t* const literal)
1258 ir_type *const global_type = get_glob_type();
1259 ir_type *const elem_type = ir_type_wchar_t;
1260 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1261 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1264 ident *const id = id_unique("Lstr.%u");
1265 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1266 set_entity_ld_ident(entity, id);
1267 set_entity_variability(entity, variability_constant);
1268 set_entity_allocation(entity, allocation_static);
1270 ir_mode *const mode = get_type_mode(elem_type);
1272 const wchar_rep_t *const string = literal->value.begin;
1273 const size_t slen = literal->value.size;
1275 set_array_lower_bound_int(type, 0, 0);
1276 set_array_upper_bound_int(type, 0, slen);
1277 set_type_size_bytes(type, slen);
1278 set_type_state(type, layout_fixed);
1280 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1281 for(size_t i = 0; i < slen; ++i) {
1282 tvs[i] = new_tarval_from_long(string[i], mode);
1285 set_array_entity_values(entity, tvs, slen);
1288 return create_symconst(dbgi, mode_P_data, entity);
1291 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1292 ir_node *const addr)
1294 ir_type *irtype = get_ir_type(type);
1295 if (is_compound_type(irtype)
1296 || is_Method_type(irtype)
1297 || is_Array_type(irtype)) {
1301 ir_mode *const mode = get_type_mode(irtype);
1302 ir_node *const memory = get_store();
1303 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1304 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1305 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1307 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1308 set_Load_volatility(load, volatility_is_volatile);
1311 set_store(load_mem);
1316 * Creates a strict Conv if neccessary.
1318 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1320 ir_mode *mode = get_irn_mode(node);
1322 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1324 if (!mode_is_float(mode))
1327 /* check if there is already a Conv */
1328 if (is_Conv(node)) {
1329 /* convert it into a strict Conv */
1330 set_Conv_strict(node, 1);
1334 /* otherwise create a new one */
1335 return new_d_strictConv(dbgi, node, mode);
1338 static ir_node *get_global_var_address(dbg_info *const dbgi,
1339 const declaration_t *const decl)
1341 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1343 ir_entity *const entity = decl->v.entity;
1344 switch ((storage_class_tag_t)decl->storage_class) {
1345 case STORAGE_CLASS_THREAD:
1346 case STORAGE_CLASS_THREAD_EXTERN:
1347 case STORAGE_CLASS_THREAD_STATIC: {
1348 ir_node *const no_mem = new_NoMem();
1349 ir_node *const tls = get_irg_tls(current_ir_graph);
1350 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1354 return create_symconst(dbgi, mode_P_data, entity);
1359 * Returns the correct base address depending on whether it is a parameter or a
1360 * normal local variable.
1362 static ir_node *get_local_frame(ir_entity *const ent)
1364 ir_graph *const irg = current_ir_graph;
1365 const ir_type *const owner = get_entity_owner(ent);
1366 if (owner == get_irg_frame_type(irg)) {
1367 return get_irg_frame(irg);
1369 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1370 return get_irg_value_param_base(irg);
1374 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1376 ir_mode *value_mode = get_irn_mode(value);
1378 if (value_mode == dest_mode || is_Bad(value))
1381 if (dest_mode == mode_b) {
1382 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1383 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1384 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1388 return new_d_Conv(dbgi, value, dest_mode);
1391 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1393 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1394 declaration_t *declaration = ref->declaration;
1395 type_t *type = skip_typeref(declaration->type);
1397 /* make sure the type is constructed */
1398 (void) get_ir_type(type);
1400 switch((declaration_kind_t) declaration->declaration_kind) {
1401 case DECLARATION_KIND_TYPE:
1402 case DECLARATION_KIND_UNKNOWN:
1405 case DECLARATION_KIND_ENUM_ENTRY: {
1406 ir_mode *const mode = get_ir_mode(type);
1407 return new_Const(mode, declaration->v.enum_val);
1410 case DECLARATION_KIND_LOCAL_VARIABLE: {
1411 ir_mode *const mode = get_ir_mode(type);
1412 return get_value(declaration->v.value_number, mode);
1414 case DECLARATION_KIND_FUNCTION: {
1415 ir_mode *const mode = get_ir_mode(type);
1416 return create_symconst(dbgi, mode, declaration->v.entity);
1418 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1419 ir_node *const addr = get_global_var_address(dbgi, declaration);
1420 return deref_address(dbgi, declaration->type, addr);
1423 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1424 ir_entity *entity = declaration->v.entity;
1425 ir_node *frame = get_local_frame(entity);
1426 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1427 return deref_address(dbgi, declaration->type, sel);
1430 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1431 return declaration->v.vla_base;
1433 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1434 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1435 case DECLARATION_KIND_COMPOUND_MEMBER:
1436 case DECLARATION_KIND_LABEL_BLOCK:
1437 panic("not implemented reference type");
1440 panic("reference to declaration with unknown type found");
1443 static ir_node *reference_addr(const reference_expression_t *ref)
1445 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1446 declaration_t *declaration = ref->declaration;
1448 switch((declaration_kind_t) declaration->declaration_kind) {
1449 case DECLARATION_KIND_TYPE:
1450 case DECLARATION_KIND_UNKNOWN:
1452 case DECLARATION_KIND_LOCAL_VARIABLE:
1453 /* you can store to a local variable (so we don't panic but return NULL
1454 * as an indicator for no real address) */
1456 case DECLARATION_KIND_FUNCTION: {
1457 type_t *const type = skip_typeref(ref->base.type);
1458 ir_mode *const mode = get_ir_mode(type);
1459 return create_symconst(dbgi, mode, declaration->v.entity);
1461 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1462 ir_node *const addr = get_global_var_address(dbgi, declaration);
1465 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1466 ir_entity *entity = declaration->v.entity;
1467 ir_node *frame = get_local_frame(entity);
1468 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1473 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1474 return declaration->v.vla_base;
1476 case DECLARATION_KIND_ENUM_ENTRY:
1477 panic("trying to reference enum entry");
1479 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1480 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1481 case DECLARATION_KIND_COMPOUND_MEMBER:
1482 case DECLARATION_KIND_LABEL_BLOCK:
1483 panic("not implemented reference type");
1486 panic("reference to declaration with unknown type found");
1490 * Transform calls to builtin functions.
1492 static ir_node *process_builtin_call(const call_expression_t *call)
1494 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1496 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1497 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1499 type_t *type = skip_typeref(builtin->base.type);
1500 assert(is_type_pointer(type));
1502 type_t *function_type = skip_typeref(type->pointer.points_to);
1503 symbol_t *symbol = builtin->symbol;
1505 switch(symbol->ID) {
1506 case T___builtin_alloca: {
1507 if (call->arguments == NULL || call->arguments->next != NULL) {
1508 panic("invalid number of parameters on __builtin_alloca");
1510 expression_t *argument = call->arguments->expression;
1511 ir_node *size = expression_to_firm(argument);
1513 ir_node *store = get_store();
1514 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1516 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1518 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1522 case T___builtin_huge_val: {
1523 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1524 tarval *tv = get_mode_infinite(mode);
1525 ir_node *res = new_d_Const(dbgi, mode, tv);
1528 case T___builtin_nan:
1529 case T___builtin_nanf:
1530 case T___builtin_nand: {
1531 /* Ignore string for now... */
1532 assert(is_type_function(function_type));
1533 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1534 tarval *tv = get_mode_NAN(mode);
1535 ir_node *res = new_d_Const(dbgi, mode, tv);
1538 case T___builtin_va_end:
1541 panic("Unsupported builtin found\n");
1546 * Transform a call expression.
1547 * Handles some special cases, like alloca() calls, which must be resolved
1548 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1549 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1552 static ir_node *call_expression_to_firm(const call_expression_t *call)
1554 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1555 assert(get_cur_block() != NULL);
1557 expression_t *function = call->function;
1558 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1559 return process_builtin_call(call);
1561 if (function->kind == EXPR_REFERENCE) {
1562 const reference_expression_t *ref = &function->reference;
1563 declaration_t *declaration = ref->declaration;
1565 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1566 if (declaration->v.entity == rts_entities[rts_alloca]) {
1567 /* handle alloca() call */
1568 expression_t *argument = call->arguments->expression;
1569 ir_node *size = expression_to_firm(argument);
1571 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1573 ir_node *store = get_store();
1574 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1575 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1577 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1579 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1585 ir_node *callee = expression_to_firm(function);
1587 type_t *type = skip_typeref(function->base.type);
1588 assert(is_type_pointer(type));
1589 pointer_type_t *pointer_type = &type->pointer;
1590 type_t *points_to = skip_typeref(pointer_type->points_to);
1591 assert(is_type_function(points_to));
1592 function_type_t *function_type = &points_to->function;
1594 int n_parameters = 0;
1595 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1596 ir_type *new_method_type = NULL;
1597 if (function_type->variadic || function_type->unspecified_parameters) {
1598 const call_argument_t *argument = call->arguments;
1599 for( ; argument != NULL; argument = argument->next) {
1603 /* we need to construct a new method type matching the call
1605 int n_res = get_method_n_ress(ir_method_type);
1606 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1607 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1608 n_parameters, n_res, dbgi);
1609 set_method_calling_convention(new_method_type,
1610 get_method_calling_convention(ir_method_type));
1611 set_method_additional_properties(new_method_type,
1612 get_method_additional_properties(ir_method_type));
1613 set_method_variadicity(new_method_type,
1614 get_method_variadicity(ir_method_type));
1616 for(int i = 0; i < n_res; ++i) {
1617 set_method_res_type(new_method_type, i,
1618 get_method_res_type(ir_method_type, i));
1620 argument = call->arguments;
1621 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1622 expression_t *expression = argument->expression;
1623 ir_type *irtype = get_ir_type(expression->base.type);
1624 set_method_param_type(new_method_type, i, irtype);
1626 ir_method_type = new_method_type;
1628 n_parameters = get_method_n_params(ir_method_type);
1631 ir_node *in[n_parameters];
1633 const call_argument_t *argument = call->arguments;
1634 for(int n = 0; n < n_parameters; ++n) {
1635 expression_t *expression = argument->expression;
1636 ir_node *arg_node = expression_to_firm(expression);
1638 arg_node = do_strict_conv(dbgi, arg_node);
1642 argument = argument->next;
1645 ir_node *store = get_store();
1646 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1648 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1651 type_t *return_type = skip_typeref(function_type->return_type);
1652 ir_node *result = NULL;
1654 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1656 if (is_type_scalar(return_type)) {
1657 mode = get_ir_mode(return_type);
1661 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1662 result = new_d_Proj(dbgi, resproj, mode, 0);
1665 if (function->kind == EXPR_REFERENCE &&
1666 function->reference.declaration->modifiers & DM_NORETURN) {
1667 /* A dead end: Keep the Call and the Block. Also place all further
1668 * nodes into a new and unreachable block. */
1670 keep_alive(get_cur_block());
1677 static void statement_to_firm(statement_t *statement);
1678 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1680 static ir_node *expression_to_addr(const expression_t *expression);
1681 static ir_node *create_condition_evaluation(const expression_t *expression,
1682 ir_node *true_block,
1683 ir_node *false_block);
1685 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1688 value = do_strict_conv(dbgi, value);
1690 ir_node *memory = get_store();
1692 if (is_type_scalar(type)) {
1693 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1694 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1695 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1696 set_Store_volatility(store, volatility_is_volatile);
1697 set_store(store_mem);
1699 ir_type *irtype = get_ir_type(type);
1700 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1701 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1702 set_store(copyb_mem);
1706 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1708 tarval *all_one = get_mode_all_one(mode);
1709 int mode_size = get_mode_size_bits(mode);
1711 assert(offset >= 0);
1713 assert(offset + size <= mode_size);
1714 if (size == mode_size) {
1718 long shiftr = get_mode_size_bits(mode) - size;
1719 long shiftl = offset;
1720 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1721 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1722 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1723 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1728 static void bitfield_store_to_firm(const select_expression_t *expression,
1729 ir_node *addr, ir_node *value)
1731 type_t *type = expression->base.type;
1732 ir_mode *mode = get_ir_mode(type);
1734 assert(get_irn_mode(value) == mode || is_Bad(value));
1736 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1738 /* kill upper bits of value and shift to right position */
1739 ir_entity *entity = expression->compound_entry->v.entity;
1740 int bitoffset = get_entity_offset_bits_remainder(entity);
1741 ir_type *entity_type = get_entity_type(entity);
1742 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1744 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1745 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1746 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1747 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1748 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1749 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1751 /* load current value */
1752 ir_node *mem = get_store();
1753 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1754 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1755 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1756 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1757 tarval *inv_mask = tarval_not(shift_mask);
1758 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1759 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1761 /* construct new value and store */
1762 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1763 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1764 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1765 set_store(store_mem);
1767 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1768 set_Load_volatility(load, volatility_is_volatile);
1769 set_Store_volatility(store, volatility_is_volatile);
1773 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1776 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1777 type_t *type = expression->base.type;
1778 ir_mode *mode = get_ir_mode(type);
1779 ir_node *mem = get_store();
1780 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1781 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1782 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1784 load_res = create_conv(dbgi, load_res, mode_int);
1786 set_store(load_mem);
1788 /* kill upper bits */
1789 ir_entity *entity = expression->compound_entry->v.entity;
1790 int bitoffset = get_entity_offset_bits_remainder(entity);
1791 ir_type *entity_type = get_entity_type(entity);
1792 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1793 long shift_bitsl = machine_size - bitoffset - bitsize;
1794 assert(shift_bitsl >= 0);
1795 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1796 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1797 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1799 long shift_bitsr = bitoffset + shift_bitsl;
1800 assert(shift_bitsr <= (long) machine_size);
1801 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1802 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1804 if (mode_is_signed(mode)) {
1805 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1807 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1810 return create_conv(dbgi, shiftr, mode);
1813 static void set_value_for_expression_addr(const expression_t *expression,
1814 ir_node *value, ir_node *addr)
1816 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1817 value = do_strict_conv(dbgi, value);
1819 if (expression->kind == EXPR_REFERENCE) {
1820 const reference_expression_t *ref = &expression->reference;
1822 declaration_t *declaration = ref->declaration;
1823 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1824 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1825 set_value(declaration->v.value_number, value);
1831 addr = expression_to_addr(expression);
1833 type_t *type = skip_typeref(expression->base.type);
1835 if (expression->kind == EXPR_SELECT) {
1836 const select_expression_t *select = &expression->select;
1838 declaration_t *declaration = select->compound_entry;
1839 if (declaration->type->kind == TYPE_BITFIELD) {
1840 bitfield_store_to_firm(select, addr, value);
1845 assign_value(dbgi, addr, type, value);
1848 static void set_value_for_expression(const expression_t *expression,
1851 set_value_for_expression_addr(expression, value, NULL);
1854 static ir_node *get_value_from_lvalue(const expression_t *expression,
1857 if (expression->kind == EXPR_REFERENCE) {
1858 const reference_expression_t *ref = &expression->reference;
1860 declaration_t *declaration = ref->declaration;
1861 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1862 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1863 assert(addr == NULL);
1864 ir_mode *mode = get_ir_mode(expression->base.type);
1865 return get_value(declaration->v.value_number, mode);
1869 assert(addr != NULL);
1870 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1873 if (expression->kind == EXPR_SELECT &&
1874 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1875 value = bitfield_extract_to_firm(&expression->select, addr);
1877 value = deref_address(dbgi, expression->base.type, addr);
1884 static ir_node *create_incdec(const unary_expression_t *expression)
1886 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1887 const expression_t *value_expr = expression->value;
1888 ir_node *addr = expression_to_addr(value_expr);
1889 ir_node *value = get_value_from_lvalue(value_expr, addr);
1891 type_t *type = skip_typeref(expression->base.type);
1892 ir_mode *mode = get_ir_mode(expression->base.type);
1895 if (is_type_pointer(type)) {
1896 pointer_type_t *pointer_type = &type->pointer;
1897 offset = get_type_size(pointer_type->points_to);
1899 assert(is_type_arithmetic(type));
1900 offset = new_Const(mode, get_mode_one(mode));
1904 ir_node *store_value;
1905 switch(expression->base.kind) {
1906 case EXPR_UNARY_POSTFIX_INCREMENT:
1908 store_value = new_d_Add(dbgi, value, offset, mode);
1910 case EXPR_UNARY_POSTFIX_DECREMENT:
1912 store_value = new_d_Sub(dbgi, value, offset, mode);
1914 case EXPR_UNARY_PREFIX_INCREMENT:
1915 result = new_d_Add(dbgi, value, offset, mode);
1916 store_value = result;
1918 case EXPR_UNARY_PREFIX_DECREMENT:
1919 result = new_d_Sub(dbgi, value, offset, mode);
1920 store_value = result;
1923 panic("no incdec expr in create_incdec");
1926 set_value_for_expression_addr(value_expr, store_value, addr);
1931 static bool is_local_variable(expression_t *expression)
1933 if (expression->kind != EXPR_REFERENCE)
1935 reference_expression_t *ref_expr = &expression->reference;
1936 declaration_t *declaration = ref_expr->declaration;
1937 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1940 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1943 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1944 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1945 case EXPR_BINARY_NOTEQUAL:
1946 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1947 case EXPR_BINARY_ISLESS:
1948 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1949 case EXPR_BINARY_ISLESSEQUAL:
1950 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1951 case EXPR_BINARY_ISGREATER:
1952 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1953 case EXPR_BINARY_ISGREATEREQUAL:
1954 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1955 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1960 panic("trying to get pn_Cmp from non-comparison binexpr type");
1964 * Handle the assume optimizer hint: check if a Confirm
1965 * node can be created.
1967 * @param dbi debug info
1968 * @param expr the IL assume expression
1970 * we support here only some simple cases:
1975 static ir_node *handle_assume_compare(dbg_info *dbi,
1976 const binary_expression_t *expression)
1978 expression_t *op1 = expression->left;
1979 expression_t *op2 = expression->right;
1980 declaration_t *var2, *var = NULL;
1981 ir_node *res = NULL;
1984 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1986 if (is_local_variable(op1) && is_local_variable(op2)) {
1987 var = op1->reference.declaration;
1988 var2 = op2->reference.declaration;
1990 type_t *const type = skip_typeref(var->type);
1991 ir_mode *const mode = get_ir_mode(type);
1993 ir_node *const irn1 = get_value(var->v.value_number, mode);
1994 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1996 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1997 set_value(var2->v.value_number, res);
1999 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2000 set_value(var->v.value_number, res);
2006 if (is_local_variable(op1) && is_constant_expression(op2)) {
2007 var = op1->reference.declaration;
2009 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2010 cmp_val = get_inversed_pnc(cmp_val);
2011 var = op2->reference.declaration;
2016 type_t *const type = skip_typeref(var->type);
2017 ir_mode *const mode = get_ir_mode(type);
2019 res = get_value(var->v.value_number, mode);
2020 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2021 set_value(var->v.value_number, res);
2027 * Handle the assume optimizer hint.
2029 * @param dbi debug info
2030 * @param expr the IL assume expression
2032 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2033 switch(expression->kind) {
2034 case EXPR_BINARY_EQUAL:
2035 case EXPR_BINARY_NOTEQUAL:
2036 case EXPR_BINARY_LESS:
2037 case EXPR_BINARY_LESSEQUAL:
2038 case EXPR_BINARY_GREATER:
2039 case EXPR_BINARY_GREATEREQUAL:
2040 return handle_assume_compare(dbi, &expression->binary);
2046 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2048 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2049 type_t *type = skip_typeref(expression->base.type);
2051 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2052 return expression_to_addr(expression->value);
2054 const expression_t *value = expression->value;
2056 switch(expression->base.kind) {
2057 case EXPR_UNARY_NEGATE: {
2058 ir_node *value_node = expression_to_firm(value);
2059 ir_mode *mode = get_ir_mode(type);
2060 return new_d_Minus(dbgi, value_node, mode);
2062 case EXPR_UNARY_PLUS:
2063 return expression_to_firm(value);
2064 case EXPR_UNARY_BITWISE_NEGATE: {
2065 ir_node *value_node = expression_to_firm(value);
2066 ir_mode *mode = get_ir_mode(type);
2067 return new_d_Not(dbgi, value_node, mode);
2069 case EXPR_UNARY_NOT: {
2070 ir_node *value_node = _expression_to_firm(value);
2071 value_node = create_conv(dbgi, value_node, mode_b);
2072 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2075 case EXPR_UNARY_DEREFERENCE: {
2076 ir_node *value_node = expression_to_firm(value);
2077 type_t *value_type = skip_typeref(value->base.type);
2078 assert(is_type_pointer(value_type));
2079 type_t *points_to = value_type->pointer.points_to;
2080 return deref_address(dbgi, points_to, value_node);
2082 case EXPR_UNARY_POSTFIX_INCREMENT:
2083 case EXPR_UNARY_POSTFIX_DECREMENT:
2084 case EXPR_UNARY_PREFIX_INCREMENT:
2085 case EXPR_UNARY_PREFIX_DECREMENT:
2086 return create_incdec(expression);
2087 case EXPR_UNARY_CAST: {
2088 ir_node *value_node = expression_to_firm(value);
2089 if (is_type_scalar(type)) {
2090 ir_mode *mode = get_ir_mode(type);
2091 ir_node *node = create_conv(dbgi, value_node, mode);
2092 node = do_strict_conv(dbgi, node);
2095 /* make sure firm type is constructed */
2096 (void) get_ir_type(type);
2100 case EXPR_UNARY_CAST_IMPLICIT: {
2101 ir_node *value_node = expression_to_firm(value);
2102 if (is_type_scalar(type)) {
2103 ir_mode *mode = get_ir_mode(type);
2104 return create_conv(dbgi, value_node, mode);
2109 case EXPR_UNARY_ASSUME:
2110 if (firm_opt.confirm)
2111 return handle_assume(dbgi, value);
2118 panic("invalid UNEXPR type found");
2122 * produces a 0/1 depending of the value of a mode_b node
2124 static ir_node *produce_condition_result(const expression_t *expression,
2127 ir_mode *mode = get_ir_mode(expression->base.type);
2128 ir_node *cur_block = get_cur_block();
2130 ir_node *one_block = new_immBlock();
2131 ir_node *one = new_Const(mode, get_mode_one(mode));
2132 ir_node *jmp_one = new_d_Jmp(dbgi);
2134 ir_node *zero_block = new_immBlock();
2135 ir_node *zero = new_Const(mode, get_mode_null(mode));
2136 ir_node *jmp_zero = new_d_Jmp(dbgi);
2138 set_cur_block(cur_block);
2139 create_condition_evaluation(expression, one_block, zero_block);
2140 mature_immBlock(one_block);
2141 mature_immBlock(zero_block);
2143 ir_node *common_block = new_immBlock();
2144 add_immBlock_pred(common_block, jmp_one);
2145 add_immBlock_pred(common_block, jmp_zero);
2146 mature_immBlock(common_block);
2148 ir_node *in[2] = { one, zero };
2149 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2154 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2155 ir_node *value, type_t *type)
2157 pointer_type_t *const pointer_type = &type->pointer;
2158 type_t *const points_to = skip_typeref(pointer_type->points_to);
2159 unsigned elem_size = get_type_size_const(points_to);
2161 /* gcc extension: allow arithmetic with void * and function * */
2162 if ((elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) ||
2163 is_type_function(points_to)) {
2167 assert(elem_size >= 1);
2171 value = create_conv(dbgi, value, mode_int);
2172 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2173 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2177 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2178 ir_node *left, ir_node *right)
2181 type_t *type_left = skip_typeref(expression->left->base.type);
2182 type_t *type_right = skip_typeref(expression->right->base.type);
2184 expression_kind_t kind = expression->base.kind;
2187 case EXPR_BINARY_SHIFTLEFT:
2188 case EXPR_BINARY_SHIFTRIGHT:
2189 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2190 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2191 mode = get_irn_mode(left);
2192 right = create_conv(dbgi, right, mode_uint);
2195 case EXPR_BINARY_SUB:
2196 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2197 const pointer_type_t *const ptr_type = &type_left->pointer;
2199 mode = get_ir_mode(expression->base.type);
2200 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2201 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2202 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2203 ir_node *const no_mem = new_NoMem();
2204 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2205 mode, op_pin_state_floats);
2206 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2209 case EXPR_BINARY_SUB_ASSIGN:
2210 if (is_type_pointer(type_left)) {
2211 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2212 mode = get_ir_mode(type_left);
2217 case EXPR_BINARY_ADD:
2218 case EXPR_BINARY_ADD_ASSIGN:
2219 if (is_type_pointer(type_left)) {
2220 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2221 mode = get_ir_mode(type_left);
2223 } else if (is_type_pointer(type_right)) {
2224 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2225 mode = get_ir_mode(type_right);
2232 mode = get_irn_mode(right);
2233 left = create_conv(dbgi, left, mode);
2238 case EXPR_BINARY_ADD_ASSIGN:
2239 case EXPR_BINARY_ADD:
2240 return new_d_Add(dbgi, left, right, mode);
2241 case EXPR_BINARY_SUB_ASSIGN:
2242 case EXPR_BINARY_SUB:
2243 return new_d_Sub(dbgi, left, right, mode);
2244 case EXPR_BINARY_MUL_ASSIGN:
2245 case EXPR_BINARY_MUL:
2246 return new_d_Mul(dbgi, left, right, mode);
2247 case EXPR_BINARY_BITWISE_AND:
2248 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2249 return new_d_And(dbgi, left, right, mode);
2250 case EXPR_BINARY_BITWISE_OR:
2251 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2252 return new_d_Or(dbgi, left, right, mode);
2253 case EXPR_BINARY_BITWISE_XOR:
2254 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2255 return new_d_Eor(dbgi, left, right, mode);
2256 case EXPR_BINARY_SHIFTLEFT:
2257 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2258 return new_d_Shl(dbgi, left, right, mode);
2259 case EXPR_BINARY_SHIFTRIGHT:
2260 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2261 if (mode_is_signed(mode)) {
2262 return new_d_Shrs(dbgi, left, right, mode);
2264 return new_d_Shr(dbgi, left, right, mode);
2266 case EXPR_BINARY_DIV:
2267 case EXPR_BINARY_DIV_ASSIGN: {
2268 ir_node *pin = new_Pin(new_NoMem());
2271 if (mode_is_float(mode)) {
2272 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2273 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2275 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2276 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2280 case EXPR_BINARY_MOD:
2281 case EXPR_BINARY_MOD_ASSIGN: {
2282 ir_node *pin = new_Pin(new_NoMem());
2283 assert(!mode_is_float(mode));
2284 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2285 op_pin_state_floats);
2286 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2290 panic("unexpected expression kind");
2294 static ir_node *create_lazy_op(const binary_expression_t *expression)
2296 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2297 type_t *type = expression->base.type;
2298 ir_mode *mode = get_ir_mode(type);
2300 if (is_constant_expression(expression->left)) {
2301 long val = fold_constant(expression->left);
2302 expression_kind_t ekind = expression->base.kind;
2303 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2304 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
2305 (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2306 return expression_to_firm(expression->right);
2308 return new_Const(mode, get_mode_one(mode));
2312 return produce_condition_result((const expression_t*) expression, dbgi);
2315 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2316 ir_node *right, ir_mode *mode);
2318 static ir_node *create_assign_binop(const binary_expression_t *expression)
2320 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2321 const expression_t *left_expr = expression->left;
2322 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2323 ir_node *right = expression_to_firm(expression->right);
2324 ir_node *left_addr = expression_to_addr(left_expr);
2325 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2326 ir_node *result = create_op(dbgi, expression, left, right);
2328 result = create_conv(dbgi, result, left_mode);
2329 result = do_strict_conv(dbgi, result);
2331 set_value_for_expression_addr(left_expr, result, left_addr);
2336 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2338 expression_kind_t kind = expression->base.kind;
2341 case EXPR_BINARY_EQUAL:
2342 case EXPR_BINARY_NOTEQUAL:
2343 case EXPR_BINARY_LESS:
2344 case EXPR_BINARY_LESSEQUAL:
2345 case EXPR_BINARY_GREATER:
2346 case EXPR_BINARY_GREATEREQUAL:
2347 case EXPR_BINARY_ISGREATER:
2348 case EXPR_BINARY_ISGREATEREQUAL:
2349 case EXPR_BINARY_ISLESS:
2350 case EXPR_BINARY_ISLESSEQUAL:
2351 case EXPR_BINARY_ISLESSGREATER:
2352 case EXPR_BINARY_ISUNORDERED: {
2353 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2354 ir_node *left = expression_to_firm(expression->left);
2355 ir_node *right = expression_to_firm(expression->right);
2356 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2357 long pnc = get_pnc(kind, expression->left->base.type);
2358 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2361 case EXPR_BINARY_ASSIGN: {
2362 ir_node *addr = expression_to_addr(expression->left);
2363 ir_node *right = expression_to_firm(expression->right);
2364 set_value_for_expression_addr(expression->left, right, addr);
2368 case EXPR_BINARY_ADD:
2369 case EXPR_BINARY_SUB:
2370 case EXPR_BINARY_MUL:
2371 case EXPR_BINARY_DIV:
2372 case EXPR_BINARY_MOD:
2373 case EXPR_BINARY_BITWISE_AND:
2374 case EXPR_BINARY_BITWISE_OR:
2375 case EXPR_BINARY_BITWISE_XOR:
2376 case EXPR_BINARY_SHIFTLEFT:
2377 case EXPR_BINARY_SHIFTRIGHT:
2379 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2380 ir_node *left = expression_to_firm(expression->left);
2381 ir_node *right = expression_to_firm(expression->right);
2382 return create_op(dbgi, expression, left, right);
2384 case EXPR_BINARY_LOGICAL_AND:
2385 case EXPR_BINARY_LOGICAL_OR:
2386 return create_lazy_op(expression);
2387 case EXPR_BINARY_COMMA:
2388 /* create side effects of left side */
2389 (void) expression_to_firm(expression->left);
2390 return _expression_to_firm(expression->right);
2392 case EXPR_BINARY_ADD_ASSIGN:
2393 case EXPR_BINARY_SUB_ASSIGN:
2394 case EXPR_BINARY_MUL_ASSIGN:
2395 case EXPR_BINARY_MOD_ASSIGN:
2396 case EXPR_BINARY_DIV_ASSIGN:
2397 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2398 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2399 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2400 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2401 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2402 return create_assign_binop(expression);
2403 case EXPR_BINARY_BUILTIN_EXPECT:
2404 return _expression_to_firm(expression->left);
2406 panic("TODO binexpr type");
2410 static ir_node *array_access_addr(const array_access_expression_t *expression)
2412 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2413 ir_node *base_addr = expression_to_firm(expression->array_ref);
2414 ir_node *offset = expression_to_firm(expression->index);
2416 type_t *offset_type = skip_typeref(expression->index->base.type);
2418 if (is_type_signed(offset_type)) {
2419 mode = get_ir_mode(type_ssize_t);
2421 mode = get_ir_mode(type_size_t);
2423 offset = create_conv(dbgi, offset, mode);
2425 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2426 assert(is_type_pointer(ref_type));
2427 pointer_type_t *pointer_type = &ref_type->pointer;
2429 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2430 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2431 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2433 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2438 static ir_node *array_access_to_firm(
2439 const array_access_expression_t *expression)
2441 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2442 ir_node *addr = array_access_addr(expression);
2443 type_t *type = revert_automatic_type_conversion(
2444 (const expression_t*) expression);
2445 type = skip_typeref(type);
2447 return deref_address(dbgi, type, addr);
2450 static long get_offsetof_offset(const offsetof_expression_t *expression)
2452 type_t *orig_type = expression->type;
2455 designator_t *designator = expression->designator;
2456 for( ; designator != NULL; designator = designator->next) {
2457 type_t *type = skip_typeref(orig_type);
2458 /* be sure the type is constructed */
2459 (void) get_ir_type(type);
2461 if (designator->symbol != NULL) {
2462 assert(is_type_compound(type));
2463 symbol_t *symbol = designator->symbol;
2465 declaration_t *declaration = type->compound.declaration;
2466 declaration_t *iter = declaration->scope.declarations;
2467 for( ; iter != NULL; iter = iter->next) {
2468 if (iter->symbol == symbol) {
2472 assert(iter != NULL);
2474 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2475 offset += get_entity_offset(iter->v.entity);
2477 orig_type = iter->type;
2479 expression_t *array_index = designator->array_index;
2480 assert(designator->array_index != NULL);
2481 assert(is_type_array(type));
2482 assert(is_type_valid(array_index->base.type));
2484 long index = fold_constant(array_index);
2485 ir_type *arr_type = get_ir_type(type);
2486 ir_type *elem_type = get_array_element_type(arr_type);
2487 long elem_size = get_type_size_bytes(elem_type);
2489 offset += index * elem_size;
2491 orig_type = type->array.element_type;
2498 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2500 ir_mode *mode = get_ir_mode(expression->base.type);
2501 long offset = get_offsetof_offset(expression);
2502 tarval *tv = new_tarval_from_long(offset, mode);
2503 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2505 return new_d_Const(dbgi, mode, tv);
2508 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2509 ir_entity *entity, type_t *type);
2511 static ir_node *compound_literal_to_firm(
2512 const compound_literal_expression_t *expression)
2514 type_t *type = expression->type;
2516 /* create an entity on the stack */
2517 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2519 ident *const id = id_unique("CompLit.%u");
2520 ir_type *const irtype = get_ir_type(type);
2521 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2522 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2523 set_entity_ld_ident(entity, id);
2525 set_entity_variability(entity, variability_uninitialized);
2527 /* create initialisation code */
2528 initializer_t *initializer = expression->initializer;
2529 create_local_initializer(initializer, dbgi, entity, type);
2531 /* create a sel for the compound literal address */
2532 ir_node *frame = get_local_frame(entity);
2533 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2538 * Transform a sizeof expression into Firm code.
2540 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2542 type_t *type = expression->type;
2544 type = expression->tp_expression->base.type;
2545 assert(type != NULL);
2548 type = skip_typeref(type);
2549 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2550 if (is_type_array(type) && type->array.is_vla
2551 && expression->tp_expression != NULL) {
2552 expression_to_firm(expression->tp_expression);
2555 return get_type_size(type);
2559 * Transform an alignof expression into Firm code.
2561 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2563 type_t *type = expression->type;
2565 /* beware: if expression is a variable reference, return the
2566 alignment of the variable. */
2567 const expression_t *tp_expression = expression->tp_expression;
2568 const declaration_t *declaration = expr_is_variable(tp_expression);
2569 if (declaration != NULL) {
2570 /* TODO: get the alignment of this variable. */
2572 type = tp_expression->base.type;
2573 assert(type != NULL);
2576 ir_mode *const mode = get_ir_mode(expression->base.type);
2577 symconst_symbol sym;
2578 sym.type_p = get_ir_type(type);
2579 return new_SymConst(mode, sym, symconst_type_align);
2582 static void init_ir_types(void);
2584 long fold_constant(const expression_t *expression)
2586 bool constant_folding_old = constant_folding;
2587 constant_folding = true;
2591 assert(is_constant_expression(expression));
2593 ir_graph *old_current_ir_graph = current_ir_graph;
2594 if (current_ir_graph == NULL) {
2595 current_ir_graph = get_const_code_irg();
2598 ir_node *cnst = expression_to_firm(expression);
2599 current_ir_graph = old_current_ir_graph;
2601 if (!is_Const(cnst)) {
2602 panic("couldn't fold constant\n");
2605 tarval *tv = get_Const_tarval(cnst);
2606 if (!tarval_is_long(tv)) {
2607 panic("result of constant folding is not integer\n");
2610 constant_folding = constant_folding_old;
2612 return get_tarval_long(tv);
2615 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2617 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2619 /* first try to fold a constant condition */
2620 if (is_constant_expression(expression->condition)) {
2621 long val = fold_constant(expression->condition);
2623 expression_t *true_expression = expression->true_expression;
2624 if (true_expression == NULL)
2625 true_expression = expression->condition;
2626 return expression_to_firm(true_expression);
2628 return expression_to_firm(expression->false_expression);
2632 ir_node *cur_block = get_cur_block();
2634 /* create the true block */
2635 ir_node *true_block = new_immBlock();
2637 ir_node *true_val = expression->true_expression != NULL ?
2638 expression_to_firm(expression->true_expression) : NULL;
2639 ir_node *true_jmp = new_Jmp();
2641 /* create the false block */
2642 ir_node *false_block = new_immBlock();
2644 ir_node *false_val = expression_to_firm(expression->false_expression);
2645 ir_node *false_jmp = new_Jmp();
2647 /* create the condition evaluation */
2648 set_cur_block(cur_block);
2649 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
2650 if (expression->true_expression == NULL) {
2651 if (cond_expr != NULL) {
2652 true_val = cond_expr;
2654 /* Condition ended with a short circuit (&&, ||, !) operation.
2655 * Generate a "1" as value for the true branch. */
2656 ir_mode *const mode = mode_Is;
2657 true_val = new_Const(mode, get_mode_one(mode));
2660 mature_immBlock(true_block);
2661 mature_immBlock(false_block);
2663 /* create the common block */
2664 ir_node *common_block = new_immBlock();
2665 add_immBlock_pred(common_block, true_jmp);
2666 add_immBlock_pred(common_block, false_jmp);
2667 mature_immBlock(common_block);
2669 /* TODO improve static semantics, so either both or no values are NULL */
2670 if (true_val == NULL || false_val == NULL)
2673 ir_node *in[2] = { true_val, false_val };
2674 ir_mode *mode = get_irn_mode(true_val);
2675 assert(get_irn_mode(false_val) == mode);
2676 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2682 * Returns an IR-node representing the address of a field.
2684 static ir_node *select_addr(const select_expression_t *expression)
2686 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2688 ir_node *compound_addr = expression_to_firm(expression->compound);
2690 /* make sure the type is constructed */
2691 type_t *type = skip_typeref(expression->compound->base.type);
2692 if (is_type_pointer(type)) {
2693 type = type->pointer.points_to;
2695 (void) get_ir_type(type);
2697 declaration_t *entry = expression->compound_entry;
2698 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2699 ir_entity *entity = entry->v.entity;
2701 assert(entity != NULL);
2703 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2708 static ir_node *select_to_firm(const select_expression_t *expression)
2710 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2711 ir_node *addr = select_addr(expression);
2712 type_t *type = revert_automatic_type_conversion(
2713 (const expression_t*) expression);
2714 type = skip_typeref(type);
2716 declaration_t *entry = expression->compound_entry;
2717 type_t *entry_type = skip_typeref(entry->type);
2719 if (entry_type->kind == TYPE_BITFIELD) {
2720 return bitfield_extract_to_firm(expression, addr);
2723 return deref_address(dbgi, type, addr);
2726 /* Values returned by __builtin_classify_type. */
2727 typedef enum gcc_type_class
2733 enumeral_type_class,
2736 reference_type_class,
2740 function_type_class,
2751 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2753 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2759 const atomic_type_t *const atomic_type = &type->atomic;
2760 switch (atomic_type->akind) {
2761 /* should not be reached */
2762 case ATOMIC_TYPE_INVALID:
2766 /* gcc cannot do that */
2767 case ATOMIC_TYPE_VOID:
2768 tc = void_type_class;
2771 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2772 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2773 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2774 case ATOMIC_TYPE_SHORT:
2775 case ATOMIC_TYPE_USHORT:
2776 case ATOMIC_TYPE_INT:
2777 case ATOMIC_TYPE_UINT:
2778 case ATOMIC_TYPE_LONG:
2779 case ATOMIC_TYPE_ULONG:
2780 case ATOMIC_TYPE_LONGLONG:
2781 case ATOMIC_TYPE_ULONGLONG:
2782 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2783 tc = integer_type_class;
2786 case ATOMIC_TYPE_FLOAT:
2787 case ATOMIC_TYPE_DOUBLE:
2788 case ATOMIC_TYPE_LONG_DOUBLE:
2789 tc = real_type_class;
2792 panic("Unexpected atomic type in classify_type_to_firm().");
2795 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2796 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2797 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2798 case TYPE_ARRAY: /* gcc handles this as pointer */
2799 case TYPE_FUNCTION: /* gcc handles this as pointer */
2800 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2801 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2802 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2804 /* gcc handles this as integer */
2805 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2808 /* typedef/typeof should be skipped already */
2815 panic("unexpected TYPE classify_type_to_firm().");
2818 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2819 ir_mode *const mode = mode_int;
2820 tarval *const tv = new_tarval_from_long(tc, mode);
2821 return new_d_Const(dbgi, mode, tv);
2824 static ir_node *function_name_to_firm(
2825 const funcname_expression_t *const expr)
2827 switch(expr->kind) {
2828 case FUNCNAME_FUNCTION:
2829 case FUNCNAME_PRETTY_FUNCTION:
2830 case FUNCNAME_FUNCDNAME:
2831 if (current_function_name == NULL) {
2832 const source_position_t *const src_pos = &expr->base.source_position;
2833 const char *const name = current_function_decl->symbol->string;
2834 const string_t string = { name, strlen(name) + 1 };
2835 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2837 return current_function_name;
2838 case FUNCNAME_FUNCSIG:
2839 if (current_funcsig == NULL) {
2840 const source_position_t *const src_pos = &expr->base.source_position;
2841 ir_entity *ent = get_irg_entity(current_ir_graph);
2842 const char *const name = get_entity_ld_name(ent);
2843 const string_t string = { name, strlen(name) + 1 };
2844 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2846 return current_funcsig;
2848 panic("Unsupported function name");
2851 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2853 statement_t *statement = expr->statement;
2855 assert(statement->kind == STATEMENT_COMPOUND);
2856 return compound_statement_to_firm(&statement->compound);
2859 static ir_node *va_start_expression_to_firm(
2860 const va_start_expression_t *const expr)
2862 ir_type *const method_type = get_ir_type(current_function_decl->type);
2863 int const n = get_method_n_params(method_type) - 1;
2864 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2865 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2866 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2867 ir_node *const no_mem = new_NoMem();
2868 ir_node *const arg_sel =
2869 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2871 ir_node *const cnst = get_type_size(expr->parameter->type);
2872 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2873 set_value_for_expression(expr->ap, add);
2878 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2880 type_t *const type = expr->base.type;
2881 expression_t *const ap_expr = expr->ap;
2882 ir_node *const ap_addr = expression_to_addr(ap_expr);
2883 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2884 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2885 ir_node *const res = deref_address(dbgi, type, ap);
2887 ir_node *const cnst = get_type_size(expr->base.type);
2888 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2890 set_value_for_expression_addr(ap_expr, add, ap_addr);
2895 static ir_node *dereference_addr(const unary_expression_t *const expression)
2897 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2898 return expression_to_firm(expression->value);
2902 * Returns a IR-node representing an lvalue of the given expression.
2904 static ir_node *expression_to_addr(const expression_t *expression)
2906 switch(expression->kind) {
2907 case EXPR_REFERENCE:
2908 return reference_addr(&expression->reference);
2909 case EXPR_ARRAY_ACCESS:
2910 return array_access_addr(&expression->array_access);
2912 return select_addr(&expression->select);
2914 return call_expression_to_firm(&expression->call);
2915 case EXPR_UNARY_DEREFERENCE: {
2916 return dereference_addr(&expression->unary);
2921 panic("trying to get address of non-lvalue");
2924 static ir_node *builtin_constant_to_firm(
2925 const builtin_constant_expression_t *expression)
2927 ir_mode *mode = get_ir_mode(expression->base.type);
2930 if (is_constant_expression(expression->value)) {
2935 return new_Const_long(mode, v);
2938 static ir_node *builtin_prefetch_to_firm(
2939 const builtin_prefetch_expression_t *expression)
2941 ir_node *adr = expression_to_firm(expression->adr);
2942 /* no Firm support for prefetch yet */
2948 * creates firm nodes for an expression. The difference between this function
2949 * and expression_to_firm is, that this version might produce mode_b nodes
2950 * instead of mode_Is.
2952 static ir_node *_expression_to_firm(const expression_t *expression)
2955 if (!constant_folding) {
2956 assert(!expression->base.transformed);
2957 ((expression_t*) expression)->base.transformed = true;
2961 switch(expression->kind) {
2962 case EXPR_CHARACTER_CONSTANT:
2963 return character_constant_to_firm(&expression->conste);
2964 case EXPR_WIDE_CHARACTER_CONSTANT:
2965 return wide_character_constant_to_firm(&expression->conste);
2967 return const_to_firm(&expression->conste);
2968 case EXPR_STRING_LITERAL:
2969 return string_literal_to_firm(&expression->string);
2970 case EXPR_WIDE_STRING_LITERAL:
2971 return wide_string_literal_to_firm(&expression->wide_string);
2972 case EXPR_REFERENCE:
2973 return reference_expression_to_firm(&expression->reference);
2975 return call_expression_to_firm(&expression->call);
2977 return unary_expression_to_firm(&expression->unary);
2979 return binary_expression_to_firm(&expression->binary);
2980 case EXPR_ARRAY_ACCESS:
2981 return array_access_to_firm(&expression->array_access);
2983 return sizeof_to_firm(&expression->typeprop);
2985 return alignof_to_firm(&expression->typeprop);
2986 case EXPR_CONDITIONAL:
2987 return conditional_to_firm(&expression->conditional);
2989 return select_to_firm(&expression->select);
2990 case EXPR_CLASSIFY_TYPE:
2991 return classify_type_to_firm(&expression->classify_type);
2993 return function_name_to_firm(&expression->funcname);
2994 case EXPR_STATEMENT:
2995 return statement_expression_to_firm(&expression->statement);
2997 return va_start_expression_to_firm(&expression->va_starte);
2999 return va_arg_expression_to_firm(&expression->va_arge);
3000 case EXPR_BUILTIN_SYMBOL:
3001 panic("unimplemented expression found");
3002 case EXPR_BUILTIN_CONSTANT_P:
3003 return builtin_constant_to_firm(&expression->builtin_constant);
3004 case EXPR_BUILTIN_PREFETCH:
3005 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
3007 return offsetof_to_firm(&expression->offsetofe);
3008 case EXPR_COMPOUND_LITERAL:
3009 return compound_literal_to_firm(&expression->compound_literal);
3015 panic("invalid expression found");
3018 static bool produces_mode_b(const expression_t *expression)
3020 switch (expression->kind) {
3021 case EXPR_BINARY_EQUAL:
3022 case EXPR_BINARY_NOTEQUAL:
3023 case EXPR_BINARY_LESS:
3024 case EXPR_BINARY_LESSEQUAL:
3025 case EXPR_BINARY_GREATER:
3026 case EXPR_BINARY_GREATEREQUAL:
3027 case EXPR_BINARY_ISGREATER:
3028 case EXPR_BINARY_ISGREATEREQUAL:
3029 case EXPR_BINARY_ISLESS:
3030 case EXPR_BINARY_ISLESSEQUAL:
3031 case EXPR_BINARY_ISLESSGREATER:
3032 case EXPR_BINARY_ISUNORDERED:
3033 case EXPR_UNARY_NOT:
3036 case EXPR_BINARY_BUILTIN_EXPECT:
3037 return produces_mode_b(expression->binary.left);
3038 case EXPR_BINARY_COMMA:
3039 return produces_mode_b(expression->binary.right);
3046 static ir_node *expression_to_firm(const expression_t *expression)
3048 if (!produces_mode_b(expression)) {
3049 ir_node *res = _expression_to_firm(expression);
3050 assert(res == NULL || get_irn_mode(res) != mode_b);
3054 if (is_constant_expression(expression)) {
3055 ir_node *res = _expression_to_firm(expression);
3056 ir_mode *mode = get_ir_mode(expression->base.type);
3057 assert(is_Const(res));
3058 if (is_Const_null(res)) {
3059 return new_Const_long(mode, 0);
3061 return new_Const_long(mode, 1);
3065 /* we have to produce a 0/1 from the mode_b expression */
3066 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3067 return produce_condition_result(expression, dbgi);
3071 * create a short-circuit expression evaluation that tries to construct
3072 * efficient control flow structures for &&, || and ! expressions
3074 static ir_node *create_condition_evaluation(const expression_t *expression,
3075 ir_node *true_block,
3076 ir_node *false_block)
3078 switch(expression->kind) {
3079 case EXPR_UNARY_NOT: {
3080 const unary_expression_t *unary_expression = &expression->unary;
3081 create_condition_evaluation(unary_expression->value, false_block,
3085 case EXPR_BINARY_LOGICAL_AND: {
3086 const binary_expression_t *binary_expression = &expression->binary;
3088 ir_node *cur_block = get_cur_block();
3089 ir_node *extra_block = new_immBlock();
3090 set_cur_block(cur_block);
3091 create_condition_evaluation(binary_expression->left, extra_block,
3093 mature_immBlock(extra_block);
3094 set_cur_block(extra_block);
3095 create_condition_evaluation(binary_expression->right, true_block,
3099 case EXPR_BINARY_LOGICAL_OR: {
3100 const binary_expression_t *binary_expression = &expression->binary;
3102 ir_node *cur_block = get_cur_block();
3103 ir_node *extra_block = new_immBlock();
3104 set_cur_block(cur_block);
3105 create_condition_evaluation(binary_expression->left, true_block,
3107 mature_immBlock(extra_block);
3108 set_cur_block(extra_block);
3109 create_condition_evaluation(binary_expression->right, true_block,
3117 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3118 ir_node *cond_expr = _expression_to_firm(expression);
3119 ir_node *condition = create_conv(NULL, cond_expr, mode_b);
3120 ir_node *cond = new_d_Cond(dbgi, condition);
3121 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3122 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3124 /* set branch prediction info based on __builtin_expect */
3125 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3126 long cnst = fold_constant(expression->binary.right);
3127 cond_jmp_predicate pred;
3130 pred = COND_JMP_PRED_FALSE;
3132 pred = COND_JMP_PRED_TRUE;
3134 set_Cond_jmp_pred(cond, pred);
3137 add_immBlock_pred(true_block, true_proj);
3138 if (false_block != NULL) {
3139 add_immBlock_pred(false_block, false_proj);
3142 set_cur_block(NULL);
3148 static void create_declaration_entity(declaration_t *declaration,
3149 declaration_kind_t declaration_kind,
3150 ir_type *parent_type)
3152 type_t *const type = skip_typeref(declaration->type);
3153 if (is_type_function(type)) {
3154 (void) get_function_entity(declaration);
3158 ident *const id = new_id_from_str(declaration->symbol->string);
3159 ir_type *const irtype = get_ir_type(type);
3160 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3161 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3163 declaration->declaration_kind = (unsigned char) declaration_kind;
3164 declaration->v.entity = entity;
3165 set_entity_variability(entity, variability_uninitialized);
3166 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3167 if (parent_type == get_tls_type())
3168 set_entity_allocation(entity, allocation_automatic);
3169 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3170 set_entity_allocation(entity, allocation_static);
3172 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3173 set_entity_volatility(entity, volatility_is_volatile);
3178 typedef struct type_path_entry_t type_path_entry_t;
3179 struct type_path_entry_t {
3181 ir_initializer_t *initializer;
3183 declaration_t *compound_entry;
3186 typedef struct type_path_t type_path_t;
3187 struct type_path_t {
3188 type_path_entry_t *path;
3193 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3195 size_t len = ARR_LEN(path->path);
3197 for(size_t i = 0; i < len; ++i) {
3198 const type_path_entry_t *entry = & path->path[i];
3200 type_t *type = skip_typeref(entry->type);
3201 if (is_type_compound(type)) {
3202 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3203 } else if (is_type_array(type)) {
3204 fprintf(stderr, "[%zd]", entry->index);
3206 fprintf(stderr, "-INVALID-");
3209 fprintf(stderr, " (");
3210 print_type(path->top_type);
3211 fprintf(stderr, ")");
3214 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3216 size_t len = ARR_LEN(path->path);
3218 return & path->path[len-1];
3221 static type_path_entry_t *append_to_type_path(type_path_t *path)
3223 size_t len = ARR_LEN(path->path);
3224 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3226 type_path_entry_t *result = & path->path[len];
3227 memset(result, 0, sizeof(result[0]));
3231 static size_t get_compound_size(const compound_type_t *type)
3233 declaration_t *declaration = type->declaration;
3234 declaration_t *member = declaration->scope.declarations;
3236 for( ; member != NULL; member = member->next) {
3239 /* TODO: cache results? */
3244 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3246 type_t *orig_top_type = path->top_type;
3247 type_t *top_type = skip_typeref(orig_top_type);
3249 assert(is_type_compound(top_type) || is_type_array(top_type));
3251 if (ARR_LEN(path->path) == 0) {
3254 type_path_entry_t *top = get_type_path_top(path);
3255 ir_initializer_t *initializer = top->initializer;
3256 return get_initializer_compound_value(initializer, top->index);
3260 static void descend_into_subtype(type_path_t *path)
3262 type_t *orig_top_type = path->top_type;
3263 type_t *top_type = skip_typeref(orig_top_type);
3265 assert(is_type_compound(top_type) || is_type_array(top_type));
3267 ir_initializer_t *initializer = get_initializer_entry(path);
3269 type_path_entry_t *top = append_to_type_path(path);
3270 top->type = top_type;
3274 if (is_type_compound(top_type)) {
3275 declaration_t *declaration = top_type->compound.declaration;
3276 declaration_t *entry = declaration->scope.declarations;
3278 top->compound_entry = entry;
3280 len = get_compound_size(&top_type->compound);
3282 path->top_type = entry->type;
3284 assert(is_type_array(top_type));
3285 assert(top_type->array.size > 0);
3288 path->top_type = top_type->array.element_type;
3289 len = top_type->array.size;
3291 if (initializer == NULL
3292 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3293 initializer = create_initializer_compound(len);
3294 /* we have to set the entry at the 2nd latest path entry... */
3295 size_t path_len = ARR_LEN(path->path);
3296 assert(path_len >= 1);
3298 type_path_entry_t *entry = & path->path[path_len-2];
3299 ir_initializer_t *tinitializer = entry->initializer;
3300 set_initializer_compound_value(tinitializer, entry->index,
3304 top->initializer = initializer;
3307 static void ascend_from_subtype(type_path_t *path)
3309 type_path_entry_t *top = get_type_path_top(path);
3311 path->top_type = top->type;
3313 size_t len = ARR_LEN(path->path);
3314 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3317 static void walk_designator(type_path_t *path, const designator_t *designator)
3319 /* designators start at current object type */
3320 ARR_RESIZE(type_path_entry_t, path->path, 1);
3322 for( ; designator != NULL; designator = designator->next) {
3323 type_path_entry_t *top = get_type_path_top(path);
3324 type_t *orig_type = top->type;
3325 type_t *type = skip_typeref(orig_type);
3327 if (designator->symbol != NULL) {
3328 assert(is_type_compound(type));
3330 symbol_t *symbol = designator->symbol;
3332 declaration_t *declaration = type->compound.declaration;
3333 declaration_t *iter = declaration->scope.declarations;
3334 for( ; iter != NULL; iter = iter->next, ++index) {
3335 if (iter->symbol == symbol) {
3339 assert(iter != NULL);
3341 top->type = orig_type;
3342 top->compound_entry = iter;
3344 orig_type = iter->type;
3346 expression_t *array_index = designator->array_index;
3347 assert(designator->array_index != NULL);
3348 assert(is_type_array(type));
3349 assert(is_type_valid(array_index->base.type));
3351 long index = fold_constant(array_index);
3354 if (type->array.size_constant == 1) {
3355 long array_size = type->array.size;
3356 assert(index < array_size);
3360 top->type = orig_type;
3361 top->index = (size_t) index;
3362 orig_type = type->array.element_type;
3364 path->top_type = orig_type;
3366 if (designator->next != NULL) {
3367 descend_into_subtype(path);
3371 path->invalid = false;
3374 static void advance_current_object(type_path_t *path)
3376 if (path->invalid) {
3377 /* TODO: handle this... */
3378 panic("invalid initializer in ast2firm (excessive elements)");
3382 type_path_entry_t *top = get_type_path_top(path);
3384 type_t *type = skip_typeref(top->type);
3385 if (is_type_union(type)) {
3386 top->compound_entry = NULL;
3387 } else if (is_type_struct(type)) {
3388 declaration_t *entry = top->compound_entry;
3391 entry = entry->next;
3392 top->compound_entry = entry;
3393 if (entry != NULL) {
3394 path->top_type = entry->type;
3398 assert(is_type_array(type));
3401 if (!type->array.size_constant || top->index < type->array.size) {
3406 /* we're past the last member of the current sub-aggregate, try if we
3407 * can ascend in the type hierarchy and continue with another subobject */
3408 size_t len = ARR_LEN(path->path);
3411 ascend_from_subtype(path);
3412 advance_current_object(path);
3414 path->invalid = true;
3419 static ir_initializer_t *create_ir_initializer(
3420 const initializer_t *initializer, type_t *type);
3422 static ir_initializer_t *create_ir_initializer_value(
3423 const initializer_value_t *initializer)
3425 if (is_type_compound(initializer->value->base.type)) {
3426 panic("initializer creation for compounds not implemented yet");
3428 ir_node *value = expression_to_firm(initializer->value);
3429 return create_initializer_const(value);
3432 static ir_initializer_t *create_ir_initializer_list(
3433 const initializer_list_t *initializer, type_t *type)
3436 memset(&path, 0, sizeof(path));
3437 path.top_type = type;
3438 path.path = NEW_ARR_F(type_path_entry_t, 0);
3440 descend_into_subtype(&path);
3442 for(size_t i = 0; i < initializer->len; ++i) {
3443 const initializer_t *sub_initializer = initializer->initializers[i];
3445 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3446 walk_designator(&path, sub_initializer->designator.designator);
3450 if (sub_initializer->kind == INITIALIZER_VALUE) {
3451 /* we might have to descend into types until we're at a scalar
3454 type_t *orig_top_type = path.top_type;
3455 type_t *top_type = skip_typeref(orig_top_type);
3457 if (is_type_scalar(top_type))
3459 descend_into_subtype(&path);
3463 ir_initializer_t *sub_irinitializer
3464 = create_ir_initializer(sub_initializer, path.top_type);
3466 size_t path_len = ARR_LEN(path.path);
3467 assert(path_len >= 1);
3468 type_path_entry_t *entry = & path.path[path_len-1];
3469 ir_initializer_t *tinitializer = entry->initializer;
3470 set_initializer_compound_value(tinitializer, entry->index,
3473 advance_current_object(&path);
3476 assert(ARR_LEN(path.path) >= 1);
3477 ir_initializer_t *result = path.path[0].initializer;
3478 DEL_ARR_F(path.path);
3483 static ir_initializer_t *create_ir_initializer_string(
3484 const initializer_string_t *initializer, type_t *type)
3486 type = skip_typeref(type);
3488 size_t string_len = initializer->string.size;
3489 assert(type->kind == TYPE_ARRAY);
3490 assert(type->array.size_constant);
3491 size_t len = type->array.size;
3492 ir_initializer_t *irinitializer = create_initializer_compound(len);
3494 const char *string = initializer->string.begin;
3495 ir_mode *mode = get_ir_mode(type->array.element_type);
3497 for(size_t i = 0; i < len; ++i) {
3502 tarval *tv = new_tarval_from_long(c, mode);
3503 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3505 set_initializer_compound_value(irinitializer, i, char_initializer);
3508 return irinitializer;
3511 static ir_initializer_t *create_ir_initializer_wide_string(
3512 const initializer_wide_string_t *initializer, type_t *type)
3514 size_t string_len = initializer->string.size;
3515 assert(type->kind == TYPE_ARRAY);
3516 assert(type->array.size_constant);
3517 size_t len = type->array.size;
3518 ir_initializer_t *irinitializer = create_initializer_compound(len);
3520 const wchar_rep_t *string = initializer->string.begin;
3521 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3523 for(size_t i = 0; i < len; ++i) {
3525 if (i < string_len) {
3528 tarval *tv = new_tarval_from_long(c, mode);
3529 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3531 set_initializer_compound_value(irinitializer, i, char_initializer);
3534 return irinitializer;
3537 static ir_initializer_t *create_ir_initializer(
3538 const initializer_t *initializer, type_t *type)
3540 switch(initializer->kind) {
3541 case INITIALIZER_STRING:
3542 return create_ir_initializer_string(&initializer->string, type);
3544 case INITIALIZER_WIDE_STRING:
3545 return create_ir_initializer_wide_string(&initializer->wide_string,
3548 case INITIALIZER_LIST:
3549 return create_ir_initializer_list(&initializer->list, type);
3551 case INITIALIZER_VALUE:
3552 return create_ir_initializer_value(&initializer->value);
3554 case INITIALIZER_DESIGNATOR:
3555 panic("unexpected designator initializer found");
3557 panic("unknown initializer");
3560 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3563 if (is_atomic_type(type)) {
3564 ir_mode *mode = get_type_mode(type);
3565 tarval *zero = get_mode_null(mode);
3566 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3568 /* TODO: bitfields */
3569 ir_node *mem = get_store();
3570 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3571 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3574 assert(is_compound_type(type));
3577 if (is_Array_type(type)) {
3578 assert(has_array_upper_bound(type, 0));
3579 n_members = get_array_upper_bound_int(type, 0);
3581 n_members = get_compound_n_members(type);
3584 for(int i = 0; i < n_members; ++i) {
3587 if (is_Array_type(type)) {
3588 ir_entity *entity = get_array_element_entity(type);
3589 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3590 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3591 ir_node *in[1] = { cnst };
3592 irtype = get_array_element_type(type);
3593 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3595 ir_entity *member = get_compound_member(type, i);
3597 irtype = get_entity_type(member);
3598 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3601 create_dynamic_null_initializer(irtype, dbgi, addr);
3606 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3607 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3609 switch(get_initializer_kind(initializer)) {
3610 case IR_INITIALIZER_NULL: {
3611 create_dynamic_null_initializer(type, dbgi, base_addr);
3614 case IR_INITIALIZER_CONST: {
3615 ir_node *node = get_initializer_const_value(initializer);
3616 ir_mode *mode = get_irn_mode(node);
3617 assert(get_type_mode(type) == mode);
3619 /* TODO: bitfields... */
3620 ir_node *mem = get_store();
3621 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3622 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3626 case IR_INITIALIZER_TARVAL: {
3627 tarval *tv = get_initializer_tarval_value(initializer);
3628 ir_mode *mode = get_tarval_mode(tv);
3629 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3630 assert(get_type_mode(type) == mode);
3632 /* TODO: bitfields... */
3633 ir_node *mem = get_store();
3634 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3635 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3639 case IR_INITIALIZER_COMPOUND: {
3640 assert(is_compound_type(type));
3642 if (is_Array_type(type)) {
3643 assert(has_array_upper_bound(type, 0));
3644 n_members = get_array_upper_bound_int(type, 0);
3646 n_members = get_compound_n_members(type);
3649 if (get_initializer_compound_n_entries(initializer)
3650 != (unsigned) n_members)
3651 panic("initializer doesn't match compound type");
3653 for(int i = 0; i < n_members; ++i) {
3656 if (is_Array_type(type)) {
3657 ir_entity *entity = get_array_element_entity(type);
3658 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3659 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3660 ir_node *in[1] = { cnst };
3661 irtype = get_array_element_type(type);
3662 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3664 ir_entity *member = get_compound_member(type, i);
3666 irtype = get_entity_type(member);
3667 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3670 ir_initializer_t *sub_init
3671 = get_initializer_compound_value(initializer, i);
3673 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3679 panic("invalid IR_INITIALIZER found");
3682 static void create_dynamic_initializer(ir_initializer_t *initializer,
3683 dbg_info *dbgi, ir_entity *entity)
3685 ir_node *frame = get_local_frame(entity);
3686 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3687 ir_type *type = get_entity_type(entity);
3689 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3692 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3693 ir_entity *entity, type_t *type)
3695 ir_node *memory = get_store();
3696 ir_node *nomem = new_NoMem();
3697 ir_node *frame = get_irg_frame(current_ir_graph);
3698 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3700 if (initializer->kind == INITIALIZER_VALUE) {
3701 initializer_value_t *initializer_value = &initializer->value;
3703 ir_node *value = expression_to_firm(initializer_value->value);
3704 type = skip_typeref(type);
3705 assign_value(dbgi, addr, type, value);
3709 if (!is_constant_initializer(initializer)) {
3710 ir_initializer_t *irinitializer
3711 = create_ir_initializer(initializer, type);
3713 create_dynamic_initializer(irinitializer, dbgi, entity);
3717 /* create the ir_initializer */
3718 ir_graph *const old_current_ir_graph = current_ir_graph;
3719 current_ir_graph = get_const_code_irg();
3721 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3723 assert(current_ir_graph == get_const_code_irg());
3724 current_ir_graph = old_current_ir_graph;
3726 /* create a "template" entity which is copied to the entity on the stack */
3727 ident *const id = id_unique("initializer.%u");
3728 ir_type *const irtype = get_ir_type(type);
3729 ir_type *const global_type = get_glob_type();
3730 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3731 set_entity_ld_ident(init_entity, id);
3733 set_entity_variability(init_entity, variability_initialized);
3734 set_entity_visibility(init_entity, visibility_local);
3735 set_entity_allocation(init_entity, allocation_static);
3737 set_entity_initializer(init_entity, irinitializer);
3739 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3740 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3742 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3743 set_store(copyb_mem);
3746 static void create_initializer_local_variable_entity(declaration_t *declaration)
3748 initializer_t *initializer = declaration->init.initializer;
3749 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3750 ir_entity *entity = declaration->v.entity;
3751 type_t *type = declaration->type;
3752 create_local_initializer(initializer, dbgi, entity, type);
3755 static void create_declaration_initializer(declaration_t *declaration)
3757 initializer_t *initializer = declaration->init.initializer;
3758 if (initializer == NULL)
3761 declaration_kind_t declaration_kind
3762 = (declaration_kind_t) declaration->declaration_kind;
3763 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3764 create_initializer_local_variable_entity(declaration);
3768 type_t *type = skip_typeref(declaration->type);
3770 if (initializer->kind == INITIALIZER_VALUE) {
3771 initializer_value_t *initializer_value = &initializer->value;
3773 = get_dbg_info(&declaration->source_position);
3775 ir_node *value = expression_to_firm(initializer_value->value);
3776 value = do_strict_conv(dbgi, value);
3778 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3779 set_value(declaration->v.value_number, value);
3781 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3783 ir_entity *entity = declaration->v.entity;
3785 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3786 set_entity_variability(entity, variability_constant);
3788 set_entity_variability(entity, variability_initialized);
3790 set_atomic_ent_value(entity, value);
3793 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3794 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3796 ir_entity *entity = declaration->v.entity;
3797 ir_initializer_t *irinitializer
3798 = create_ir_initializer(initializer, type);
3800 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3801 set_entity_variability(entity, variability_constant);
3803 set_entity_variability(entity, variability_initialized);
3805 set_entity_initializer(entity, irinitializer);
3809 static void create_variable_length_array(declaration_t *declaration)
3811 /* initializers are not allowed for VLAs */
3812 assert(declaration->init.initializer == NULL);
3814 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3815 declaration->v.vla_base = NULL;
3817 /* TODO: record VLA somewhere so we create the free node when we leave
3821 static void allocate_variable_length_array(declaration_t *declaration)
3823 /* initializers are not allowed for VLAs */
3824 assert(declaration->init.initializer == NULL);
3825 assert(get_cur_block() != NULL);
3827 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3828 type_t *type = declaration->type;
3829 ir_type *el_type = get_ir_type(type->array.element_type);
3831 /* make sure size_node is calculated */
3832 get_type_size(type);
3833 ir_node *elems = type->array.size_node;
3834 ir_node *mem = get_store();
3835 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3837 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3838 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3841 assert(declaration->declaration_kind
3842 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3843 declaration->v.vla_base = addr;
3847 * Creates a Firm local variable from a declaration.
3849 static void create_local_variable(declaration_t *declaration)
3851 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3853 bool needs_entity = declaration->address_taken;
3854 type_t *type = skip_typeref(declaration->type);
3856 /* is it a variable length array? */
3857 if (is_type_array(type) && !type->array.size_constant) {
3858 create_variable_length_array(declaration);
3860 } else if (is_type_array(type) || is_type_compound(type)) {
3861 needs_entity = true;
3862 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3863 needs_entity = true;
3867 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3868 create_declaration_entity(declaration,
3869 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3872 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3873 declaration->v.value_number = next_value_number_function;
3874 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3875 ++next_value_number_function;
3879 static void create_local_static_variable(declaration_t *declaration)
3881 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3883 type_t *const type = skip_typeref(declaration->type);
3884 ir_type *const global_type = get_glob_type();
3885 ir_type *const irtype = get_ir_type(type);
3886 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3888 size_t l = strlen(declaration->symbol->string);
3889 char buf[l + sizeof(".%u")];
3890 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3891 ident *const id = id_unique(buf);
3893 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3895 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3896 set_entity_volatility(entity, volatility_is_volatile);
3899 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3900 declaration->v.entity = entity;
3901 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3902 set_entity_variability(entity, variability_uninitialized);
3903 set_entity_visibility(entity, visibility_local);
3904 set_entity_allocation(entity, allocation_static);
3906 ir_graph *const old_current_ir_graph = current_ir_graph;
3907 current_ir_graph = get_const_code_irg();
3909 create_declaration_initializer(declaration);
3911 assert(current_ir_graph == get_const_code_irg());
3912 current_ir_graph = old_current_ir_graph;
3917 static void return_statement_to_firm(return_statement_t *statement)
3919 if (get_cur_block() == NULL)
3922 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3923 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3928 if (get_method_n_ress(func_irtype) > 0) {
3929 ir_type *res_type = get_method_res_type(func_irtype, 0);
3931 if (statement->value != NULL) {
3932 ir_node *node = expression_to_firm(statement->value);
3933 node = do_strict_conv(dbgi, node);
3937 if (is_compound_type(res_type)) {
3940 mode = get_type_mode(res_type);
3942 in[0] = new_Unknown(mode);
3946 /* build return_value for its side effects */
3947 if (statement->value != NULL) {
3948 expression_to_firm(statement->value);
3953 ir_node *store = get_store();
3954 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3956 ir_node *end_block = get_irg_end_block(current_ir_graph);
3957 add_immBlock_pred(end_block, ret);
3959 set_cur_block(NULL);
3962 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3964 if (get_cur_block() == NULL)
3967 return expression_to_firm(statement->expression);
3970 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3972 declaration_t *declaration = compound->scope.declarations;
3973 for( ; declaration != NULL; declaration = declaration->next) {
3974 create_local_declaration(declaration);
3977 ir_node *result = NULL;
3978 statement_t *statement = compound->statements;
3979 for( ; statement != NULL; statement = statement->base.next) {
3980 if (statement->base.next == NULL
3981 && statement->kind == STATEMENT_EXPRESSION) {
3982 result = expression_statement_to_firm(
3983 &statement->expression);
3986 statement_to_firm(statement);
3992 static void create_global_variable(declaration_t *declaration)
3996 switch ((storage_class_tag_t)declaration->storage_class) {
3997 case STORAGE_CLASS_STATIC:
3998 vis = visibility_local;
4001 case STORAGE_CLASS_EXTERN:
4002 vis = visibility_external_allocated;
4005 case STORAGE_CLASS_NONE:
4006 vis = visibility_external_visible;
4009 case STORAGE_CLASS_THREAD:
4010 vis = visibility_external_visible;
4013 case STORAGE_CLASS_THREAD_EXTERN:
4014 vis = visibility_external_allocated;
4017 case STORAGE_CLASS_THREAD_STATIC:
4018 vis = visibility_local;
4022 var_type = get_tls_type();
4026 var_type = get_glob_type();
4030 create_declaration_entity(declaration,
4031 DECLARATION_KIND_GLOBAL_VARIABLE,
4033 if (!is_type_function(skip_typeref(declaration->type))) {
4034 set_entity_visibility(declaration->v.entity, vis);
4039 case STORAGE_CLASS_TYPEDEF:
4040 case STORAGE_CLASS_AUTO:
4041 case STORAGE_CLASS_REGISTER:
4042 case STORAGE_CLASS_ENUM_ENTRY:
4045 panic("Invalid storage class for global variable");
4048 static void create_local_declaration(declaration_t *declaration)
4050 if (declaration->namespc != NAMESPACE_NORMAL)
4052 /* construct type */
4053 (void) get_ir_type(declaration->type);
4054 if (declaration->symbol == NULL) {
4058 type_t *type = skip_typeref(declaration->type);
4060 switch ((storage_class_tag_t) declaration->storage_class) {
4061 case STORAGE_CLASS_STATIC:
4062 create_local_static_variable(declaration);
4064 case STORAGE_CLASS_EXTERN:
4065 create_global_variable(declaration);
4066 create_declaration_initializer(declaration);
4068 case STORAGE_CLASS_NONE:
4069 case STORAGE_CLASS_AUTO:
4070 case STORAGE_CLASS_REGISTER:
4071 if (is_type_function(type)) {
4072 if (declaration->init.statement != NULL) {
4073 panic("nested functions not supported yet");
4075 get_function_entity(declaration);
4078 create_local_variable(declaration);
4081 case STORAGE_CLASS_ENUM_ENTRY:
4082 /* should already be handled */
4083 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4085 case STORAGE_CLASS_TYPEDEF:
4086 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4088 case STORAGE_CLASS_THREAD:
4089 case STORAGE_CLASS_THREAD_EXTERN:
4090 case STORAGE_CLASS_THREAD_STATIC:
4093 panic("invalid storage class found");
4096 static void initialize_local_declaration(declaration_t *declaration)
4098 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4101 switch ((declaration_kind_t) declaration->declaration_kind) {
4102 case DECLARATION_KIND_LOCAL_VARIABLE:
4103 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4104 create_declaration_initializer(declaration);
4107 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4108 allocate_variable_length_array(declaration);
4111 case DECLARATION_KIND_LABEL_BLOCK:
4112 case DECLARATION_KIND_COMPOUND_MEMBER:
4113 case DECLARATION_KIND_GLOBAL_VARIABLE:
4114 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4115 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4116 case DECLARATION_KIND_FUNCTION:
4117 case DECLARATION_KIND_TYPE:
4118 case DECLARATION_KIND_ENUM_ENTRY:
4121 case DECLARATION_KIND_UNKNOWN:
4122 panic("can't initialize unknwon declaration");
4124 panic("invalid declaration kind");
4127 static void declaration_statement_to_firm(declaration_statement_t *statement)
4129 declaration_t *declaration = statement->declarations_begin;
4130 declaration_t *end = statement->declarations_end->next;
4131 for( ; declaration != end; declaration = declaration->next) {
4132 if (declaration->namespc != NAMESPACE_NORMAL)
4134 initialize_local_declaration(declaration);
4138 static void if_statement_to_firm(if_statement_t *statement)
4140 ir_node *cur_block = get_cur_block();
4142 ir_node *fallthrough_block = NULL;
4144 /* the true (blocks) */
4145 ir_node *true_block = NULL;
4146 if (statement->true_statement != NULL) {
4147 true_block = new_immBlock();
4148 statement_to_firm(statement->true_statement);
4149 if (get_cur_block() != NULL) {
4150 ir_node *jmp = new_Jmp();
4151 if (fallthrough_block == NULL)
4152 fallthrough_block = new_immBlock();
4153 add_immBlock_pred(fallthrough_block, jmp);
4157 /* the false (blocks) */
4158 ir_node *false_block = NULL;
4159 if (statement->false_statement != NULL) {
4160 false_block = new_immBlock();
4162 statement_to_firm(statement->false_statement);
4163 if (get_cur_block() != NULL) {
4164 ir_node *jmp = new_Jmp();
4165 if (fallthrough_block == NULL)
4166 fallthrough_block = new_immBlock();
4167 add_immBlock_pred(fallthrough_block, jmp);
4171 /* create the condition */
4172 if (cur_block != NULL) {
4173 if (true_block == NULL || false_block == NULL) {
4174 if (fallthrough_block == NULL)
4175 fallthrough_block = new_immBlock();
4176 if (true_block == NULL)
4177 true_block = fallthrough_block;
4178 if (false_block == NULL)
4179 false_block = fallthrough_block;
4182 set_cur_block(cur_block);
4183 create_condition_evaluation(statement->condition, true_block,
4187 mature_immBlock(true_block);
4188 if (false_block != fallthrough_block && false_block != NULL) {
4189 mature_immBlock(false_block);
4191 if (fallthrough_block != NULL) {
4192 mature_immBlock(fallthrough_block);
4195 set_cur_block(fallthrough_block);
4198 static void while_statement_to_firm(while_statement_t *statement)
4200 ir_node *jmp = NULL;
4201 if (get_cur_block() != NULL) {
4205 /* create the header block */
4206 ir_node *header_block = new_immBlock();
4208 add_immBlock_pred(header_block, jmp);
4212 ir_node *old_continue_label = continue_label;
4213 ir_node *old_break_label = break_label;
4214 continue_label = header_block;
4217 ir_node *body_block = new_immBlock();
4218 statement_to_firm(statement->body);
4219 ir_node *false_block = break_label;
4221 assert(continue_label == header_block);
4222 continue_label = old_continue_label;
4223 break_label = old_break_label;
4225 if (get_cur_block() != NULL) {
4227 add_immBlock_pred(header_block, jmp);
4230 /* shortcut for while(true) */
4231 if (is_constant_expression(statement->condition)
4232 && fold_constant(statement->condition) != 0) {
4233 set_cur_block(header_block);
4234 ir_node *header_jmp = new_Jmp();
4235 add_immBlock_pred(body_block, header_jmp);
4237 keep_alive(body_block);
4238 set_cur_block(body_block);
4239 keep_alive(get_store());
4241 if (false_block == NULL) {
4242 false_block = new_immBlock();
4245 /* create the condition */
4246 set_cur_block(header_block);
4248 create_condition_evaluation(statement->condition, body_block,
4252 mature_immBlock(body_block);
4253 mature_immBlock(header_block);
4254 if (false_block != NULL) {
4255 mature_immBlock(false_block);
4258 set_cur_block(false_block);
4261 static void do_while_statement_to_firm(do_while_statement_t *statement)
4263 ir_node *jmp = NULL;
4264 if (get_cur_block() != NULL) {
4268 /* create the header block */
4269 ir_node *header_block = new_immBlock();
4272 ir_node *body_block = new_immBlock();
4274 add_immBlock_pred(body_block, jmp);
4277 ir_node *old_continue_label = continue_label;
4278 ir_node *old_break_label = break_label;
4279 continue_label = header_block;
4282 statement_to_firm(statement->body);
4283 ir_node *false_block = break_label;
4285 assert(continue_label == header_block);
4286 continue_label = old_continue_label;
4287 break_label = old_break_label;
4289 if (get_cur_block() != NULL) {
4290 ir_node *body_jmp = new_Jmp();
4291 add_immBlock_pred(header_block, body_jmp);
4292 mature_immBlock(header_block);
4295 if (false_block == NULL) {
4296 false_block = new_immBlock();
4299 /* create the condition */
4300 set_cur_block(header_block);
4302 create_condition_evaluation(statement->condition, body_block, false_block);
4303 mature_immBlock(body_block);
4304 mature_immBlock(header_block);
4305 if (false_block != NULL) {
4306 mature_immBlock(false_block);
4309 set_cur_block(false_block);
4312 static void for_statement_to_firm(for_statement_t *statement)
4314 ir_node *jmp = NULL;
4316 /* create declarations */
4317 declaration_t *declaration = statement->scope.declarations;
4318 for( ; declaration != NULL; declaration = declaration->next) {
4319 create_local_declaration(declaration);
4321 declaration = statement->scope.declarations;
4322 for( ; declaration != NULL; declaration = declaration->next) {
4323 initialize_local_declaration(declaration);
4326 if (get_cur_block() != NULL) {
4327 if (statement->initialisation != NULL) {
4328 expression_to_firm(statement->initialisation);
4335 /* create the step block */
4336 ir_node *const step_block = new_immBlock();
4337 if (statement->step != NULL) {
4338 expression_to_firm(statement->step);
4340 ir_node *const step_jmp = new_Jmp();
4342 /* create the header block */
4343 ir_node *const header_block = new_immBlock();
4345 add_immBlock_pred(header_block, jmp);
4347 add_immBlock_pred(header_block, step_jmp);
4349 /* the false block */
4350 ir_node *const false_block = new_immBlock();
4353 ir_node * body_block;
4354 if (statement->body != NULL) {
4355 ir_node *const old_continue_label = continue_label;
4356 ir_node *const old_break_label = break_label;
4357 continue_label = step_block;
4358 break_label = false_block;
4360 body_block = new_immBlock();
4361 statement_to_firm(statement->body);
4363 assert(continue_label == step_block);
4364 assert(break_label == false_block);
4365 continue_label = old_continue_label;
4366 break_label = old_break_label;
4368 if (get_cur_block() != NULL) {
4370 add_immBlock_pred(step_block, jmp);
4373 body_block = step_block;
4376 /* create the condition */
4377 set_cur_block(header_block);
4378 if (statement->condition != NULL) {
4379 create_condition_evaluation(statement->condition, body_block,
4382 keep_alive(header_block);
4383 keep_alive(get_store());
4385 add_immBlock_pred(body_block, jmp);
4388 mature_immBlock(body_block);
4389 mature_immBlock(false_block);
4390 mature_immBlock(step_block);
4391 mature_immBlock(header_block);
4392 mature_immBlock(false_block);
4394 set_cur_block(false_block);
4397 static void create_jump_statement(const statement_t *statement,
4398 ir_node *target_block)
4400 if (get_cur_block() == NULL)
4403 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4404 ir_node *jump = new_d_Jmp(dbgi);
4405 add_immBlock_pred(target_block, jump);
4407 set_cur_block(NULL);
4410 static ir_node *get_break_label(void)
4412 if (break_label == NULL) {
4413 ir_node *cur_block = get_cur_block();
4414 break_label = new_immBlock();
4415 set_cur_block(cur_block);
4420 static void switch_statement_to_firm(switch_statement_t *statement)
4422 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4424 ir_node *expression = expression_to_firm(statement->expression);
4425 ir_node *cond = new_d_Cond(dbgi, expression);
4427 set_cur_block(NULL);
4429 ir_node *const old_switch_cond = current_switch_cond;
4430 ir_node *const old_break_label = break_label;
4431 const bool old_saw_default_label = saw_default_label;
4432 saw_default_label = false;
4433 current_switch_cond = cond;
4435 switch_statement_t *const old_switch = current_switch;
4436 current_switch = statement;
4438 /* determine a free number for the default label */
4439 unsigned long num_cases = 0;
4441 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4442 if (l->expression == NULL) {
4446 if (l->last_case >= l->first_case)
4447 num_cases += l->last_case - l->first_case;
4448 if (l->last_case > def_nr)
4449 def_nr = l->last_case;
4452 if (def_nr + 1 < 0) {
4453 /* Bad: an overflow occurred, we cannot be sure that the
4454 * maximum + 1 is a free number. Scan the values a second
4455 * time to find a free number.
4457 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
4460 memset(bits, 0, (num_cases + 7) >> 3);
4461 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4462 if (l->expression == NULL) {
4466 for (long cns = l->first_case; cns <= l->last_case; ++cns) {
4467 if (cns >= 0 && (unsigned long)cns < num_cases)
4468 bits[cns >> 3] |= (1 << (cns & 7));
4471 /* We look at the first num_cases constants:
4472 * Either they are densed, so we took the last (num_cases)
4473 * one, or they are non densed, so we will find one free
4476 for (i = 0; i < num_cases; ++i)
4477 if ((bits[i >> 3] & (i & 7)) == 0)
4485 statement->default_proj_nr = def_nr;
4487 if (statement->body != NULL) {
4488 statement_to_firm(statement->body);
4491 if (get_cur_block() != NULL) {
4492 ir_node *jmp = new_Jmp();
4493 add_immBlock_pred(get_break_label(), jmp);
4496 if (!saw_default_label) {
4497 set_cur_block(get_nodes_block(cond));
4498 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4499 statement->default_proj_nr);
4500 add_immBlock_pred(get_break_label(), proj);
4503 if (break_label != NULL) {
4504 mature_immBlock(break_label);
4506 set_cur_block(break_label);
4508 assert(current_switch_cond == cond);
4509 current_switch = old_switch;
4510 current_switch_cond = old_switch_cond;
4511 break_label = old_break_label;
4512 saw_default_label = old_saw_default_label;
4515 static void case_label_to_firm(const case_label_statement_t *statement)
4517 if (statement->is_empty)
4520 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4522 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4525 ir_node *old_block = get_nodes_block(current_switch_cond);
4526 ir_node *block = new_immBlock();
4528 set_cur_block(old_block);
4529 if (statement->expression != NULL) {
4530 long pn = statement->first_case;
4531 long end_pn = statement->last_case;
4532 assert(pn <= end_pn);
4533 /* create jumps for all cases in the given range */
4535 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4536 add_immBlock_pred(block, proj);
4537 } while(pn++ < end_pn);
4539 saw_default_label = true;
4540 proj = new_d_defaultProj(dbgi, current_switch_cond,
4541 current_switch->default_proj_nr);
4543 add_immBlock_pred(block, proj);
4546 if (fallthrough != NULL) {
4547 add_immBlock_pred(block, fallthrough);
4549 mature_immBlock(block);
4550 set_cur_block(block);
4552 if (statement->statement != NULL) {
4553 statement_to_firm(statement->statement);
4557 static ir_node *get_label_block(declaration_t *label)
4559 assert(label->namespc == NAMESPACE_LABEL);
4561 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4562 return label->v.block;
4564 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4566 ir_node *old_cur_block = get_cur_block();
4567 ir_node *block = new_immBlock();
4568 set_cur_block(old_cur_block);
4570 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4571 label->v.block = block;
4573 ARR_APP1(ir_node *, imature_blocks, block);
4578 static void label_to_firm(const label_statement_t *statement)
4580 ir_node *block = get_label_block(statement->label);
4582 if (get_cur_block() != NULL) {
4583 ir_node *jmp = new_Jmp();
4584 add_immBlock_pred(block, jmp);
4587 set_cur_block(block);
4588 keep_alive(get_store());
4591 if (statement->statement != NULL) {
4592 statement_to_firm(statement->statement);
4596 static void goto_to_firm(const goto_statement_t *statement)
4598 if (get_cur_block() == NULL)
4601 ir_node *block = get_label_block(statement->label);
4602 ir_node *jmp = new_Jmp();
4603 add_immBlock_pred(block, jmp);
4605 set_cur_block(NULL);
4608 static void asm_statement_to_firm(const asm_statement_t *statement)
4610 bool needs_memory = false;
4612 if (statement->is_volatile) {
4613 needs_memory = true;
4616 size_t n_clobbers = 0;
4617 asm_clobber_t *clobber = statement->clobbers;
4618 for( ; clobber != NULL; clobber = clobber->next) {
4619 const char *clobber_str = clobber->clobber.begin;
4621 if (!be_is_valid_clobber(clobber_str)) {
4622 errorf(&statement->base.source_position,
4623 "invalid clobber '%s' specified", clobber->clobber);
4627 if (strcmp(clobber_str, "memory") == 0) {
4628 needs_memory = true;
4632 ident *id = new_id_from_str(clobber_str);
4633 obstack_ptr_grow(&asm_obst, id);
4636 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4637 ident **clobbers = NULL;
4638 if (n_clobbers > 0) {
4639 clobbers = obstack_finish(&asm_obst);
4642 size_t n_inputs = 0;
4643 asm_argument_t *argument = statement->inputs;
4644 for ( ; argument != NULL; argument = argument->next)
4646 size_t n_outputs = 0;
4647 argument = statement->outputs;
4648 for ( ; argument != NULL; argument = argument->next)
4651 unsigned next_pos = 0;
4653 ir_node *ins[n_inputs + n_outputs + 1];
4656 ir_asm_constraint tmp_in_constraints[n_outputs];
4658 const expression_t *out_exprs[n_outputs];
4659 ir_node *out_addrs[n_outputs];
4660 size_t out_size = 0;
4662 argument = statement->outputs;
4663 for ( ; argument != NULL; argument = argument->next) {
4664 const char *constraints = argument->constraints.begin;
4665 asm_constraint_flags_t asm_flags
4666 = be_parse_asm_constraints(constraints);
4668 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4669 errorf(&statement->base.source_position,
4670 "some constraints in '%s' are not supported", constraints);
4673 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4674 errorf(&statement->base.source_position,
4675 "some constraints in '%s' are invalid", constraints);
4678 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4679 errorf(&statement->base.source_position,
4680 "no write flag specified for output constraints '%s'",
4685 unsigned pos = next_pos++;
4686 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4687 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4688 expression_t *expr = argument->expression;
4689 ir_node *addr = expression_to_addr(expr);
4690 /* in+output, construct an artifical same_as constraint on the
4692 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4694 ir_node *value = get_value_from_lvalue(expr, addr);
4696 snprintf(buf, sizeof(buf), "%d", pos);
4698 ir_asm_constraint constraint;
4699 constraint.pos = pos;
4700 constraint.constraint = new_id_from_str(buf);
4701 constraint.mode = get_ir_mode(expr->base.type);
4702 tmp_in_constraints[in_size] = constraint;
4703 ins[in_size] = value;
4708 out_exprs[out_size] = expr;
4709 out_addrs[out_size] = addr;
4711 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4712 /* pure memory ops need no input (but we have to make sure we
4713 * attach to the memory) */
4714 assert(! (asm_flags &
4715 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4716 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4717 needs_memory = true;
4719 /* we need to attach the address to the inputs */
4720 expression_t *expr = argument->expression;
4722 ir_asm_constraint constraint;
4723 constraint.pos = pos;
4724 constraint.constraint = new_id_from_str(constraints);
4725 constraint.mode = NULL;
4726 tmp_in_constraints[in_size] = constraint;
4728 ins[in_size] = expression_to_addr(expr);
4732 errorf(&statement->base.source_position,
4733 "only modifiers but no place set in constraints '%s'",
4738 ir_asm_constraint constraint;
4739 constraint.pos = pos;
4740 constraint.constraint = new_id_from_str(constraints);
4741 constraint.mode = get_ir_mode(argument->expression->base.type);
4743 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4745 assert(obstack_object_size(&asm_obst)
4746 == out_size * sizeof(ir_asm_constraint));
4747 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4750 obstack_grow(&asm_obst, tmp_in_constraints,
4751 in_size * sizeof(tmp_in_constraints[0]));
4752 /* find and count input and output arguments */
4753 argument = statement->inputs;
4754 for( ; argument != NULL; argument = argument->next) {
4755 const char *constraints = argument->constraints.begin;
4756 asm_constraint_flags_t asm_flags
4757 = be_parse_asm_constraints(constraints);
4759 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4760 errorf(&statement->base.source_position,
4761 "some constraints in '%s' are not supported", constraints);
4764 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4765 errorf(&statement->base.source_position,
4766 "some constraints in '%s' are invalid", constraints);
4769 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4770 errorf(&statement->base.source_position,
4771 "write flag specified for input constraints '%s'",
4777 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4778 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4779 /* we can treat this as "normal" input */
4780 input = expression_to_firm(argument->expression);
4781 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4782 /* pure memory ops need no input (but we have to make sure we
4783 * attach to the memory) */
4784 assert(! (asm_flags &
4785 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4786 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4787 needs_memory = true;
4788 input = expression_to_addr(argument->expression);
4790 errorf(&statement->base.source_position,
4791 "only modifiers but no place set in constraints '%s'",
4796 ir_asm_constraint constraint;
4797 constraint.pos = next_pos++;
4798 constraint.constraint = new_id_from_str(constraints);
4799 constraint.mode = get_irn_mode(input);
4801 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4802 ins[in_size++] = input;
4806 ir_asm_constraint constraint;
4807 constraint.pos = next_pos++;
4808 constraint.constraint = new_id_from_str("");
4809 constraint.mode = mode_M;
4811 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4812 ins[in_size++] = get_store();
4815 assert(obstack_object_size(&asm_obst)
4816 == in_size * sizeof(ir_asm_constraint));
4817 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4819 /* create asm node */
4820 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4822 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4824 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4825 out_size, output_constraints,
4826 n_clobbers, clobbers, asm_text);
4828 if (statement->is_volatile) {
4829 set_irn_pinned(node, op_pin_state_pinned);
4831 set_irn_pinned(node, op_pin_state_floats);
4834 /* create output projs & connect them */
4836 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4841 for (i = 0; i < out_size; ++i) {
4842 const expression_t *out_expr = out_exprs[i];
4844 ir_mode *mode = get_ir_mode(out_expr->base.type);
4845 ir_node *proj = new_Proj(node, mode, pn);
4846 ir_node *addr = out_addrs[i];
4848 set_value_for_expression_addr(out_expr, proj, addr);
4852 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4853 statement_to_firm(statement->try_statement);
4854 warningf(&statement->base.source_position, "structured exception handling ignored");
4857 static void leave_statement_to_firm(leave_statement_t *statement) {
4858 errorf(&statement->base.source_position, "__leave not supported yet");
4862 * Transform a statement.
4864 static void statement_to_firm(statement_t *statement)
4867 assert(!statement->base.transformed);
4868 statement->base.transformed = true;
4871 switch(statement->kind) {
4872 case STATEMENT_INVALID:
4873 panic("invalid statement found");
4875 case STATEMENT_EMPTY:
4878 case STATEMENT_COMPOUND:
4879 compound_statement_to_firm(&statement->compound);
4881 case STATEMENT_RETURN:
4882 return_statement_to_firm(&statement->returns);
4884 case STATEMENT_EXPRESSION:
4885 expression_statement_to_firm(&statement->expression);
4888 if_statement_to_firm(&statement->ifs);
4890 case STATEMENT_WHILE:
4891 while_statement_to_firm(&statement->whiles);
4893 case STATEMENT_DO_WHILE:
4894 do_while_statement_to_firm(&statement->do_while);
4896 case STATEMENT_DECLARATION:
4897 declaration_statement_to_firm(&statement->declaration);
4899 case STATEMENT_BREAK:
4900 create_jump_statement(statement, get_break_label());
4902 case STATEMENT_CONTINUE:
4903 create_jump_statement(statement, continue_label);
4905 case STATEMENT_SWITCH:
4906 switch_statement_to_firm(&statement->switchs);
4908 case STATEMENT_CASE_LABEL:
4909 case_label_to_firm(&statement->case_label);
4912 for_statement_to_firm(&statement->fors);
4914 case STATEMENT_LABEL:
4915 label_to_firm(&statement->label);
4917 case STATEMENT_GOTO:
4918 goto_to_firm(&statement->gotos);
4921 asm_statement_to_firm(&statement->asms);
4923 case STATEMENT_MS_TRY:
4924 ms_try_statement_to_firm(&statement->ms_try);
4926 case STATEMENT_LEAVE:
4927 leave_statement_to_firm(&statement->leave);
4930 panic("Statement not implemented\n");
4933 static int count_decls_in_expression(const expression_t *expression);
4935 static int count_local_declarations(const declaration_t * decl,
4936 const declaration_t *const end)
4939 for (; decl != end; decl = decl->next) {
4940 if (decl->namespc != NAMESPACE_NORMAL)
4942 type_t *type = skip_typeref(decl->type);
4944 if (!decl->address_taken && is_type_scalar(type))
4946 const initializer_t *initializer = decl->init.initializer;
4947 /* FIXME: should walk initializer hierarchies... */
4948 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4949 count += count_decls_in_expression(initializer->value.value);
4955 static int count_decls_in_expression(const expression_t *expression) {
4958 if (expression == NULL)
4961 switch((expression_kind_t) expression->base.kind) {
4962 case EXPR_STATEMENT:
4963 return count_decls_in_stmts(expression->statement.statement);
4965 int count_left = count_decls_in_expression(expression->binary.left);
4966 int count_right = count_decls_in_expression(expression->binary.right);
4967 return count_left + count_right;
4970 return count_decls_in_expression(expression->unary.value);
4972 call_argument_t *argument = expression->call.arguments;
4973 for( ; argument != NULL; argument = argument->next) {
4974 count += count_decls_in_expression(argument->expression);
4981 panic("unexpected expression kind");
4983 case EXPR_COMPOUND_LITERAL:
4987 case EXPR_CONDITIONAL:
4988 count += count_decls_in_expression(expression->conditional.condition);
4989 count += count_decls_in_expression(expression->conditional.true_expression);
4990 count += count_decls_in_expression(expression->conditional.false_expression);
4993 case EXPR_BUILTIN_PREFETCH:
4994 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4995 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4996 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4999 case EXPR_BUILTIN_CONSTANT_P:
5000 count += count_decls_in_expression(expression->builtin_constant.value);
5004 count += count_decls_in_expression(expression->select.compound);
5007 case EXPR_ARRAY_ACCESS:
5008 count += count_decls_in_expression(expression->array_access.array_ref);
5009 count += count_decls_in_expression(expression->array_access.index);
5012 case EXPR_CLASSIFY_TYPE:
5013 count += count_decls_in_expression(expression->classify_type.type_expression);
5017 case EXPR_ALIGNOF: {
5018 expression_t *tp_expression = expression->typeprop.tp_expression;
5019 if (tp_expression != NULL) {
5020 count += count_decls_in_expression(tp_expression);
5026 case EXPR_REFERENCE:
5028 case EXPR_CHARACTER_CONSTANT:
5029 case EXPR_WIDE_CHARACTER_CONSTANT:
5030 case EXPR_STRING_LITERAL:
5031 case EXPR_WIDE_STRING_LITERAL:
5033 case EXPR_BUILTIN_SYMBOL:
5039 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
5040 * (or implement all the missing expressions here/implement a walker)
5046 static int count_decls_in_stmts(const statement_t *stmt)
5049 for (; stmt != NULL; stmt = stmt->base.next) {
5050 switch (stmt->kind) {
5051 case STATEMENT_EMPTY:
5054 case STATEMENT_DECLARATION: {
5055 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5056 count += count_local_declarations(decl_stmt->declarations_begin,
5057 decl_stmt->declarations_end->next);
5061 case STATEMENT_COMPOUND: {
5062 const compound_statement_t *const comp =
5064 count += count_decls_in_stmts(comp->statements);
5068 case STATEMENT_IF: {
5069 const if_statement_t *const if_stmt = &stmt->ifs;
5070 count += count_decls_in_expression(if_stmt->condition);
5071 count += count_decls_in_stmts(if_stmt->true_statement);
5072 count += count_decls_in_stmts(if_stmt->false_statement);
5076 case STATEMENT_SWITCH: {
5077 const switch_statement_t *const switch_stmt = &stmt->switchs;
5078 count += count_decls_in_expression(switch_stmt->expression);
5079 count += count_decls_in_stmts(switch_stmt->body);
5083 case STATEMENT_LABEL: {
5084 const label_statement_t *const label_stmt = &stmt->label;
5085 if (label_stmt->statement != NULL) {
5086 count += count_decls_in_stmts(label_stmt->statement);
5091 case STATEMENT_WHILE: {
5092 const while_statement_t *const while_stmt = &stmt->whiles;
5093 count += count_decls_in_expression(while_stmt->condition);
5094 count += count_decls_in_stmts(while_stmt->body);
5098 case STATEMENT_DO_WHILE: {
5099 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5100 count += count_decls_in_expression(do_while_stmt->condition);
5101 count += count_decls_in_stmts(do_while_stmt->body);
5105 case STATEMENT_FOR: {
5106 const for_statement_t *const for_stmt = &stmt->fors;
5107 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5108 count += count_decls_in_expression(for_stmt->initialisation);
5109 count += count_decls_in_expression(for_stmt->condition);
5110 count += count_decls_in_expression(for_stmt->step);
5111 count += count_decls_in_stmts(for_stmt->body);
5115 case STATEMENT_CASE_LABEL: {
5116 const case_label_statement_t *label = &stmt->case_label;
5117 count += count_decls_in_expression(label->expression);
5118 if (label->statement != NULL) {
5119 count += count_decls_in_stmts(label->statement);
5125 case STATEMENT_BREAK:
5126 case STATEMENT_CONTINUE:
5129 case STATEMENT_EXPRESSION: {
5130 const expression_statement_t *expr_stmt = &stmt->expression;
5131 count += count_decls_in_expression(expr_stmt->expression);
5135 case STATEMENT_GOTO:
5136 case STATEMENT_LEAVE:
5137 case STATEMENT_INVALID:
5140 case STATEMENT_RETURN: {
5141 const return_statement_t *ret_stmt = &stmt->returns;
5142 count += count_decls_in_expression(ret_stmt->value);
5146 case STATEMENT_MS_TRY: {
5147 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5148 count += count_decls_in_stmts(try_stmt->try_statement);
5149 if (try_stmt->except_expression != NULL)
5150 count += count_decls_in_expression(try_stmt->except_expression);
5151 count += count_decls_in_stmts(try_stmt->final_statement);
5159 static int get_function_n_local_vars(declaration_t *declaration)
5163 /* count parameters */
5164 count += count_local_declarations(declaration->scope.declarations, NULL);
5166 /* count local variables declared in body */
5167 count += count_decls_in_stmts(declaration->init.statement);
5172 static void initialize_function_parameters(declaration_t *declaration)
5174 ir_graph *irg = current_ir_graph;
5175 ir_node *args = get_irg_args(irg);
5176 ir_node *start_block = get_irg_start_block(irg);
5177 ir_type *function_irtype = get_ir_type(declaration->type);
5180 declaration_t *parameter = declaration->scope.declarations;
5181 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5182 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5183 type_t *type = skip_typeref(parameter->type);
5185 bool needs_entity = parameter->address_taken;
5186 assert(!is_type_array(type));
5187 if (is_type_compound(type)) {
5188 needs_entity = true;
5192 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5193 ident *id = new_id_from_str(parameter->symbol->string);
5194 set_entity_ident(entity, id);
5196 parameter->declaration_kind
5197 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5198 parameter->v.entity = entity;
5202 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5203 ir_mode *param_mode = get_type_mode(param_irtype);
5206 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5208 ir_mode *mode = get_ir_mode(type);
5209 value = create_conv(NULL, value, mode);
5210 value = do_strict_conv(NULL, value);
5212 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5213 parameter->v.value_number = next_value_number_function;
5214 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5215 ++next_value_number_function;
5217 set_value(parameter->v.value_number, value);
5222 * Handle additional decl modifiers for IR-graphs
5224 * @param irg the IR-graph
5225 * @param dec_modifiers additional modifiers
5227 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5229 if (decl_modifiers & DM_NORETURN) {
5230 /* TRUE if the declaration includes the Microsoft
5231 __declspec(noreturn) specifier. */
5232 set_irg_additional_property(irg, mtp_property_noreturn);
5234 if (decl_modifiers & DM_NOTHROW) {
5235 /* TRUE if the declaration includes the Microsoft
5236 __declspec(nothrow) specifier. */
5237 set_irg_additional_property(irg, mtp_property_nothrow);
5239 if (decl_modifiers & DM_NAKED) {
5240 /* TRUE if the declaration includes the Microsoft
5241 __declspec(naked) specifier. */
5242 set_irg_additional_property(irg, mtp_property_naked);
5244 if (decl_modifiers & DM_FORCEINLINE) {
5245 /* TRUE if the declaration includes the
5246 Microsoft __forceinline specifier. */
5247 set_irg_inline_property(irg, irg_inline_forced);
5249 if (decl_modifiers & DM_NOINLINE) {
5250 /* TRUE if the declaration includes the Microsoft
5251 __declspec(noinline) specifier. */
5252 set_irg_inline_property(irg, irg_inline_forbidden);
5256 static void add_function_pointer(ir_type *segment, ir_entity *method,
5257 const char *unique_template)
5259 ir_type *method_type = get_entity_type(method);
5260 ident *id = id_unique(unique_template);
5261 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5263 ident *ide = id_unique(unique_template);
5264 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5265 ir_graph *irg = get_const_code_irg();
5266 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5269 set_entity_compiler_generated(ptr, 1);
5270 set_entity_variability(ptr, variability_constant);
5271 set_atomic_ent_value(ptr, val);
5275 * Create code for a function.
5277 static void create_function(declaration_t *declaration)
5279 ir_entity *function_entity = get_function_entity(declaration);
5281 if (declaration->init.statement == NULL)
5284 if (declaration->modifiers & DM_CONSTRUCTOR) {
5285 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5286 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5288 if (declaration->modifiers & DM_DESTRUCTOR) {
5289 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5290 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5293 current_function_decl = declaration;
5294 current_function_name = NULL;
5295 current_funcsig = NULL;
5297 assert(imature_blocks == NULL);
5298 imature_blocks = NEW_ARR_F(ir_node*, 0);
5300 int n_local_vars = get_function_n_local_vars(declaration);
5301 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5303 set_irg_fp_model(irg, firm_opt.fp_model);
5304 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5305 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5307 ir_node *first_block = get_cur_block();
5309 /* set inline flags */
5310 if (declaration->is_inline)
5311 set_irg_inline_property(irg, irg_inline_recomended);
5312 handle_decl_modifier_irg(irg, declaration->modifiers);
5314 next_value_number_function = 0;
5315 initialize_function_parameters(declaration);
5317 statement_to_firm(declaration->init.statement);
5319 ir_node *end_block = get_irg_end_block(irg);
5321 /* do we have a return statement yet? */
5322 if (get_cur_block() != NULL) {
5323 type_t *type = skip_typeref(declaration->type);
5324 assert(is_type_function(type));
5325 const function_type_t *func_type = &type->function;
5326 const type_t *return_type
5327 = skip_typeref(func_type->return_type);
5330 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5331 ret = new_Return(get_store(), 0, NULL);
5334 if (is_type_scalar(return_type)) {
5335 mode = get_ir_mode(func_type->return_type);
5341 /* ยง5.1.2.2.3 main implicitly returns 0 */
5342 if (strcmp(declaration->symbol->string, "main") == 0) {
5343 in[0] = new_Const(mode, get_mode_null(mode));
5345 in[0] = new_Unknown(mode);
5347 ret = new_Return(get_store(), 1, in);
5349 add_immBlock_pred(end_block, ret);
5352 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
5353 mature_immBlock(imature_blocks[i]);
5355 DEL_ARR_F(imature_blocks);
5356 imature_blocks = NULL;
5358 mature_immBlock(first_block);
5359 mature_immBlock(end_block);
5361 irg_finalize_cons(irg);
5363 /* finalize the frame type */
5364 ir_type *frame_type = get_irg_frame_type(irg);
5365 int n = get_compound_n_members(frame_type);
5368 for(int i = 0; i < n; ++i) {
5369 ir_entity *entity = get_compound_member(frame_type, i);
5370 ir_type *entity_type = get_entity_type(entity);
5372 int align = get_type_alignment_bytes(entity_type);
5373 if (align > align_all)
5377 misalign = offset % align;
5379 offset += align - misalign;
5383 set_entity_offset(entity, offset);
5384 offset += get_type_size_bytes(entity_type);
5386 set_type_size_bytes(frame_type, offset);
5387 set_type_alignment_bytes(frame_type, align_all);
5392 static void scope_to_firm(scope_t *scope)
5394 /* first pass: create declarations */
5395 declaration_t *declaration = scope->declarations;
5396 for( ; declaration != NULL; declaration = declaration->next) {
5397 if (declaration->namespc != NAMESPACE_NORMAL)
5399 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5400 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5402 if (declaration->symbol == NULL)
5405 type_t *type = skip_typeref(declaration->type);
5406 if (is_type_function(type)) {
5407 get_function_entity(declaration);
5409 create_global_variable(declaration);
5413 /* second pass: create code/initializers */
5414 declaration = scope->declarations;
5415 for( ; declaration != NULL; declaration = declaration->next) {
5416 if (declaration->namespc != NAMESPACE_NORMAL)
5418 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5419 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5421 if (declaration->symbol == NULL)
5424 type_t *type = declaration->type;
5425 if (type->kind == TYPE_FUNCTION) {
5426 create_function(declaration);
5428 assert(declaration->declaration_kind
5429 == DECLARATION_KIND_GLOBAL_VARIABLE);
5430 current_ir_graph = get_const_code_irg();
5431 create_declaration_initializer(declaration);
5436 static void set_be_option(const char *option)
5438 int res = firm_be_option(option);
5442 void init_ast2firm(void)
5444 obstack_init(&asm_obst);
5445 init_atomic_modes();
5447 id_underscore = new_id_from_chars("_", 1);
5448 id_imp = new_id_from_chars("__imp_", 6);
5450 /* OS option must be set to the backend */
5451 switch (firm_opt.os_support) {
5452 case OS_SUPPORT_MINGW:
5453 create_ld_ident = create_ld_ident_win32;
5454 set_be_option("ia32-gasmode=mingw");
5456 case OS_SUPPORT_LINUX:
5457 create_ld_ident = create_ld_ident_linux_elf;
5458 set_be_option("ia32-gasmode=elf");
5460 case OS_SUPPORT_MACHO:
5461 create_ld_ident = create_ld_ident_macho;
5462 set_be_option("ia32-gasmode=macho");
5463 set_be_option("ia32-stackalign=4");
5464 set_be_option("pic");
5468 /* create idents for all known runtime functions */
5469 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5470 rts_idents[i] = new_id_from_str(rts_data[i].name);
5473 entitymap_init(&entitymap);
5476 static void init_ir_types(void)
5478 static int ir_types_initialized = 0;
5479 if (ir_types_initialized)
5481 ir_types_initialized = 1;
5483 ir_type_int = get_ir_type(type_int);
5484 ir_type_const_char = get_ir_type(type_const_char);
5485 ir_type_wchar_t = get_ir_type(type_wchar_t);
5486 ir_type_void = get_ir_type(type_void);
5489 void exit_ast2firm(void)
5491 entitymap_destroy(&entitymap);
5492 obstack_free(&asm_obst, NULL);
5495 static void global_asm_to_firm(statement_t *s)
5497 for (; s != NULL; s = s->base.next) {
5498 assert(s->kind == STATEMENT_ASM);
5500 char const *const text = s->asms.asm_text.begin;
5501 size_t size = s->asms.asm_text.size;
5503 /* skip the last \0 */
5504 if (text[size - 1] == '\0')
5507 ident *const id = new_id_from_chars(text, size);
5512 void translation_unit_to_firm(translation_unit_t *unit)
5514 /* just to be sure */
5515 continue_label = NULL;
5517 current_switch_cond = NULL;
5521 scope_to_firm(&unit->scope);
5522 global_asm_to_firm(unit->global_asm);
5524 current_ir_graph = NULL;