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 declaration_t **all_labels;
62 static ir_node *ijmp_list;
63 static bool constant_folding;
65 static const declaration_t *current_function_decl;
66 static ir_node *current_function_name;
67 static ir_node *current_funcsig;
68 static switch_statement_t *current_switch;
69 static ir_graph *current_function;
71 static entitymap_t entitymap;
73 static struct obstack asm_obst;
75 typedef enum declaration_kind_t {
76 DECLARATION_KIND_UNKNOWN,
77 DECLARATION_KIND_FUNCTION,
78 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
79 DECLARATION_KIND_GLOBAL_VARIABLE,
80 DECLARATION_KIND_LOCAL_VARIABLE,
81 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
82 DECLARATION_KIND_COMPOUND_MEMBER,
83 DECLARATION_KIND_LABEL_BLOCK,
84 DECLARATION_KIND_ENUM_ENTRY,
85 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
86 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
90 static ir_type *get_ir_type(type_t *type);
91 static ir_type *get_ir_type_incomplete(type_t *type);
92 static int count_decls_in_stmts(const statement_t *stmt);
94 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
96 const declaration_t *declaration = get_irg_loc_description(irg, pos);
98 if (declaration != NULL) {
99 warningf(&declaration->source_position,
100 "variable '%#T' might be used uninitialized",
101 declaration->type, declaration->symbol);
103 return new_r_Unknown(irg, mode);
106 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
108 const source_position_t *pos = (const source_position_t*) dbg;
111 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
115 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
117 const source_position_t *pos = (const source_position_t*) dbg;
122 return pos->input_name;
125 static dbg_info *get_dbg_info(const source_position_t *pos)
127 return (dbg_info*) pos;
130 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
132 static ir_mode *mode_int, *mode_uint;
134 static ir_node *_expression_to_firm(const expression_t *expression);
135 static ir_node *expression_to_firm(const expression_t *expression);
136 static inline ir_mode *get_ir_mode(type_t *type);
137 static void create_local_declaration(declaration_t *declaration);
139 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
141 unsigned flags = get_atomic_type_flags(kind);
142 unsigned size = get_atomic_type_size(kind);
143 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
144 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
147 unsigned bit_size = size * 8;
148 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
149 ir_mode_arithmetic arithmetic;
150 unsigned modulo_shift;
152 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
153 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
154 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
155 sort = irms_int_number;
156 arithmetic = irma_twos_complement;
157 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
159 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
160 snprintf(name, sizeof(name), "f%d", bit_size);
161 sort = irms_float_number;
162 arithmetic = irma_ieee754;
165 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
173 * Initialises the atomic modes depending on the machine size.
175 static void init_atomic_modes(void)
177 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
178 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
180 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
181 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
183 /* there's no real void type in firm */
184 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
186 /* initialize pointer modes */
188 ir_mode_sort sort = irms_reference;
189 unsigned bit_size = machine_size;
191 ir_mode_arithmetic arithmetic = irma_twos_complement;
192 unsigned modulo_shift
193 = bit_size < machine_size ? machine_size : bit_size;
195 snprintf(name, sizeof(name), "p%d", machine_size);
196 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
199 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
200 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
202 /* Hmm, pointers should be machine size */
203 set_modeP_data(ptr_mode);
204 set_modeP_code(ptr_mode);
207 static unsigned get_compound_type_size(compound_type_t *type)
209 ir_type *irtype = get_ir_type((type_t*) type);
210 return get_type_size_bytes(irtype);
213 static unsigned get_array_type_size(array_type_t *type)
215 assert(!type->is_vla);
216 ir_type *irtype = get_ir_type((type_t*) type);
217 return get_type_size_bytes(irtype);
221 static unsigned get_type_size_const(type_t *type)
225 panic("error type occurred");
227 return get_atomic_type_size(type->atomic.akind);
229 return 2 * get_atomic_type_size(type->complex.akind);
231 return get_atomic_type_size(type->imaginary.akind);
233 return get_mode_size_bytes(mode_int);
234 case TYPE_COMPOUND_UNION:
235 case TYPE_COMPOUND_STRUCT:
236 return get_compound_type_size(&type->compound);
238 /* just a pointer to the function */
239 return get_mode_size_bytes(mode_P_code);
241 return get_mode_size_bytes(mode_P_data);
243 return get_array_type_size(&type->array);
245 return get_type_size_const(type->builtin.real_type);
247 panic("type size of bitfield request");
253 panic("Trying to determine size of invalid type");
256 static ir_node *get_type_size(type_t *type)
258 type = skip_typeref(type);
260 if (is_type_array(type) && type->array.is_vla) {
261 ir_node *size_node = type->array.size_node;
262 if (size_node == NULL) {
263 size_node = expression_to_firm(type->array.size_expression);
264 assert(!is_Const(size_node));
265 type->array.size_node = size_node;
268 ir_node *elem_size = get_type_size(type->array.element_type);
269 ir_mode *mode = get_irn_mode(size_node);
270 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
274 ir_mode *mode = get_ir_mode(type_size_t);
276 sym.type_p = get_ir_type(type);
277 return new_SymConst(mode, sym, symconst_type_size);
280 static unsigned count_parameters(const function_type_t *function_type)
284 function_parameter_t *parameter = function_type->parameters;
285 for ( ; parameter != NULL; parameter = parameter->next) {
293 * Creates a Firm type for an atomic type
295 static ir_type *create_atomic_type(const atomic_type_t *type)
297 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
298 atomic_type_kind_t kind = type->akind;
299 ir_mode *mode = _atomic_modes[kind];
300 ident *id = get_mode_ident(mode);
301 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
303 set_type_alignment_bytes(irtype, type->base.alignment);
309 * Creates a Firm type for a complex type
311 static ir_type *create_complex_type(const complex_type_t *type)
313 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
314 atomic_type_kind_t kind = type->akind;
315 ir_mode *mode = _atomic_modes[kind];
316 ident *id = get_mode_ident(mode);
321 /* FIXME: finish the array */
326 * Creates a Firm type for an imaginary type
328 static ir_type *create_imaginary_type(const imaginary_type_t *type)
330 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
331 atomic_type_kind_t kind = type->akind;
332 ir_mode *mode = _atomic_modes[kind];
333 ident *id = get_mode_ident(mode);
334 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
336 set_type_alignment_bytes(irtype, type->base.alignment);
342 * return type of a parameter (and take transparent union gnu extension into
345 static type_t *get_parameter_type(type_t *type)
347 type = skip_typeref(type);
348 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
349 declaration_t *decl = type->compound.declaration;
350 type = decl->scope.declarations->type;
356 static ir_type *create_method_type(const function_type_t *function_type)
358 type_t *return_type = function_type->return_type;
360 ident *id = id_unique("functiontype.%u");
361 int n_parameters = count_parameters(function_type);
362 int n_results = return_type == type_void ? 0 : 1;
363 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
364 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
366 if (return_type != type_void) {
367 ir_type *restype = get_ir_type(return_type);
368 set_method_res_type(irtype, 0, restype);
371 function_parameter_t *parameter = function_type->parameters;
373 for ( ; parameter != NULL; parameter = parameter->next) {
374 type_t *type = get_parameter_type(parameter->type);
375 ir_type *p_irtype = get_ir_type(type);
376 set_method_param_type(irtype, n, p_irtype);
380 if (function_type->variadic || function_type->unspecified_parameters) {
381 set_method_variadicity(irtype, variadicity_variadic);
384 unsigned cc = get_method_calling_convention(irtype);
385 switch (function_type->calling_convention) {
386 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
389 set_method_calling_convention(irtype, SET_CDECL(cc));
393 if (function_type->variadic || function_type->unspecified_parameters)
396 /* only non-variadic function can use stdcall, else use cdecl */
397 set_method_calling_convention(irtype, SET_STDCALL(cc));
401 if (function_type->variadic || function_type->unspecified_parameters)
403 /* only non-variadic function can use fastcall, else use cdecl */
404 set_method_calling_convention(irtype, SET_FASTCALL(cc));
408 /* Hmm, leave default, not accepted by the parser yet. */
409 warningf(&function_type->base.source_position, "THISCALL calling convention not supported yet");
415 static ir_type *create_pointer_type(pointer_type_t *type)
417 type_t *points_to = type->points_to;
418 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
419 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
420 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
421 ir_points_to, mode_P_data, dbgi);
426 static ir_type *create_array_type(array_type_t *type)
428 type_t *element_type = type->element_type;
429 ir_type *ir_element_type = get_ir_type(element_type);
431 ident *id = id_unique("array.%u");
432 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
433 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
435 const int align = get_type_alignment_bytes(ir_element_type);
436 set_type_alignment_bytes(ir_type, align);
438 if (type->size_constant) {
439 int n_elements = type->size;
441 set_array_bounds_int(ir_type, 0, 0, n_elements);
443 size_t elemsize = get_type_size_bytes(ir_element_type);
444 if (elemsize % align > 0) {
445 elemsize += align - (elemsize % align);
447 set_type_size_bytes(ir_type, n_elements * elemsize);
449 set_array_lower_bound_int(ir_type, 0, 0);
451 set_type_state(ir_type, layout_fixed);
457 * Return the signed integer type of size bits.
459 * @param size the size
461 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
464 static ir_mode *s_modes[64 + 1] = {NULL, };
468 if (size <= 0 || size > 64)
471 mode = s_modes[size];
475 snprintf(name, sizeof(name), "bf_I%u", size);
476 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
477 size <= 32 ? 32 : size );
478 s_modes[size] = mode;
482 snprintf(name, sizeof(name), "I%u", size);
483 ident *id = new_id_from_str(name);
484 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
485 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
486 set_primitive_base_type(res, base_tp);
492 * Return the unsigned integer type of size bits.
494 * @param size the size
496 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
499 static ir_mode *u_modes[64 + 1] = {NULL, };
503 if (size <= 0 || size > 64)
506 mode = u_modes[size];
510 snprintf(name, sizeof(name), "bf_U%u", size);
511 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
512 size <= 32 ? 32 : size );
513 u_modes[size] = mode;
518 snprintf(name, sizeof(name), "U%u", size);
519 ident *id = new_id_from_str(name);
520 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
521 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
522 set_primitive_base_type(res, base_tp);
527 static ir_type *create_bitfield_type(bitfield_type_t *const type)
529 type_t *base = skip_typeref(type->base_type);
530 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
531 ir_type *irbase = get_ir_type(base);
533 unsigned size = fold_constant(type->size);
535 assert(!is_type_float(base));
536 if (is_type_signed(base)) {
537 return get_signed_int_type_for_bit_size(irbase, size);
539 return get_unsigned_int_type_for_bit_size(irbase, size);
543 #define INVALID_TYPE ((ir_type_ptr)-1)
546 COMPOUND_IS_STRUCT = false,
547 COMPOUND_IS_UNION = true
551 * Construct firm type from ast struct type.
553 * As anonymous inner structs get flattened to a single firm type, we might get
554 * irtype, outer_offset and out_align passed (they represent the position of
555 * the anonymous inner struct inside the resulting firm struct)
557 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
558 size_t *outer_offset, size_t *outer_align,
559 bool incomplete, bool is_union)
561 declaration_t *declaration = type->declaration;
562 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
564 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
565 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
567 return declaration->v.irtype;
569 size_t align_all = 1;
571 size_t bit_offset = 0;
574 if (irtype == NULL) {
575 symbol_t *symbol = declaration->symbol;
577 if (symbol != NULL) {
578 id = new_id_from_str(symbol->string);
581 id = id_unique("__anonymous_union.%u");
583 id = id_unique("__anonymous_struct.%u");
586 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
589 irtype = new_d_type_union(id, dbgi);
591 irtype = new_d_type_struct(id, dbgi);
594 declaration->declaration_kind
595 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
596 declaration->v.irtype = irtype;
597 //type->base.firm_type = irtype;
599 offset = *outer_offset;
600 align_all = *outer_align;
606 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
608 declaration_t *entry = declaration->scope.declarations;
609 for( ; entry != NULL; entry = entry->next) {
610 if (entry->namespc != NAMESPACE_NORMAL)
613 size_t prev_offset = offset;
615 symbol_t *symbol = entry->symbol;
616 type_t *entry_type = skip_typeref(entry->type);
617 dbg_info *dbgi = get_dbg_info(&entry->source_position);
620 if (symbol != NULL) {
621 ident = new_id_from_str(symbol->string);
623 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
624 create_compound_type(&entry_type->compound, irtype, &offset,
625 &align_all, false, COMPOUND_IS_STRUCT);
626 goto finished_member;
627 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
628 create_compound_type(&entry_type->compound, irtype, &offset,
629 &align_all, false, COMPOUND_IS_UNION);
630 goto finished_member;
632 assert(entry_type->kind == TYPE_BITFIELD);
634 ident = id_unique("anon.%u");
637 ir_type *base_irtype;
638 if (entry_type->kind == TYPE_BITFIELD) {
639 base_irtype = get_ir_type(entry_type->bitfield.base_type);
641 base_irtype = get_ir_type(entry_type);
644 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
645 size_t misalign = offset % entry_alignment;
647 ir_type *entry_irtype = get_ir_type(entry_type);
648 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
651 size_t bits_remainder;
652 if (entry_type->kind == TYPE_BITFIELD) {
653 size_t size_bits = fold_constant(entry_type->bitfield.size);
654 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
656 if (size_bits > rest_size_bits) {
657 /* start a new bucket */
658 offset += entry_alignment - misalign;
664 /* put into current bucket */
665 base = offset - misalign;
666 bits_remainder = misalign * 8 + bit_offset;
669 offset += size_bits / 8;
670 bit_offset = bit_offset + (size_bits % 8);
672 size_t entry_size = get_type_size_bytes(base_irtype);
673 if (misalign > 0 || bit_offset > 0)
674 offset += entry_alignment - misalign;
678 offset += entry_size;
682 if (entry_alignment > align_all) {
683 if (entry_alignment % align_all != 0) {
684 panic("uneven alignments not supported yet");
686 align_all = entry_alignment;
689 set_entity_offset(entity, base);
690 set_entity_offset_bits_remainder(entity,
691 (unsigned char) bits_remainder);
692 //add_struct_member(irtype, entity);
693 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
694 assert(entry->v.entity == NULL);
695 entry->v.entity = entity;
699 size_t entry_size = offset - prev_offset;
700 if (entry_size > size) {
712 size_t misalign = offset % align_all;
713 if (misalign > 0 || bit_offset > 0) {
714 size += align_all - misalign;
717 if (outer_offset != NULL) {
719 *outer_offset = offset;
721 *outer_offset += size;
724 if (align_all > *outer_align) {
725 if (align_all % *outer_align != 0) {
726 panic("uneven alignments not supported yet");
728 *outer_align = align_all;
731 set_type_alignment_bytes(irtype, align_all);
732 set_type_size_bytes(irtype, size);
733 set_type_state(irtype, layout_fixed);
739 static ir_type *create_enum_type(enum_type_t *const type)
741 type->base.firm_type = ir_type_int;
743 ir_mode *const mode = mode_int;
744 tarval *const one = get_mode_one(mode);
745 tarval * tv_next = get_tarval_null(mode);
747 bool constant_folding_old = constant_folding;
748 constant_folding = true;
750 declaration_t *declaration = type->declaration->next;
751 for (; declaration != NULL; declaration = declaration->next) {
752 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
755 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
757 expression_t *const init = declaration->init.enum_value;
759 ir_node *const cnst = expression_to_firm(init);
760 if (!is_Const(cnst)) {
761 panic("couldn't fold constant");
763 tv_next = get_Const_tarval(cnst);
765 declaration->v.enum_val = tv_next;
766 tv_next = tarval_add(tv_next, one);
769 constant_folding = constant_folding_old;
774 static ir_type *get_ir_type_incomplete(type_t *type)
776 assert(type != NULL);
777 type = skip_typeref(type);
779 if (type->base.firm_type != NULL) {
780 assert(type->base.firm_type != INVALID_TYPE);
781 return type->base.firm_type;
784 switch (type->kind) {
785 case TYPE_COMPOUND_STRUCT:
786 return create_compound_type(&type->compound, NULL, NULL, NULL,
787 true, COMPOUND_IS_STRUCT);
788 case TYPE_COMPOUND_UNION:
789 return create_compound_type(&type->compound, NULL, NULL, NULL,
790 true, COMPOUND_IS_UNION);
792 return get_ir_type(type);
796 static ir_type *get_ir_type(type_t *type)
798 assert(type != NULL);
800 type = skip_typeref(type);
802 if (type->base.firm_type != NULL) {
803 assert(type->base.firm_type != INVALID_TYPE);
804 return type->base.firm_type;
807 ir_type *firm_type = NULL;
808 switch (type->kind) {
810 panic("error type occurred");
812 firm_type = create_atomic_type(&type->atomic);
815 firm_type = create_complex_type(&type->complex);
818 firm_type = create_imaginary_type(&type->imaginary);
821 firm_type = create_method_type(&type->function);
824 firm_type = create_pointer_type(&type->pointer);
827 firm_type = create_array_type(&type->array);
829 case TYPE_COMPOUND_STRUCT:
830 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
831 false, COMPOUND_IS_STRUCT);
833 case TYPE_COMPOUND_UNION:
834 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
835 false, COMPOUND_IS_UNION);
838 firm_type = create_enum_type(&type->enumt);
841 firm_type = get_ir_type(type->builtin.real_type);
844 firm_type = create_bitfield_type(&type->bitfield);
852 if (firm_type == NULL)
853 panic("unknown type found");
855 type->base.firm_type = firm_type;
859 static inline ir_mode *get_ir_mode(type_t *type)
861 ir_type *irtype = get_ir_type(type);
863 /* firm doesn't report a mode for arrays somehow... */
864 if (is_Array_type(irtype)) {
868 ir_mode *mode = get_type_mode(irtype);
869 assert(mode != NULL);
873 /** Names of the runtime functions. */
874 static const struct {
875 int id; /**< the rts id */
876 int n_res; /**< number of return values */
877 const char *name; /**< the name of the rts function */
878 int n_params; /**< number of parameters */
879 unsigned flags; /**< language flags */
881 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
882 { rts_abort, 0, "abort", 0, _C89 },
883 { rts_alloca, 1, "alloca", 1, _ALL },
884 { rts_abs, 1, "abs", 1, _C89 },
885 { rts_labs, 1, "labs", 1, _C89 },
886 { rts_llabs, 1, "llabs", 1, _C99 },
887 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
889 { rts_fabs, 1, "fabs", 1, _C89 },
890 { rts_sqrt, 1, "sqrt", 1, _C89 },
891 { rts_cbrt, 1, "cbrt", 1, _C99 },
892 { rts_exp, 1, "exp", 1, _C89 },
893 { rts_exp2, 1, "exp2", 1, _C89 },
894 { rts_exp10, 1, "exp10", 1, _GNUC },
895 { rts_log, 1, "log", 1, _C89 },
896 { rts_log2, 1, "log2", 1, _C89 },
897 { rts_log10, 1, "log10", 1, _C89 },
898 { rts_pow, 1, "pow", 2, _C89 },
899 { rts_sin, 1, "sin", 1, _C89 },
900 { rts_cos, 1, "cos", 1, _C89 },
901 { rts_tan, 1, "tan", 1, _C89 },
902 { rts_asin, 1, "asin", 1, _C89 },
903 { rts_acos, 1, "acos", 1, _C89 },
904 { rts_atan, 1, "atan", 1, _C89 },
905 { rts_sinh, 1, "sinh", 1, _C89 },
906 { rts_cosh, 1, "cosh", 1, _C89 },
907 { rts_tanh, 1, "tanh", 1, _C89 },
909 { rts_fabsf, 1, "fabsf", 1, _C99 },
910 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
911 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
912 { rts_expf, 1, "expf", 1, _C99 },
913 { rts_exp2f, 1, "exp2f", 1, _C99 },
914 { rts_exp10f, 1, "exp10f", 1, _GNUC },
915 { rts_logf, 1, "logf", 1, _C99 },
916 { rts_log2f, 1, "log2f", 1, _C99 },
917 { rts_log10f, 1, "log10f", 1, _C99 },
918 { rts_powf, 1, "powf", 2, _C99 },
919 { rts_sinf, 1, "sinf", 1, _C99 },
920 { rts_cosf, 1, "cosf", 1, _C99 },
921 { rts_tanf, 1, "tanf", 1, _C99 },
922 { rts_asinf, 1, "asinf", 1, _C99 },
923 { rts_acosf, 1, "acosf", 1, _C99 },
924 { rts_atanf, 1, "atanf", 1, _C99 },
925 { rts_sinhf, 1, "sinhf", 1, _C99 },
926 { rts_coshf, 1, "coshf", 1, _C99 },
927 { rts_tanhf, 1, "tanhf", 1, _C99 },
929 { rts_fabsl, 1, "fabsl", 1, _C99 },
930 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
931 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
932 { rts_expl, 1, "expl", 1, _C99 },
933 { rts_exp2l, 1, "exp2l", 1, _C99 },
934 { rts_exp10l, 1, "exp10l", 1, _GNUC },
935 { rts_logl, 1, "logl", 1, _C99 },
936 { rts_log2l, 1, "log2l", 1, _C99 },
937 { rts_log10l, 1, "log10l", 1, _C99 },
938 { rts_powl, 1, "powl", 2, _C99 },
939 { rts_sinl, 1, "sinl", 1, _C99 },
940 { rts_cosl, 1, "cosl", 1, _C99 },
941 { rts_tanl, 1, "tanl", 1, _C99 },
942 { rts_asinl, 1, "asinl", 1, _C99 },
943 { rts_acosl, 1, "acosl", 1, _C99 },
944 { rts_atanl, 1, "atanl", 1, _C99 },
945 { rts_sinhl, 1, "sinhl", 1, _C99 },
946 { rts_coshl, 1, "coshl", 1, _C99 },
947 { rts_tanhl, 1, "tanhl", 1, _C99 },
949 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
950 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
951 { rts_strcmp, 1, "strcmp", 2, _C89 },
952 { rts_strncmp, 1, "strncmp", 3, _C89 }
955 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
958 * Mangles an entity linker (ld) name for win32 usage.
960 * @param ent the entity to be mangled
961 * @param declaration the declaration
963 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
967 if (is_Method_type(get_entity_type(ent)))
968 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
970 /* always add an underscore in win32 */
971 id = mangle(id_underscore, get_entity_ident(ent));
974 decl_modifiers_t decl_modifiers = declaration->modifiers;
975 if (decl_modifiers & DM_DLLIMPORT) {
976 /* add prefix for imported symbols */
977 id = mangle(id_imp, id);
983 * Mangles an entity linker (ld) name for Linux ELF usage.
985 * @param ent the entity to be mangled
986 * @param declaration the declaration
988 static ident *create_ld_ident_linux_elf(ir_entity *entity,
989 declaration_t *declaration)
992 return get_entity_ident(entity);
996 * Mangles an entity linker (ld) name for Mach-O usage.
998 * @param ent the entity to be mangled
999 * @param declaration the declaration
1001 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
1004 ident *id = mangle(id_underscore, get_entity_ident(ent));
1008 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
1009 declaration_t *declaration);
1010 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
1013 * Handle GNU attributes for entities
1015 * @param ent the entity
1016 * @param decl the routine declaration
1018 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
1020 if (decl->modifiers & DM_PURE) {
1021 /* TRUE if the declaration includes the GNU
1022 __attribute__((pure)) specifier. */
1023 set_entity_additional_property(ent, mtp_property_pure);
1025 if (decl->modifiers & DM_USED) {
1026 /* TRUE if the declaration includes the GNU
1027 __attribute__((used)) specifier. */
1028 set_entity_stickyness(ent, stickyness_sticky);
1033 * Creates an entity representing a function.
1035 * @param declaration the function declaration
1037 static ir_entity *get_function_entity(declaration_t *declaration)
1039 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
1040 return declaration->v.entity;
1041 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1043 symbol_t *symbol = declaration->symbol;
1044 ident *id = new_id_from_str(symbol->string);
1046 ir_type *global_type = get_glob_type();
1047 ir_type *ir_type_method = get_ir_type(declaration->type);
1048 assert(is_Method_type(ir_type_method));
1050 /* already an entity defined? */
1051 ir_entity *entity = entitymap_get(&entitymap, symbol);
1052 if (entity != NULL) {
1053 if (get_entity_visibility(entity) == visibility_external_allocated
1054 && declaration->init.statement != NULL) {
1055 set_entity_visibility(entity, visibility_external_visible);
1057 goto entity_created;
1060 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1061 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1062 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1064 handle_gnu_attributes_ent(entity, declaration);
1066 /* static inline => local
1067 * extern inline => local
1068 * inline without definition => local
1069 * inline with definition => external_visible */
1070 storage_class_tag_t const storage_class = (storage_class_tag_t)declaration->storage_class;
1071 bool const is_inline = declaration->is_inline;
1072 bool const has_body = declaration->init.statement != NULL;
1073 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1074 set_entity_visibility(entity, visibility_external_visible);
1075 } else if (storage_class == STORAGE_CLASS_STATIC ||
1076 (is_inline && has_body)) {
1078 /* this entity was declared, but is defined nowhere */
1079 set_entity_peculiarity(entity, peculiarity_description);
1081 set_entity_visibility(entity, visibility_local);
1082 } else if (has_body) {
1083 set_entity_visibility(entity, visibility_external_visible);
1085 set_entity_visibility(entity, visibility_external_allocated);
1087 set_entity_allocation(entity, allocation_static);
1089 /* We should check for file scope here, but as long as we compile C only
1090 this is not needed. */
1091 if (! firm_opt.freestanding) {
1092 /* check for a known runtime function */
1093 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1094 if (id != rts_idents[i])
1097 /* ignore those rts functions not necessary needed for current mode */
1098 if ((c_mode & rts_data[i].flags) == 0)
1100 assert(rts_entities[rts_data[i].id] == NULL);
1101 rts_entities[rts_data[i].id] = entity;
1105 entitymap_insert(&entitymap, symbol, entity);
1108 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1109 declaration->v.entity = entity;
1115 * Creates a Const node representing a constant.
1117 static ir_node *const_to_firm(const const_expression_t *cnst)
1119 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1120 ir_mode *mode = get_ir_mode(cnst->base.type);
1125 if (mode_is_float(mode)) {
1126 tv = new_tarval_from_double(cnst->v.float_value, mode);
1128 if (mode_is_signed(mode)) {
1129 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1131 len = snprintf(buf, sizeof(buf), "%llu",
1132 (unsigned long long) cnst->v.int_value);
1134 tv = new_tarval_from_str(buf, len, mode);
1137 return new_d_Const(dbgi, mode, tv);
1141 * Creates a Const node representing a character constant.
1143 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1145 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1146 ir_mode *mode = get_ir_mode(cnst->base.type);
1148 long long int v = 0;
1149 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1150 if (char_is_signed) {
1151 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1153 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1157 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1158 tarval *tv = new_tarval_from_str(buf, len, mode);
1160 return new_d_Const(dbgi, mode, tv);
1164 * Creates a Const node representing a wide character constant.
1166 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1168 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1169 ir_mode *mode = get_ir_mode(cnst->base.type);
1171 long long int v = cnst->v.wide_character.begin[0];
1174 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1175 tarval *tv = new_tarval_from_str(buf, len, mode);
1177 return new_d_Const(dbgi, mode, tv);
1181 * Creates a SymConst for a given entity.
1183 * @param dbgi debug info
1184 * @param mode the (reference) mode for the SymConst
1185 * @param entity the entity
1187 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1190 assert(entity != NULL);
1191 union symconst_symbol sym;
1192 sym.entity_p = entity;
1193 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1197 * Creates a SymConst node representing a string constant.
1199 * @param src_pos the source position of the string constant
1200 * @param id_prefix a prefix for the name of the generated string constant
1201 * @param value the value of the string constant
1203 static ir_node *string_to_firm(const source_position_t *const src_pos,
1204 const char *const id_prefix,
1205 const string_t *const value)
1207 ir_type *const global_type = get_glob_type();
1208 dbg_info *const dbgi = get_dbg_info(src_pos);
1209 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1210 ir_type_const_char, dbgi);
1212 ident *const id = id_unique(id_prefix);
1213 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1214 set_entity_ld_ident(entity, id);
1215 set_entity_variability(entity, variability_constant);
1216 set_entity_allocation(entity, allocation_static);
1218 ir_type *const elem_type = ir_type_const_char;
1219 ir_mode *const mode = get_type_mode(elem_type);
1221 const char* const string = value->begin;
1222 const size_t slen = value->size;
1224 set_array_lower_bound_int(type, 0, 0);
1225 set_array_upper_bound_int(type, 0, slen);
1226 set_type_size_bytes(type, slen);
1227 set_type_state(type, layout_fixed);
1229 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1230 for(size_t i = 0; i < slen; ++i) {
1231 tvs[i] = new_tarval_from_long(string[i], mode);
1234 set_array_entity_values(entity, tvs, slen);
1237 return create_symconst(dbgi, mode_P_data, entity);
1241 * Creates a SymConst node representing a string literal.
1243 * @param literal the string literal
1245 static ir_node *string_literal_to_firm(
1246 const string_literal_expression_t* literal)
1248 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1253 * Creates a SymConst node representing a wide string literal.
1255 * @param literal the wide string literal
1257 static ir_node *wide_string_literal_to_firm(
1258 const wide_string_literal_expression_t* const literal)
1260 ir_type *const global_type = get_glob_type();
1261 ir_type *const elem_type = ir_type_wchar_t;
1262 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1263 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1266 ident *const id = id_unique("Lstr.%u");
1267 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1268 set_entity_ld_ident(entity, id);
1269 set_entity_variability(entity, variability_constant);
1270 set_entity_allocation(entity, allocation_static);
1272 ir_mode *const mode = get_type_mode(elem_type);
1274 const wchar_rep_t *const string = literal->value.begin;
1275 const size_t slen = literal->value.size;
1277 set_array_lower_bound_int(type, 0, 0);
1278 set_array_upper_bound_int(type, 0, slen);
1279 set_type_size_bytes(type, slen);
1280 set_type_state(type, layout_fixed);
1282 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1283 for(size_t i = 0; i < slen; ++i) {
1284 tvs[i] = new_tarval_from_long(string[i], mode);
1287 set_array_entity_values(entity, tvs, slen);
1290 return create_symconst(dbgi, mode_P_data, entity);
1293 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1294 ir_node *const addr)
1296 ir_type *irtype = get_ir_type(type);
1297 if (is_compound_type(irtype)
1298 || is_Method_type(irtype)
1299 || is_Array_type(irtype)) {
1303 ir_mode *const mode = get_type_mode(irtype);
1304 ir_node *const memory = get_store();
1305 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1306 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1307 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1309 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1310 set_Load_volatility(load, volatility_is_volatile);
1313 set_store(load_mem);
1318 * Creates a strict Conv if neccessary.
1320 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1322 ir_mode *mode = get_irn_mode(node);
1324 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1326 if (!mode_is_float(mode))
1329 /* check if there is already a Conv */
1330 if (is_Conv(node)) {
1331 /* convert it into a strict Conv */
1332 set_Conv_strict(node, 1);
1336 /* otherwise create a new one */
1337 return new_d_strictConv(dbgi, node, mode);
1340 static ir_node *get_global_var_address(dbg_info *const dbgi,
1341 const declaration_t *const decl)
1343 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1345 ir_entity *const entity = decl->v.entity;
1346 switch ((storage_class_tag_t)decl->storage_class) {
1347 case STORAGE_CLASS_THREAD:
1348 case STORAGE_CLASS_THREAD_EXTERN:
1349 case STORAGE_CLASS_THREAD_STATIC: {
1350 ir_node *const no_mem = new_NoMem();
1351 ir_node *const tls = get_irg_tls(current_ir_graph);
1352 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1356 return create_symconst(dbgi, mode_P_data, entity);
1361 * Returns the correct base address depending on whether it is a parameter or a
1362 * normal local variable.
1364 static ir_node *get_local_frame(ir_entity *const ent)
1366 ir_graph *const irg = current_ir_graph;
1367 const ir_type *const owner = get_entity_owner(ent);
1368 if (owner == get_irg_frame_type(irg)) {
1369 return get_irg_frame(irg);
1371 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1372 return get_irg_value_param_base(irg);
1376 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1378 ir_mode *value_mode = get_irn_mode(value);
1380 if (value_mode == dest_mode || is_Bad(value))
1383 if (dest_mode == mode_b) {
1384 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1385 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1386 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1390 return new_d_Conv(dbgi, value, dest_mode);
1394 * Keep all memory edges of the given block.
1396 static void keep_all_memory(ir_node *block) {
1397 ir_node *old = get_cur_block();
1399 set_cur_block(block);
1400 keep_alive(get_store());
1401 /* TODO: keep all memory edges from restricted pointers */
1405 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1407 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1408 declaration_t *declaration = ref->declaration;
1409 type_t *type = skip_typeref(declaration->type);
1411 /* make sure the type is constructed */
1412 (void) get_ir_type(type);
1414 switch ((declaration_kind_t)declaration->declaration_kind) {
1415 case DECLARATION_KIND_TYPE:
1416 case DECLARATION_KIND_UNKNOWN:
1419 case DECLARATION_KIND_ENUM_ENTRY: {
1420 ir_mode *const mode = get_ir_mode(type);
1421 return new_Const(mode, declaration->v.enum_val);
1424 case DECLARATION_KIND_LOCAL_VARIABLE: {
1425 ir_mode *const mode = get_ir_mode(type);
1426 return get_value(declaration->v.value_number, mode);
1428 case DECLARATION_KIND_FUNCTION: {
1429 ir_mode *const mode = get_ir_mode(type);
1430 return create_symconst(dbgi, mode, declaration->v.entity);
1432 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1433 ir_node *const addr = get_global_var_address(dbgi, declaration);
1434 return deref_address(dbgi, declaration->type, addr);
1437 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1438 ir_entity *entity = declaration->v.entity;
1439 ir_node *frame = get_local_frame(entity);
1440 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1441 return deref_address(dbgi, declaration->type, sel);
1444 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1445 return declaration->v.vla_base;
1447 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1448 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1449 case DECLARATION_KIND_COMPOUND_MEMBER:
1450 case DECLARATION_KIND_LABEL_BLOCK:
1451 panic("not implemented reference type");
1454 panic("reference to declaration with unknown type found");
1457 static ir_node *reference_addr(const reference_expression_t *ref)
1459 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1460 declaration_t *declaration = ref->declaration;
1462 switch((declaration_kind_t) declaration->declaration_kind) {
1463 case DECLARATION_KIND_TYPE:
1464 case DECLARATION_KIND_UNKNOWN:
1466 case DECLARATION_KIND_LOCAL_VARIABLE:
1467 /* you can store to a local variable (so we don't panic but return NULL
1468 * as an indicator for no real address) */
1470 case DECLARATION_KIND_FUNCTION: {
1471 type_t *const type = skip_typeref(ref->base.type);
1472 ir_mode *const mode = get_ir_mode(type);
1473 return create_symconst(dbgi, mode, declaration->v.entity);
1475 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1476 ir_node *const addr = get_global_var_address(dbgi, declaration);
1479 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1480 ir_entity *entity = declaration->v.entity;
1481 ir_node *frame = get_local_frame(entity);
1482 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1487 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1488 return declaration->v.vla_base;
1490 case DECLARATION_KIND_ENUM_ENTRY:
1491 panic("trying to reference enum entry");
1493 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1494 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1495 case DECLARATION_KIND_COMPOUND_MEMBER:
1496 case DECLARATION_KIND_LABEL_BLOCK:
1497 panic("not implemented reference type");
1500 panic("reference to declaration with unknown type found");
1504 * Transform calls to builtin functions.
1506 static ir_node *process_builtin_call(const call_expression_t *call)
1508 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1510 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1511 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1513 type_t *type = skip_typeref(builtin->base.type);
1514 assert(is_type_pointer(type));
1516 type_t *function_type = skip_typeref(type->pointer.points_to);
1517 symbol_t *symbol = builtin->symbol;
1519 switch(symbol->ID) {
1520 case T___builtin_alloca: {
1521 if (call->arguments == NULL || call->arguments->next != NULL) {
1522 panic("invalid number of parameters on __builtin_alloca");
1524 expression_t *argument = call->arguments->expression;
1525 ir_node *size = expression_to_firm(argument);
1527 ir_node *store = get_store();
1528 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1530 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1532 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1536 case T___builtin_huge_val: {
1537 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1538 tarval *tv = get_mode_infinite(mode);
1539 ir_node *res = new_d_Const(dbgi, mode, tv);
1542 case T___builtin_nan:
1543 case T___builtin_nanf:
1544 case T___builtin_nand: {
1545 /* Ignore string for now... */
1546 assert(is_type_function(function_type));
1547 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1548 tarval *tv = get_mode_NAN(mode);
1549 ir_node *res = new_d_Const(dbgi, mode, tv);
1552 case T___builtin_va_end:
1555 panic("Unsupported builtin found\n");
1560 * Transform a call expression.
1561 * Handles some special cases, like alloca() calls, which must be resolved
1562 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1563 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1566 static ir_node *call_expression_to_firm(const call_expression_t *call)
1568 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1569 assert(get_cur_block() != NULL);
1571 expression_t *function = call->function;
1572 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1573 return process_builtin_call(call);
1575 if (function->kind == EXPR_REFERENCE) {
1576 const reference_expression_t *ref = &function->reference;
1577 declaration_t *declaration = ref->declaration;
1579 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1580 if (declaration->v.entity == rts_entities[rts_alloca]) {
1581 /* handle alloca() call */
1582 expression_t *argument = call->arguments->expression;
1583 ir_node *size = expression_to_firm(argument);
1585 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1587 ir_node *store = get_store();
1588 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1589 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1591 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1593 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1599 ir_node *callee = expression_to_firm(function);
1601 type_t *type = skip_typeref(function->base.type);
1602 assert(is_type_pointer(type));
1603 pointer_type_t *pointer_type = &type->pointer;
1604 type_t *points_to = skip_typeref(pointer_type->points_to);
1605 assert(is_type_function(points_to));
1606 function_type_t *function_type = &points_to->function;
1608 int n_parameters = 0;
1609 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1610 ir_type *new_method_type = NULL;
1611 if (function_type->variadic || function_type->unspecified_parameters) {
1612 const call_argument_t *argument = call->arguments;
1613 for( ; argument != NULL; argument = argument->next) {
1617 /* we need to construct a new method type matching the call
1619 int n_res = get_method_n_ress(ir_method_type);
1620 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1621 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1622 n_parameters, n_res, dbgi);
1623 set_method_calling_convention(new_method_type,
1624 get_method_calling_convention(ir_method_type));
1625 set_method_additional_properties(new_method_type,
1626 get_method_additional_properties(ir_method_type));
1627 set_method_variadicity(new_method_type,
1628 get_method_variadicity(ir_method_type));
1630 for(int i = 0; i < n_res; ++i) {
1631 set_method_res_type(new_method_type, i,
1632 get_method_res_type(ir_method_type, i));
1634 argument = call->arguments;
1635 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1636 expression_t *expression = argument->expression;
1637 ir_type *irtype = get_ir_type(expression->base.type);
1638 set_method_param_type(new_method_type, i, irtype);
1640 ir_method_type = new_method_type;
1642 n_parameters = get_method_n_params(ir_method_type);
1645 ir_node *in[n_parameters];
1647 const call_argument_t *argument = call->arguments;
1648 for(int n = 0; n < n_parameters; ++n) {
1649 expression_t *expression = argument->expression;
1650 ir_node *arg_node = expression_to_firm(expression);
1652 arg_node = do_strict_conv(dbgi, arg_node);
1656 argument = argument->next;
1659 ir_node *store = get_store();
1660 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1662 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1665 type_t *return_type = skip_typeref(function_type->return_type);
1666 ir_node *result = NULL;
1668 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1670 if (is_type_scalar(return_type)) {
1671 mode = get_ir_mode(return_type);
1675 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1676 result = new_d_Proj(dbgi, resproj, mode, 0);
1679 if (function->kind == EXPR_REFERENCE &&
1680 function->reference.declaration->modifiers & DM_NORETURN) {
1681 /* A dead end: Keep the Call and the Block. Also place all further
1682 * nodes into a new and unreachable block. */
1684 keep_alive(get_cur_block());
1691 static void statement_to_firm(statement_t *statement);
1692 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1694 static ir_node *expression_to_addr(const expression_t *expression);
1695 static ir_node *create_condition_evaluation(const expression_t *expression,
1696 ir_node *true_block,
1697 ir_node *false_block);
1699 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1702 value = do_strict_conv(dbgi, value);
1704 ir_node *memory = get_store();
1706 if (is_type_scalar(type)) {
1707 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1708 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1709 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1710 set_Store_volatility(store, volatility_is_volatile);
1711 set_store(store_mem);
1713 ir_type *irtype = get_ir_type(type);
1714 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1715 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1716 set_store(copyb_mem);
1720 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1722 tarval *all_one = get_mode_all_one(mode);
1723 int mode_size = get_mode_size_bits(mode);
1725 assert(offset >= 0);
1727 assert(offset + size <= mode_size);
1728 if (size == mode_size) {
1732 long shiftr = get_mode_size_bits(mode) - size;
1733 long shiftl = offset;
1734 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1735 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1736 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1737 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1742 static void bitfield_store_to_firm(const select_expression_t *expression,
1743 ir_node *addr, ir_node *value)
1745 type_t *type = expression->base.type;
1746 ir_mode *mode = get_ir_mode(type);
1748 assert(get_irn_mode(value) == mode || is_Bad(value));
1750 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1752 /* kill upper bits of value and shift to right position */
1753 ir_entity *entity = expression->compound_entry->v.entity;
1754 int bitoffset = get_entity_offset_bits_remainder(entity);
1755 ir_type *entity_type = get_entity_type(entity);
1756 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1758 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1759 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1760 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1761 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1762 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1763 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1765 /* load current value */
1766 ir_node *mem = get_store();
1767 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1768 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1769 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1770 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1771 tarval *inv_mask = tarval_not(shift_mask);
1772 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1773 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1775 /* construct new value and store */
1776 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1777 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1778 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1779 set_store(store_mem);
1781 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1782 set_Load_volatility(load, volatility_is_volatile);
1783 set_Store_volatility(store, volatility_is_volatile);
1787 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1790 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1791 type_t *type = expression->base.type;
1792 ir_mode *mode = get_ir_mode(type);
1793 ir_node *mem = get_store();
1794 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1795 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1796 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1798 load_res = create_conv(dbgi, load_res, mode_int);
1800 set_store(load_mem);
1802 /* kill upper bits */
1803 ir_entity *entity = expression->compound_entry->v.entity;
1804 int bitoffset = get_entity_offset_bits_remainder(entity);
1805 ir_type *entity_type = get_entity_type(entity);
1806 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1807 long shift_bitsl = machine_size - bitoffset - bitsize;
1808 assert(shift_bitsl >= 0);
1809 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1810 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1811 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1813 long shift_bitsr = bitoffset + shift_bitsl;
1814 assert(shift_bitsr <= (long) machine_size);
1815 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1816 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1818 if (mode_is_signed(mode)) {
1819 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1821 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1824 return create_conv(dbgi, shiftr, mode);
1827 static void set_value_for_expression_addr(const expression_t *expression,
1828 ir_node *value, ir_node *addr)
1830 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1831 value = do_strict_conv(dbgi, value);
1833 if (expression->kind == EXPR_REFERENCE) {
1834 const reference_expression_t *ref = &expression->reference;
1836 declaration_t *declaration = ref->declaration;
1837 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1838 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1839 set_value(declaration->v.value_number, value);
1845 addr = expression_to_addr(expression);
1847 type_t *type = skip_typeref(expression->base.type);
1849 if (expression->kind == EXPR_SELECT) {
1850 const select_expression_t *select = &expression->select;
1852 declaration_t *declaration = select->compound_entry;
1853 if (declaration->type->kind == TYPE_BITFIELD) {
1854 bitfield_store_to_firm(select, addr, value);
1859 assign_value(dbgi, addr, type, value);
1862 static void set_value_for_expression(const expression_t *expression,
1865 set_value_for_expression_addr(expression, value, NULL);
1868 static ir_node *get_value_from_lvalue(const expression_t *expression,
1871 if (expression->kind == EXPR_REFERENCE) {
1872 const reference_expression_t *ref = &expression->reference;
1874 declaration_t *declaration = ref->declaration;
1875 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1876 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1877 assert(addr == NULL);
1878 ir_mode *mode = get_ir_mode(expression->base.type);
1879 return get_value(declaration->v.value_number, mode);
1883 assert(addr != NULL);
1884 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1887 if (expression->kind == EXPR_SELECT &&
1888 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1889 value = bitfield_extract_to_firm(&expression->select, addr);
1891 value = deref_address(dbgi, expression->base.type, addr);
1898 static ir_node *create_incdec(const unary_expression_t *expression)
1900 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1901 const expression_t *value_expr = expression->value;
1902 ir_node *addr = expression_to_addr(value_expr);
1903 ir_node *value = get_value_from_lvalue(value_expr, addr);
1905 type_t *type = skip_typeref(expression->base.type);
1906 ir_mode *mode = get_ir_mode(expression->base.type);
1909 if (is_type_pointer(type)) {
1910 pointer_type_t *pointer_type = &type->pointer;
1911 offset = get_type_size(pointer_type->points_to);
1913 assert(is_type_arithmetic(type));
1914 offset = new_Const(mode, get_mode_one(mode));
1918 ir_node *store_value;
1919 switch(expression->base.kind) {
1920 case EXPR_UNARY_POSTFIX_INCREMENT:
1922 store_value = new_d_Add(dbgi, value, offset, mode);
1924 case EXPR_UNARY_POSTFIX_DECREMENT:
1926 store_value = new_d_Sub(dbgi, value, offset, mode);
1928 case EXPR_UNARY_PREFIX_INCREMENT:
1929 result = new_d_Add(dbgi, value, offset, mode);
1930 store_value = result;
1932 case EXPR_UNARY_PREFIX_DECREMENT:
1933 result = new_d_Sub(dbgi, value, offset, mode);
1934 store_value = result;
1937 panic("no incdec expr in create_incdec");
1940 set_value_for_expression_addr(value_expr, store_value, addr);
1945 static bool is_local_variable(expression_t *expression)
1947 if (expression->kind != EXPR_REFERENCE)
1949 reference_expression_t *ref_expr = &expression->reference;
1950 declaration_t *declaration = ref_expr->declaration;
1951 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1954 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1957 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1958 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1959 case EXPR_BINARY_NOTEQUAL:
1960 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1961 case EXPR_BINARY_ISLESS:
1962 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1963 case EXPR_BINARY_ISLESSEQUAL:
1964 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1965 case EXPR_BINARY_ISGREATER:
1966 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1967 case EXPR_BINARY_ISGREATEREQUAL:
1968 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1969 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1974 panic("trying to get pn_Cmp from non-comparison binexpr type");
1978 * Handle the assume optimizer hint: check if a Confirm
1979 * node can be created.
1981 * @param dbi debug info
1982 * @param expr the IL assume expression
1984 * we support here only some simple cases:
1989 static ir_node *handle_assume_compare(dbg_info *dbi,
1990 const binary_expression_t *expression)
1992 expression_t *op1 = expression->left;
1993 expression_t *op2 = expression->right;
1994 declaration_t *var2, *var = NULL;
1995 ir_node *res = NULL;
1998 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2000 if (is_local_variable(op1) && is_local_variable(op2)) {
2001 var = op1->reference.declaration;
2002 var2 = op2->reference.declaration;
2004 type_t *const type = skip_typeref(var->type);
2005 ir_mode *const mode = get_ir_mode(type);
2007 ir_node *const irn1 = get_value(var->v.value_number, mode);
2008 ir_node *const irn2 = get_value(var2->v.value_number, mode);
2010 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2011 set_value(var2->v.value_number, res);
2013 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2014 set_value(var->v.value_number, res);
2020 if (is_local_variable(op1) && is_constant_expression(op2)) {
2021 var = op1->reference.declaration;
2023 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2024 cmp_val = get_inversed_pnc(cmp_val);
2025 var = op2->reference.declaration;
2030 type_t *const type = skip_typeref(var->type);
2031 ir_mode *const mode = get_ir_mode(type);
2033 res = get_value(var->v.value_number, mode);
2034 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2035 set_value(var->v.value_number, res);
2041 * Handle the assume optimizer hint.
2043 * @param dbi debug info
2044 * @param expr the IL assume expression
2046 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2047 switch(expression->kind) {
2048 case EXPR_BINARY_EQUAL:
2049 case EXPR_BINARY_NOTEQUAL:
2050 case EXPR_BINARY_LESS:
2051 case EXPR_BINARY_LESSEQUAL:
2052 case EXPR_BINARY_GREATER:
2053 case EXPR_BINARY_GREATEREQUAL:
2054 return handle_assume_compare(dbi, &expression->binary);
2060 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2062 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2063 type_t *type = skip_typeref(expression->base.type);
2065 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2066 return expression_to_addr(expression->value);
2068 const expression_t *value = expression->value;
2070 switch(expression->base.kind) {
2071 case EXPR_UNARY_NEGATE: {
2072 ir_node *value_node = expression_to_firm(value);
2073 ir_mode *mode = get_ir_mode(type);
2074 return new_d_Minus(dbgi, value_node, mode);
2076 case EXPR_UNARY_PLUS:
2077 return expression_to_firm(value);
2078 case EXPR_UNARY_BITWISE_NEGATE: {
2079 ir_node *value_node = expression_to_firm(value);
2080 ir_mode *mode = get_ir_mode(type);
2081 return new_d_Not(dbgi, value_node, mode);
2083 case EXPR_UNARY_NOT: {
2084 ir_node *value_node = _expression_to_firm(value);
2085 value_node = create_conv(dbgi, value_node, mode_b);
2086 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2089 case EXPR_UNARY_DEREFERENCE: {
2090 ir_node *value_node = expression_to_firm(value);
2091 type_t *value_type = skip_typeref(value->base.type);
2092 assert(is_type_pointer(value_type));
2093 type_t *points_to = value_type->pointer.points_to;
2094 return deref_address(dbgi, points_to, value_node);
2096 case EXPR_UNARY_POSTFIX_INCREMENT:
2097 case EXPR_UNARY_POSTFIX_DECREMENT:
2098 case EXPR_UNARY_PREFIX_INCREMENT:
2099 case EXPR_UNARY_PREFIX_DECREMENT:
2100 return create_incdec(expression);
2101 case EXPR_UNARY_CAST: {
2102 ir_node *value_node = expression_to_firm(value);
2103 if (is_type_scalar(type)) {
2104 ir_mode *mode = get_ir_mode(type);
2105 ir_node *node = create_conv(dbgi, value_node, mode);
2106 node = do_strict_conv(dbgi, node);
2109 /* make sure firm type is constructed */
2110 (void) get_ir_type(type);
2114 case EXPR_UNARY_CAST_IMPLICIT: {
2115 ir_node *value_node = expression_to_firm(value);
2116 if (is_type_scalar(type)) {
2117 ir_mode *mode = get_ir_mode(type);
2118 return create_conv(dbgi, value_node, mode);
2123 case EXPR_UNARY_ASSUME:
2124 if (firm_opt.confirm)
2125 return handle_assume(dbgi, value);
2132 panic("invalid UNEXPR type found");
2136 * produces a 0/1 depending of the value of a mode_b node
2138 static ir_node *produce_condition_result(const expression_t *expression,
2141 ir_mode *mode = get_ir_mode(expression->base.type);
2142 ir_node *cur_block = get_cur_block();
2144 ir_node *one_block = new_immBlock();
2145 ir_node *one = new_Const(mode, get_mode_one(mode));
2146 ir_node *jmp_one = new_d_Jmp(dbgi);
2148 ir_node *zero_block = new_immBlock();
2149 ir_node *zero = new_Const(mode, get_mode_null(mode));
2150 ir_node *jmp_zero = new_d_Jmp(dbgi);
2152 set_cur_block(cur_block);
2153 create_condition_evaluation(expression, one_block, zero_block);
2154 mature_immBlock(one_block);
2155 mature_immBlock(zero_block);
2157 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2158 new_Block(2, in_cf);
2160 ir_node *in[2] = { one, zero };
2161 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2166 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2167 ir_node *value, type_t *type)
2169 pointer_type_t *const pointer_type = &type->pointer;
2170 type_t *const points_to = skip_typeref(pointer_type->points_to);
2171 unsigned elem_size = get_type_size_const(points_to);
2173 /* gcc extension: allow arithmetic with void * and function * */
2174 if ((elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) ||
2175 is_type_function(points_to)) {
2179 assert(elem_size >= 1);
2183 value = create_conv(dbgi, value, mode_int);
2184 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2185 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2189 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2190 ir_node *left, ir_node *right)
2193 type_t *type_left = skip_typeref(expression->left->base.type);
2194 type_t *type_right = skip_typeref(expression->right->base.type);
2196 expression_kind_t kind = expression->base.kind;
2199 case EXPR_BINARY_SHIFTLEFT:
2200 case EXPR_BINARY_SHIFTRIGHT:
2201 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2202 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2203 mode = get_irn_mode(left);
2204 right = create_conv(dbgi, right, mode_uint);
2207 case EXPR_BINARY_SUB:
2208 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2209 const pointer_type_t *const ptr_type = &type_left->pointer;
2211 mode = get_ir_mode(expression->base.type);
2212 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2213 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2214 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2215 ir_node *const no_mem = new_NoMem();
2216 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2217 mode, op_pin_state_floats);
2218 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2221 case EXPR_BINARY_SUB_ASSIGN:
2222 if (is_type_pointer(type_left)) {
2223 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2224 mode = get_ir_mode(type_left);
2229 case EXPR_BINARY_ADD:
2230 case EXPR_BINARY_ADD_ASSIGN:
2231 if (is_type_pointer(type_left)) {
2232 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2233 mode = get_ir_mode(type_left);
2235 } else if (is_type_pointer(type_right)) {
2236 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2237 mode = get_ir_mode(type_right);
2244 mode = get_irn_mode(right);
2245 left = create_conv(dbgi, left, mode);
2250 case EXPR_BINARY_ADD_ASSIGN:
2251 case EXPR_BINARY_ADD:
2252 return new_d_Add(dbgi, left, right, mode);
2253 case EXPR_BINARY_SUB_ASSIGN:
2254 case EXPR_BINARY_SUB:
2255 return new_d_Sub(dbgi, left, right, mode);
2256 case EXPR_BINARY_MUL_ASSIGN:
2257 case EXPR_BINARY_MUL:
2258 return new_d_Mul(dbgi, left, right, mode);
2259 case EXPR_BINARY_BITWISE_AND:
2260 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2261 return new_d_And(dbgi, left, right, mode);
2262 case EXPR_BINARY_BITWISE_OR:
2263 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2264 return new_d_Or(dbgi, left, right, mode);
2265 case EXPR_BINARY_BITWISE_XOR:
2266 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2267 return new_d_Eor(dbgi, left, right, mode);
2268 case EXPR_BINARY_SHIFTLEFT:
2269 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2270 return new_d_Shl(dbgi, left, right, mode);
2271 case EXPR_BINARY_SHIFTRIGHT:
2272 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2273 if (mode_is_signed(mode)) {
2274 return new_d_Shrs(dbgi, left, right, mode);
2276 return new_d_Shr(dbgi, left, right, mode);
2278 case EXPR_BINARY_DIV:
2279 case EXPR_BINARY_DIV_ASSIGN: {
2280 ir_node *pin = new_Pin(new_NoMem());
2283 if (mode_is_float(mode)) {
2284 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2285 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2287 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2288 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2292 case EXPR_BINARY_MOD:
2293 case EXPR_BINARY_MOD_ASSIGN: {
2294 ir_node *pin = new_Pin(new_NoMem());
2295 assert(!mode_is_float(mode));
2296 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2297 op_pin_state_floats);
2298 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2302 panic("unexpected expression kind");
2306 static ir_node *create_lazy_op(const binary_expression_t *expression)
2308 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2309 type_t *type = expression->base.type;
2310 ir_mode *mode = get_ir_mode(type);
2312 if (is_constant_expression(expression->left)) {
2313 long val = fold_constant(expression->left);
2314 expression_kind_t ekind = expression->base.kind;
2315 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2316 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
2317 (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2318 return expression_to_firm(expression->right);
2320 return new_Const(mode, get_mode_one(mode));
2324 return produce_condition_result((const expression_t*) expression, dbgi);
2327 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2328 ir_node *right, ir_mode *mode);
2330 static ir_node *create_assign_binop(const binary_expression_t *expression)
2332 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2333 const expression_t *left_expr = expression->left;
2334 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2335 ir_node *right = expression_to_firm(expression->right);
2336 ir_node *left_addr = expression_to_addr(left_expr);
2337 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2338 ir_node *result = create_op(dbgi, expression, left, right);
2340 result = create_conv(dbgi, result, left_mode);
2341 result = do_strict_conv(dbgi, result);
2343 set_value_for_expression_addr(left_expr, result, left_addr);
2348 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2350 expression_kind_t kind = expression->base.kind;
2353 case EXPR_BINARY_EQUAL:
2354 case EXPR_BINARY_NOTEQUAL:
2355 case EXPR_BINARY_LESS:
2356 case EXPR_BINARY_LESSEQUAL:
2357 case EXPR_BINARY_GREATER:
2358 case EXPR_BINARY_GREATEREQUAL:
2359 case EXPR_BINARY_ISGREATER:
2360 case EXPR_BINARY_ISGREATEREQUAL:
2361 case EXPR_BINARY_ISLESS:
2362 case EXPR_BINARY_ISLESSEQUAL:
2363 case EXPR_BINARY_ISLESSGREATER:
2364 case EXPR_BINARY_ISUNORDERED: {
2365 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2366 ir_node *left = expression_to_firm(expression->left);
2367 ir_node *right = expression_to_firm(expression->right);
2368 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2369 long pnc = get_pnc(kind, expression->left->base.type);
2370 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2373 case EXPR_BINARY_ASSIGN: {
2374 ir_node *addr = expression_to_addr(expression->left);
2375 ir_node *right = expression_to_firm(expression->right);
2376 set_value_for_expression_addr(expression->left, right, addr);
2380 case EXPR_BINARY_ADD:
2381 case EXPR_BINARY_SUB:
2382 case EXPR_BINARY_MUL:
2383 case EXPR_BINARY_DIV:
2384 case EXPR_BINARY_MOD:
2385 case EXPR_BINARY_BITWISE_AND:
2386 case EXPR_BINARY_BITWISE_OR:
2387 case EXPR_BINARY_BITWISE_XOR:
2388 case EXPR_BINARY_SHIFTLEFT:
2389 case EXPR_BINARY_SHIFTRIGHT:
2391 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2392 ir_node *left = expression_to_firm(expression->left);
2393 ir_node *right = expression_to_firm(expression->right);
2394 return create_op(dbgi, expression, left, right);
2396 case EXPR_BINARY_LOGICAL_AND:
2397 case EXPR_BINARY_LOGICAL_OR:
2398 return create_lazy_op(expression);
2399 case EXPR_BINARY_COMMA:
2400 /* create side effects of left side */
2401 (void) expression_to_firm(expression->left);
2402 return _expression_to_firm(expression->right);
2404 case EXPR_BINARY_ADD_ASSIGN:
2405 case EXPR_BINARY_SUB_ASSIGN:
2406 case EXPR_BINARY_MUL_ASSIGN:
2407 case EXPR_BINARY_MOD_ASSIGN:
2408 case EXPR_BINARY_DIV_ASSIGN:
2409 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2410 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2411 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2412 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2413 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2414 return create_assign_binop(expression);
2415 case EXPR_BINARY_BUILTIN_EXPECT:
2416 return _expression_to_firm(expression->left);
2418 panic("TODO binexpr type");
2422 static ir_node *array_access_addr(const array_access_expression_t *expression)
2424 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2425 ir_node *base_addr = expression_to_firm(expression->array_ref);
2426 ir_node *offset = expression_to_firm(expression->index);
2428 type_t *offset_type = skip_typeref(expression->index->base.type);
2430 if (is_type_signed(offset_type)) {
2431 mode = get_ir_mode(type_ssize_t);
2433 mode = get_ir_mode(type_size_t);
2435 offset = create_conv(dbgi, offset, mode);
2437 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2438 assert(is_type_pointer(ref_type));
2439 pointer_type_t *pointer_type = &ref_type->pointer;
2441 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2442 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2443 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2445 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2450 static ir_node *array_access_to_firm(
2451 const array_access_expression_t *expression)
2453 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2454 ir_node *addr = array_access_addr(expression);
2455 type_t *type = revert_automatic_type_conversion(
2456 (const expression_t*) expression);
2457 type = skip_typeref(type);
2459 return deref_address(dbgi, type, addr);
2462 static long get_offsetof_offset(const offsetof_expression_t *expression)
2464 type_t *orig_type = expression->type;
2467 designator_t *designator = expression->designator;
2468 for( ; designator != NULL; designator = designator->next) {
2469 type_t *type = skip_typeref(orig_type);
2470 /* be sure the type is constructed */
2471 (void) get_ir_type(type);
2473 if (designator->symbol != NULL) {
2474 assert(is_type_compound(type));
2475 symbol_t *symbol = designator->symbol;
2477 declaration_t *declaration = type->compound.declaration;
2478 declaration_t *iter = declaration->scope.declarations;
2479 for( ; iter != NULL; iter = iter->next) {
2480 if (iter->symbol == symbol) {
2484 assert(iter != NULL);
2486 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2487 offset += get_entity_offset(iter->v.entity);
2489 orig_type = iter->type;
2491 expression_t *array_index = designator->array_index;
2492 assert(designator->array_index != NULL);
2493 assert(is_type_array(type));
2494 assert(is_type_valid(array_index->base.type));
2496 long index = fold_constant(array_index);
2497 ir_type *arr_type = get_ir_type(type);
2498 ir_type *elem_type = get_array_element_type(arr_type);
2499 long elem_size = get_type_size_bytes(elem_type);
2501 offset += index * elem_size;
2503 orig_type = type->array.element_type;
2510 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2512 ir_mode *mode = get_ir_mode(expression->base.type);
2513 long offset = get_offsetof_offset(expression);
2514 tarval *tv = new_tarval_from_long(offset, mode);
2515 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2517 return new_d_Const(dbgi, mode, tv);
2520 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2521 ir_entity *entity, type_t *type);
2523 static ir_node *compound_literal_to_firm(
2524 const compound_literal_expression_t *expression)
2526 type_t *type = expression->type;
2528 /* create an entity on the stack */
2529 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2531 ident *const id = id_unique("CompLit.%u");
2532 ir_type *const irtype = get_ir_type(type);
2533 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2534 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2535 set_entity_ld_ident(entity, id);
2537 set_entity_variability(entity, variability_uninitialized);
2539 /* create initialisation code */
2540 initializer_t *initializer = expression->initializer;
2541 create_local_initializer(initializer, dbgi, entity, type);
2543 /* create a sel for the compound literal address */
2544 ir_node *frame = get_local_frame(entity);
2545 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2550 * Transform a sizeof expression into Firm code.
2552 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2554 type_t *type = expression->type;
2556 type = expression->tp_expression->base.type;
2557 assert(type != NULL);
2560 type = skip_typeref(type);
2561 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2562 if (is_type_array(type) && type->array.is_vla
2563 && expression->tp_expression != NULL) {
2564 expression_to_firm(expression->tp_expression);
2567 return get_type_size(type);
2571 * Transform an alignof expression into Firm code.
2573 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2575 type_t *type = expression->type;
2577 /* beware: if expression is a variable reference, return the
2578 alignment of the variable. */
2579 const expression_t *tp_expression = expression->tp_expression;
2580 const declaration_t *declaration = expr_is_variable(tp_expression);
2581 if (declaration != NULL) {
2582 /* TODO: get the alignment of this variable. */
2584 type = tp_expression->base.type;
2585 assert(type != NULL);
2588 ir_mode *const mode = get_ir_mode(expression->base.type);
2589 symconst_symbol sym;
2590 sym.type_p = get_ir_type(type);
2591 return new_SymConst(mode, sym, symconst_type_align);
2594 static void init_ir_types(void);
2596 long fold_constant(const expression_t *expression)
2598 bool constant_folding_old = constant_folding;
2599 constant_folding = true;
2603 assert(is_constant_expression(expression));
2605 ir_graph *old_current_ir_graph = current_ir_graph;
2606 if (current_ir_graph == NULL) {
2607 current_ir_graph = get_const_code_irg();
2610 ir_node *cnst = expression_to_firm(expression);
2611 current_ir_graph = old_current_ir_graph;
2613 if (!is_Const(cnst)) {
2614 panic("couldn't fold constant\n");
2617 tarval *tv = get_Const_tarval(cnst);
2618 if (!tarval_is_long(tv)) {
2619 panic("result of constant folding is not integer\n");
2622 constant_folding = constant_folding_old;
2624 return get_tarval_long(tv);
2627 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2629 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2631 /* first try to fold a constant condition */
2632 if (is_constant_expression(expression->condition)) {
2633 long val = fold_constant(expression->condition);
2635 expression_t *true_expression = expression->true_expression;
2636 if (true_expression == NULL)
2637 true_expression = expression->condition;
2638 return expression_to_firm(true_expression);
2640 return expression_to_firm(expression->false_expression);
2644 ir_node *cur_block = get_cur_block();
2646 /* create the true block */
2647 ir_node *true_block = new_immBlock();
2649 ir_node *true_val = expression->true_expression != NULL ?
2650 expression_to_firm(expression->true_expression) : NULL;
2651 ir_node *true_jmp = new_Jmp();
2653 /* create the false block */
2654 ir_node *false_block = new_immBlock();
2656 ir_node *false_val = expression_to_firm(expression->false_expression);
2657 ir_node *false_jmp = new_Jmp();
2659 /* create the condition evaluation */
2660 set_cur_block(cur_block);
2661 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
2662 if (expression->true_expression == NULL) {
2663 if (cond_expr != NULL) {
2664 true_val = cond_expr;
2666 /* Condition ended with a short circuit (&&, ||, !) operation.
2667 * Generate a "1" as value for the true branch. */
2668 ir_mode *const mode = mode_Is;
2669 true_val = new_Const(mode, get_mode_one(mode));
2672 mature_immBlock(true_block);
2673 mature_immBlock(false_block);
2675 /* create the common block */
2676 ir_node *in_cf[2] = { true_jmp, false_jmp };
2677 new_Block(2, in_cf);
2679 /* TODO improve static semantics, so either both or no values are NULL */
2680 if (true_val == NULL || false_val == NULL)
2683 ir_node *in[2] = { true_val, false_val };
2684 ir_mode *mode = get_irn_mode(true_val);
2685 assert(get_irn_mode(false_val) == mode);
2686 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2692 * Returns an IR-node representing the address of a field.
2694 static ir_node *select_addr(const select_expression_t *expression)
2696 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2698 ir_node *compound_addr = expression_to_firm(expression->compound);
2700 /* make sure the type is constructed */
2701 type_t *type = skip_typeref(expression->compound->base.type);
2702 if (is_type_pointer(type)) {
2703 type = type->pointer.points_to;
2705 (void) get_ir_type(type);
2707 declaration_t *entry = expression->compound_entry;
2708 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2709 ir_entity *entity = entry->v.entity;
2711 assert(entity != NULL);
2713 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2718 static ir_node *select_to_firm(const select_expression_t *expression)
2720 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2721 ir_node *addr = select_addr(expression);
2722 type_t *type = revert_automatic_type_conversion(
2723 (const expression_t*) expression);
2724 type = skip_typeref(type);
2726 declaration_t *entry = expression->compound_entry;
2727 type_t *entry_type = skip_typeref(entry->type);
2729 if (entry_type->kind == TYPE_BITFIELD) {
2730 return bitfield_extract_to_firm(expression, addr);
2733 return deref_address(dbgi, type, addr);
2736 /* Values returned by __builtin_classify_type. */
2737 typedef enum gcc_type_class
2743 enumeral_type_class,
2746 reference_type_class,
2750 function_type_class,
2761 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2763 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2769 const atomic_type_t *const atomic_type = &type->atomic;
2770 switch (atomic_type->akind) {
2771 /* should not be reached */
2772 case ATOMIC_TYPE_INVALID:
2776 /* gcc cannot do that */
2777 case ATOMIC_TYPE_VOID:
2778 tc = void_type_class;
2781 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2782 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2783 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2784 case ATOMIC_TYPE_SHORT:
2785 case ATOMIC_TYPE_USHORT:
2786 case ATOMIC_TYPE_INT:
2787 case ATOMIC_TYPE_UINT:
2788 case ATOMIC_TYPE_LONG:
2789 case ATOMIC_TYPE_ULONG:
2790 case ATOMIC_TYPE_LONGLONG:
2791 case ATOMIC_TYPE_ULONGLONG:
2792 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2793 tc = integer_type_class;
2796 case ATOMIC_TYPE_FLOAT:
2797 case ATOMIC_TYPE_DOUBLE:
2798 case ATOMIC_TYPE_LONG_DOUBLE:
2799 tc = real_type_class;
2802 panic("Unexpected atomic type in classify_type_to_firm().");
2805 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2806 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2807 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2808 case TYPE_ARRAY: /* gcc handles this as pointer */
2809 case TYPE_FUNCTION: /* gcc handles this as pointer */
2810 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2811 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2812 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2814 /* gcc handles this as integer */
2815 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2818 /* typedef/typeof should be skipped already */
2825 panic("unexpected TYPE classify_type_to_firm().");
2828 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2829 ir_mode *const mode = mode_int;
2830 tarval *const tv = new_tarval_from_long(tc, mode);
2831 return new_d_Const(dbgi, mode, tv);
2834 static ir_node *function_name_to_firm(
2835 const funcname_expression_t *const expr)
2837 switch(expr->kind) {
2838 case FUNCNAME_FUNCTION:
2839 case FUNCNAME_PRETTY_FUNCTION:
2840 case FUNCNAME_FUNCDNAME:
2841 if (current_function_name == NULL) {
2842 const source_position_t *const src_pos = &expr->base.source_position;
2843 const char *const name = current_function_decl->symbol->string;
2844 const string_t string = { name, strlen(name) + 1 };
2845 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2847 return current_function_name;
2848 case FUNCNAME_FUNCSIG:
2849 if (current_funcsig == NULL) {
2850 const source_position_t *const src_pos = &expr->base.source_position;
2851 ir_entity *ent = get_irg_entity(current_ir_graph);
2852 const char *const name = get_entity_ld_name(ent);
2853 const string_t string = { name, strlen(name) + 1 };
2854 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2856 return current_funcsig;
2858 panic("Unsupported function name");
2861 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2863 statement_t *statement = expr->statement;
2865 assert(statement->kind == STATEMENT_COMPOUND);
2866 return compound_statement_to_firm(&statement->compound);
2869 static ir_node *va_start_expression_to_firm(
2870 const va_start_expression_t *const expr)
2872 ir_type *const method_type = get_ir_type(current_function_decl->type);
2873 int const n = get_method_n_params(method_type) - 1;
2874 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2875 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2876 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2877 ir_node *const no_mem = new_NoMem();
2878 ir_node *const arg_sel =
2879 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2881 ir_node *const cnst = get_type_size(expr->parameter->type);
2882 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2883 set_value_for_expression(expr->ap, add);
2888 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2890 type_t *const type = expr->base.type;
2891 expression_t *const ap_expr = expr->ap;
2892 ir_node *const ap_addr = expression_to_addr(ap_expr);
2893 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2894 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2895 ir_node *const res = deref_address(dbgi, type, ap);
2897 ir_node *const cnst = get_type_size(expr->base.type);
2898 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2900 set_value_for_expression_addr(ap_expr, add, ap_addr);
2905 static ir_node *dereference_addr(const unary_expression_t *const expression)
2907 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2908 return expression_to_firm(expression->value);
2912 * Returns a IR-node representing an lvalue of the given expression.
2914 static ir_node *expression_to_addr(const expression_t *expression)
2916 switch(expression->kind) {
2917 case EXPR_REFERENCE:
2918 return reference_addr(&expression->reference);
2919 case EXPR_ARRAY_ACCESS:
2920 return array_access_addr(&expression->array_access);
2922 return select_addr(&expression->select);
2924 return call_expression_to_firm(&expression->call);
2925 case EXPR_UNARY_DEREFERENCE: {
2926 return dereference_addr(&expression->unary);
2931 panic("trying to get address of non-lvalue");
2934 static ir_node *builtin_constant_to_firm(
2935 const builtin_constant_expression_t *expression)
2937 ir_mode *mode = get_ir_mode(expression->base.type);
2940 if (is_constant_expression(expression->value)) {
2945 return new_Const_long(mode, v);
2948 static ir_node *builtin_prefetch_to_firm(
2949 const builtin_prefetch_expression_t *expression)
2951 ir_node *adr = expression_to_firm(expression->adr);
2952 /* no Firm support for prefetch yet */
2957 static ir_node *get_label_block(declaration_t *label)
2959 assert(label->namespc == NAMESPACE_LABEL || label->namespc == NAMESPACE_LOCAL_LABEL);
2961 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
2962 return label->v.block;
2964 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
2966 /* beware: might be called from create initializer with current_ir_graph
2967 * set to const_code_irg. */
2968 ir_graph *rem = current_ir_graph;
2969 current_ir_graph = current_function;
2971 ir_node *old_cur_block = get_cur_block();
2972 ir_node *block = new_immBlock();
2973 set_cur_block(old_cur_block);
2975 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
2976 label->v.block = block;
2978 ARR_APP1(declaration_t *, all_labels, label);
2980 current_ir_graph = rem;
2985 * Pointer to a label. This is used for the
2986 * GNU address-of-label extension.
2988 static ir_node *label_address_to_firm(
2989 const label_address_expression_t *label)
2991 ir_node *block = get_label_block(label->declaration);
2992 ir_label_t nr = get_Block_label(block);
2995 nr = get_irp_next_label_nr();
2996 set_Block_label(block, nr);
2998 symconst_symbol value;
3000 return new_SymConst(mode_P_code, value, symconst_label);
3004 * creates firm nodes for an expression. The difference between this function
3005 * and expression_to_firm is, that this version might produce mode_b nodes
3006 * instead of mode_Is.
3008 static ir_node *_expression_to_firm(const expression_t *expression)
3011 if (!constant_folding) {
3012 assert(!expression->base.transformed);
3013 ((expression_t*) expression)->base.transformed = true;
3017 switch (expression->kind) {
3018 case EXPR_CHARACTER_CONSTANT:
3019 return character_constant_to_firm(&expression->conste);
3020 case EXPR_WIDE_CHARACTER_CONSTANT:
3021 return wide_character_constant_to_firm(&expression->conste);
3023 return const_to_firm(&expression->conste);
3024 case EXPR_STRING_LITERAL:
3025 return string_literal_to_firm(&expression->string);
3026 case EXPR_WIDE_STRING_LITERAL:
3027 return wide_string_literal_to_firm(&expression->wide_string);
3028 case EXPR_REFERENCE:
3029 return reference_expression_to_firm(&expression->reference);
3031 return call_expression_to_firm(&expression->call);
3033 return unary_expression_to_firm(&expression->unary);
3035 return binary_expression_to_firm(&expression->binary);
3036 case EXPR_ARRAY_ACCESS:
3037 return array_access_to_firm(&expression->array_access);
3039 return sizeof_to_firm(&expression->typeprop);
3041 return alignof_to_firm(&expression->typeprop);
3042 case EXPR_CONDITIONAL:
3043 return conditional_to_firm(&expression->conditional);
3045 return select_to_firm(&expression->select);
3046 case EXPR_CLASSIFY_TYPE:
3047 return classify_type_to_firm(&expression->classify_type);
3049 return function_name_to_firm(&expression->funcname);
3050 case EXPR_STATEMENT:
3051 return statement_expression_to_firm(&expression->statement);
3053 return va_start_expression_to_firm(&expression->va_starte);
3055 return va_arg_expression_to_firm(&expression->va_arge);
3056 case EXPR_BUILTIN_SYMBOL:
3057 panic("unimplemented expression found");
3058 case EXPR_BUILTIN_CONSTANT_P:
3059 return builtin_constant_to_firm(&expression->builtin_constant);
3060 case EXPR_BUILTIN_PREFETCH:
3061 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
3063 return offsetof_to_firm(&expression->offsetofe);
3064 case EXPR_COMPOUND_LITERAL:
3065 return compound_literal_to_firm(&expression->compound_literal);
3066 case EXPR_LABEL_ADDRESS:
3067 return label_address_to_firm(&expression->label_address);
3073 panic("invalid expression found");
3076 static bool produces_mode_b(const expression_t *expression)
3078 switch (expression->kind) {
3079 case EXPR_BINARY_EQUAL:
3080 case EXPR_BINARY_NOTEQUAL:
3081 case EXPR_BINARY_LESS:
3082 case EXPR_BINARY_LESSEQUAL:
3083 case EXPR_BINARY_GREATER:
3084 case EXPR_BINARY_GREATEREQUAL:
3085 case EXPR_BINARY_ISGREATER:
3086 case EXPR_BINARY_ISGREATEREQUAL:
3087 case EXPR_BINARY_ISLESS:
3088 case EXPR_BINARY_ISLESSEQUAL:
3089 case EXPR_BINARY_ISLESSGREATER:
3090 case EXPR_BINARY_ISUNORDERED:
3091 case EXPR_UNARY_NOT:
3094 case EXPR_BINARY_BUILTIN_EXPECT:
3095 return produces_mode_b(expression->binary.left);
3096 case EXPR_BINARY_COMMA:
3097 return produces_mode_b(expression->binary.right);
3104 static ir_node *expression_to_firm(const expression_t *expression)
3106 if (!produces_mode_b(expression)) {
3107 ir_node *res = _expression_to_firm(expression);
3108 assert(res == NULL || get_irn_mode(res) != mode_b);
3112 if (is_constant_expression(expression)) {
3113 ir_node *res = _expression_to_firm(expression);
3114 ir_mode *mode = get_ir_mode(expression->base.type);
3115 assert(is_Const(res));
3116 if (is_Const_null(res)) {
3117 return new_Const_long(mode, 0);
3119 return new_Const_long(mode, 1);
3123 /* we have to produce a 0/1 from the mode_b expression */
3124 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3125 return produce_condition_result(expression, dbgi);
3129 * create a short-circuit expression evaluation that tries to construct
3130 * efficient control flow structures for &&, || and ! expressions
3132 static ir_node *create_condition_evaluation(const expression_t *expression,
3133 ir_node *true_block,
3134 ir_node *false_block)
3136 switch(expression->kind) {
3137 case EXPR_UNARY_NOT: {
3138 const unary_expression_t *unary_expression = &expression->unary;
3139 create_condition_evaluation(unary_expression->value, false_block,
3143 case EXPR_BINARY_LOGICAL_AND: {
3144 const binary_expression_t *binary_expression = &expression->binary;
3146 ir_node *cur_block = get_cur_block();
3147 ir_node *extra_block = new_immBlock();
3148 set_cur_block(cur_block);
3149 create_condition_evaluation(binary_expression->left, extra_block,
3151 mature_immBlock(extra_block);
3152 set_cur_block(extra_block);
3153 create_condition_evaluation(binary_expression->right, true_block,
3157 case EXPR_BINARY_LOGICAL_OR: {
3158 const binary_expression_t *binary_expression = &expression->binary;
3160 ir_node *cur_block = get_cur_block();
3161 ir_node *extra_block = new_immBlock();
3162 set_cur_block(cur_block);
3163 create_condition_evaluation(binary_expression->left, true_block,
3165 mature_immBlock(extra_block);
3166 set_cur_block(extra_block);
3167 create_condition_evaluation(binary_expression->right, true_block,
3175 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3176 ir_node *cond_expr = _expression_to_firm(expression);
3177 ir_node *condition = create_conv(NULL, cond_expr, mode_b);
3178 ir_node *cond = new_d_Cond(dbgi, condition);
3179 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3180 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3182 /* set branch prediction info based on __builtin_expect */
3183 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3184 long cnst = fold_constant(expression->binary.right);
3185 cond_jmp_predicate pred;
3188 pred = COND_JMP_PRED_FALSE;
3190 pred = COND_JMP_PRED_TRUE;
3192 set_Cond_jmp_pred(cond, pred);
3195 add_immBlock_pred(true_block, true_proj);
3196 if (false_block != NULL) {
3197 add_immBlock_pred(false_block, false_proj);
3200 set_cur_block(NULL);
3206 static void create_declaration_entity(declaration_t *declaration,
3207 declaration_kind_t declaration_kind,
3208 ir_type *parent_type)
3210 type_t *const type = skip_typeref(declaration->type);
3211 if (is_type_function(type)) {
3212 (void) get_function_entity(declaration);
3216 ident *const id = new_id_from_str(declaration->symbol->string);
3217 ir_type *const irtype = get_ir_type(type);
3218 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3219 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3221 declaration->declaration_kind = (unsigned char) declaration_kind;
3222 declaration->v.entity = entity;
3223 set_entity_variability(entity, variability_uninitialized);
3224 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3225 if (parent_type == get_tls_type())
3226 set_entity_allocation(entity, allocation_automatic);
3227 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3228 set_entity_allocation(entity, allocation_static);
3230 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3231 set_entity_volatility(entity, volatility_is_volatile);
3236 typedef struct type_path_entry_t type_path_entry_t;
3237 struct type_path_entry_t {
3239 ir_initializer_t *initializer;
3241 declaration_t *compound_entry;
3244 typedef struct type_path_t type_path_t;
3245 struct type_path_t {
3246 type_path_entry_t *path;
3251 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3253 size_t len = ARR_LEN(path->path);
3255 for(size_t i = 0; i < len; ++i) {
3256 const type_path_entry_t *entry = & path->path[i];
3258 type_t *type = skip_typeref(entry->type);
3259 if (is_type_compound(type)) {
3260 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3261 } else if (is_type_array(type)) {
3262 fprintf(stderr, "[%zd]", entry->index);
3264 fprintf(stderr, "-INVALID-");
3267 fprintf(stderr, " (");
3268 print_type(path->top_type);
3269 fprintf(stderr, ")");
3272 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3274 size_t len = ARR_LEN(path->path);
3276 return & path->path[len-1];
3279 static type_path_entry_t *append_to_type_path(type_path_t *path)
3281 size_t len = ARR_LEN(path->path);
3282 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3284 type_path_entry_t *result = & path->path[len];
3285 memset(result, 0, sizeof(result[0]));
3289 static size_t get_compound_size(const compound_type_t *type)
3291 declaration_t *declaration = type->declaration;
3292 declaration_t *member = declaration->scope.declarations;
3294 for( ; member != NULL; member = member->next) {
3297 /* TODO: cache results? */
3302 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3304 type_t *orig_top_type = path->top_type;
3305 type_t *top_type = skip_typeref(orig_top_type);
3307 assert(is_type_compound(top_type) || is_type_array(top_type));
3309 if (ARR_LEN(path->path) == 0) {
3312 type_path_entry_t *top = get_type_path_top(path);
3313 ir_initializer_t *initializer = top->initializer;
3314 return get_initializer_compound_value(initializer, top->index);
3318 static void descend_into_subtype(type_path_t *path)
3320 type_t *orig_top_type = path->top_type;
3321 type_t *top_type = skip_typeref(orig_top_type);
3323 assert(is_type_compound(top_type) || is_type_array(top_type));
3325 ir_initializer_t *initializer = get_initializer_entry(path);
3327 type_path_entry_t *top = append_to_type_path(path);
3328 top->type = top_type;
3332 if (is_type_compound(top_type)) {
3333 declaration_t *declaration = top_type->compound.declaration;
3334 declaration_t *entry = declaration->scope.declarations;
3336 top->compound_entry = entry;
3338 len = get_compound_size(&top_type->compound);
3340 path->top_type = entry->type;
3342 assert(is_type_array(top_type));
3343 assert(top_type->array.size > 0);
3346 path->top_type = top_type->array.element_type;
3347 len = top_type->array.size;
3349 if (initializer == NULL
3350 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3351 initializer = create_initializer_compound(len);
3352 /* we have to set the entry at the 2nd latest path entry... */
3353 size_t path_len = ARR_LEN(path->path);
3354 assert(path_len >= 1);
3356 type_path_entry_t *entry = & path->path[path_len-2];
3357 ir_initializer_t *tinitializer = entry->initializer;
3358 set_initializer_compound_value(tinitializer, entry->index,
3362 top->initializer = initializer;
3365 static void ascend_from_subtype(type_path_t *path)
3367 type_path_entry_t *top = get_type_path_top(path);
3369 path->top_type = top->type;
3371 size_t len = ARR_LEN(path->path);
3372 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3375 static void walk_designator(type_path_t *path, const designator_t *designator)
3377 /* designators start at current object type */
3378 ARR_RESIZE(type_path_entry_t, path->path, 1);
3380 for( ; designator != NULL; designator = designator->next) {
3381 type_path_entry_t *top = get_type_path_top(path);
3382 type_t *orig_type = top->type;
3383 type_t *type = skip_typeref(orig_type);
3385 if (designator->symbol != NULL) {
3386 assert(is_type_compound(type));
3388 symbol_t *symbol = designator->symbol;
3390 declaration_t *declaration = type->compound.declaration;
3391 declaration_t *iter = declaration->scope.declarations;
3392 for( ; iter != NULL; iter = iter->next, ++index) {
3393 if (iter->symbol == symbol) {
3397 assert(iter != NULL);
3399 top->type = orig_type;
3400 top->compound_entry = iter;
3402 orig_type = iter->type;
3404 expression_t *array_index = designator->array_index;
3405 assert(designator->array_index != NULL);
3406 assert(is_type_array(type));
3407 assert(is_type_valid(array_index->base.type));
3409 long index = fold_constant(array_index);
3412 if (type->array.size_constant == 1) {
3413 long array_size = type->array.size;
3414 assert(index < array_size);
3418 top->type = orig_type;
3419 top->index = (size_t) index;
3420 orig_type = type->array.element_type;
3422 path->top_type = orig_type;
3424 if (designator->next != NULL) {
3425 descend_into_subtype(path);
3429 path->invalid = false;
3432 static void advance_current_object(type_path_t *path)
3434 if (path->invalid) {
3435 /* TODO: handle this... */
3436 panic("invalid initializer in ast2firm (excessive elements)");
3440 type_path_entry_t *top = get_type_path_top(path);
3442 type_t *type = skip_typeref(top->type);
3443 if (is_type_union(type)) {
3444 top->compound_entry = NULL;
3445 } else if (is_type_struct(type)) {
3446 declaration_t *entry = top->compound_entry;
3449 entry = entry->next;
3450 top->compound_entry = entry;
3451 if (entry != NULL) {
3452 path->top_type = entry->type;
3456 assert(is_type_array(type));
3459 if (!type->array.size_constant || top->index < type->array.size) {
3464 /* we're past the last member of the current sub-aggregate, try if we
3465 * can ascend in the type hierarchy and continue with another subobject */
3466 size_t len = ARR_LEN(path->path);
3469 ascend_from_subtype(path);
3470 advance_current_object(path);
3472 path->invalid = true;
3477 static ir_initializer_t *create_ir_initializer(
3478 const initializer_t *initializer, type_t *type);
3480 static ir_initializer_t *create_ir_initializer_value(
3481 const initializer_value_t *initializer)
3483 if (is_type_compound(initializer->value->base.type)) {
3484 panic("initializer creation for compounds not implemented yet");
3486 ir_node *value = expression_to_firm(initializer->value);
3487 return create_initializer_const(value);
3490 static ir_initializer_t *create_ir_initializer_list(
3491 const initializer_list_t *initializer, type_t *type)
3494 memset(&path, 0, sizeof(path));
3495 path.top_type = type;
3496 path.path = NEW_ARR_F(type_path_entry_t, 0);
3498 descend_into_subtype(&path);
3500 for(size_t i = 0; i < initializer->len; ++i) {
3501 const initializer_t *sub_initializer = initializer->initializers[i];
3503 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3504 walk_designator(&path, sub_initializer->designator.designator);
3508 if (sub_initializer->kind == INITIALIZER_VALUE) {
3509 /* we might have to descend into types until we're at a scalar
3512 type_t *orig_top_type = path.top_type;
3513 type_t *top_type = skip_typeref(orig_top_type);
3515 if (is_type_scalar(top_type))
3517 descend_into_subtype(&path);
3521 ir_initializer_t *sub_irinitializer
3522 = create_ir_initializer(sub_initializer, path.top_type);
3524 size_t path_len = ARR_LEN(path.path);
3525 assert(path_len >= 1);
3526 type_path_entry_t *entry = & path.path[path_len-1];
3527 ir_initializer_t *tinitializer = entry->initializer;
3528 set_initializer_compound_value(tinitializer, entry->index,
3531 advance_current_object(&path);
3534 assert(ARR_LEN(path.path) >= 1);
3535 ir_initializer_t *result = path.path[0].initializer;
3536 DEL_ARR_F(path.path);
3541 static ir_initializer_t *create_ir_initializer_string(
3542 const initializer_string_t *initializer, type_t *type)
3544 type = skip_typeref(type);
3546 size_t string_len = initializer->string.size;
3547 assert(type->kind == TYPE_ARRAY);
3548 assert(type->array.size_constant);
3549 size_t len = type->array.size;
3550 ir_initializer_t *irinitializer = create_initializer_compound(len);
3552 const char *string = initializer->string.begin;
3553 ir_mode *mode = get_ir_mode(type->array.element_type);
3555 for(size_t i = 0; i < len; ++i) {
3560 tarval *tv = new_tarval_from_long(c, mode);
3561 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3563 set_initializer_compound_value(irinitializer, i, char_initializer);
3566 return irinitializer;
3569 static ir_initializer_t *create_ir_initializer_wide_string(
3570 const initializer_wide_string_t *initializer, type_t *type)
3572 size_t string_len = initializer->string.size;
3573 assert(type->kind == TYPE_ARRAY);
3574 assert(type->array.size_constant);
3575 size_t len = type->array.size;
3576 ir_initializer_t *irinitializer = create_initializer_compound(len);
3578 const wchar_rep_t *string = initializer->string.begin;
3579 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3581 for(size_t i = 0; i < len; ++i) {
3583 if (i < string_len) {
3586 tarval *tv = new_tarval_from_long(c, mode);
3587 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3589 set_initializer_compound_value(irinitializer, i, char_initializer);
3592 return irinitializer;
3595 static ir_initializer_t *create_ir_initializer(
3596 const initializer_t *initializer, type_t *type)
3598 switch(initializer->kind) {
3599 case INITIALIZER_STRING:
3600 return create_ir_initializer_string(&initializer->string, type);
3602 case INITIALIZER_WIDE_STRING:
3603 return create_ir_initializer_wide_string(&initializer->wide_string,
3606 case INITIALIZER_LIST:
3607 return create_ir_initializer_list(&initializer->list, type);
3609 case INITIALIZER_VALUE:
3610 return create_ir_initializer_value(&initializer->value);
3612 case INITIALIZER_DESIGNATOR:
3613 panic("unexpected designator initializer found");
3615 panic("unknown initializer");
3618 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3621 if (is_atomic_type(type)) {
3622 ir_mode *mode = get_type_mode(type);
3623 tarval *zero = get_mode_null(mode);
3624 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3626 /* TODO: bitfields */
3627 ir_node *mem = get_store();
3628 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3629 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3632 assert(is_compound_type(type));
3635 if (is_Array_type(type)) {
3636 assert(has_array_upper_bound(type, 0));
3637 n_members = get_array_upper_bound_int(type, 0);
3639 n_members = get_compound_n_members(type);
3642 for(int i = 0; i < n_members; ++i) {
3645 if (is_Array_type(type)) {
3646 ir_entity *entity = get_array_element_entity(type);
3647 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3648 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3649 ir_node *in[1] = { cnst };
3650 irtype = get_array_element_type(type);
3651 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3653 ir_entity *member = get_compound_member(type, i);
3655 irtype = get_entity_type(member);
3656 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3659 create_dynamic_null_initializer(irtype, dbgi, addr);
3664 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3665 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3667 switch(get_initializer_kind(initializer)) {
3668 case IR_INITIALIZER_NULL: {
3669 create_dynamic_null_initializer(type, dbgi, base_addr);
3672 case IR_INITIALIZER_CONST: {
3673 ir_node *node = get_initializer_const_value(initializer);
3674 ir_mode *mode = get_irn_mode(node);
3675 assert(get_type_mode(type) == mode);
3677 /* TODO: bitfields... */
3678 ir_node *mem = get_store();
3679 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3680 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3684 case IR_INITIALIZER_TARVAL: {
3685 tarval *tv = get_initializer_tarval_value(initializer);
3686 ir_mode *mode = get_tarval_mode(tv);
3687 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3688 assert(get_type_mode(type) == mode);
3690 /* TODO: bitfields... */
3691 ir_node *mem = get_store();
3692 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3693 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3697 case IR_INITIALIZER_COMPOUND: {
3698 assert(is_compound_type(type));
3700 if (is_Array_type(type)) {
3701 assert(has_array_upper_bound(type, 0));
3702 n_members = get_array_upper_bound_int(type, 0);
3704 n_members = get_compound_n_members(type);
3707 if (get_initializer_compound_n_entries(initializer)
3708 != (unsigned) n_members)
3709 panic("initializer doesn't match compound type");
3711 for(int i = 0; i < n_members; ++i) {
3714 if (is_Array_type(type)) {
3715 ir_entity *entity = get_array_element_entity(type);
3716 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3717 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3718 ir_node *in[1] = { cnst };
3719 irtype = get_array_element_type(type);
3720 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3722 ir_entity *member = get_compound_member(type, i);
3724 irtype = get_entity_type(member);
3725 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3728 ir_initializer_t *sub_init
3729 = get_initializer_compound_value(initializer, i);
3731 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3737 panic("invalid IR_INITIALIZER found");
3740 static void create_dynamic_initializer(ir_initializer_t *initializer,
3741 dbg_info *dbgi, ir_entity *entity)
3743 ir_node *frame = get_local_frame(entity);
3744 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3745 ir_type *type = get_entity_type(entity);
3747 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3750 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3751 ir_entity *entity, type_t *type)
3753 ir_node *memory = get_store();
3754 ir_node *nomem = new_NoMem();
3755 ir_node *frame = get_irg_frame(current_ir_graph);
3756 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3758 if (initializer->kind == INITIALIZER_VALUE) {
3759 initializer_value_t *initializer_value = &initializer->value;
3761 ir_node *value = expression_to_firm(initializer_value->value);
3762 type = skip_typeref(type);
3763 assign_value(dbgi, addr, type, value);
3767 if (!is_constant_initializer(initializer)) {
3768 ir_initializer_t *irinitializer
3769 = create_ir_initializer(initializer, type);
3771 create_dynamic_initializer(irinitializer, dbgi, entity);
3775 /* create the ir_initializer */
3776 ir_graph *const old_current_ir_graph = current_ir_graph;
3777 current_ir_graph = get_const_code_irg();
3779 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3781 assert(current_ir_graph == get_const_code_irg());
3782 current_ir_graph = old_current_ir_graph;
3784 /* create a "template" entity which is copied to the entity on the stack */
3785 ident *const id = id_unique("initializer.%u");
3786 ir_type *const irtype = get_ir_type(type);
3787 ir_type *const global_type = get_glob_type();
3788 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3789 set_entity_ld_ident(init_entity, id);
3791 set_entity_variability(init_entity, variability_initialized);
3792 set_entity_visibility(init_entity, visibility_local);
3793 set_entity_allocation(init_entity, allocation_static);
3795 set_entity_initializer(init_entity, irinitializer);
3797 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3798 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3800 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3801 set_store(copyb_mem);
3804 static void create_initializer_local_variable_entity(declaration_t *declaration)
3806 initializer_t *initializer = declaration->init.initializer;
3807 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3808 ir_entity *entity = declaration->v.entity;
3809 type_t *type = declaration->type;
3810 create_local_initializer(initializer, dbgi, entity, type);
3813 static void create_declaration_initializer(declaration_t *declaration)
3815 initializer_t *initializer = declaration->init.initializer;
3816 if (initializer == NULL)
3819 declaration_kind_t declaration_kind
3820 = (declaration_kind_t) declaration->declaration_kind;
3821 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3822 create_initializer_local_variable_entity(declaration);
3826 type_t *type = skip_typeref(declaration->type);
3828 if (initializer->kind == INITIALIZER_VALUE) {
3829 initializer_value_t *initializer_value = &initializer->value;
3831 = get_dbg_info(&declaration->source_position);
3833 ir_node *value = expression_to_firm(initializer_value->value);
3834 value = do_strict_conv(dbgi, value);
3836 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3837 set_value(declaration->v.value_number, value);
3839 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3841 ir_entity *entity = declaration->v.entity;
3843 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3844 set_entity_variability(entity, variability_constant);
3846 set_entity_variability(entity, variability_initialized);
3848 set_atomic_ent_value(entity, value);
3851 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3852 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3854 ir_entity *entity = declaration->v.entity;
3855 ir_initializer_t *irinitializer
3856 = create_ir_initializer(initializer, type);
3858 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3859 set_entity_variability(entity, variability_constant);
3861 set_entity_variability(entity, variability_initialized);
3863 set_entity_initializer(entity, irinitializer);
3867 static void create_variable_length_array(declaration_t *declaration)
3869 /* initializers are not allowed for VLAs */
3870 assert(declaration->init.initializer == NULL);
3872 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3873 declaration->v.vla_base = NULL;
3875 /* TODO: record VLA somewhere so we create the free node when we leave
3879 static void allocate_variable_length_array(declaration_t *declaration)
3881 /* initializers are not allowed for VLAs */
3882 assert(declaration->init.initializer == NULL);
3883 assert(get_cur_block() != NULL);
3885 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3886 type_t *type = declaration->type;
3887 ir_type *el_type = get_ir_type(type->array.element_type);
3889 /* make sure size_node is calculated */
3890 get_type_size(type);
3891 ir_node *elems = type->array.size_node;
3892 ir_node *mem = get_store();
3893 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3895 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3896 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3899 assert(declaration->declaration_kind
3900 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3901 declaration->v.vla_base = addr;
3905 * Creates a Firm local variable from a declaration.
3907 static void create_local_variable(declaration_t *declaration)
3909 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3911 bool needs_entity = declaration->address_taken;
3912 type_t *type = skip_typeref(declaration->type);
3914 /* is it a variable length array? */
3915 if (is_type_array(type) && !type->array.size_constant) {
3916 create_variable_length_array(declaration);
3918 } else if (is_type_array(type) || is_type_compound(type)) {
3919 needs_entity = true;
3920 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3921 needs_entity = true;
3925 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3926 create_declaration_entity(declaration,
3927 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3930 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3931 declaration->v.value_number = next_value_number_function;
3932 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3933 ++next_value_number_function;
3937 static void create_local_static_variable(declaration_t *declaration)
3939 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3941 type_t *const type = skip_typeref(declaration->type);
3942 ir_type *const global_type = get_glob_type();
3943 ir_type *const irtype = get_ir_type(type);
3944 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3946 size_t l = strlen(declaration->symbol->string);
3947 char buf[l + sizeof(".%u")];
3948 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3949 ident *const id = id_unique(buf);
3951 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3953 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3954 set_entity_volatility(entity, volatility_is_volatile);
3957 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3958 declaration->v.entity = entity;
3959 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3960 set_entity_variability(entity, variability_uninitialized);
3961 set_entity_visibility(entity, visibility_local);
3962 set_entity_allocation(entity, allocation_static);
3964 ir_graph *const old_current_ir_graph = current_ir_graph;
3965 current_ir_graph = get_const_code_irg();
3967 create_declaration_initializer(declaration);
3969 assert(current_ir_graph == get_const_code_irg());
3970 current_ir_graph = old_current_ir_graph;
3975 static void return_statement_to_firm(return_statement_t *statement)
3977 if (get_cur_block() == NULL)
3980 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3981 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3986 if (get_method_n_ress(func_irtype) > 0) {
3987 ir_type *res_type = get_method_res_type(func_irtype, 0);
3989 if (statement->value != NULL) {
3990 ir_node *node = expression_to_firm(statement->value);
3991 node = do_strict_conv(dbgi, node);
3995 if (is_compound_type(res_type)) {
3998 mode = get_type_mode(res_type);
4000 in[0] = new_Unknown(mode);
4004 /* build return_value for its side effects */
4005 if (statement->value != NULL) {
4006 expression_to_firm(statement->value);
4011 ir_node *store = get_store();
4012 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4014 ir_node *end_block = get_irg_end_block(current_ir_graph);
4015 add_immBlock_pred(end_block, ret);
4017 set_cur_block(NULL);
4020 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4022 if (get_cur_block() == NULL)
4025 return expression_to_firm(statement->expression);
4028 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4030 declaration_t *declaration = compound->scope.declarations;
4031 for( ; declaration != NULL; declaration = declaration->next) {
4032 create_local_declaration(declaration);
4035 ir_node *result = NULL;
4036 statement_t *statement = compound->statements;
4037 for( ; statement != NULL; statement = statement->base.next) {
4038 if (statement->base.next == NULL
4039 && statement->kind == STATEMENT_EXPRESSION) {
4040 result = expression_statement_to_firm(
4041 &statement->expression);
4044 statement_to_firm(statement);
4050 static void create_global_variable(declaration_t *declaration)
4054 switch ((storage_class_tag_t)declaration->storage_class) {
4055 case STORAGE_CLASS_STATIC:
4056 vis = visibility_local;
4059 case STORAGE_CLASS_EXTERN:
4060 vis = visibility_external_allocated;
4063 case STORAGE_CLASS_NONE:
4064 vis = visibility_external_visible;
4067 case STORAGE_CLASS_THREAD:
4068 vis = visibility_external_visible;
4071 case STORAGE_CLASS_THREAD_EXTERN:
4072 vis = visibility_external_allocated;
4075 case STORAGE_CLASS_THREAD_STATIC:
4076 vis = visibility_local;
4080 var_type = get_tls_type();
4084 var_type = get_glob_type();
4088 create_declaration_entity(declaration,
4089 DECLARATION_KIND_GLOBAL_VARIABLE,
4091 if (!is_type_function(skip_typeref(declaration->type))) {
4092 set_entity_visibility(declaration->v.entity, vis);
4097 case STORAGE_CLASS_TYPEDEF:
4098 case STORAGE_CLASS_AUTO:
4099 case STORAGE_CLASS_REGISTER:
4100 case STORAGE_CLASS_ENUM_ENTRY:
4103 panic("Invalid storage class for global variable");
4106 static void create_local_declaration(declaration_t *declaration)
4108 if (declaration->namespc != NAMESPACE_NORMAL)
4110 /* construct type */
4111 (void) get_ir_type(declaration->type);
4112 if (declaration->symbol == NULL) {
4116 type_t *type = skip_typeref(declaration->type);
4118 switch ((storage_class_tag_t) declaration->storage_class) {
4119 case STORAGE_CLASS_STATIC:
4120 create_local_static_variable(declaration);
4122 case STORAGE_CLASS_EXTERN:
4123 create_global_variable(declaration);
4124 create_declaration_initializer(declaration);
4126 case STORAGE_CLASS_NONE:
4127 case STORAGE_CLASS_AUTO:
4128 case STORAGE_CLASS_REGISTER:
4129 if (is_type_function(type)) {
4130 if (declaration->init.statement != NULL) {
4131 panic("nested functions not supported yet");
4133 get_function_entity(declaration);
4136 create_local_variable(declaration);
4139 case STORAGE_CLASS_ENUM_ENTRY:
4140 /* should already be handled */
4141 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4143 case STORAGE_CLASS_TYPEDEF:
4144 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4146 case STORAGE_CLASS_THREAD:
4147 case STORAGE_CLASS_THREAD_EXTERN:
4148 case STORAGE_CLASS_THREAD_STATIC:
4151 panic("invalid storage class found");
4154 static void initialize_local_declaration(declaration_t *declaration)
4156 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4159 switch ((declaration_kind_t) declaration->declaration_kind) {
4160 case DECLARATION_KIND_LOCAL_VARIABLE:
4161 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4162 create_declaration_initializer(declaration);
4165 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4166 allocate_variable_length_array(declaration);
4169 case DECLARATION_KIND_LABEL_BLOCK:
4170 case DECLARATION_KIND_COMPOUND_MEMBER:
4171 case DECLARATION_KIND_GLOBAL_VARIABLE:
4172 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4173 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4174 case DECLARATION_KIND_FUNCTION:
4175 case DECLARATION_KIND_TYPE:
4176 case DECLARATION_KIND_ENUM_ENTRY:
4179 case DECLARATION_KIND_UNKNOWN:
4180 panic("can't initialize unknwon declaration");
4182 panic("invalid declaration kind");
4185 static void declaration_statement_to_firm(declaration_statement_t *statement)
4187 declaration_t *declaration = statement->declarations_begin;
4188 declaration_t *end = statement->declarations_end->next;
4189 for( ; declaration != end; declaration = declaration->next) {
4190 if (declaration->namespc != NAMESPACE_NORMAL)
4192 initialize_local_declaration(declaration);
4196 static void if_statement_to_firm(if_statement_t *statement)
4198 ir_node *cur_block = get_cur_block();
4200 ir_node *fallthrough_block = NULL;
4202 /* the true (blocks) */
4203 ir_node *true_block = NULL;
4204 if (statement->true_statement != NULL) {
4205 true_block = new_immBlock();
4206 statement_to_firm(statement->true_statement);
4207 if (get_cur_block() != NULL) {
4208 ir_node *jmp = new_Jmp();
4209 if (fallthrough_block == NULL)
4210 fallthrough_block = new_immBlock();
4211 add_immBlock_pred(fallthrough_block, jmp);
4215 /* the false (blocks) */
4216 ir_node *false_block = NULL;
4217 if (statement->false_statement != NULL) {
4218 false_block = new_immBlock();
4220 statement_to_firm(statement->false_statement);
4221 if (get_cur_block() != NULL) {
4222 ir_node *jmp = new_Jmp();
4223 if (fallthrough_block == NULL)
4224 fallthrough_block = new_immBlock();
4225 add_immBlock_pred(fallthrough_block, jmp);
4229 /* create the condition */
4230 if (cur_block != NULL) {
4231 if (true_block == NULL || false_block == NULL) {
4232 if (fallthrough_block == NULL)
4233 fallthrough_block = new_immBlock();
4234 if (true_block == NULL)
4235 true_block = fallthrough_block;
4236 if (false_block == NULL)
4237 false_block = fallthrough_block;
4240 set_cur_block(cur_block);
4241 create_condition_evaluation(statement->condition, true_block,
4245 mature_immBlock(true_block);
4246 if (false_block != fallthrough_block && false_block != NULL) {
4247 mature_immBlock(false_block);
4249 if (fallthrough_block != NULL) {
4250 mature_immBlock(fallthrough_block);
4253 set_cur_block(fallthrough_block);
4256 static void while_statement_to_firm(while_statement_t *statement)
4258 ir_node *jmp = NULL;
4259 if (get_cur_block() != NULL) {
4263 /* create the header block */
4264 ir_node *header_block = new_immBlock();
4266 add_immBlock_pred(header_block, jmp);
4270 ir_node *old_continue_label = continue_label;
4271 ir_node *old_break_label = break_label;
4272 continue_label = header_block;
4275 ir_node *body_block = new_immBlock();
4276 statement_to_firm(statement->body);
4277 ir_node *false_block = break_label;
4279 assert(continue_label == header_block);
4280 continue_label = old_continue_label;
4281 break_label = old_break_label;
4283 if (get_cur_block() != NULL) {
4285 add_immBlock_pred(header_block, jmp);
4288 /* shortcut for while(true) */
4289 if (is_constant_expression(statement->condition)
4290 && fold_constant(statement->condition) != 0) {
4291 set_cur_block(header_block);
4292 ir_node *header_jmp = new_Jmp();
4293 add_immBlock_pred(body_block, header_jmp);
4295 keep_alive(body_block);
4296 keep_all_memory(body_block);
4297 set_cur_block(body_block);
4299 if (false_block == NULL) {
4300 false_block = new_immBlock();
4303 /* create the condition */
4304 set_cur_block(header_block);
4306 create_condition_evaluation(statement->condition, body_block,
4310 mature_immBlock(body_block);
4311 mature_immBlock(header_block);
4312 if (false_block != NULL) {
4313 mature_immBlock(false_block);
4316 set_cur_block(false_block);
4319 static void do_while_statement_to_firm(do_while_statement_t *statement)
4321 ir_node *jmp = NULL;
4322 if (get_cur_block() != NULL) {
4326 /* create the header block */
4327 ir_node *header_block = new_immBlock();
4330 ir_node *body_block = new_immBlock();
4332 add_immBlock_pred(body_block, jmp);
4335 ir_node *old_continue_label = continue_label;
4336 ir_node *old_break_label = break_label;
4337 continue_label = header_block;
4340 statement_to_firm(statement->body);
4341 ir_node *false_block = break_label;
4343 assert(continue_label == header_block);
4344 continue_label = old_continue_label;
4345 break_label = old_break_label;
4347 if (get_cur_block() != NULL) {
4348 ir_node *body_jmp = new_Jmp();
4349 add_immBlock_pred(header_block, body_jmp);
4350 mature_immBlock(header_block);
4353 if (false_block == NULL) {
4354 false_block = new_immBlock();
4357 /* create the condition */
4358 set_cur_block(header_block);
4360 create_condition_evaluation(statement->condition, body_block, false_block);
4361 mature_immBlock(body_block);
4362 mature_immBlock(header_block);
4363 if (false_block != NULL) {
4364 mature_immBlock(false_block);
4367 set_cur_block(false_block);
4370 static void for_statement_to_firm(for_statement_t *statement)
4372 ir_node *jmp = NULL;
4374 /* create declarations */
4375 declaration_t *declaration = statement->scope.declarations;
4376 for( ; declaration != NULL; declaration = declaration->next) {
4377 create_local_declaration(declaration);
4379 declaration = statement->scope.declarations;
4380 for( ; declaration != NULL; declaration = declaration->next) {
4381 initialize_local_declaration(declaration);
4384 if (get_cur_block() != NULL) {
4385 if (statement->initialisation != NULL) {
4386 expression_to_firm(statement->initialisation);
4393 /* create the step block */
4394 ir_node *const step_block = new_immBlock();
4395 if (statement->step != NULL) {
4396 expression_to_firm(statement->step);
4398 ir_node *const step_jmp = new_Jmp();
4400 /* create the header block */
4401 ir_node *const header_block = new_immBlock();
4403 add_immBlock_pred(header_block, jmp);
4405 add_immBlock_pred(header_block, step_jmp);
4407 /* the false block */
4408 ir_node *const false_block = new_immBlock();
4411 ir_node * body_block;
4412 if (statement->body != NULL) {
4413 ir_node *const old_continue_label = continue_label;
4414 ir_node *const old_break_label = break_label;
4415 continue_label = step_block;
4416 break_label = false_block;
4418 body_block = new_immBlock();
4419 statement_to_firm(statement->body);
4421 assert(continue_label == step_block);
4422 assert(break_label == false_block);
4423 continue_label = old_continue_label;
4424 break_label = old_break_label;
4426 if (get_cur_block() != NULL) {
4428 add_immBlock_pred(step_block, jmp);
4431 body_block = step_block;
4434 /* create the condition */
4435 set_cur_block(header_block);
4436 if (statement->condition != NULL) {
4437 create_condition_evaluation(statement->condition, body_block,
4440 keep_alive(header_block);
4441 keep_all_memory(header_block);
4443 add_immBlock_pred(body_block, jmp);
4446 mature_immBlock(body_block);
4447 mature_immBlock(false_block);
4448 mature_immBlock(step_block);
4449 mature_immBlock(header_block);
4450 mature_immBlock(false_block);
4452 set_cur_block(false_block);
4455 static void create_jump_statement(const statement_t *statement,
4456 ir_node *target_block)
4458 if (get_cur_block() == NULL)
4461 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4462 ir_node *jump = new_d_Jmp(dbgi);
4463 add_immBlock_pred(target_block, jump);
4465 set_cur_block(NULL);
4468 static ir_node *get_break_label(void)
4470 if (break_label == NULL) {
4471 ir_node *cur_block = get_cur_block();
4472 break_label = new_immBlock();
4473 set_cur_block(cur_block);
4478 static void switch_statement_to_firm(switch_statement_t *statement)
4480 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4482 ir_node *expression = expression_to_firm(statement->expression);
4483 ir_node *cond = new_d_Cond(dbgi, expression);
4485 set_cur_block(NULL);
4487 ir_node *const old_switch_cond = current_switch_cond;
4488 ir_node *const old_break_label = break_label;
4489 const bool old_saw_default_label = saw_default_label;
4490 saw_default_label = false;
4491 current_switch_cond = cond;
4493 switch_statement_t *const old_switch = current_switch;
4494 current_switch = statement;
4496 /* determine a free number for the default label */
4497 unsigned long num_cases = 0;
4499 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4500 if (l->expression == NULL) {
4504 if (l->last_case >= l->first_case)
4505 num_cases += l->last_case - l->first_case;
4506 if (l->last_case > def_nr)
4507 def_nr = l->last_case;
4510 if (def_nr + 1 < 0) {
4511 /* Bad: an overflow occurred, we cannot be sure that the
4512 * maximum + 1 is a free number. Scan the values a second
4513 * time to find a free number.
4515 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
4518 memset(bits, 0, (num_cases + 7) >> 3);
4519 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4520 if (l->expression == NULL) {
4524 for (long cns = l->first_case; cns <= l->last_case; ++cns) {
4525 if (cns >= 0 && (unsigned long)cns < num_cases)
4526 bits[cns >> 3] |= (1 << (cns & 7));
4529 /* We look at the first num_cases constants:
4530 * Either they are densed, so we took the last (num_cases)
4531 * one, or they are non densed, so we will find one free
4534 for (i = 0; i < num_cases; ++i)
4535 if ((bits[i >> 3] & (i & 7)) == 0)
4543 statement->default_proj_nr = def_nr;
4545 if (statement->body != NULL) {
4546 statement_to_firm(statement->body);
4549 if (get_cur_block() != NULL) {
4550 ir_node *jmp = new_Jmp();
4551 add_immBlock_pred(get_break_label(), jmp);
4554 if (!saw_default_label) {
4555 set_cur_block(get_nodes_block(cond));
4556 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4557 statement->default_proj_nr);
4558 add_immBlock_pred(get_break_label(), proj);
4561 if (break_label != NULL) {
4562 mature_immBlock(break_label);
4564 set_cur_block(break_label);
4566 assert(current_switch_cond == cond);
4567 current_switch = old_switch;
4568 current_switch_cond = old_switch_cond;
4569 break_label = old_break_label;
4570 saw_default_label = old_saw_default_label;
4573 static void case_label_to_firm(const case_label_statement_t *statement)
4575 if (statement->is_empty)
4578 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4580 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4583 ir_node *old_block = get_nodes_block(current_switch_cond);
4584 ir_node *block = new_immBlock();
4586 set_cur_block(old_block);
4587 if (statement->expression != NULL) {
4588 long pn = statement->first_case;
4589 long end_pn = statement->last_case;
4590 assert(pn <= end_pn);
4591 /* create jumps for all cases in the given range */
4593 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4594 add_immBlock_pred(block, proj);
4595 } while(pn++ < end_pn);
4597 saw_default_label = true;
4598 proj = new_d_defaultProj(dbgi, current_switch_cond,
4599 current_switch->default_proj_nr);
4601 add_immBlock_pred(block, proj);
4604 if (fallthrough != NULL) {
4605 add_immBlock_pred(block, fallthrough);
4607 mature_immBlock(block);
4608 set_cur_block(block);
4610 if (statement->statement != NULL) {
4611 statement_to_firm(statement->statement);
4615 static void label_to_firm(const label_statement_t *statement)
4617 ir_node *block = get_label_block(statement->label);
4619 if (get_cur_block() != NULL) {
4620 ir_node *jmp = new_Jmp();
4621 add_immBlock_pred(block, jmp);
4624 set_cur_block(block);
4626 keep_all_memory(block);
4628 if (statement->statement != NULL) {
4629 statement_to_firm(statement->statement);
4633 static void goto_to_firm(const goto_statement_t *statement)
4635 if (get_cur_block() == NULL)
4638 if (statement->expression) {
4639 ir_node *irn = expression_to_firm(statement->expression);
4640 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4641 ir_node *ijmp = new_d_IJmp(dbgi, irn);
4643 set_irn_link(ijmp, ijmp_list);
4646 ir_node *block = get_label_block(statement->label);
4647 ir_node *jmp = new_Jmp();
4648 add_immBlock_pred(block, jmp);
4650 set_cur_block(NULL);
4653 static void asm_statement_to_firm(const asm_statement_t *statement)
4655 bool needs_memory = false;
4657 if (statement->is_volatile) {
4658 needs_memory = true;
4661 size_t n_clobbers = 0;
4662 asm_clobber_t *clobber = statement->clobbers;
4663 for( ; clobber != NULL; clobber = clobber->next) {
4664 const char *clobber_str = clobber->clobber.begin;
4666 if (!be_is_valid_clobber(clobber_str)) {
4667 errorf(&statement->base.source_position,
4668 "invalid clobber '%s' specified", clobber->clobber);
4672 if (strcmp(clobber_str, "memory") == 0) {
4673 needs_memory = true;
4677 ident *id = new_id_from_str(clobber_str);
4678 obstack_ptr_grow(&asm_obst, id);
4681 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4682 ident **clobbers = NULL;
4683 if (n_clobbers > 0) {
4684 clobbers = obstack_finish(&asm_obst);
4687 size_t n_inputs = 0;
4688 asm_argument_t *argument = statement->inputs;
4689 for ( ; argument != NULL; argument = argument->next)
4691 size_t n_outputs = 0;
4692 argument = statement->outputs;
4693 for ( ; argument != NULL; argument = argument->next)
4696 unsigned next_pos = 0;
4698 ir_node *ins[n_inputs + n_outputs + 1];
4701 ir_asm_constraint tmp_in_constraints[n_outputs];
4703 const expression_t *out_exprs[n_outputs];
4704 ir_node *out_addrs[n_outputs];
4705 size_t out_size = 0;
4707 argument = statement->outputs;
4708 for ( ; argument != NULL; argument = argument->next) {
4709 const char *constraints = argument->constraints.begin;
4710 asm_constraint_flags_t asm_flags
4711 = be_parse_asm_constraints(constraints);
4713 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4714 errorf(&statement->base.source_position,
4715 "some constraints in '%s' are not supported", constraints);
4718 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4719 errorf(&statement->base.source_position,
4720 "some constraints in '%s' are invalid", constraints);
4723 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4724 errorf(&statement->base.source_position,
4725 "no write flag specified for output constraints '%s'",
4730 unsigned pos = next_pos++;
4731 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4732 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4733 expression_t *expr = argument->expression;
4734 ir_node *addr = expression_to_addr(expr);
4735 /* in+output, construct an artifical same_as constraint on the
4737 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4739 ir_node *value = get_value_from_lvalue(expr, addr);
4741 snprintf(buf, sizeof(buf), "%d", pos);
4743 ir_asm_constraint constraint;
4744 constraint.pos = pos;
4745 constraint.constraint = new_id_from_str(buf);
4746 constraint.mode = get_ir_mode(expr->base.type);
4747 tmp_in_constraints[in_size] = constraint;
4748 ins[in_size] = value;
4753 out_exprs[out_size] = expr;
4754 out_addrs[out_size] = addr;
4756 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4757 /* pure memory ops need no input (but we have to make sure we
4758 * attach to the memory) */
4759 assert(! (asm_flags &
4760 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4761 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4762 needs_memory = true;
4764 /* we need to attach the address to the inputs */
4765 expression_t *expr = argument->expression;
4767 ir_asm_constraint constraint;
4768 constraint.pos = pos;
4769 constraint.constraint = new_id_from_str(constraints);
4770 constraint.mode = NULL;
4771 tmp_in_constraints[in_size] = constraint;
4773 ins[in_size] = expression_to_addr(expr);
4777 errorf(&statement->base.source_position,
4778 "only modifiers but no place set in constraints '%s'",
4783 ir_asm_constraint constraint;
4784 constraint.pos = pos;
4785 constraint.constraint = new_id_from_str(constraints);
4786 constraint.mode = get_ir_mode(argument->expression->base.type);
4788 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4790 assert(obstack_object_size(&asm_obst)
4791 == out_size * sizeof(ir_asm_constraint));
4792 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4795 obstack_grow(&asm_obst, tmp_in_constraints,
4796 in_size * sizeof(tmp_in_constraints[0]));
4797 /* find and count input and output arguments */
4798 argument = statement->inputs;
4799 for( ; argument != NULL; argument = argument->next) {
4800 const char *constraints = argument->constraints.begin;
4801 asm_constraint_flags_t asm_flags
4802 = be_parse_asm_constraints(constraints);
4804 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4805 errorf(&statement->base.source_position,
4806 "some constraints in '%s' are not supported", constraints);
4809 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4810 errorf(&statement->base.source_position,
4811 "some constraints in '%s' are invalid", constraints);
4814 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4815 errorf(&statement->base.source_position,
4816 "write flag specified for input constraints '%s'",
4822 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4823 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4824 /* we can treat this as "normal" input */
4825 input = expression_to_firm(argument->expression);
4826 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4827 /* pure memory ops need no input (but we have to make sure we
4828 * attach to the memory) */
4829 assert(! (asm_flags &
4830 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4831 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4832 needs_memory = true;
4833 input = expression_to_addr(argument->expression);
4835 errorf(&statement->base.source_position,
4836 "only modifiers but no place set in constraints '%s'",
4841 ir_asm_constraint constraint;
4842 constraint.pos = next_pos++;
4843 constraint.constraint = new_id_from_str(constraints);
4844 constraint.mode = get_irn_mode(input);
4846 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4847 ins[in_size++] = input;
4851 ir_asm_constraint constraint;
4852 constraint.pos = next_pos++;
4853 constraint.constraint = new_id_from_str("");
4854 constraint.mode = mode_M;
4856 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4857 ins[in_size++] = get_store();
4860 assert(obstack_object_size(&asm_obst)
4861 == in_size * sizeof(ir_asm_constraint));
4862 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4864 /* create asm node */
4865 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4867 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4869 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4870 out_size, output_constraints,
4871 n_clobbers, clobbers, asm_text);
4873 if (statement->is_volatile) {
4874 set_irn_pinned(node, op_pin_state_pinned);
4876 set_irn_pinned(node, op_pin_state_floats);
4879 /* create output projs & connect them */
4881 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4886 for (i = 0; i < out_size; ++i) {
4887 const expression_t *out_expr = out_exprs[i];
4889 ir_mode *mode = get_ir_mode(out_expr->base.type);
4890 ir_node *proj = new_Proj(node, mode, pn);
4891 ir_node *addr = out_addrs[i];
4893 set_value_for_expression_addr(out_expr, proj, addr);
4897 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4898 statement_to_firm(statement->try_statement);
4899 warningf(&statement->base.source_position, "structured exception handling ignored");
4902 static void leave_statement_to_firm(leave_statement_t *statement) {
4903 errorf(&statement->base.source_position, "__leave not supported yet");
4907 * Transform a statement.
4909 static void statement_to_firm(statement_t *statement)
4912 assert(!statement->base.transformed);
4913 statement->base.transformed = true;
4916 switch(statement->kind) {
4917 case STATEMENT_INVALID:
4918 panic("invalid statement found");
4920 case STATEMENT_EMPTY:
4923 case STATEMENT_COMPOUND:
4924 compound_statement_to_firm(&statement->compound);
4926 case STATEMENT_RETURN:
4927 return_statement_to_firm(&statement->returns);
4929 case STATEMENT_EXPRESSION:
4930 expression_statement_to_firm(&statement->expression);
4933 if_statement_to_firm(&statement->ifs);
4935 case STATEMENT_WHILE:
4936 while_statement_to_firm(&statement->whiles);
4938 case STATEMENT_DO_WHILE:
4939 do_while_statement_to_firm(&statement->do_while);
4941 case STATEMENT_DECLARATION:
4942 declaration_statement_to_firm(&statement->declaration);
4944 case STATEMENT_BREAK:
4945 create_jump_statement(statement, get_break_label());
4947 case STATEMENT_CONTINUE:
4948 create_jump_statement(statement, continue_label);
4950 case STATEMENT_SWITCH:
4951 switch_statement_to_firm(&statement->switchs);
4953 case STATEMENT_CASE_LABEL:
4954 case_label_to_firm(&statement->case_label);
4957 for_statement_to_firm(&statement->fors);
4959 case STATEMENT_LABEL:
4960 label_to_firm(&statement->label);
4962 case STATEMENT_GOTO:
4963 goto_to_firm(&statement->gotos);
4966 asm_statement_to_firm(&statement->asms);
4968 case STATEMENT_MS_TRY:
4969 ms_try_statement_to_firm(&statement->ms_try);
4971 case STATEMENT_LEAVE:
4972 leave_statement_to_firm(&statement->leave);
4975 panic("Statement not implemented\n");
4978 static int count_decls_in_expression(const expression_t *expression);
4980 static int count_local_declarations(const declaration_t * decl,
4981 const declaration_t *const end)
4984 for (; decl != end; decl = decl->next) {
4985 if (decl->namespc != NAMESPACE_NORMAL)
4987 type_t *type = skip_typeref(decl->type);
4989 if (!decl->address_taken && is_type_scalar(type))
4991 const initializer_t *initializer = decl->init.initializer;
4992 /* FIXME: should walk initializer hierarchies... */
4993 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4994 count += count_decls_in_expression(initializer->value.value);
5000 static int count_decls_in_expression(const expression_t *expression) {
5003 if (expression == NULL)
5006 switch((expression_kind_t) expression->base.kind) {
5007 case EXPR_STATEMENT:
5008 return count_decls_in_stmts(expression->statement.statement);
5010 int count_left = count_decls_in_expression(expression->binary.left);
5011 int count_right = count_decls_in_expression(expression->binary.right);
5012 return count_left + count_right;
5015 return count_decls_in_expression(expression->unary.value);
5017 call_argument_t *argument = expression->call.arguments;
5018 for( ; argument != NULL; argument = argument->next) {
5019 count += count_decls_in_expression(argument->expression);
5026 panic("unexpected expression kind");
5028 case EXPR_COMPOUND_LITERAL:
5032 case EXPR_CONDITIONAL:
5033 count += count_decls_in_expression(expression->conditional.condition);
5034 count += count_decls_in_expression(expression->conditional.true_expression);
5035 count += count_decls_in_expression(expression->conditional.false_expression);
5038 case EXPR_BUILTIN_PREFETCH:
5039 count += count_decls_in_expression(expression->builtin_prefetch.adr);
5040 count += count_decls_in_expression(expression->builtin_prefetch.rw);
5041 count += count_decls_in_expression(expression->builtin_prefetch.locality);
5044 case EXPR_BUILTIN_CONSTANT_P:
5045 count += count_decls_in_expression(expression->builtin_constant.value);
5049 count += count_decls_in_expression(expression->select.compound);
5052 case EXPR_ARRAY_ACCESS:
5053 count += count_decls_in_expression(expression->array_access.array_ref);
5054 count += count_decls_in_expression(expression->array_access.index);
5057 case EXPR_CLASSIFY_TYPE:
5058 count += count_decls_in_expression(expression->classify_type.type_expression);
5062 case EXPR_ALIGNOF: {
5063 expression_t *tp_expression = expression->typeprop.tp_expression;
5064 if (tp_expression != NULL) {
5065 count += count_decls_in_expression(tp_expression);
5071 case EXPR_REFERENCE:
5073 case EXPR_CHARACTER_CONSTANT:
5074 case EXPR_WIDE_CHARACTER_CONSTANT:
5075 case EXPR_STRING_LITERAL:
5076 case EXPR_WIDE_STRING_LITERAL:
5078 case EXPR_BUILTIN_SYMBOL:
5081 case EXPR_LABEL_ADDRESS:
5085 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
5086 * (or implement all the missing expressions here/implement a walker)
5092 static int count_decls_in_stmts(const statement_t *stmt)
5095 for (; stmt != NULL; stmt = stmt->base.next) {
5096 switch (stmt->kind) {
5097 case STATEMENT_EMPTY:
5100 case STATEMENT_DECLARATION: {
5101 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5102 count += count_local_declarations(decl_stmt->declarations_begin,
5103 decl_stmt->declarations_end->next);
5107 case STATEMENT_COMPOUND: {
5108 const compound_statement_t *const comp =
5110 count += count_decls_in_stmts(comp->statements);
5114 case STATEMENT_IF: {
5115 const if_statement_t *const if_stmt = &stmt->ifs;
5116 count += count_decls_in_expression(if_stmt->condition);
5117 count += count_decls_in_stmts(if_stmt->true_statement);
5118 count += count_decls_in_stmts(if_stmt->false_statement);
5122 case STATEMENT_SWITCH: {
5123 const switch_statement_t *const switch_stmt = &stmt->switchs;
5124 count += count_decls_in_expression(switch_stmt->expression);
5125 count += count_decls_in_stmts(switch_stmt->body);
5129 case STATEMENT_LABEL: {
5130 const label_statement_t *const label_stmt = &stmt->label;
5131 if (label_stmt->statement != NULL) {
5132 count += count_decls_in_stmts(label_stmt->statement);
5137 case STATEMENT_WHILE: {
5138 const while_statement_t *const while_stmt = &stmt->whiles;
5139 count += count_decls_in_expression(while_stmt->condition);
5140 count += count_decls_in_stmts(while_stmt->body);
5144 case STATEMENT_DO_WHILE: {
5145 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5146 count += count_decls_in_expression(do_while_stmt->condition);
5147 count += count_decls_in_stmts(do_while_stmt->body);
5151 case STATEMENT_FOR: {
5152 const for_statement_t *const for_stmt = &stmt->fors;
5153 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5154 count += count_decls_in_expression(for_stmt->initialisation);
5155 count += count_decls_in_expression(for_stmt->condition);
5156 count += count_decls_in_expression(for_stmt->step);
5157 count += count_decls_in_stmts(for_stmt->body);
5161 case STATEMENT_CASE_LABEL: {
5162 const case_label_statement_t *label = &stmt->case_label;
5163 count += count_decls_in_expression(label->expression);
5164 if (label->statement != NULL) {
5165 count += count_decls_in_stmts(label->statement);
5171 case STATEMENT_BREAK:
5172 case STATEMENT_CONTINUE:
5175 case STATEMENT_EXPRESSION: {
5176 const expression_statement_t *expr_stmt = &stmt->expression;
5177 count += count_decls_in_expression(expr_stmt->expression);
5181 case STATEMENT_GOTO:
5182 case STATEMENT_LEAVE:
5183 case STATEMENT_INVALID:
5186 case STATEMENT_RETURN: {
5187 const return_statement_t *ret_stmt = &stmt->returns;
5188 count += count_decls_in_expression(ret_stmt->value);
5192 case STATEMENT_MS_TRY: {
5193 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5194 count += count_decls_in_stmts(try_stmt->try_statement);
5195 if (try_stmt->except_expression != NULL)
5196 count += count_decls_in_expression(try_stmt->except_expression);
5197 count += count_decls_in_stmts(try_stmt->final_statement);
5205 static int get_function_n_local_vars(declaration_t *declaration)
5209 /* count parameters */
5210 count += count_local_declarations(declaration->scope.declarations, NULL);
5212 /* count local variables declared in body */
5213 count += count_decls_in_stmts(declaration->init.statement);
5218 static void initialize_function_parameters(declaration_t *declaration)
5220 ir_graph *irg = current_ir_graph;
5221 ir_node *args = get_irg_args(irg);
5222 ir_node *start_block = get_irg_start_block(irg);
5223 ir_type *function_irtype = get_ir_type(declaration->type);
5226 declaration_t *parameter = declaration->scope.declarations;
5227 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5228 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5229 type_t *type = skip_typeref(parameter->type);
5231 bool needs_entity = parameter->address_taken;
5232 assert(!is_type_array(type));
5233 if (is_type_compound(type)) {
5234 needs_entity = true;
5238 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5239 ident *id = new_id_from_str(parameter->symbol->string);
5240 set_entity_ident(entity, id);
5242 parameter->declaration_kind
5243 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5244 parameter->v.entity = entity;
5248 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5249 ir_mode *param_mode = get_type_mode(param_irtype);
5252 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5254 ir_mode *mode = get_ir_mode(type);
5255 value = create_conv(NULL, value, mode);
5256 value = do_strict_conv(NULL, value);
5258 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5259 parameter->v.value_number = next_value_number_function;
5260 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5261 ++next_value_number_function;
5263 set_value(parameter->v.value_number, value);
5268 * Handle additional decl modifiers for IR-graphs
5270 * @param irg the IR-graph
5271 * @param dec_modifiers additional modifiers
5273 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5275 if (decl_modifiers & DM_NORETURN) {
5276 /* TRUE if the declaration includes the Microsoft
5277 __declspec(noreturn) specifier. */
5278 set_irg_additional_property(irg, mtp_property_noreturn);
5280 if (decl_modifiers & DM_NOTHROW) {
5281 /* TRUE if the declaration includes the Microsoft
5282 __declspec(nothrow) specifier. */
5283 set_irg_additional_property(irg, mtp_property_nothrow);
5285 if (decl_modifiers & DM_NAKED) {
5286 /* TRUE if the declaration includes the Microsoft
5287 __declspec(naked) specifier. */
5288 set_irg_additional_property(irg, mtp_property_naked);
5290 if (decl_modifiers & DM_FORCEINLINE) {
5291 /* TRUE if the declaration includes the
5292 Microsoft __forceinline specifier. */
5293 set_irg_inline_property(irg, irg_inline_forced);
5295 if (decl_modifiers & DM_NOINLINE) {
5296 /* TRUE if the declaration includes the Microsoft
5297 __declspec(noinline) specifier. */
5298 set_irg_inline_property(irg, irg_inline_forbidden);
5302 static void add_function_pointer(ir_type *segment, ir_entity *method,
5303 const char *unique_template)
5305 ir_type *method_type = get_entity_type(method);
5306 ident *id = id_unique(unique_template);
5307 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5309 ident *ide = id_unique(unique_template);
5310 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5311 ir_graph *irg = get_const_code_irg();
5312 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5315 set_entity_compiler_generated(ptr, 1);
5316 set_entity_variability(ptr, variability_constant);
5317 set_atomic_ent_value(ptr, val);
5321 * Generate possible IJmp branches to a given label block.
5323 static void gen_ijmp_branches(ir_node *block) {
5325 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5326 add_immBlock_pred(block, ijmp);
5331 * Create code for a function.
5333 static void create_function(declaration_t *declaration)
5335 ir_entity *function_entity = get_function_entity(declaration);
5337 if (declaration->init.statement == NULL)
5340 if (declaration->modifiers & DM_CONSTRUCTOR) {
5341 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5342 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5344 if (declaration->modifiers & DM_DESTRUCTOR) {
5345 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5346 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5349 current_function_decl = declaration;
5350 current_function_name = NULL;
5351 current_funcsig = NULL;
5353 assert(all_labels == NULL);
5354 all_labels = NEW_ARR_F(declaration_t *, 0);
5357 int n_local_vars = get_function_n_local_vars(declaration);
5358 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5360 ir_graph *old_current_function = current_function;
5361 current_function = irg;
5363 set_irg_fp_model(irg, firm_opt.fp_model);
5364 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5365 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5367 ir_node *first_block = get_cur_block();
5369 /* set inline flags */
5370 if (declaration->is_inline)
5371 set_irg_inline_property(irg, irg_inline_recomended);
5372 handle_decl_modifier_irg(irg, declaration->modifiers);
5374 next_value_number_function = 0;
5375 initialize_function_parameters(declaration);
5377 statement_to_firm(declaration->init.statement);
5379 ir_node *end_block = get_irg_end_block(irg);
5381 /* do we have a return statement yet? */
5382 if (get_cur_block() != NULL) {
5383 type_t *type = skip_typeref(declaration->type);
5384 assert(is_type_function(type));
5385 const function_type_t *func_type = &type->function;
5386 const type_t *return_type
5387 = skip_typeref(func_type->return_type);
5390 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5391 ret = new_Return(get_store(), 0, NULL);
5394 if (is_type_scalar(return_type)) {
5395 mode = get_ir_mode(func_type->return_type);
5401 /* ยง5.1.2.2.3 main implicitly returns 0 */
5402 if (strcmp(declaration->symbol->string, "main") == 0) {
5403 in[0] = new_Const(mode, get_mode_null(mode));
5405 in[0] = new_Unknown(mode);
5407 ret = new_Return(get_store(), 1, in);
5409 add_immBlock_pred(end_block, ret);
5412 bool has_computed_gotos = false;
5413 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5414 declaration_t *label = all_labels[i];
5415 if (label->address_taken) {
5416 gen_ijmp_branches(label->v.block);
5417 has_computed_gotos = true;
5419 mature_immBlock(label->v.block);
5421 if (has_computed_gotos) {
5422 /* if we have computed goto's in the function, we cannot inline it */
5423 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5424 warningf(&declaration->source_position,
5425 "function '%Y' can never be inlined because it contains a computed goto",
5426 declaration->symbol);
5428 set_irg_inline_property(irg, irg_inline_forbidden);
5431 DEL_ARR_F(all_labels);
5434 mature_immBlock(first_block);
5435 mature_immBlock(end_block);
5437 irg_finalize_cons(irg);
5439 /* finalize the frame type */
5440 ir_type *frame_type = get_irg_frame_type(irg);
5441 int n = get_compound_n_members(frame_type);
5444 for(int i = 0; i < n; ++i) {
5445 ir_entity *entity = get_compound_member(frame_type, i);
5446 ir_type *entity_type = get_entity_type(entity);
5448 int align = get_type_alignment_bytes(entity_type);
5449 if (align > align_all)
5453 misalign = offset % align;
5455 offset += align - misalign;
5459 set_entity_offset(entity, offset);
5460 offset += get_type_size_bytes(entity_type);
5462 set_type_size_bytes(frame_type, offset);
5463 set_type_alignment_bytes(frame_type, align_all);
5466 current_function = old_current_function;
5469 static void scope_to_firm(scope_t *scope)
5471 /* first pass: create declarations */
5472 declaration_t *declaration = scope->declarations;
5473 for( ; declaration != NULL; declaration = declaration->next) {
5474 if (declaration->namespc != NAMESPACE_NORMAL)
5476 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5477 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5479 if (declaration->symbol == NULL)
5482 type_t *type = skip_typeref(declaration->type);
5483 if (is_type_function(type)) {
5484 get_function_entity(declaration);
5486 create_global_variable(declaration);
5490 /* second pass: create code/initializers */
5491 declaration = scope->declarations;
5492 for( ; declaration != NULL; declaration = declaration->next) {
5493 if (declaration->namespc != NAMESPACE_NORMAL)
5495 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5496 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5498 if (declaration->symbol == NULL)
5501 type_t *type = declaration->type;
5502 if (type->kind == TYPE_FUNCTION) {
5503 create_function(declaration);
5505 assert(declaration->declaration_kind
5506 == DECLARATION_KIND_GLOBAL_VARIABLE);
5507 current_ir_graph = get_const_code_irg();
5508 create_declaration_initializer(declaration);
5513 static void set_be_option(const char *option)
5515 int res = firm_be_option(option);
5519 void init_ast2firm(void)
5521 obstack_init(&asm_obst);
5522 init_atomic_modes();
5524 id_underscore = new_id_from_chars("_", 1);
5525 id_imp = new_id_from_chars("__imp_", 6);
5527 /* OS option must be set to the backend */
5528 switch (firm_opt.os_support) {
5529 case OS_SUPPORT_MINGW:
5530 create_ld_ident = create_ld_ident_win32;
5531 set_be_option("ia32-gasmode=mingw");
5533 case OS_SUPPORT_LINUX:
5534 create_ld_ident = create_ld_ident_linux_elf;
5535 set_be_option("ia32-gasmode=elf");
5537 case OS_SUPPORT_MACHO:
5538 create_ld_ident = create_ld_ident_macho;
5539 set_be_option("ia32-gasmode=macho");
5540 set_be_option("ia32-stackalign=4");
5541 set_be_option("pic");
5545 /* create idents for all known runtime functions */
5546 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5547 rts_idents[i] = new_id_from_str(rts_data[i].name);
5550 entitymap_init(&entitymap);
5553 static void init_ir_types(void)
5555 static int ir_types_initialized = 0;
5556 if (ir_types_initialized)
5558 ir_types_initialized = 1;
5560 ir_type_int = get_ir_type(type_int);
5561 ir_type_const_char = get_ir_type(type_const_char);
5562 ir_type_wchar_t = get_ir_type(type_wchar_t);
5563 ir_type_void = get_ir_type(type_void);
5566 void exit_ast2firm(void)
5568 entitymap_destroy(&entitymap);
5569 obstack_free(&asm_obst, NULL);
5572 static void global_asm_to_firm(statement_t *s)
5574 for (; s != NULL; s = s->base.next) {
5575 assert(s->kind == STATEMENT_ASM);
5577 char const *const text = s->asms.asm_text.begin;
5578 size_t size = s->asms.asm_text.size;
5580 /* skip the last \0 */
5581 if (text[size - 1] == '\0')
5584 ident *const id = new_id_from_chars(text, size);
5589 void translation_unit_to_firm(translation_unit_t *unit)
5591 /* just to be sure */
5592 continue_label = NULL;
5594 current_switch_cond = NULL;
5598 scope_to_firm(&unit->scope);
5599 global_asm_to_firm(unit->global_asm);
5601 current_ir_graph = NULL;