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 * Creates an entity representing a function.
986 * @param declaration the function declaration
988 static ir_entity *get_function_entity(declaration_t *declaration)
990 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
991 return declaration->v.entity;
992 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
994 symbol_t *symbol = declaration->symbol;
995 ident *id = new_id_from_str(symbol->string);
997 ir_type *global_type = get_glob_type();
998 ir_type *ir_type_method = get_ir_type(declaration->type);
999 assert(is_Method_type(ir_type_method));
1001 /* already an entity defined? */
1002 ir_entity *entity = entitymap_get(&entitymap, symbol);
1003 if (entity != NULL) {
1004 if (get_entity_visibility(entity) == visibility_external_allocated
1005 && declaration->init.statement != NULL) {
1006 set_entity_visibility(entity, visibility_external_visible);
1008 goto entity_created;
1011 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1012 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1013 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1015 if (declaration->modifiers & DM_USED) {
1016 set_entity_stickyness(entity, stickyness_sticky);
1019 /* static inline => local
1020 * extern inline => local
1021 * inline without definition => local
1022 * inline with definition => external_visible */
1023 storage_class_tag_t const storage_class = declaration->storage_class;
1024 bool const is_inline = declaration->is_inline;
1025 bool const has_body = declaration->init.statement != NULL;
1026 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1027 set_entity_visibility(entity, visibility_external_visible);
1028 } else if (storage_class == STORAGE_CLASS_STATIC ||
1029 (is_inline && has_body)) {
1031 /* this entity was declared, but is defined nowhere */
1032 set_entity_peculiarity(entity, peculiarity_description);
1034 set_entity_visibility(entity, visibility_local);
1035 } else if (has_body) {
1036 set_entity_visibility(entity, visibility_external_visible);
1038 set_entity_visibility(entity, visibility_external_allocated);
1040 set_entity_allocation(entity, allocation_static);
1042 /* We should check for file scope here, but as long as we compile C only
1043 this is not needed. */
1044 if (! firm_opt.freestanding) {
1045 /* check for a known runtime function */
1046 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1047 if (id != rts_idents[i])
1050 /* ignore those rts functions not necessary needed for current mode */
1051 if ((c_mode & rts_data[i].flags) == 0)
1053 assert(rts_entities[rts_data[i].id] == NULL);
1054 rts_entities[rts_data[i].id] = entity;
1058 entitymap_insert(&entitymap, symbol, entity);
1061 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1062 declaration->v.entity = entity;
1068 * Creates a Const node representing a constant.
1070 static ir_node *const_to_firm(const const_expression_t *cnst)
1072 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1073 ir_mode *mode = get_ir_mode(cnst->base.type);
1078 if (mode_is_float(mode)) {
1079 tv = new_tarval_from_double(cnst->v.float_value, mode);
1081 if (mode_is_signed(mode)) {
1082 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1084 len = snprintf(buf, sizeof(buf), "%llu",
1085 (unsigned long long) cnst->v.int_value);
1087 tv = new_tarval_from_str(buf, len, mode);
1090 return new_d_Const(dbgi, mode, tv);
1094 * Creates a Const node representing a character constant.
1096 static ir_node *character_constant_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);
1101 long long int v = 0;
1102 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1103 if (char_is_signed) {
1104 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1106 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1110 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1111 tarval *tv = new_tarval_from_str(buf, len, mode);
1113 return new_d_Const(dbgi, mode, tv);
1117 * Creates a Const node representing a wide character constant.
1119 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1121 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1122 ir_mode *mode = get_ir_mode(cnst->base.type);
1124 long long int v = cnst->v.wide_character.begin[0];
1127 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1128 tarval *tv = new_tarval_from_str(buf, len, mode);
1130 return new_d_Const(dbgi, mode, tv);
1134 * Creates a SymConst for a given entity.
1136 * @param dbgi debug info
1137 * @param mode the (reference) mode for the SymConst
1138 * @param entity the entity
1140 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1143 assert(entity != NULL);
1144 union symconst_symbol sym;
1145 sym.entity_p = entity;
1146 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1150 * Creates a SymConst node representing a string constant.
1152 * @param src_pos the source position of the string constant
1153 * @param id_prefix a prefix for the name of the generated string constant
1154 * @param value the value of the string constant
1156 static ir_node *string_to_firm(const source_position_t *const src_pos,
1157 const char *const id_prefix,
1158 const string_t *const value)
1160 ir_type *const global_type = get_glob_type();
1161 dbg_info *const dbgi = get_dbg_info(src_pos);
1162 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1163 ir_type_const_char, dbgi);
1165 ident *const id = id_unique(id_prefix);
1166 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1167 set_entity_ld_ident(entity, id);
1168 set_entity_variability(entity, variability_constant);
1169 set_entity_allocation(entity, allocation_static);
1171 ir_type *const elem_type = ir_type_const_char;
1172 ir_mode *const mode = get_type_mode(elem_type);
1174 const char* const string = value->begin;
1175 const size_t slen = value->size;
1177 set_array_lower_bound_int(type, 0, 0);
1178 set_array_upper_bound_int(type, 0, slen);
1179 set_type_size_bytes(type, slen);
1180 set_type_state(type, layout_fixed);
1182 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1183 for(size_t i = 0; i < slen; ++i) {
1184 tvs[i] = new_tarval_from_long(string[i], mode);
1187 set_array_entity_values(entity, tvs, slen);
1190 return create_symconst(dbgi, mode_P_data, entity);
1194 * Creates a SymConst node representing a string literal.
1196 * @param literal the string literal
1198 static ir_node *string_literal_to_firm(
1199 const string_literal_expression_t* literal)
1201 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1206 * Creates a SymConst node representing a wide string literal.
1208 * @param literal the wide string literal
1210 static ir_node *wide_string_literal_to_firm(
1211 const wide_string_literal_expression_t* const literal)
1213 ir_type *const global_type = get_glob_type();
1214 ir_type *const elem_type = ir_type_wchar_t;
1215 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1216 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1219 ident *const id = id_unique("Lstr.%u");
1220 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1221 set_entity_ld_ident(entity, id);
1222 set_entity_variability(entity, variability_constant);
1223 set_entity_allocation(entity, allocation_static);
1225 ir_mode *const mode = get_type_mode(elem_type);
1227 const wchar_rep_t *const string = literal->value.begin;
1228 const size_t slen = literal->value.size;
1230 set_array_lower_bound_int(type, 0, 0);
1231 set_array_upper_bound_int(type, 0, slen);
1232 set_type_size_bytes(type, slen);
1233 set_type_state(type, layout_fixed);
1235 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1236 for(size_t i = 0; i < slen; ++i) {
1237 tvs[i] = new_tarval_from_long(string[i], mode);
1240 set_array_entity_values(entity, tvs, slen);
1243 return create_symconst(dbgi, mode_P_data, entity);
1246 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1247 ir_node *const addr)
1249 ir_type *irtype = get_ir_type(type);
1250 if (is_compound_type(irtype)
1251 || is_Method_type(irtype)
1252 || is_Array_type(irtype)) {
1256 ir_mode *const mode = get_type_mode(irtype);
1257 ir_node *const memory = get_store();
1258 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1259 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1260 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1262 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1263 set_Load_volatility(load, volatility_is_volatile);
1266 set_store(load_mem);
1271 * Creates a strict Conv if neccessary.
1273 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1275 ir_mode *mode = get_irn_mode(node);
1277 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1279 if (!mode_is_float(mode))
1282 /* check if there is already a Conv */
1283 if (is_Conv(node)) {
1284 /* convert it into a strict Conv */
1285 set_Conv_strict(node, 1);
1289 /* otherwise create a new one */
1290 return new_d_strictConv(dbgi, node, mode);
1293 static ir_node *get_global_var_address(dbg_info *const dbgi,
1294 const declaration_t *const decl)
1296 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1298 ir_entity *const entity = decl->v.entity;
1299 switch ((storage_class_tag_t)decl->storage_class) {
1300 case STORAGE_CLASS_THREAD:
1301 case STORAGE_CLASS_THREAD_EXTERN:
1302 case STORAGE_CLASS_THREAD_STATIC: {
1303 ir_node *const no_mem = new_NoMem();
1304 ir_node *const tls = get_irg_tls(current_ir_graph);
1305 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1309 return create_symconst(dbgi, mode_P_data, entity);
1314 * Returns the correct base address depending on whether it is a parameter or a
1315 * normal local variable.
1317 static ir_node *get_local_frame(ir_entity *const ent)
1319 ir_graph *const irg = current_ir_graph;
1320 const ir_type *const owner = get_entity_owner(ent);
1321 if (owner == get_irg_frame_type(irg)) {
1322 return get_irg_frame(irg);
1324 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1325 return get_irg_value_param_base(irg);
1329 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1331 ir_mode *value_mode = get_irn_mode(value);
1333 if (value_mode == dest_mode || is_Bad(value))
1336 if (dest_mode == mode_b) {
1337 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1338 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1339 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1343 return new_d_Conv(dbgi, value, dest_mode);
1346 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1348 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1349 declaration_t *declaration = ref->declaration;
1350 type_t *type = skip_typeref(declaration->type);
1352 /* make sure the type is constructed */
1353 (void) get_ir_type(type);
1355 switch((declaration_kind_t) declaration->declaration_kind) {
1356 case DECLARATION_KIND_TYPE:
1357 case DECLARATION_KIND_UNKNOWN:
1360 case DECLARATION_KIND_ENUM_ENTRY: {
1361 ir_mode *const mode = get_ir_mode(type);
1362 return new_Const(mode, declaration->v.enum_val);
1365 case DECLARATION_KIND_LOCAL_VARIABLE: {
1366 ir_mode *const mode = get_ir_mode(type);
1367 return get_value(declaration->v.value_number, mode);
1369 case DECLARATION_KIND_FUNCTION: {
1370 ir_mode *const mode = get_ir_mode(type);
1371 return create_symconst(dbgi, mode, declaration->v.entity);
1373 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1374 ir_node *const addr = get_global_var_address(dbgi, declaration);
1375 return deref_address(dbgi, declaration->type, addr);
1378 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1379 ir_entity *entity = declaration->v.entity;
1380 ir_node *frame = get_local_frame(entity);
1381 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1382 return deref_address(dbgi, declaration->type, sel);
1385 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1386 return declaration->v.vla_base;
1388 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1389 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1390 case DECLARATION_KIND_COMPOUND_MEMBER:
1391 case DECLARATION_KIND_LABEL_BLOCK:
1392 panic("not implemented reference type");
1395 panic("reference to declaration with unknown type found");
1398 static ir_node *reference_addr(const reference_expression_t *ref)
1400 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1401 declaration_t *declaration = ref->declaration;
1403 switch((declaration_kind_t) declaration->declaration_kind) {
1404 case DECLARATION_KIND_TYPE:
1405 case DECLARATION_KIND_UNKNOWN:
1407 case DECLARATION_KIND_LOCAL_VARIABLE:
1408 /* you can store to a local variable (so we don't panic but return NULL
1409 * as an indicator for no real address) */
1411 case DECLARATION_KIND_FUNCTION: {
1412 type_t *const type = skip_typeref(ref->base.type);
1413 ir_mode *const mode = get_ir_mode(type);
1414 return create_symconst(dbgi, mode, declaration->v.entity);
1416 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1417 ir_node *const addr = get_global_var_address(dbgi, declaration);
1420 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1421 ir_entity *entity = declaration->v.entity;
1422 ir_node *frame = get_local_frame(entity);
1423 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1428 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1429 return declaration->v.vla_base;
1431 case DECLARATION_KIND_ENUM_ENTRY:
1432 panic("trying to reference enum entry");
1434 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1435 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1436 case DECLARATION_KIND_COMPOUND_MEMBER:
1437 case DECLARATION_KIND_LABEL_BLOCK:
1438 panic("not implemented reference type");
1441 panic("reference to declaration with unknown type found");
1445 * Transform calls to builtin functions.
1447 static ir_node *process_builtin_call(const call_expression_t *call)
1449 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1451 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1452 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1454 type_t *type = skip_typeref(builtin->base.type);
1455 assert(is_type_pointer(type));
1457 type_t *function_type = skip_typeref(type->pointer.points_to);
1458 symbol_t *symbol = builtin->symbol;
1460 switch(symbol->ID) {
1461 case T___builtin_alloca: {
1462 if (call->arguments == NULL || call->arguments->next != NULL) {
1463 panic("invalid number of parameters on __builtin_alloca");
1465 expression_t *argument = call->arguments->expression;
1466 ir_node *size = expression_to_firm(argument);
1468 ir_node *store = get_store();
1469 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1471 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1473 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1477 case T___builtin_huge_val: {
1478 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1479 tarval *tv = get_mode_infinite(mode);
1480 ir_node *res = new_d_Const(dbgi, mode, tv);
1483 case T___builtin_nan:
1484 case T___builtin_nanf:
1485 case T___builtin_nand: {
1486 /* Ignore string for now... */
1487 assert(is_type_function(function_type));
1488 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1489 tarval *tv = get_mode_NAN(mode);
1490 ir_node *res = new_d_Const(dbgi, mode, tv);
1493 case T___builtin_va_end:
1496 panic("Unsupported builtin found\n");
1501 * Transform a call expression.
1502 * Handles some special cases, like alloca() calls, which must be resolved
1503 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1504 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1507 static ir_node *call_expression_to_firm(const call_expression_t *call)
1509 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1510 assert(get_cur_block() != NULL);
1512 expression_t *function = call->function;
1513 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1514 return process_builtin_call(call);
1516 if (function->kind == EXPR_REFERENCE) {
1517 const reference_expression_t *ref = &function->reference;
1518 declaration_t *declaration = ref->declaration;
1520 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1521 if (declaration->v.entity == rts_entities[rts_alloca]) {
1522 /* handle alloca() call */
1523 expression_t *argument = call->arguments->expression;
1524 ir_node *size = expression_to_firm(argument);
1526 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1528 ir_node *store = get_store();
1529 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1530 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1532 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1534 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1540 ir_node *callee = expression_to_firm(function);
1542 type_t *type = skip_typeref(function->base.type);
1543 assert(is_type_pointer(type));
1544 pointer_type_t *pointer_type = &type->pointer;
1545 type_t *points_to = skip_typeref(pointer_type->points_to);
1546 assert(is_type_function(points_to));
1547 function_type_t *function_type = &points_to->function;
1549 int n_parameters = 0;
1550 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1551 ir_type *new_method_type = NULL;
1552 if (function_type->variadic || function_type->unspecified_parameters) {
1553 const call_argument_t *argument = call->arguments;
1554 for( ; argument != NULL; argument = argument->next) {
1558 /* we need to construct a new method type matching the call
1560 int n_res = get_method_n_ress(ir_method_type);
1561 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1562 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1563 n_parameters, n_res, dbgi);
1564 set_method_calling_convention(new_method_type,
1565 get_method_calling_convention(ir_method_type));
1566 set_method_additional_properties(new_method_type,
1567 get_method_additional_properties(ir_method_type));
1568 set_method_variadicity(new_method_type,
1569 get_method_variadicity(ir_method_type));
1571 for(int i = 0; i < n_res; ++i) {
1572 set_method_res_type(new_method_type, i,
1573 get_method_res_type(ir_method_type, i));
1575 argument = call->arguments;
1576 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1577 expression_t *expression = argument->expression;
1578 ir_type *irtype = get_ir_type(expression->base.type);
1579 set_method_param_type(new_method_type, i, irtype);
1581 ir_method_type = new_method_type;
1583 n_parameters = get_method_n_params(ir_method_type);
1586 ir_node *in[n_parameters];
1588 const call_argument_t *argument = call->arguments;
1589 for(int n = 0; n < n_parameters; ++n) {
1590 expression_t *expression = argument->expression;
1591 ir_node *arg_node = expression_to_firm(expression);
1593 arg_node = do_strict_conv(dbgi, arg_node);
1597 argument = argument->next;
1600 ir_node *store = get_store();
1601 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1603 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1606 type_t *return_type = skip_typeref(function_type->return_type);
1607 ir_node *result = NULL;
1609 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1611 if (is_type_scalar(return_type)) {
1612 mode = get_ir_mode(return_type);
1616 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1617 result = new_d_Proj(dbgi, resproj, mode, 0);
1623 static void statement_to_firm(statement_t *statement);
1624 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1626 static ir_node *expression_to_addr(const expression_t *expression);
1627 static void create_condition_evaluation(const expression_t *expression,
1628 ir_node *true_block,
1629 ir_node *false_block);
1631 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1634 value = do_strict_conv(dbgi, value);
1636 ir_node *memory = get_store();
1638 if (is_type_scalar(type)) {
1639 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1640 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1641 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1642 set_Store_volatility(store, volatility_is_volatile);
1643 set_store(store_mem);
1645 ir_type *irtype = get_ir_type(type);
1646 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1647 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1648 set_store(copyb_mem);
1652 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1654 tarval *all_one = get_mode_all_one(mode);
1655 int mode_size = get_mode_size_bits(mode);
1657 assert(offset >= 0 && size >= 0);
1658 assert(offset + size <= mode_size);
1659 if (size == mode_size) {
1663 long shiftr = get_mode_size_bits(mode) - size;
1664 long shiftl = offset;
1665 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1666 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1667 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1668 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1673 static void bitfield_store_to_firm(const select_expression_t *expression,
1674 ir_node *addr, ir_node *value)
1676 type_t *type = expression->base.type;
1677 ir_mode *mode = get_ir_mode(type);
1679 assert(get_irn_mode(value) == mode || is_Bad(value));
1681 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1683 /* kill upper bits of value and shift to right position */
1684 ir_entity *entity = expression->compound_entry->v.entity;
1685 int bitoffset = get_entity_offset_bits_remainder(entity);
1686 ir_type *entity_type = get_entity_type(entity);
1687 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1689 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1690 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1691 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1692 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1693 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1694 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1696 /* load current value */
1697 ir_node *mem = get_store();
1698 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1699 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1700 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1701 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1702 tarval *inv_mask = tarval_not(shift_mask);
1703 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1704 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1706 /* construct new value and store */
1707 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1708 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1709 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1710 set_store(store_mem);
1712 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1713 set_Load_volatility(load, volatility_is_volatile);
1714 set_Store_volatility(store, volatility_is_volatile);
1718 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1721 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1722 type_t *type = expression->base.type;
1723 ir_mode *mode = get_ir_mode(type);
1724 ir_node *mem = get_store();
1725 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1726 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1727 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1729 load_res = create_conv(dbgi, load_res, mode_int);
1731 set_store(load_mem);
1733 /* kill upper bits */
1734 ir_entity *entity = expression->compound_entry->v.entity;
1735 int bitoffset = get_entity_offset_bits_remainder(entity);
1736 ir_type *entity_type = get_entity_type(entity);
1737 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1738 long shift_bitsl = machine_size - bitoffset - bitsize;
1739 assert(shift_bitsl >= 0);
1740 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1741 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1742 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1744 long shift_bitsr = bitoffset + shift_bitsl;
1745 assert(shift_bitsr <= (long) machine_size);
1746 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1747 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1749 if (mode_is_signed(mode)) {
1750 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1752 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1755 return create_conv(dbgi, shiftr, mode);
1758 static void set_value_for_expression_addr(const expression_t *expression,
1759 ir_node *value, ir_node *addr)
1761 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1762 value = do_strict_conv(dbgi, value);
1764 if (expression->kind == EXPR_REFERENCE) {
1765 const reference_expression_t *ref = &expression->reference;
1767 declaration_t *declaration = ref->declaration;
1768 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1769 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1770 set_value(declaration->v.value_number, value);
1776 addr = expression_to_addr(expression);
1778 type_t *type = skip_typeref(expression->base.type);
1780 if (expression->kind == EXPR_SELECT) {
1781 const select_expression_t *select = &expression->select;
1783 declaration_t *declaration = select->compound_entry;
1784 if (declaration->type->kind == TYPE_BITFIELD) {
1785 bitfield_store_to_firm(select, addr, value);
1790 assign_value(dbgi, addr, type, value);
1793 static void set_value_for_expression(const expression_t *expression,
1796 set_value_for_expression_addr(expression, value, NULL);
1799 static ir_node *get_value_from_lvalue(const expression_t *expression,
1802 if (expression->kind == EXPR_REFERENCE) {
1803 const reference_expression_t *ref = &expression->reference;
1805 declaration_t *declaration = ref->declaration;
1806 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1807 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1808 assert(addr == NULL);
1809 ir_mode *mode = get_ir_mode(expression->base.type);
1810 return get_value(declaration->v.value_number, mode);
1814 assert(addr != NULL);
1815 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1818 if (expression->kind == EXPR_SELECT &&
1819 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1820 value = bitfield_extract_to_firm(&expression->select, addr);
1822 value = deref_address(dbgi, expression->base.type, addr);
1829 static ir_node *create_incdec(const unary_expression_t *expression)
1831 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1832 const expression_t *value_expr = expression->value;
1833 ir_node *addr = expression_to_addr(value_expr);
1834 ir_node *value = get_value_from_lvalue(value_expr, addr);
1836 type_t *type = skip_typeref(expression->base.type);
1837 ir_mode *mode = get_ir_mode(expression->base.type);
1840 if (is_type_pointer(type)) {
1841 pointer_type_t *pointer_type = &type->pointer;
1842 offset = get_type_size(pointer_type->points_to);
1844 assert(is_type_arithmetic(type));
1845 offset = new_Const(mode, get_mode_one(mode));
1849 ir_node *store_value;
1850 switch(expression->base.kind) {
1851 case EXPR_UNARY_POSTFIX_INCREMENT:
1853 store_value = new_d_Add(dbgi, value, offset, mode);
1855 case EXPR_UNARY_POSTFIX_DECREMENT:
1857 store_value = new_d_Sub(dbgi, value, offset, mode);
1859 case EXPR_UNARY_PREFIX_INCREMENT:
1860 result = new_d_Add(dbgi, value, offset, mode);
1861 store_value = result;
1863 case EXPR_UNARY_PREFIX_DECREMENT:
1864 result = new_d_Sub(dbgi, value, offset, mode);
1865 store_value = result;
1868 panic("no incdec expr in create_incdec");
1871 set_value_for_expression_addr(value_expr, store_value, addr);
1876 static bool is_local_variable(expression_t *expression)
1878 if (expression->kind != EXPR_REFERENCE)
1880 reference_expression_t *ref_expr = &expression->reference;
1881 declaration_t *declaration = ref_expr->declaration;
1882 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1885 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1888 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1889 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1890 case EXPR_BINARY_NOTEQUAL:
1891 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1892 case EXPR_BINARY_ISLESS:
1893 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1894 case EXPR_BINARY_ISLESSEQUAL:
1895 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1896 case EXPR_BINARY_ISGREATER:
1897 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1898 case EXPR_BINARY_ISGREATEREQUAL:
1899 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1900 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1905 panic("trying to get pn_Cmp from non-comparison binexpr type");
1909 * Handle the assume optimizer hint: check if a Confirm
1910 * node can be created.
1912 * @param dbi debug info
1913 * @param expr the IL assume expression
1915 * we support here only some simple cases:
1920 static ir_node *handle_assume_compare(dbg_info *dbi,
1921 const binary_expression_t *expression)
1923 expression_t *op1 = expression->left;
1924 expression_t *op2 = expression->right;
1925 declaration_t *var2, *var = NULL;
1926 ir_node *res = NULL;
1929 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1931 if (is_local_variable(op1) && is_local_variable(op2)) {
1932 var = op1->reference.declaration;
1933 var2 = op2->reference.declaration;
1935 type_t *const type = skip_typeref(var->type);
1936 ir_mode *const mode = get_ir_mode(type);
1938 ir_node *const irn1 = get_value(var->v.value_number, mode);
1939 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1941 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1942 set_value(var2->v.value_number, res);
1944 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1945 set_value(var->v.value_number, res);
1951 if (is_local_variable(op1) && is_constant_expression(op2)) {
1952 var = op1->reference.declaration;
1954 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
1955 cmp_val = get_inversed_pnc(cmp_val);
1956 var = op2->reference.declaration;
1961 type_t *const type = skip_typeref(var->type);
1962 ir_mode *const mode = get_ir_mode(type);
1964 res = get_value(var->v.value_number, mode);
1965 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
1966 set_value(var->v.value_number, res);
1972 * Handle the assume optimizer hint.
1974 * @param dbi debug info
1975 * @param expr the IL assume expression
1977 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
1978 switch(expression->kind) {
1979 case EXPR_BINARY_EQUAL:
1980 case EXPR_BINARY_NOTEQUAL:
1981 case EXPR_BINARY_LESS:
1982 case EXPR_BINARY_LESSEQUAL:
1983 case EXPR_BINARY_GREATER:
1984 case EXPR_BINARY_GREATEREQUAL:
1985 return handle_assume_compare(dbi, &expression->binary);
1991 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
1993 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1994 type_t *type = skip_typeref(expression->base.type);
1996 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
1997 return expression_to_addr(expression->value);
1999 const expression_t *value = expression->value;
2001 switch(expression->base.kind) {
2002 case EXPR_UNARY_NEGATE: {
2003 ir_node *value_node = expression_to_firm(value);
2004 ir_mode *mode = get_ir_mode(type);
2005 return new_d_Minus(dbgi, value_node, mode);
2007 case EXPR_UNARY_PLUS:
2008 return expression_to_firm(value);
2009 case EXPR_UNARY_BITWISE_NEGATE: {
2010 ir_node *value_node = expression_to_firm(value);
2011 ir_mode *mode = get_ir_mode(type);
2012 return new_d_Not(dbgi, value_node, mode);
2014 case EXPR_UNARY_NOT: {
2015 ir_node *value_node = _expression_to_firm(value);
2016 value_node = create_conv(dbgi, value_node, mode_b);
2017 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2020 case EXPR_UNARY_DEREFERENCE: {
2021 ir_node *value_node = expression_to_firm(value);
2022 type_t *value_type = skip_typeref(value->base.type);
2023 assert(is_type_pointer(value_type));
2024 type_t *points_to = value_type->pointer.points_to;
2025 return deref_address(dbgi, points_to, value_node);
2027 case EXPR_UNARY_POSTFIX_INCREMENT:
2028 case EXPR_UNARY_POSTFIX_DECREMENT:
2029 case EXPR_UNARY_PREFIX_INCREMENT:
2030 case EXPR_UNARY_PREFIX_DECREMENT:
2031 return create_incdec(expression);
2032 case EXPR_UNARY_CAST: {
2033 ir_node *value_node = expression_to_firm(value);
2034 if (is_type_scalar(type)) {
2035 ir_mode *mode = get_ir_mode(type);
2036 ir_node *node = create_conv(dbgi, value_node, mode);
2037 node = do_strict_conv(dbgi, node);
2040 /* make sure firm type is constructed */
2041 (void) get_ir_type(type);
2045 case EXPR_UNARY_CAST_IMPLICIT: {
2046 ir_node *value_node = expression_to_firm(value);
2047 if (is_type_scalar(type)) {
2048 ir_mode *mode = get_ir_mode(type);
2049 return create_conv(dbgi, value_node, mode);
2054 case EXPR_UNARY_ASSUME:
2055 if (firm_opt.confirm)
2056 return handle_assume(dbgi, value);
2063 panic("invalid UNEXPR type found");
2067 * produces a 0/1 depending of the value of a mode_b node
2069 static ir_node *produce_condition_result(const expression_t *expression,
2072 ir_mode *mode = get_ir_mode(expression->base.type);
2073 ir_node *cur_block = get_cur_block();
2075 ir_node *one_block = new_immBlock();
2076 ir_node *one = new_Const(mode, get_mode_one(mode));
2077 ir_node *jmp_one = new_d_Jmp(dbgi);
2079 ir_node *zero_block = new_immBlock();
2080 ir_node *zero = new_Const(mode, get_mode_null(mode));
2081 ir_node *jmp_zero = new_d_Jmp(dbgi);
2083 set_cur_block(cur_block);
2084 create_condition_evaluation(expression, one_block, zero_block);
2085 mature_immBlock(one_block);
2086 mature_immBlock(zero_block);
2088 ir_node *common_block = new_immBlock();
2089 add_immBlock_pred(common_block, jmp_one);
2090 add_immBlock_pred(common_block, jmp_zero);
2091 mature_immBlock(common_block);
2093 ir_node *in[2] = { one, zero };
2094 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2099 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2100 ir_node *value, type_t *type)
2102 pointer_type_t *const pointer_type = &type->pointer;
2103 type_t *const points_to = skip_typeref(pointer_type->points_to);
2104 unsigned elem_size = get_type_size_const(points_to);
2107 if (elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
2111 assert(elem_size >= 1);
2115 value = create_conv(dbgi, value, mode_int);
2116 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2117 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2121 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2122 ir_node *left, ir_node *right)
2125 type_t *type_left = skip_typeref(expression->left->base.type);
2126 type_t *type_right = skip_typeref(expression->right->base.type);
2128 expression_kind_t kind = expression->base.kind;
2131 case EXPR_BINARY_SHIFTLEFT:
2132 case EXPR_BINARY_SHIFTRIGHT:
2133 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2134 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2135 mode = get_irn_mode(left);
2136 right = create_conv(dbgi, right, mode_uint);
2139 case EXPR_BINARY_SUB:
2140 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2141 const pointer_type_t *const ptr_type = &type_left->pointer;
2143 mode = get_ir_mode(expression->base.type);
2144 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2145 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2146 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2147 ir_node *const no_mem = new_NoMem();
2148 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2149 mode, op_pin_state_floats);
2150 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2153 case EXPR_BINARY_SUB_ASSIGN:
2154 if (is_type_pointer(type_left)) {
2155 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2156 mode = get_ir_mode(type_left);
2161 case EXPR_BINARY_ADD:
2162 case EXPR_BINARY_ADD_ASSIGN:
2163 if (is_type_pointer(type_left)) {
2164 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2165 mode = get_ir_mode(type_left);
2167 } else if (is_type_pointer(type_right)) {
2168 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2169 mode = get_ir_mode(type_right);
2176 mode = get_irn_mode(right);
2177 left = create_conv(dbgi, left, mode);
2182 case EXPR_BINARY_ADD_ASSIGN:
2183 case EXPR_BINARY_ADD:
2184 return new_d_Add(dbgi, left, right, mode);
2185 case EXPR_BINARY_SUB_ASSIGN:
2186 case EXPR_BINARY_SUB:
2187 return new_d_Sub(dbgi, left, right, mode);
2188 case EXPR_BINARY_MUL_ASSIGN:
2189 case EXPR_BINARY_MUL:
2190 return new_d_Mul(dbgi, left, right, mode);
2191 case EXPR_BINARY_BITWISE_AND:
2192 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2193 return new_d_And(dbgi, left, right, mode);
2194 case EXPR_BINARY_BITWISE_OR:
2195 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2196 return new_d_Or(dbgi, left, right, mode);
2197 case EXPR_BINARY_BITWISE_XOR:
2198 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2199 return new_d_Eor(dbgi, left, right, mode);
2200 case EXPR_BINARY_SHIFTLEFT:
2201 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2202 return new_d_Shl(dbgi, left, right, mode);
2203 case EXPR_BINARY_SHIFTRIGHT:
2204 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2205 if (mode_is_signed(mode)) {
2206 return new_d_Shrs(dbgi, left, right, mode);
2208 return new_d_Shr(dbgi, left, right, mode);
2210 case EXPR_BINARY_DIV:
2211 case EXPR_BINARY_DIV_ASSIGN: {
2212 ir_node *pin = new_Pin(new_NoMem());
2215 if (mode_is_float(mode)) {
2216 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2217 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2219 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2220 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2224 case EXPR_BINARY_MOD:
2225 case EXPR_BINARY_MOD_ASSIGN: {
2226 ir_node *pin = new_Pin(new_NoMem());
2227 assert(!mode_is_float(mode));
2228 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2229 op_pin_state_floats);
2230 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2234 panic("unexpected expression kind");
2238 static ir_node *create_lazy_op(const binary_expression_t *expression)
2240 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2241 type_t *type = expression->base.type;
2242 ir_mode *mode = get_ir_mode(type);
2244 if (is_constant_expression(expression->left)) {
2245 long val = fold_constant(expression->left);
2246 expression_kind_t ekind = expression->base.kind;
2247 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0)
2248 || (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2249 return expression_to_firm(expression->right);
2251 assert((ekind == EXPR_BINARY_LOGICAL_AND && val == 0)
2252 || (ekind == EXPR_BINARY_LOGICAL_OR && val != 0));
2253 return new_Const(mode, get_mode_one(mode));
2257 return produce_condition_result((const expression_t*) expression, dbgi);
2260 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2261 ir_node *right, ir_mode *mode);
2263 static ir_node *create_assign_binop(const binary_expression_t *expression)
2265 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2266 const expression_t *left_expr = expression->left;
2267 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2268 ir_node *left_addr = expression_to_addr(left_expr);
2269 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2270 ir_node *right = expression_to_firm(expression->right);
2271 ir_node *result = create_op(dbgi, expression, left, right);
2273 result = create_conv(dbgi, result, left_mode);
2274 result = do_strict_conv(dbgi, result);
2276 set_value_for_expression_addr(left_expr, result, left_addr);
2281 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2283 expression_kind_t kind = expression->base.kind;
2286 case EXPR_BINARY_EQUAL:
2287 case EXPR_BINARY_NOTEQUAL:
2288 case EXPR_BINARY_LESS:
2289 case EXPR_BINARY_LESSEQUAL:
2290 case EXPR_BINARY_GREATER:
2291 case EXPR_BINARY_GREATEREQUAL:
2292 case EXPR_BINARY_ISGREATER:
2293 case EXPR_BINARY_ISGREATEREQUAL:
2294 case EXPR_BINARY_ISLESS:
2295 case EXPR_BINARY_ISLESSEQUAL:
2296 case EXPR_BINARY_ISLESSGREATER:
2297 case EXPR_BINARY_ISUNORDERED: {
2298 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2299 ir_node *left = expression_to_firm(expression->left);
2300 ir_node *right = expression_to_firm(expression->right);
2301 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2302 long pnc = get_pnc(kind, expression->left->base.type);
2303 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2306 case EXPR_BINARY_ASSIGN: {
2307 ir_node *addr = expression_to_addr(expression->left);
2308 ir_node *right = expression_to_firm(expression->right);
2309 set_value_for_expression_addr(expression->left, right, addr);
2313 case EXPR_BINARY_ADD:
2314 case EXPR_BINARY_SUB:
2315 case EXPR_BINARY_MUL:
2316 case EXPR_BINARY_DIV:
2317 case EXPR_BINARY_MOD:
2318 case EXPR_BINARY_BITWISE_AND:
2319 case EXPR_BINARY_BITWISE_OR:
2320 case EXPR_BINARY_BITWISE_XOR:
2321 case EXPR_BINARY_SHIFTLEFT:
2322 case EXPR_BINARY_SHIFTRIGHT:
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 return create_op(dbgi, expression, left, right);
2329 case EXPR_BINARY_LOGICAL_AND:
2330 case EXPR_BINARY_LOGICAL_OR:
2331 return create_lazy_op(expression);
2332 case EXPR_BINARY_COMMA:
2333 /* create side effects of left side */
2334 (void) expression_to_firm(expression->left);
2335 return _expression_to_firm(expression->right);
2337 case EXPR_BINARY_ADD_ASSIGN:
2338 case EXPR_BINARY_SUB_ASSIGN:
2339 case EXPR_BINARY_MUL_ASSIGN:
2340 case EXPR_BINARY_MOD_ASSIGN:
2341 case EXPR_BINARY_DIV_ASSIGN:
2342 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2343 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2344 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2345 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2346 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2347 return create_assign_binop(expression);
2348 case EXPR_BINARY_BUILTIN_EXPECT:
2349 return _expression_to_firm(expression->left);
2351 panic("TODO binexpr type");
2355 static ir_node *array_access_addr(const array_access_expression_t *expression)
2357 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2358 ir_node *base_addr = expression_to_firm(expression->array_ref);
2359 ir_node *offset = expression_to_firm(expression->index);
2361 type_t *offset_type = skip_typeref(expression->index->base.type);
2363 if (is_type_signed(offset_type)) {
2364 mode = get_ir_mode(type_ssize_t);
2366 mode = get_ir_mode(type_size_t);
2368 offset = create_conv(dbgi, offset, mode);
2370 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2371 assert(is_type_pointer(ref_type));
2372 pointer_type_t *pointer_type = &ref_type->pointer;
2374 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2375 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2376 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2378 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2383 static ir_node *array_access_to_firm(
2384 const array_access_expression_t *expression)
2386 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2387 ir_node *addr = array_access_addr(expression);
2388 type_t *type = revert_automatic_type_conversion(
2389 (const expression_t*) expression);
2390 type = skip_typeref(type);
2392 return deref_address(dbgi, type, addr);
2395 static long get_offsetof_offset(const offsetof_expression_t *expression)
2397 type_t *orig_type = expression->type;
2400 designator_t *designator = expression->designator;
2401 for( ; designator != NULL; designator = designator->next) {
2402 type_t *type = skip_typeref(orig_type);
2403 /* be sure the type is constructed */
2404 (void) get_ir_type(type);
2406 if (designator->symbol != NULL) {
2407 assert(is_type_compound(type));
2408 symbol_t *symbol = designator->symbol;
2410 declaration_t *declaration = type->compound.declaration;
2411 declaration_t *iter = declaration->scope.declarations;
2412 for( ; iter != NULL; iter = iter->next) {
2413 if (iter->symbol == symbol) {
2417 assert(iter != NULL);
2419 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2420 offset += get_entity_offset(iter->v.entity);
2422 orig_type = iter->type;
2424 expression_t *array_index = designator->array_index;
2425 assert(designator->array_index != NULL);
2426 assert(is_type_array(type));
2427 assert(is_type_valid(array_index->base.type));
2429 long index = fold_constant(array_index);
2430 ir_type *arr_type = get_ir_type(type);
2431 ir_type *elem_type = get_array_element_type(arr_type);
2432 long elem_size = get_type_size_bytes(elem_type);
2434 offset += index * elem_size;
2436 orig_type = type->array.element_type;
2443 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2445 ir_mode *mode = get_ir_mode(expression->base.type);
2446 long offset = get_offsetof_offset(expression);
2447 tarval *tv = new_tarval_from_long(offset, mode);
2448 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2450 return new_d_Const(dbgi, mode, tv);
2453 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2454 ir_entity *entity, type_t *type);
2456 static ir_node *compound_literal_to_firm(
2457 const compound_literal_expression_t *expression)
2459 type_t *type = expression->type;
2461 /* create an entity on the stack */
2462 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2464 ident *const id = id_unique("CompLit.%u");
2465 ir_type *const irtype = get_ir_type(type);
2466 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2467 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2468 set_entity_ld_ident(entity, id);
2470 set_entity_variability(entity, variability_uninitialized);
2472 /* create initialisation code */
2473 initializer_t *initializer = expression->initializer;
2474 create_local_initializer(initializer, dbgi, entity, type);
2476 /* create a sel for the compound literal address */
2477 ir_node *frame = get_local_frame(entity);
2478 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2483 * Transform a sizeof expression into Firm code.
2485 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2487 type_t *type = expression->type;
2489 type = expression->tp_expression->base.type;
2490 assert(type != NULL);
2493 type = skip_typeref(type);
2494 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2495 if (is_type_array(type) && type->array.is_vla
2496 && expression->tp_expression != NULL) {
2497 expression_to_firm(expression->tp_expression);
2500 return get_type_size(type);
2504 * Transform an alignof expression into Firm code.
2506 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2508 type_t *type = expression->type;
2510 /* beware: if expression is a variable reference, return the
2511 alignment of the variable. */
2512 const expression_t *tp_expression = expression->tp_expression;
2513 const declaration_t *declaration = expr_is_variable(tp_expression);
2514 if (declaration != NULL) {
2515 /* TODO: get the alignment of this variable. */
2517 type = tp_expression->base.type;
2518 assert(type != NULL);
2521 ir_mode *const mode = get_ir_mode(expression->base.type);
2522 symconst_symbol sym;
2523 sym.type_p = get_ir_type(type);
2524 return new_SymConst(mode, sym, symconst_type_align);
2527 static void init_ir_types(void);
2528 long fold_constant(const expression_t *expression)
2530 bool constant_folding_old = constant_folding;
2531 constant_folding = true;
2535 assert(is_constant_expression(expression));
2537 ir_graph *old_current_ir_graph = current_ir_graph;
2538 if (current_ir_graph == NULL) {
2539 current_ir_graph = get_const_code_irg();
2542 ir_node *cnst = expression_to_firm(expression);
2543 current_ir_graph = old_current_ir_graph;
2545 if (!is_Const(cnst)) {
2546 panic("couldn't fold constant\n");
2549 tarval *tv = get_Const_tarval(cnst);
2550 if (!tarval_is_long(tv)) {
2551 panic("result of constant folding is not integer\n");
2554 constant_folding = constant_folding_old;
2556 return get_tarval_long(tv);
2559 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2561 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2563 /* first try to fold a constant condition */
2564 if (is_constant_expression(expression->condition)) {
2565 long val = fold_constant(expression->condition);
2567 return expression_to_firm(expression->true_expression);
2569 return expression_to_firm(expression->false_expression);
2573 ir_node *cur_block = get_cur_block();
2575 /* create the true block */
2576 ir_node *true_block = new_immBlock();
2578 ir_node *true_val = expression_to_firm(expression->true_expression);
2579 ir_node *true_jmp = new_Jmp();
2581 /* create the false block */
2582 ir_node *false_block = new_immBlock();
2584 ir_node *false_val = expression_to_firm(expression->false_expression);
2585 ir_node *false_jmp = new_Jmp();
2587 /* create the condition evaluation */
2588 set_cur_block(cur_block);
2589 create_condition_evaluation(expression->condition, true_block, false_block);
2590 mature_immBlock(true_block);
2591 mature_immBlock(false_block);
2593 /* create the common block */
2594 ir_node *common_block = new_immBlock();
2595 add_immBlock_pred(common_block, true_jmp);
2596 add_immBlock_pred(common_block, false_jmp);
2597 mature_immBlock(common_block);
2599 /* TODO improve static semantics, so either both or no values are NULL */
2600 if (true_val == NULL || false_val == NULL)
2603 ir_node *in[2] = { true_val, false_val };
2604 ir_mode *mode = get_irn_mode(true_val);
2605 assert(get_irn_mode(false_val) == mode);
2606 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2612 * Returns an IR-node representing the address of a field.
2614 static ir_node *select_addr(const select_expression_t *expression)
2616 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2618 ir_node *compound_addr = expression_to_firm(expression->compound);
2620 /* make sure the type is constructed */
2621 type_t *type = skip_typeref(expression->compound->base.type);
2622 if (is_type_pointer(type)) {
2623 type = type->pointer.points_to;
2625 (void) get_ir_type(type);
2627 declaration_t *entry = expression->compound_entry;
2628 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2629 ir_entity *entity = entry->v.entity;
2631 assert(entity != NULL);
2633 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2638 static ir_node *select_to_firm(const select_expression_t *expression)
2640 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2641 ir_node *addr = select_addr(expression);
2642 type_t *type = revert_automatic_type_conversion(
2643 (const expression_t*) expression);
2644 type = skip_typeref(type);
2646 declaration_t *entry = expression->compound_entry;
2647 type_t *entry_type = skip_typeref(entry->type);
2649 if (entry_type->kind == TYPE_BITFIELD) {
2650 return bitfield_extract_to_firm(expression, addr);
2653 return deref_address(dbgi, type, addr);
2656 /* Values returned by __builtin_classify_type. */
2657 typedef enum gcc_type_class
2663 enumeral_type_class,
2666 reference_type_class,
2670 function_type_class,
2681 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2683 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2689 const atomic_type_t *const atomic_type = &type->atomic;
2690 switch (atomic_type->akind) {
2691 /* should not be reached */
2692 case ATOMIC_TYPE_INVALID:
2696 /* gcc cannot do that */
2697 case ATOMIC_TYPE_VOID:
2698 tc = void_type_class;
2701 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2702 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2703 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2704 case ATOMIC_TYPE_SHORT:
2705 case ATOMIC_TYPE_USHORT:
2706 case ATOMIC_TYPE_INT:
2707 case ATOMIC_TYPE_UINT:
2708 case ATOMIC_TYPE_LONG:
2709 case ATOMIC_TYPE_ULONG:
2710 case ATOMIC_TYPE_LONGLONG:
2711 case ATOMIC_TYPE_ULONGLONG:
2712 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2713 tc = integer_type_class;
2716 case ATOMIC_TYPE_FLOAT:
2717 case ATOMIC_TYPE_DOUBLE:
2718 case ATOMIC_TYPE_LONG_DOUBLE:
2719 tc = real_type_class;
2722 panic("Unexpected atomic type in classify_type_to_firm().");
2725 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2726 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2727 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2728 case TYPE_ARRAY: /* gcc handles this as pointer */
2729 case TYPE_FUNCTION: /* gcc handles this as pointer */
2730 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2731 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2732 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2734 /* gcc handles this as integer */
2735 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2738 /* typedef/typeof should be skipped already */
2745 panic("unexpected TYPE classify_type_to_firm().");
2748 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2749 ir_mode *const mode = mode_int;
2750 tarval *const tv = new_tarval_from_long(tc, mode);
2751 return new_d_Const(dbgi, mode, tv);
2754 static ir_node *function_name_to_firm(
2755 const funcname_expression_t *const expr)
2757 switch(expr->kind) {
2758 case FUNCNAME_FUNCTION:
2759 case FUNCNAME_PRETTY_FUNCTION:
2760 case FUNCNAME_FUNCDNAME:
2761 if (current_function_name == NULL) {
2762 const source_position_t *const src_pos = &expr->base.source_position;
2763 const char *const name = current_function_decl->symbol->string;
2764 const string_t string = { name, strlen(name) + 1 };
2765 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2767 return current_function_name;
2768 case FUNCNAME_FUNCSIG:
2769 if (current_funcsig == NULL) {
2770 const source_position_t *const src_pos = &expr->base.source_position;
2771 ir_entity *ent = get_irg_entity(current_ir_graph);
2772 const char *const name = get_entity_ld_name(ent);
2773 const string_t string = { name, strlen(name) + 1 };
2774 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2776 return current_funcsig;
2778 panic("Unsupported function name");
2781 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2783 statement_t *statement = expr->statement;
2785 assert(statement->kind == STATEMENT_COMPOUND);
2786 return compound_statement_to_firm(&statement->compound);
2789 static ir_node *va_start_expression_to_firm(
2790 const va_start_expression_t *const expr)
2792 ir_type *const method_type = get_ir_type(current_function_decl->type);
2793 int const n = get_method_n_params(method_type) - 1;
2794 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2795 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2796 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2797 ir_node *const no_mem = new_NoMem();
2798 ir_node *const arg_sel =
2799 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2801 ir_node *const cnst = get_type_size(expr->parameter->type);
2802 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2803 set_value_for_expression(expr->ap, add);
2808 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2810 type_t *const type = expr->base.type;
2811 expression_t *const ap_expr = expr->ap;
2812 ir_node *const ap_addr = expression_to_addr(ap_expr);
2813 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2814 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2815 ir_node *const res = deref_address(dbgi, type, ap);
2817 ir_node *const cnst = get_type_size(expr->base.type);
2818 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2820 set_value_for_expression_addr(ap_expr, add, ap_addr);
2825 static ir_node *dereference_addr(const unary_expression_t *const expression)
2827 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2828 return expression_to_firm(expression->value);
2832 * Returns a IR-node representing an lvalue of the given expression.
2834 static ir_node *expression_to_addr(const expression_t *expression)
2836 switch(expression->kind) {
2837 case EXPR_REFERENCE:
2838 return reference_addr(&expression->reference);
2839 case EXPR_ARRAY_ACCESS:
2840 return array_access_addr(&expression->array_access);
2842 return select_addr(&expression->select);
2844 return call_expression_to_firm(&expression->call);
2845 case EXPR_UNARY_DEREFERENCE: {
2846 return dereference_addr(&expression->unary);
2851 panic("trying to get address of non-lvalue");
2854 static ir_node *builtin_constant_to_firm(
2855 const builtin_constant_expression_t *expression)
2857 ir_mode *mode = get_ir_mode(expression->base.type);
2860 if (is_constant_expression(expression->value)) {
2865 return new_Const_long(mode, v);
2868 static ir_node *builtin_prefetch_to_firm(
2869 const builtin_prefetch_expression_t *expression)
2871 ir_node *adr = expression_to_firm(expression->adr);
2872 /* no Firm support for prefetch yet */
2878 * creates firm nodes for an expression. The difference between this function
2879 * and expression_to_firm is, that this version might produce mode_b nodes
2880 * instead of mode_Is.
2882 static ir_node *_expression_to_firm(const expression_t *expression)
2885 if (!constant_folding) {
2886 assert(!expression->base.transformed);
2887 ((expression_t*) expression)->base.transformed = true;
2891 switch(expression->kind) {
2892 case EXPR_CHARACTER_CONSTANT:
2893 return character_constant_to_firm(&expression->conste);
2894 case EXPR_WIDE_CHARACTER_CONSTANT:
2895 return wide_character_constant_to_firm(&expression->conste);
2897 return const_to_firm(&expression->conste);
2898 case EXPR_STRING_LITERAL:
2899 return string_literal_to_firm(&expression->string);
2900 case EXPR_WIDE_STRING_LITERAL:
2901 return wide_string_literal_to_firm(&expression->wide_string);
2902 case EXPR_REFERENCE:
2903 return reference_expression_to_firm(&expression->reference);
2905 return call_expression_to_firm(&expression->call);
2907 return unary_expression_to_firm(&expression->unary);
2909 return binary_expression_to_firm(&expression->binary);
2910 case EXPR_ARRAY_ACCESS:
2911 return array_access_to_firm(&expression->array_access);
2913 return sizeof_to_firm(&expression->typeprop);
2915 return alignof_to_firm(&expression->typeprop);
2916 case EXPR_CONDITIONAL:
2917 return conditional_to_firm(&expression->conditional);
2919 return select_to_firm(&expression->select);
2920 case EXPR_CLASSIFY_TYPE:
2921 return classify_type_to_firm(&expression->classify_type);
2923 return function_name_to_firm(&expression->funcname);
2924 case EXPR_STATEMENT:
2925 return statement_expression_to_firm(&expression->statement);
2927 return va_start_expression_to_firm(&expression->va_starte);
2929 return va_arg_expression_to_firm(&expression->va_arge);
2930 case EXPR_BUILTIN_SYMBOL:
2931 panic("unimplemented expression found");
2932 case EXPR_BUILTIN_CONSTANT_P:
2933 return builtin_constant_to_firm(&expression->builtin_constant);
2934 case EXPR_BUILTIN_PREFETCH:
2935 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2937 return offsetof_to_firm(&expression->offsetofe);
2938 case EXPR_COMPOUND_LITERAL:
2939 return compound_literal_to_firm(&expression->compound_literal);
2945 panic("invalid expression found");
2948 static bool produces_mode_b(const expression_t *expression)
2950 switch (expression->kind) {
2951 case EXPR_BINARY_EQUAL:
2952 case EXPR_BINARY_NOTEQUAL:
2953 case EXPR_BINARY_LESS:
2954 case EXPR_BINARY_LESSEQUAL:
2955 case EXPR_BINARY_GREATER:
2956 case EXPR_BINARY_GREATEREQUAL:
2957 case EXPR_BINARY_ISGREATER:
2958 case EXPR_BINARY_ISGREATEREQUAL:
2959 case EXPR_BINARY_ISLESS:
2960 case EXPR_BINARY_ISLESSEQUAL:
2961 case EXPR_BINARY_ISLESSGREATER:
2962 case EXPR_BINARY_ISUNORDERED:
2963 case EXPR_UNARY_NOT:
2966 case EXPR_BINARY_BUILTIN_EXPECT:
2967 return produces_mode_b(expression->binary.left);
2968 case EXPR_BINARY_COMMA:
2969 return produces_mode_b(expression->binary.right);
2976 static ir_node *expression_to_firm(const expression_t *expression)
2978 if (!produces_mode_b(expression)) {
2979 ir_node *res = _expression_to_firm(expression);
2980 assert(res == NULL || get_irn_mode(res) != mode_b);
2984 if (is_constant_expression(expression)) {
2985 ir_node *res = _expression_to_firm(expression);
2986 ir_mode *mode = get_ir_mode(expression->base.type);
2987 assert(is_Const(res));
2988 if (is_Const_null(res)) {
2989 return new_Const_long(mode, 0);
2991 return new_Const_long(mode, 1);
2995 /* we have to produce a 0/1 from the mode_b expression */
2996 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2997 return produce_condition_result(expression, dbgi);
3000 static ir_node *expression_to_modeb(const expression_t *expression)
3002 ir_node *res = _expression_to_firm(expression);
3003 res = create_conv(NULL, res, mode_b);
3009 * create a short-circuit expression evaluation that tries to construct
3010 * efficient control flow structures for &&, || and ! expressions
3012 static void create_condition_evaluation(const expression_t *expression,
3013 ir_node *true_block,
3014 ir_node *false_block)
3016 switch(expression->kind) {
3017 case EXPR_UNARY_NOT: {
3018 const unary_expression_t *unary_expression = &expression->unary;
3019 create_condition_evaluation(unary_expression->value, false_block,
3023 case EXPR_BINARY_LOGICAL_AND: {
3024 const binary_expression_t *binary_expression = &expression->binary;
3026 ir_node *cur_block = get_cur_block();
3027 ir_node *extra_block = new_immBlock();
3028 set_cur_block(cur_block);
3029 create_condition_evaluation(binary_expression->left, extra_block,
3031 mature_immBlock(extra_block);
3032 set_cur_block(extra_block);
3033 create_condition_evaluation(binary_expression->right, true_block,
3037 case EXPR_BINARY_LOGICAL_OR: {
3038 const binary_expression_t *binary_expression = &expression->binary;
3040 ir_node *cur_block = get_cur_block();
3041 ir_node *extra_block = new_immBlock();
3042 set_cur_block(cur_block);
3043 create_condition_evaluation(binary_expression->left, true_block,
3045 mature_immBlock(extra_block);
3046 set_cur_block(extra_block);
3047 create_condition_evaluation(binary_expression->right, true_block,
3055 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3056 ir_node *condition = expression_to_modeb(expression);
3057 ir_node *cond = new_d_Cond(dbgi, condition);
3058 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3059 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3061 /* set branch prediction info based on __builtin_expect */
3062 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3063 long cnst = fold_constant(expression->binary.right);
3064 cond_jmp_predicate pred;
3067 pred = COND_JMP_PRED_FALSE;
3069 pred = COND_JMP_PRED_TRUE;
3071 set_Cond_jmp_pred(cond, pred);
3074 add_immBlock_pred(true_block, true_proj);
3075 if (false_block != NULL) {
3076 add_immBlock_pred(false_block, false_proj);
3079 set_cur_block(NULL);
3084 static void create_declaration_entity(declaration_t *declaration,
3085 declaration_kind_t declaration_kind,
3086 ir_type *parent_type)
3088 type_t *const type = skip_typeref(declaration->type);
3089 if (is_type_function(type)) {
3090 (void) get_function_entity(declaration);
3094 ident *const id = new_id_from_str(declaration->symbol->string);
3095 ir_type *const irtype = get_ir_type(type);
3096 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3097 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3099 declaration->declaration_kind = (unsigned char) declaration_kind;
3100 declaration->v.entity = entity;
3101 set_entity_variability(entity, variability_uninitialized);
3102 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3103 if (parent_type == get_tls_type())
3104 set_entity_allocation(entity, allocation_automatic);
3105 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3106 set_entity_allocation(entity, allocation_static);
3108 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3109 set_entity_volatility(entity, volatility_is_volatile);
3114 typedef struct type_path_entry_t type_path_entry_t;
3115 struct type_path_entry_t {
3117 ir_initializer_t *initializer;
3119 declaration_t *compound_entry;
3122 typedef struct type_path_t type_path_t;
3123 struct type_path_t {
3124 type_path_entry_t *path;
3129 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3131 size_t len = ARR_LEN(path->path);
3133 for(size_t i = 0; i < len; ++i) {
3134 const type_path_entry_t *entry = & path->path[i];
3136 type_t *type = skip_typeref(entry->type);
3137 if (is_type_compound(type)) {
3138 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3139 } else if (is_type_array(type)) {
3140 fprintf(stderr, "[%zd]", entry->index);
3142 fprintf(stderr, "-INVALID-");
3145 fprintf(stderr, " (");
3146 print_type(path->top_type);
3147 fprintf(stderr, ")");
3150 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3152 size_t len = ARR_LEN(path->path);
3154 return & path->path[len-1];
3157 static type_path_entry_t *append_to_type_path(type_path_t *path)
3159 size_t len = ARR_LEN(path->path);
3160 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3162 type_path_entry_t *result = & path->path[len];
3163 memset(result, 0, sizeof(result[0]));
3167 static size_t get_compound_size(const compound_type_t *type)
3169 declaration_t *declaration = type->declaration;
3170 declaration_t *member = declaration->scope.declarations;
3172 for( ; member != NULL; member = member->next) {
3175 /* TODO: cache results? */
3180 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3182 type_t *orig_top_type = path->top_type;
3183 type_t *top_type = skip_typeref(orig_top_type);
3185 assert(is_type_compound(top_type) || is_type_array(top_type));
3187 if (ARR_LEN(path->path) == 0) {
3190 type_path_entry_t *top = get_type_path_top(path);
3191 ir_initializer_t *initializer = top->initializer;
3192 return get_initializer_compound_value(initializer, top->index);
3196 static void descend_into_subtype(type_path_t *path)
3198 type_t *orig_top_type = path->top_type;
3199 type_t *top_type = skip_typeref(orig_top_type);
3201 assert(is_type_compound(top_type) || is_type_array(top_type));
3203 ir_initializer_t *initializer = get_initializer_entry(path);
3205 type_path_entry_t *top = append_to_type_path(path);
3206 top->type = top_type;
3210 if (is_type_compound(top_type)) {
3211 declaration_t *declaration = top_type->compound.declaration;
3212 declaration_t *entry = declaration->scope.declarations;
3214 top->compound_entry = entry;
3216 len = get_compound_size(&top_type->compound);
3218 path->top_type = entry->type;
3220 assert(is_type_array(top_type));
3221 assert(top_type->array.size > 0);
3224 path->top_type = top_type->array.element_type;
3225 len = top_type->array.size;
3227 if (initializer == NULL
3228 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3229 initializer = create_initializer_compound(len);
3230 /* we have to set the entry at the 2nd latest path entry... */
3231 size_t path_len = ARR_LEN(path->path);
3232 assert(path_len >= 1);
3234 type_path_entry_t *entry = & path->path[path_len-2];
3235 ir_initializer_t *tinitializer = entry->initializer;
3236 set_initializer_compound_value(tinitializer, entry->index,
3240 top->initializer = initializer;
3243 static void ascend_from_subtype(type_path_t *path)
3245 type_path_entry_t *top = get_type_path_top(path);
3247 path->top_type = top->type;
3249 size_t len = ARR_LEN(path->path);
3250 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3253 static void walk_designator(type_path_t *path, const designator_t *designator)
3255 /* designators start at current object type */
3256 ARR_RESIZE(type_path_entry_t, path->path, 1);
3258 for( ; designator != NULL; designator = designator->next) {
3259 type_path_entry_t *top = get_type_path_top(path);
3260 type_t *orig_type = top->type;
3261 type_t *type = skip_typeref(orig_type);
3263 if (designator->symbol != NULL) {
3264 assert(is_type_compound(type));
3266 symbol_t *symbol = designator->symbol;
3268 declaration_t *declaration = type->compound.declaration;
3269 declaration_t *iter = declaration->scope.declarations;
3270 for( ; iter != NULL; iter = iter->next, ++index) {
3271 if (iter->symbol == symbol) {
3275 assert(iter != NULL);
3277 top->type = orig_type;
3278 top->compound_entry = iter;
3280 orig_type = iter->type;
3282 expression_t *array_index = designator->array_index;
3283 assert(designator->array_index != NULL);
3284 assert(is_type_array(type));
3285 assert(is_type_valid(array_index->base.type));
3287 long index = fold_constant(array_index);
3290 if (type->array.size_constant == 1) {
3291 long array_size = type->array.size;
3292 assert(index < array_size);
3296 top->type = orig_type;
3297 top->index = (size_t) index;
3298 orig_type = type->array.element_type;
3300 path->top_type = orig_type;
3302 if (designator->next != NULL) {
3303 descend_into_subtype(path);
3307 path->invalid = false;
3310 static void advance_current_object(type_path_t *path)
3312 if (path->invalid) {
3313 /* TODO: handle this... */
3314 panic("invalid initializer in ast2firm (excessive elements)");
3318 type_path_entry_t *top = get_type_path_top(path);
3320 type_t *type = skip_typeref(top->type);
3321 if (is_type_union(type)) {
3322 top->compound_entry = NULL;
3323 } else if (is_type_struct(type)) {
3324 declaration_t *entry = top->compound_entry;
3327 entry = entry->next;
3328 top->compound_entry = entry;
3329 if (entry != NULL) {
3330 path->top_type = entry->type;
3334 assert(is_type_array(type));
3337 if (!type->array.size_constant || top->index < type->array.size) {
3342 /* we're past the last member of the current sub-aggregate, try if we
3343 * can ascend in the type hierarchy and continue with another subobject */
3344 size_t len = ARR_LEN(path->path);
3347 ascend_from_subtype(path);
3348 advance_current_object(path);
3350 path->invalid = true;
3355 static ir_initializer_t *create_ir_initializer(
3356 const initializer_t *initializer, type_t *type);
3358 static ir_initializer_t *create_ir_initializer_value(
3359 const initializer_value_t *initializer)
3361 if (is_type_compound(initializer->value->base.type)) {
3362 panic("initializer creation for compounds not implemented yet");
3364 ir_node *value = expression_to_firm(initializer->value);
3365 return create_initializer_const(value);
3368 static ir_initializer_t *create_ir_initializer_list(
3369 const initializer_list_t *initializer, type_t *type)
3372 memset(&path, 0, sizeof(path));
3373 path.top_type = type;
3374 path.path = NEW_ARR_F(type_path_entry_t, 0);
3376 descend_into_subtype(&path);
3378 for(size_t i = 0; i < initializer->len; ++i) {
3379 const initializer_t *sub_initializer = initializer->initializers[i];
3381 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3382 walk_designator(&path, sub_initializer->designator.designator);
3386 if (sub_initializer->kind == INITIALIZER_VALUE) {
3387 /* we might have to descend into types until we're at a scalar
3390 type_t *orig_top_type = path.top_type;
3391 type_t *top_type = skip_typeref(orig_top_type);
3393 if (is_type_scalar(top_type))
3395 descend_into_subtype(&path);
3399 ir_initializer_t *sub_irinitializer
3400 = create_ir_initializer(sub_initializer, path.top_type);
3402 size_t path_len = ARR_LEN(path.path);
3403 assert(path_len >= 1);
3404 type_path_entry_t *entry = & path.path[path_len-1];
3405 ir_initializer_t *tinitializer = entry->initializer;
3406 set_initializer_compound_value(tinitializer, entry->index,
3409 advance_current_object(&path);
3412 assert(ARR_LEN(path.path) >= 1);
3413 ir_initializer_t *result = path.path[0].initializer;
3414 DEL_ARR_F(path.path);
3419 static ir_initializer_t *create_ir_initializer_string(
3420 const initializer_string_t *initializer, type_t *type)
3422 type = skip_typeref(type);
3424 size_t string_len = initializer->string.size;
3425 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3426 size_t len = type->array.size;
3427 ir_initializer_t *irinitializer = create_initializer_compound(len);
3429 const char *string = initializer->string.begin;
3430 ir_mode *mode = get_ir_mode(type->array.element_type);
3432 for(size_t i = 0; i < len; ++i) {
3437 tarval *tv = new_tarval_from_long(c, mode);
3438 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3440 set_initializer_compound_value(irinitializer, i, char_initializer);
3443 return irinitializer;
3446 static ir_initializer_t *create_ir_initializer_wide_string(
3447 const initializer_wide_string_t *initializer, type_t *type)
3449 size_t string_len = initializer->string.size;
3450 assert(type->kind == TYPE_ARRAY && type->array.size_constant);
3451 size_t len = type->array.size;
3452 ir_initializer_t *irinitializer = create_initializer_compound(len);
3454 const wchar_rep_t *string = initializer->string.begin;
3455 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3457 for(size_t i = 0; i < len; ++i) {
3459 if (i < string_len) {
3462 tarval *tv = new_tarval_from_long(c, mode);
3463 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3465 set_initializer_compound_value(irinitializer, i, char_initializer);
3468 return irinitializer;
3471 static ir_initializer_t *create_ir_initializer(
3472 const initializer_t *initializer, type_t *type)
3474 switch(initializer->kind) {
3475 case INITIALIZER_STRING:
3476 return create_ir_initializer_string(&initializer->string, type);
3478 case INITIALIZER_WIDE_STRING:
3479 return create_ir_initializer_wide_string(&initializer->wide_string,
3482 case INITIALIZER_LIST:
3483 return create_ir_initializer_list(&initializer->list, type);
3485 case INITIALIZER_VALUE:
3486 return create_ir_initializer_value(&initializer->value);
3488 case INITIALIZER_DESIGNATOR:
3489 panic("unexpected designator initializer found");
3491 panic("unknown initializer");
3494 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3497 if (is_atomic_type(type)) {
3498 ir_mode *mode = get_type_mode(type);
3499 tarval *zero = get_mode_null(mode);
3500 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3502 /* TODO: bitfields */
3503 ir_node *mem = get_store();
3504 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3505 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3508 assert(is_compound_type(type));
3511 if (is_Array_type(type)) {
3512 assert(has_array_upper_bound(type, 0));
3513 n_members = get_array_upper_bound_int(type, 0);
3515 n_members = get_compound_n_members(type);
3518 for(int i = 0; i < n_members; ++i) {
3521 if (is_Array_type(type)) {
3522 ir_entity *entity = get_array_element_entity(type);
3523 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3524 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3525 ir_node *in[1] = { cnst };
3526 irtype = get_array_element_type(type);
3527 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3529 ir_entity *member = get_compound_member(type, i);
3531 irtype = get_entity_type(member);
3532 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3535 create_dynamic_null_initializer(irtype, dbgi, addr);
3540 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3541 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3543 switch(get_initializer_kind(initializer)) {
3544 case IR_INITIALIZER_NULL: {
3545 create_dynamic_null_initializer(type, dbgi, base_addr);
3548 case IR_INITIALIZER_CONST: {
3549 ir_node *node = get_initializer_const_value(initializer);
3550 ir_mode *mode = get_irn_mode(node);
3551 assert(get_type_mode(type) == mode);
3553 /* TODO: bitfields... */
3554 ir_node *mem = get_store();
3555 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3556 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3560 case IR_INITIALIZER_TARVAL: {
3561 tarval *tv = get_initializer_tarval_value(initializer);
3562 ir_mode *mode = get_tarval_mode(tv);
3563 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3564 assert(get_type_mode(type) == mode);
3566 /* TODO: bitfields... */
3567 ir_node *mem = get_store();
3568 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3569 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3573 case IR_INITIALIZER_COMPOUND: {
3574 assert(is_compound_type(type));
3576 if (is_Array_type(type)) {
3577 assert(has_array_upper_bound(type, 0));
3578 n_members = get_array_upper_bound_int(type, 0);
3580 n_members = get_compound_n_members(type);
3583 if (get_initializer_compound_n_entries(initializer)
3584 != (unsigned) n_members)
3585 panic("initializer doesn't match compound type");
3587 for(int i = 0; i < n_members; ++i) {
3590 if (is_Array_type(type)) {
3591 ir_entity *entity = get_array_element_entity(type);
3592 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3593 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3594 ir_node *in[1] = { cnst };
3595 irtype = get_array_element_type(type);
3596 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3598 ir_entity *member = get_compound_member(type, i);
3600 irtype = get_entity_type(member);
3601 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3604 ir_initializer_t *sub_init
3605 = get_initializer_compound_value(initializer, i);
3607 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3613 panic("invalid IR_INITIALIZER found");
3616 static void create_dynamic_initializer(ir_initializer_t *initializer,
3617 dbg_info *dbgi, ir_entity *entity)
3619 ir_node *frame = get_local_frame(entity);
3620 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3621 ir_type *type = get_entity_type(entity);
3623 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3626 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3627 ir_entity *entity, type_t *type)
3629 ir_node *memory = get_store();
3630 ir_node *nomem = new_NoMem();
3631 ir_node *frame = get_irg_frame(current_ir_graph);
3632 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3634 if (initializer->kind == INITIALIZER_VALUE) {
3635 initializer_value_t *initializer_value = &initializer->value;
3637 ir_node *value = expression_to_firm(initializer_value->value);
3638 type = skip_typeref(type);
3639 assign_value(dbgi, addr, type, value);
3643 if (!is_constant_initializer(initializer)) {
3644 ir_initializer_t *irinitializer
3645 = create_ir_initializer(initializer, type);
3647 create_dynamic_initializer(irinitializer, dbgi, entity);
3651 /* create the ir_initializer */
3652 ir_graph *const old_current_ir_graph = current_ir_graph;
3653 current_ir_graph = get_const_code_irg();
3655 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3657 assert(current_ir_graph == get_const_code_irg());
3658 current_ir_graph = old_current_ir_graph;
3660 /* create a "template" entity which is copied to the entity on the stack */
3661 ident *const id = id_unique("initializer.%u");
3662 ir_type *const irtype = get_ir_type(type);
3663 ir_type *const global_type = get_glob_type();
3664 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3665 set_entity_ld_ident(init_entity, id);
3667 set_entity_variability(init_entity, variability_initialized);
3668 set_entity_visibility(init_entity, visibility_local);
3669 set_entity_allocation(init_entity, allocation_static);
3671 set_entity_initializer(init_entity, irinitializer);
3673 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3674 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3676 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3677 set_store(copyb_mem);
3680 static void create_initializer_local_variable_entity(declaration_t *declaration)
3682 initializer_t *initializer = declaration->init.initializer;
3683 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3684 ir_entity *entity = declaration->v.entity;
3685 type_t *type = declaration->type;
3686 create_local_initializer(initializer, dbgi, entity, type);
3689 static void create_declaration_initializer(declaration_t *declaration)
3691 initializer_t *initializer = declaration->init.initializer;
3692 if (initializer == NULL)
3695 declaration_kind_t declaration_kind
3696 = (declaration_kind_t) declaration->declaration_kind;
3697 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3698 create_initializer_local_variable_entity(declaration);
3702 type_t *type = skip_typeref(declaration->type);
3704 if (initializer->kind == INITIALIZER_VALUE) {
3705 initializer_value_t *initializer_value = &initializer->value;
3707 = get_dbg_info(&declaration->source_position);
3709 ir_node *value = expression_to_firm(initializer_value->value);
3710 value = do_strict_conv(dbgi, value);
3712 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3713 set_value(declaration->v.value_number, value);
3715 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3717 ir_entity *entity = declaration->v.entity;
3719 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3720 set_entity_variability(entity, variability_constant);
3722 set_entity_variability(entity, variability_initialized);
3724 set_atomic_ent_value(entity, value);
3727 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3728 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3730 ir_entity *entity = declaration->v.entity;
3731 ir_initializer_t *irinitializer
3732 = create_ir_initializer(initializer, type);
3734 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3735 set_entity_variability(entity, variability_constant);
3737 set_entity_variability(entity, variability_initialized);
3739 set_entity_initializer(entity, irinitializer);
3743 static void create_variable_length_array(declaration_t *declaration)
3745 /* initializers are not allowed for VLAs */
3746 assert(declaration->init.initializer == NULL);
3748 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3749 declaration->v.vla_base = NULL;
3751 /* TODO: record VLA somewhere so we create the free node when we leave
3755 static void allocate_variable_length_array(declaration_t *declaration)
3757 /* initializers are not allowed for VLAs */
3758 assert(declaration->init.initializer == NULL);
3759 assert(get_cur_block() != NULL);
3761 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3762 type_t *type = declaration->type;
3763 ir_type *el_type = get_ir_type(type->array.element_type);
3765 /* make sure size_node is calculated */
3766 get_type_size(type);
3767 ir_node *elems = type->array.size_node;
3768 ir_node *mem = get_store();
3769 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3771 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3772 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3775 assert(declaration->declaration_kind
3776 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3777 declaration->v.vla_base = addr;
3781 * Creates a Firm local variable from a declaration.
3783 static void create_local_variable(declaration_t *declaration)
3785 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3787 bool needs_entity = declaration->address_taken;
3788 type_t *type = skip_typeref(declaration->type);
3790 /* is it a variable length array? */
3791 if (is_type_array(type) && !type->array.size_constant) {
3792 create_variable_length_array(declaration);
3794 } else if (is_type_array(type) || is_type_compound(type)) {
3795 needs_entity = true;
3796 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3797 needs_entity = true;
3801 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3802 create_declaration_entity(declaration,
3803 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3806 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3807 declaration->v.value_number = next_value_number_function;
3808 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3809 ++next_value_number_function;
3813 static void create_local_static_variable(declaration_t *declaration)
3815 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3817 type_t *const type = skip_typeref(declaration->type);
3818 ir_type *const global_type = get_glob_type();
3819 ir_type *const irtype = get_ir_type(type);
3820 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3822 size_t l = strlen(declaration->symbol->string);
3823 char buf[l + sizeof(".%u")];
3824 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3825 ident *const id = id_unique(buf);
3827 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3829 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3830 set_entity_volatility(entity, volatility_is_volatile);
3833 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3834 declaration->v.entity = entity;
3835 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3836 set_entity_variability(entity, variability_uninitialized);
3837 set_entity_visibility(entity, visibility_local);
3838 set_entity_allocation(entity, allocation_static);
3840 ir_graph *const old_current_ir_graph = current_ir_graph;
3841 current_ir_graph = get_const_code_irg();
3843 create_declaration_initializer(declaration);
3845 assert(current_ir_graph == get_const_code_irg());
3846 current_ir_graph = old_current_ir_graph;
3851 static void return_statement_to_firm(return_statement_t *statement)
3853 if (get_cur_block() == NULL)
3856 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3857 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3862 if (get_method_n_ress(func_irtype) > 0) {
3863 ir_type *res_type = get_method_res_type(func_irtype, 0);
3865 if (statement->value != NULL) {
3866 ir_node *node = expression_to_firm(statement->value);
3867 node = do_strict_conv(dbgi, node);
3871 if (is_compound_type(res_type)) {
3874 mode = get_type_mode(res_type);
3876 in[0] = new_Unknown(mode);
3880 /* build return_value for its side effects */
3881 if (statement->value != NULL) {
3882 expression_to_firm(statement->value);
3887 ir_node *store = get_store();
3888 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3890 ir_node *end_block = get_irg_end_block(current_ir_graph);
3891 add_immBlock_pred(end_block, ret);
3893 set_cur_block(NULL);
3896 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3898 if (get_cur_block() == NULL)
3901 return expression_to_firm(statement->expression);
3904 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3906 declaration_t *declaration = compound->scope.declarations;
3907 for( ; declaration != NULL; declaration = declaration->next) {
3908 create_local_declaration(declaration);
3911 ir_node *result = NULL;
3912 statement_t *statement = compound->statements;
3913 for( ; statement != NULL; statement = statement->base.next) {
3914 if (statement->base.next == NULL
3915 && statement->kind == STATEMENT_EXPRESSION) {
3916 result = expression_statement_to_firm(
3917 &statement->expression);
3920 statement_to_firm(statement);
3926 static void create_global_variable(declaration_t *declaration)
3930 switch ((storage_class_tag_t)declaration->storage_class) {
3931 case STORAGE_CLASS_STATIC:
3932 vis = visibility_local;
3935 case STORAGE_CLASS_EXTERN:
3936 vis = visibility_external_allocated;
3939 case STORAGE_CLASS_NONE:
3940 vis = visibility_external_visible;
3943 case STORAGE_CLASS_THREAD:
3944 vis = visibility_external_visible;
3947 case STORAGE_CLASS_THREAD_EXTERN:
3948 vis = visibility_external_allocated;
3951 case STORAGE_CLASS_THREAD_STATIC:
3952 vis = visibility_local;
3956 var_type = get_tls_type();
3960 var_type = get_glob_type();
3964 create_declaration_entity(declaration,
3965 DECLARATION_KIND_GLOBAL_VARIABLE,
3967 if (!is_type_function(skip_typeref(declaration->type))) {
3968 set_entity_visibility(declaration->v.entity, vis);
3973 case STORAGE_CLASS_TYPEDEF:
3974 case STORAGE_CLASS_AUTO:
3975 case STORAGE_CLASS_REGISTER:
3976 case STORAGE_CLASS_ENUM_ENTRY:
3979 panic("Invalid storage class for global variable");
3982 static void create_local_declaration(declaration_t *declaration)
3984 if (declaration->namespc != NAMESPACE_NORMAL)
3986 /* construct type */
3987 (void) get_ir_type(declaration->type);
3988 if (declaration->symbol == NULL) {
3992 type_t *type = skip_typeref(declaration->type);
3994 switch ((storage_class_tag_t) declaration->storage_class) {
3995 case STORAGE_CLASS_STATIC:
3996 create_local_static_variable(declaration);
3998 case STORAGE_CLASS_EXTERN:
3999 create_global_variable(declaration);
4000 create_declaration_initializer(declaration);
4002 case STORAGE_CLASS_NONE:
4003 case STORAGE_CLASS_AUTO:
4004 case STORAGE_CLASS_REGISTER:
4005 if (is_type_function(type)) {
4006 if (declaration->init.statement != NULL) {
4007 panic("nested functions not supported yet");
4009 get_function_entity(declaration);
4012 create_local_variable(declaration);
4015 case STORAGE_CLASS_ENUM_ENTRY:
4016 /* should already be handled */
4017 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4019 case STORAGE_CLASS_TYPEDEF:
4020 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4022 case STORAGE_CLASS_THREAD:
4023 case STORAGE_CLASS_THREAD_EXTERN:
4024 case STORAGE_CLASS_THREAD_STATIC:
4027 panic("invalid storage class found");
4030 static void initialize_local_declaration(declaration_t *declaration)
4032 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4035 switch ((declaration_kind_t) declaration->declaration_kind) {
4036 case DECLARATION_KIND_LOCAL_VARIABLE:
4037 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4038 create_declaration_initializer(declaration);
4041 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4042 allocate_variable_length_array(declaration);
4045 case DECLARATION_KIND_LABEL_BLOCK:
4046 case DECLARATION_KIND_COMPOUND_MEMBER:
4047 case DECLARATION_KIND_GLOBAL_VARIABLE:
4048 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4049 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4050 case DECLARATION_KIND_FUNCTION:
4051 case DECLARATION_KIND_TYPE:
4052 case DECLARATION_KIND_ENUM_ENTRY:
4055 case DECLARATION_KIND_UNKNOWN:
4056 panic("can't initialize unknwon declaration");
4058 panic("invalid declaration kind");
4061 static void declaration_statement_to_firm(declaration_statement_t *statement)
4063 declaration_t *declaration = statement->declarations_begin;
4064 declaration_t *end = statement->declarations_end->next;
4065 for( ; declaration != end; declaration = declaration->next) {
4066 if (declaration->namespc != NAMESPACE_NORMAL)
4068 initialize_local_declaration(declaration);
4072 static void if_statement_to_firm(if_statement_t *statement)
4074 ir_node *cur_block = get_cur_block();
4076 ir_node *fallthrough_block = NULL;
4078 /* the true (blocks) */
4079 ir_node *true_block = NULL;
4080 if (statement->true_statement != NULL) {
4081 true_block = new_immBlock();
4082 statement_to_firm(statement->true_statement);
4083 if (get_cur_block() != NULL) {
4084 ir_node *jmp = new_Jmp();
4085 if (fallthrough_block == NULL)
4086 fallthrough_block = new_immBlock();
4087 add_immBlock_pred(fallthrough_block, jmp);
4091 /* the false (blocks) */
4092 ir_node *false_block = NULL;
4093 if (statement->false_statement != NULL) {
4094 false_block = new_immBlock();
4096 statement_to_firm(statement->false_statement);
4097 if (get_cur_block() != NULL) {
4098 ir_node *jmp = new_Jmp();
4099 if (fallthrough_block == NULL)
4100 fallthrough_block = new_immBlock();
4101 add_immBlock_pred(fallthrough_block, jmp);
4105 /* create the condition */
4106 if (cur_block != NULL) {
4107 if (true_block == NULL || false_block == NULL) {
4108 if (fallthrough_block == NULL)
4109 fallthrough_block = new_immBlock();
4110 if (true_block == NULL)
4111 true_block = fallthrough_block;
4112 if (false_block == NULL)
4113 false_block = fallthrough_block;
4116 set_cur_block(cur_block);
4117 create_condition_evaluation(statement->condition, true_block,
4121 mature_immBlock(true_block);
4122 if (false_block != fallthrough_block) {
4123 mature_immBlock(false_block);
4125 if (fallthrough_block != NULL) {
4126 mature_immBlock(fallthrough_block);
4129 set_cur_block(fallthrough_block);
4132 static void while_statement_to_firm(while_statement_t *statement)
4134 ir_node *jmp = NULL;
4135 if (get_cur_block() != NULL) {
4139 /* create the header block */
4140 ir_node *header_block = new_immBlock();
4142 add_immBlock_pred(header_block, jmp);
4146 ir_node *old_continue_label = continue_label;
4147 ir_node *old_break_label = break_label;
4148 continue_label = header_block;
4151 ir_node *body_block = new_immBlock();
4152 statement_to_firm(statement->body);
4153 ir_node *false_block = break_label;
4155 assert(continue_label == header_block);
4156 continue_label = old_continue_label;
4157 break_label = old_break_label;
4159 if (get_cur_block() != NULL) {
4161 add_immBlock_pred(header_block, jmp);
4164 /* shortcut for while(true) */
4165 if (is_constant_expression(statement->condition)
4166 && fold_constant(statement->condition) != 0) {
4167 set_cur_block(header_block);
4168 ir_node *header_jmp = new_Jmp();
4169 add_immBlock_pred(body_block, header_jmp);
4171 keep_alive(body_block);
4173 if (false_block == NULL) {
4174 false_block = new_immBlock();
4177 /* create the condition */
4178 set_cur_block(header_block);
4180 create_condition_evaluation(statement->condition, body_block,
4184 mature_immBlock(body_block);
4185 mature_immBlock(header_block);
4186 if (false_block != NULL) {
4187 mature_immBlock(false_block);
4190 set_cur_block(false_block);
4193 static void do_while_statement_to_firm(do_while_statement_t *statement)
4195 ir_node *jmp = NULL;
4196 if (get_cur_block() != NULL) {
4200 /* create the header block */
4201 ir_node *header_block = new_immBlock();
4204 ir_node *body_block = new_immBlock();
4206 add_immBlock_pred(body_block, jmp);
4209 ir_node *old_continue_label = continue_label;
4210 ir_node *old_break_label = break_label;
4211 continue_label = header_block;
4214 statement_to_firm(statement->body);
4215 ir_node *false_block = break_label;
4217 assert(continue_label == header_block);
4218 continue_label = old_continue_label;
4219 break_label = old_break_label;
4221 if (get_cur_block() != NULL) {
4222 ir_node *body_jmp = new_Jmp();
4223 add_immBlock_pred(header_block, body_jmp);
4224 mature_immBlock(header_block);
4227 if (false_block == NULL) {
4228 false_block = new_immBlock();
4231 /* create the condition */
4232 set_cur_block(header_block);
4234 create_condition_evaluation(statement->condition, body_block, false_block);
4235 mature_immBlock(body_block);
4236 mature_immBlock(header_block);
4237 if (false_block != NULL) {
4238 mature_immBlock(false_block);
4241 set_cur_block(false_block);
4244 static void for_statement_to_firm(for_statement_t *statement)
4246 ir_node *jmp = NULL;
4248 /* create declarations */
4249 declaration_t *declaration = statement->scope.declarations;
4250 for( ; declaration != NULL; declaration = declaration->next) {
4251 create_local_declaration(declaration);
4253 declaration = statement->scope.declarations;
4254 for( ; declaration != NULL; declaration = declaration->next) {
4255 initialize_local_declaration(declaration);
4258 if (get_cur_block() != NULL) {
4259 if (statement->initialisation != NULL) {
4260 expression_to_firm(statement->initialisation);
4267 /* create the step block */
4268 ir_node *const step_block = new_immBlock();
4269 if (statement->step != NULL) {
4270 expression_to_firm(statement->step);
4272 ir_node *const step_jmp = new_Jmp();
4274 /* create the header block */
4275 ir_node *const header_block = new_immBlock();
4277 add_immBlock_pred(header_block, jmp);
4279 add_immBlock_pred(header_block, step_jmp);
4281 /* the false block */
4282 ir_node *const false_block = new_immBlock();
4285 ir_node * body_block;
4286 if (statement->body != NULL) {
4287 ir_node *const old_continue_label = continue_label;
4288 ir_node *const old_break_label = break_label;
4289 continue_label = step_block;
4290 break_label = false_block;
4292 body_block = new_immBlock();
4293 statement_to_firm(statement->body);
4295 assert(continue_label == step_block);
4296 assert(break_label == false_block);
4297 continue_label = old_continue_label;
4298 break_label = old_break_label;
4300 if (get_cur_block() != NULL) {
4302 add_immBlock_pred(step_block, jmp);
4305 body_block = step_block;
4308 /* create the condition */
4309 set_cur_block(header_block);
4310 if (statement->condition != NULL) {
4311 create_condition_evaluation(statement->condition, body_block,
4314 keep_alive(header_block);
4316 add_immBlock_pred(body_block, jmp);
4319 mature_immBlock(body_block);
4320 mature_immBlock(false_block);
4321 mature_immBlock(step_block);
4322 mature_immBlock(header_block);
4323 mature_immBlock(false_block);
4325 set_cur_block(false_block);
4328 static void create_jump_statement(const statement_t *statement,
4329 ir_node *target_block)
4331 if (get_cur_block() == NULL)
4334 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4335 ir_node *jump = new_d_Jmp(dbgi);
4336 add_immBlock_pred(target_block, jump);
4338 set_cur_block(NULL);
4341 static ir_node *get_break_label(void)
4343 if (break_label == NULL) {
4344 ir_node *cur_block = get_cur_block();
4345 break_label = new_immBlock();
4346 set_cur_block(cur_block);
4351 static void switch_statement_to_firm(const switch_statement_t *statement)
4353 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4355 ir_node *expression = expression_to_firm(statement->expression);
4356 ir_node *cond = new_d_Cond(dbgi, expression);
4358 set_cur_block(NULL);
4360 ir_node *const old_switch_cond = current_switch_cond;
4361 ir_node *const old_break_label = break_label;
4362 const bool old_saw_default_label = saw_default_label;
4363 saw_default_label = false;
4364 current_switch_cond = cond;
4367 if (statement->body != NULL) {
4368 statement_to_firm(statement->body);
4371 if (get_cur_block() != NULL) {
4372 ir_node *jmp = new_Jmp();
4373 add_immBlock_pred(get_break_label(), jmp);
4376 if (!saw_default_label) {
4377 set_cur_block(get_nodes_block(cond));
4378 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4379 MAGIC_DEFAULT_PN_NUMBER);
4380 add_immBlock_pred(get_break_label(), proj);
4383 if (break_label != NULL) {
4384 mature_immBlock(break_label);
4386 set_cur_block(break_label);
4388 assert(current_switch_cond == cond);
4389 current_switch_cond = old_switch_cond;
4390 break_label = old_break_label;
4391 saw_default_label = old_saw_default_label;
4394 static void case_label_to_firm(const case_label_statement_t *statement)
4396 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4398 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4400 /* let's create a node and hope firm constant folding creates a Const
4403 ir_node *old_block = get_nodes_block(current_switch_cond);
4404 ir_node *block = new_immBlock();
4406 set_cur_block(old_block);
4407 if (statement->expression != NULL) {
4408 long start_pn = fold_constant(statement->expression);
4409 long end_pn = start_pn;
4410 if (statement->end_range != NULL) {
4411 end_pn = fold_constant(statement->end_range);
4413 assert(start_pn <= end_pn);
4414 /* create jumps for all cases in the given range */
4415 for (long pn = start_pn; pn <= end_pn; ++pn) {
4416 if (pn == MAGIC_DEFAULT_PN_NUMBER) {
4417 /* oops someone detected our cheating... */
4418 panic("magic default pn used");
4420 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4421 add_immBlock_pred(block, proj);
4424 saw_default_label = true;
4425 proj = new_d_defaultProj(dbgi, current_switch_cond,
4426 MAGIC_DEFAULT_PN_NUMBER);
4428 add_immBlock_pred(block, proj);
4431 if (fallthrough != NULL) {
4432 add_immBlock_pred(block, fallthrough);
4434 mature_immBlock(block);
4435 set_cur_block(block);
4437 if (statement->statement != NULL) {
4438 statement_to_firm(statement->statement);
4442 static ir_node *get_label_block(declaration_t *label)
4444 assert(label->namespc == NAMESPACE_LABEL);
4446 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4447 return label->v.block;
4449 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4451 ir_node *old_cur_block = get_cur_block();
4452 ir_node *block = new_immBlock();
4453 set_cur_block(old_cur_block);
4455 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4456 label->v.block = block;
4458 ARR_APP1(ir_node *, imature_blocks, block);
4463 static void label_to_firm(const label_statement_t *statement)
4465 ir_node *block = get_label_block(statement->label);
4467 if (get_cur_block() != NULL) {
4468 ir_node *jmp = new_Jmp();
4469 add_immBlock_pred(block, jmp);
4472 set_cur_block(block);
4475 if (statement->statement != NULL) {
4476 statement_to_firm(statement->statement);
4480 static void goto_to_firm(const goto_statement_t *statement)
4482 if (get_cur_block() == NULL)
4485 ir_node *block = get_label_block(statement->label);
4486 ir_node *jmp = new_Jmp();
4487 add_immBlock_pred(block, jmp);
4489 set_cur_block(NULL);
4492 static void asm_statement_to_firm(const asm_statement_t *statement)
4494 bool needs_memory = false;
4496 if (statement->is_volatile) {
4497 needs_memory = true;
4500 size_t n_clobbers = 0;
4501 asm_clobber_t *clobber = statement->clobbers;
4502 for( ; clobber != NULL; clobber = clobber->next) {
4503 const char *clobber_str = clobber->clobber.begin;
4505 if (!be_is_valid_clobber(clobber_str)) {
4506 errorf(&statement->base.source_position,
4507 "invalid clobber '%s' specified", clobber->clobber);
4511 if (strcmp(clobber_str, "memory") == 0) {
4512 needs_memory = true;
4516 ident *id = new_id_from_str(clobber_str);
4517 obstack_ptr_grow(&asm_obst, id);
4520 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4521 ident **clobbers = NULL;
4522 if (n_clobbers > 0) {
4523 clobbers = obstack_finish(&asm_obst);
4526 size_t n_inputs = 0;
4527 asm_argument_t *argument = statement->inputs;
4528 for ( ; argument != NULL; argument = argument->next)
4530 size_t n_outputs = 0;
4531 argument = statement->outputs;
4532 for ( ; argument != NULL; argument = argument->next)
4535 unsigned next_pos = 0;
4537 ir_node *ins[n_inputs + n_outputs + 1];
4540 ir_asm_constraint *tmp_in_constraints
4541 = xmalloc(n_outputs * sizeof(tmp_in_constraints[0]));
4543 const expression_t *out_exprs[n_outputs];
4544 ir_node *out_addrs[n_outputs];
4545 size_t out_size = 0;
4547 argument = statement->outputs;
4548 for ( ; argument != NULL; argument = argument->next) {
4549 const char *constraints = argument->constraints.begin;
4550 asm_constraint_flags_t asm_flags
4551 = be_parse_asm_constraints(constraints);
4553 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4554 errorf(&statement->base.source_position,
4555 "some constraints in '%s' are not supported", constraints);
4558 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4559 errorf(&statement->base.source_position,
4560 "some constraints in '%s' are invalid", constraints);
4563 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4564 errorf(&statement->base.source_position,
4565 "no write flag specified for output constraints '%s'",
4570 unsigned pos = next_pos++;
4571 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4572 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4573 expression_t *expr = argument->expression;
4574 ir_node *addr = expression_to_addr(expr);
4575 /* in+output, construct an artifical same_as constraint on the
4577 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4579 ir_node *value = get_value_from_lvalue(expr, addr);
4581 snprintf(buf, sizeof(buf), "%d", pos);
4583 ir_asm_constraint constraint;
4584 constraint.pos = pos;
4585 constraint.constraint = new_id_from_str(buf);
4586 constraint.mode = get_ir_mode(expr->base.type);
4587 tmp_in_constraints[in_size] = constraint;
4588 ins[in_size] = value;
4593 out_exprs[out_size] = expr;
4594 out_addrs[out_size] = addr;
4596 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4597 /* pure memory ops need no input (but we have to make sure we
4598 * attach to the memory) */
4599 assert(! (asm_flags &
4600 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4601 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4602 needs_memory = true;
4604 /* we need to attach the address to the inputs */
4605 expression_t *expr = argument->expression;
4607 ir_asm_constraint constraint;
4608 constraint.pos = pos;
4609 constraint.constraint = new_id_from_str(constraints);
4610 constraint.mode = NULL;
4611 tmp_in_constraints[in_size] = constraint;
4613 ins[in_size] = expression_to_addr(expr);
4617 errorf(&statement->base.source_position,
4618 "only modifiers but no place set in constraints '%s'",
4623 ir_asm_constraint constraint;
4624 constraint.pos = pos;
4625 constraint.constraint = new_id_from_str(constraints);
4626 constraint.mode = get_ir_mode(argument->expression->base.type);
4628 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4630 assert(obstack_object_size(&asm_obst)
4631 == out_size * sizeof(ir_asm_constraint));
4632 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4635 obstack_grow(&asm_obst, tmp_in_constraints,
4636 in_size * sizeof(tmp_in_constraints[0]));
4637 free(tmp_in_constraints);
4638 /* find and count input and output arguments */
4639 argument = statement->inputs;
4640 for( ; argument != NULL; argument = argument->next) {
4641 const char *constraints = argument->constraints.begin;
4642 asm_constraint_flags_t asm_flags
4643 = be_parse_asm_constraints(constraints);
4645 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4646 errorf(&statement->base.source_position,
4647 "some constraints in '%s' are not supported", constraints);
4650 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4651 errorf(&statement->base.source_position,
4652 "some constraints in '%s' are invalid", constraints);
4655 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4656 errorf(&statement->base.source_position,
4657 "write flag specified for input constraints '%s'",
4663 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4664 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4665 /* we can treat this as "normal" input */
4666 input = expression_to_firm(argument->expression);
4667 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4668 /* pure memory ops need no input (but we have to make sure we
4669 * attach to the memory) */
4670 assert(! (asm_flags &
4671 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4672 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4673 needs_memory = true;
4674 input = expression_to_addr(argument->expression);
4676 errorf(&statement->base.source_position,
4677 "only modifiers but no place set in constraints '%s'",
4682 ir_asm_constraint constraint;
4683 constraint.pos = next_pos++;
4684 constraint.constraint = new_id_from_str(constraints);
4685 constraint.mode = get_irn_mode(input);
4687 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4688 ins[in_size++] = input;
4692 ir_asm_constraint constraint;
4693 constraint.pos = next_pos++;
4694 constraint.constraint = new_id_from_str("");
4695 constraint.mode = mode_M;
4697 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4698 ins[in_size++] = get_store();
4701 assert(obstack_object_size(&asm_obst)
4702 == in_size * sizeof(ir_asm_constraint));
4703 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4705 /* create asm node */
4706 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4708 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4710 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4711 out_size, output_constraints,
4712 n_clobbers, clobbers, asm_text);
4714 if (statement->is_volatile) {
4715 set_irn_pinned(node, op_pin_state_pinned);
4717 set_irn_pinned(node, op_pin_state_floats);
4720 /* create output projs & connect them */
4722 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4727 for (i = 0; i < out_size; ++i) {
4728 const expression_t *out_expr = out_exprs[i];
4730 ir_mode *mode = get_ir_mode(out_expr->base.type);
4731 ir_node *proj = new_Proj(node, mode, pn);
4732 ir_node *addr = out_addrs[i];
4734 set_value_for_expression_addr(out_expr, proj, addr);
4738 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4739 statement_to_firm(statement->try_statement);
4740 warningf(&statement->base.source_position, "structured exception handling ignored");
4743 static void leave_statement_to_firm(leave_statement_t *statement) {
4744 errorf(&statement->base.source_position, "__leave not supported yet");
4748 * Transform a statement.
4750 static void statement_to_firm(statement_t *statement)
4753 assert(!statement->base.transformed);
4754 statement->base.transformed = true;
4757 switch(statement->kind) {
4758 case STATEMENT_INVALID:
4759 panic("invalid statement found");
4761 case STATEMENT_EMPTY:
4764 case STATEMENT_COMPOUND:
4765 compound_statement_to_firm(&statement->compound);
4767 case STATEMENT_RETURN:
4768 return_statement_to_firm(&statement->returns);
4770 case STATEMENT_EXPRESSION:
4771 expression_statement_to_firm(&statement->expression);
4774 if_statement_to_firm(&statement->ifs);
4776 case STATEMENT_WHILE:
4777 while_statement_to_firm(&statement->whiles);
4779 case STATEMENT_DO_WHILE:
4780 do_while_statement_to_firm(&statement->do_while);
4782 case STATEMENT_DECLARATION:
4783 declaration_statement_to_firm(&statement->declaration);
4785 case STATEMENT_BREAK:
4786 create_jump_statement(statement, get_break_label());
4788 case STATEMENT_CONTINUE:
4789 create_jump_statement(statement, continue_label);
4791 case STATEMENT_SWITCH:
4792 switch_statement_to_firm(&statement->switchs);
4794 case STATEMENT_CASE_LABEL:
4795 case_label_to_firm(&statement->case_label);
4798 for_statement_to_firm(&statement->fors);
4800 case STATEMENT_LABEL:
4801 label_to_firm(&statement->label);
4803 case STATEMENT_GOTO:
4804 goto_to_firm(&statement->gotos);
4807 asm_statement_to_firm(&statement->asms);
4809 case STATEMENT_MS_TRY:
4810 ms_try_statement_to_firm(&statement->ms_try);
4812 case STATEMENT_LEAVE:
4813 leave_statement_to_firm(&statement->leave);
4816 panic("Statement not implemented\n");
4819 static int count_decls_in_expression(const expression_t *expression);
4821 static int count_local_declarations(const declaration_t * decl,
4822 const declaration_t *const end)
4825 for (; decl != end; decl = decl->next) {
4826 if (decl->namespc != NAMESPACE_NORMAL)
4828 type_t *type = skip_typeref(decl->type);
4830 if (!decl->address_taken && is_type_scalar(type))
4832 const initializer_t *initializer = decl->init.initializer;
4833 /* FIXME: should walk initializer hierarchies... */
4834 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4835 count += count_decls_in_expression(initializer->value.value);
4841 static int count_decls_in_expression(const expression_t *expression) {
4844 if (expression == NULL)
4847 switch((expression_kind_t) expression->base.kind) {
4848 case EXPR_STATEMENT:
4849 return count_decls_in_stmts(expression->statement.statement);
4851 int count_left = count_decls_in_expression(expression->binary.left);
4852 int count_right = count_decls_in_expression(expression->binary.right);
4853 return count_left + count_right;
4856 return count_decls_in_expression(expression->unary.value);
4858 call_argument_t *argument = expression->call.arguments;
4859 for( ; argument != NULL; argument = argument->next) {
4860 count += count_decls_in_expression(argument->expression);
4867 panic("unexpected expression kind");
4869 case EXPR_COMPOUND_LITERAL:
4873 case EXPR_CONDITIONAL:
4874 count += count_decls_in_expression(expression->conditional.condition);
4875 count += count_decls_in_expression(expression->conditional.true_expression);
4876 count += count_decls_in_expression(expression->conditional.false_expression);
4879 case EXPR_BUILTIN_PREFETCH:
4880 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4881 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4882 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4885 case EXPR_BUILTIN_CONSTANT_P:
4886 count += count_decls_in_expression(expression->builtin_constant.value);
4890 count += count_decls_in_expression(expression->select.compound);
4893 case EXPR_ARRAY_ACCESS:
4894 count += count_decls_in_expression(expression->array_access.array_ref);
4895 count += count_decls_in_expression(expression->array_access.index);
4898 case EXPR_CLASSIFY_TYPE:
4899 count += count_decls_in_expression(expression->classify_type.type_expression);
4903 case EXPR_ALIGNOF: {
4904 expression_t *tp_expression = expression->typeprop.tp_expression;
4905 if (tp_expression != NULL) {
4906 count += count_decls_in_expression(tp_expression);
4912 case EXPR_REFERENCE:
4914 case EXPR_CHARACTER_CONSTANT:
4915 case EXPR_WIDE_CHARACTER_CONSTANT:
4916 case EXPR_STRING_LITERAL:
4917 case EXPR_WIDE_STRING_LITERAL:
4919 case EXPR_BUILTIN_SYMBOL:
4925 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4926 * (or implement all the missing expressions here/implement a walker)
4932 static int count_decls_in_stmts(const statement_t *stmt)
4935 for (; stmt != NULL; stmt = stmt->base.next) {
4936 switch (stmt->kind) {
4937 case STATEMENT_EMPTY:
4940 case STATEMENT_DECLARATION: {
4941 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4942 count += count_local_declarations(decl_stmt->declarations_begin,
4943 decl_stmt->declarations_end->next);
4947 case STATEMENT_COMPOUND: {
4948 const compound_statement_t *const comp =
4950 count += count_decls_in_stmts(comp->statements);
4954 case STATEMENT_IF: {
4955 const if_statement_t *const if_stmt = &stmt->ifs;
4956 count += count_decls_in_expression(if_stmt->condition);
4957 count += count_decls_in_stmts(if_stmt->true_statement);
4958 count += count_decls_in_stmts(if_stmt->false_statement);
4962 case STATEMENT_SWITCH: {
4963 const switch_statement_t *const switch_stmt = &stmt->switchs;
4964 count += count_decls_in_expression(switch_stmt->expression);
4965 count += count_decls_in_stmts(switch_stmt->body);
4969 case STATEMENT_LABEL: {
4970 const label_statement_t *const label_stmt = &stmt->label;
4971 if (label_stmt->statement != NULL) {
4972 count += count_decls_in_stmts(label_stmt->statement);
4977 case STATEMENT_WHILE: {
4978 const while_statement_t *const while_stmt = &stmt->whiles;
4979 count += count_decls_in_expression(while_stmt->condition);
4980 count += count_decls_in_stmts(while_stmt->body);
4984 case STATEMENT_DO_WHILE: {
4985 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
4986 count += count_decls_in_expression(do_while_stmt->condition);
4987 count += count_decls_in_stmts(do_while_stmt->body);
4991 case STATEMENT_FOR: {
4992 const for_statement_t *const for_stmt = &stmt->fors;
4993 count += count_local_declarations(for_stmt->scope.declarations, NULL);
4994 count += count_decls_in_expression(for_stmt->initialisation);
4995 count += count_decls_in_expression(for_stmt->condition);
4996 count += count_decls_in_expression(for_stmt->step);
4997 count += count_decls_in_stmts(for_stmt->body);
5001 case STATEMENT_CASE_LABEL: {
5002 const case_label_statement_t *label = &stmt->case_label;
5003 count += count_decls_in_expression(label->expression);
5004 if (label->statement != NULL) {
5005 count += count_decls_in_stmts(label->statement);
5011 case STATEMENT_BREAK:
5012 case STATEMENT_CONTINUE:
5015 case STATEMENT_EXPRESSION: {
5016 const expression_statement_t *expr_stmt = &stmt->expression;
5017 count += count_decls_in_expression(expr_stmt->expression);
5021 case STATEMENT_GOTO:
5022 case STATEMENT_LEAVE:
5023 case STATEMENT_INVALID:
5026 case STATEMENT_RETURN: {
5027 const return_statement_t *ret_stmt = &stmt->returns;
5028 count += count_decls_in_expression(ret_stmt->value);
5032 case STATEMENT_MS_TRY: {
5033 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5034 count += count_decls_in_stmts(try_stmt->try_statement);
5035 if (try_stmt->except_expression != NULL)
5036 count += count_decls_in_expression(try_stmt->except_expression);
5037 count += count_decls_in_stmts(try_stmt->final_statement);
5045 static int get_function_n_local_vars(declaration_t *declaration)
5049 /* count parameters */
5050 count += count_local_declarations(declaration->scope.declarations, NULL);
5052 /* count local variables declared in body */
5053 count += count_decls_in_stmts(declaration->init.statement);
5058 static void initialize_function_parameters(declaration_t *declaration)
5060 ir_graph *irg = current_ir_graph;
5061 ir_node *args = get_irg_args(irg);
5062 ir_node *start_block = get_irg_start_block(irg);
5063 ir_type *function_irtype = get_ir_type(declaration->type);
5066 declaration_t *parameter = declaration->scope.declarations;
5067 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5068 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5069 type_t *type = skip_typeref(parameter->type);
5071 bool needs_entity = parameter->address_taken;
5072 assert(!is_type_array(type));
5073 if (is_type_compound(type)) {
5074 needs_entity = true;
5078 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5079 ident *id = new_id_from_str(parameter->symbol->string);
5080 set_entity_ident(entity, id);
5082 parameter->declaration_kind
5083 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5084 parameter->v.entity = entity;
5088 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5089 ir_mode *param_mode = get_type_mode(param_irtype);
5092 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5094 ir_mode *mode = get_ir_mode(type);
5095 value = create_conv(NULL, value, mode);
5096 value = do_strict_conv(NULL, value);
5098 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5099 parameter->v.value_number = next_value_number_function;
5100 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5101 ++next_value_number_function;
5103 set_value(parameter->v.value_number, value);
5108 * Handle additional decl modifiers for IR-graphs
5110 * @param irg the IR-graph
5111 * @param dec_modifiers additional modifiers
5113 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5115 if (decl_modifiers & DM_NORETURN) {
5116 /* TRUE if the declaration includes the Microsoft
5117 __declspec(noreturn) specifier. */
5118 set_irg_additional_property(irg, mtp_property_noreturn);
5120 if (decl_modifiers & DM_NOTHROW) {
5121 /* TRUE if the declaration includes the Microsoft
5122 __declspec(nothrow) specifier. */
5123 set_irg_additional_property(irg, mtp_property_nothrow);
5125 if (decl_modifiers & DM_NAKED) {
5126 /* TRUE if the declaration includes the Microsoft
5127 __declspec(naked) specifier. */
5128 set_irg_additional_property(irg, mtp_property_naked);
5130 if (decl_modifiers & DM_FORCEINLINE) {
5131 /* TRUE if the declaration includes the
5132 Microsoft __forceinline specifier. */
5133 set_irg_inline_property(irg, irg_inline_forced);
5135 if (decl_modifiers & DM_NOINLINE) {
5136 /* TRUE if the declaration includes the Microsoft
5137 __declspec(noinline) specifier. */
5138 set_irg_inline_property(irg, irg_inline_forbidden);
5142 static void add_function_pointer(ir_type *segment, ir_entity *method,
5143 const char *unique_template)
5145 ir_type *method_type = get_entity_type(method);
5146 ident *id = id_unique(unique_template);
5147 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5149 ident *ide = id_unique(unique_template);
5150 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5151 ir_graph *irg = get_const_code_irg();
5152 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5155 set_entity_compiler_generated(ptr, 1);
5156 set_entity_variability(ptr, variability_constant);
5157 set_atomic_ent_value(ptr, val);
5161 * Create code for a function.
5163 static void create_function(declaration_t *declaration)
5165 ir_entity *function_entity = get_function_entity(declaration);
5167 if (declaration->init.statement == NULL)
5170 if (declaration->modifiers & DM_CONSTRUCTOR) {
5171 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5172 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5174 if (declaration->modifiers & DM_DESTRUCTOR) {
5175 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5176 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5179 current_function_decl = declaration;
5180 current_function_name = NULL;
5181 current_funcsig = NULL;
5183 assert(imature_blocks == NULL);
5184 imature_blocks = NEW_ARR_F(ir_node*, 0);
5186 int n_local_vars = get_function_n_local_vars(declaration);
5187 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5189 set_irg_fp_model(irg, firm_opt.fp_model);
5190 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5191 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5193 ir_node *first_block = get_cur_block();
5195 /* set inline flags */
5196 if (declaration->is_inline)
5197 set_irg_inline_property(irg, irg_inline_recomended);
5198 handle_decl_modifier_irg(irg, declaration->modifiers);
5200 next_value_number_function = 0;
5201 initialize_function_parameters(declaration);
5203 statement_to_firm(declaration->init.statement);
5205 ir_node *end_block = get_irg_end_block(irg);
5207 /* do we have a return statement yet? */
5208 if (get_cur_block() != NULL) {
5209 type_t *type = skip_typeref(declaration->type);
5210 assert(is_type_function(type));
5211 const function_type_t *func_type = &type->function;
5212 const type_t *return_type
5213 = skip_typeref(func_type->return_type);
5216 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5217 ret = new_Return(get_store(), 0, NULL);
5220 if (is_type_scalar(return_type)) {
5221 mode = get_ir_mode(func_type->return_type);
5227 /* ยง5.1.2.2.3 main implicitly returns 0 */
5228 if (strcmp(declaration->symbol->string, "main") == 0) {
5229 in[0] = new_Const(mode, get_mode_null(mode));
5231 in[0] = new_Unknown(mode);
5232 if (warning.return_type) {
5233 warningf(&declaration->source_position,
5234 "missing return statement at end of non-void function '%Y'",
5235 declaration->symbol);
5238 ret = new_Return(get_store(), 1, in);
5240 add_immBlock_pred(end_block, ret);
5243 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
5244 mature_immBlock(imature_blocks[i]);
5246 DEL_ARR_F(imature_blocks);
5247 imature_blocks = NULL;
5249 mature_immBlock(first_block);
5250 mature_immBlock(end_block);
5252 irg_finalize_cons(irg);
5254 /* finalize the frame type */
5255 ir_type *frame_type = get_irg_frame_type(irg);
5256 int n = get_compound_n_members(frame_type);
5259 for(int i = 0; i < n; ++i) {
5260 ir_entity *entity = get_compound_member(frame_type, i);
5261 ir_type *entity_type = get_entity_type(entity);
5263 int align = get_type_alignment_bytes(entity_type);
5264 if (align > align_all)
5268 misalign = offset % align;
5270 offset += align - misalign;
5274 set_entity_offset(entity, offset);
5275 offset += get_type_size_bytes(entity_type);
5277 set_type_size_bytes(frame_type, offset);
5278 set_type_alignment_bytes(frame_type, align_all);
5283 static void scope_to_firm(scope_t *scope)
5285 /* first pass: create declarations */
5286 declaration_t *declaration = scope->declarations;
5287 for( ; declaration != NULL; declaration = declaration->next) {
5288 if (declaration->namespc != NAMESPACE_NORMAL)
5290 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5291 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5293 if (declaration->symbol == NULL)
5296 type_t *type = skip_typeref(declaration->type);
5297 if (is_type_function(type)) {
5298 get_function_entity(declaration);
5300 create_global_variable(declaration);
5304 /* second pass: create code/initializers */
5305 declaration = scope->declarations;
5306 for( ; declaration != NULL; declaration = declaration->next) {
5307 if (declaration->namespc != NAMESPACE_NORMAL)
5309 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5310 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5312 if (declaration->symbol == NULL)
5315 type_t *type = declaration->type;
5316 if (type->kind == TYPE_FUNCTION) {
5317 create_function(declaration);
5319 assert(declaration->declaration_kind
5320 == DECLARATION_KIND_GLOBAL_VARIABLE);
5321 current_ir_graph = get_const_code_irg();
5322 create_declaration_initializer(declaration);
5327 void init_ast2firm(void)
5329 obstack_init(&asm_obst);
5330 init_atomic_modes();
5332 id_underscore = new_id_from_chars("_", 1);
5333 id_imp = new_id_from_chars("__imp_", 6);
5335 /* OS option must be set to the backend */
5336 const char *s = "ia32-gasmode=linux";
5337 switch (firm_opt.os_support) {
5338 case OS_SUPPORT_MINGW:
5339 create_ld_ident = create_ld_ident_win32;
5340 s = "ia32-gasmode=mingw";
5342 case OS_SUPPORT_LINUX:
5343 create_ld_ident = create_ld_ident_linux_elf;
5344 s = "ia32-gasmode=elf";
5346 case OS_SUPPORT_MACHO:
5347 create_ld_ident = create_ld_ident_macho;
5348 s = "ia32-gasmode=macho";
5351 int res = firm_be_option(s);
5355 /* create idents for all known runtime functions */
5356 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5357 rts_idents[i] = new_id_from_str(rts_data[i].name);
5360 entitymap_init(&entitymap);
5363 static void init_ir_types(void)
5365 static int ir_types_initialized = 0;
5366 if (ir_types_initialized)
5368 ir_types_initialized = 1;
5370 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
5371 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5372 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5374 ir_type_int = get_ir_type(type_int);
5375 ir_type_const_char = get_ir_type(type_const_char);
5376 ir_type_wchar_t = get_ir_type(type_wchar_t);
5377 ir_type_void = get_ir_type(type_void);
5380 void exit_ast2firm(void)
5382 entitymap_destroy(&entitymap);
5383 obstack_free(&asm_obst, NULL);
5386 void translation_unit_to_firm(translation_unit_t *unit)
5388 /* just to be sure */
5389 continue_label = NULL;
5391 current_switch_cond = NULL;
5395 scope_to_firm(&unit->scope);