2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <libfirm/firm.h>
27 #include <libfirm/adt/obst.h>
28 #include <libfirm/be.h>
32 #include "adt/error.h"
33 #include "adt/array.h"
39 #include "diagnostic.h"
40 #include "lang_features.h"
43 #include "entitymap_t.h"
44 #include "driver/firm_opt.h"
45 #include "driver/firm_cmdline.h"
47 #define MAGIC_DEFAULT_PN_NUMBER (long) -314159265
49 /* some idents needed for name mangling */
50 static ident *id_underscore;
53 static ir_type *ir_type_const_char;
54 static ir_type *ir_type_wchar_t;
55 static ir_type *ir_type_void;
56 static ir_type *ir_type_int;
58 static type_t *type_const_char;
60 static int next_value_number_function;
61 static ir_node *continue_label;
62 static ir_node *break_label;
63 static ir_node *current_switch_cond;
64 static bool saw_default_label;
65 static ir_node **imature_blocks;
66 static bool constant_folding;
68 static const declaration_t *current_function_decl;
69 static ir_node *current_function_name;
70 static ir_node *current_funcsig;
72 static entitymap_t entitymap;
74 static struct obstack asm_obst;
76 typedef enum declaration_kind_t {
77 DECLARATION_KIND_UNKNOWN,
78 DECLARATION_KIND_FUNCTION,
79 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
80 DECLARATION_KIND_GLOBAL_VARIABLE,
81 DECLARATION_KIND_LOCAL_VARIABLE,
82 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
83 DECLARATION_KIND_COMPOUND_MEMBER,
84 DECLARATION_KIND_LABEL_BLOCK,
85 DECLARATION_KIND_ENUM_ENTRY,
86 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
87 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
91 static ir_type *get_ir_type(type_t *type);
92 static ir_type *get_ir_type_incomplete(type_t *type);
93 static int count_decls_in_stmts(const statement_t *stmt);
95 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
97 const declaration_t *declaration = get_irg_loc_description(irg, pos);
99 warningf(&declaration->source_position,
100 "variable '%#T' might be used uninitialized",
101 declaration->type, declaration->symbol);
102 return new_r_Unknown(irg, mode);
105 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
107 const source_position_t *pos = (const source_position_t*) dbg;
110 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
114 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
116 const source_position_t *pos = (const source_position_t*) dbg;
121 return pos->input_name;
124 static dbg_info *get_dbg_info(const source_position_t *pos)
126 return (dbg_info*) pos;
129 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
131 static ir_mode *mode_int, *mode_uint;
133 static ir_node *_expression_to_firm(const expression_t *expression);
134 static ir_node *expression_to_firm(const expression_t *expression);
135 static inline ir_mode *get_ir_mode(type_t *type);
136 static void create_local_declaration(declaration_t *declaration);
138 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
140 unsigned flags = get_atomic_type_flags(kind);
141 unsigned size = get_atomic_type_size(kind);
142 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
143 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
146 unsigned bit_size = size * 8;
147 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
148 ir_mode_arithmetic arithmetic;
149 unsigned modulo_shift;
151 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
152 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
153 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
154 sort = irms_int_number;
155 arithmetic = irma_twos_complement;
156 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
158 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
159 snprintf(name, sizeof(name), "f%d", bit_size);
160 sort = irms_float_number;
161 arithmetic = irma_ieee754;
164 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
172 * Initialises the atomic modes depending on the machine size.
174 static void init_atomic_modes(void)
176 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
177 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
179 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
180 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
182 /* there's no real void type in firm */
183 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
185 /* initialize pointer modes */
187 ir_mode_sort sort = irms_reference;
188 unsigned bit_size = machine_size;
190 ir_mode_arithmetic arithmetic = irma_twos_complement;
191 unsigned modulo_shift
192 = bit_size < machine_size ? machine_size : bit_size;
194 snprintf(name, sizeof(name), "p%d", machine_size);
195 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
198 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
199 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
201 /* Hmm, pointers should be machine size */
202 set_modeP_data(ptr_mode);
203 set_modeP_code(ptr_mode);
206 static unsigned get_compound_type_size(compound_type_t *type)
208 ir_type *irtype = get_ir_type((type_t*) type);
209 return get_type_size_bytes(irtype);
212 static unsigned get_array_type_size(array_type_t *type)
214 assert(!type->is_vla);
215 ir_type *irtype = get_ir_type((type_t*) type);
216 return get_type_size_bytes(irtype);
220 static unsigned get_type_size_const(type_t *type)
224 panic("error type occurred");
226 return get_atomic_type_size(type->atomic.akind);
228 return 2 * get_atomic_type_size(type->complex.akind);
230 return get_atomic_type_size(type->imaginary.akind);
232 return get_mode_size_bytes(mode_int);
233 case TYPE_COMPOUND_UNION:
234 case TYPE_COMPOUND_STRUCT:
235 return get_compound_type_size(&type->compound);
237 /* just a pointer to the function */
238 return get_mode_size_bytes(mode_P_code);
240 return get_mode_size_bytes(mode_P_data);
242 return get_array_type_size(&type->array);
244 return get_type_size_const(type->builtin.real_type);
246 panic("type size of bitfield request");
252 panic("Trying to determine size of invalid type");
255 static ir_node *get_type_size(type_t *type)
257 type = skip_typeref(type);
259 if (is_type_array(type) && type->array.is_vla) {
260 ir_node *size_node = type->array.size_node;
261 if (size_node == NULL) {
262 size_node = expression_to_firm(type->array.size_expression);
263 assert(!is_Const(size_node));
264 type->array.size_node = size_node;
267 ir_node *elem_size = get_type_size(type->array.element_type);
268 ir_mode *mode = get_irn_mode(size_node);
269 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
273 ir_mode *mode = get_ir_mode(type_size_t);
275 sym.type_p = get_ir_type(type);
276 return new_SymConst(mode, sym, symconst_type_size);
279 static unsigned count_parameters(const function_type_t *function_type)
283 function_parameter_t *parameter = function_type->parameters;
284 for ( ; parameter != NULL; parameter = parameter->next) {
292 * Creates a Firm type for an atomic type
294 static ir_type *create_atomic_type(const atomic_type_t *type)
296 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
297 atomic_type_kind_t kind = type->akind;
298 ir_mode *mode = _atomic_modes[kind];
299 ident *id = get_mode_ident(mode);
300 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
302 set_type_alignment_bytes(irtype, type->base.alignment);
308 * Creates a Firm type for a complex type
310 static ir_type *create_complex_type(const complex_type_t *type)
312 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
313 atomic_type_kind_t kind = type->akind;
314 ir_mode *mode = _atomic_modes[kind];
315 ident *id = get_mode_ident(mode);
320 /* FIXME: finish the array */
325 * Creates a Firm type for an imaginary type
327 static ir_type *create_imaginary_type(const imaginary_type_t *type)
329 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
330 atomic_type_kind_t kind = type->akind;
331 ir_mode *mode = _atomic_modes[kind];
332 ident *id = get_mode_ident(mode);
333 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
335 set_type_alignment_bytes(irtype, type->base.alignment);
341 * return type of a parameter (and take transparent union gnu extension into
344 static type_t *get_parameter_type(type_t *type)
346 type = skip_typeref(type);
347 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
348 declaration_t *decl = type->compound.declaration;
349 type = decl->scope.declarations->type;
355 static ir_type *create_method_type(const function_type_t *function_type)
357 type_t *return_type = function_type->return_type;
359 ident *id = id_unique("functiontype.%u");
360 int n_parameters = count_parameters(function_type);
361 int n_results = return_type == type_void ? 0 : 1;
362 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
363 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
365 if (return_type != type_void) {
366 ir_type *restype = get_ir_type(return_type);
367 set_method_res_type(irtype, 0, restype);
370 function_parameter_t *parameter = function_type->parameters;
372 for ( ; parameter != NULL; parameter = parameter->next) {
373 type_t *type = get_parameter_type(parameter->type);
374 ir_type *p_irtype = get_ir_type(type);
375 set_method_param_type(irtype, n, p_irtype);
379 if (function_type->variadic || function_type->unspecified_parameters) {
380 set_method_variadicity(irtype, variadicity_variadic);
386 static ir_type *create_pointer_type(pointer_type_t *type)
388 type_t *points_to = type->points_to;
389 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
390 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
391 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
392 ir_points_to, mode_P_data, dbgi);
397 static ir_type *create_array_type(array_type_t *type)
399 type_t *element_type = type->element_type;
400 ir_type *ir_element_type = get_ir_type(element_type);
402 ident *id = id_unique("array.%u");
403 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
404 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
406 const int align = get_type_alignment_bytes(ir_element_type);
407 set_type_alignment_bytes(ir_type, align);
409 if (type->size_constant) {
410 int n_elements = type->size;
412 set_array_bounds_int(ir_type, 0, 0, n_elements);
414 size_t elemsize = get_type_size_bytes(ir_element_type);
415 if (elemsize % align > 0) {
416 elemsize += align - (elemsize % align);
418 set_type_size_bytes(ir_type, n_elements * elemsize);
420 set_array_lower_bound_int(ir_type, 0, 0);
422 set_type_state(ir_type, layout_fixed);
428 * Return the signed integer type of size bits.
430 * @param size the size
432 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
435 static ir_mode *s_modes[64 + 1] = {NULL, };
439 if (size <= 0 || size > 64)
442 mode = s_modes[size];
446 snprintf(name, sizeof(name), "bf_I%u", size);
447 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
448 size <= 32 ? 32 : size );
449 s_modes[size] = mode;
453 snprintf(name, sizeof(name), "I%u", size);
454 ident *id = new_id_from_str(name);
455 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
456 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
457 set_primitive_base_type(res, base_tp);
463 * Return the unsigned integer type of size bits.
465 * @param size the size
467 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
470 static ir_mode *u_modes[64 + 1] = {NULL, };
474 if (size <= 0 || size > 64)
477 mode = u_modes[size];
481 snprintf(name, sizeof(name), "bf_U%u", size);
482 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
483 size <= 32 ? 32 : size );
484 u_modes[size] = mode;
489 snprintf(name, sizeof(name), "U%u", size);
490 ident *id = new_id_from_str(name);
491 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
492 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
493 set_primitive_base_type(res, base_tp);
498 static ir_type *create_bitfield_type(bitfield_type_t *const type)
500 type_t *base = skip_typeref(type->base_type);
501 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
502 ir_type *irbase = get_ir_type(base);
504 unsigned size = fold_constant(type->size);
506 assert(!is_type_float(base));
507 if (is_type_signed(base)) {
508 return get_signed_int_type_for_bit_size(irbase, size);
510 return get_unsigned_int_type_for_bit_size(irbase, size);
514 #define INVALID_TYPE ((ir_type_ptr)-1)
517 COMPOUND_IS_STRUCT = false,
518 COMPOUND_IS_UNION = true
522 * Construct firm type from ast struct type.
524 * As anonymous inner structs get flattened to a single firm type, we might get
525 * irtype, outer_offset and out_align passed (they represent the position of
526 * the anonymous inner struct inside the resulting firm struct)
528 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
529 size_t *outer_offset, size_t *outer_align,
530 bool incomplete, bool is_union)
532 declaration_t *declaration = type->declaration;
533 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
535 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
536 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
538 return declaration->v.irtype;
540 size_t align_all = 1;
542 size_t bit_offset = 0;
545 if (irtype == NULL) {
546 symbol_t *symbol = declaration->symbol;
548 if (symbol != NULL) {
549 id = new_id_from_str(symbol->string);
552 id = id_unique("__anonymous_union.%u");
554 id = id_unique("__anonymous_struct.%u");
557 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
560 irtype = new_d_type_union(id, dbgi);
562 irtype = new_d_type_struct(id, dbgi);
565 declaration->declaration_kind
566 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
567 declaration->v.irtype = irtype;
568 //type->base.firm_type = irtype;
570 offset = *outer_offset;
571 align_all = *outer_align;
577 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
579 declaration_t *entry = declaration->scope.declarations;
580 for( ; entry != NULL; entry = entry->next) {
581 if (entry->namespc != NAMESPACE_NORMAL)
584 size_t prev_offset = offset;
586 symbol_t *symbol = entry->symbol;
587 type_t *entry_type = skip_typeref(entry->type);
588 dbg_info *dbgi = get_dbg_info(&entry->source_position);
591 if (symbol != NULL) {
592 ident = new_id_from_str(symbol->string);
594 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
595 create_compound_type(&entry_type->compound, irtype, &offset,
596 &align_all, false, COMPOUND_IS_STRUCT);
597 goto finished_member;
598 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
599 create_compound_type(&entry_type->compound, irtype, &offset,
600 &align_all, false, COMPOUND_IS_UNION);
601 goto finished_member;
603 assert(entry_type->kind == TYPE_BITFIELD);
605 ident = id_unique("anon.%u");
608 ir_type *base_irtype;
609 if (entry_type->kind == TYPE_BITFIELD) {
610 base_irtype = get_ir_type(entry_type->bitfield.base_type);
612 base_irtype = get_ir_type(entry_type);
615 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
616 size_t misalign = offset % entry_alignment;
618 ir_type *entry_irtype = get_ir_type(entry_type);
619 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
622 size_t bits_remainder;
623 if (entry_type->kind == TYPE_BITFIELD) {
624 size_t size_bits = fold_constant(entry_type->bitfield.size);
625 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
627 if (size_bits > rest_size_bits) {
628 /* start a new bucket */
629 offset += entry_alignment - misalign;
635 /* put into current bucket */
636 base = offset - misalign;
637 bits_remainder = misalign * 8 + bit_offset;
640 offset += size_bits / 8;
641 bit_offset = bit_offset + (size_bits % 8);
643 size_t entry_size = get_type_size_bytes(base_irtype);
644 if (misalign > 0 || bit_offset > 0)
645 offset += entry_alignment - misalign;
649 offset += entry_size;
653 if (entry_alignment > align_all) {
654 if (entry_alignment % align_all != 0) {
655 panic("uneven alignments not supported yet");
657 align_all = entry_alignment;
660 set_entity_offset(entity, base);
661 set_entity_offset_bits_remainder(entity,
662 (unsigned char) bits_remainder);
663 //add_struct_member(irtype, entity);
664 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
665 assert(entry->v.entity == NULL);
666 entry->v.entity = entity;
670 size_t entry_size = offset - prev_offset;
671 if (entry_size > size) {
683 size_t misalign = offset % align_all;
684 if (misalign > 0 || bit_offset > 0) {
685 size += align_all - misalign;
688 if (outer_offset != NULL) {
690 *outer_offset = offset;
692 *outer_offset += size;
695 if (align_all > *outer_align) {
696 if (align_all % *outer_align != 0) {
697 panic("uneven alignments not supported yet");
699 *outer_align = align_all;
702 set_type_alignment_bytes(irtype, align_all);
703 set_type_size_bytes(irtype, size);
704 set_type_state(irtype, layout_fixed);
710 static ir_type *create_enum_type(enum_type_t *const type)
712 type->base.firm_type = ir_type_int;
714 ir_mode *const mode = mode_int;
715 tarval *const one = get_mode_one(mode);
716 tarval * tv_next = get_tarval_null(mode);
718 bool constant_folding_old = constant_folding;
719 constant_folding = true;
721 declaration_t *declaration = type->declaration->next;
722 for (; declaration != NULL; declaration = declaration->next) {
723 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
726 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
728 expression_t *const init = declaration->init.enum_value;
730 ir_node *const cnst = expression_to_firm(init);
731 if (!is_Const(cnst)) {
732 panic("couldn't fold constant");
734 tv_next = get_Const_tarval(cnst);
736 declaration->v.enum_val = tv_next;
737 tv_next = tarval_add(tv_next, one);
740 constant_folding = constant_folding_old;
745 static ir_type *get_ir_type_incomplete(type_t *type)
747 assert(type != NULL);
748 type = skip_typeref(type);
750 if (type->base.firm_type != NULL) {
751 assert(type->base.firm_type != INVALID_TYPE);
752 return type->base.firm_type;
755 switch (type->kind) {
756 case TYPE_COMPOUND_STRUCT:
757 return create_compound_type(&type->compound, NULL, NULL, NULL,
758 true, COMPOUND_IS_STRUCT);
759 case TYPE_COMPOUND_UNION:
760 return create_compound_type(&type->compound, NULL, NULL, NULL,
761 true, COMPOUND_IS_UNION);
763 return get_ir_type(type);
767 static ir_type *get_ir_type(type_t *type)
769 assert(type != NULL);
771 type = skip_typeref(type);
773 if (type->base.firm_type != NULL) {
774 assert(type->base.firm_type != INVALID_TYPE);
775 return type->base.firm_type;
778 ir_type *firm_type = NULL;
779 switch (type->kind) {
781 panic("error type occurred");
783 firm_type = create_atomic_type(&type->atomic);
786 firm_type = create_complex_type(&type->complex);
789 firm_type = create_imaginary_type(&type->imaginary);
792 firm_type = create_method_type(&type->function);
795 firm_type = create_pointer_type(&type->pointer);
798 firm_type = create_array_type(&type->array);
800 case TYPE_COMPOUND_STRUCT:
801 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
802 false, COMPOUND_IS_STRUCT);
804 case TYPE_COMPOUND_UNION:
805 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
806 false, COMPOUND_IS_UNION);
809 firm_type = create_enum_type(&type->enumt);
812 firm_type = get_ir_type(type->builtin.real_type);
815 firm_type = create_bitfield_type(&type->bitfield);
823 if (firm_type == NULL)
824 panic("unknown type found");
826 type->base.firm_type = firm_type;
830 static inline ir_mode *get_ir_mode(type_t *type)
832 ir_type *irtype = get_ir_type(type);
834 /* firm doesn't report a mode for arrays somehow... */
835 if (is_Array_type(irtype)) {
839 ir_mode *mode = get_type_mode(irtype);
840 assert(mode != NULL);
844 /** Names of the runtime functions. */
845 static const struct {
846 int id; /**< the rts id */
847 int n_res; /**< number of return values */
848 const char *name; /**< the name of the rts function */
849 int n_params; /**< number of parameters */
850 unsigned flags; /**< language flags */
852 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
853 { rts_abort, 0, "abort", 0, _C89 },
854 { rts_alloca, 1, "alloca", 1, _ALL },
855 { rts_abs, 1, "abs", 1, _C89 },
856 { rts_labs, 1, "labs", 1, _C89 },
857 { rts_llabs, 1, "llabs", 1, _C99 },
858 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
860 { rts_fabs, 1, "fabs", 1, _C89 },
861 { rts_sqrt, 1, "sqrt", 1, _C89 },
862 { rts_cbrt, 1, "cbrt", 1, _C99 },
863 { rts_exp, 1, "exp", 1, _C89 },
864 { rts_exp2, 1, "exp2", 1, _C89 },
865 { rts_exp10, 1, "exp10", 1, _GNUC },
866 { rts_log, 1, "log", 1, _C89 },
867 { rts_log2, 1, "log2", 1, _C89 },
868 { rts_log10, 1, "log10", 1, _C89 },
869 { rts_pow, 1, "pow", 2, _C89 },
870 { rts_sin, 1, "sin", 1, _C89 },
871 { rts_cos, 1, "cos", 1, _C89 },
872 { rts_tan, 1, "tan", 1, _C89 },
873 { rts_asin, 1, "asin", 1, _C89 },
874 { rts_acos, 1, "acos", 1, _C89 },
875 { rts_atan, 1, "atan", 1, _C89 },
876 { rts_sinh, 1, "sinh", 1, _C89 },
877 { rts_cosh, 1, "cosh", 1, _C89 },
878 { rts_tanh, 1, "tanh", 1, _C89 },
880 { rts_fabsf, 1, "fabsf", 1, _C99 },
881 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
882 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
883 { rts_expf, 1, "expf", 1, _C99 },
884 { rts_exp2f, 1, "exp2f", 1, _C99 },
885 { rts_exp10f, 1, "exp10f", 1, _GNUC },
886 { rts_logf, 1, "logf", 1, _C99 },
887 { rts_log2f, 1, "log2f", 1, _C99 },
888 { rts_log10f, 1, "log10f", 1, _C99 },
889 { rts_powf, 1, "powf", 2, _C99 },
890 { rts_sinf, 1, "sinf", 1, _C99 },
891 { rts_cosf, 1, "cosf", 1, _C99 },
892 { rts_tanf, 1, "tanf", 1, _C99 },
893 { rts_asinf, 1, "asinf", 1, _C99 },
894 { rts_acosf, 1, "acosf", 1, _C99 },
895 { rts_atanf, 1, "atanf", 1, _C99 },
896 { rts_sinhf, 1, "sinhf", 1, _C99 },
897 { rts_coshf, 1, "coshf", 1, _C99 },
898 { rts_tanhf, 1, "tanhf", 1, _C99 },
900 { rts_fabsl, 1, "fabsl", 1, _C99 },
901 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
902 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
903 { rts_expl, 1, "expl", 1, _C99 },
904 { rts_exp2l, 1, "exp2l", 1, _C99 },
905 { rts_exp10l, 1, "exp10l", 1, _GNUC },
906 { rts_logl, 1, "logl", 1, _C99 },
907 { rts_log2l, 1, "log2l", 1, _C99 },
908 { rts_log10l, 1, "log10l", 1, _C99 },
909 { rts_powl, 1, "powl", 2, _C99 },
910 { rts_sinl, 1, "sinl", 1, _C99 },
911 { rts_cosl, 1, "cosl", 1, _C99 },
912 { rts_tanl, 1, "tanl", 1, _C99 },
913 { rts_asinl, 1, "asinl", 1, _C99 },
914 { rts_acosl, 1, "acosl", 1, _C99 },
915 { rts_atanl, 1, "atanl", 1, _C99 },
916 { rts_sinhl, 1, "sinhl", 1, _C99 },
917 { rts_coshl, 1, "coshl", 1, _C99 },
918 { rts_tanhl, 1, "tanhl", 1, _C99 },
920 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
921 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
922 { rts_strcmp, 1, "strcmp", 2, _C89 },
923 { rts_strncmp, 1, "strncmp", 3, _C89 }
926 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
929 * Mangles an entity linker (ld) name for win32 usage.
931 * @param ent the entity to be mangled
932 * @param declaration the declaration
934 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
938 if (is_Method_type(get_entity_type(ent)))
939 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
941 /* always add an underscore in win32 */
942 id = mangle(id_underscore, get_entity_ident(ent));
945 decl_modifiers_t decl_modifiers = declaration->modifiers;
946 if (decl_modifiers & DM_DLLIMPORT) {
947 /* add prefix for imported symbols */
948 id = mangle(id_imp, id);
954 * Mangles an entity linker (ld) name for Linux ELF usage.
956 * @param ent the entity to be mangled
957 * @param declaration the declaration
959 static ident *create_ld_ident_linux_elf(ir_entity *entity,
960 declaration_t *declaration)
963 return get_entity_ident(entity);
967 * Mangles an entity linker (ld) name for Mach-O usage.
969 * @param ent the entity to be mangled
970 * @param declaration the declaration
972 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
975 ident *id = mangle(id_underscore, get_entity_ident(ent));
979 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
980 declaration_t *declaration);
981 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
984 * Handle GNU attributes for entities
986 * @param ent the entity
987 * @param decl the routine declaration
989 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
991 if (decl->modifiers & DM_PURE) {
992 /* TRUE if the declaration includes the GNU
993 __attribute__((pure)) specifier. */
994 set_entity_additional_property(ent, mtp_property_pure);
996 if (decl->modifiers & DM_USED) {
997 /* TRUE if the declaration includes the GNU
998 __attribute__((used)) specifier. */
999 set_entity_stickyness(ent, stickyness_sticky);
1004 * Creates an entity representing a function.
1006 * @param declaration the function declaration
1008 static ir_entity *get_function_entity(declaration_t *declaration)
1010 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
1011 return declaration->v.entity;
1012 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1014 symbol_t *symbol = declaration->symbol;
1015 ident *id = new_id_from_str(symbol->string);
1017 ir_type *global_type = get_glob_type();
1018 ir_type *ir_type_method = get_ir_type(declaration->type);
1019 assert(is_Method_type(ir_type_method));
1021 if (declaration->modifiers & DM_CDECL) {
1022 set_method_calling_convention(ir_type_method, cc_fixed | cc_cdecl_set);
1023 } else if (declaration->modifiers & DM_FASTCALL) {
1024 set_method_calling_convention(ir_type_method, cc_fixed | cc_fastcall_set);
1025 } else if (declaration->modifiers & DM_STDCALL) {
1026 set_method_calling_convention(ir_type_method, cc_fixed | cc_stdcall_set);
1029 /* already an entity defined? */
1030 ir_entity *entity = entitymap_get(&entitymap, symbol);
1031 if (entity != NULL) {
1032 if (get_entity_visibility(entity) == visibility_external_allocated
1033 && declaration->init.statement != NULL) {
1034 set_entity_visibility(entity, visibility_external_visible);
1036 goto entity_created;
1039 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1040 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1041 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1043 handle_gnu_attributes_ent(entity, declaration);
1045 /* static inline => local
1046 * extern inline => local
1047 * inline without definition => local
1048 * inline with definition => external_visible */
1049 storage_class_tag_t const storage_class = declaration->storage_class;
1050 bool const is_inline = declaration->is_inline;
1051 bool const has_body = declaration->init.statement != NULL;
1052 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1053 set_entity_visibility(entity, visibility_external_visible);
1054 } else if (storage_class == STORAGE_CLASS_STATIC ||
1055 (is_inline && has_body)) {
1057 /* this entity was declared, but is defined nowhere */
1058 set_entity_peculiarity(entity, peculiarity_description);
1060 set_entity_visibility(entity, visibility_local);
1061 } else if (has_body) {
1062 set_entity_visibility(entity, visibility_external_visible);
1064 set_entity_visibility(entity, visibility_external_allocated);
1066 set_entity_allocation(entity, allocation_static);
1068 /* We should check for file scope here, but as long as we compile C only
1069 this is not needed. */
1070 if (! firm_opt.freestanding) {
1071 /* check for a known runtime function */
1072 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1073 if (id != rts_idents[i])
1076 /* ignore those rts functions not necessary needed for current mode */
1077 if ((c_mode & rts_data[i].flags) == 0)
1079 assert(rts_entities[rts_data[i].id] == NULL);
1080 rts_entities[rts_data[i].id] = entity;
1084 entitymap_insert(&entitymap, symbol, entity);
1087 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1088 declaration->v.entity = entity;
1094 * Creates a Const node representing a constant.
1096 static ir_node *const_to_firm(const const_expression_t *cnst)
1098 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1099 ir_mode *mode = get_ir_mode(cnst->base.type);
1104 if (mode_is_float(mode)) {
1105 tv = new_tarval_from_double(cnst->v.float_value, mode);
1107 if (mode_is_signed(mode)) {
1108 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1110 len = snprintf(buf, sizeof(buf), "%llu",
1111 (unsigned long long) cnst->v.int_value);
1113 tv = new_tarval_from_str(buf, len, mode);
1116 return new_d_Const(dbgi, mode, tv);
1120 * Creates a Const node representing a character constant.
1122 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1124 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1125 ir_mode *mode = get_ir_mode(cnst->base.type);
1127 long long int v = 0;
1128 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1129 if (char_is_signed) {
1130 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1132 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1136 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1137 tarval *tv = new_tarval_from_str(buf, len, mode);
1139 return new_d_Const(dbgi, mode, tv);
1143 * Creates a Const node representing a wide character constant.
1145 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1147 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1148 ir_mode *mode = get_ir_mode(cnst->base.type);
1150 long long int v = cnst->v.wide_character.begin[0];
1153 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1154 tarval *tv = new_tarval_from_str(buf, len, mode);
1156 return new_d_Const(dbgi, mode, tv);
1160 * Creates a SymConst for a given entity.
1162 * @param dbgi debug info
1163 * @param mode the (reference) mode for the SymConst
1164 * @param entity the entity
1166 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1169 assert(entity != NULL);
1170 union symconst_symbol sym;
1171 sym.entity_p = entity;
1172 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1176 * Creates a SymConst node representing a string constant.
1178 * @param src_pos the source position of the string constant
1179 * @param id_prefix a prefix for the name of the generated string constant
1180 * @param value the value of the string constant
1182 static ir_node *string_to_firm(const source_position_t *const src_pos,
1183 const char *const id_prefix,
1184 const string_t *const value)
1186 ir_type *const global_type = get_glob_type();
1187 dbg_info *const dbgi = get_dbg_info(src_pos);
1188 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1189 ir_type_const_char, dbgi);
1191 ident *const id = id_unique(id_prefix);
1192 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1193 set_entity_ld_ident(entity, id);
1194 set_entity_variability(entity, variability_constant);
1195 set_entity_allocation(entity, allocation_static);
1197 ir_type *const elem_type = ir_type_const_char;
1198 ir_mode *const mode = get_type_mode(elem_type);
1200 const char* const string = value->begin;
1201 const size_t slen = value->size;
1203 set_array_lower_bound_int(type, 0, 0);
1204 set_array_upper_bound_int(type, 0, slen);
1205 set_type_size_bytes(type, slen);
1206 set_type_state(type, layout_fixed);
1208 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1209 for(size_t i = 0; i < slen; ++i) {
1210 tvs[i] = new_tarval_from_long(string[i], mode);
1213 set_array_entity_values(entity, tvs, slen);
1216 return create_symconst(dbgi, mode_P_data, entity);
1220 * Creates a SymConst node representing a string literal.
1222 * @param literal the string literal
1224 static ir_node *string_literal_to_firm(
1225 const string_literal_expression_t* literal)
1227 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1232 * Creates a SymConst node representing a wide string literal.
1234 * @param literal the wide string literal
1236 static ir_node *wide_string_literal_to_firm(
1237 const wide_string_literal_expression_t* const literal)
1239 ir_type *const global_type = get_glob_type();
1240 ir_type *const elem_type = ir_type_wchar_t;
1241 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1242 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1245 ident *const id = id_unique("Lstr.%u");
1246 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1247 set_entity_ld_ident(entity, id);
1248 set_entity_variability(entity, variability_constant);
1249 set_entity_allocation(entity, allocation_static);
1251 ir_mode *const mode = get_type_mode(elem_type);
1253 const wchar_rep_t *const string = literal->value.begin;
1254 const size_t slen = literal->value.size;
1256 set_array_lower_bound_int(type, 0, 0);
1257 set_array_upper_bound_int(type, 0, slen);
1258 set_type_size_bytes(type, slen);
1259 set_type_state(type, layout_fixed);
1261 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1262 for(size_t i = 0; i < slen; ++i) {
1263 tvs[i] = new_tarval_from_long(string[i], mode);
1266 set_array_entity_values(entity, tvs, slen);
1269 return create_symconst(dbgi, mode_P_data, entity);
1272 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1273 ir_node *const addr)
1275 ir_type *irtype = get_ir_type(type);
1276 if (is_compound_type(irtype)
1277 || is_Method_type(irtype)
1278 || is_Array_type(irtype)) {
1282 ir_mode *const mode = get_type_mode(irtype);
1283 ir_node *const memory = get_store();
1284 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1285 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1286 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1288 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1289 set_Load_volatility(load, volatility_is_volatile);
1292 set_store(load_mem);
1297 * Creates a strict Conv if neccessary.
1299 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1301 ir_mode *mode = get_irn_mode(node);
1303 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1305 if (!mode_is_float(mode))
1308 /* check if there is already a Conv */
1309 if (is_Conv(node)) {
1310 /* convert it into a strict Conv */
1311 set_Conv_strict(node, 1);
1315 /* otherwise create a new one */
1316 return new_d_strictConv(dbgi, node, mode);
1319 static ir_node *get_global_var_address(dbg_info *const dbgi,
1320 const declaration_t *const decl)
1322 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1324 ir_entity *const entity = decl->v.entity;
1325 switch ((storage_class_tag_t)decl->storage_class) {
1326 case STORAGE_CLASS_THREAD:
1327 case STORAGE_CLASS_THREAD_EXTERN:
1328 case STORAGE_CLASS_THREAD_STATIC: {
1329 ir_node *const no_mem = new_NoMem();
1330 ir_node *const tls = get_irg_tls(current_ir_graph);
1331 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1335 return create_symconst(dbgi, mode_P_data, entity);
1340 * Returns the correct base address depending on whether it is a parameter or a
1341 * normal local variable.
1343 static ir_node *get_local_frame(ir_entity *const ent)
1345 ir_graph *const irg = current_ir_graph;
1346 const ir_type *const owner = get_entity_owner(ent);
1347 if (owner == get_irg_frame_type(irg)) {
1348 return get_irg_frame(irg);
1350 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1351 return get_irg_value_param_base(irg);
1355 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1357 ir_mode *value_mode = get_irn_mode(value);
1359 if (value_mode == dest_mode || is_Bad(value))
1362 if (dest_mode == mode_b) {
1363 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1364 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1365 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1369 return new_d_Conv(dbgi, value, dest_mode);
1372 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1374 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1375 declaration_t *declaration = ref->declaration;
1376 type_t *type = skip_typeref(declaration->type);
1378 /* make sure the type is constructed */
1379 (void) get_ir_type(type);
1381 switch((declaration_kind_t) declaration->declaration_kind) {
1382 case DECLARATION_KIND_TYPE:
1383 case DECLARATION_KIND_UNKNOWN:
1386 case DECLARATION_KIND_ENUM_ENTRY: {
1387 ir_mode *const mode = get_ir_mode(type);
1388 return new_Const(mode, declaration->v.enum_val);
1391 case DECLARATION_KIND_LOCAL_VARIABLE: {
1392 ir_mode *const mode = get_ir_mode(type);
1393 return get_value(declaration->v.value_number, mode);
1395 case DECLARATION_KIND_FUNCTION: {
1396 ir_mode *const mode = get_ir_mode(type);
1397 return create_symconst(dbgi, mode, declaration->v.entity);
1399 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1400 ir_node *const addr = get_global_var_address(dbgi, declaration);
1401 return deref_address(dbgi, declaration->type, addr);
1404 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1405 ir_entity *entity = declaration->v.entity;
1406 ir_node *frame = get_local_frame(entity);
1407 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1408 return deref_address(dbgi, declaration->type, sel);
1411 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1412 return declaration->v.vla_base;
1414 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1415 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1416 case DECLARATION_KIND_COMPOUND_MEMBER:
1417 case DECLARATION_KIND_LABEL_BLOCK:
1418 panic("not implemented reference type");
1421 panic("reference to declaration with unknown type found");
1424 static ir_node *reference_addr(const reference_expression_t *ref)
1426 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1427 declaration_t *declaration = ref->declaration;
1429 switch((declaration_kind_t) declaration->declaration_kind) {
1430 case DECLARATION_KIND_TYPE:
1431 case DECLARATION_KIND_UNKNOWN:
1433 case DECLARATION_KIND_LOCAL_VARIABLE:
1434 /* you can store to a local variable (so we don't panic but return NULL
1435 * as an indicator for no real address) */
1437 case DECLARATION_KIND_FUNCTION: {
1438 type_t *const type = skip_typeref(ref->base.type);
1439 ir_mode *const mode = get_ir_mode(type);
1440 return create_symconst(dbgi, mode, declaration->v.entity);
1442 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1443 ir_node *const addr = get_global_var_address(dbgi, declaration);
1446 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1447 ir_entity *entity = declaration->v.entity;
1448 ir_node *frame = get_local_frame(entity);
1449 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1454 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1455 return declaration->v.vla_base;
1457 case DECLARATION_KIND_ENUM_ENTRY:
1458 panic("trying to reference enum entry");
1460 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1461 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1462 case DECLARATION_KIND_COMPOUND_MEMBER:
1463 case DECLARATION_KIND_LABEL_BLOCK:
1464 panic("not implemented reference type");
1467 panic("reference to declaration with unknown type found");
1471 * Transform calls to builtin functions.
1473 static ir_node *process_builtin_call(const call_expression_t *call)
1475 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1477 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1478 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1480 type_t *type = skip_typeref(builtin->base.type);
1481 assert(is_type_pointer(type));
1483 type_t *function_type = skip_typeref(type->pointer.points_to);
1484 symbol_t *symbol = builtin->symbol;
1486 switch(symbol->ID) {
1487 case T___builtin_alloca: {
1488 if (call->arguments == NULL || call->arguments->next != NULL) {
1489 panic("invalid number of parameters on __builtin_alloca");
1491 expression_t *argument = call->arguments->expression;
1492 ir_node *size = expression_to_firm(argument);
1494 ir_node *store = get_store();
1495 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1497 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1499 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1503 case T___builtin_huge_val: {
1504 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1505 tarval *tv = get_mode_infinite(mode);
1506 ir_node *res = new_d_Const(dbgi, mode, tv);
1509 case T___builtin_nan:
1510 case T___builtin_nanf:
1511 case T___builtin_nand: {
1512 /* Ignore string for now... */
1513 assert(is_type_function(function_type));
1514 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1515 tarval *tv = get_mode_NAN(mode);
1516 ir_node *res = new_d_Const(dbgi, mode, tv);
1519 case T___builtin_va_end:
1522 panic("Unsupported builtin found\n");
1527 * Transform a call expression.
1528 * Handles some special cases, like alloca() calls, which must be resolved
1529 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1530 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1533 static ir_node *call_expression_to_firm(const call_expression_t *call)
1535 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1536 assert(get_cur_block() != NULL);
1538 expression_t *function = call->function;
1539 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1540 return process_builtin_call(call);
1542 if (function->kind == EXPR_REFERENCE) {
1543 const reference_expression_t *ref = &function->reference;
1544 declaration_t *declaration = ref->declaration;
1546 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1547 if (declaration->v.entity == rts_entities[rts_alloca]) {
1548 /* handle alloca() call */
1549 expression_t *argument = call->arguments->expression;
1550 ir_node *size = expression_to_firm(argument);
1552 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1554 ir_node *store = get_store();
1555 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1556 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1558 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1560 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1566 ir_node *callee = expression_to_firm(function);
1568 type_t *type = skip_typeref(function->base.type);
1569 assert(is_type_pointer(type));
1570 pointer_type_t *pointer_type = &type->pointer;
1571 type_t *points_to = skip_typeref(pointer_type->points_to);
1572 assert(is_type_function(points_to));
1573 function_type_t *function_type = &points_to->function;
1575 int n_parameters = 0;
1576 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1577 ir_type *new_method_type = NULL;
1578 if (function_type->variadic || function_type->unspecified_parameters) {
1579 const call_argument_t *argument = call->arguments;
1580 for( ; argument != NULL; argument = argument->next) {
1584 /* we need to construct a new method type matching the call
1586 int n_res = get_method_n_ress(ir_method_type);
1587 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1588 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1589 n_parameters, n_res, dbgi);
1590 set_method_calling_convention(new_method_type,
1591 get_method_calling_convention(ir_method_type));
1592 set_method_additional_properties(new_method_type,
1593 get_method_additional_properties(ir_method_type));
1594 set_method_variadicity(new_method_type,
1595 get_method_variadicity(ir_method_type));
1597 for(int i = 0; i < n_res; ++i) {
1598 set_method_res_type(new_method_type, i,
1599 get_method_res_type(ir_method_type, i));
1601 argument = call->arguments;
1602 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1603 expression_t *expression = argument->expression;
1604 ir_type *irtype = get_ir_type(expression->base.type);
1605 set_method_param_type(new_method_type, i, irtype);
1607 ir_method_type = new_method_type;
1609 n_parameters = get_method_n_params(ir_method_type);
1612 ir_node *in[n_parameters];
1614 const call_argument_t *argument = call->arguments;
1615 for(int n = 0; n < n_parameters; ++n) {
1616 expression_t *expression = argument->expression;
1617 ir_node *arg_node = expression_to_firm(expression);
1619 arg_node = do_strict_conv(dbgi, arg_node);
1623 argument = argument->next;
1626 ir_node *store = get_store();
1627 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1629 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1632 type_t *return_type = skip_typeref(function_type->return_type);
1633 ir_node *result = NULL;
1635 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1637 if (is_type_scalar(return_type)) {
1638 mode = get_ir_mode(return_type);
1642 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1643 result = new_d_Proj(dbgi, resproj, mode, 0);
1649 static void statement_to_firm(statement_t *statement);
1650 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1652 static ir_node *expression_to_addr(const expression_t *expression);
1653 static void create_condition_evaluation(const expression_t *expression,
1654 ir_node *true_block,
1655 ir_node *false_block);
1657 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1660 value = do_strict_conv(dbgi, value);
1662 ir_node *memory = get_store();
1664 if (is_type_scalar(type)) {
1665 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1666 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1667 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1668 set_Store_volatility(store, volatility_is_volatile);
1669 set_store(store_mem);
1671 ir_type *irtype = get_ir_type(type);
1672 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1673 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1674 set_store(copyb_mem);
1678 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1680 tarval *all_one = get_mode_all_one(mode);
1681 int mode_size = get_mode_size_bits(mode);
1683 assert(offset >= 0 && size >= 0);
1684 assert(offset + size <= mode_size);
1685 if (size == mode_size) {
1689 long shiftr = get_mode_size_bits(mode) - size;
1690 long shiftl = offset;
1691 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1692 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1693 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1694 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1699 static void bitfield_store_to_firm(const select_expression_t *expression,
1700 ir_node *addr, ir_node *value)
1702 type_t *type = expression->base.type;
1703 ir_mode *mode = get_ir_mode(type);
1705 assert(get_irn_mode(value) == mode || is_Bad(value));
1707 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1709 /* kill upper bits of value and shift to right position */
1710 ir_entity *entity = expression->compound_entry->v.entity;
1711 int bitoffset = get_entity_offset_bits_remainder(entity);
1712 ir_type *entity_type = get_entity_type(entity);
1713 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1715 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1716 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1717 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1718 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1719 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1720 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1722 /* load current value */
1723 ir_node *mem = get_store();
1724 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1725 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1726 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1727 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1728 tarval *inv_mask = tarval_not(shift_mask);
1729 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1730 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1732 /* construct new value and store */
1733 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1734 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1735 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1736 set_store(store_mem);
1738 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1739 set_Load_volatility(load, volatility_is_volatile);
1740 set_Store_volatility(store, volatility_is_volatile);
1744 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1747 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1748 type_t *type = expression->base.type;
1749 ir_mode *mode = get_ir_mode(type);
1750 ir_node *mem = get_store();
1751 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1752 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1753 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1755 load_res = create_conv(dbgi, load_res, mode_int);
1757 set_store(load_mem);
1759 /* kill upper bits */
1760 ir_entity *entity = expression->compound_entry->v.entity;
1761 int bitoffset = get_entity_offset_bits_remainder(entity);
1762 ir_type *entity_type = get_entity_type(entity);
1763 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1764 long shift_bitsl = machine_size - bitoffset - bitsize;
1765 assert(shift_bitsl >= 0);
1766 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1767 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1768 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1770 long shift_bitsr = bitoffset + shift_bitsl;
1771 assert(shift_bitsr <= (long) machine_size);
1772 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1773 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1775 if (mode_is_signed(mode)) {
1776 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1778 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1781 return create_conv(dbgi, shiftr, mode);
1784 static void set_value_for_expression_addr(const expression_t *expression,
1785 ir_node *value, ir_node *addr)
1787 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1788 value = do_strict_conv(dbgi, value);
1790 if (expression->kind == EXPR_REFERENCE) {
1791 const reference_expression_t *ref = &expression->reference;
1793 declaration_t *declaration = ref->declaration;
1794 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1795 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1796 set_value(declaration->v.value_number, value);
1802 addr = expression_to_addr(expression);
1804 type_t *type = skip_typeref(expression->base.type);
1806 if (expression->kind == EXPR_SELECT) {
1807 const select_expression_t *select = &expression->select;
1809 declaration_t *declaration = select->compound_entry;
1810 if (declaration->type->kind == TYPE_BITFIELD) {
1811 bitfield_store_to_firm(select, addr, value);
1816 assign_value(dbgi, addr, type, value);
1819 static void set_value_for_expression(const expression_t *expression,
1822 set_value_for_expression_addr(expression, value, NULL);
1825 static ir_node *get_value_from_lvalue(const expression_t *expression,
1828 if (expression->kind == EXPR_REFERENCE) {
1829 const reference_expression_t *ref = &expression->reference;
1831 declaration_t *declaration = ref->declaration;
1832 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1833 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1834 assert(addr == NULL);
1835 ir_mode *mode = get_ir_mode(expression->base.type);
1836 return get_value(declaration->v.value_number, mode);
1840 assert(addr != NULL);
1841 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1844 if (expression->kind == EXPR_SELECT &&
1845 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1846 value = bitfield_extract_to_firm(&expression->select, addr);
1848 value = deref_address(dbgi, expression->base.type, addr);
1855 static ir_node *create_incdec(const unary_expression_t *expression)
1857 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1858 const expression_t *value_expr = expression->value;
1859 ir_node *addr = expression_to_addr(value_expr);
1860 ir_node *value = get_value_from_lvalue(value_expr, addr);
1862 type_t *type = skip_typeref(expression->base.type);
1863 ir_mode *mode = get_ir_mode(expression->base.type);
1866 if (is_type_pointer(type)) {
1867 pointer_type_t *pointer_type = &type->pointer;
1868 offset = get_type_size(pointer_type->points_to);
1870 assert(is_type_arithmetic(type));
1871 offset = new_Const(mode, get_mode_one(mode));
1875 ir_node *store_value;
1876 switch(expression->base.kind) {
1877 case EXPR_UNARY_POSTFIX_INCREMENT:
1879 store_value = new_d_Add(dbgi, value, offset, mode);
1881 case EXPR_UNARY_POSTFIX_DECREMENT:
1883 store_value = new_d_Sub(dbgi, value, offset, mode);
1885 case EXPR_UNARY_PREFIX_INCREMENT:
1886 result = new_d_Add(dbgi, value, offset, mode);
1887 store_value = result;
1889 case EXPR_UNARY_PREFIX_DECREMENT:
1890 result = new_d_Sub(dbgi, value, offset, mode);
1891 store_value = result;
1894 panic("no incdec expr in create_incdec");
1897 set_value_for_expression_addr(value_expr, store_value, addr);
1902 static bool is_local_variable(expression_t *expression)
1904 if (expression->kind != EXPR_REFERENCE)
1906 reference_expression_t *ref_expr = &expression->reference;
1907 declaration_t *declaration = ref_expr->declaration;
1908 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1911 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1914 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1915 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1916 case EXPR_BINARY_NOTEQUAL:
1917 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1918 case EXPR_BINARY_ISLESS:
1919 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1920 case EXPR_BINARY_ISLESSEQUAL:
1921 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1922 case EXPR_BINARY_ISGREATER:
1923 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1924 case EXPR_BINARY_ISGREATEREQUAL:
1925 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1926 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1931 panic("trying to get pn_Cmp from non-comparison binexpr type");
1935 * Handle the assume optimizer hint: check if a Confirm
1936 * node can be created.
1938 * @param dbi debug info
1939 * @param expr the IL assume expression
1941 * we support here only some simple cases:
1946 static ir_node *handle_assume_compare(dbg_info *dbi,
1947 const binary_expression_t *expression)
1949 expression_t *op1 = expression->left;
1950 expression_t *op2 = expression->right;
1951 declaration_t *var2, *var = NULL;
1952 ir_node *res = NULL;
1955 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1957 if (is_local_variable(op1) && is_local_variable(op2)) {
1958 var = op1->reference.declaration;
1959 var2 = op2->reference.declaration;
1961 type_t *const type = skip_typeref(var->type);
1962 ir_mode *const mode = get_ir_mode(type);
1964 ir_node *const irn1 = get_value(var->v.value_number, mode);
1965 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1967 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1968 set_value(var2->v.value_number, res);
1970 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1971 set_value(var->v.value_number, res);
1977 if (is_local_variable(op1) && is_constant_expression(op2)) {
1978 var = op1->reference.declaration;
1980 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1981 cmp_val = get_inversed_pnc(cmp_val);
1982 var = op2->reference.declaration;
1987 type_t *const type = skip_typeref(var->type);
1988 ir_mode *const mode = get_ir_mode(type);
1990 res = get_value(var->v.value_number, mode);
1991 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1992 set_value(var->v.value_number, res);
1998 * Handle the assume optimizer hint.
2000 * @param dbi debug info
2001 * @param expr the IL assume expression
2003 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2004 switch(expression->kind) {
2005 case EXPR_BINARY_EQUAL:
2006 case EXPR_BINARY_NOTEQUAL:
2007 case EXPR_BINARY_LESS:
2008 case EXPR_BINARY_LESSEQUAL:
2009 case EXPR_BINARY_GREATER:
2010 case EXPR_BINARY_GREATEREQUAL:
2011 return handle_assume_compare(dbi, &expression->binary);
2017 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2019 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2020 type_t *type = skip_typeref(expression->base.type);
2022 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2023 return expression_to_addr(expression->value);
2025 const expression_t *value = expression->value;
2027 switch(expression->base.kind) {
2028 case EXPR_UNARY_NEGATE: {
2029 ir_node *value_node = expression_to_firm(value);
2030 ir_mode *mode = get_ir_mode(type);
2031 return new_d_Minus(dbgi, value_node, mode);
2033 case EXPR_UNARY_PLUS:
2034 return expression_to_firm(value);
2035 case EXPR_UNARY_BITWISE_NEGATE: {
2036 ir_node *value_node = expression_to_firm(value);
2037 ir_mode *mode = get_ir_mode(type);
2038 return new_d_Not(dbgi, value_node, mode);
2040 case EXPR_UNARY_NOT: {
2041 ir_node *value_node = _expression_to_firm(value);
2042 value_node = create_conv(dbgi, value_node, mode_b);
2043 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2046 case EXPR_UNARY_DEREFERENCE: {
2047 ir_node *value_node = expression_to_firm(value);
2048 type_t *value_type = skip_typeref(value->base.type);
2049 assert(is_type_pointer(value_type));
2050 type_t *points_to = value_type->pointer.points_to;
2051 return deref_address(dbgi, points_to, value_node);
2053 case EXPR_UNARY_POSTFIX_INCREMENT:
2054 case EXPR_UNARY_POSTFIX_DECREMENT:
2055 case EXPR_UNARY_PREFIX_INCREMENT:
2056 case EXPR_UNARY_PREFIX_DECREMENT:
2057 return create_incdec(expression);
2058 case EXPR_UNARY_CAST: {
2059 ir_node *value_node = expression_to_firm(value);
2060 if (is_type_scalar(type)) {
2061 ir_mode *mode = get_ir_mode(type);
2062 ir_node *node = create_conv(dbgi, value_node, mode);
2063 node = do_strict_conv(dbgi, node);
2066 /* make sure firm type is constructed */
2067 (void) get_ir_type(type);
2071 case EXPR_UNARY_CAST_IMPLICIT: {
2072 ir_node *value_node = expression_to_firm(value);
2073 if (is_type_scalar(type)) {
2074 ir_mode *mode = get_ir_mode(type);
2075 return create_conv(dbgi, value_node, mode);
2080 case EXPR_UNARY_ASSUME:
2081 if (firm_opt.confirm)
2082 return handle_assume(dbgi, value);
2089 panic("invalid UNEXPR type found");
2093 * produces a 0/1 depending of the value of a mode_b node
2095 static ir_node *produce_condition_result(const expression_t *expression,
2098 ir_mode *mode = get_ir_mode(expression->base.type);
2099 ir_node *cur_block = get_cur_block();
2101 ir_node *one_block = new_immBlock();
2102 ir_node *one = new_Const(mode, get_mode_one(mode));
2103 ir_node *jmp_one = new_d_Jmp(dbgi);
2105 ir_node *zero_block = new_immBlock();
2106 ir_node *zero = new_Const(mode, get_mode_null(mode));
2107 ir_node *jmp_zero = new_d_Jmp(dbgi);
2109 set_cur_block(cur_block);
2110 create_condition_evaluation(expression, one_block, zero_block);
2111 mature_immBlock(one_block);
2112 mature_immBlock(zero_block);
2114 ir_node *common_block = new_immBlock();
2115 add_immBlock_pred(common_block, jmp_one);
2116 add_immBlock_pred(common_block, jmp_zero);
2117 mature_immBlock(common_block);
2119 ir_node *in[2] = { one, zero };
2120 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2125 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2126 ir_node *value, type_t *type)
2128 pointer_type_t *const pointer_type = &type->pointer;
2129 type_t *const points_to = skip_typeref(pointer_type->points_to);
2130 unsigned elem_size = get_type_size_const(points_to);
2133 if (elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
2137 assert(elem_size >= 1);
2141 value = create_conv(dbgi, value, mode_int);
2142 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2143 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2147 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2148 ir_node *left, ir_node *right)
2151 type_t *type_left = skip_typeref(expression->left->base.type);
2152 type_t *type_right = skip_typeref(expression->right->base.type);
2154 expression_kind_t kind = expression->base.kind;
2157 case EXPR_BINARY_SHIFTLEFT:
2158 case EXPR_BINARY_SHIFTRIGHT:
2159 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2160 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2161 mode = get_irn_mode(left);
2162 right = create_conv(dbgi, right, mode_uint);
2165 case EXPR_BINARY_SUB:
2166 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2167 const pointer_type_t *const ptr_type = &type_left->pointer;
2169 mode = get_ir_mode(expression->base.type);
2170 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2171 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2172 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2173 ir_node *const no_mem = new_NoMem();
2174 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2175 mode, op_pin_state_floats);
2176 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2179 case EXPR_BINARY_SUB_ASSIGN:
2180 if (is_type_pointer(type_left)) {
2181 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2182 mode = get_ir_mode(type_left);
2187 case EXPR_BINARY_ADD:
2188 case EXPR_BINARY_ADD_ASSIGN:
2189 if (is_type_pointer(type_left)) {
2190 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2191 mode = get_ir_mode(type_left);
2193 } else if (is_type_pointer(type_right)) {
2194 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2195 mode = get_ir_mode(type_right);
2202 mode = get_irn_mode(right);
2203 left = create_conv(dbgi, left, mode);
2208 case EXPR_BINARY_ADD_ASSIGN:
2209 case EXPR_BINARY_ADD:
2210 return new_d_Add(dbgi, left, right, mode);
2211 case EXPR_BINARY_SUB_ASSIGN:
2212 case EXPR_BINARY_SUB:
2213 return new_d_Sub(dbgi, left, right, mode);
2214 case EXPR_BINARY_MUL_ASSIGN:
2215 case EXPR_BINARY_MUL:
2216 return new_d_Mul(dbgi, left, right, mode);
2217 case EXPR_BINARY_BITWISE_AND:
2218 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2219 return new_d_And(dbgi, left, right, mode);
2220 case EXPR_BINARY_BITWISE_OR:
2221 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2222 return new_d_Or(dbgi, left, right, mode);
2223 case EXPR_BINARY_BITWISE_XOR:
2224 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2225 return new_d_Eor(dbgi, left, right, mode);
2226 case EXPR_BINARY_SHIFTLEFT:
2227 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2228 return new_d_Shl(dbgi, left, right, mode);
2229 case EXPR_BINARY_SHIFTRIGHT:
2230 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2231 if (mode_is_signed(mode)) {
2232 return new_d_Shrs(dbgi, left, right, mode);
2234 return new_d_Shr(dbgi, left, right, mode);
2236 case EXPR_BINARY_DIV:
2237 case EXPR_BINARY_DIV_ASSIGN: {
2238 ir_node *pin = new_Pin(new_NoMem());
2241 if (mode_is_float(mode)) {
2242 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2243 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2245 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2246 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2250 case EXPR_BINARY_MOD:
2251 case EXPR_BINARY_MOD_ASSIGN: {
2252 ir_node *pin = new_Pin(new_NoMem());
2253 assert(!mode_is_float(mode));
2254 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2255 op_pin_state_floats);
2256 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2260 panic("unexpected expression kind");
2264 static ir_node *create_lazy_op(const binary_expression_t *expression)
2266 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2267 type_t *type = expression->base.type;
2268 ir_mode *mode = get_ir_mode(type);
2270 if (is_constant_expression(expression->left)) {
2271 long val = fold_constant(expression->left);
2272 expression_kind_t ekind = expression->base.kind;
2273 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
2274 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2275 return expression_to_firm(expression->right);
2277 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
2278 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
2279 return new_Const(mode, get_mode_one(mode));
2283 return produce_condition_result((const expression_t*) expression, dbgi);
2286 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2287 ir_node *right, ir_mode *mode);
2289 static ir_node *create_assign_binop(const binary_expression_t *expression)
2291 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2292 const expression_t *left_expr = expression->left;
2293 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2294 ir_node *left_addr = expression_to_addr(left_expr);
2295 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2296 ir_node *right = expression_to_firm(expression->right);
2297 ir_node *result = create_op(dbgi, expression, left, right);
2299 result = create_conv(dbgi, result, left_mode);
2300 result = do_strict_conv(dbgi, result);
2302 set_value_for_expression_addr(left_expr, result, left_addr);
2307 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2309 expression_kind_t kind = expression->base.kind;
2312 case EXPR_BINARY_EQUAL:
2313 case EXPR_BINARY_NOTEQUAL:
2314 case EXPR_BINARY_LESS:
2315 case EXPR_BINARY_LESSEQUAL:
2316 case EXPR_BINARY_GREATER:
2317 case EXPR_BINARY_GREATEREQUAL:
2318 case EXPR_BINARY_ISGREATER:
2319 case EXPR_BINARY_ISGREATEREQUAL:
2320 case EXPR_BINARY_ISLESS:
2321 case EXPR_BINARY_ISLESSEQUAL:
2322 case EXPR_BINARY_ISLESSGREATER:
2323 case EXPR_BINARY_ISUNORDERED: {
2324 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2325 ir_node *left = expression_to_firm(expression->left);
2326 ir_node *right = expression_to_firm(expression->right);
2327 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2328 long pnc = get_pnc(kind, expression->left->base.type);
2329 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2332 case EXPR_BINARY_ASSIGN: {
2333 ir_node *addr = expression_to_addr(expression->left);
2334 ir_node *right = expression_to_firm(expression->right);
2335 set_value_for_expression_addr(expression->left, right, addr);
2339 case EXPR_BINARY_ADD:
2340 case EXPR_BINARY_SUB:
2341 case EXPR_BINARY_MUL:
2342 case EXPR_BINARY_DIV:
2343 case EXPR_BINARY_MOD:
2344 case EXPR_BINARY_BITWISE_AND:
2345 case EXPR_BINARY_BITWISE_OR:
2346 case EXPR_BINARY_BITWISE_XOR:
2347 case EXPR_BINARY_SHIFTLEFT:
2348 case EXPR_BINARY_SHIFTRIGHT:
2350 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2351 ir_node *left = expression_to_firm(expression->left);
2352 ir_node *right = expression_to_firm(expression->right);
2353 return create_op(dbgi, expression, left, right);
2355 case EXPR_BINARY_LOGICAL_AND:
2356 case EXPR_BINARY_LOGICAL_OR:
2357 return create_lazy_op(expression);
2358 case EXPR_BINARY_COMMA:
2359 /* create side effects of left side */
2360 (void) expression_to_firm(expression->left);
2361 return _expression_to_firm(expression->right);
2363 case EXPR_BINARY_ADD_ASSIGN:
2364 case EXPR_BINARY_SUB_ASSIGN:
2365 case EXPR_BINARY_MUL_ASSIGN:
2366 case EXPR_BINARY_MOD_ASSIGN:
2367 case EXPR_BINARY_DIV_ASSIGN:
2368 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2369 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2370 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2371 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2372 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2373 return create_assign_binop(expression);
2374 case EXPR_BINARY_BUILTIN_EXPECT:
2375 return _expression_to_firm(expression->left);
2377 panic("TODO binexpr type");
2381 static ir_node *array_access_addr(const array_access_expression_t *expression)
2383 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2384 ir_node *base_addr = expression_to_firm(expression->array_ref);
2385 ir_node *offset = expression_to_firm(expression->index);
2387 type_t *offset_type = skip_typeref(expression->index->base.type);
2389 if (is_type_signed(offset_type)) {
2390 mode = get_ir_mode(type_ssize_t);
2392 mode = get_ir_mode(type_size_t);
2394 offset = create_conv(dbgi, offset, mode);
2396 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2397 assert(is_type_pointer(ref_type));
2398 pointer_type_t *pointer_type = &ref_type->pointer;
2400 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2401 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2402 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2404 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2409 static ir_node *array_access_to_firm(
2410 const array_access_expression_t *expression)
2412 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2413 ir_node *addr = array_access_addr(expression);
2414 type_t *type = revert_automatic_type_conversion(
2415 (const expression_t*) expression);
2416 type = skip_typeref(type);
2418 return deref_address(dbgi, type, addr);
2421 static long get_offsetof_offset(const offsetof_expression_t *expression)
2423 type_t *orig_type = expression->type;
2426 designator_t *designator = expression->designator;
2427 for( ; designator != NULL; designator = designator->next) {
2428 type_t *type = skip_typeref(orig_type);
2429 /* be sure the type is constructed */
2430 (void) get_ir_type(type);
2432 if (designator->symbol != NULL) {
2433 assert(is_type_compound(type));
2434 symbol_t *symbol = designator->symbol;
2436 declaration_t *declaration = type->compound.declaration;
2437 declaration_t *iter = declaration->scope.declarations;
2438 for( ; iter != NULL; iter = iter->next) {
2439 if (iter->symbol == symbol) {
2443 assert(iter != NULL);
2445 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2446 offset += get_entity_offset(iter->v.entity);
2448 orig_type = iter->type;
2450 expression_t *array_index = designator->array_index;
2451 assert(designator->array_index != NULL);
2452 assert(is_type_array(type));
2453 assert(is_type_valid(array_index->base.type));
2455 long index = fold_constant(array_index);
2456 ir_type *arr_type = get_ir_type(type);
2457 ir_type *elem_type = get_array_element_type(arr_type);
2458 long elem_size = get_type_size_bytes(elem_type);
2460 offset += index * elem_size;
2462 orig_type = type->array.element_type;
2469 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2471 ir_mode *mode = get_ir_mode(expression->base.type);
2472 long offset = get_offsetof_offset(expression);
2473 tarval *tv = new_tarval_from_long(offset, mode);
2474 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2476 return new_d_Const(dbgi, mode, tv);
2479 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2480 ir_entity *entity, type_t *type);
2482 static ir_node *compound_literal_to_firm(
2483 const compound_literal_expression_t *expression)
2485 type_t *type = expression->type;
2487 /* create an entity on the stack */
2488 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2490 ident *const id = id_unique("CompLit.%u");
2491 ir_type *const irtype = get_ir_type(type);
2492 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2493 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2494 set_entity_ld_ident(entity, id);
2496 set_entity_variability(entity, variability_uninitialized);
2498 /* create initialisation code */
2499 initializer_t *initializer = expression->initializer;
2500 create_local_initializer(initializer, dbgi, entity, type);
2502 /* create a sel for the compound literal address */
2503 ir_node *frame = get_local_frame(entity);
2504 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2509 * Transform a sizeof expression into Firm code.
2511 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2513 type_t *type = expression->type;
2515 type = expression->tp_expression->base.type;
2516 assert(type != NULL);
2519 type = skip_typeref(type);
2520 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2521 if (is_type_array(type) && type->array.is_vla
2522 && expression->tp_expression != NULL) {
2523 expression_to_firm(expression->tp_expression);
2526 return get_type_size(type);
2530 * Transform an alignof expression into Firm code.
2532 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2534 type_t *type = expression->type;
2536 /* beware: if expression is a variable reference, return the
2537 alignment of the variable. */
2538 const expression_t *tp_expression = expression->tp_expression;
2539 const declaration_t *declaration = expr_is_variable(tp_expression);
2540 if (declaration != NULL) {
2541 /* TODO: get the alignment of this variable. */
2543 type = tp_expression->base.type;
2544 assert(type != NULL);
2547 ir_mode *const mode = get_ir_mode(expression->base.type);
2548 symconst_symbol sym;
2549 sym.type_p = get_ir_type(type);
2550 return new_SymConst(mode, sym, symconst_type_align);
2553 static void init_ir_types(void);
2554 long fold_constant(const expression_t *expression)
2556 bool constant_folding_old = constant_folding;
2557 constant_folding = true;
2561 assert(is_constant_expression(expression));
2563 ir_graph *old_current_ir_graph = current_ir_graph;
2564 if (current_ir_graph == NULL) {
2565 current_ir_graph = get_const_code_irg();
2568 ir_node *cnst = expression_to_firm(expression);
2569 current_ir_graph = old_current_ir_graph;
2571 if (!is_Const(cnst)) {
2572 panic("couldn't fold constant\n");
2575 tarval *tv = get_Const_tarval(cnst);
2576 if (!tarval_is_long(tv)) {
2577 panic("result of constant folding is not integer\n");
2580 constant_folding = constant_folding_old;
2582 return get_tarval_long(tv);
2585 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2587 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2589 /* first try to fold a constant condition */
2590 if (is_constant_expression(expression->condition)) {
2591 long val = fold_constant(expression->condition);
2593 return expression_to_firm(expression->true_expression);
2595 return expression_to_firm(expression->false_expression);
2599 ir_node *cur_block = get_cur_block();
2601 /* create the true block */
2602 ir_node *true_block = new_immBlock();
2604 ir_node *true_val = expression_to_firm(expression->true_expression);
2605 ir_node *true_jmp = new_Jmp();
2607 /* create the false block */
2608 ir_node *false_block = new_immBlock();
2610 ir_node *false_val = expression_to_firm(expression->false_expression);
2611 ir_node *false_jmp = new_Jmp();
2613 /* create the condition evaluation */
2614 set_cur_block(cur_block);
2615 create_condition_evaluation(expression->condition, true_block, false_block);
2616 mature_immBlock(true_block);
2617 mature_immBlock(false_block);
2619 /* create the common block */
2620 ir_node *common_block = new_immBlock();
2621 add_immBlock_pred(common_block, true_jmp);
2622 add_immBlock_pred(common_block, false_jmp);
2623 mature_immBlock(common_block);
2625 /* TODO improve static semantics, so either both or no values are NULL */
2626 if (true_val == NULL || false_val == NULL)
2629 ir_node *in[2] = { true_val, false_val };
2630 ir_mode *mode = get_irn_mode(true_val);
2631 assert(get_irn_mode(false_val) == mode);
2632 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2638 * Returns an IR-node representing the address of a field.
2640 static ir_node *select_addr(const select_expression_t *expression)
2642 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2644 ir_node *compound_addr = expression_to_firm(expression->compound);
2646 /* make sure the type is constructed */
2647 type_t *type = skip_typeref(expression->compound->base.type);
2648 if (is_type_pointer(type)) {
2649 type = type->pointer.points_to;
2651 (void) get_ir_type(type);
2653 declaration_t *entry = expression->compound_entry;
2654 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2655 ir_entity *entity = entry->v.entity;
2657 assert(entity != NULL);
2659 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2664 static ir_node *select_to_firm(const select_expression_t *expression)
2666 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2667 ir_node *addr = select_addr(expression);
2668 type_t *type = revert_automatic_type_conversion(
2669 (const expression_t*) expression);
2670 type = skip_typeref(type);
2672 declaration_t *entry = expression->compound_entry;
2673 type_t *entry_type = skip_typeref(entry->type);
2675 if (entry_type->kind == TYPE_BITFIELD) {
2676 return bitfield_extract_to_firm(expression, addr);
2679 return deref_address(dbgi, type, addr);
2682 /* Values returned by __builtin_classify_type. */
2683 typedef enum gcc_type_class
2689 enumeral_type_class,
2692 reference_type_class,
2696 function_type_class,
2707 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2709 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2715 const atomic_type_t *const atomic_type = &type->atomic;
2716 switch (atomic_type->akind) {
2717 /* should not be reached */
2718 case ATOMIC_TYPE_INVALID:
2722 /* gcc cannot do that */
2723 case ATOMIC_TYPE_VOID:
2724 tc = void_type_class;
2727 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2728 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2729 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2730 case ATOMIC_TYPE_SHORT:
2731 case ATOMIC_TYPE_USHORT:
2732 case ATOMIC_TYPE_INT:
2733 case ATOMIC_TYPE_UINT:
2734 case ATOMIC_TYPE_LONG:
2735 case ATOMIC_TYPE_ULONG:
2736 case ATOMIC_TYPE_LONGLONG:
2737 case ATOMIC_TYPE_ULONGLONG:
2738 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2739 tc = integer_type_class;
2742 case ATOMIC_TYPE_FLOAT:
2743 case ATOMIC_TYPE_DOUBLE:
2744 case ATOMIC_TYPE_LONG_DOUBLE:
2745 tc = real_type_class;
2748 panic("Unexpected atomic type in classify_type_to_firm().");
2751 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2752 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2753 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2754 case TYPE_ARRAY: /* gcc handles this as pointer */
2755 case TYPE_FUNCTION: /* gcc handles this as pointer */
2756 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2757 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2758 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2760 /* gcc handles this as integer */
2761 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2764 /* typedef/typeof should be skipped already */
2771 panic("unexpected TYPE classify_type_to_firm().");
2774 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2775 ir_mode *const mode = mode_int;
2776 tarval *const tv = new_tarval_from_long(tc, mode);
2777 return new_d_Const(dbgi, mode, tv);
2780 static ir_node *function_name_to_firm(
2781 const funcname_expression_t *const expr)
2783 switch(expr->kind) {
2784 case FUNCNAME_FUNCTION:
2785 case FUNCNAME_PRETTY_FUNCTION:
2786 case FUNCNAME_FUNCDNAME:
2787 if (current_function_name == NULL) {
2788 const source_position_t *const src_pos = &expr->base.source_position;
2789 const char *const name = current_function_decl->symbol->string;
2790 const string_t string = { name, strlen(name) + 1 };
2791 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2793 return current_function_name;
2794 case FUNCNAME_FUNCSIG:
2795 if (current_funcsig == NULL) {
2796 const source_position_t *const src_pos = &expr->base.source_position;
2797 ir_entity *ent = get_irg_entity(current_ir_graph);
2798 const char *const name = get_entity_ld_name(ent);
2799 const string_t string = { name, strlen(name) + 1 };
2800 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2802 return current_funcsig;
2804 panic("Unsupported function name");
2807 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2809 statement_t *statement = expr->statement;
2811 assert(statement->kind == STATEMENT_COMPOUND);
2812 return compound_statement_to_firm(&statement->compound);
2815 static ir_node *va_start_expression_to_firm(
2816 const va_start_expression_t *const expr)
2818 ir_type *const method_type = get_ir_type(current_function_decl->type);
2819 int const n = get_method_n_params(method_type) - 1;
2820 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2821 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2822 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2823 ir_node *const no_mem = new_NoMem();
2824 ir_node *const arg_sel =
2825 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2827 ir_node *const cnst = get_type_size(expr->parameter->type);
2828 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2829 set_value_for_expression(expr->ap, add);
2834 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2836 type_t *const type = expr->base.type;
2837 expression_t *const ap_expr = expr->ap;
2838 ir_node *const ap_addr = expression_to_addr(ap_expr);
2839 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2840 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2841 ir_node *const res = deref_address(dbgi, type, ap);
2843 ir_node *const cnst = get_type_size(expr->base.type);
2844 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2846 set_value_for_expression_addr(ap_expr, add, ap_addr);
2851 static ir_node *dereference_addr(const unary_expression_t *const expression)
2853 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2854 return expression_to_firm(expression->value);
2858 * Returns a IR-node representing an lvalue of the given expression.
2860 static ir_node *expression_to_addr(const expression_t *expression)
2862 switch(expression->kind) {
2863 case EXPR_REFERENCE:
2864 return reference_addr(&expression->reference);
2865 case EXPR_ARRAY_ACCESS:
2866 return array_access_addr(&expression->array_access);
2868 return select_addr(&expression->select);
2870 return call_expression_to_firm(&expression->call);
2871 case EXPR_UNARY_DEREFERENCE: {
2872 return dereference_addr(&expression->unary);
2877 panic("trying to get address of non-lvalue");
2880 static ir_node *builtin_constant_to_firm(
2881 const builtin_constant_expression_t *expression)
2883 ir_mode *mode = get_ir_mode(expression->base.type);
2886 if (is_constant_expression(expression->value)) {
2891 return new_Const_long(mode, v);
2894 static ir_node *builtin_prefetch_to_firm(
2895 const builtin_prefetch_expression_t *expression)
2897 ir_node *adr = expression_to_firm(expression->adr);
2898 /* no Firm support for prefetch yet */
2904 * creates firm nodes for an expression. The difference between this function
2905 * and expression_to_firm is, that this version might produce mode_b nodes
2906 * instead of mode_Is.
2908 static ir_node *_expression_to_firm(const expression_t *expression)
2911 if (!constant_folding) {
2912 assert(!expression->base.transformed);
2913 ((expression_t*) expression)->base.transformed = true;
2917 switch(expression->kind) {
2918 case EXPR_CHARACTER_CONSTANT:
2919 return character_constant_to_firm(&expression->conste);
2920 case EXPR_WIDE_CHARACTER_CONSTANT:
2921 return wide_character_constant_to_firm(&expression->conste);
2923 return const_to_firm(&expression->conste);
2924 case EXPR_STRING_LITERAL:
2925 return string_literal_to_firm(&expression->string);
2926 case EXPR_WIDE_STRING_LITERAL:
2927 return wide_string_literal_to_firm(&expression->wide_string);
2928 case EXPR_REFERENCE:
2929 return reference_expression_to_firm(&expression->reference);
2931 return call_expression_to_firm(&expression->call);
2933 return unary_expression_to_firm(&expression->unary);
2935 return binary_expression_to_firm(&expression->binary);
2936 case EXPR_ARRAY_ACCESS:
2937 return array_access_to_firm(&expression->array_access);
2939 return sizeof_to_firm(&expression->typeprop);
2941 return alignof_to_firm(&expression->typeprop);
2942 case EXPR_CONDITIONAL:
2943 return conditional_to_firm(&expression->conditional);
2945 return select_to_firm(&expression->select);
2946 case EXPR_CLASSIFY_TYPE:
2947 return classify_type_to_firm(&expression->classify_type);
2949 return function_name_to_firm(&expression->funcname);
2950 case EXPR_STATEMENT:
2951 return statement_expression_to_firm(&expression->statement);
2953 return va_start_expression_to_firm(&expression->va_starte);
2955 return va_arg_expression_to_firm(&expression->va_arge);
2956 case EXPR_BUILTIN_SYMBOL:
2957 panic("unimplemented expression found");
2958 case EXPR_BUILTIN_CONSTANT_P:
2959 return builtin_constant_to_firm(&expression->builtin_constant);
2960 case EXPR_BUILTIN_PREFETCH:
2961 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2963 return offsetof_to_firm(&expression->offsetofe);
2964 case EXPR_COMPOUND_LITERAL:
2965 return compound_literal_to_firm(&expression->compound_literal);
2971 panic("invalid expression found");
2974 static bool produces_mode_b(const expression_t *expression)
2976 switch (expression->kind) {
2977 case EXPR_BINARY_EQUAL:
2978 case EXPR_BINARY_NOTEQUAL:
2979 case EXPR_BINARY_LESS:
2980 case EXPR_BINARY_LESSEQUAL:
2981 case EXPR_BINARY_GREATER:
2982 case EXPR_BINARY_GREATEREQUAL:
2983 case EXPR_BINARY_ISGREATER:
2984 case EXPR_BINARY_ISGREATEREQUAL:
2985 case EXPR_BINARY_ISLESS:
2986 case EXPR_BINARY_ISLESSEQUAL:
2987 case EXPR_BINARY_ISLESSGREATER:
2988 case EXPR_BINARY_ISUNORDERED:
2989 case EXPR_UNARY_NOT:
2992 case EXPR_BINARY_BUILTIN_EXPECT:
2993 return produces_mode_b(expression->binary.left);
2994 case EXPR_BINARY_COMMA:
2995 return produces_mode_b(expression->binary.right);
3002 static ir_node *expression_to_firm(const expression_t *expression)
3004 if (!produces_mode_b(expression)) {
3005 ir_node *res = _expression_to_firm(expression);
3006 assert(res == NULL || get_irn_mode(res) != mode_b);
3010 if (is_constant_expression(expression)) {
3011 ir_node *res = _expression_to_firm(expression);
3012 ir_mode *mode = get_ir_mode(expression->base.type);
3013 assert(is_Const(res));
3014 if (is_Const_null(res)) {
3015 return new_Const_long(mode, 0);
3017 return new_Const_long(mode, 1);
3021 /* we have to produce a 0/1 from the mode_b expression */
3022 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3023 return produce_condition_result(expression, dbgi);
3026 static ir_node *expression_to_modeb(const expression_t *expression)
3028 ir_node *res = _expression_to_firm(expression);
3029 res = create_conv(NULL, res, mode_b);
3035 * create a short-circuit expression evaluation that tries to construct
3036 * efficient control flow structures for &&, || and ! expressions
3038 static void create_condition_evaluation(const expression_t *expression,
3039 ir_node *true_block,
3040 ir_node *false_block)
3042 switch(expression->kind) {
3043 case EXPR_UNARY_NOT: {
3044 const unary_expression_t *unary_expression = &expression->unary;
3045 create_condition_evaluation(unary_expression->value, false_block,
3049 case EXPR_BINARY_LOGICAL_AND: {
3050 const binary_expression_t *binary_expression = &expression->binary;
3052 ir_node *cur_block = get_cur_block();
3053 ir_node *extra_block = new_immBlock();
3054 set_cur_block(cur_block);
3055 create_condition_evaluation(binary_expression->left, extra_block,
3057 mature_immBlock(extra_block);
3058 set_cur_block(extra_block);
3059 create_condition_evaluation(binary_expression->right, true_block,
3063 case EXPR_BINARY_LOGICAL_OR: {
3064 const binary_expression_t *binary_expression = &expression->binary;
3066 ir_node *cur_block = get_cur_block();
3067 ir_node *extra_block = new_immBlock();
3068 set_cur_block(cur_block);
3069 create_condition_evaluation(binary_expression->left, true_block,
3071 mature_immBlock(extra_block);
3072 set_cur_block(extra_block);
3073 create_condition_evaluation(binary_expression->right, true_block,
3081 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3082 ir_node *condition = expression_to_modeb(expression);
3083 ir_node *cond = new_d_Cond(dbgi, condition);
3084 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3085 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3087 /* set branch prediction info based on __builtin_expect */
3088 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3089 long cnst = fold_constant(expression->binary.right);
3090 cond_jmp_predicate pred;
3093 pred = COND_JMP_PRED_FALSE;
3095 pred = COND_JMP_PRED_TRUE;
3097 set_Cond_jmp_pred(cond, pred);
3100 add_immBlock_pred(true_block, true_proj);
3101 if (false_block != NULL) {
3102 add_immBlock_pred(false_block, false_proj);
3105 set_cur_block(NULL);
3110 static void create_declaration_entity(declaration_t *declaration,
3111 declaration_kind_t declaration_kind,
3112 ir_type *parent_type)
3114 type_t *const type = skip_typeref(declaration->type);
3115 if (is_type_function(type)) {
3116 (void) get_function_entity(declaration);
3120 ident *const id = new_id_from_str(declaration->symbol->string);
3121 ir_type *const irtype = get_ir_type(type);
3122 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3123 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3125 declaration->declaration_kind = (unsigned char) declaration_kind;
3126 declaration->v.entity = entity;
3127 set_entity_variability(entity, variability_uninitialized);
3128 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3129 if (parent_type == get_tls_type())
3130 set_entity_allocation(entity, allocation_automatic);
3131 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3132 set_entity_allocation(entity, allocation_static);
3134 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3135 set_entity_volatility(entity, volatility_is_volatile);
3140 typedef struct type_path_entry_t type_path_entry_t;
3141 struct type_path_entry_t {
3143 ir_initializer_t *initializer;
3145 declaration_t *compound_entry;
3148 typedef struct type_path_t type_path_t;
3149 struct type_path_t {
3150 type_path_entry_t *path;
3155 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3157 size_t len = ARR_LEN(path->path);
3159 for(size_t i = 0; i < len; ++i) {
3160 const type_path_entry_t *entry = & path->path[i];
3162 type_t *type = skip_typeref(entry->type);
3163 if (is_type_compound(type)) {
3164 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3165 } else if (is_type_array(type)) {
3166 fprintf(stderr, "[%zd]", entry->index);
3168 fprintf(stderr, "-INVALID-");
3171 fprintf(stderr, " (");
3172 print_type(path->top_type);
3173 fprintf(stderr, ")");
3176 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3178 size_t len = ARR_LEN(path->path);
3180 return & path->path[len-1];
3183 static type_path_entry_t *append_to_type_path(type_path_t *path)
3185 size_t len = ARR_LEN(path->path);
3186 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3188 type_path_entry_t *result = & path->path[len];
3189 memset(result, 0, sizeof(result[0]));
3193 static size_t get_compound_size(const compound_type_t *type)
3195 declaration_t *declaration = type->declaration;
3196 declaration_t *member = declaration->scope.declarations;
3198 for( ; member != NULL; member = member->next) {
3201 /* TODO: cache results? */
3206 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3208 type_t *orig_top_type = path->top_type;
3209 type_t *top_type = skip_typeref(orig_top_type);
3211 assert(is_type_compound(top_type) || is_type_array(top_type));
3213 if (ARR_LEN(path->path) == 0) {
3216 type_path_entry_t *top = get_type_path_top(path);
3217 ir_initializer_t *initializer = top->initializer;
3218 return get_initializer_compound_value(initializer, top->index);
3222 static void descend_into_subtype(type_path_t *path)
3224 type_t *orig_top_type = path->top_type;
3225 type_t *top_type = skip_typeref(orig_top_type);
3227 assert(is_type_compound(top_type) || is_type_array(top_type));
3229 ir_initializer_t *initializer = get_initializer_entry(path);
3231 type_path_entry_t *top = append_to_type_path(path);
3232 top->type = top_type;
3236 if (is_type_compound(top_type)) {
3237 declaration_t *declaration = top_type->compound.declaration;
3238 declaration_t *entry = declaration->scope.declarations;
3240 top->compound_entry = entry;
3242 len = get_compound_size(&top_type->compound);
3244 path->top_type = entry->type;
3246 assert(is_type_array(top_type));
3247 assert(top_type->array.size > 0);
3250 path->top_type = top_type->array.element_type;
3251 len = top_type->array.size;
3253 if (initializer == NULL
3254 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3255 initializer = create_initializer_compound(len);
3256 /* we have to set the entry at the 2nd latest path entry... */
3257 size_t path_len = ARR_LEN(path->path);
3258 assert(path_len >= 1);
3260 type_path_entry_t *entry = & path->path[path_len-2];
3261 ir_initializer_t *tinitializer = entry->initializer;
3262 set_initializer_compound_value(tinitializer, entry->index,
3266 top->initializer = initializer;
3269 static void ascend_from_subtype(type_path_t *path)
3271 type_path_entry_t *top = get_type_path_top(path);
3273 path->top_type = top->type;
3275 size_t len = ARR_LEN(path->path);
3276 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3279 static void walk_designator(type_path_t *path, const designator_t *designator)
3281 /* designators start at current object type */
3282 ARR_RESIZE(type_path_entry_t, path->path, 1);
3284 for( ; designator != NULL; designator = designator->next) {
3285 type_path_entry_t *top = get_type_path_top(path);
3286 type_t *orig_type = top->type;
3287 type_t *type = skip_typeref(orig_type);
3289 if (designator->symbol != NULL) {
3290 assert(is_type_compound(type));
3292 symbol_t *symbol = designator->symbol;
3294 declaration_t *declaration = type->compound.declaration;
3295 declaration_t *iter = declaration->scope.declarations;
3296 for( ; iter != NULL; iter = iter->next, ++index) {
3297 if (iter->symbol == symbol) {
3301 assert(iter != NULL);
3303 top->type = orig_type;
3304 top->compound_entry = iter;
3306 orig_type = iter->type;
3308 expression_t *array_index = designator->array_index;
3309 assert(designator->array_index != NULL);
3310 assert(is_type_array(type));
3311 assert(is_type_valid(array_index->base.type));
3313 long index = fold_constant(array_index);
3316 if (type->array.size_constant == 1) {
3317 long array_size = type->array.size;
3318 assert(index < array_size);
3322 top->type = orig_type;
3323 top->index = (size_t) index;
3324 orig_type = type->array.element_type;
3326 path->top_type = orig_type;
3328 if (designator->next != NULL) {
3329 descend_into_subtype(path);
3333 path->invalid = false;
3336 static void advance_current_object(type_path_t *path)
3338 if (path->invalid) {
3339 /* TODO: handle this... */
3340 panic("invalid initializer in ast2firm (excessive elements)");
3344 type_path_entry_t *top = get_type_path_top(path);
3346 type_t *type = skip_typeref(top->type);
3347 if (is_type_union(type)) {
3348 top->compound_entry = NULL;
3349 } else if (is_type_struct(type)) {
3350 declaration_t *entry = top->compound_entry;
3353 entry = entry->next;
3354 top->compound_entry = entry;
3355 if (entry != NULL) {
3356 path->top_type = entry->type;
3360 assert(is_type_array(type));
3363 if (!type->array.size_constant || top->index < type->array.size) {
3368 /* we're past the last member of the current sub-aggregate, try if we
3369 * can ascend in the type hierarchy and continue with another subobject */
3370 size_t len = ARR_LEN(path->path);
3373 ascend_from_subtype(path);
3374 advance_current_object(path);
3376 path->invalid = true;
3381 static ir_initializer_t *create_ir_initializer(
3382 const initializer_t *initializer, type_t *type);
3384 static ir_initializer_t *create_ir_initializer_value(
3385 const initializer_value_t *initializer)
3387 if (is_type_compound(initializer->value->base.type)) {
3388 panic("initializer creation for compounds not implemented yet");
3390 ir_node *value = expression_to_firm(initializer->value);
3391 return create_initializer_const(value);
3394 static ir_initializer_t *create_ir_initializer_list(
3395 const initializer_list_t *initializer, type_t *type)
3398 memset(&path, 0, sizeof(path));
3399 path.top_type = type;
3400 path.path = NEW_ARR_F(type_path_entry_t, 0);
3402 descend_into_subtype(&path);
3404 for(size_t i = 0; i < initializer->len; ++i) {
3405 const initializer_t *sub_initializer = initializer->initializers[i];
3407 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3408 walk_designator(&path, sub_initializer->designator.designator);
3412 if (sub_initializer->kind == INITIALIZER_VALUE) {
3413 /* we might have to descend into types until we're at a scalar
3416 type_t *orig_top_type = path.top_type;
3417 type_t *top_type = skip_typeref(orig_top_type);
3419 if (is_type_scalar(top_type))
3421 descend_into_subtype(&path);
3425 ir_initializer_t *sub_irinitializer
3426 = create_ir_initializer(sub_initializer, path.top_type);
3428 size_t path_len = ARR_LEN(path.path);
3429 assert(path_len >= 1);
3430 type_path_entry_t *entry = & path.path[path_len-1];
3431 ir_initializer_t *tinitializer = entry->initializer;
3432 set_initializer_compound_value(tinitializer, entry->index,
3435 advance_current_object(&path);
3438 assert(ARR_LEN(path.path) >= 1);
3439 ir_initializer_t *result = path.path[0].initializer;
3440 DEL_ARR_F(path.path);
3445 static ir_initializer_t *create_ir_initializer_string(
3446 const initializer_string_t *initializer, type_t *type)
3448 type = skip_typeref(type);
3450 size_t string_len = initializer->string.size;
3451 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3452 size_t len = type->array.size;
3453 ir_initializer_t *irinitializer = create_initializer_compound(len);
3455 const char *string = initializer->string.begin;
3456 ir_mode *mode = get_ir_mode(type->array.element_type);
3458 for(size_t i = 0; i < len; ++i) {
3463 tarval *tv = new_tarval_from_long(c, mode);
3464 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3466 set_initializer_compound_value(irinitializer, i, char_initializer);
3469 return irinitializer;
3472 static ir_initializer_t *create_ir_initializer_wide_string(
3473 const initializer_wide_string_t *initializer, type_t *type)
3475 size_t string_len = initializer->string.size;
3476 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3477 size_t len = type->array.size;
3478 ir_initializer_t *irinitializer = create_initializer_compound(len);
3480 const wchar_rep_t *string = initializer->string.begin;
3481 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3483 for(size_t i = 0; i < len; ++i) {
3485 if (i < string_len) {
3488 tarval *tv = new_tarval_from_long(c, mode);
3489 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3491 set_initializer_compound_value(irinitializer, i, char_initializer);
3494 return irinitializer;
3497 static ir_initializer_t *create_ir_initializer(
3498 const initializer_t *initializer, type_t *type)
3500 switch(initializer->kind) {
3501 case INITIALIZER_STRING:
3502 return create_ir_initializer_string(&initializer->string, type);
3504 case INITIALIZER_WIDE_STRING:
3505 return create_ir_initializer_wide_string(&initializer->wide_string,
3508 case INITIALIZER_LIST:
3509 return create_ir_initializer_list(&initializer->list, type);
3511 case INITIALIZER_VALUE:
3512 return create_ir_initializer_value(&initializer->value);
3514 case INITIALIZER_DESIGNATOR:
3515 panic("unexpected designator initializer found");
3517 panic("unknown initializer");
3520 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3523 if (is_atomic_type(type)) {
3524 ir_mode *mode = get_type_mode(type);
3525 tarval *zero = get_mode_null(mode);
3526 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3528 /* TODO: bitfields */
3529 ir_node *mem = get_store();
3530 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3531 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3534 assert(is_compound_type(type));
3537 if (is_Array_type(type)) {
3538 assert(has_array_upper_bound(type, 0));
3539 n_members = get_array_upper_bound_int(type, 0);
3541 n_members = get_compound_n_members(type);
3544 for(int i = 0; i < n_members; ++i) {
3547 if (is_Array_type(type)) {
3548 ir_entity *entity = get_array_element_entity(type);
3549 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3550 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3551 ir_node *in[1] = { cnst };
3552 irtype = get_array_element_type(type);
3553 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3555 ir_entity *member = get_compound_member(type, i);
3557 irtype = get_entity_type(member);
3558 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3561 create_dynamic_null_initializer(irtype, dbgi, addr);
3566 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3567 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3569 switch(get_initializer_kind(initializer)) {
3570 case IR_INITIALIZER_NULL: {
3571 create_dynamic_null_initializer(type, dbgi, base_addr);
3574 case IR_INITIALIZER_CONST: {
3575 ir_node *node = get_initializer_const_value(initializer);
3576 ir_mode *mode = get_irn_mode(node);
3577 assert(get_type_mode(type) == mode);
3579 /* TODO: bitfields... */
3580 ir_node *mem = get_store();
3581 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3582 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3586 case IR_INITIALIZER_TARVAL: {
3587 tarval *tv = get_initializer_tarval_value(initializer);
3588 ir_mode *mode = get_tarval_mode(tv);
3589 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3590 assert(get_type_mode(type) == mode);
3592 /* TODO: bitfields... */
3593 ir_node *mem = get_store();
3594 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3595 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3599 case IR_INITIALIZER_COMPOUND: {
3600 assert(is_compound_type(type));
3602 if (is_Array_type(type)) {
3603 assert(has_array_upper_bound(type, 0));
3604 n_members = get_array_upper_bound_int(type, 0);
3606 n_members = get_compound_n_members(type);
3609 if (get_initializer_compound_n_entries(initializer)
3610 != (unsigned) n_members)
3611 panic("initializer doesn't match compound type");
3613 for(int i = 0; i < n_members; ++i) {
3616 if (is_Array_type(type)) {
3617 ir_entity *entity = get_array_element_entity(type);
3618 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3619 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3620 ir_node *in[1] = { cnst };
3621 irtype = get_array_element_type(type);
3622 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3624 ir_entity *member = get_compound_member(type, i);
3626 irtype = get_entity_type(member);
3627 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3630 ir_initializer_t *sub_init
3631 = get_initializer_compound_value(initializer, i);
3633 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3639 panic("invalid IR_INITIALIZER found");
3642 static void create_dynamic_initializer(ir_initializer_t *initializer,
3643 dbg_info *dbgi, ir_entity *entity)
3645 ir_node *frame = get_local_frame(entity);
3646 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3647 ir_type *type = get_entity_type(entity);
3649 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3652 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3653 ir_entity *entity, type_t *type)
3655 ir_node *memory = get_store();
3656 ir_node *nomem = new_NoMem();
3657 ir_node *frame = get_irg_frame(current_ir_graph);
3658 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3660 if (initializer->kind == INITIALIZER_VALUE) {
3661 initializer_value_t *initializer_value = &initializer->value;
3663 ir_node *value = expression_to_firm(initializer_value->value);
3664 type = skip_typeref(type);
3665 assign_value(dbgi, addr, type, value);
3669 if (!is_constant_initializer(initializer)) {
3670 ir_initializer_t *irinitializer
3671 = create_ir_initializer(initializer, type);
3673 create_dynamic_initializer(irinitializer, dbgi, entity);
3677 /* create the ir_initializer */
3678 ir_graph *const old_current_ir_graph = current_ir_graph;
3679 current_ir_graph = get_const_code_irg();
3681 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3683 assert(current_ir_graph == get_const_code_irg());
3684 current_ir_graph = old_current_ir_graph;
3686 /* create a "template" entity which is copied to the entity on the stack */
3687 ident *const id = id_unique("initializer.%u");
3688 ir_type *const irtype = get_ir_type(type);
3689 ir_type *const global_type = get_glob_type();
3690 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3691 set_entity_ld_ident(init_entity, id);
3693 set_entity_variability(init_entity, variability_initialized);
3694 set_entity_visibility(init_entity, visibility_local);
3695 set_entity_allocation(init_entity, allocation_static);
3697 set_entity_initializer(init_entity, irinitializer);
3699 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3700 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3702 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3703 set_store(copyb_mem);
3706 static void create_initializer_local_variable_entity(declaration_t *declaration)
3708 initializer_t *initializer = declaration->init.initializer;
3709 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3710 ir_entity *entity = declaration->v.entity;
3711 type_t *type = declaration->type;
3712 create_local_initializer(initializer, dbgi, entity, type);
3715 static void create_declaration_initializer(declaration_t *declaration)
3717 initializer_t *initializer = declaration->init.initializer;
3718 if (initializer == NULL)
3721 declaration_kind_t declaration_kind
3722 = (declaration_kind_t) declaration->declaration_kind;
3723 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3724 create_initializer_local_variable_entity(declaration);
3728 type_t *type = skip_typeref(declaration->type);
3730 if (initializer->kind == INITIALIZER_VALUE) {
3731 initializer_value_t *initializer_value = &initializer->value;
3733 = get_dbg_info(&declaration->source_position);
3735 ir_node *value = expression_to_firm(initializer_value->value);
3736 value = do_strict_conv(dbgi, value);
3738 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3739 set_value(declaration->v.value_number, value);
3741 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3743 ir_entity *entity = declaration->v.entity;
3745 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3746 set_entity_variability(entity, variability_constant);
3748 set_entity_variability(entity, variability_initialized);
3750 set_atomic_ent_value(entity, value);
3753 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3754 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3756 ir_entity *entity = declaration->v.entity;
3757 ir_initializer_t *irinitializer
3758 = create_ir_initializer(initializer, type);
3760 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3761 set_entity_variability(entity, variability_constant);
3763 set_entity_variability(entity, variability_initialized);
3765 set_entity_initializer(entity, irinitializer);
3769 static void create_variable_length_array(declaration_t *declaration)
3771 /* initializers are not allowed for VLAs */
3772 assert(declaration->init.initializer == NULL);
3774 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3775 declaration->v.vla_base = NULL;
3777 /* TODO: record VLA somewhere so we create the free node when we leave
3781 static void allocate_variable_length_array(declaration_t *declaration)
3783 /* initializers are not allowed for VLAs */
3784 assert(declaration->init.initializer == NULL);
3785 assert(get_cur_block() != NULL);
3787 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3788 type_t *type = declaration->type;
3789 ir_type *el_type = get_ir_type(type->array.element_type);
3791 /* make sure size_node is calculated */
3792 get_type_size(type);
3793 ir_node *elems = type->array.size_node;
3794 ir_node *mem = get_store();
3795 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3797 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3798 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3801 assert(declaration->declaration_kind
3802 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3803 declaration->v.vla_base = addr;
3807 * Creates a Firm local variable from a declaration.
3809 static void create_local_variable(declaration_t *declaration)
3811 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3813 bool needs_entity = declaration->address_taken;
3814 type_t *type = skip_typeref(declaration->type);
3816 /* is it a variable length array? */
3817 if (is_type_array(type) && !type->array.size_constant) {
3818 create_variable_length_array(declaration);
3820 } else if (is_type_array(type) || is_type_compound(type)) {
3821 needs_entity = true;
3822 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3823 needs_entity = true;
3827 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3828 create_declaration_entity(declaration,
3829 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3832 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3833 declaration->v.value_number = next_value_number_function;
3834 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3835 ++next_value_number_function;
3839 static void create_local_static_variable(declaration_t *declaration)
3841 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3843 type_t *const type = skip_typeref(declaration->type);
3844 ir_type *const global_type = get_glob_type();
3845 ir_type *const irtype = get_ir_type(type);
3846 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3848 size_t l = strlen(declaration->symbol->string);
3849 char buf[l + sizeof(".%u")];
3850 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3851 ident *const id = id_unique(buf);
3853 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3855 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3856 set_entity_volatility(entity, volatility_is_volatile);
3859 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3860 declaration->v.entity = entity;
3861 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3862 set_entity_variability(entity, variability_uninitialized);
3863 set_entity_visibility(entity, visibility_local);
3864 set_entity_allocation(entity, allocation_static);
3866 ir_graph *const old_current_ir_graph = current_ir_graph;
3867 current_ir_graph = get_const_code_irg();
3869 create_declaration_initializer(declaration);
3871 assert(current_ir_graph == get_const_code_irg());
3872 current_ir_graph = old_current_ir_graph;
3877 static void return_statement_to_firm(return_statement_t *statement)
3879 if (get_cur_block() == NULL)
3882 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3883 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3888 if (get_method_n_ress(func_irtype) > 0) {
3889 ir_type *res_type = get_method_res_type(func_irtype, 0);
3891 if (statement->value != NULL) {
3892 ir_node *node = expression_to_firm(statement->value);
3893 node = do_strict_conv(dbgi, node);
3897 if (is_compound_type(res_type)) {
3900 mode = get_type_mode(res_type);
3902 in[0] = new_Unknown(mode);
3906 /* build return_value for its side effects */
3907 if (statement->value != NULL) {
3908 expression_to_firm(statement->value);
3913 ir_node *store = get_store();
3914 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3916 ir_node *end_block = get_irg_end_block(current_ir_graph);
3917 add_immBlock_pred(end_block, ret);
3919 set_cur_block(NULL);
3922 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3924 if (get_cur_block() == NULL)
3927 return expression_to_firm(statement->expression);
3930 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3932 declaration_t *declaration = compound->scope.declarations;
3933 for( ; declaration != NULL; declaration = declaration->next) {
3934 create_local_declaration(declaration);
3937 ir_node *result = NULL;
3938 statement_t *statement = compound->statements;
3939 for( ; statement != NULL; statement = statement->base.next) {
3940 if (statement->base.next == NULL
3941 && statement->kind == STATEMENT_EXPRESSION) {
3942 result = expression_statement_to_firm(
3943 &statement->expression);
3946 statement_to_firm(statement);
3952 static void create_global_variable(declaration_t *declaration)
3956 switch ((storage_class_tag_t)declaration->storage_class) {
3957 case STORAGE_CLASS_STATIC:
3958 vis = visibility_local;
3961 case STORAGE_CLASS_EXTERN:
3962 vis = visibility_external_allocated;
3965 case STORAGE_CLASS_NONE:
3966 vis = visibility_external_visible;
3969 case STORAGE_CLASS_THREAD:
3970 vis = visibility_external_visible;
3973 case STORAGE_CLASS_THREAD_EXTERN:
3974 vis = visibility_external_allocated;
3977 case STORAGE_CLASS_THREAD_STATIC:
3978 vis = visibility_local;
3982 var_type = get_tls_type();
3986 var_type = get_glob_type();
3990 create_declaration_entity(declaration,
3991 DECLARATION_KIND_GLOBAL_VARIABLE,
3993 if (!is_type_function(skip_typeref(declaration->type))) {
3994 set_entity_visibility(declaration->v.entity, vis);
3999 case STORAGE_CLASS_TYPEDEF:
4000 case STORAGE_CLASS_AUTO:
4001 case STORAGE_CLASS_REGISTER:
4002 case STORAGE_CLASS_ENUM_ENTRY:
4005 panic("Invalid storage class for global variable");
4008 static void create_local_declaration(declaration_t *declaration)
4010 if (declaration->namespc != NAMESPACE_NORMAL)
4012 /* construct type */
4013 (void) get_ir_type(declaration->type);
4014 if (declaration->symbol == NULL) {
4018 type_t *type = skip_typeref(declaration->type);
4020 switch ((storage_class_tag_t) declaration->storage_class) {
4021 case STORAGE_CLASS_STATIC:
4022 create_local_static_variable(declaration);
4024 case STORAGE_CLASS_EXTERN:
4025 create_global_variable(declaration);
4026 create_declaration_initializer(declaration);
4028 case STORAGE_CLASS_NONE:
4029 case STORAGE_CLASS_AUTO:
4030 case STORAGE_CLASS_REGISTER:
4031 if (is_type_function(type)) {
4032 if (declaration->init.statement != NULL) {
4033 panic("nested functions not supported yet");
4035 get_function_entity(declaration);
4038 create_local_variable(declaration);
4041 case STORAGE_CLASS_ENUM_ENTRY:
4042 /* should already be handled */
4043 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4045 case STORAGE_CLASS_TYPEDEF:
4046 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4048 case STORAGE_CLASS_THREAD:
4049 case STORAGE_CLASS_THREAD_EXTERN:
4050 case STORAGE_CLASS_THREAD_STATIC:
4053 panic("invalid storage class found");
4056 static void initialize_local_declaration(declaration_t *declaration)
4058 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4061 switch ((declaration_kind_t) declaration->declaration_kind) {
4062 case DECLARATION_KIND_LOCAL_VARIABLE:
4063 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4064 create_declaration_initializer(declaration);
4067 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4068 allocate_variable_length_array(declaration);
4071 case DECLARATION_KIND_LABEL_BLOCK:
4072 case DECLARATION_KIND_COMPOUND_MEMBER:
4073 case DECLARATION_KIND_GLOBAL_VARIABLE:
4074 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4075 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4076 case DECLARATION_KIND_FUNCTION:
4077 case DECLARATION_KIND_TYPE:
4078 case DECLARATION_KIND_ENUM_ENTRY:
4081 case DECLARATION_KIND_UNKNOWN:
4082 panic("can't initialize unknwon declaration");
4084 panic("invalid declaration kind");
4087 static void declaration_statement_to_firm(declaration_statement_t *statement)
4089 declaration_t *declaration = statement->declarations_begin;
4090 declaration_t *end = statement->declarations_end->next;
4091 for( ; declaration != end; declaration = declaration->next) {
4092 if (declaration->namespc != NAMESPACE_NORMAL)
4094 initialize_local_declaration(declaration);
4098 static void if_statement_to_firm(if_statement_t *statement)
4100 ir_node *cur_block = get_cur_block();
4102 ir_node *fallthrough_block = NULL;
4104 /* the true (blocks) */
4105 ir_node *true_block = NULL;
4106 if (statement->true_statement != NULL) {
4107 true_block = new_immBlock();
4108 statement_to_firm(statement->true_statement);
4109 if (get_cur_block() != NULL) {
4110 ir_node *jmp = new_Jmp();
4111 if (fallthrough_block == NULL)
4112 fallthrough_block = new_immBlock();
4113 add_immBlock_pred(fallthrough_block, jmp);
4117 /* the false (blocks) */
4118 ir_node *false_block = NULL;
4119 if (statement->false_statement != NULL) {
4120 false_block = new_immBlock();
4122 statement_to_firm(statement->false_statement);
4123 if (get_cur_block() != NULL) {
4124 ir_node *jmp = new_Jmp();
4125 if (fallthrough_block == NULL)
4126 fallthrough_block = new_immBlock();
4127 add_immBlock_pred(fallthrough_block, jmp);
4131 /* create the condition */
4132 if (cur_block != NULL) {
4133 if (true_block == NULL || false_block == NULL) {
4134 if (fallthrough_block == NULL)
4135 fallthrough_block = new_immBlock();
4136 if (true_block == NULL)
4137 true_block = fallthrough_block;
4138 if (false_block == NULL)
4139 false_block = fallthrough_block;
4142 set_cur_block(cur_block);
4143 create_condition_evaluation(statement->condition, true_block,
4147 mature_immBlock(true_block);
4148 if (false_block != fallthrough_block) {
4149 mature_immBlock(false_block);
4151 if (fallthrough_block != NULL) {
4152 mature_immBlock(fallthrough_block);
4155 set_cur_block(fallthrough_block);
4158 static void while_statement_to_firm(while_statement_t *statement)
4160 ir_node *jmp = NULL;
4161 if (get_cur_block() != NULL) {
4165 /* create the header block */
4166 ir_node *header_block = new_immBlock();
4168 add_immBlock_pred(header_block, jmp);
4172 ir_node *old_continue_label = continue_label;
4173 ir_node *old_break_label = break_label;
4174 continue_label = header_block;
4177 ir_node *body_block = new_immBlock();
4178 statement_to_firm(statement->body);
4179 ir_node *false_block = break_label;
4181 assert(continue_label == header_block);
4182 continue_label = old_continue_label;
4183 break_label = old_break_label;
4185 if (get_cur_block() != NULL) {
4187 add_immBlock_pred(header_block, jmp);
4190 /* shortcut for while(true) */
4191 if (is_constant_expression(statement->condition)
4192 && fold_constant(statement->condition) != 0) {
4193 set_cur_block(header_block);
4194 ir_node *header_jmp = new_Jmp();
4195 add_immBlock_pred(body_block, header_jmp);
4197 keep_alive(body_block);
4199 if (false_block == NULL) {
4200 false_block = new_immBlock();
4203 /* create the condition */
4204 set_cur_block(header_block);
4206 create_condition_evaluation(statement->condition, body_block,
4210 mature_immBlock(body_block);
4211 mature_immBlock(header_block);
4212 if (false_block != NULL) {
4213 mature_immBlock(false_block);
4216 set_cur_block(false_block);
4219 static void do_while_statement_to_firm(do_while_statement_t *statement)
4221 ir_node *jmp = NULL;
4222 if (get_cur_block() != NULL) {
4226 /* create the header block */
4227 ir_node *header_block = new_immBlock();
4230 ir_node *body_block = new_immBlock();
4232 add_immBlock_pred(body_block, jmp);
4235 ir_node *old_continue_label = continue_label;
4236 ir_node *old_break_label = break_label;
4237 continue_label = header_block;
4240 statement_to_firm(statement->body);
4241 ir_node *false_block = break_label;
4243 assert(continue_label == header_block);
4244 continue_label = old_continue_label;
4245 break_label = old_break_label;
4247 if (get_cur_block() != NULL) {
4248 ir_node *body_jmp = new_Jmp();
4249 add_immBlock_pred(header_block, body_jmp);
4250 mature_immBlock(header_block);
4253 if (false_block == NULL) {
4254 false_block = new_immBlock();
4257 /* create the condition */
4258 set_cur_block(header_block);
4260 create_condition_evaluation(statement->condition, body_block, false_block);
4261 mature_immBlock(body_block);
4262 mature_immBlock(header_block);
4263 if (false_block != NULL) {
4264 mature_immBlock(false_block);
4267 set_cur_block(false_block);
4270 static void for_statement_to_firm(for_statement_t *statement)
4272 ir_node *jmp = NULL;
4274 /* create declarations */
4275 declaration_t *declaration = statement->scope.declarations;
4276 for( ; declaration != NULL; declaration = declaration->next) {
4277 create_local_declaration(declaration);
4279 declaration = statement->scope.declarations;
4280 for( ; declaration != NULL; declaration = declaration->next) {
4281 initialize_local_declaration(declaration);
4284 if (get_cur_block() != NULL) {
4285 if (statement->initialisation != NULL) {
4286 expression_to_firm(statement->initialisation);
4293 /* create the step block */
4294 ir_node *const step_block = new_immBlock();
4295 if (statement->step != NULL) {
4296 expression_to_firm(statement->step);
4298 ir_node *const step_jmp = new_Jmp();
4300 /* create the header block */
4301 ir_node *const header_block = new_immBlock();
4303 add_immBlock_pred(header_block, jmp);
4305 add_immBlock_pred(header_block, step_jmp);
4307 /* the false block */
4308 ir_node *const false_block = new_immBlock();
4311 ir_node * body_block;
4312 if (statement->body != NULL) {
4313 ir_node *const old_continue_label = continue_label;
4314 ir_node *const old_break_label = break_label;
4315 continue_label = step_block;
4316 break_label = false_block;
4318 body_block = new_immBlock();
4319 statement_to_firm(statement->body);
4321 assert(continue_label == step_block);
4322 assert(break_label == false_block);
4323 continue_label = old_continue_label;
4324 break_label = old_break_label;
4326 if (get_cur_block() != NULL) {
4328 add_immBlock_pred(step_block, jmp);
4331 body_block = step_block;
4334 /* create the condition */
4335 set_cur_block(header_block);
4336 if (statement->condition != NULL) {
4337 create_condition_evaluation(statement->condition, body_block,
4340 keep_alive(header_block);
4342 add_immBlock_pred(body_block, jmp);
4345 mature_immBlock(body_block);
4346 mature_immBlock(false_block);
4347 mature_immBlock(step_block);
4348 mature_immBlock(header_block);
4349 mature_immBlock(false_block);
4351 set_cur_block(false_block);
4354 static void create_jump_statement(const statement_t *statement,
4355 ir_node *target_block)
4357 if (get_cur_block() == NULL)
4360 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4361 ir_node *jump = new_d_Jmp(dbgi);
4362 add_immBlock_pred(target_block, jump);
4364 set_cur_block(NULL);
4367 static ir_node *get_break_label(void)
4369 if (break_label == NULL) {
4370 ir_node *cur_block = get_cur_block();
4371 break_label = new_immBlock();
4372 set_cur_block(cur_block);
4377 static void switch_statement_to_firm(const switch_statement_t *statement)
4379 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4381 ir_node *expression = expression_to_firm(statement->expression);
4382 ir_node *cond = new_d_Cond(dbgi, expression);
4384 set_cur_block(NULL);
4386 ir_node *const old_switch_cond = current_switch_cond;
4387 ir_node *const old_break_label = break_label;
4388 const bool old_saw_default_label = saw_default_label;
4389 saw_default_label = false;
4390 current_switch_cond = cond;
4393 if (statement->body != NULL) {
4394 statement_to_firm(statement->body);
4397 if (get_cur_block() != NULL) {
4398 ir_node *jmp = new_Jmp();
4399 add_immBlock_pred(get_break_label(), jmp);
4402 if (!saw_default_label) {
4403 set_cur_block(get_nodes_block(cond));
4404 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4405 MAGIC_DEFAULT_PN_NUMBER);
4406 add_immBlock_pred(get_break_label(), proj);
4409 if (break_label != NULL) {
4410 mature_immBlock(break_label);
4412 set_cur_block(break_label);
4414 assert(current_switch_cond == cond);
4415 current_switch_cond = old_switch_cond;
4416 break_label = old_break_label;
4417 saw_default_label = old_saw_default_label;
4420 static void case_label_to_firm(const case_label_statement_t *statement)
4422 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4424 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4426 /* let's create a node and hope firm constant folding creates a Const
4429 ir_node *old_block = get_nodes_block(current_switch_cond);
4430 ir_node *block = new_immBlock();
4432 set_cur_block(old_block);
4433 if (statement->expression != NULL) {
4434 long start_pn = fold_constant(statement->expression);
4435 long end_pn = start_pn;
4436 if (statement->end_range != NULL) {
4437 end_pn = fold_constant(statement->end_range);
4439 assert(start_pn <= end_pn);
4440 /* create jumps for all cases in the given range */
4441 for (long pn = start_pn; pn <= end_pn; ++pn) {
4442 if (pn == MAGIC_DEFAULT_PN_NUMBER) {
4443 /* oops someone detected our cheating... */
4444 panic("magic default pn used");
4446 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4447 add_immBlock_pred(block, proj);
4450 saw_default_label = true;
4451 proj = new_d_defaultProj(dbgi, current_switch_cond,
4452 MAGIC_DEFAULT_PN_NUMBER);
4454 add_immBlock_pred(block, proj);
4457 if (fallthrough != NULL) {
4458 add_immBlock_pred(block, fallthrough);
4460 mature_immBlock(block);
4461 set_cur_block(block);
4463 if (statement->statement != NULL) {
4464 statement_to_firm(statement->statement);
4468 static ir_node *get_label_block(declaration_t *label)
4470 assert(label->namespc == NAMESPACE_LABEL);
4472 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4473 return label->v.block;
4475 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4477 ir_node *old_cur_block = get_cur_block();
4478 ir_node *block = new_immBlock();
4479 set_cur_block(old_cur_block);
4481 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4482 label->v.block = block;
4484 ARR_APP1(ir_node *, imature_blocks, block);
4489 static void label_to_firm(const label_statement_t *statement)
4491 ir_node *block = get_label_block(statement->label);
4493 if (get_cur_block() != NULL) {
4494 ir_node *jmp = new_Jmp();
4495 add_immBlock_pred(block, jmp);
4498 set_cur_block(block);
4501 if (statement->statement != NULL) {
4502 statement_to_firm(statement->statement);
4506 static void goto_to_firm(const goto_statement_t *statement)
4508 if (get_cur_block() == NULL)
4511 ir_node *block = get_label_block(statement->label);
4512 ir_node *jmp = new_Jmp();
4513 add_immBlock_pred(block, jmp);
4515 set_cur_block(NULL);
4518 static void asm_statement_to_firm(const asm_statement_t *statement)
4520 bool needs_memory = false;
4522 if (statement->is_volatile) {
4523 needs_memory = true;
4526 size_t n_clobbers = 0;
4527 asm_clobber_t *clobber = statement->clobbers;
4528 for( ; clobber != NULL; clobber = clobber->next) {
4529 const char *clobber_str = clobber->clobber.begin;
4531 if (!be_is_valid_clobber(clobber_str)) {
4532 errorf(&statement->base.source_position,
4533 "invalid clobber '%s' specified", clobber->clobber);
4537 if (strcmp(clobber_str, "memory") == 0) {
4538 needs_memory = true;
4542 ident *id = new_id_from_str(clobber_str);
4543 obstack_ptr_grow(&asm_obst, id);
4546 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4547 ident **clobbers = NULL;
4548 if (n_clobbers > 0) {
4549 clobbers = obstack_finish(&asm_obst);
4552 size_t n_inputs = 0;
4553 asm_argument_t *argument = statement->inputs;
4554 for ( ; argument != NULL; argument = argument->next)
4556 size_t n_outputs = 0;
4557 argument = statement->outputs;
4558 for ( ; argument != NULL; argument = argument->next)
4561 unsigned next_pos = 0;
4563 ir_node *ins[n_inputs + n_outputs + 1];
4566 ir_asm_constraint *tmp_in_constraints
4567 = xmalloc(n_outputs * sizeof(tmp_in_constraints[0]));
4569 const expression_t *out_exprs[n_outputs];
4570 ir_node *out_addrs[n_outputs];
4571 size_t out_size = 0;
4573 argument = statement->outputs;
4574 for ( ; argument != NULL; argument = argument->next) {
4575 const char *constraints = argument->constraints.begin;
4576 asm_constraint_flags_t asm_flags
4577 = be_parse_asm_constraints(constraints);
4579 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4580 errorf(&statement->base.source_position,
4581 "some constraints in '%s' are not supported", constraints);
4584 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4585 errorf(&statement->base.source_position,
4586 "some constraints in '%s' are invalid", constraints);
4589 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4590 errorf(&statement->base.source_position,
4591 "no write flag specified for output constraints '%s'",
4596 unsigned pos = next_pos++;
4597 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4598 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4599 expression_t *expr = argument->expression;
4600 ir_node *addr = expression_to_addr(expr);
4601 /* in+output, construct an artifical same_as constraint on the
4603 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4605 ir_node *value = get_value_from_lvalue(expr, addr);
4607 snprintf(buf, sizeof(buf), "%d", pos);
4609 ir_asm_constraint constraint;
4610 constraint.pos = pos;
4611 constraint.constraint = new_id_from_str(buf);
4612 constraint.mode = get_ir_mode(expr->base.type);
4613 tmp_in_constraints[in_size] = constraint;
4614 ins[in_size] = value;
4619 out_exprs[out_size] = expr;
4620 out_addrs[out_size] = addr;
4622 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4623 /* pure memory ops need no input (but we have to make sure we
4624 * attach to the memory) */
4625 assert(! (asm_flags &
4626 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4627 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4628 needs_memory = true;
4630 /* we need to attach the address to the inputs */
4631 expression_t *expr = argument->expression;
4633 ir_asm_constraint constraint;
4634 constraint.pos = pos;
4635 constraint.constraint = new_id_from_str(constraints);
4636 constraint.mode = NULL;
4637 tmp_in_constraints[in_size] = constraint;
4639 ins[in_size] = expression_to_addr(expr);
4643 errorf(&statement->base.source_position,
4644 "only modifiers but no place set in constraints '%s'",
4649 ir_asm_constraint constraint;
4650 constraint.pos = pos;
4651 constraint.constraint = new_id_from_str(constraints);
4652 constraint.mode = get_ir_mode(argument->expression->base.type);
4654 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4656 assert(obstack_object_size(&asm_obst)
4657 == out_size * sizeof(ir_asm_constraint));
4658 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4661 obstack_grow(&asm_obst, tmp_in_constraints,
4662 in_size * sizeof(tmp_in_constraints[0]));
4663 free(tmp_in_constraints);
4664 /* find and count input and output arguments */
4665 argument = statement->inputs;
4666 for( ; argument != NULL; argument = argument->next) {
4667 const char *constraints = argument->constraints.begin;
4668 asm_constraint_flags_t asm_flags
4669 = be_parse_asm_constraints(constraints);
4671 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4672 errorf(&statement->base.source_position,
4673 "some constraints in '%s' are not supported", constraints);
4676 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4677 errorf(&statement->base.source_position,
4678 "some constraints in '%s' are invalid", constraints);
4681 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4682 errorf(&statement->base.source_position,
4683 "write flag specified for input constraints '%s'",
4689 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4690 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4691 /* we can treat this as "normal" input */
4692 input = expression_to_firm(argument->expression);
4693 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4694 /* pure memory ops need no input (but we have to make sure we
4695 * attach to the memory) */
4696 assert(! (asm_flags &
4697 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4698 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4699 needs_memory = true;
4700 input = expression_to_addr(argument->expression);
4702 errorf(&statement->base.source_position,
4703 "only modifiers but no place set in constraints '%s'",
4708 ir_asm_constraint constraint;
4709 constraint.pos = next_pos++;
4710 constraint.constraint = new_id_from_str(constraints);
4711 constraint.mode = get_irn_mode(input);
4713 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4714 ins[in_size++] = input;
4718 ir_asm_constraint constraint;
4719 constraint.pos = next_pos++;
4720 constraint.constraint = new_id_from_str("");
4721 constraint.mode = mode_M;
4723 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4724 ins[in_size++] = get_store();
4727 assert(obstack_object_size(&asm_obst)
4728 == in_size * sizeof(ir_asm_constraint));
4729 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4731 /* create asm node */
4732 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4734 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4736 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4737 out_size, output_constraints,
4738 n_clobbers, clobbers, asm_text);
4740 if (statement->is_volatile) {
4741 set_irn_pinned(node, op_pin_state_pinned);
4743 set_irn_pinned(node, op_pin_state_floats);
4746 /* create output projs & connect them */
4748 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4753 for (i = 0; i < out_size; ++i) {
4754 const expression_t *out_expr = out_exprs[i];
4756 ir_mode *mode = get_ir_mode(out_expr->base.type);
4757 ir_node *proj = new_Proj(node, mode, pn);
4758 ir_node *addr = out_addrs[i];
4760 set_value_for_expression_addr(out_expr, proj, addr);
4764 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4765 statement_to_firm(statement->try_statement);
4766 warningf(&statement->base.source_position, "structured exception handling ignored");
4769 static void leave_statement_to_firm(leave_statement_t *statement) {
4770 errorf(&statement->base.source_position, "__leave not supported yet");
4774 * Transform a statement.
4776 static void statement_to_firm(statement_t *statement)
4779 assert(!statement->base.transformed);
4780 statement->base.transformed = true;
4783 switch(statement->kind) {
4784 case STATEMENT_INVALID:
4785 panic("invalid statement found");
4787 case STATEMENT_EMPTY:
4790 case STATEMENT_COMPOUND:
4791 compound_statement_to_firm(&statement->compound);
4793 case STATEMENT_RETURN:
4794 return_statement_to_firm(&statement->returns);
4796 case STATEMENT_EXPRESSION:
4797 expression_statement_to_firm(&statement->expression);
4800 if_statement_to_firm(&statement->ifs);
4802 case STATEMENT_WHILE:
4803 while_statement_to_firm(&statement->whiles);
4805 case STATEMENT_DO_WHILE:
4806 do_while_statement_to_firm(&statement->do_while);
4808 case STATEMENT_DECLARATION:
4809 declaration_statement_to_firm(&statement->declaration);
4811 case STATEMENT_BREAK:
4812 create_jump_statement(statement, get_break_label());
4814 case STATEMENT_CONTINUE:
4815 create_jump_statement(statement, continue_label);
4817 case STATEMENT_SWITCH:
4818 switch_statement_to_firm(&statement->switchs);
4820 case STATEMENT_CASE_LABEL:
4821 case_label_to_firm(&statement->case_label);
4824 for_statement_to_firm(&statement->fors);
4826 case STATEMENT_LABEL:
4827 label_to_firm(&statement->label);
4829 case STATEMENT_GOTO:
4830 goto_to_firm(&statement->gotos);
4833 asm_statement_to_firm(&statement->asms);
4835 case STATEMENT_MS_TRY:
4836 ms_try_statement_to_firm(&statement->ms_try);
4838 case STATEMENT_LEAVE:
4839 leave_statement_to_firm(&statement->leave);
4842 panic("Statement not implemented\n");
4845 static int count_decls_in_expression(const expression_t *expression);
4847 static int count_local_declarations(const declaration_t * decl,
4848 const declaration_t *const end)
4851 for (; decl != end; decl = decl->next) {
4852 if (decl->namespc != NAMESPACE_NORMAL)
4854 type_t *type = skip_typeref(decl->type);
4856 if (!decl->address_taken && is_type_scalar(type))
4858 const initializer_t *initializer = decl->init.initializer;
4859 /* FIXME: should walk initializer hierarchies... */
4860 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4861 count += count_decls_in_expression(initializer->value.value);
4867 static int count_decls_in_expression(const expression_t *expression) {
4870 if (expression == NULL)
4873 switch((expression_kind_t) expression->base.kind) {
4874 case EXPR_STATEMENT:
4875 return count_decls_in_stmts(expression->statement.statement);
4877 int count_left = count_decls_in_expression(expression->binary.left);
4878 int count_right = count_decls_in_expression(expression->binary.right);
4879 return count_left + count_right;
4882 return count_decls_in_expression(expression->unary.value);
4884 call_argument_t *argument = expression->call.arguments;
4885 for( ; argument != NULL; argument = argument->next) {
4886 count += count_decls_in_expression(argument->expression);
4893 panic("unexpected expression kind");
4895 case EXPR_COMPOUND_LITERAL:
4899 case EXPR_CONDITIONAL:
4900 count += count_decls_in_expression(expression->conditional.condition);
4901 count += count_decls_in_expression(expression->conditional.true_expression);
4902 count += count_decls_in_expression(expression->conditional.false_expression);
4905 case EXPR_BUILTIN_PREFETCH:
4906 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4907 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4908 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4911 case EXPR_BUILTIN_CONSTANT_P:
4912 count += count_decls_in_expression(expression->builtin_constant.value);
4916 count += count_decls_in_expression(expression->select.compound);
4919 case EXPR_ARRAY_ACCESS:
4920 count += count_decls_in_expression(expression->array_access.array_ref);
4921 count += count_decls_in_expression(expression->array_access.index);
4924 case EXPR_CLASSIFY_TYPE:
4925 count += count_decls_in_expression(expression->classify_type.type_expression);
4929 case EXPR_ALIGNOF: {
4930 expression_t *tp_expression = expression->typeprop.tp_expression;
4931 if (tp_expression != NULL) {
4932 count += count_decls_in_expression(tp_expression);
4938 case EXPR_REFERENCE:
4940 case EXPR_CHARACTER_CONSTANT:
4941 case EXPR_WIDE_CHARACTER_CONSTANT:
4942 case EXPR_STRING_LITERAL:
4943 case EXPR_WIDE_STRING_LITERAL:
4945 case EXPR_BUILTIN_SYMBOL:
4951 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4952 * (or implement all the missing expressions here/implement a walker)
4958 static int count_decls_in_stmts(const statement_t *stmt)
4961 for (; stmt != NULL; stmt = stmt->base.next) {
4962 switch (stmt->kind) {
4963 case STATEMENT_EMPTY:
4966 case STATEMENT_DECLARATION: {
4967 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4968 count += count_local_declarations(decl_stmt->declarations_begin,
4969 decl_stmt->declarations_end->next);
4973 case STATEMENT_COMPOUND: {
4974 const compound_statement_t *const comp =
4976 count += count_decls_in_stmts(comp->statements);
4980 case STATEMENT_IF: {
4981 const if_statement_t *const if_stmt = &stmt->ifs;
4982 count += count_decls_in_expression(if_stmt->condition);
4983 count += count_decls_in_stmts(if_stmt->true_statement);
4984 count += count_decls_in_stmts(if_stmt->false_statement);
4988 case STATEMENT_SWITCH: {
4989 const switch_statement_t *const switch_stmt = &stmt->switchs;
4990 count += count_decls_in_expression(switch_stmt->expression);
4991 count += count_decls_in_stmts(switch_stmt->body);
4995 case STATEMENT_LABEL: {
4996 const label_statement_t *const label_stmt = &stmt->label;
4997 if (label_stmt->statement != NULL) {
4998 count += count_decls_in_stmts(label_stmt->statement);
5003 case STATEMENT_WHILE: {
5004 const while_statement_t *const while_stmt = &stmt->whiles;
5005 count += count_decls_in_expression(while_stmt->condition);
5006 count += count_decls_in_stmts(while_stmt->body);
5010 case STATEMENT_DO_WHILE: {
5011 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5012 count += count_decls_in_expression(do_while_stmt->condition);
5013 count += count_decls_in_stmts(do_while_stmt->body);
5017 case STATEMENT_FOR: {
5018 const for_statement_t *const for_stmt = &stmt->fors;
5019 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5020 count += count_decls_in_expression(for_stmt->initialisation);
5021 count += count_decls_in_expression(for_stmt->condition);
5022 count += count_decls_in_expression(for_stmt->step);
5023 count += count_decls_in_stmts(for_stmt->body);
5027 case STATEMENT_CASE_LABEL: {
5028 const case_label_statement_t *label = &stmt->case_label;
5029 count += count_decls_in_expression(label->expression);
5030 if (label->statement != NULL) {
5031 count += count_decls_in_stmts(label->statement);
5037 case STATEMENT_BREAK:
5038 case STATEMENT_CONTINUE:
5041 case STATEMENT_EXPRESSION: {
5042 const expression_statement_t *expr_stmt = &stmt->expression;
5043 count += count_decls_in_expression(expr_stmt->expression);
5047 case STATEMENT_GOTO:
5048 case STATEMENT_LEAVE:
5049 case STATEMENT_INVALID:
5052 case STATEMENT_RETURN: {
5053 const return_statement_t *ret_stmt = &stmt->returns;
5054 count += count_decls_in_expression(ret_stmt->value);
5058 case STATEMENT_MS_TRY: {
5059 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5060 count += count_decls_in_stmts(try_stmt->try_statement);
5061 if (try_stmt->except_expression != NULL)
5062 count += count_decls_in_expression(try_stmt->except_expression);
5063 count += count_decls_in_stmts(try_stmt->final_statement);
5071 static int get_function_n_local_vars(declaration_t *declaration)
5075 /* count parameters */
5076 count += count_local_declarations(declaration->scope.declarations, NULL);
5078 /* count local variables declared in body */
5079 count += count_decls_in_stmts(declaration->init.statement);
5084 static void initialize_function_parameters(declaration_t *declaration)
5086 ir_graph *irg = current_ir_graph;
5087 ir_node *args = get_irg_args(irg);
5088 ir_node *start_block = get_irg_start_block(irg);
5089 ir_type *function_irtype = get_ir_type(declaration->type);
5092 declaration_t *parameter = declaration->scope.declarations;
5093 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5094 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5095 type_t *type = skip_typeref(parameter->type);
5097 bool needs_entity = parameter->address_taken;
5098 assert(!is_type_array(type));
5099 if (is_type_compound(type)) {
5100 needs_entity = true;
5104 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5105 ident *id = new_id_from_str(parameter->symbol->string);
5106 set_entity_ident(entity, id);
5108 parameter->declaration_kind
5109 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5110 parameter->v.entity = entity;
5114 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5115 ir_mode *param_mode = get_type_mode(param_irtype);
5118 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5120 ir_mode *mode = get_ir_mode(type);
5121 value = create_conv(NULL, value, mode);
5122 value = do_strict_conv(NULL, value);
5124 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5125 parameter->v.value_number = next_value_number_function;
5126 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5127 ++next_value_number_function;
5129 set_value(parameter->v.value_number, value);
5134 * Handle additional decl modifiers for IR-graphs
5136 * @param irg the IR-graph
5137 * @param dec_modifiers additional modifiers
5139 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5141 if (decl_modifiers & DM_NORETURN) {
5142 /* TRUE if the declaration includes the Microsoft
5143 __declspec(noreturn) specifier. */
5144 set_irg_additional_property(irg, mtp_property_noreturn);
5146 if (decl_modifiers & DM_NOTHROW) {
5147 /* TRUE if the declaration includes the Microsoft
5148 __declspec(nothrow) specifier. */
5149 set_irg_additional_property(irg, mtp_property_nothrow);
5151 if (decl_modifiers & DM_NAKED) {
5152 /* TRUE if the declaration includes the Microsoft
5153 __declspec(naked) specifier. */
5154 set_irg_additional_property(irg, mtp_property_naked);
5156 if (decl_modifiers & DM_FORCEINLINE) {
5157 /* TRUE if the declaration includes the
5158 Microsoft __forceinline specifier. */
5159 set_irg_inline_property(irg, irg_inline_forced);
5161 if (decl_modifiers & DM_NOINLINE) {
5162 /* TRUE if the declaration includes the Microsoft
5163 __declspec(noinline) specifier. */
5164 set_irg_inline_property(irg, irg_inline_forbidden);
5168 static void add_function_pointer(ir_type *segment, ir_entity *method,
5169 const char *unique_template)
5171 ir_type *method_type = get_entity_type(method);
5172 ident *id = id_unique(unique_template);
5173 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5175 ident *ide = id_unique(unique_template);
5176 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5177 ir_graph *irg = get_const_code_irg();
5178 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5181 set_entity_compiler_generated(ptr, 1);
5182 set_entity_variability(ptr, variability_constant);
5183 set_atomic_ent_value(ptr, val);
5187 * Create code for a function.
5189 static void create_function(declaration_t *declaration)
5191 ir_entity *function_entity = get_function_entity(declaration);
5193 if (declaration->init.statement == NULL)
5196 if (declaration->modifiers & DM_CONSTRUCTOR) {
5197 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5198 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5200 if (declaration->modifiers & DM_DESTRUCTOR) {
5201 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5202 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5205 current_function_decl = declaration;
5206 current_function_name = NULL;
5207 current_funcsig = NULL;
5209 assert(imature_blocks == NULL);
5210 imature_blocks = NEW_ARR_F(ir_node*, 0);
5212 int n_local_vars = get_function_n_local_vars(declaration);
5213 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5215 set_irg_fp_model(irg, firm_opt.fp_model);
5216 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5217 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5219 ir_node *first_block = get_cur_block();
5221 /* set inline flags */
5222 if (declaration->is_inline)
5223 set_irg_inline_property(irg, irg_inline_recomended);
5224 handle_decl_modifier_irg(irg, declaration->modifiers);
5226 next_value_number_function = 0;
5227 initialize_function_parameters(declaration);
5229 statement_to_firm(declaration->init.statement);
5231 ir_node *end_block = get_irg_end_block(irg);
5233 /* do we have a return statement yet? */
5234 if (get_cur_block() != NULL) {
5235 type_t *type = skip_typeref(declaration->type);
5236 assert(is_type_function(type));
5237 const function_type_t *func_type = &type->function;
5238 const type_t *return_type
5239 = skip_typeref(func_type->return_type);
5242 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5243 ret = new_Return(get_store(), 0, NULL);
5246 if (is_type_scalar(return_type)) {
5247 mode = get_ir_mode(func_type->return_type);
5253 /* ยง5.1.2.2.3 main implicitly returns 0 */
5254 if (strcmp(declaration->symbol->string, "main") == 0) {
5255 in[0] = new_Const(mode, get_mode_null(mode));
5257 in[0] = new_Unknown(mode);
5258 if (warning.return_type) {
5259 warningf(&declaration->source_position,
5260 "missing return statement at end of non-void function '%Y'",
5261 declaration->symbol);
5264 ret = new_Return(get_store(), 1, in);
5266 add_immBlock_pred(end_block, ret);
5269 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
5270 mature_immBlock(imature_blocks[i]);
5272 DEL_ARR_F(imature_blocks);
5273 imature_blocks = NULL;
5275 mature_immBlock(first_block);
5276 mature_immBlock(end_block);
5278 irg_finalize_cons(irg);
5280 /* finalize the frame type */
5281 ir_type *frame_type = get_irg_frame_type(irg);
5282 int n = get_compound_n_members(frame_type);
5285 for(int i = 0; i < n; ++i) {
5286 ir_entity *entity = get_compound_member(frame_type, i);
5287 ir_type *entity_type = get_entity_type(entity);
5289 int align = get_type_alignment_bytes(entity_type);
5290 if (align > align_all)
5294 misalign = offset % align;
5296 offset += align - misalign;
5300 set_entity_offset(entity, offset);
5301 offset += get_type_size_bytes(entity_type);
5303 set_type_size_bytes(frame_type, offset);
5304 set_type_alignment_bytes(frame_type, align_all);
5309 static void scope_to_firm(scope_t *scope)
5311 /* first pass: create declarations */
5312 declaration_t *declaration = scope->declarations;
5313 for( ; declaration != NULL; declaration = declaration->next) {
5314 if (declaration->namespc != NAMESPACE_NORMAL)
5316 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5317 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5319 if (declaration->symbol == NULL)
5322 type_t *type = skip_typeref(declaration->type);
5323 if (is_type_function(type)) {
5324 get_function_entity(declaration);
5326 create_global_variable(declaration);
5330 /* second pass: create code/initializers */
5331 declaration = scope->declarations;
5332 for( ; declaration != NULL; declaration = declaration->next) {
5333 if (declaration->namespc != NAMESPACE_NORMAL)
5335 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5336 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5338 if (declaration->symbol == NULL)
5341 type_t *type = declaration->type;
5342 if (type->kind == TYPE_FUNCTION) {
5343 create_function(declaration);
5345 assert(declaration->declaration_kind
5346 == DECLARATION_KIND_GLOBAL_VARIABLE);
5347 current_ir_graph = get_const_code_irg();
5348 create_declaration_initializer(declaration);
5353 void init_ast2firm(void)
5355 obstack_init(&asm_obst);
5356 init_atomic_modes();
5358 id_underscore = new_id_from_chars("_", 1);
5359 id_imp = new_id_from_chars("__imp_", 6);
5361 /* OS option must be set to the backend */
5362 const char *s = "ia32-gasmode=linux";
5363 switch (firm_opt.os_support) {
5364 case OS_SUPPORT_MINGW:
5365 create_ld_ident = create_ld_ident_win32;
5366 s = "ia32-gasmode=mingw";
5368 case OS_SUPPORT_LINUX:
5369 create_ld_ident = create_ld_ident_linux_elf;
5370 s = "ia32-gasmode=elf";
5372 case OS_SUPPORT_MACHO:
5373 create_ld_ident = create_ld_ident_macho;
5374 s = "ia32-gasmode=macho";
5377 int res = firm_be_option(s);
5381 /* create idents for all known runtime functions */
5382 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5383 rts_idents[i] = new_id_from_str(rts_data[i].name);
5386 entitymap_init(&entitymap);
5389 static void init_ir_types(void)
5391 static int ir_types_initialized = 0;
5392 if (ir_types_initialized)
5394 ir_types_initialized = 1;
5396 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
5397 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5398 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5400 ir_type_int = get_ir_type(type_int);
5401 ir_type_const_char = get_ir_type(type_const_char);
5402 ir_type_wchar_t = get_ir_type(type_wchar_t);
5403 ir_type_void = get_ir_type(type_void);
5406 void exit_ast2firm(void)
5408 entitymap_destroy(&entitymap);
5409 obstack_free(&asm_obst, NULL);
5412 void translation_unit_to_firm(translation_unit_t *unit)
5414 /* just to be sure */
5415 continue_label = NULL;
5417 current_switch_cond = NULL;
5421 scope_to_firm(&unit->scope);