2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <libfirm/firm.h>
27 #include <libfirm/adt/obst.h>
31 #include "adt/error.h"
32 #include "adt/array.h"
38 #include "diagnostic.h"
39 #include "lang_features.h"
41 #include "driver/firm_opt.h"
42 #include "driver/firm_cmdline.h"
44 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
46 /* some idents needed for name mangling */
47 static ident *id_underscore;
50 static ir_type *ir_type_const_char;
51 static ir_type *ir_type_wchar_t;
52 static ir_type *ir_type_void;
53 static ir_type *ir_type_int;
55 static type_t *type_const_char;
57 static int next_value_number_function;
58 static ir_node *continue_label;
59 static ir_node *break_label;
60 static ir_node *current_switch_cond;
61 static bool saw_default_label;
62 static ir_node **imature_blocks;
64 static const declaration_t *current_function_decl;
65 static ir_node *current_function_name;
66 static ir_node *current_funcsig;
68 static struct obstack asm_obst;
70 typedef enum declaration_kind_t {
71 DECLARATION_KIND_UNKNOWN,
72 DECLARATION_KIND_FUNCTION,
73 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
74 DECLARATION_KIND_GLOBAL_VARIABLE,
75 DECLARATION_KIND_LOCAL_VARIABLE,
76 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
77 DECLARATION_KIND_COMPOUND_MEMBER,
78 DECLARATION_KIND_LABEL_BLOCK,
79 DECLARATION_KIND_ENUM_ENTRY,
80 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
81 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
84 static ir_type *get_ir_type(type_t *type);
85 static ir_type *get_ir_type_incomplete(type_t *type);
86 static int count_decls_in_stmts(const statement_t *stmt);
88 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
90 const declaration_t *declaration = get_irg_loc_description(irg, pos);
92 warningf(&declaration->source_position,
93 "variable '%#T' might be used uninitialized",
94 declaration->type, declaration->symbol);
95 return new_r_Unknown(irg, mode);
98 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
100 const source_position_t *pos = (const source_position_t*) dbg;
103 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
107 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
109 const source_position_t *pos = (const source_position_t*) dbg;
114 return pos->input_name;
117 static dbg_info *get_dbg_info(const source_position_t *pos)
119 return (dbg_info*) pos;
122 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
124 static ir_mode *mode_int, *mode_uint;
126 static ir_node *expression_to_firm(const expression_t *expression);
127 static inline ir_mode *get_ir_mode(type_t *type);
129 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
131 unsigned flags = get_atomic_type_flags(kind);
132 unsigned size = get_atomic_type_size(kind);
133 if( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
134 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
137 unsigned bit_size = size * 8;
138 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
139 ir_mode_arithmetic arithmetic;
140 unsigned modulo_shift;
142 if(flags & ATOMIC_TYPE_FLAG_INTEGER) {
143 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
144 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
145 sort = irms_int_number;
146 arithmetic = irma_twos_complement;
147 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
149 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
150 snprintf(name, sizeof(name), "f%d", bit_size);
151 sort = irms_float_number;
152 arithmetic = irma_ieee754;
155 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
163 * Initialises the atomic modes depending on the machine size.
165 static void init_atomic_modes(void)
167 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
168 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
170 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
171 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
173 /* there's no real void type in firm */
174 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
176 /* initialize pointer modes */
178 ir_mode_sort sort = irms_reference;
179 unsigned bit_size = machine_size;
181 ir_mode_arithmetic arithmetic = irma_twos_complement;
182 unsigned modulo_shift
183 = bit_size < machine_size ? machine_size : bit_size;
185 snprintf(name, sizeof(name), "p%d", machine_size);
186 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
189 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
190 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
192 /* Hmm, pointers should be machine size */
193 set_modeP_data(ptr_mode);
194 set_modeP_code(ptr_mode);
197 static unsigned get_compound_type_size(compound_type_t *type)
199 ir_type *irtype = get_ir_type((type_t*) type);
200 return get_type_size_bytes(irtype);
203 static unsigned get_array_type_size(array_type_t *type)
205 assert(!type->is_vla);
206 ir_type *irtype = get_ir_type((type_t*) type);
207 return get_type_size_bytes(irtype);
211 static unsigned get_type_size_const(type_t *type)
213 type = skip_typeref(type);
217 panic("error type occured");
219 return get_atomic_type_size(type->atomic.akind);
221 return 2 * get_atomic_type_size(type->complex.akind);
223 return get_atomic_type_size(type->imaginary.akind);
225 return get_mode_size_bytes(mode_int);
226 case TYPE_COMPOUND_UNION:
227 case TYPE_COMPOUND_STRUCT:
228 return get_compound_type_size(&type->compound);
230 /* just a pointer to the function */
231 return get_mode_size_bytes(mode_P_code);
233 return get_mode_size_bytes(mode_P_data);
235 return get_array_type_size(&type->array);
237 return get_type_size_const(type->builtin.real_type);
239 panic("type size of bitfield request");
245 panic("Trying to determine size of invalid type");
248 static ir_node *get_type_size(type_t *type)
250 type = skip_typeref(type);
252 if(is_type_array(type) && type->array.is_vla) {
253 ir_node *size_node = type->array.size_node;
254 if(size_node == NULL) {
255 size_node = expression_to_firm(type->array.size_expression);
256 assert(!is_Const(size_node));
257 type->array.size_node = size_node;
260 ir_node *elem_size = get_type_size(type->array.element_type);
261 ir_mode *mode = get_irn_mode(size_node);
262 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
266 ir_mode *mode = get_ir_mode(type_size_t);
268 sym.type_p = get_ir_type(type);
269 return new_SymConst(mode, sym, symconst_type_size);
272 static unsigned count_parameters(const function_type_t *function_type)
276 function_parameter_t *parameter = function_type->parameters;
277 for ( ; parameter != NULL; parameter = parameter->next) {
285 * Creates a Firm type for an atomic type
287 static ir_type *create_atomic_type(const atomic_type_t *type)
289 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
290 atomic_type_kind_t kind = type->akind;
291 ir_mode *mode = _atomic_modes[kind];
292 ident *id = get_mode_ident(mode);
293 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
295 set_type_alignment_bytes(irtype, type->base.alignment);
301 * Creates a Firm type for a complex type
303 static ir_type *create_complex_type(const complex_type_t *type)
305 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
306 atomic_type_kind_t kind = type->akind;
307 ir_mode *mode = _atomic_modes[kind];
308 ident *id = get_mode_ident(mode);
313 /* FIXME: finish the array */
318 * Creates a Firm type for an imaginary type
320 static ir_type *create_imaginary_type(const imaginary_type_t *type)
322 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
323 atomic_type_kind_t kind = type->akind;
324 ir_mode *mode = _atomic_modes[kind];
325 ident *id = get_mode_ident(mode);
326 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
328 set_type_alignment_bytes(irtype, type->base.alignment);
333 static ir_type *create_method_type(const function_type_t *function_type)
335 type_t *return_type = function_type->return_type;
337 ident *id = id_unique("functiontype.%u");
338 int n_parameters = count_parameters(function_type);
339 int n_results = return_type == type_void ? 0 : 1;
340 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
341 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
343 if(return_type != type_void) {
344 ir_type *restype = get_ir_type(return_type);
345 set_method_res_type(irtype, 0, restype);
348 function_parameter_t *parameter = function_type->parameters;
350 for( ; parameter != NULL; parameter = parameter->next) {
351 ir_type *p_irtype = get_ir_type(parameter->type);
352 set_method_param_type(irtype, n, p_irtype);
356 if(function_type->variadic || function_type->unspecified_parameters) {
357 set_method_variadicity(irtype, variadicity_variadic);
363 static ir_type *create_pointer_type(pointer_type_t *type)
365 type_t *points_to = type->points_to;
366 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
367 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
368 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
369 ir_points_to, mode_P_data, dbgi);
374 static ir_type *create_array_type(array_type_t *type)
376 type_t *element_type = type->element_type;
377 ir_type *ir_element_type = get_ir_type(element_type);
379 ident *id = id_unique("array.%u");
380 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
381 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
383 const int align = get_type_alignment_bytes(ir_element_type);
384 set_type_alignment_bytes(ir_type, align);
386 if(type->size_constant) {
387 int n_elements = type->size;
389 set_array_bounds_int(ir_type, 0, 0, n_elements);
391 size_t elemsize = get_type_size_bytes(ir_element_type);
392 if(elemsize % align > 0) {
393 elemsize += align - (elemsize % align);
395 set_type_size_bytes(ir_type, n_elements * elemsize);
397 set_array_lower_bound_int(ir_type, 0, 0);
399 set_type_state(ir_type, layout_fixed);
405 * Return the signed integer type of size bits.
407 * @param size the size
409 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
412 static ir_mode *s_modes[64 + 1] = {NULL, };
416 if (size <= 0 || size > 64)
419 mode = s_modes[size];
423 snprintf(name, sizeof(name), "bf_I%u", size);
424 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
425 size <= 32 ? 32 : size );
426 s_modes[size] = mode;
430 snprintf(name, sizeof(name), "I%u", size);
431 ident *id = new_id_from_str(name);
432 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
433 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
434 set_primitive_base_type(res, base_tp);
440 * Return the unsigned integer type of size bits.
442 * @param size the size
444 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
447 static ir_mode *u_modes[64 + 1] = {NULL, };
451 if (size <= 0 || size > 64)
454 mode = u_modes[size];
458 snprintf(name, sizeof(name), "bf_U%u", size);
459 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
460 size <= 32 ? 32 : size );
461 u_modes[size] = mode;
466 snprintf(name, sizeof(name), "U%u", size);
467 ident *id = new_id_from_str(name);
468 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
469 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
470 set_primitive_base_type(res, base_tp);
475 static ir_type *create_bitfield_type(bitfield_type_t *const type)
477 type_t *base = skip_typeref(type->base_type);
478 assert(base->kind == TYPE_ATOMIC);
479 ir_type *irbase = get_ir_type(base);
481 unsigned size = fold_constant(type->size);
483 assert(!is_type_float(base));
484 if(is_type_signed(base)) {
485 return get_signed_int_type_for_bit_size(irbase, size);
487 return get_unsigned_int_type_for_bit_size(irbase, size);
491 #define INVALID_TYPE ((ir_type_ptr)-1)
494 COMPOUND_IS_STRUCT = false,
495 COMPOUND_IS_UNION = true
499 * Construct firm type from ast struct tyep.
501 * As anonymous inner structs get flattened to a single firm type, we might get
502 * irtype, outer_offset and out_align passed (they represent the position of
503 * the anonymous inner struct inside the resulting firm struct)
505 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
506 size_t *outer_offset, size_t *outer_align,
507 bool incomplete, bool is_union)
509 declaration_t *declaration = type->declaration;
510 declaration_kind_t kind = declaration->declaration_kind;
512 if(kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
513 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
515 return declaration->v.irtype;
517 size_t align_all = 1;
519 size_t bit_offset = 0;
523 symbol_t *symbol = declaration->symbol;
526 id = new_id_from_str(symbol->string);
529 id = id_unique("__anonymous_union.%u");
531 id = id_unique("__anonymous_struct.%u");
534 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
537 irtype = new_d_type_union(id, dbgi);
539 irtype = new_d_type_struct(id, dbgi);
542 declaration->declaration_kind
543 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
544 declaration->v.irtype = irtype;
545 //type->base.firm_type = irtype;
547 offset = *outer_offset;
548 align_all = *outer_align;
554 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
556 declaration_t *entry = declaration->scope.declarations;
557 for( ; entry != NULL; entry = entry->next) {
558 if(entry->namespc != NAMESPACE_NORMAL)
561 size_t prev_offset = offset;
563 symbol_t *symbol = entry->symbol;
564 type_t *entry_type = skip_typeref(entry->type);
565 dbg_info *dbgi = get_dbg_info(&entry->source_position);
569 ident = new_id_from_str(symbol->string);
571 if(entry_type->kind == TYPE_COMPOUND_STRUCT) {
572 create_compound_type(&entry_type->compound, irtype, &offset,
573 &align_all, false, COMPOUND_IS_STRUCT);
574 goto finished_member;
575 } else if(entry_type->kind == TYPE_COMPOUND_UNION) {
576 create_compound_type(&entry_type->compound, irtype, &offset,
577 &align_all, false, COMPOUND_IS_UNION);
578 goto finished_member;
580 assert(entry_type->kind == TYPE_BITFIELD);
582 ident = id_unique("anon.%u");
585 ir_type *base_irtype;
586 if(entry_type->kind == TYPE_BITFIELD) {
587 base_irtype = get_ir_type(entry_type->bitfield.base_type);
589 base_irtype = get_ir_type(entry_type);
592 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
593 size_t misalign = offset % entry_alignment;
595 ir_type *entry_irtype = get_ir_type(entry_type);
596 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
599 size_t bits_remainder;
600 if(entry_type->kind == TYPE_BITFIELD) {
601 size_t size_bits = fold_constant(entry_type->bitfield.size);
602 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
604 if(size_bits > rest_size_bits) {
605 /* start a new bucket */
606 offset += entry_alignment - misalign;
612 /* put into current bucket */
613 base = offset - misalign;
614 bits_remainder = misalign * 8 + bit_offset;
617 offset += size_bits / 8;
618 bit_offset = bit_offset + (size_bits % 8);
620 size_t entry_size = get_type_size_bytes(base_irtype);
621 if(misalign > 0 || bit_offset > 0)
622 offset += entry_alignment - misalign;
626 offset += entry_size;
630 if(entry_alignment > align_all) {
631 if(entry_alignment % align_all != 0) {
632 panic("uneven alignments not supported yet");
634 align_all = entry_alignment;
637 set_entity_offset(entity, base);
638 set_entity_offset_bits_remainder(entity,
639 (unsigned char) bits_remainder);
640 //add_struct_member(irtype, entity);
641 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
642 assert(entry->v.entity == NULL);
643 entry->v.entity = entity;
647 size_t entry_size = offset - prev_offset;
648 if (entry_size > size) {
660 size_t misalign = offset % align_all;
661 if(misalign > 0 || bit_offset > 0) {
662 size += align_all - misalign;
665 if(outer_offset != NULL) {
667 *outer_offset = offset;
669 *outer_offset += size;
672 if (align_all > *outer_align) {
673 if(align_all % *outer_align != 0) {
674 panic("uneven alignments not supported yet");
676 *outer_align = align_all;
679 set_type_alignment_bytes(irtype, align_all);
680 set_type_size_bytes(irtype, size);
681 set_type_state(irtype, layout_fixed);
687 static ir_type *create_enum_type(enum_type_t *const type)
689 type->base.firm_type = ir_type_int;
691 ir_mode *const mode = mode_int;
692 tarval *const one = get_mode_one(mode);
693 tarval * tv_next = get_tarval_null(mode);
695 declaration_t *declaration = type->declaration->next;
696 for (; declaration != NULL; declaration = declaration->next) {
697 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
700 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
702 expression_t *const init = declaration->init.enum_value;
704 ir_node *const cnst = expression_to_firm(init);
705 if (!is_Const(cnst)) {
706 panic("couldn't fold constant");
708 tv_next = get_Const_tarval(cnst);
710 declaration->v.enum_val = tv_next;
711 tv_next = tarval_add(tv_next, one);
717 static ir_type *get_ir_type_incomplete(type_t *type)
719 assert(type != NULL);
720 type = skip_typeref(type);
722 if (type->base.firm_type != NULL) {
723 assert(type->base.firm_type != INVALID_TYPE);
724 return type->base.firm_type;
727 switch (type->kind) {
728 case TYPE_COMPOUND_STRUCT:
729 return create_compound_type(&type->compound, NULL, NULL, NULL,
730 true, COMPOUND_IS_STRUCT);
732 case TYPE_COMPOUND_UNION:
733 return create_compound_type(&type->compound, NULL, NULL, NULL,
734 true, COMPOUND_IS_UNION);
736 return get_ir_type(type);
740 static ir_type *get_ir_type(type_t *type)
742 assert(type != NULL);
744 type = skip_typeref(type);
746 if (type->base.firm_type != NULL) {
747 assert(type->base.firm_type != INVALID_TYPE);
748 return type->base.firm_type;
751 ir_type *firm_type = NULL;
752 switch (type->kind) {
754 panic("error type occurred");
756 firm_type = create_atomic_type(&type->atomic);
759 firm_type = create_complex_type(&type->complex);
762 firm_type = create_imaginary_type(&type->imaginary);
765 firm_type = create_method_type(&type->function);
768 firm_type = create_pointer_type(&type->pointer);
771 firm_type = create_array_type(&type->array);
773 case TYPE_COMPOUND_STRUCT:
774 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
775 false, COMPOUND_IS_STRUCT);
777 case TYPE_COMPOUND_UNION:
778 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
779 false, COMPOUND_IS_UNION);
782 firm_type = create_enum_type(&type->enumt);
785 firm_type = get_ir_type(type->builtin.real_type);
788 firm_type = create_bitfield_type(&type->bitfield);
796 if(firm_type == NULL)
797 panic("unknown type found");
799 type->base.firm_type = firm_type;
803 static inline ir_mode *get_ir_mode(type_t *type)
805 ir_type *irtype = get_ir_type(type);
807 /* firm doesn't report a mode for arrays somehow... */
808 if(is_Array_type(irtype)) {
812 ir_mode *mode = get_type_mode(irtype);
813 assert(mode != NULL);
817 static ident *predef_idents[rts_max];
819 /** Names of the runtime functions. */
820 static const struct {
821 int id; /**< the rts id */
822 int n_res; /**< number of return values */
823 const char *name; /**< the name of the rts function */
824 int n_params; /**< number of parameters */
825 unsigned flags; /**< language flags */
827 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
828 { rts_abort, 0, "abort", 0, _C89 },
829 { rts_abs, 1, "abs", 1, _C89 },
830 { rts_labs, 1, "labs", 1, _C89 },
831 { rts_llabs, 1, "llabs", 1, _C99 },
832 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
834 { rts_fabs, 1, "fabs", 1, _C89 },
835 { rts_sqrt, 1, "sqrt", 1, _C89 },
836 { rts_cbrt, 1, "cbrt", 1, _C99 },
837 { rts_exp, 1, "exp", 1, _C89 },
838 { rts_exp2, 1, "exp2", 1, _C89 },
839 { rts_exp10, 1, "exp10", 1, _GNUC },
840 { rts_log, 1, "log", 1, _C89 },
841 { rts_log2, 1, "log2", 1, _C89 },
842 { rts_log10, 1, "log10", 1, _C89 },
843 { rts_pow, 1, "pow", 2, _C89 },
844 { rts_sin, 1, "sin", 1, _C89 },
845 { rts_cos, 1, "cos", 1, _C89 },
846 { rts_tan, 1, "tan", 1, _C89 },
847 { rts_asin, 1, "asin", 1, _C89 },
848 { rts_acos, 1, "acos", 1, _C89 },
849 { rts_atan, 1, "atan", 1, _C89 },
850 { rts_sinh, 1, "sinh", 1, _C89 },
851 { rts_cosh, 1, "cosh", 1, _C89 },
852 { rts_tanh, 1, "tanh", 1, _C89 },
854 { rts_fabsf, 1, "fabsf", 1, _C99 },
855 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
856 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
857 { rts_expf, 1, "expf", 1, _C99 },
858 { rts_exp2f, 1, "exp2f", 1, _C99 },
859 { rts_exp10f, 1, "exp10f", 1, _GNUC },
860 { rts_logf, 1, "logf", 1, _C99 },
861 { rts_log2f, 1, "log2f", 1, _C99 },
862 { rts_log10f, 1, "log10f", 1, _C99 },
863 { rts_powf, 1, "powf", 2, _C99 },
864 { rts_sinf, 1, "sinf", 1, _C99 },
865 { rts_cosf, 1, "cosf", 1, _C99 },
866 { rts_tanf, 1, "tanf", 1, _C99 },
867 { rts_asinf, 1, "asinf", 1, _C99 },
868 { rts_acosf, 1, "acosf", 1, _C99 },
869 { rts_atanf, 1, "atanf", 1, _C99 },
870 { rts_sinhf, 1, "sinhf", 1, _C99 },
871 { rts_coshf, 1, "coshf", 1, _C99 },
872 { rts_tanhf, 1, "tanhf", 1, _C99 },
874 { rts_fabsl, 1, "fabsl", 1, _C99 },
875 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
876 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
877 { rts_expl, 1, "expl", 1, _C99 },
878 { rts_exp2l, 1, "exp2l", 1, _C99 },
879 { rts_exp10l, 1, "exp10l", 1, _GNUC },
880 { rts_logl, 1, "logl", 1, _C99 },
881 { rts_log2l, 1, "log2l", 1, _C99 },
882 { rts_log10l, 1, "log10l", 1, _C99 },
883 { rts_powl, 1, "powl", 2, _C99 },
884 { rts_sinl, 1, "sinl", 1, _C99 },
885 { rts_cosl, 1, "cosl", 1, _C99 },
886 { rts_tanl, 1, "tanl", 1, _C99 },
887 { rts_asinl, 1, "asinl", 1, _C99 },
888 { rts_acosl, 1, "acosl", 1, _C99 },
889 { rts_atanl, 1, "atanl", 1, _C99 },
890 { rts_sinhl, 1, "sinhl", 1, _C99 },
891 { rts_coshl, 1, "coshl", 1, _C99 },
892 { rts_tanhl, 1, "tanhl", 1, _C99 },
894 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
895 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
896 { rts_strcmp, 1, "strcmp", 2, _C89 },
897 { rts_strncmp, 1, "strncmp", 3, _C89 }
901 * Mangles an entity linker (ld) name for win32 usage.
903 * @param ent the entity to be mangled
904 * @param declaration the declaration
906 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
910 if (is_Method_type(get_entity_type(ent)))
911 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
913 /* always add an underscore in win32 */
914 id = mangle(id_underscore, get_entity_ident(ent));
917 decl_modifiers_t decl_modifiers = declaration->decl_modifiers;
918 if (decl_modifiers & DM_DLLIMPORT) {
919 /* add prefix for imported symbols */
920 id = mangle(id_imp, id);
926 * Mangles an entity linker (ld) name for Linux ELF usage.
928 * @param ent the entity to be mangled
929 * @param declaration the declaration
931 static ident *create_ld_ident_linux_elf(ir_entity *entity,
932 declaration_t *declaration)
935 return get_entity_ident(entity);
939 * Mangles an entity linker (ld) name for Mach-O usage.
941 * @param ent the entity to be mangled
942 * @param declaration the declaration
944 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
947 ident *id = mangle(id_underscore, get_entity_ident(ent));
951 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
952 declaration_t *declaration);
953 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
955 static ir_entity* get_function_entity(declaration_t *declaration)
957 if(declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
958 return declaration->v.entity;
959 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
961 symbol_t *symbol = declaration->symbol;
962 ident *id = new_id_from_str(symbol->string);
964 ir_type *global_type = get_glob_type();
965 ir_type *ir_type_method = get_ir_type(declaration->type);
966 assert(is_Method_type(ir_type_method));
968 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
969 ir_entity *const entity = new_d_entity(global_type, id, ir_type_method, dbgi);
970 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
971 if(declaration->storage_class == STORAGE_CLASS_STATIC &&
972 declaration->init.statement == NULL) {
973 /* this entity was declared, but never defined */
974 set_entity_peculiarity(entity, peculiarity_description);
976 if(declaration->storage_class == STORAGE_CLASS_STATIC
977 || declaration->is_inline) {
978 set_entity_visibility(entity, visibility_local);
979 } else if(declaration->init.statement != NULL) {
980 set_entity_visibility(entity, visibility_external_visible);
982 set_entity_visibility(entity, visibility_external_allocated);
984 /* We should check for file scope here, but as long as we compile C only
985 this is not needed. */
986 int n_params = get_method_n_params(ir_type_method);
987 int n_res = get_method_n_ress(ir_type_method);
990 if (n_params == 0 && n_res == 0 && id == predef_idents[rts_abort]) {
991 /* found abort(), store for later */
995 if (! firm_opt.freestanding) {
996 /* check for a known runtime function */
997 for (i = 0; i < rts_max; ++i) {
998 /* ignore those rts functions not necessary needed for current mode */
999 if ((c_mode & rts_data[i].flags) == 0)
1001 if (n_params == rts_data[i].n_params && n_res == rts_data[i].n_res &&
1002 id == predef_idents[rts_data[i].id])
1003 rts_entities[rts_data[i].id] = entity;
1008 set_entity_allocation(entity, allocation_static);
1010 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1011 declaration->v.entity = entity;
1016 static ir_node *const_to_firm(const const_expression_t *cnst)
1018 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1019 ir_mode *mode = get_ir_mode(cnst->base.type);
1024 if(mode_is_float(mode)) {
1025 tv = new_tarval_from_double(cnst->v.float_value, mode);
1027 if(mode_is_signed(mode)) {
1028 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1030 len = snprintf(buf, sizeof(buf), "%llu",
1031 (unsigned long long) cnst->v.int_value);
1033 tv = new_tarval_from_str(buf, len, mode);
1036 return new_d_Const(dbgi, mode, tv);
1039 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1041 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1042 ir_mode *mode = get_ir_mode(cnst->base.type);
1044 long long int v = 0;
1045 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1046 if (char_is_signed) {
1047 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1049 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1053 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1054 tarval *tv = new_tarval_from_str(buf, len, mode);
1056 return new_d_Const(dbgi, mode, tv);
1059 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1061 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1062 ir_mode *mode = get_ir_mode(cnst->base.type);
1064 long long int v = cnst->v.wide_character.begin[0];
1067 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1068 tarval *tv = new_tarval_from_str(buf, len, mode);
1070 return new_d_Const(dbgi, mode, tv);
1073 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1076 assert(entity != NULL);
1077 union symconst_symbol sym;
1078 sym.entity_p = entity;
1079 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1082 static ir_node *string_to_firm(const source_position_t *const src_pos,
1083 const char *const id_prefix,
1084 const string_t *const value)
1086 ir_type *const global_type = get_glob_type();
1087 dbg_info *const dbgi = get_dbg_info(src_pos);
1088 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1089 ir_type_const_char, dbgi);
1091 ident *const id = id_unique(id_prefix);
1092 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1093 set_entity_ld_ident(entity, id);
1094 set_entity_variability(entity, variability_constant);
1095 set_entity_allocation(entity, allocation_static);
1097 ir_type *const elem_type = ir_type_const_char;
1098 ir_mode *const mode = get_type_mode(elem_type);
1100 const char* const string = value->begin;
1101 const size_t slen = value->size;
1103 set_array_lower_bound_int(type, 0, 0);
1104 set_array_upper_bound_int(type, 0, slen);
1105 set_type_size_bytes(type, slen);
1106 set_type_state(type, layout_fixed);
1108 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1109 for(size_t i = 0; i < slen; ++i) {
1110 tvs[i] = new_tarval_from_long(string[i], mode);
1113 set_array_entity_values(entity, tvs, slen);
1116 return create_symconst(dbgi, mode_P_data, entity);
1119 static ir_node *string_literal_to_firm(
1120 const string_literal_expression_t* literal)
1122 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1126 static ir_node *wide_string_literal_to_firm(
1127 const wide_string_literal_expression_t* const literal)
1129 ir_type *const global_type = get_glob_type();
1130 ir_type *const elem_type = ir_type_wchar_t;
1131 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1132 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1135 ident *const id = id_unique("Lstr.%u");
1136 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1137 set_entity_ld_ident(entity, id);
1138 set_entity_variability(entity, variability_constant);
1139 set_entity_allocation(entity, allocation_static);
1141 ir_mode *const mode = get_type_mode(elem_type);
1143 const wchar_rep_t *const string = literal->value.begin;
1144 const size_t slen = literal->value.size;
1146 set_array_lower_bound_int(type, 0, 0);
1147 set_array_upper_bound_int(type, 0, slen);
1148 set_type_size_bytes(type, slen);
1149 set_type_state(type, layout_fixed);
1151 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1152 for(size_t i = 0; i < slen; ++i) {
1153 tvs[i] = new_tarval_from_long(string[i], mode);
1156 set_array_entity_values(entity, tvs, slen);
1159 return create_symconst(dbgi, mode_P_data, entity);
1162 static ir_node *deref_address(type_t *const type, ir_node *const addr,
1163 dbg_info *const dbgi)
1165 ir_type *irtype = get_ir_type(type);
1166 if (is_compound_type(irtype)
1167 || is_Method_type(irtype)
1168 || is_Array_type(irtype)) {
1172 ir_mode *const mode = get_type_mode(irtype);
1173 ir_node *const memory = get_store();
1174 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1175 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1176 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1178 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1179 set_Load_volatility(load, volatility_is_volatile);
1182 set_store(load_mem);
1186 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1188 ir_mode *mode = get_irn_mode(node);
1190 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1192 if (!mode_is_float(mode))
1195 /* check if there is already a Conv */
1196 if (get_irn_op(node) == op_Conv) {
1197 /* convert it into a strict Conv */
1198 set_Conv_strict(node, 1);
1202 /* otherwise create a new one */
1203 return new_d_strictConv(dbgi, node, mode);
1206 static ir_node *get_global_var_address(dbg_info *const dbgi,
1207 const declaration_t *const decl)
1209 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1211 ir_entity *const entity = decl->v.entity;
1212 switch ((storage_class_tag_t)decl->storage_class) {
1213 case STORAGE_CLASS_THREAD:
1214 case STORAGE_CLASS_THREAD_EXTERN:
1215 case STORAGE_CLASS_THREAD_STATIC: {
1216 ir_node *const no_mem = new_NoMem();
1217 ir_node *const tls = get_irg_tls(current_ir_graph);
1218 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1222 return create_symconst(dbgi, mode_P_data, entity);
1226 /* Returns the correct base address depending on whether it is a parameter or a
1227 * normal local variable */
1228 static ir_node *get_local_frame(ir_entity *const ent)
1230 ir_graph *const irg = current_ir_graph;
1231 const ir_type *const owner = get_entity_owner(ent);
1232 if (owner == get_irg_frame_type(irg)) {
1233 return get_irg_frame(irg);
1235 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1236 return get_irg_value_param_base(irg);
1240 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1242 ir_mode *value_mode = get_irn_mode(value);
1244 if (value_mode == dest_mode || is_Bad(value))
1247 if(dest_mode == mode_b) {
1248 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1249 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1250 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1254 return new_d_Conv(dbgi, value, dest_mode);
1257 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1259 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1260 declaration_t *declaration = ref->declaration;
1261 type_t *type = skip_typeref(declaration->type);
1263 switch((declaration_kind_t) declaration->declaration_kind) {
1264 case DECLARATION_KIND_UNKNOWN:
1265 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY) {
1271 case DECLARATION_KIND_ENUM_ENTRY: {
1272 ir_mode *const mode = get_ir_mode(type);
1273 return new_Const(mode, declaration->v.enum_val);
1276 case DECLARATION_KIND_LOCAL_VARIABLE: {
1277 ir_mode *const mode = get_ir_mode(type);
1278 return get_value(declaration->v.value_number, mode);
1280 case DECLARATION_KIND_FUNCTION: {
1281 ir_mode *const mode = get_ir_mode(type);
1282 return create_symconst(dbgi, mode, declaration->v.entity);
1284 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1285 ir_node *const addr = get_global_var_address(dbgi, declaration);
1286 return deref_address(declaration->type, addr, dbgi);
1289 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1290 ir_entity *entity = declaration->v.entity;
1291 ir_node *frame = get_local_frame(entity);
1292 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1293 return deref_address(declaration->type, sel, dbgi);
1296 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1297 return declaration->v.vla_base;
1299 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1300 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1301 case DECLARATION_KIND_COMPOUND_MEMBER:
1302 case DECLARATION_KIND_LABEL_BLOCK:
1303 panic("not implemented reference type");
1306 panic("reference to declaration with unknown type found");
1309 static ir_node *reference_addr(const reference_expression_t *ref)
1311 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1312 declaration_t *declaration = ref->declaration;
1314 switch((declaration_kind_t) declaration->declaration_kind) {
1315 case DECLARATION_KIND_UNKNOWN:
1317 case DECLARATION_KIND_LOCAL_VARIABLE:
1318 panic("local variable without entity has no address");
1319 case DECLARATION_KIND_FUNCTION: {
1320 type_t *const type = skip_typeref(ref->base.type);
1321 ir_mode *const mode = get_ir_mode(type);
1322 return create_symconst(dbgi, mode, declaration->v.entity);
1324 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1325 ir_node *const addr = get_global_var_address(dbgi, declaration);
1328 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1329 ir_entity *entity = declaration->v.entity;
1330 ir_node *frame = get_local_frame(entity);
1331 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1336 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1337 return declaration->v.vla_base;
1339 case DECLARATION_KIND_ENUM_ENTRY:
1340 panic("trying to reference enum entry");
1342 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1343 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1344 case DECLARATION_KIND_COMPOUND_MEMBER:
1345 case DECLARATION_KIND_LABEL_BLOCK:
1346 panic("not implemented reference type");
1349 panic("reference to declaration with unknown type found");
1352 static ir_node *process_builtin_call(const call_expression_t *call)
1354 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1356 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1357 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1359 type_t *type = skip_typeref(builtin->base.type);
1360 assert(is_type_pointer(type));
1362 type_t *function_type = skip_typeref(type->pointer.points_to);
1363 symbol_t *symbol = builtin->symbol;
1365 switch(symbol->ID) {
1366 case T___builtin_alloca: {
1367 if(call->arguments == NULL || call->arguments->next != NULL) {
1368 panic("invalid number of parameters on __builtin_alloca");
1370 expression_t *argument = call->arguments->expression;
1371 ir_node *size = expression_to_firm(argument);
1373 ir_node *store = get_store();
1374 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1376 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1378 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1382 case T___builtin_huge_val: {
1383 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1384 tarval *tv = get_mode_infinite(mode);
1385 ir_node *res = new_d_Const(dbgi, mode, tv);
1388 case T___builtin_nan:
1389 case T___builtin_nanf:
1390 case T___builtin_nand: {
1391 /* Ignore string for now... */
1392 assert(is_type_function(function_type));
1393 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1394 tarval *tv = get_mode_NAN(mode);
1395 ir_node *res = new_d_Const(dbgi, mode, tv);
1398 case T___builtin_va_end:
1401 panic("Unsupported builtin found\n");
1405 static ir_node *call_expression_to_firm(const call_expression_t *call)
1407 assert(get_cur_block() != NULL);
1409 expression_t *function = call->function;
1410 if(function->kind == EXPR_BUILTIN_SYMBOL) {
1411 return process_builtin_call(call);
1413 ir_node *callee = expression_to_firm(function);
1415 type_t *type = skip_typeref(function->base.type);
1416 assert(is_type_pointer(type));
1417 pointer_type_t *pointer_type = &type->pointer;
1418 type_t *points_to = skip_typeref(pointer_type->points_to);
1419 assert(is_type_function(points_to));
1420 function_type_t *function_type = &points_to->function;
1422 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1424 int n_parameters = 0;
1425 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1426 ir_type *new_method_type = NULL;
1427 if(function_type->variadic || function_type->unspecified_parameters) {
1428 const call_argument_t *argument = call->arguments;
1429 for( ; argument != NULL; argument = argument->next) {
1433 /* we need to construct a new method type matching the call
1435 int n_res = get_method_n_ress(ir_method_type);
1436 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1437 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1438 n_parameters, n_res, dbgi);
1439 set_method_calling_convention(new_method_type,
1440 get_method_calling_convention(ir_method_type));
1441 set_method_additional_properties(new_method_type,
1442 get_method_additional_properties(ir_method_type));
1443 set_method_variadicity(new_method_type,
1444 get_method_variadicity(ir_method_type));
1446 for(int i = 0; i < n_res; ++i) {
1447 set_method_res_type(new_method_type, i,
1448 get_method_res_type(ir_method_type, i));
1450 argument = call->arguments;
1451 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1452 expression_t *expression = argument->expression;
1453 ir_type *irtype = get_ir_type(expression->base.type);
1454 set_method_param_type(new_method_type, i, irtype);
1456 ir_method_type = new_method_type;
1458 n_parameters = get_method_n_params(ir_method_type);
1461 ir_node *in[n_parameters];
1463 const call_argument_t *argument = call->arguments;
1464 for(int n = 0; n < n_parameters; ++n) {
1465 expression_t *expression = argument->expression;
1466 ir_node *arg_node = expression_to_firm(expression);
1468 arg_node = do_strict_conv(dbgi, arg_node);
1472 argument = argument->next;
1475 ir_node *store = get_store();
1476 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1478 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1481 type_t *return_type = skip_typeref(function_type->return_type);
1482 ir_node *result = NULL;
1484 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1486 if(is_type_scalar(return_type)) {
1487 mode = get_ir_mode(return_type);
1491 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1492 result = new_d_Proj(dbgi, resproj, mode, 0);
1498 static void statement_to_firm(statement_t *statement);
1499 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1501 static ir_node *expression_to_addr(const expression_t *expression);
1502 static void create_condition_evaluation(const expression_t *expression,
1503 ir_node *true_block,
1504 ir_node *false_block);
1506 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1509 value = do_strict_conv(dbgi, value);
1511 ir_node *memory = get_store();
1513 if(is_type_scalar(type)) {
1514 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1515 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1516 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1517 set_Store_volatility(store, volatility_is_volatile);
1518 set_store(store_mem);
1520 ir_type *irtype = get_ir_type(type);
1521 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1522 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1523 set_store(copyb_mem);
1527 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1529 tarval *all_one = get_mode_all_one(mode);
1530 int mode_size = get_mode_size_bits(mode);
1532 assert(offset >= 0 && size >= 0);
1533 assert(offset + size <= mode_size);
1534 if(size == mode_size) {
1538 long shiftr = get_mode_size_bits(mode) - size;
1539 long shiftl = offset;
1540 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1541 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1542 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1543 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1548 static void bitfield_store_to_firm(const unary_expression_t *expression,
1551 expression_t *select = expression->value;
1552 assert(select->kind == EXPR_SELECT);
1553 type_t *type = select->base.type;
1554 assert(type->kind == TYPE_BITFIELD);
1555 ir_mode *mode = get_ir_mode(type->bitfield.base_type);
1556 ir_node *addr = expression_to_addr(select);
1558 assert(get_irn_mode(value) == mode || is_Bad(value));
1560 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1562 /* kill upper bits of value and shift to right position */
1563 ir_entity *entity = select->select.compound_entry->v.entity;
1564 int bitoffset = get_entity_offset_bits_remainder(entity);
1565 ir_type *entity_type = get_entity_type(entity);
1566 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1568 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1569 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1570 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1571 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1572 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1573 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1575 /* load current value */
1576 ir_node *mem = get_store();
1577 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1578 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1579 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1580 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1581 tarval *inv_mask = tarval_not(shift_mask);
1582 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1583 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1585 /* construct new value and store */
1586 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1587 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1588 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1589 set_store(store_mem);
1591 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1592 set_Load_volatility(load, volatility_is_volatile);
1593 set_Store_volatility(store, volatility_is_volatile);
1597 static void set_value_for_expression(const expression_t *expression,
1600 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1601 value = do_strict_conv(dbgi, value);
1603 if(expression->kind == EXPR_REFERENCE) {
1604 const reference_expression_t *ref = &expression->reference;
1606 declaration_t *declaration = ref->declaration;
1607 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1608 if(declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1609 set_value(declaration->v.value_number, value);
1614 if(expression->kind == EXPR_UNARY_BITFIELD_EXTRACT) {
1615 bitfield_store_to_firm(&expression->unary, value);
1619 ir_node *addr = expression_to_addr(expression);
1620 type_t *type = skip_typeref(expression->base.type);
1621 assign_value(dbgi, addr, type, value);
1624 static ir_node *create_incdec(const unary_expression_t *expression)
1626 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1627 type_t *type = skip_typeref(expression->base.type);
1628 ir_mode *mode = get_ir_mode(type);
1629 expression_t *value = expression->value;
1631 ir_node *value_node = expression_to_firm(value);
1634 if(is_type_pointer(type)) {
1635 pointer_type_t *pointer_type = &type->pointer;
1636 offset = get_type_size(pointer_type->points_to);
1638 assert(is_type_arithmetic(type));
1639 offset = new_Const(mode, get_mode_one(mode));
1642 switch(expression->base.kind) {
1643 case EXPR_UNARY_POSTFIX_INCREMENT: {
1644 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1645 set_value_for_expression(value, new_value);
1648 case EXPR_UNARY_POSTFIX_DECREMENT: {
1649 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1650 set_value_for_expression(value, new_value);
1653 case EXPR_UNARY_PREFIX_INCREMENT: {
1654 ir_node *new_value = new_d_Add(dbgi, value_node, offset, mode);
1655 set_value_for_expression(value, new_value);
1658 case EXPR_UNARY_PREFIX_DECREMENT: {
1659 ir_node *new_value = new_d_Sub(dbgi, value_node, offset, mode);
1660 set_value_for_expression(value, new_value);
1664 panic("no incdec expr in create_incdec");
1669 static bool is_local_variable(expression_t *expression)
1671 if (expression->kind != EXPR_REFERENCE)
1673 reference_expression_t *ref_expr = &expression->reference;
1674 declaration_t *declaration = ref_expr->declaration;
1675 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1678 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1681 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1682 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1683 case EXPR_BINARY_NOTEQUAL:
1684 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1685 case EXPR_BINARY_ISLESS:
1686 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1687 case EXPR_BINARY_ISLESSEQUAL:
1688 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1689 case EXPR_BINARY_ISGREATER:
1690 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1691 case EXPR_BINARY_ISGREATEREQUAL:
1692 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1693 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1698 panic("trying to get pn_Cmp from non-comparison binexpr type");
1702 * Handle the assume optimizer hint: check if a Confirm
1703 * node can be created.
1705 * @param dbi debug info
1706 * @param expr the IL assume expression
1708 * we support here only some simple cases:
1713 static ir_node *handle_assume_compare(dbg_info *dbi,
1714 const binary_expression_t *expression)
1716 expression_t *op1 = expression->left;
1717 expression_t *op2 = expression->right;
1718 declaration_t *var2, *var = NULL;
1719 ir_node *res = NULL;
1722 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1724 if (is_local_variable(op1) && is_local_variable(op2)) {
1725 var = op1->reference.declaration;
1726 var2 = op2->reference.declaration;
1728 type_t *const type = skip_typeref(var->type);
1729 ir_mode *const mode = get_ir_mode(type);
1731 ir_node *const irn1 = get_value(var->v.value_number, mode);
1732 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1734 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1735 set_value(var2->v.value_number, res);
1737 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1738 set_value(var->v.value_number, res);
1744 if (is_local_variable(op1) && is_constant_expression(op2)) {
1745 var = op1->reference.declaration;
1747 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1748 cmp_val = get_inversed_pnc(cmp_val);
1749 var = op2->reference.declaration;
1754 type_t *const type = skip_typeref(var->type);
1755 ir_mode *const mode = get_ir_mode(type);
1757 res = get_value(var->v.value_number, mode);
1758 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1759 set_value(var->v.value_number, res);
1765 * Handle the assume optimizer hint.
1767 * @param dbi debug info
1768 * @param expr the IL assume expression
1770 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1771 switch(expression->kind) {
1772 case EXPR_BINARY_EQUAL:
1773 case EXPR_BINARY_NOTEQUAL:
1774 case EXPR_BINARY_LESS:
1775 case EXPR_BINARY_LESSEQUAL:
1776 case EXPR_BINARY_GREATER:
1777 case EXPR_BINARY_GREATEREQUAL:
1778 return handle_assume_compare(dbi, &expression->binary);
1784 static ir_node *bitfield_extract_to_firm(const unary_expression_t *expression)
1786 expression_t *select = expression->value;
1787 assert(select->kind == EXPR_SELECT);
1789 type_t *type = select->base.type;
1790 assert(type->kind == TYPE_BITFIELD);
1791 ir_mode *mode = get_ir_mode(type->bitfield.base_type);
1792 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1793 ir_node *addr = expression_to_addr(select);
1794 ir_node *mem = get_store();
1795 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1796 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1797 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1799 load_res = create_conv(dbgi, load_res, mode_int);
1801 set_store(load_mem);
1803 /* kill upper bits */
1804 ir_entity *entity = select->select.compound_entry->v.entity;
1805 int bitoffset = get_entity_offset_bits_remainder(entity);
1806 ir_type *entity_type = get_entity_type(entity);
1807 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1808 long shift_bitsl = machine_size - bitoffset - bitsize;
1809 assert(shift_bitsl >= 0);
1810 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1811 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1812 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1814 long shift_bitsr = bitoffset + shift_bitsl;
1815 assert(shift_bitsr <= (long) machine_size);
1816 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1817 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1819 if(mode_is_signed(mode)) {
1820 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1822 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1825 return create_conv(dbgi, shiftr, mode);
1828 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1830 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1831 type_t *type = skip_typeref(expression->base.type);
1833 if(expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1834 return expression_to_addr(expression->value);
1836 const expression_t *value = expression->value;
1838 switch(expression->base.kind) {
1839 case EXPR_UNARY_NEGATE: {
1840 ir_node *value_node = expression_to_firm(value);
1841 ir_mode *mode = get_ir_mode(type);
1842 return new_d_Minus(dbgi, value_node, mode);
1844 case EXPR_UNARY_PLUS:
1845 return expression_to_firm(value);
1846 case EXPR_UNARY_BITWISE_NEGATE: {
1847 ir_node *value_node = expression_to_firm(value);
1848 ir_mode *mode = get_ir_mode(type);
1849 return new_d_Not(dbgi, value_node, mode);
1851 case EXPR_UNARY_NOT: {
1852 ir_node *value_node = expression_to_firm(value);
1853 ir_mode *mode = get_ir_mode(type);
1854 if(get_irn_mode(value_node) != mode_b) {
1855 value_node = create_conv(dbgi, value_node, mode_b);
1857 value_node = new_d_Not(dbgi, value_node, mode_b);
1858 if(mode != mode_b) {
1859 value_node = create_conv(dbgi, value_node, mode);
1863 case EXPR_UNARY_DEREFERENCE: {
1864 ir_node *value_node = expression_to_firm(value);
1865 type_t *value_type = skip_typeref(value->base.type);
1866 assert(is_type_pointer(value_type));
1867 type_t *points_to = value_type->pointer.points_to;
1868 return deref_address(points_to, value_node, dbgi);
1870 case EXPR_UNARY_POSTFIX_INCREMENT:
1871 case EXPR_UNARY_POSTFIX_DECREMENT:
1872 case EXPR_UNARY_PREFIX_INCREMENT:
1873 case EXPR_UNARY_PREFIX_DECREMENT:
1874 return create_incdec(expression);
1875 case EXPR_UNARY_CAST: {
1876 ir_node *value_node = expression_to_firm(value);
1877 if(is_type_scalar(type)) {
1878 ir_mode *mode = get_ir_mode(type);
1879 ir_node *node = create_conv(dbgi, value_node, mode);
1880 node = do_strict_conv(dbgi, node);
1883 /* make sure firm type is constructed */
1884 (void) get_ir_type(type);
1888 case EXPR_UNARY_CAST_IMPLICIT: {
1889 ir_node *value_node = expression_to_firm(value);
1890 if(is_type_scalar(type)) {
1891 ir_mode *mode = get_ir_mode(type);
1892 return create_conv(dbgi, value_node, mode);
1897 case EXPR_UNARY_ASSUME:
1898 if(firm_opt.confirm)
1899 return handle_assume(dbgi, value);
1902 case EXPR_UNARY_BITFIELD_EXTRACT:
1903 return bitfield_extract_to_firm(expression);
1908 panic("invalid UNEXPR type found");
1911 static ir_node *produce_condition_result(const expression_t *expression,
1914 ir_mode *mode = get_ir_mode(expression->base.type);
1915 ir_node *cur_block = get_cur_block();
1917 ir_node *one_block = new_immBlock();
1918 ir_node *one = new_Const(mode, get_mode_one(mode));
1919 ir_node *jmp_one = new_d_Jmp(dbgi);
1921 ir_node *zero_block = new_immBlock();
1922 ir_node *zero = new_Const(mode, get_mode_null(mode));
1923 ir_node *jmp_zero = new_d_Jmp(dbgi);
1925 set_cur_block(cur_block);
1926 create_condition_evaluation(expression, one_block, zero_block);
1927 mature_immBlock(one_block);
1928 mature_immBlock(zero_block);
1930 ir_node *common_block = new_immBlock();
1931 add_immBlock_pred(common_block, jmp_one);
1932 add_immBlock_pred(common_block, jmp_zero);
1933 mature_immBlock(common_block);
1935 ir_node *in[2] = { one, zero };
1936 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
1941 static ir_node *create_lazy_op(const binary_expression_t *expression)
1943 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1944 type_t *type = expression->base.type;
1945 ir_mode *mode = get_ir_mode(type);
1947 if(is_constant_expression(expression->left)) {
1948 long val = fold_constant(expression->left);
1949 expression_kind_t ekind = expression->base.kind;
1950 if((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
1951 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
1952 return expression_to_firm(expression->right);
1954 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
1955 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
1956 return new_Const(mode, get_mode_one(mode));
1960 return produce_condition_result((const expression_t*) expression, dbgi);
1963 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
1964 ir_node *right, ir_mode *mode);
1966 static ir_node *create_arithmetic_binop(const binary_expression_t *expression,
1967 create_arithmetic_func func)
1969 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1970 ir_node *left = expression_to_firm(expression->left);
1971 ir_node *right = expression_to_firm(expression->right);
1972 type_t *type = expression->right->base.type;
1973 /* be careful with the modes, because in arithmetic assign nodes only
1974 * the right operand has the mode of the arithmetic already */
1975 ir_mode *mode = get_ir_mode(type);
1976 left = create_conv(dbgi, left, mode);
1977 ir_node *res = func(dbgi, left, right, mode);
1982 static ir_node *pointer_arithmetic(ir_node *const pointer,
1985 dbg_info *const dbgi,
1986 const create_arithmetic_func func)
1988 pointer_type_t *const pointer_type = &type->pointer;
1989 type_t *const points_to = pointer_type->points_to;
1990 const unsigned elem_size = get_type_size_const(points_to);
1992 assert(elem_size >= 1);
1993 if (elem_size > 1) {
1994 integer = create_conv(dbgi, integer, mode_int);
1995 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
1996 ir_node *const mul = new_d_Mul(dbgi, integer, cnst, mode_int);
2000 ir_mode *const mode = get_ir_mode(type);
2001 return func(dbgi, pointer, integer, mode);
2004 static ir_node *create_arithmetic_assign_binop(
2005 const binary_expression_t *expression, create_arithmetic_func func)
2007 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2008 type_t *const type = skip_typeref(expression->base.type);
2011 if (is_type_pointer(type)) {
2012 ir_node *const pointer = expression_to_firm(expression->left);
2013 ir_node * integer = expression_to_firm(expression->right);
2014 value = pointer_arithmetic(pointer, integer, type, dbgi, func);
2016 value = create_arithmetic_binop(expression, func);
2019 ir_mode *const mode = get_ir_mode(type);
2020 value = create_conv(dbgi, value, mode);
2021 set_value_for_expression(expression->left, value);
2026 static ir_node *create_add(const binary_expression_t *expression)
2028 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2029 ir_node *left = expression_to_firm(expression->left);
2030 ir_node *right = expression_to_firm(expression->right);
2031 type_t *type = expression->base.type;
2033 expression_t *expr_left = expression->left;
2034 expression_t *expr_right = expression->right;
2035 type_t *type_left = skip_typeref(expr_left->base.type);
2036 type_t *type_right = skip_typeref(expr_right->base.type);
2038 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2039 ir_mode *const mode = get_ir_mode(type);
2040 return new_d_Add(dbgi, left, right, mode);
2043 if (is_type_pointer(type_left)) {
2044 return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
2046 assert(is_type_pointer(type_right));
2047 return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
2051 static ir_node *create_sub(const binary_expression_t *expression)
2053 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2054 expression_t *const expr_left = expression->left;
2055 expression_t *const expr_right = expression->right;
2056 ir_node *const left = expression_to_firm(expr_left);
2057 ir_node *const right = expression_to_firm(expr_right);
2058 type_t *const type = expression->base.type;
2059 type_t *const type_left = skip_typeref(expr_left->base.type);
2060 type_t *const type_right = skip_typeref(expr_right->base.type);
2062 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2063 ir_mode *const mode = get_ir_mode(type);
2064 return new_d_Sub(dbgi, left, right, mode);
2065 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2066 const pointer_type_t *const ptr_type = &type_left->pointer;
2068 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2069 ir_mode *const mode = get_ir_mode(type);
2070 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2071 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2072 ir_node *const no_mem = new_NoMem();
2073 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size, mode,
2074 op_pin_state_floats);
2075 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2078 assert(is_type_pointer(type_left));
2079 return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
2082 static ir_node *create_shift(const binary_expression_t *expression)
2084 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2085 ir_node *left = expression_to_firm(expression->left);
2086 ir_node *right = expression_to_firm(expression->right);
2087 type_t *type = expression->base.type;
2088 ir_mode *mode = get_ir_mode(type);
2090 /* firm always wants the shift count to be unsigned */
2091 right = create_conv(dbgi, right, mode_uint);
2095 switch(expression->base.kind) {
2096 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2097 case EXPR_BINARY_SHIFTLEFT:
2098 res = new_d_Shl(dbgi, left, right, mode);
2100 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2101 case EXPR_BINARY_SHIFTRIGHT: {
2102 expression_t *expr_left = expression->left;
2103 type_t *type_left = skip_typeref(expr_left->base.type);
2105 if(is_type_signed(type_left)) {
2106 res = new_d_Shrs(dbgi, left, right, mode);
2108 res = new_d_Shr(dbgi, left, right, mode);
2113 panic("create shift op called for non-shift op");
2120 static ir_node *create_divmod(const binary_expression_t *expression)
2122 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2123 ir_node *left = expression_to_firm(expression->left);
2124 ir_node *right = expression_to_firm(expression->right);
2125 ir_node *pin = new_Pin(new_NoMem());
2126 /* be careful with the modes, because in arithmetic assign nodes only
2127 * the right operand has the mode of the arithmetic already */
2128 type_t *type = expression->right->base.type;
2129 ir_mode *mode = get_ir_mode(type);
2130 left = create_conv(dbgi, left, mode);
2134 switch (expression->base.kind) {
2135 case EXPR_BINARY_DIV:
2136 case EXPR_BINARY_DIV_ASSIGN:
2137 if(mode_is_float(mode)) {
2138 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2139 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2141 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2142 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2146 case EXPR_BINARY_MOD:
2147 case EXPR_BINARY_MOD_ASSIGN:
2148 assert(!mode_is_float(mode));
2149 op = new_d_Mod(dbgi, pin, left, right, mode, op_pin_state_floats);
2150 res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2153 default: panic("unexpected binary expression type in create_divmod()");
2159 static ir_node *create_arithmetic_assign_divmod(
2160 const binary_expression_t *expression)
2162 ir_node * value = create_divmod(expression);
2163 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2164 type_t *const type = expression->base.type;
2165 ir_mode *const mode = get_ir_mode(type);
2167 assert(type->kind != TYPE_POINTER);
2169 value = create_conv(dbgi, value, mode);
2170 set_value_for_expression(expression->left, value);
2175 static ir_node *create_arithmetic_assign_shift(
2176 const binary_expression_t *expression)
2178 ir_node * value = create_shift(expression);
2179 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2180 type_t *const type = expression->base.type;
2181 ir_mode *const mode = get_ir_mode(type);
2183 value = create_conv(dbgi, value, mode);
2184 set_value_for_expression(expression->left, value);
2189 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2191 expression_kind_t kind = expression->base.kind;
2194 case EXPR_BINARY_EQUAL:
2195 case EXPR_BINARY_NOTEQUAL:
2196 case EXPR_BINARY_LESS:
2197 case EXPR_BINARY_LESSEQUAL:
2198 case EXPR_BINARY_GREATER:
2199 case EXPR_BINARY_GREATEREQUAL:
2200 case EXPR_BINARY_ISGREATER:
2201 case EXPR_BINARY_ISGREATEREQUAL:
2202 case EXPR_BINARY_ISLESS:
2203 case EXPR_BINARY_ISLESSEQUAL:
2204 case EXPR_BINARY_ISLESSGREATER:
2205 case EXPR_BINARY_ISUNORDERED: {
2206 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2207 ir_node *left = expression_to_firm(expression->left);
2208 ir_node *right = expression_to_firm(expression->right);
2209 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2210 long pnc = get_pnc(kind, expression->left->base.type);
2211 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2214 case EXPR_BINARY_ASSIGN: {
2215 ir_node *right = expression_to_firm(expression->right);
2216 set_value_for_expression(expression->left, right);
2220 case EXPR_BINARY_ADD:
2221 return create_add(expression);
2222 case EXPR_BINARY_SUB:
2223 return create_sub(expression);
2224 case EXPR_BINARY_MUL:
2225 return create_arithmetic_binop(expression, new_d_Mul);
2226 case EXPR_BINARY_BITWISE_AND:
2227 return create_arithmetic_binop(expression, new_d_And);
2228 case EXPR_BINARY_BITWISE_OR:
2229 return create_arithmetic_binop(expression, new_d_Or);
2230 case EXPR_BINARY_BITWISE_XOR:
2231 return create_arithmetic_binop(expression, new_d_Eor);
2232 case EXPR_BINARY_SHIFTLEFT:
2233 case EXPR_BINARY_SHIFTRIGHT:
2234 return create_shift(expression);
2235 case EXPR_BINARY_DIV:
2236 case EXPR_BINARY_MOD:
2237 return create_divmod(expression);
2238 case EXPR_BINARY_LOGICAL_AND:
2239 case EXPR_BINARY_LOGICAL_OR:
2240 return create_lazy_op(expression);
2241 case EXPR_BINARY_COMMA:
2242 expression_to_firm(expression->left);
2243 return expression_to_firm(expression->right);
2244 case EXPR_BINARY_ADD_ASSIGN:
2245 return create_arithmetic_assign_binop(expression, new_d_Add);
2246 case EXPR_BINARY_SUB_ASSIGN:
2247 return create_arithmetic_assign_binop(expression, new_d_Sub);
2248 case EXPR_BINARY_MUL_ASSIGN:
2249 return create_arithmetic_assign_binop(expression, new_d_Mul);
2250 case EXPR_BINARY_MOD_ASSIGN:
2251 case EXPR_BINARY_DIV_ASSIGN:
2252 return create_arithmetic_assign_divmod(expression);
2253 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2254 return create_arithmetic_assign_binop(expression, new_d_And);
2255 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2256 return create_arithmetic_assign_binop(expression, new_d_Or);
2257 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2258 return create_arithmetic_assign_binop(expression, new_d_Eor);
2259 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2260 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2261 return create_arithmetic_assign_shift(expression);
2262 case EXPR_BINARY_BUILTIN_EXPECT:
2263 return expression_to_firm(expression->left);
2265 panic("TODO binexpr type");
2269 static ir_node *array_access_addr(const array_access_expression_t *expression)
2271 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2272 ir_node *base_addr = expression_to_firm(expression->array_ref);
2273 ir_node *offset = expression_to_firm(expression->index);
2275 /* Matze: it would be better to force mode to mode_uint as this creates more
2276 * opportunities for CSE. Unforunately we still have some optimisations that
2277 * are too conservative in the presence of convs. So we better go with the
2278 * mode of offset and avoid the conv */
2279 ir_mode *mode = get_irn_mode(offset);
2280 offset = create_conv(dbgi, offset, mode);
2282 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2283 assert(is_type_pointer(ref_type));
2284 pointer_type_t *pointer_type = &ref_type->pointer;
2286 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2287 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2288 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2290 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2295 static ir_node *array_access_to_firm(
2296 const array_access_expression_t *expression)
2298 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2299 ir_node *addr = array_access_addr(expression);
2300 type_t *type = revert_automatic_type_conversion(
2301 (const expression_t*) expression);
2302 type = skip_typeref(type);
2304 return deref_address(type, addr, dbgi);
2307 static long get_offsetof_offset(const offsetof_expression_t *expression)
2309 type_t *orig_type = expression->type;
2312 designator_t *designator = expression->designator;
2313 for( ; designator != NULL; designator = designator->next) {
2314 type_t *type = skip_typeref(orig_type);
2315 /* be sure the type is constructed */
2316 (void) get_ir_type(type);
2318 if(designator->symbol != NULL) {
2319 assert(is_type_compound(type));
2320 symbol_t *symbol = designator->symbol;
2322 declaration_t *declaration = type->compound.declaration;
2323 declaration_t *iter = declaration->scope.declarations;
2324 for( ; iter != NULL; iter = iter->next) {
2325 if(iter->symbol == symbol) {
2329 assert(iter != NULL);
2331 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2332 offset += get_entity_offset(iter->v.entity);
2334 orig_type = iter->type;
2336 expression_t *array_index = designator->array_index;
2337 assert(designator->array_index != NULL);
2338 assert(is_type_array(type));
2339 assert(is_type_valid(array_index->base.type));
2341 long index = fold_constant(array_index);
2342 ir_type *arr_type = get_ir_type(type);
2343 ir_type *elem_type = get_array_element_type(arr_type);
2344 long elem_size = get_type_size_bytes(elem_type);
2346 offset += index * elem_size;
2348 orig_type = type->array.element_type;
2355 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2357 ir_mode *mode = get_ir_mode(expression->base.type);
2358 long offset = get_offsetof_offset(expression);
2359 tarval *tv = new_tarval_from_long(offset, mode);
2360 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2362 return new_d_Const(dbgi, mode, tv);
2365 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2366 ir_entity *entity, type_t *type);
2368 static ir_node *compound_literal_to_firm(
2369 const compound_literal_expression_t *expression)
2371 type_t *type = expression->type;
2373 /* create an entity on the stack */
2374 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2376 ident *const id = id_unique("CompLit.%u");
2377 ir_type *const irtype = get_ir_type(type);
2378 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2379 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2380 set_entity_ld_ident(entity, id);
2382 set_entity_variability(entity, variability_uninitialized);
2384 /* create initialisation code */
2385 initializer_t *initializer = expression->initializer;
2386 create_local_initializer(initializer, dbgi, entity, type);
2388 /* create a sel for the compound literal address */
2389 ir_node *frame = get_local_frame(entity);
2390 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2395 * Transform a sizeof expression into Firm code.
2397 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2399 type_t *type = expression->type;
2401 type = expression->tp_expression->base.type;
2402 assert(type != NULL);
2405 type = skip_typeref(type);
2406 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2407 if(is_type_array(type) && type->array.is_vla
2408 && expression->tp_expression != NULL) {
2409 expression_to_firm(expression->tp_expression);
2412 return get_type_size(type);
2416 * Transform an alignof expression into Firm code.
2418 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2420 type_t *type = expression->type;
2422 /* beware: if expression is a variable reference, return the
2423 alignment of the variable. */
2424 const expression_t *tp_expression = expression->tp_expression;
2425 const declaration_t *declaration = expr_is_variable(tp_expression);
2426 if (declaration != NULL) {
2427 /* TODO: get the alignment of this variable. */
2429 type = tp_expression->base.type;
2430 assert(type != NULL);
2433 ir_mode *const mode = get_ir_mode(expression->base.type);
2434 symconst_symbol sym;
2435 sym.type_p = get_ir_type(type);
2436 return new_SymConst(mode, sym, symconst_type_align);
2439 static void init_ir_types(void);
2440 long fold_constant(const expression_t *expression)
2444 assert(is_constant_expression(expression));
2446 ir_graph *old_current_ir_graph = current_ir_graph;
2447 if(current_ir_graph == NULL) {
2448 current_ir_graph = get_const_code_irg();
2451 ir_node *cnst = expression_to_firm(expression);
2452 current_ir_graph = old_current_ir_graph;
2454 if(!is_Const(cnst)) {
2455 panic("couldn't fold constant\n");
2458 tarval *tv = get_Const_tarval(cnst);
2459 if(!tarval_is_long(tv)) {
2460 panic("result of constant folding is not integer\n");
2463 return get_tarval_long(tv);
2466 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2468 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2470 /* first try to fold a constant condition */
2471 if(is_constant_expression(expression->condition)) {
2472 long val = fold_constant(expression->condition);
2474 return expression_to_firm(expression->true_expression);
2476 return expression_to_firm(expression->false_expression);
2480 ir_node *cur_block = get_cur_block();
2482 /* create the true block */
2483 ir_node *true_block = new_immBlock();
2485 ir_node *true_val = expression_to_firm(expression->true_expression);
2486 ir_node *true_jmp = new_Jmp();
2488 /* create the false block */
2489 ir_node *false_block = new_immBlock();
2491 ir_node *false_val = expression_to_firm(expression->false_expression);
2492 ir_node *false_jmp = new_Jmp();
2494 /* create the condition evaluation */
2495 set_cur_block(cur_block);
2496 create_condition_evaluation(expression->condition, true_block, false_block);
2497 mature_immBlock(true_block);
2498 mature_immBlock(false_block);
2500 /* create the common block */
2501 ir_node *common_block = new_immBlock();
2502 add_immBlock_pred(common_block, true_jmp);
2503 add_immBlock_pred(common_block, false_jmp);
2504 mature_immBlock(common_block);
2506 /* TODO improve static semantics, so either both or no values are NULL */
2507 if (true_val == NULL || false_val == NULL)
2510 ir_node *in[2] = { true_val, false_val };
2511 ir_mode *mode = get_irn_mode(true_val);
2512 assert(get_irn_mode(false_val) == mode);
2513 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2518 static ir_node *select_addr(const select_expression_t *expression)
2520 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2522 ir_node *compound_addr = expression_to_firm(expression->compound);
2524 /* make sure the type is constructed */
2525 type_t *type = skip_typeref(expression->compound->base.type);
2526 if (is_type_pointer(type)) {
2527 type = type->pointer.points_to;
2529 (void) get_ir_type(type);
2531 declaration_t *entry = expression->compound_entry;
2532 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2533 ir_entity *entity = entry->v.entity;
2535 assert(entity != NULL);
2537 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2542 static ir_node *select_to_firm(const select_expression_t *expression)
2544 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2545 ir_node *addr = select_addr(expression);
2546 type_t *type = revert_automatic_type_conversion(
2547 (const expression_t*) expression);
2548 type = skip_typeref(type);
2550 return deref_address(type, addr, dbgi);
2553 /* Values returned by __builtin_classify_type. */
2554 typedef enum gcc_type_class
2560 enumeral_type_class,
2563 reference_type_class,
2567 function_type_class,
2578 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2580 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2586 const atomic_type_t *const atomic_type = &type->atomic;
2587 switch (atomic_type->akind) {
2588 /* should not be reached */
2589 case ATOMIC_TYPE_INVALID:
2593 /* gcc cannot do that */
2594 case ATOMIC_TYPE_VOID:
2595 tc = void_type_class;
2598 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2599 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2600 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2601 case ATOMIC_TYPE_SHORT:
2602 case ATOMIC_TYPE_USHORT:
2603 case ATOMIC_TYPE_INT:
2604 case ATOMIC_TYPE_UINT:
2605 case ATOMIC_TYPE_LONG:
2606 case ATOMIC_TYPE_ULONG:
2607 case ATOMIC_TYPE_LONGLONG:
2608 case ATOMIC_TYPE_ULONGLONG:
2609 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2610 tc = integer_type_class;
2613 case ATOMIC_TYPE_FLOAT:
2614 case ATOMIC_TYPE_DOUBLE:
2615 case ATOMIC_TYPE_LONG_DOUBLE:
2616 tc = real_type_class;
2619 panic("Unexpected atomic type in classify_type_to_firm().");
2622 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2623 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2624 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2625 case TYPE_ARRAY: /* gcc handles this as pointer */
2626 case TYPE_FUNCTION: /* gcc handles this as pointer */
2627 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2628 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2629 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2631 /* gcc handles this as integer */
2632 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2635 /* typedef/typeof should be skipped already */
2642 panic("unexpected TYPE classify_type_to_firm().");
2645 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2646 ir_mode *const mode = mode_int;
2647 tarval *const tv = new_tarval_from_long(tc, mode);
2648 return new_d_Const(dbgi, mode, tv);
2651 static ir_node *function_name_to_firm(
2652 const funcname_expression_t *const expr)
2654 switch(expr->kind) {
2655 case FUNCNAME_FUNCTION:
2656 case FUNCNAME_PRETTY_FUNCTION:
2657 case FUNCNAME_FUNCDNAME:
2658 if (current_function_name == NULL) {
2659 const source_position_t *const src_pos = &expr->base.source_position;
2660 const char *const name = current_function_decl->symbol->string;
2661 const string_t string = { name, strlen(name) + 1 };
2662 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2664 return current_function_name;
2665 case FUNCNAME_FUNCSIG:
2666 if (current_funcsig == NULL) {
2667 const source_position_t *const src_pos = &expr->base.source_position;
2668 ir_entity *ent = get_irg_entity(current_ir_graph);
2669 const char *const name = get_entity_ld_name(ent);
2670 const string_t string = { name, strlen(name) + 1 };
2671 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2673 return current_funcsig;
2675 panic("Unsupported function name");
2678 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2680 statement_t *statement = expr->statement;
2682 assert(statement->kind == STATEMENT_COMPOUND);
2683 return compound_statement_to_firm(&statement->compound);
2686 static ir_node *va_start_expression_to_firm(
2687 const va_start_expression_t *const expr)
2689 ir_type *const method_type = get_ir_type(current_function_decl->type);
2690 int const n = get_method_n_params(method_type) - 1;
2691 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2692 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2693 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2694 ir_node *const no_mem = new_NoMem();
2695 ir_node *const arg_sel =
2696 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2698 ir_node *const cnst = get_type_size(expr->parameter->type);
2699 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2700 set_value_for_expression(expr->ap, add);
2705 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2707 type_t *const type = expr->base.type;
2708 ir_node *const ap = expression_to_firm(expr->ap);
2709 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2710 ir_node *const res = deref_address(type, ap, dbgi);
2712 ir_node *const cnst = get_type_size(expr->base.type);
2713 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2714 set_value_for_expression(expr->ap, add);
2719 static ir_node *dereference_addr(const unary_expression_t *const expression)
2721 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2722 return expression_to_firm(expression->value);
2725 static ir_node *expression_to_addr(const expression_t *expression)
2727 switch(expression->kind) {
2728 case EXPR_REFERENCE:
2729 return reference_addr(&expression->reference);
2730 case EXPR_ARRAY_ACCESS:
2731 return array_access_addr(&expression->array_access);
2733 return select_addr(&expression->select);
2735 return call_expression_to_firm(&expression->call);
2736 case EXPR_UNARY_DEREFERENCE: {
2737 return dereference_addr(&expression->unary);
2742 panic("trying to get address of non-lvalue");
2745 static ir_node *builtin_constant_to_firm(
2746 const builtin_constant_expression_t *expression)
2748 ir_mode *mode = get_ir_mode(expression->base.type);
2751 if (is_constant_expression(expression->value)) {
2756 return new_Const_long(mode, v);
2759 static ir_node *builtin_prefetch_to_firm(
2760 const builtin_prefetch_expression_t *expression)
2762 ir_node *adr = expression_to_firm(expression->adr);
2763 /* no Firm support for prefetch yet */
2768 static ir_node *_expression_to_firm(const expression_t *expression)
2770 switch(expression->kind) {
2771 case EXPR_CHARACTER_CONSTANT:
2772 return character_constant_to_firm(&expression->conste);
2773 case EXPR_WIDE_CHARACTER_CONSTANT:
2774 return wide_character_constant_to_firm(&expression->conste);
2776 return const_to_firm(&expression->conste);
2777 case EXPR_STRING_LITERAL:
2778 return string_literal_to_firm(&expression->string);
2779 case EXPR_WIDE_STRING_LITERAL:
2780 return wide_string_literal_to_firm(&expression->wide_string);
2781 case EXPR_REFERENCE:
2782 return reference_expression_to_firm(&expression->reference);
2784 return call_expression_to_firm(&expression->call);
2786 return unary_expression_to_firm(&expression->unary);
2788 return binary_expression_to_firm(&expression->binary);
2789 case EXPR_ARRAY_ACCESS:
2790 return array_access_to_firm(&expression->array_access);
2792 return sizeof_to_firm(&expression->typeprop);
2794 return alignof_to_firm(&expression->typeprop);
2795 case EXPR_CONDITIONAL:
2796 return conditional_to_firm(&expression->conditional);
2798 return select_to_firm(&expression->select);
2799 case EXPR_CLASSIFY_TYPE:
2800 return classify_type_to_firm(&expression->classify_type);
2802 return function_name_to_firm(&expression->funcname);
2803 case EXPR_STATEMENT:
2804 return statement_expression_to_firm(&expression->statement);
2806 return va_start_expression_to_firm(&expression->va_starte);
2808 return va_arg_expression_to_firm(&expression->va_arge);
2809 case EXPR_BUILTIN_SYMBOL:
2810 panic("unimplemented expression found");
2811 case EXPR_BUILTIN_CONSTANT_P:
2812 return builtin_constant_to_firm(&expression->builtin_constant);
2813 case EXPR_BUILTIN_PREFETCH:
2814 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2816 return offsetof_to_firm(&expression->offsetofe);
2817 case EXPR_COMPOUND_LITERAL:
2818 return compound_literal_to_firm(&expression->compound_literal);
2824 panic("invalid expression found");
2827 static ir_node *expression_to_firm(const expression_t *expression)
2829 ir_node *res = _expression_to_firm(expression);
2831 if(res != NULL && get_irn_mode(res) == mode_b) {
2832 ir_mode *mode = get_ir_mode(expression->base.type);
2834 if(is_Const_null(res)) {
2835 return new_Const_long(mode, 0);
2837 assert(is_Const_one(res));
2838 return new_Const_long(mode, 1);
2842 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2843 return produce_condition_result(expression, dbgi);
2849 static ir_node *expression_to_modeb(const expression_t *expression)
2851 ir_node *res = _expression_to_firm(expression);
2852 res = create_conv(NULL, res, mode_b);
2858 * create a short-circuit expression evaluation that tries to construct
2859 * efficient control flow structures for &&, || and ! expressions
2861 static void create_condition_evaluation(const expression_t *expression,
2862 ir_node *true_block,
2863 ir_node *false_block)
2865 switch(expression->kind) {
2866 case EXPR_UNARY_NOT: {
2867 const unary_expression_t *unary_expression = &expression->unary;
2868 create_condition_evaluation(unary_expression->value, false_block,
2872 case EXPR_BINARY_LOGICAL_AND: {
2873 const binary_expression_t *binary_expression = &expression->binary;
2875 ir_node *cur_block = get_cur_block();
2876 ir_node *extra_block = new_immBlock();
2877 set_cur_block(cur_block);
2878 create_condition_evaluation(binary_expression->left, extra_block,
2880 mature_immBlock(extra_block);
2881 set_cur_block(extra_block);
2882 create_condition_evaluation(binary_expression->right, true_block,
2886 case EXPR_BINARY_LOGICAL_OR: {
2887 const binary_expression_t *binary_expression = &expression->binary;
2889 ir_node *cur_block = get_cur_block();
2890 ir_node *extra_block = new_immBlock();
2891 set_cur_block(cur_block);
2892 create_condition_evaluation(binary_expression->left, true_block,
2894 mature_immBlock(extra_block);
2895 set_cur_block(extra_block);
2896 create_condition_evaluation(binary_expression->right, true_block,
2904 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2905 ir_node *condition = expression_to_modeb(expression);
2906 ir_node *cond = new_d_Cond(dbgi, condition);
2907 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
2908 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
2910 /* set branch prediction info based on __builtin_expect */
2911 if(expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
2912 long cnst = fold_constant(expression->binary.right);
2913 cond_jmp_predicate pred;
2916 pred = COND_JMP_PRED_FALSE;
2918 pred = COND_JMP_PRED_TRUE;
2920 set_Cond_jmp_pred(cond, pred);
2923 add_immBlock_pred(true_block, true_proj);
2924 add_immBlock_pred(false_block, false_proj);
2926 set_cur_block(NULL);
2931 static void create_declaration_entity(declaration_t *declaration,
2932 declaration_kind_t declaration_kind,
2933 ir_type *parent_type)
2935 ident *const id = new_id_from_str(declaration->symbol->string);
2936 type_t *const type = skip_typeref(declaration->type);
2937 ir_type *const irtype = get_ir_type(type);
2938 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
2939 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
2941 declaration->declaration_kind = (unsigned char) declaration_kind;
2942 declaration->v.entity = entity;
2943 set_entity_variability(entity, variability_uninitialized);
2944 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
2945 if(parent_type == get_tls_type())
2946 set_entity_allocation(entity, allocation_automatic);
2947 else if(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
2948 set_entity_allocation(entity, allocation_static);
2950 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
2951 set_entity_volatility(entity, volatility_is_volatile);
2956 typedef struct type_path_entry_t type_path_entry_t;
2957 struct type_path_entry_t {
2959 ir_initializer_t *initializer;
2961 declaration_t *compound_entry;
2964 typedef struct type_path_t type_path_t;
2965 struct type_path_t {
2966 type_path_entry_t *path;
2971 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
2973 size_t len = ARR_LEN(path->path);
2975 for(size_t i = 0; i < len; ++i) {
2976 const type_path_entry_t *entry = & path->path[i];
2978 type_t *type = skip_typeref(entry->type);
2979 if(is_type_compound(type)) {
2980 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
2981 } else if(is_type_array(type)) {
2982 fprintf(stderr, "[%zd]", entry->index);
2984 fprintf(stderr, "-INVALID-");
2987 fprintf(stderr, " (");
2988 print_type(path->top_type);
2989 fprintf(stderr, ")");
2992 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2994 size_t len = ARR_LEN(path->path);
2996 return & path->path[len-1];
2999 static type_path_entry_t *append_to_type_path(type_path_t *path)
3001 size_t len = ARR_LEN(path->path);
3002 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3004 type_path_entry_t *result = & path->path[len];
3005 memset(result, 0, sizeof(result[0]));
3009 static size_t get_compound_size(const compound_type_t *type)
3011 declaration_t *declaration = type->declaration;
3012 declaration_t *member = declaration->scope.declarations;
3014 for( ; member != NULL; member = member->next) {
3017 /* TODO: cache results? */
3022 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3024 type_t *orig_top_type = path->top_type;
3025 type_t *top_type = skip_typeref(orig_top_type);
3027 assert(is_type_compound(top_type) || is_type_array(top_type));
3029 if(ARR_LEN(path->path) == 0) {
3032 type_path_entry_t *top = get_type_path_top(path);
3033 ir_initializer_t *initializer = top->initializer;
3034 return get_initializer_compound_value(initializer, top->index);
3038 static void descend_into_subtype(type_path_t *path)
3040 type_t *orig_top_type = path->top_type;
3041 type_t *top_type = skip_typeref(orig_top_type);
3043 assert(is_type_compound(top_type) || is_type_array(top_type));
3045 ir_initializer_t *initializer = get_initializer_entry(path);
3047 type_path_entry_t *top = append_to_type_path(path);
3048 top->type = top_type;
3052 if(is_type_compound(top_type)) {
3053 declaration_t *declaration = top_type->compound.declaration;
3054 declaration_t *entry = declaration->scope.declarations;
3056 top->compound_entry = entry;
3058 len = get_compound_size(&top_type->compound);
3060 path->top_type = entry->type;
3062 assert(is_type_array(top_type));
3063 assert(top_type->array.size > 0);
3066 path->top_type = top_type->array.element_type;
3067 len = top_type->array.size;
3069 if(initializer == NULL
3070 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3071 initializer = create_initializer_compound(len);
3072 /* we have to set the entry at the 2nd latest path entry... */
3073 size_t path_len = ARR_LEN(path->path);
3074 assert(path_len >= 1);
3076 type_path_entry_t *entry = & path->path[path_len-2];
3077 ir_initializer_t *tinitializer = entry->initializer;
3078 set_initializer_compound_value(tinitializer, entry->index,
3082 top->initializer = initializer;
3085 static void ascend_from_subtype(type_path_t *path)
3087 type_path_entry_t *top = get_type_path_top(path);
3089 path->top_type = top->type;
3091 size_t len = ARR_LEN(path->path);
3092 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3095 static void walk_designator(type_path_t *path, const designator_t *designator)
3097 /* designators start at current object type */
3098 ARR_RESIZE(type_path_entry_t, path->path, 1);
3100 for( ; designator != NULL; designator = designator->next) {
3101 type_path_entry_t *top = get_type_path_top(path);
3102 type_t *orig_type = top->type;
3103 type_t *type = skip_typeref(orig_type);
3105 if(designator->symbol != NULL) {
3106 assert(is_type_compound(type));
3108 symbol_t *symbol = designator->symbol;
3110 declaration_t *declaration = type->compound.declaration;
3111 declaration_t *iter = declaration->scope.declarations;
3112 for( ; iter != NULL; iter = iter->next, ++index) {
3113 if(iter->symbol == symbol) {
3117 assert(iter != NULL);
3119 top->type = orig_type;
3120 top->compound_entry = iter;
3122 orig_type = iter->type;
3124 expression_t *array_index = designator->array_index;
3125 assert(designator->array_index != NULL);
3126 assert(is_type_array(type));
3127 assert(is_type_valid(array_index->base.type));
3129 long index = fold_constant(array_index);
3132 if(type->array.size_constant == 1) {
3133 long array_size = type->array.size;
3134 assert(index < array_size);
3138 top->type = orig_type;
3139 top->index = (size_t) index;
3140 orig_type = type->array.element_type;
3142 path->top_type = orig_type;
3144 if(designator->next != NULL) {
3145 descend_into_subtype(path);
3149 path->invalid = false;
3152 static void advance_current_object(type_path_t *path)
3155 /* TODO: handle this... */
3156 panic("invalid initializer in ast2firm (excessive elements)");
3160 type_path_entry_t *top = get_type_path_top(path);
3162 type_t *type = skip_typeref(top->type);
3163 if(is_type_union(type)) {
3164 top->compound_entry = NULL;
3165 } else if(is_type_struct(type)) {
3166 declaration_t *entry = top->compound_entry;
3169 entry = entry->next;
3170 top->compound_entry = entry;
3172 path->top_type = entry->type;
3176 assert(is_type_array(type));
3179 if(!type->array.size_constant || top->index < type->array.size) {
3184 /* we're past the last member of the current sub-aggregate, try if we
3185 * can ascend in the type hierarchy and continue with another subobject */
3186 size_t len = ARR_LEN(path->path);
3189 ascend_from_subtype(path);
3190 advance_current_object(path);
3192 path->invalid = true;
3197 static ir_initializer_t *create_ir_initializer(
3198 const initializer_t *initializer, type_t *type);
3200 static ir_initializer_t *create_ir_initializer_value(
3201 const initializer_value_t *initializer)
3203 if (is_type_compound(initializer->value->base.type)) {
3204 panic("initializer creation for compounds not implemented yet");
3206 ir_node *value = expression_to_firm(initializer->value);
3207 return create_initializer_const(value);
3210 static ir_initializer_t *create_ir_initializer_list(
3211 const initializer_list_t *initializer, type_t *type)
3214 memset(&path, 0, sizeof(path));
3215 path.top_type = type;
3216 path.path = NEW_ARR_F(type_path_entry_t, 0);
3218 descend_into_subtype(&path);
3220 for(size_t i = 0; i < initializer->len; ++i) {
3221 const initializer_t *sub_initializer = initializer->initializers[i];
3223 if(sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3224 walk_designator(&path, sub_initializer->designator.designator);
3228 if(sub_initializer->kind == INITIALIZER_VALUE) {
3229 /* we might have to descend into types until we're at a scalar
3232 type_t *orig_top_type = path.top_type;
3233 type_t *top_type = skip_typeref(orig_top_type);
3235 if(is_type_scalar(top_type))
3237 descend_into_subtype(&path);
3241 ir_initializer_t *sub_irinitializer
3242 = create_ir_initializer(sub_initializer, path.top_type);
3244 size_t path_len = ARR_LEN(path.path);
3245 assert(path_len >= 1);
3246 type_path_entry_t *entry = & path.path[path_len-1];
3247 ir_initializer_t *tinitializer = entry->initializer;
3248 set_initializer_compound_value(tinitializer, entry->index,
3251 advance_current_object(&path);
3254 assert(ARR_LEN(path.path) >= 1);
3255 ir_initializer_t *result = path.path[0].initializer;
3256 DEL_ARR_F(path.path);
3261 static ir_initializer_t *create_ir_initializer_string(
3262 const initializer_string_t *initializer, type_t *type)
3264 type = skip_typeref(type);
3266 size_t string_len = initializer->string.size;
3267 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3268 size_t len = type->array.size;
3269 ir_initializer_t *irinitializer = create_initializer_compound(len);
3271 const char *string = initializer->string.begin;
3272 ir_mode *mode = get_type_mode(ir_type_const_char);
3274 for(size_t i = 0; i < len; ++i) {
3279 tarval *tv = new_tarval_from_long(c, mode);
3280 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3282 set_initializer_compound_value(irinitializer, i, char_initializer);
3285 return irinitializer;
3288 static ir_initializer_t *create_ir_initializer_wide_string(
3289 const initializer_wide_string_t *initializer, type_t *type)
3291 size_t string_len = initializer->string.size;
3292 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3293 size_t len = type->array.size;
3294 ir_initializer_t *irinitializer = create_initializer_compound(len);
3296 const wchar_rep_t *string = initializer->string.begin;
3297 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3299 for(size_t i = 0; i < len; ++i) {
3301 if(i < string_len) {
3304 tarval *tv = new_tarval_from_long(c, mode);
3305 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3307 set_initializer_compound_value(irinitializer, i, char_initializer);
3310 return irinitializer;
3313 static ir_initializer_t *create_ir_initializer(
3314 const initializer_t *initializer, type_t *type)
3316 switch(initializer->kind) {
3317 case INITIALIZER_STRING:
3318 return create_ir_initializer_string(&initializer->string, type);
3320 case INITIALIZER_WIDE_STRING:
3321 return create_ir_initializer_wide_string(&initializer->wide_string,
3324 case INITIALIZER_LIST:
3325 return create_ir_initializer_list(&initializer->list, type);
3327 case INITIALIZER_VALUE:
3328 return create_ir_initializer_value(&initializer->value);
3330 case INITIALIZER_DESIGNATOR:
3331 panic("unexpected designator initializer found");
3333 panic("unknown initializer");
3336 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3339 if (is_atomic_type(type)) {
3340 ir_mode *mode = get_type_mode(type);
3341 tarval *zero = get_mode_null(mode);
3342 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3344 /* TODO: bitfields */
3345 ir_node *mem = get_store();
3346 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3347 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3350 assert(is_compound_type(type));
3353 if(is_Array_type(type)) {
3354 assert(has_array_upper_bound(type, 0));
3355 n_members = get_array_upper_bound_int(type, 0);
3357 n_members = get_compound_n_members(type);
3360 for(int i = 0; i < n_members; ++i) {
3363 if(is_Array_type(type)) {
3364 ir_entity *entity = get_array_element_entity(type);
3365 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3366 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3367 ir_node *in[1] = { cnst };
3368 irtype = get_array_element_type(type);
3369 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3371 ir_entity *member = get_compound_member(type, i);
3373 irtype = get_entity_type(member);
3374 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3377 create_dynamic_null_initializer(irtype, dbgi, addr);
3382 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3383 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3385 switch(get_initializer_kind(initializer)) {
3386 case IR_INITIALIZER_NULL: {
3387 create_dynamic_null_initializer(type, dbgi, base_addr);
3390 case IR_INITIALIZER_CONST: {
3391 ir_node *node = get_initializer_const_value(initializer);
3392 ir_mode *mode = get_irn_mode(node);
3393 assert(get_type_mode(type) == mode);
3395 /* TODO: bitfields... */
3396 ir_node *mem = get_store();
3397 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3398 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3402 case IR_INITIALIZER_TARVAL: {
3403 tarval *tv = get_initializer_tarval_value(initializer);
3404 ir_mode *mode = get_tarval_mode(tv);
3405 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3406 assert(get_type_mode(type) == mode);
3408 /* TODO: bitfields... */
3409 ir_node *mem = get_store();
3410 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3411 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3415 case IR_INITIALIZER_COMPOUND: {
3416 assert(is_compound_type(type));
3418 if(is_Array_type(type)) {
3419 assert(has_array_upper_bound(type, 0));
3420 n_members = get_array_upper_bound_int(type, 0);
3422 n_members = get_compound_n_members(type);
3425 if(get_initializer_compound_n_entries(initializer)
3426 != (unsigned) n_members)
3427 panic("initializer doesn't match compound type");
3429 for(int i = 0; i < n_members; ++i) {
3432 if(is_Array_type(type)) {
3433 ir_entity *entity = get_array_element_entity(type);
3434 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3435 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3436 ir_node *in[1] = { cnst };
3437 irtype = get_array_element_type(type);
3438 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3440 ir_entity *member = get_compound_member(type, i);
3442 irtype = get_entity_type(member);
3443 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3446 ir_initializer_t *sub_init
3447 = get_initializer_compound_value(initializer, i);
3449 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3455 panic("invalid IR_INITIALIZER found");
3458 static void create_dynamic_initializer(ir_initializer_t *initializer,
3459 dbg_info *dbgi, ir_entity *entity)
3461 ir_node *frame = get_local_frame(entity);
3462 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3463 ir_type *type = get_entity_type(entity);
3465 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3468 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3469 ir_entity *entity, type_t *type)
3471 ir_node *memory = get_store();
3472 ir_node *nomem = new_NoMem();
3473 ir_node *frame = get_irg_frame(current_ir_graph);
3474 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3476 if(initializer->kind == INITIALIZER_VALUE) {
3477 initializer_value_t *initializer_value = &initializer->value;
3479 ir_node *value = expression_to_firm(initializer_value->value);
3480 type = skip_typeref(type);
3481 assign_value(dbgi, addr, type, value);
3485 if(!is_constant_initializer(initializer)) {
3486 ir_initializer_t *irinitializer
3487 = create_ir_initializer(initializer, type);
3489 create_dynamic_initializer(irinitializer, dbgi, entity);
3493 /* create the ir_initializer */
3494 ir_graph *const old_current_ir_graph = current_ir_graph;
3495 current_ir_graph = get_const_code_irg();
3497 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3499 assert(current_ir_graph == get_const_code_irg());
3500 current_ir_graph = old_current_ir_graph;
3502 /* create a "template" entity which is copied to the entity on the stack */
3503 ident *const id = id_unique("initializer.%u");
3504 ir_type *const irtype = get_ir_type(type);
3505 ir_type *const global_type = get_glob_type();
3506 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3507 set_entity_ld_ident(init_entity, id);
3509 set_entity_variability(init_entity, variability_initialized);
3510 set_entity_visibility(init_entity, visibility_local);
3511 set_entity_allocation(init_entity, allocation_static);
3513 set_entity_initializer(init_entity, irinitializer);
3515 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3516 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3518 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3519 set_store(copyb_mem);
3522 static void create_initializer_local_variable_entity(declaration_t *declaration)
3524 initializer_t *initializer = declaration->init.initializer;
3525 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3526 ir_entity *entity = declaration->v.entity;
3527 type_t *type = declaration->type;
3528 create_local_initializer(initializer, dbgi, entity, type);
3531 static void create_declaration_initializer(declaration_t *declaration)
3533 initializer_t *initializer = declaration->init.initializer;
3534 if(initializer == NULL)
3537 declaration_kind_t declaration_kind
3538 = (declaration_kind_t) declaration->declaration_kind;
3539 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3540 create_initializer_local_variable_entity(declaration);
3544 if(initializer->kind == INITIALIZER_VALUE) {
3545 initializer_value_t *initializer_value = &initializer->value;
3547 = get_dbg_info(&declaration->source_position);
3549 ir_node *value = expression_to_firm(initializer_value->value);
3550 value = do_strict_conv(dbgi, value);
3552 if(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3553 set_value(declaration->v.value_number, value);
3555 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3557 ir_entity *entity = declaration->v.entity;
3559 set_entity_variability(entity, variability_initialized);
3560 set_atomic_ent_value(entity, value);
3563 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3564 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3566 ir_entity *entity = declaration->v.entity;
3567 ir_initializer_t *irinitializer
3568 = create_ir_initializer(initializer, declaration->type);
3570 set_entity_variability(entity, variability_initialized);
3571 set_entity_initializer(entity, irinitializer);
3575 static void create_variable_length_array(declaration_t *declaration)
3577 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3578 type_t *type = declaration->type;
3579 ir_node *mem = get_store();
3580 ir_type *el_type = get_ir_type(type->array.element_type);
3582 /* make sure size_node is calculated */
3583 get_type_size(type);
3584 ir_node *elems = type->array.size_node;
3585 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3587 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3588 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3591 /* initializers are not allowed for VLAs */
3592 assert(declaration->init.initializer == NULL);
3594 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3595 declaration->v.vla_base = addr;
3597 /* TODO: record VLA somewhere so we create the free node when we leave
3602 * Creates a Firm local variable from a declaration.
3604 static void create_local_variable(declaration_t *declaration)
3606 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3608 bool needs_entity = declaration->address_taken;
3609 type_t *type = skip_typeref(declaration->type);
3611 /* is it a variable length array? */
3612 if(is_type_array(type) && !type->array.size_constant) {
3613 create_variable_length_array(declaration);
3615 } else if(is_type_array(type) || is_type_compound(type)) {
3616 needs_entity = true;
3617 } else if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3618 needs_entity = true;
3622 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3623 create_declaration_entity(declaration,
3624 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3627 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3628 declaration->v.value_number = next_value_number_function;
3629 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3630 ++next_value_number_function;
3633 create_declaration_initializer(declaration);
3636 static void create_local_static_variable(declaration_t *declaration)
3638 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3640 type_t *const type = skip_typeref(declaration->type);
3641 ir_type *const global_type = get_glob_type();
3642 ir_type *const irtype = get_ir_type(type);
3643 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3645 size_t l = strlen(declaration->symbol->string);
3646 char buf[l + sizeof(".%u")];
3647 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3648 ident *const id = id_unique(buf);
3650 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3652 if(type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3653 set_entity_volatility(entity, volatility_is_volatile);
3656 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3657 declaration->v.entity = entity;
3658 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3659 set_entity_variability(entity, variability_uninitialized);
3660 set_entity_visibility(entity, visibility_local);
3661 set_entity_allocation(entity, allocation_static);
3663 ir_graph *const old_current_ir_graph = current_ir_graph;
3664 current_ir_graph = get_const_code_irg();
3666 create_declaration_initializer(declaration);
3668 assert(current_ir_graph == get_const_code_irg());
3669 current_ir_graph = old_current_ir_graph;
3674 static void return_statement_to_firm(return_statement_t *statement)
3676 if(get_cur_block() == NULL)
3679 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3680 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3685 if(get_method_n_ress(func_irtype) > 0) {
3686 ir_type *res_type = get_method_res_type(func_irtype, 0);
3688 if(statement->value != NULL) {
3689 ir_node *node = expression_to_firm(statement->value);
3690 node = do_strict_conv(dbgi, node);
3694 if(is_compound_type(res_type)) {
3697 mode = get_type_mode(res_type);
3699 in[0] = new_Unknown(mode);
3703 /* build return_value for its side effects */
3704 if(statement->value != NULL) {
3705 expression_to_firm(statement->value);
3710 ir_node *store = get_store();
3711 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3713 ir_node *end_block = get_irg_end_block(current_ir_graph);
3714 add_immBlock_pred(end_block, ret);
3716 set_cur_block(NULL);
3719 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3721 if(get_cur_block() == NULL)
3724 return expression_to_firm(statement->expression);
3727 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3729 ir_node *result = NULL;
3730 statement_t *statement = compound->statements;
3731 for( ; statement != NULL; statement = statement->base.next) {
3732 //context2firm(&statement->scope);
3734 if(statement->base.next == NULL
3735 && statement->kind == STATEMENT_EXPRESSION) {
3736 result = expression_statement_to_firm(
3737 &statement->expression);
3740 statement_to_firm(statement);
3746 static void create_global_variable(declaration_t *declaration)
3750 switch ((storage_class_tag_t)declaration->storage_class) {
3751 case STORAGE_CLASS_STATIC:
3752 vis = visibility_local;
3755 case STORAGE_CLASS_EXTERN:
3756 vis = visibility_external_allocated;
3759 case STORAGE_CLASS_NONE:
3760 vis = visibility_external_visible;
3763 case STORAGE_CLASS_THREAD:
3764 vis = visibility_external_visible;
3767 case STORAGE_CLASS_THREAD_EXTERN:
3768 vis = visibility_external_allocated;
3771 case STORAGE_CLASS_THREAD_STATIC:
3772 vis = visibility_local;
3776 var_type = get_tls_type();
3780 var_type = get_glob_type();
3784 create_declaration_entity(declaration,
3785 DECLARATION_KIND_GLOBAL_VARIABLE,
3787 set_entity_visibility(declaration->v.entity, vis);
3791 case STORAGE_CLASS_TYPEDEF:
3792 case STORAGE_CLASS_AUTO:
3793 case STORAGE_CLASS_REGISTER:
3794 case STORAGE_CLASS_ENUM_ENTRY:
3797 panic("Invalid storage class for global variable");
3800 static void create_local_declaration(declaration_t *declaration)
3802 if(declaration->symbol == NULL)
3805 type_t *type = skip_typeref(declaration->type);
3807 switch ((storage_class_tag_t) declaration->storage_class) {
3808 case STORAGE_CLASS_STATIC:
3809 create_local_static_variable(declaration);
3811 case STORAGE_CLASS_EXTERN:
3812 create_global_variable(declaration);
3813 create_declaration_initializer(declaration);
3815 case STORAGE_CLASS_NONE:
3816 case STORAGE_CLASS_AUTO:
3817 case STORAGE_CLASS_REGISTER:
3818 if(is_type_function(type)) {
3819 if(declaration->init.statement != NULL) {
3820 panic("nested functions not supported yet");
3822 get_function_entity(declaration);
3825 create_local_variable(declaration);
3828 case STORAGE_CLASS_ENUM_ENTRY:
3829 case STORAGE_CLASS_TYPEDEF:
3830 case STORAGE_CLASS_THREAD:
3831 case STORAGE_CLASS_THREAD_EXTERN:
3832 case STORAGE_CLASS_THREAD_STATIC:
3835 panic("invalid storage class found");
3838 static void declaration_statement_to_firm(declaration_statement_t *statement)
3840 declaration_t *declaration = statement->declarations_begin;
3841 declaration_t *end = statement->declarations_end->next;
3842 for( ; declaration != end; declaration = declaration->next) {
3843 if(declaration->namespc != NAMESPACE_NORMAL)
3845 create_local_declaration(declaration);
3849 static void if_statement_to_firm(if_statement_t *statement)
3851 ir_node *cur_block = get_cur_block();
3853 ir_node *fallthrough_block = new_immBlock();
3855 /* the true (blocks) */
3856 ir_node *true_block;
3857 if (statement->true_statement != NULL) {
3858 true_block = new_immBlock();
3859 statement_to_firm(statement->true_statement);
3860 if(get_cur_block() != NULL) {
3861 ir_node *jmp = new_Jmp();
3862 add_immBlock_pred(fallthrough_block, jmp);
3865 true_block = fallthrough_block;
3868 /* the false (blocks) */
3869 ir_node *false_block;
3870 if(statement->false_statement != NULL) {
3871 false_block = new_immBlock();
3873 statement_to_firm(statement->false_statement);
3874 if(get_cur_block() != NULL) {
3875 ir_node *jmp = new_Jmp();
3876 add_immBlock_pred(fallthrough_block, jmp);
3879 false_block = fallthrough_block;
3882 /* create the condition */
3883 if(cur_block != NULL) {
3884 set_cur_block(cur_block);
3885 create_condition_evaluation(statement->condition, true_block,
3889 mature_immBlock(true_block);
3890 if(false_block != fallthrough_block) {
3891 mature_immBlock(false_block);
3893 mature_immBlock(fallthrough_block);
3895 set_cur_block(fallthrough_block);
3898 static void while_statement_to_firm(while_statement_t *statement)
3900 ir_node *jmp = NULL;
3901 if(get_cur_block() != NULL) {
3905 /* create the header block */
3906 ir_node *header_block = new_immBlock();
3908 add_immBlock_pred(header_block, jmp);
3911 /* the false block */
3912 ir_node *false_block = new_immBlock();
3915 ir_node *body_block;
3916 if (statement->body != NULL) {
3917 ir_node *old_continue_label = continue_label;
3918 ir_node *old_break_label = break_label;
3919 continue_label = header_block;
3920 break_label = false_block;
3922 body_block = new_immBlock();
3923 statement_to_firm(statement->body);
3925 assert(continue_label == header_block);
3926 assert(break_label == false_block);
3927 continue_label = old_continue_label;
3928 break_label = old_break_label;
3930 if(get_cur_block() != NULL) {
3932 add_immBlock_pred(header_block, jmp);
3935 body_block = header_block;
3938 /* create the condition */
3939 set_cur_block(header_block);
3941 create_condition_evaluation(statement->condition, body_block, false_block);
3942 mature_immBlock(body_block);
3943 mature_immBlock(false_block);
3944 mature_immBlock(header_block);
3946 set_cur_block(false_block);
3949 static void do_while_statement_to_firm(do_while_statement_t *statement)
3951 ir_node *jmp = NULL;
3952 if(get_cur_block() != NULL) {
3956 /* create the header block */
3957 ir_node *header_block = new_immBlock();
3959 /* the false block */
3960 ir_node *false_block = new_immBlock();
3963 ir_node *body_block = new_immBlock();
3965 add_immBlock_pred(body_block, jmp);
3968 if (statement->body != NULL) {
3969 ir_node *old_continue_label = continue_label;
3970 ir_node *old_break_label = break_label;
3971 continue_label = header_block;
3972 break_label = false_block;
3974 statement_to_firm(statement->body);
3976 assert(continue_label == header_block);
3977 assert(break_label == false_block);
3978 continue_label = old_continue_label;
3979 break_label = old_break_label;
3981 if (get_cur_block() == NULL) {
3982 mature_immBlock(header_block);
3983 mature_immBlock(body_block);
3984 mature_immBlock(false_block);
3989 ir_node *body_jmp = new_Jmp();
3990 add_immBlock_pred(header_block, body_jmp);
3991 mature_immBlock(header_block);
3993 /* create the condition */
3994 set_cur_block(header_block);
3996 create_condition_evaluation(statement->condition, body_block, false_block);
3997 mature_immBlock(body_block);
3998 mature_immBlock(false_block);
3999 mature_immBlock(header_block);
4001 set_cur_block(false_block);
4004 static void for_statement_to_firm(for_statement_t *statement)
4006 ir_node *jmp = NULL;
4007 if (get_cur_block() != NULL) {
4008 if(statement->initialisation != NULL) {
4009 expression_to_firm(statement->initialisation);
4012 /* create declarations */
4013 declaration_t *declaration = statement->scope.declarations;
4014 for( ; declaration != NULL; declaration = declaration->next) {
4015 create_local_declaration(declaration);
4022 /* create the step block */
4023 ir_node *const step_block = new_immBlock();
4024 if (statement->step != NULL) {
4025 expression_to_firm(statement->step);
4027 ir_node *const step_jmp = new_Jmp();
4029 /* create the header block */
4030 ir_node *const header_block = new_immBlock();
4032 add_immBlock_pred(header_block, jmp);
4034 add_immBlock_pred(header_block, step_jmp);
4036 /* the false block */
4037 ir_node *const false_block = new_immBlock();
4040 ir_node * body_block;
4041 if (statement->body != NULL) {
4042 ir_node *const old_continue_label = continue_label;
4043 ir_node *const old_break_label = break_label;
4044 continue_label = step_block;
4045 break_label = false_block;
4047 body_block = new_immBlock();
4048 statement_to_firm(statement->body);
4050 assert(continue_label == step_block);
4051 assert(break_label == false_block);
4052 continue_label = old_continue_label;
4053 break_label = old_break_label;
4055 if (get_cur_block() != NULL) {
4057 add_immBlock_pred(step_block, jmp);
4060 body_block = step_block;
4063 /* create the condition */
4064 set_cur_block(header_block);
4065 if (statement->condition != NULL) {
4066 create_condition_evaluation(statement->condition, body_block,
4069 keep_alive(header_block);
4071 add_immBlock_pred(body_block, jmp);
4074 mature_immBlock(body_block);
4075 mature_immBlock(false_block);
4076 mature_immBlock(step_block);
4077 mature_immBlock(header_block);
4078 mature_immBlock(false_block);
4080 set_cur_block(false_block);
4083 static void create_jump_statement(const statement_t *statement,
4084 ir_node *target_block)
4086 if(get_cur_block() == NULL)
4089 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4090 ir_node *jump = new_d_Jmp(dbgi);
4091 add_immBlock_pred(target_block, jump);
4093 set_cur_block(NULL);
4096 static void switch_statement_to_firm(const switch_statement_t *statement)
4098 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4100 ir_node *expression = expression_to_firm(statement->expression);
4101 ir_node *cond = new_d_Cond(dbgi, expression);
4102 ir_node *break_block = new_immBlock();
4104 set_cur_block(NULL);
4106 ir_node *const old_switch_cond = current_switch_cond;
4107 ir_node *const old_break_label = break_label;
4108 const bool old_saw_default_label = saw_default_label;
4109 saw_default_label = false;
4110 current_switch_cond = cond;
4111 break_label = break_block;
4113 if (statement->body != NULL) {
4114 statement_to_firm(statement->body);
4117 if(get_cur_block() != NULL) {
4118 ir_node *jmp = new_Jmp();
4119 add_immBlock_pred(break_block, jmp);
4122 if (!saw_default_label) {
4123 set_cur_block(get_nodes_block(cond));
4124 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4125 MAGIC_DEFAULT_PN_NUMBER);
4126 add_immBlock_pred(break_block, proj);
4129 assert(current_switch_cond == cond);
4130 assert(break_label == break_block);
4131 current_switch_cond = old_switch_cond;
4132 break_label = old_break_label;
4133 saw_default_label = old_saw_default_label;
4135 mature_immBlock(break_block);
4136 set_cur_block(break_block);
4139 static void case_label_to_firm(const case_label_statement_t *statement)
4141 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4143 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4145 /* let's create a node and hope firm constant folding creates a Const
4148 ir_node *old_block = get_nodes_block(current_switch_cond);
4149 ir_node *block = new_immBlock();
4151 set_cur_block(old_block);
4152 if(statement->expression != NULL) {
4153 long start_pn = fold_constant(statement->expression);
4154 long end_pn = start_pn;
4155 if (statement->end_range != NULL) {
4156 end_pn = fold_constant(statement->end_range);
4158 assert(start_pn <= end_pn);
4159 /* create jumps for all cases in the given range */
4160 for (long pn = start_pn; pn <= end_pn; ++pn) {
4161 if(pn == MAGIC_DEFAULT_PN_NUMBER) {
4162 /* oops someone detected our cheating... */
4163 panic("magic default pn used");
4165 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4166 add_immBlock_pred(block, proj);
4169 saw_default_label = true;
4170 proj = new_d_defaultProj(dbgi, current_switch_cond,
4171 MAGIC_DEFAULT_PN_NUMBER);
4173 add_immBlock_pred(block, proj);
4176 if (fallthrough != NULL) {
4177 add_immBlock_pred(block, fallthrough);
4179 mature_immBlock(block);
4180 set_cur_block(block);
4182 if(statement->statement != NULL) {
4183 statement_to_firm(statement->statement);
4187 static ir_node *get_label_block(declaration_t *label)
4189 assert(label->namespc == NAMESPACE_LABEL);
4191 if(label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4192 return label->v.block;
4194 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4196 ir_node *old_cur_block = get_cur_block();
4197 ir_node *block = new_immBlock();
4198 set_cur_block(old_cur_block);
4200 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4201 label->v.block = block;
4203 ARR_APP1(ir_node *, imature_blocks, block);
4208 static void label_to_firm(const label_statement_t *statement)
4210 ir_node *block = get_label_block(statement->label);
4212 if(get_cur_block() != NULL) {
4213 ir_node *jmp = new_Jmp();
4214 add_immBlock_pred(block, jmp);
4217 set_cur_block(block);
4220 if(statement->statement != NULL) {
4221 statement_to_firm(statement->statement);
4225 static void goto_to_firm(const goto_statement_t *statement)
4227 if(get_cur_block() == NULL)
4230 ir_node *block = get_label_block(statement->label);
4231 ir_node *jmp = new_Jmp();
4232 add_immBlock_pred(block, jmp);
4234 set_cur_block(NULL);
4237 typedef enum modifier_t {
4238 ASM_MODIFIER_WRITE_ONLY = 1 << 0,
4239 ASM_MODIFIER_READ_WRITE = 1 << 1,
4240 ASM_MODIFIER_COMMUTATIVE = 1 << 2,
4241 ASM_MODIFIER_EARLYCLOBBER = 1 << 3,
4244 static void asm_statement_to_firm(const asm_statement_t *statement)
4247 fprintf(stderr, "WARNING asm not implemented yet!\n");
4249 bool needs_memory = false;
4251 size_t n_clobbers = 0;
4252 asm_clobber_t *clobber = statement->clobbers;
4253 for( ; clobber != NULL; clobber = clobber->next) {
4254 if(strcmp(clobber->clobber, "memory") == 0) {
4255 needs_memory = true;
4259 ident *id = new_id_from_str(clobber->clobber);
4260 obstack_ptr_grow(&asm_obst, id);
4263 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4264 ident **clobbers = NULL;
4265 if(n_clobbers > 0) {
4266 clobbers = obstack_finish(&asm_obst);
4269 /* find and count input and output constraints */
4270 asm_constraint_t *constraint = statement->inputs;
4271 for( ; constraint != NULL; constraint = constraint->next) {
4273 bool supports_memop = false;
4274 for(const char *c = constraint->constraints; *c != 0; ++c) {
4275 /* TODO: improve error messages */
4279 panic("multiple alternative assembler constraints not "
4287 supports_memop = true;
4288 obstack_1grow(&asm_obst, *c);
4291 if(modifiers & ASM_MODIFIER_READ_WRITE)
4292 panic("inconsistent register constraints");
4293 modifiers |= ASM_MODIFIER_WRITE_ONLY;
4296 if(modifiers & ASM_MODIFIER_WRITE_ONLY)
4297 panic("inconsistent register constraints");
4298 modifiers |= ASM_MODIFIER_READ_WRITE;
4301 modifiers |= ASM_MODIFIER_EARLYCLOBBER;
4302 panic("early clobber assembler constraint not supported yet");
4305 modifiers |= ASM_MODIFIER_COMMUTATIVE;
4306 panic("commutative assembler constraint not supported yet");
4309 /* skip register preferences stuff... */
4310 while(*c != 0 && *c != ',')
4314 /* skip register preferences stuff... */
4318 obstack_1grow(&asm_obst, *c);
4322 obstack_1grow(&asm_obst, '\0');
4323 const char *constraint_string = obstack_finish(&asm_obst);
4325 needs_memory |= supports_memop;
4326 if(supports_memop) {
4333 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4334 statement_to_firm(statement->try_statement);
4335 warningf(&statement->base.source_position, "structured exception handling ignored");
4338 static void leave_statement_to_firm(leave_statement_t *statement) {
4339 errorf(&statement->base.source_position, "__leave not supported yet");
4342 static void statement_to_firm(statement_t *statement)
4344 switch(statement->kind) {
4345 case STATEMENT_INVALID:
4346 panic("invalid statement found");
4348 case STATEMENT_EMPTY:
4351 case STATEMENT_COMPOUND:
4352 compound_statement_to_firm(&statement->compound);
4354 case STATEMENT_RETURN:
4355 return_statement_to_firm(&statement->returns);
4357 case STATEMENT_EXPRESSION:
4358 expression_statement_to_firm(&statement->expression);
4361 if_statement_to_firm(&statement->ifs);
4363 case STATEMENT_WHILE:
4364 while_statement_to_firm(&statement->whiles);
4366 case STATEMENT_DO_WHILE:
4367 do_while_statement_to_firm(&statement->do_while);
4369 case STATEMENT_DECLARATION:
4370 declaration_statement_to_firm(&statement->declaration);
4372 case STATEMENT_BREAK:
4373 create_jump_statement(statement, break_label);
4375 case STATEMENT_CONTINUE:
4376 create_jump_statement(statement, continue_label);
4378 case STATEMENT_SWITCH:
4379 switch_statement_to_firm(&statement->switchs);
4381 case STATEMENT_CASE_LABEL:
4382 case_label_to_firm(&statement->case_label);
4385 for_statement_to_firm(&statement->fors);
4387 case STATEMENT_LABEL:
4388 label_to_firm(&statement->label);
4390 case STATEMENT_GOTO:
4391 goto_to_firm(&statement->gotos);
4394 asm_statement_to_firm(&statement->asms);
4396 case STATEMENT_MS_TRY:
4397 ms_try_statement_to_firm(&statement->ms_try);
4399 case STATEMENT_LEAVE:
4400 leave_statement_to_firm(&statement->leave);
4403 panic("Statement not implemented\n");
4406 static int count_decls_in_expression(const expression_t *expression);
4408 static int count_local_declarations(const declaration_t * decl,
4409 const declaration_t *const end)
4412 for (; decl != end; decl = decl->next) {
4413 if(decl->namespc != NAMESPACE_NORMAL)
4415 const type_t *type = skip_typeref(decl->type);
4416 if (!decl->address_taken && is_type_scalar(type))
4418 const initializer_t *initializer = decl->init.initializer;
4419 /* FIXME: should walk initializer hierarchies... */
4420 if(initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4421 count += count_decls_in_expression(initializer->value.value);
4427 static int count_decls_in_expression(const expression_t *expression) {
4430 if(expression == NULL)
4433 switch((expression_kind_t) expression->base.kind) {
4434 case EXPR_STATEMENT:
4435 return count_decls_in_stmts(expression->statement.statement);
4437 int count_left = count_decls_in_expression(expression->binary.left);
4438 int count_right = count_decls_in_expression(expression->binary.right);
4439 return count_left + count_right;
4442 return count_decls_in_expression(expression->unary.value);
4444 call_argument_t *argument = expression->call.arguments;
4445 for( ; argument != NULL; argument = argument->next) {
4446 count += count_decls_in_expression(argument->expression);
4453 panic("unexpected expression kind");
4455 case EXPR_COMPOUND_LITERAL:
4459 case EXPR_CONDITIONAL:
4460 count += count_decls_in_expression(expression->conditional.condition);
4461 count += count_decls_in_expression(expression->conditional.true_expression);
4462 count += count_decls_in_expression(expression->conditional.false_expression);
4465 case EXPR_BUILTIN_PREFETCH:
4466 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4467 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4468 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4471 case EXPR_BUILTIN_CONSTANT_P:
4472 count += count_decls_in_expression(expression->builtin_constant.value);
4476 count += count_decls_in_expression(expression->select.compound);
4479 case EXPR_ARRAY_ACCESS:
4480 count += count_decls_in_expression(expression->array_access.array_ref);
4481 count += count_decls_in_expression(expression->array_access.index);
4484 case EXPR_CLASSIFY_TYPE:
4485 count += count_decls_in_expression(expression->classify_type.type_expression);
4489 case EXPR_ALIGNOF: {
4490 expression_t *tp_expression = expression->typeprop.tp_expression;
4491 if (tp_expression != NULL) {
4492 count += count_decls_in_expression(tp_expression);
4498 case EXPR_REFERENCE:
4500 case EXPR_CHARACTER_CONSTANT:
4501 case EXPR_WIDE_CHARACTER_CONSTANT:
4502 case EXPR_STRING_LITERAL:
4503 case EXPR_WIDE_STRING_LITERAL:
4505 case EXPR_BUILTIN_SYMBOL:
4511 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4512 * (or implement all the missing expressions here/implement a walker)
4518 static int count_decls_in_stmts(const statement_t *stmt)
4521 for (; stmt != NULL; stmt = stmt->base.next) {
4522 switch (stmt->kind) {
4523 case STATEMENT_EMPTY:
4526 case STATEMENT_DECLARATION: {
4527 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4528 count += count_local_declarations(decl_stmt->declarations_begin,
4529 decl_stmt->declarations_end->next);
4533 case STATEMENT_COMPOUND: {
4534 const compound_statement_t *const comp =
4536 count += count_decls_in_stmts(comp->statements);
4540 case STATEMENT_IF: {
4541 const if_statement_t *const if_stmt = &stmt->ifs;
4542 count += count_decls_in_expression(if_stmt->condition);
4543 count += count_decls_in_stmts(if_stmt->true_statement);
4544 count += count_decls_in_stmts(if_stmt->false_statement);
4548 case STATEMENT_SWITCH: {
4549 const switch_statement_t *const switch_stmt = &stmt->switchs;
4550 count += count_decls_in_expression(switch_stmt->expression);
4551 count += count_decls_in_stmts(switch_stmt->body);
4555 case STATEMENT_LABEL: {
4556 const label_statement_t *const label_stmt = &stmt->label;
4557 if(label_stmt->statement != NULL) {
4558 count += count_decls_in_stmts(label_stmt->statement);
4563 case STATEMENT_WHILE: {
4564 const while_statement_t *const while_stmt = &stmt->whiles;
4565 count += count_decls_in_expression(while_stmt->condition);
4566 count += count_decls_in_stmts(while_stmt->body);
4570 case STATEMENT_DO_WHILE: {
4571 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4572 count += count_decls_in_expression(do_while_stmt->condition);
4573 count += count_decls_in_stmts(do_while_stmt->body);
4577 case STATEMENT_FOR: {
4578 const for_statement_t *const for_stmt = &stmt->fors;
4579 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4580 count += count_decls_in_expression(for_stmt->initialisation);
4581 count += count_decls_in_expression(for_stmt->condition);
4582 count += count_decls_in_expression(for_stmt->step);
4583 count += count_decls_in_stmts(for_stmt->body);
4587 case STATEMENT_CASE_LABEL: {
4588 const case_label_statement_t *label = &stmt->case_label;
4589 count += count_decls_in_expression(label->expression);
4590 if(label->statement != NULL) {
4591 count += count_decls_in_stmts(label->statement);
4597 case STATEMENT_BREAK:
4598 case STATEMENT_CONTINUE:
4601 case STATEMENT_EXPRESSION: {
4602 const expression_statement_t *expr_stmt = &stmt->expression;
4603 count += count_decls_in_expression(expr_stmt->expression);
4607 case STATEMENT_GOTO:
4608 case STATEMENT_LEAVE:
4609 case STATEMENT_INVALID:
4612 case STATEMENT_RETURN: {
4613 const return_statement_t *ret_stmt = &stmt->returns;
4614 count += count_decls_in_expression(ret_stmt->value);
4618 case STATEMENT_MS_TRY: {
4619 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
4620 count += count_decls_in_stmts(try_stmt->try_statement);
4621 if(try_stmt->except_expression != NULL)
4622 count += count_decls_in_expression(try_stmt->except_expression);
4623 count += count_decls_in_stmts(try_stmt->final_statement);
4631 static int get_function_n_local_vars(declaration_t *declaration)
4635 /* count parameters */
4636 count += count_local_declarations(declaration->scope.declarations, NULL);
4638 /* count local variables declared in body */
4639 count += count_decls_in_stmts(declaration->init.statement);
4644 static void initialize_function_parameters(declaration_t *declaration)
4646 ir_graph *irg = current_ir_graph;
4647 ir_node *args = get_irg_args(irg);
4648 ir_node *start_block = get_irg_start_block(irg);
4649 ir_type *function_irtype = get_ir_type(declaration->type);
4652 declaration_t *parameter = declaration->scope.declarations;
4653 for( ; parameter != NULL; parameter = parameter->next, ++n) {
4654 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
4655 type_t *type = skip_typeref(parameter->type);
4657 bool needs_entity = parameter->address_taken;
4658 assert(!is_type_array(type));
4659 if(is_type_compound(type)) {
4660 needs_entity = true;
4664 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
4665 ident *id = new_id_from_str(parameter->symbol->string);
4666 set_entity_ident(entity, id);
4668 parameter->declaration_kind
4669 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
4670 parameter->v.entity = entity;
4674 ir_mode *mode = get_ir_mode(parameter->type);
4676 ir_node *proj = new_r_Proj(irg, start_block, args, mode, pn);
4678 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
4679 parameter->v.value_number = next_value_number_function;
4680 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
4681 ++next_value_number_function;
4683 set_value(parameter->v.value_number, proj);
4688 * Handle additional decl modifiers for IR-graphs
4690 * @param irg the IR-graph
4691 * @param dec_modifiers additional modifiers
4693 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
4695 if (decl_modifiers & DM_NORETURN) {
4696 /* TRUE if the declaration includes the Microsoft
4697 __declspec(noreturn) specifier. */
4698 set_irg_additional_property(irg, mtp_property_noreturn);
4700 if (decl_modifiers & DM_NOTHROW) {
4701 /* TRUE if the declaration includes the Microsoft
4702 __declspec(nothrow) specifier. */
4703 set_irg_additional_property(irg, mtp_property_nothrow);
4705 if (decl_modifiers & DM_NAKED) {
4706 /* TRUE if the declaration includes the Microsoft
4707 __declspec(naked) specifier. */
4708 set_irg_additional_property(irg, mtp_property_naked);
4710 if (decl_modifiers & DM_FORCEINLINE) {
4711 /* TRUE if the declaration includes the
4712 Microsoft __forceinline specifier. */
4713 set_irg_inline_property(irg, irg_inline_forced);
4715 if (decl_modifiers & DM_NOINLINE) {
4716 /* TRUE if the declaration includes the Microsoft
4717 __declspec(noinline) specifier. */
4718 set_irg_inline_property(irg, irg_inline_forbidden);
4722 static void create_function(declaration_t *declaration)
4724 ir_entity *function_entity = get_function_entity(declaration);
4726 if(declaration->init.statement == NULL)
4729 current_function_decl = declaration;
4730 current_function_name = NULL;
4731 current_funcsig = NULL;
4733 assert(imature_blocks == NULL);
4734 imature_blocks = NEW_ARR_F(ir_node*, 0);
4736 int n_local_vars = get_function_n_local_vars(declaration);
4737 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
4739 set_irg_fp_model(irg, firm_opt.fp_model);
4740 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
4741 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
4743 ir_node *first_block = get_cur_block();
4745 /* set inline flags */
4746 if (declaration->is_inline)
4747 set_irg_inline_property(irg, irg_inline_recomended);
4748 handle_decl_modifier_irg(irg, declaration->decl_modifiers);
4750 next_value_number_function = 0;
4751 initialize_function_parameters(declaration);
4753 statement_to_firm(declaration->init.statement);
4755 ir_node *end_block = get_irg_end_block(irg);
4757 /* do we have a return statement yet? */
4758 if(get_cur_block() != NULL) {
4759 type_t *type = skip_typeref(declaration->type);
4760 assert(is_type_function(type));
4761 const function_type_t *func_type = &type->function;
4762 const type_t *return_type
4763 = skip_typeref(func_type->return_type);
4766 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
4767 ret = new_Return(get_store(), 0, NULL);
4770 if(is_type_scalar(return_type)) {
4771 mode = get_ir_mode(func_type->return_type);
4777 /* ยง5.1.2.2.3 main implicitly returns 0 */
4778 if (strcmp(declaration->symbol->string, "main") == 0) {
4779 in[0] = new_Const(mode, get_mode_null(mode));
4781 in[0] = new_Unknown(mode);
4783 ret = new_Return(get_store(), 1, in);
4785 add_immBlock_pred(end_block, ret);
4788 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
4789 mature_immBlock(imature_blocks[i]);
4791 DEL_ARR_F(imature_blocks);
4792 imature_blocks = NULL;
4794 mature_immBlock(first_block);
4795 mature_immBlock(end_block);
4797 irg_finalize_cons(irg);
4799 /* finalize the frame type */
4800 ir_type *frame_type = get_irg_frame_type(irg);
4801 int n = get_compound_n_members(frame_type);
4804 for(int i = 0; i < n; ++i) {
4805 ir_entity *entity = get_compound_member(frame_type, i);
4806 ir_type *entity_type = get_entity_type(entity);
4808 int align = get_type_alignment_bytes(entity_type);
4809 if(align > align_all)
4813 misalign = offset % align;
4815 offset += align - misalign;
4819 set_entity_offset(entity, offset);
4820 offset += get_type_size_bytes(entity_type);
4822 set_type_size_bytes(frame_type, offset);
4823 set_type_alignment_bytes(frame_type, align_all);
4828 static void scope_to_firm(scope_t *scope)
4830 /* first pass: create declarations */
4831 declaration_t *declaration = scope->declarations;
4832 for( ; declaration != NULL; declaration = declaration->next) {
4833 if(declaration->namespc != NAMESPACE_NORMAL)
4835 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4836 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4838 if(declaration->symbol == NULL)
4841 type_t *type = skip_typeref(declaration->type);
4842 if(is_type_function(type)) {
4843 get_function_entity(declaration);
4845 create_global_variable(declaration);
4849 /* second pass: create code/initializers */
4850 declaration = scope->declarations;
4851 for( ; declaration != NULL; declaration = declaration->next) {
4852 if(declaration->namespc != NAMESPACE_NORMAL)
4854 if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
4855 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
4857 if(declaration->symbol == NULL)
4860 type_t *type = declaration->type;
4861 if(type->kind == TYPE_FUNCTION) {
4862 create_function(declaration);
4864 assert(declaration->declaration_kind
4865 == DECLARATION_KIND_GLOBAL_VARIABLE);
4866 current_ir_graph = get_const_code_irg();
4867 create_declaration_initializer(declaration);
4872 void init_ast2firm(void)
4874 obstack_init(&asm_obst);
4875 init_atomic_modes();
4877 id_underscore = new_id_from_chars("_", 1);
4878 id_imp = new_id_from_chars("__imp_", 6);
4880 /* OS option must be set to the backend */
4881 const char *s = "ia32-gasmode=linux";
4882 switch (firm_opt.os_support) {
4883 case OS_SUPPORT_WIN32:
4884 create_ld_ident = create_ld_ident_win32;
4885 s = "ia32-gasmode=mingw";
4887 case OS_SUPPORT_LINUX:
4888 create_ld_ident = create_ld_ident_linux_elf;
4889 s = "ia32-gasmode=linux"; break;
4891 case OS_SUPPORT_MACHO:
4892 create_ld_ident = create_ld_ident_macho;
4893 s = "ia32-gasmode=macho"; break;
4898 /* create idents for all known runtime functions */
4899 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
4900 predef_idents[rts_data[i].id] = new_id_from_str(rts_data[i].name);
4904 static void init_ir_types(void)
4906 static int ir_types_initialized = 0;
4907 if(ir_types_initialized)
4909 ir_types_initialized = 1;
4911 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4912 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4913 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4915 ir_type_int = get_ir_type(type_int);
4916 ir_type_const_char = get_ir_type(type_const_char);
4917 ir_type_wchar_t = get_ir_type(type_wchar_t);
4918 ir_type_void = get_ir_type(type_void);
4921 void exit_ast2firm(void)
4923 obstack_free(&asm_obst, NULL);
4926 void translation_unit_to_firm(translation_unit_t *unit)
4928 /* just to be sure */
4929 continue_label = NULL;
4931 current_switch_cond = NULL;
4935 scope_to_firm(&unit->scope);