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 if (declaration != NULL) {
100 warningf(&declaration->source_position,
101 "variable '%#T' might be used uninitialized",
102 declaration->type, declaration->symbol);
104 return new_r_Unknown(irg, mode);
107 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
109 const source_position_t *pos = (const source_position_t*) dbg;
112 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
116 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
118 const source_position_t *pos = (const source_position_t*) dbg;
123 return pos->input_name;
126 static dbg_info *get_dbg_info(const source_position_t *pos)
128 return (dbg_info*) pos;
131 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
133 static ir_mode *mode_int, *mode_uint;
135 static ir_node *_expression_to_firm(const expression_t *expression);
136 static ir_node *expression_to_firm(const expression_t *expression);
137 static inline ir_mode *get_ir_mode(type_t *type);
138 static void create_local_declaration(declaration_t *declaration);
140 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
142 unsigned flags = get_atomic_type_flags(kind);
143 unsigned size = get_atomic_type_size(kind);
144 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
145 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
148 unsigned bit_size = size * 8;
149 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
150 ir_mode_arithmetic arithmetic;
151 unsigned modulo_shift;
153 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
154 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
155 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
156 sort = irms_int_number;
157 arithmetic = irma_twos_complement;
158 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
160 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
161 snprintf(name, sizeof(name), "f%d", bit_size);
162 sort = irms_float_number;
163 arithmetic = irma_ieee754;
166 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
174 * Initialises the atomic modes depending on the machine size.
176 static void init_atomic_modes(void)
178 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
179 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
181 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
182 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
184 /* there's no real void type in firm */
185 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
187 /* initialize pointer modes */
189 ir_mode_sort sort = irms_reference;
190 unsigned bit_size = machine_size;
192 ir_mode_arithmetic arithmetic = irma_twos_complement;
193 unsigned modulo_shift
194 = bit_size < machine_size ? machine_size : bit_size;
196 snprintf(name, sizeof(name), "p%d", machine_size);
197 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
200 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
201 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
203 /* Hmm, pointers should be machine size */
204 set_modeP_data(ptr_mode);
205 set_modeP_code(ptr_mode);
208 static unsigned get_compound_type_size(compound_type_t *type)
210 ir_type *irtype = get_ir_type((type_t*) type);
211 return get_type_size_bytes(irtype);
214 static unsigned get_array_type_size(array_type_t *type)
216 assert(!type->is_vla);
217 ir_type *irtype = get_ir_type((type_t*) type);
218 return get_type_size_bytes(irtype);
222 static unsigned get_type_size_const(type_t *type)
226 panic("error type occurred");
228 return get_atomic_type_size(type->atomic.akind);
230 return 2 * get_atomic_type_size(type->complex.akind);
232 return get_atomic_type_size(type->imaginary.akind);
234 return get_mode_size_bytes(mode_int);
235 case TYPE_COMPOUND_UNION:
236 case TYPE_COMPOUND_STRUCT:
237 return get_compound_type_size(&type->compound);
239 /* just a pointer to the function */
240 return get_mode_size_bytes(mode_P_code);
242 return get_mode_size_bytes(mode_P_data);
244 return get_array_type_size(&type->array);
246 return get_type_size_const(type->builtin.real_type);
248 panic("type size of bitfield request");
254 panic("Trying to determine size of invalid type");
257 static ir_node *get_type_size(type_t *type)
259 type = skip_typeref(type);
261 if (is_type_array(type) && type->array.is_vla) {
262 ir_node *size_node = type->array.size_node;
263 if (size_node == NULL) {
264 size_node = expression_to_firm(type->array.size_expression);
265 assert(!is_Const(size_node));
266 type->array.size_node = size_node;
269 ir_node *elem_size = get_type_size(type->array.element_type);
270 ir_mode *mode = get_irn_mode(size_node);
271 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
275 ir_mode *mode = get_ir_mode(type_size_t);
277 sym.type_p = get_ir_type(type);
278 return new_SymConst(mode, sym, symconst_type_size);
281 static unsigned count_parameters(const function_type_t *function_type)
285 function_parameter_t *parameter = function_type->parameters;
286 for ( ; parameter != NULL; parameter = parameter->next) {
294 * Creates a Firm type for an atomic type
296 static ir_type *create_atomic_type(const atomic_type_t *type)
298 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
299 atomic_type_kind_t kind = type->akind;
300 ir_mode *mode = _atomic_modes[kind];
301 ident *id = get_mode_ident(mode);
302 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
304 set_type_alignment_bytes(irtype, type->base.alignment);
310 * Creates a Firm type for a complex type
312 static ir_type *create_complex_type(const complex_type_t *type)
314 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
315 atomic_type_kind_t kind = type->akind;
316 ir_mode *mode = _atomic_modes[kind];
317 ident *id = get_mode_ident(mode);
322 /* FIXME: finish the array */
327 * Creates a Firm type for an imaginary type
329 static ir_type *create_imaginary_type(const imaginary_type_t *type)
331 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
332 atomic_type_kind_t kind = type->akind;
333 ir_mode *mode = _atomic_modes[kind];
334 ident *id = get_mode_ident(mode);
335 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
337 set_type_alignment_bytes(irtype, type->base.alignment);
343 * return type of a parameter (and take transparent union gnu extension into
346 static type_t *get_parameter_type(type_t *type)
348 type = skip_typeref(type);
349 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
350 declaration_t *decl = type->compound.declaration;
351 type = decl->scope.declarations->type;
357 static ir_type *create_method_type(const function_type_t *function_type)
359 type_t *return_type = function_type->return_type;
361 ident *id = id_unique("functiontype.%u");
362 int n_parameters = count_parameters(function_type);
363 int n_results = return_type == type_void ? 0 : 1;
364 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
365 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
367 if (return_type != type_void) {
368 ir_type *restype = get_ir_type(return_type);
369 set_method_res_type(irtype, 0, restype);
372 function_parameter_t *parameter = function_type->parameters;
374 for ( ; parameter != NULL; parameter = parameter->next) {
375 type_t *type = get_parameter_type(parameter->type);
376 ir_type *p_irtype = get_ir_type(type);
377 set_method_param_type(irtype, n, p_irtype);
381 if (function_type->variadic || function_type->unspecified_parameters) {
382 set_method_variadicity(irtype, variadicity_variadic);
385 unsigned cc = get_method_calling_convention(irtype);
386 switch (function_type->calling_convention) {
387 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
390 set_method_calling_convention(irtype, SET_CDECL(cc));
394 if (function_type->variadic || function_type->unspecified_parameters)
397 /* only non-variadic function can use stdcall, else use cdecl */
398 set_method_calling_convention(irtype, SET_STDCALL(cc));
402 if (function_type->variadic || function_type->unspecified_parameters)
404 /* only non-variadic function can use fastcall, else use cdecl */
405 set_method_calling_convention(irtype, SET_FASTCALL(cc));
409 /* Hmm, leave default, not accepted by the parser yet. */
410 warningf(&function_type->base.source_position, "THISCALL calling convention not supported yet");
416 static ir_type *create_pointer_type(pointer_type_t *type)
418 type_t *points_to = type->points_to;
419 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
420 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
421 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
422 ir_points_to, mode_P_data, dbgi);
427 static ir_type *create_array_type(array_type_t *type)
429 type_t *element_type = type->element_type;
430 ir_type *ir_element_type = get_ir_type(element_type);
432 ident *id = id_unique("array.%u");
433 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
434 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
436 const int align = get_type_alignment_bytes(ir_element_type);
437 set_type_alignment_bytes(ir_type, align);
439 if (type->size_constant) {
440 int n_elements = type->size;
442 set_array_bounds_int(ir_type, 0, 0, n_elements);
444 size_t elemsize = get_type_size_bytes(ir_element_type);
445 if (elemsize % align > 0) {
446 elemsize += align - (elemsize % align);
448 set_type_size_bytes(ir_type, n_elements * elemsize);
450 set_array_lower_bound_int(ir_type, 0, 0);
452 set_type_state(ir_type, layout_fixed);
458 * Return the signed integer type of size bits.
460 * @param size the size
462 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
465 static ir_mode *s_modes[64 + 1] = {NULL, };
469 if (size <= 0 || size > 64)
472 mode = s_modes[size];
476 snprintf(name, sizeof(name), "bf_I%u", size);
477 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
478 size <= 32 ? 32 : size );
479 s_modes[size] = mode;
483 snprintf(name, sizeof(name), "I%u", size);
484 ident *id = new_id_from_str(name);
485 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
486 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
487 set_primitive_base_type(res, base_tp);
493 * Return the unsigned integer type of size bits.
495 * @param size the size
497 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
500 static ir_mode *u_modes[64 + 1] = {NULL, };
504 if (size <= 0 || size > 64)
507 mode = u_modes[size];
511 snprintf(name, sizeof(name), "bf_U%u", size);
512 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
513 size <= 32 ? 32 : size );
514 u_modes[size] = mode;
519 snprintf(name, sizeof(name), "U%u", size);
520 ident *id = new_id_from_str(name);
521 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
522 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
523 set_primitive_base_type(res, base_tp);
528 static ir_type *create_bitfield_type(bitfield_type_t *const type)
530 type_t *base = skip_typeref(type->base_type);
531 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
532 ir_type *irbase = get_ir_type(base);
534 unsigned size = fold_constant(type->size);
536 assert(!is_type_float(base));
537 if (is_type_signed(base)) {
538 return get_signed_int_type_for_bit_size(irbase, size);
540 return get_unsigned_int_type_for_bit_size(irbase, size);
544 #define INVALID_TYPE ((ir_type_ptr)-1)
547 COMPOUND_IS_STRUCT = false,
548 COMPOUND_IS_UNION = true
552 * Construct firm type from ast struct type.
554 * As anonymous inner structs get flattened to a single firm type, we might get
555 * irtype, outer_offset and out_align passed (they represent the position of
556 * the anonymous inner struct inside the resulting firm struct)
558 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
559 size_t *outer_offset, size_t *outer_align,
560 bool incomplete, bool is_union)
562 declaration_t *declaration = type->declaration;
563 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
565 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
566 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
568 return declaration->v.irtype;
570 size_t align_all = 1;
572 size_t bit_offset = 0;
575 if (irtype == NULL) {
576 symbol_t *symbol = declaration->symbol;
578 if (symbol != NULL) {
579 id = new_id_from_str(symbol->string);
582 id = id_unique("__anonymous_union.%u");
584 id = id_unique("__anonymous_struct.%u");
587 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
590 irtype = new_d_type_union(id, dbgi);
592 irtype = new_d_type_struct(id, dbgi);
595 declaration->declaration_kind
596 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
597 declaration->v.irtype = irtype;
598 //type->base.firm_type = irtype;
600 offset = *outer_offset;
601 align_all = *outer_align;
607 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
609 declaration_t *entry = declaration->scope.declarations;
610 for( ; entry != NULL; entry = entry->next) {
611 if (entry->namespc != NAMESPACE_NORMAL)
614 size_t prev_offset = offset;
616 symbol_t *symbol = entry->symbol;
617 type_t *entry_type = skip_typeref(entry->type);
618 dbg_info *dbgi = get_dbg_info(&entry->source_position);
621 if (symbol != NULL) {
622 ident = new_id_from_str(symbol->string);
624 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
625 create_compound_type(&entry_type->compound, irtype, &offset,
626 &align_all, false, COMPOUND_IS_STRUCT);
627 goto finished_member;
628 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
629 create_compound_type(&entry_type->compound, irtype, &offset,
630 &align_all, false, COMPOUND_IS_UNION);
631 goto finished_member;
633 assert(entry_type->kind == TYPE_BITFIELD);
635 ident = id_unique("anon.%u");
638 ir_type *base_irtype;
639 if (entry_type->kind == TYPE_BITFIELD) {
640 base_irtype = get_ir_type(entry_type->bitfield.base_type);
642 base_irtype = get_ir_type(entry_type);
645 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
646 size_t misalign = offset % entry_alignment;
648 ir_type *entry_irtype = get_ir_type(entry_type);
649 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
652 size_t bits_remainder;
653 if (entry_type->kind == TYPE_BITFIELD) {
654 size_t size_bits = fold_constant(entry_type->bitfield.size);
655 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
657 if (size_bits > rest_size_bits) {
658 /* start a new bucket */
659 offset += entry_alignment - misalign;
665 /* put into current bucket */
666 base = offset - misalign;
667 bits_remainder = misalign * 8 + bit_offset;
670 offset += size_bits / 8;
671 bit_offset = bit_offset + (size_bits % 8);
673 size_t entry_size = get_type_size_bytes(base_irtype);
674 if (misalign > 0 || bit_offset > 0)
675 offset += entry_alignment - misalign;
679 offset += entry_size;
683 if (entry_alignment > align_all) {
684 if (entry_alignment % align_all != 0) {
685 panic("uneven alignments not supported yet");
687 align_all = entry_alignment;
690 set_entity_offset(entity, base);
691 set_entity_offset_bits_remainder(entity,
692 (unsigned char) bits_remainder);
693 //add_struct_member(irtype, entity);
694 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
695 assert(entry->v.entity == NULL);
696 entry->v.entity = entity;
700 size_t entry_size = offset - prev_offset;
701 if (entry_size > size) {
713 size_t misalign = offset % align_all;
714 if (misalign > 0 || bit_offset > 0) {
715 size += align_all - misalign;
718 if (outer_offset != NULL) {
720 *outer_offset = offset;
722 *outer_offset += size;
725 if (align_all > *outer_align) {
726 if (align_all % *outer_align != 0) {
727 panic("uneven alignments not supported yet");
729 *outer_align = align_all;
732 set_type_alignment_bytes(irtype, align_all);
733 set_type_size_bytes(irtype, size);
734 set_type_state(irtype, layout_fixed);
740 static ir_type *create_enum_type(enum_type_t *const type)
742 type->base.firm_type = ir_type_int;
744 ir_mode *const mode = mode_int;
745 tarval *const one = get_mode_one(mode);
746 tarval * tv_next = get_tarval_null(mode);
748 bool constant_folding_old = constant_folding;
749 constant_folding = true;
751 declaration_t *declaration = type->declaration->next;
752 for (; declaration != NULL; declaration = declaration->next) {
753 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
756 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
758 expression_t *const init = declaration->init.enum_value;
760 ir_node *const cnst = expression_to_firm(init);
761 if (!is_Const(cnst)) {
762 panic("couldn't fold constant");
764 tv_next = get_Const_tarval(cnst);
766 declaration->v.enum_val = tv_next;
767 tv_next = tarval_add(tv_next, one);
770 constant_folding = constant_folding_old;
775 static ir_type *get_ir_type_incomplete(type_t *type)
777 assert(type != NULL);
778 type = skip_typeref(type);
780 if (type->base.firm_type != NULL) {
781 assert(type->base.firm_type != INVALID_TYPE);
782 return type->base.firm_type;
785 switch (type->kind) {
786 case TYPE_COMPOUND_STRUCT:
787 return create_compound_type(&type->compound, NULL, NULL, NULL,
788 true, COMPOUND_IS_STRUCT);
789 case TYPE_COMPOUND_UNION:
790 return create_compound_type(&type->compound, NULL, NULL, NULL,
791 true, COMPOUND_IS_UNION);
793 return get_ir_type(type);
797 static ir_type *get_ir_type(type_t *type)
799 assert(type != NULL);
801 type = skip_typeref(type);
803 if (type->base.firm_type != NULL) {
804 assert(type->base.firm_type != INVALID_TYPE);
805 return type->base.firm_type;
808 ir_type *firm_type = NULL;
809 switch (type->kind) {
811 panic("error type occurred");
813 firm_type = create_atomic_type(&type->atomic);
816 firm_type = create_complex_type(&type->complex);
819 firm_type = create_imaginary_type(&type->imaginary);
822 firm_type = create_method_type(&type->function);
825 firm_type = create_pointer_type(&type->pointer);
828 firm_type = create_array_type(&type->array);
830 case TYPE_COMPOUND_STRUCT:
831 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
832 false, COMPOUND_IS_STRUCT);
834 case TYPE_COMPOUND_UNION:
835 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
836 false, COMPOUND_IS_UNION);
839 firm_type = create_enum_type(&type->enumt);
842 firm_type = get_ir_type(type->builtin.real_type);
845 firm_type = create_bitfield_type(&type->bitfield);
853 if (firm_type == NULL)
854 panic("unknown type found");
856 type->base.firm_type = firm_type;
860 static inline ir_mode *get_ir_mode(type_t *type)
862 ir_type *irtype = get_ir_type(type);
864 /* firm doesn't report a mode for arrays somehow... */
865 if (is_Array_type(irtype)) {
869 ir_mode *mode = get_type_mode(irtype);
870 assert(mode != NULL);
874 /** Names of the runtime functions. */
875 static const struct {
876 int id; /**< the rts id */
877 int n_res; /**< number of return values */
878 const char *name; /**< the name of the rts function */
879 int n_params; /**< number of parameters */
880 unsigned flags; /**< language flags */
882 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
883 { rts_abort, 0, "abort", 0, _C89 },
884 { rts_alloca, 1, "alloca", 1, _ALL },
885 { rts_abs, 1, "abs", 1, _C89 },
886 { rts_labs, 1, "labs", 1, _C89 },
887 { rts_llabs, 1, "llabs", 1, _C99 },
888 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
890 { rts_fabs, 1, "fabs", 1, _C89 },
891 { rts_sqrt, 1, "sqrt", 1, _C89 },
892 { rts_cbrt, 1, "cbrt", 1, _C99 },
893 { rts_exp, 1, "exp", 1, _C89 },
894 { rts_exp2, 1, "exp2", 1, _C89 },
895 { rts_exp10, 1, "exp10", 1, _GNUC },
896 { rts_log, 1, "log", 1, _C89 },
897 { rts_log2, 1, "log2", 1, _C89 },
898 { rts_log10, 1, "log10", 1, _C89 },
899 { rts_pow, 1, "pow", 2, _C89 },
900 { rts_sin, 1, "sin", 1, _C89 },
901 { rts_cos, 1, "cos", 1, _C89 },
902 { rts_tan, 1, "tan", 1, _C89 },
903 { rts_asin, 1, "asin", 1, _C89 },
904 { rts_acos, 1, "acos", 1, _C89 },
905 { rts_atan, 1, "atan", 1, _C89 },
906 { rts_sinh, 1, "sinh", 1, _C89 },
907 { rts_cosh, 1, "cosh", 1, _C89 },
908 { rts_tanh, 1, "tanh", 1, _C89 },
910 { rts_fabsf, 1, "fabsf", 1, _C99 },
911 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
912 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
913 { rts_expf, 1, "expf", 1, _C99 },
914 { rts_exp2f, 1, "exp2f", 1, _C99 },
915 { rts_exp10f, 1, "exp10f", 1, _GNUC },
916 { rts_logf, 1, "logf", 1, _C99 },
917 { rts_log2f, 1, "log2f", 1, _C99 },
918 { rts_log10f, 1, "log10f", 1, _C99 },
919 { rts_powf, 1, "powf", 2, _C99 },
920 { rts_sinf, 1, "sinf", 1, _C99 },
921 { rts_cosf, 1, "cosf", 1, _C99 },
922 { rts_tanf, 1, "tanf", 1, _C99 },
923 { rts_asinf, 1, "asinf", 1, _C99 },
924 { rts_acosf, 1, "acosf", 1, _C99 },
925 { rts_atanf, 1, "atanf", 1, _C99 },
926 { rts_sinhf, 1, "sinhf", 1, _C99 },
927 { rts_coshf, 1, "coshf", 1, _C99 },
928 { rts_tanhf, 1, "tanhf", 1, _C99 },
930 { rts_fabsl, 1, "fabsl", 1, _C99 },
931 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
932 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
933 { rts_expl, 1, "expl", 1, _C99 },
934 { rts_exp2l, 1, "exp2l", 1, _C99 },
935 { rts_exp10l, 1, "exp10l", 1, _GNUC },
936 { rts_logl, 1, "logl", 1, _C99 },
937 { rts_log2l, 1, "log2l", 1, _C99 },
938 { rts_log10l, 1, "log10l", 1, _C99 },
939 { rts_powl, 1, "powl", 2, _C99 },
940 { rts_sinl, 1, "sinl", 1, _C99 },
941 { rts_cosl, 1, "cosl", 1, _C99 },
942 { rts_tanl, 1, "tanl", 1, _C99 },
943 { rts_asinl, 1, "asinl", 1, _C99 },
944 { rts_acosl, 1, "acosl", 1, _C99 },
945 { rts_atanl, 1, "atanl", 1, _C99 },
946 { rts_sinhl, 1, "sinhl", 1, _C99 },
947 { rts_coshl, 1, "coshl", 1, _C99 },
948 { rts_tanhl, 1, "tanhl", 1, _C99 },
950 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
951 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
952 { rts_strcmp, 1, "strcmp", 2, _C89 },
953 { rts_strncmp, 1, "strncmp", 3, _C89 }
956 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
959 * Mangles an entity linker (ld) name for win32 usage.
961 * @param ent the entity to be mangled
962 * @param declaration the declaration
964 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
968 if (is_Method_type(get_entity_type(ent)))
969 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
971 /* always add an underscore in win32 */
972 id = mangle(id_underscore, get_entity_ident(ent));
975 decl_modifiers_t decl_modifiers = declaration->modifiers;
976 if (decl_modifiers & DM_DLLIMPORT) {
977 /* add prefix for imported symbols */
978 id = mangle(id_imp, id);
984 * Mangles an entity linker (ld) name for Linux ELF usage.
986 * @param ent the entity to be mangled
987 * @param declaration the declaration
989 static ident *create_ld_ident_linux_elf(ir_entity *entity,
990 declaration_t *declaration)
993 return get_entity_ident(entity);
997 * Mangles an entity linker (ld) name for Mach-O usage.
999 * @param ent the entity to be mangled
1000 * @param declaration the declaration
1002 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
1005 ident *id = mangle(id_underscore, get_entity_ident(ent));
1009 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
1010 declaration_t *declaration);
1011 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
1014 * Handle GNU attributes for entities
1016 * @param ent the entity
1017 * @param decl the routine declaration
1019 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
1021 if (decl->modifiers & DM_PURE) {
1022 /* TRUE if the declaration includes the GNU
1023 __attribute__((pure)) specifier. */
1024 set_entity_additional_property(ent, mtp_property_pure);
1026 if (decl->modifiers & DM_USED) {
1027 /* TRUE if the declaration includes the GNU
1028 __attribute__((used)) specifier. */
1029 set_entity_stickyness(ent, stickyness_sticky);
1034 * Creates an entity representing a function.
1036 * @param declaration the function declaration
1038 static ir_entity *get_function_entity(declaration_t *declaration)
1040 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION)
1041 return declaration->v.entity;
1042 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1044 symbol_t *symbol = declaration->symbol;
1045 ident *id = new_id_from_str(symbol->string);
1047 ir_type *global_type = get_glob_type();
1048 ir_type *ir_type_method = get_ir_type(declaration->type);
1049 assert(is_Method_type(ir_type_method));
1051 /* already an entity defined? */
1052 ir_entity *entity = entitymap_get(&entitymap, symbol);
1053 if (entity != NULL) {
1054 if (get_entity_visibility(entity) == visibility_external_allocated
1055 && declaration->init.statement != NULL) {
1056 set_entity_visibility(entity, visibility_external_visible);
1058 goto entity_created;
1061 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1062 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1063 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1065 handle_gnu_attributes_ent(entity, declaration);
1067 /* static inline => local
1068 * extern inline => local
1069 * inline without definition => local
1070 * inline with definition => external_visible */
1071 storage_class_tag_t const storage_class = declaration->storage_class;
1072 bool const is_inline = declaration->is_inline;
1073 bool const has_body = declaration->init.statement != NULL;
1074 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1075 set_entity_visibility(entity, visibility_external_visible);
1076 } else if (storage_class == STORAGE_CLASS_STATIC ||
1077 (is_inline && has_body)) {
1079 /* this entity was declared, but is defined nowhere */
1080 set_entity_peculiarity(entity, peculiarity_description);
1082 set_entity_visibility(entity, visibility_local);
1083 } else if (has_body) {
1084 set_entity_visibility(entity, visibility_external_visible);
1086 set_entity_visibility(entity, visibility_external_allocated);
1088 set_entity_allocation(entity, allocation_static);
1090 /* We should check for file scope here, but as long as we compile C only
1091 this is not needed. */
1092 if (! firm_opt.freestanding) {
1093 /* check for a known runtime function */
1094 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1095 if (id != rts_idents[i])
1098 /* ignore those rts functions not necessary needed for current mode */
1099 if ((c_mode & rts_data[i].flags) == 0)
1101 assert(rts_entities[rts_data[i].id] == NULL);
1102 rts_entities[rts_data[i].id] = entity;
1106 entitymap_insert(&entitymap, symbol, entity);
1109 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1110 declaration->v.entity = entity;
1116 * Creates a Const node representing a constant.
1118 static ir_node *const_to_firm(const const_expression_t *cnst)
1120 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1121 ir_mode *mode = get_ir_mode(cnst->base.type);
1126 if (mode_is_float(mode)) {
1127 tv = new_tarval_from_double(cnst->v.float_value, mode);
1129 if (mode_is_signed(mode)) {
1130 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1132 len = snprintf(buf, sizeof(buf), "%llu",
1133 (unsigned long long) cnst->v.int_value);
1135 tv = new_tarval_from_str(buf, len, mode);
1138 return new_d_Const(dbgi, mode, tv);
1142 * Creates a Const node representing a character constant.
1144 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1146 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1147 ir_mode *mode = get_ir_mode(cnst->base.type);
1149 long long int v = 0;
1150 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1151 if (char_is_signed) {
1152 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1154 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1158 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1159 tarval *tv = new_tarval_from_str(buf, len, mode);
1161 return new_d_Const(dbgi, mode, tv);
1165 * Creates a Const node representing a wide character constant.
1167 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1169 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1170 ir_mode *mode = get_ir_mode(cnst->base.type);
1172 long long int v = cnst->v.wide_character.begin[0];
1175 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1176 tarval *tv = new_tarval_from_str(buf, len, mode);
1178 return new_d_Const(dbgi, mode, tv);
1182 * Creates a SymConst for a given entity.
1184 * @param dbgi debug info
1185 * @param mode the (reference) mode for the SymConst
1186 * @param entity the entity
1188 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1191 assert(entity != NULL);
1192 union symconst_symbol sym;
1193 sym.entity_p = entity;
1194 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1198 * Creates a SymConst node representing a string constant.
1200 * @param src_pos the source position of the string constant
1201 * @param id_prefix a prefix for the name of the generated string constant
1202 * @param value the value of the string constant
1204 static ir_node *string_to_firm(const source_position_t *const src_pos,
1205 const char *const id_prefix,
1206 const string_t *const value)
1208 ir_type *const global_type = get_glob_type();
1209 dbg_info *const dbgi = get_dbg_info(src_pos);
1210 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1211 ir_type_const_char, dbgi);
1213 ident *const id = id_unique(id_prefix);
1214 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1215 set_entity_ld_ident(entity, id);
1216 set_entity_variability(entity, variability_constant);
1217 set_entity_allocation(entity, allocation_static);
1219 ir_type *const elem_type = ir_type_const_char;
1220 ir_mode *const mode = get_type_mode(elem_type);
1222 const char* const string = value->begin;
1223 const size_t slen = value->size;
1225 set_array_lower_bound_int(type, 0, 0);
1226 set_array_upper_bound_int(type, 0, slen);
1227 set_type_size_bytes(type, slen);
1228 set_type_state(type, layout_fixed);
1230 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1231 for(size_t i = 0; i < slen; ++i) {
1232 tvs[i] = new_tarval_from_long(string[i], mode);
1235 set_array_entity_values(entity, tvs, slen);
1238 return create_symconst(dbgi, mode_P_data, entity);
1242 * Creates a SymConst node representing a string literal.
1244 * @param literal the string literal
1246 static ir_node *string_literal_to_firm(
1247 const string_literal_expression_t* literal)
1249 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1254 * Creates a SymConst node representing a wide string literal.
1256 * @param literal the wide string literal
1258 static ir_node *wide_string_literal_to_firm(
1259 const wide_string_literal_expression_t* const literal)
1261 ir_type *const global_type = get_glob_type();
1262 ir_type *const elem_type = ir_type_wchar_t;
1263 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1264 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1267 ident *const id = id_unique("Lstr.%u");
1268 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1269 set_entity_ld_ident(entity, id);
1270 set_entity_variability(entity, variability_constant);
1271 set_entity_allocation(entity, allocation_static);
1273 ir_mode *const mode = get_type_mode(elem_type);
1275 const wchar_rep_t *const string = literal->value.begin;
1276 const size_t slen = literal->value.size;
1278 set_array_lower_bound_int(type, 0, 0);
1279 set_array_upper_bound_int(type, 0, slen);
1280 set_type_size_bytes(type, slen);
1281 set_type_state(type, layout_fixed);
1283 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1284 for(size_t i = 0; i < slen; ++i) {
1285 tvs[i] = new_tarval_from_long(string[i], mode);
1288 set_array_entity_values(entity, tvs, slen);
1291 return create_symconst(dbgi, mode_P_data, entity);
1294 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1295 ir_node *const addr)
1297 ir_type *irtype = get_ir_type(type);
1298 if (is_compound_type(irtype)
1299 || is_Method_type(irtype)
1300 || is_Array_type(irtype)) {
1304 ir_mode *const mode = get_type_mode(irtype);
1305 ir_node *const memory = get_store();
1306 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1307 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1308 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1310 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1311 set_Load_volatility(load, volatility_is_volatile);
1314 set_store(load_mem);
1319 * Creates a strict Conv if neccessary.
1321 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1323 ir_mode *mode = get_irn_mode(node);
1325 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1327 if (!mode_is_float(mode))
1330 /* check if there is already a Conv */
1331 if (is_Conv(node)) {
1332 /* convert it into a strict Conv */
1333 set_Conv_strict(node, 1);
1337 /* otherwise create a new one */
1338 return new_d_strictConv(dbgi, node, mode);
1341 static ir_node *get_global_var_address(dbg_info *const dbgi,
1342 const declaration_t *const decl)
1344 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1346 ir_entity *const entity = decl->v.entity;
1347 switch ((storage_class_tag_t)decl->storage_class) {
1348 case STORAGE_CLASS_THREAD:
1349 case STORAGE_CLASS_THREAD_EXTERN:
1350 case STORAGE_CLASS_THREAD_STATIC: {
1351 ir_node *const no_mem = new_NoMem();
1352 ir_node *const tls = get_irg_tls(current_ir_graph);
1353 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1357 return create_symconst(dbgi, mode_P_data, entity);
1362 * Returns the correct base address depending on whether it is a parameter or a
1363 * normal local variable.
1365 static ir_node *get_local_frame(ir_entity *const ent)
1367 ir_graph *const irg = current_ir_graph;
1368 const ir_type *const owner = get_entity_owner(ent);
1369 if (owner == get_irg_frame_type(irg)) {
1370 return get_irg_frame(irg);
1372 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1373 return get_irg_value_param_base(irg);
1377 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1379 ir_mode *value_mode = get_irn_mode(value);
1381 if (value_mode == dest_mode || is_Bad(value))
1384 if (dest_mode == mode_b) {
1385 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1386 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1387 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1391 return new_d_Conv(dbgi, value, dest_mode);
1394 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1396 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1397 declaration_t *declaration = ref->declaration;
1398 type_t *type = skip_typeref(declaration->type);
1400 /* make sure the type is constructed */
1401 (void) get_ir_type(type);
1403 switch((declaration_kind_t) declaration->declaration_kind) {
1404 case DECLARATION_KIND_TYPE:
1405 case DECLARATION_KIND_UNKNOWN:
1408 case DECLARATION_KIND_ENUM_ENTRY: {
1409 ir_mode *const mode = get_ir_mode(type);
1410 return new_Const(mode, declaration->v.enum_val);
1413 case DECLARATION_KIND_LOCAL_VARIABLE: {
1414 ir_mode *const mode = get_ir_mode(type);
1415 return get_value(declaration->v.value_number, mode);
1417 case DECLARATION_KIND_FUNCTION: {
1418 ir_mode *const mode = get_ir_mode(type);
1419 return create_symconst(dbgi, mode, declaration->v.entity);
1421 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1422 ir_node *const addr = get_global_var_address(dbgi, declaration);
1423 return deref_address(dbgi, declaration->type, addr);
1426 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1427 ir_entity *entity = declaration->v.entity;
1428 ir_node *frame = get_local_frame(entity);
1429 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1430 return deref_address(dbgi, declaration->type, sel);
1433 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1434 return declaration->v.vla_base;
1436 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1437 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1438 case DECLARATION_KIND_COMPOUND_MEMBER:
1439 case DECLARATION_KIND_LABEL_BLOCK:
1440 panic("not implemented reference type");
1443 panic("reference to declaration with unknown type found");
1446 static ir_node *reference_addr(const reference_expression_t *ref)
1448 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1449 declaration_t *declaration = ref->declaration;
1451 switch((declaration_kind_t) declaration->declaration_kind) {
1452 case DECLARATION_KIND_TYPE:
1453 case DECLARATION_KIND_UNKNOWN:
1455 case DECLARATION_KIND_LOCAL_VARIABLE:
1456 /* you can store to a local variable (so we don't panic but return NULL
1457 * as an indicator for no real address) */
1459 case DECLARATION_KIND_FUNCTION: {
1460 type_t *const type = skip_typeref(ref->base.type);
1461 ir_mode *const mode = get_ir_mode(type);
1462 return create_symconst(dbgi, mode, declaration->v.entity);
1464 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1465 ir_node *const addr = get_global_var_address(dbgi, declaration);
1468 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1469 ir_entity *entity = declaration->v.entity;
1470 ir_node *frame = get_local_frame(entity);
1471 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1476 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1477 return declaration->v.vla_base;
1479 case DECLARATION_KIND_ENUM_ENTRY:
1480 panic("trying to reference enum entry");
1482 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1483 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1484 case DECLARATION_KIND_COMPOUND_MEMBER:
1485 case DECLARATION_KIND_LABEL_BLOCK:
1486 panic("not implemented reference type");
1489 panic("reference to declaration with unknown type found");
1493 * Transform calls to builtin functions.
1495 static ir_node *process_builtin_call(const call_expression_t *call)
1497 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1499 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1500 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1502 type_t *type = skip_typeref(builtin->base.type);
1503 assert(is_type_pointer(type));
1505 type_t *function_type = skip_typeref(type->pointer.points_to);
1506 symbol_t *symbol = builtin->symbol;
1508 switch(symbol->ID) {
1509 case T___builtin_alloca: {
1510 if (call->arguments == NULL || call->arguments->next != NULL) {
1511 panic("invalid number of parameters on __builtin_alloca");
1513 expression_t *argument = call->arguments->expression;
1514 ir_node *size = expression_to_firm(argument);
1516 ir_node *store = get_store();
1517 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1519 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1521 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1525 case T___builtin_huge_val: {
1526 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1527 tarval *tv = get_mode_infinite(mode);
1528 ir_node *res = new_d_Const(dbgi, mode, tv);
1531 case T___builtin_nan:
1532 case T___builtin_nanf:
1533 case T___builtin_nand: {
1534 /* Ignore string for now... */
1535 assert(is_type_function(function_type));
1536 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1537 tarval *tv = get_mode_NAN(mode);
1538 ir_node *res = new_d_Const(dbgi, mode, tv);
1541 case T___builtin_va_end:
1544 panic("Unsupported builtin found\n");
1549 * Transform a call expression.
1550 * Handles some special cases, like alloca() calls, which must be resolved
1551 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1552 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1555 static ir_node *call_expression_to_firm(const call_expression_t *call)
1557 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1558 assert(get_cur_block() != NULL);
1560 expression_t *function = call->function;
1561 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1562 return process_builtin_call(call);
1564 if (function->kind == EXPR_REFERENCE) {
1565 const reference_expression_t *ref = &function->reference;
1566 declaration_t *declaration = ref->declaration;
1568 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1569 if (declaration->v.entity == rts_entities[rts_alloca]) {
1570 /* handle alloca() call */
1571 expression_t *argument = call->arguments->expression;
1572 ir_node *size = expression_to_firm(argument);
1574 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1576 ir_node *store = get_store();
1577 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1578 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1580 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1582 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1588 ir_node *callee = expression_to_firm(function);
1590 type_t *type = skip_typeref(function->base.type);
1591 assert(is_type_pointer(type));
1592 pointer_type_t *pointer_type = &type->pointer;
1593 type_t *points_to = skip_typeref(pointer_type->points_to);
1594 assert(is_type_function(points_to));
1595 function_type_t *function_type = &points_to->function;
1597 int n_parameters = 0;
1598 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1599 ir_type *new_method_type = NULL;
1600 if (function_type->variadic || function_type->unspecified_parameters) {
1601 const call_argument_t *argument = call->arguments;
1602 for( ; argument != NULL; argument = argument->next) {
1606 /* we need to construct a new method type matching the call
1608 int n_res = get_method_n_ress(ir_method_type);
1609 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1610 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1611 n_parameters, n_res, dbgi);
1612 set_method_calling_convention(new_method_type,
1613 get_method_calling_convention(ir_method_type));
1614 set_method_additional_properties(new_method_type,
1615 get_method_additional_properties(ir_method_type));
1616 set_method_variadicity(new_method_type,
1617 get_method_variadicity(ir_method_type));
1619 for(int i = 0; i < n_res; ++i) {
1620 set_method_res_type(new_method_type, i,
1621 get_method_res_type(ir_method_type, i));
1623 argument = call->arguments;
1624 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1625 expression_t *expression = argument->expression;
1626 ir_type *irtype = get_ir_type(expression->base.type);
1627 set_method_param_type(new_method_type, i, irtype);
1629 ir_method_type = new_method_type;
1631 n_parameters = get_method_n_params(ir_method_type);
1634 ir_node *in[n_parameters];
1636 const call_argument_t *argument = call->arguments;
1637 for(int n = 0; n < n_parameters; ++n) {
1638 expression_t *expression = argument->expression;
1639 ir_node *arg_node = expression_to_firm(expression);
1641 arg_node = do_strict_conv(dbgi, arg_node);
1645 argument = argument->next;
1648 ir_node *store = get_store();
1649 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1651 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1654 type_t *return_type = skip_typeref(function_type->return_type);
1655 ir_node *result = NULL;
1657 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1659 if (is_type_scalar(return_type)) {
1660 mode = get_ir_mode(return_type);
1664 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1665 result = new_d_Proj(dbgi, resproj, mode, 0);
1671 static void statement_to_firm(statement_t *statement);
1672 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1674 static ir_node *expression_to_addr(const expression_t *expression);
1675 static void create_condition_evaluation(const expression_t *expression,
1676 ir_node *true_block,
1677 ir_node *false_block);
1679 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1682 value = do_strict_conv(dbgi, value);
1684 ir_node *memory = get_store();
1686 if (is_type_scalar(type)) {
1687 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1688 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1689 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1690 set_Store_volatility(store, volatility_is_volatile);
1691 set_store(store_mem);
1693 ir_type *irtype = get_ir_type(type);
1694 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1695 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1696 set_store(copyb_mem);
1700 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1702 tarval *all_one = get_mode_all_one(mode);
1703 int mode_size = get_mode_size_bits(mode);
1705 assert(offset >= 0);
1707 assert(offset + size <= mode_size);
1708 if (size == mode_size) {
1712 long shiftr = get_mode_size_bits(mode) - size;
1713 long shiftl = offset;
1714 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1715 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1716 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1717 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1722 static void bitfield_store_to_firm(const select_expression_t *expression,
1723 ir_node *addr, ir_node *value)
1725 type_t *type = expression->base.type;
1726 ir_mode *mode = get_ir_mode(type);
1728 assert(get_irn_mode(value) == mode || is_Bad(value));
1730 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1732 /* kill upper bits of value and shift to right position */
1733 ir_entity *entity = expression->compound_entry->v.entity;
1734 int bitoffset = get_entity_offset_bits_remainder(entity);
1735 ir_type *entity_type = get_entity_type(entity);
1736 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1738 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1739 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1740 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1741 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1742 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1743 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1745 /* load current value */
1746 ir_node *mem = get_store();
1747 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1748 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1749 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1750 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1751 tarval *inv_mask = tarval_not(shift_mask);
1752 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1753 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1755 /* construct new value and store */
1756 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1757 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1758 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1759 set_store(store_mem);
1761 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1762 set_Load_volatility(load, volatility_is_volatile);
1763 set_Store_volatility(store, volatility_is_volatile);
1767 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1770 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1771 type_t *type = expression->base.type;
1772 ir_mode *mode = get_ir_mode(type);
1773 ir_node *mem = get_store();
1774 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1775 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1776 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1778 load_res = create_conv(dbgi, load_res, mode_int);
1780 set_store(load_mem);
1782 /* kill upper bits */
1783 ir_entity *entity = expression->compound_entry->v.entity;
1784 int bitoffset = get_entity_offset_bits_remainder(entity);
1785 ir_type *entity_type = get_entity_type(entity);
1786 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1787 long shift_bitsl = machine_size - bitoffset - bitsize;
1788 assert(shift_bitsl >= 0);
1789 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1790 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1791 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1793 long shift_bitsr = bitoffset + shift_bitsl;
1794 assert(shift_bitsr <= (long) machine_size);
1795 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1796 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1798 if (mode_is_signed(mode)) {
1799 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1801 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1804 return create_conv(dbgi, shiftr, mode);
1807 static void set_value_for_expression_addr(const expression_t *expression,
1808 ir_node *value, ir_node *addr)
1810 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1811 value = do_strict_conv(dbgi, value);
1813 if (expression->kind == EXPR_REFERENCE) {
1814 const reference_expression_t *ref = &expression->reference;
1816 declaration_t *declaration = ref->declaration;
1817 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1818 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1819 set_value(declaration->v.value_number, value);
1825 addr = expression_to_addr(expression);
1827 type_t *type = skip_typeref(expression->base.type);
1829 if (expression->kind == EXPR_SELECT) {
1830 const select_expression_t *select = &expression->select;
1832 declaration_t *declaration = select->compound_entry;
1833 if (declaration->type->kind == TYPE_BITFIELD) {
1834 bitfield_store_to_firm(select, addr, value);
1839 assign_value(dbgi, addr, type, value);
1842 static void set_value_for_expression(const expression_t *expression,
1845 set_value_for_expression_addr(expression, value, NULL);
1848 static ir_node *get_value_from_lvalue(const expression_t *expression,
1851 if (expression->kind == EXPR_REFERENCE) {
1852 const reference_expression_t *ref = &expression->reference;
1854 declaration_t *declaration = ref->declaration;
1855 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1856 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1857 assert(addr == NULL);
1858 ir_mode *mode = get_ir_mode(expression->base.type);
1859 return get_value(declaration->v.value_number, mode);
1863 assert(addr != NULL);
1864 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1867 if (expression->kind == EXPR_SELECT &&
1868 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1869 value = bitfield_extract_to_firm(&expression->select, addr);
1871 value = deref_address(dbgi, expression->base.type, addr);
1878 static ir_node *create_incdec(const unary_expression_t *expression)
1880 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1881 const expression_t *value_expr = expression->value;
1882 ir_node *addr = expression_to_addr(value_expr);
1883 ir_node *value = get_value_from_lvalue(value_expr, addr);
1885 type_t *type = skip_typeref(expression->base.type);
1886 ir_mode *mode = get_ir_mode(expression->base.type);
1889 if (is_type_pointer(type)) {
1890 pointer_type_t *pointer_type = &type->pointer;
1891 offset = get_type_size(pointer_type->points_to);
1893 assert(is_type_arithmetic(type));
1894 offset = new_Const(mode, get_mode_one(mode));
1898 ir_node *store_value;
1899 switch(expression->base.kind) {
1900 case EXPR_UNARY_POSTFIX_INCREMENT:
1902 store_value = new_d_Add(dbgi, value, offset, mode);
1904 case EXPR_UNARY_POSTFIX_DECREMENT:
1906 store_value = new_d_Sub(dbgi, value, offset, mode);
1908 case EXPR_UNARY_PREFIX_INCREMENT:
1909 result = new_d_Add(dbgi, value, offset, mode);
1910 store_value = result;
1912 case EXPR_UNARY_PREFIX_DECREMENT:
1913 result = new_d_Sub(dbgi, value, offset, mode);
1914 store_value = result;
1917 panic("no incdec expr in create_incdec");
1920 set_value_for_expression_addr(value_expr, store_value, addr);
1925 static bool is_local_variable(expression_t *expression)
1927 if (expression->kind != EXPR_REFERENCE)
1929 reference_expression_t *ref_expr = &expression->reference;
1930 declaration_t *declaration = ref_expr->declaration;
1931 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1934 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1937 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1938 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1939 case EXPR_BINARY_NOTEQUAL:
1940 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
1941 case EXPR_BINARY_ISLESS:
1942 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
1943 case EXPR_BINARY_ISLESSEQUAL:
1944 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
1945 case EXPR_BINARY_ISGREATER:
1946 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
1947 case EXPR_BINARY_ISGREATEREQUAL:
1948 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
1949 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
1954 panic("trying to get pn_Cmp from non-comparison binexpr type");
1958 * Handle the assume optimizer hint: check if a Confirm
1959 * node can be created.
1961 * @param dbi debug info
1962 * @param expr the IL assume expression
1964 * we support here only some simple cases:
1969 static ir_node *handle_assume_compare(dbg_info *dbi,
1970 const binary_expression_t *expression)
1972 expression_t *op1 = expression->left;
1973 expression_t *op2 = expression->right;
1974 declaration_t *var2, *var = NULL;
1975 ir_node *res = NULL;
1978 cmp_val = get_pnc(expression->base.kind, op1->base.type);
1980 if (is_local_variable(op1) && is_local_variable(op2)) {
1981 var = op1->reference.declaration;
1982 var2 = op2->reference.declaration;
1984 type_t *const type = skip_typeref(var->type);
1985 ir_mode *const mode = get_ir_mode(type);
1987 ir_node *const irn1 = get_value(var->v.value_number, mode);
1988 ir_node *const irn2 = get_value(var2->v.value_number, mode);
1990 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
1991 set_value(var2->v.value_number, res);
1993 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
1994 set_value(var->v.value_number, res);
2000 if (is_local_variable(op1) && is_constant_expression(op2)) {
2001 var = op1->reference.declaration;
2003 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2004 cmp_val = get_inversed_pnc(cmp_val);
2005 var = op2->reference.declaration;
2010 type_t *const type = skip_typeref(var->type);
2011 ir_mode *const mode = get_ir_mode(type);
2013 res = get_value(var->v.value_number, mode);
2014 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2015 set_value(var->v.value_number, res);
2021 * Handle the assume optimizer hint.
2023 * @param dbi debug info
2024 * @param expr the IL assume expression
2026 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2027 switch(expression->kind) {
2028 case EXPR_BINARY_EQUAL:
2029 case EXPR_BINARY_NOTEQUAL:
2030 case EXPR_BINARY_LESS:
2031 case EXPR_BINARY_LESSEQUAL:
2032 case EXPR_BINARY_GREATER:
2033 case EXPR_BINARY_GREATEREQUAL:
2034 return handle_assume_compare(dbi, &expression->binary);
2040 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2042 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2043 type_t *type = skip_typeref(expression->base.type);
2045 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2046 return expression_to_addr(expression->value);
2048 const expression_t *value = expression->value;
2050 switch(expression->base.kind) {
2051 case EXPR_UNARY_NEGATE: {
2052 ir_node *value_node = expression_to_firm(value);
2053 ir_mode *mode = get_ir_mode(type);
2054 return new_d_Minus(dbgi, value_node, mode);
2056 case EXPR_UNARY_PLUS:
2057 return expression_to_firm(value);
2058 case EXPR_UNARY_BITWISE_NEGATE: {
2059 ir_node *value_node = expression_to_firm(value);
2060 ir_mode *mode = get_ir_mode(type);
2061 return new_d_Not(dbgi, value_node, mode);
2063 case EXPR_UNARY_NOT: {
2064 ir_node *value_node = _expression_to_firm(value);
2065 value_node = create_conv(dbgi, value_node, mode_b);
2066 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2069 case EXPR_UNARY_DEREFERENCE: {
2070 ir_node *value_node = expression_to_firm(value);
2071 type_t *value_type = skip_typeref(value->base.type);
2072 assert(is_type_pointer(value_type));
2073 type_t *points_to = value_type->pointer.points_to;
2074 return deref_address(dbgi, points_to, value_node);
2076 case EXPR_UNARY_POSTFIX_INCREMENT:
2077 case EXPR_UNARY_POSTFIX_DECREMENT:
2078 case EXPR_UNARY_PREFIX_INCREMENT:
2079 case EXPR_UNARY_PREFIX_DECREMENT:
2080 return create_incdec(expression);
2081 case EXPR_UNARY_CAST: {
2082 ir_node *value_node = expression_to_firm(value);
2083 if (is_type_scalar(type)) {
2084 ir_mode *mode = get_ir_mode(type);
2085 ir_node *node = create_conv(dbgi, value_node, mode);
2086 node = do_strict_conv(dbgi, node);
2089 /* make sure firm type is constructed */
2090 (void) get_ir_type(type);
2094 case EXPR_UNARY_CAST_IMPLICIT: {
2095 ir_node *value_node = expression_to_firm(value);
2096 if (is_type_scalar(type)) {
2097 ir_mode *mode = get_ir_mode(type);
2098 return create_conv(dbgi, value_node, mode);
2103 case EXPR_UNARY_ASSUME:
2104 if (firm_opt.confirm)
2105 return handle_assume(dbgi, value);
2112 panic("invalid UNEXPR type found");
2116 * produces a 0/1 depending of the value of a mode_b node
2118 static ir_node *produce_condition_result(const expression_t *expression,
2121 ir_mode *mode = get_ir_mode(expression->base.type);
2122 ir_node *cur_block = get_cur_block();
2124 ir_node *one_block = new_immBlock();
2125 ir_node *one = new_Const(mode, get_mode_one(mode));
2126 ir_node *jmp_one = new_d_Jmp(dbgi);
2128 ir_node *zero_block = new_immBlock();
2129 ir_node *zero = new_Const(mode, get_mode_null(mode));
2130 ir_node *jmp_zero = new_d_Jmp(dbgi);
2132 set_cur_block(cur_block);
2133 create_condition_evaluation(expression, one_block, zero_block);
2134 mature_immBlock(one_block);
2135 mature_immBlock(zero_block);
2137 ir_node *common_block = new_immBlock();
2138 add_immBlock_pred(common_block, jmp_one);
2139 add_immBlock_pred(common_block, jmp_zero);
2140 mature_immBlock(common_block);
2142 ir_node *in[2] = { one, zero };
2143 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2148 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2149 ir_node *value, type_t *type)
2151 pointer_type_t *const pointer_type = &type->pointer;
2152 type_t *const points_to = skip_typeref(pointer_type->points_to);
2153 unsigned elem_size = get_type_size_const(points_to);
2156 if (elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
2160 assert(elem_size >= 1);
2164 value = create_conv(dbgi, value, mode_int);
2165 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2166 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2170 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2171 ir_node *left, ir_node *right)
2174 type_t *type_left = skip_typeref(expression->left->base.type);
2175 type_t *type_right = skip_typeref(expression->right->base.type);
2177 expression_kind_t kind = expression->base.kind;
2180 case EXPR_BINARY_SHIFTLEFT:
2181 case EXPR_BINARY_SHIFTRIGHT:
2182 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2183 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2184 mode = get_irn_mode(left);
2185 right = create_conv(dbgi, right, mode_uint);
2188 case EXPR_BINARY_SUB:
2189 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2190 const pointer_type_t *const ptr_type = &type_left->pointer;
2192 mode = get_ir_mode(expression->base.type);
2193 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2194 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2195 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2196 ir_node *const no_mem = new_NoMem();
2197 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2198 mode, op_pin_state_floats);
2199 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2202 case EXPR_BINARY_SUB_ASSIGN:
2203 if (is_type_pointer(type_left)) {
2204 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2205 mode = get_ir_mode(type_left);
2210 case EXPR_BINARY_ADD:
2211 case EXPR_BINARY_ADD_ASSIGN:
2212 if (is_type_pointer(type_left)) {
2213 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2214 mode = get_ir_mode(type_left);
2216 } else if (is_type_pointer(type_right)) {
2217 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2218 mode = get_ir_mode(type_right);
2225 mode = get_irn_mode(right);
2226 left = create_conv(dbgi, left, mode);
2231 case EXPR_BINARY_ADD_ASSIGN:
2232 case EXPR_BINARY_ADD:
2233 return new_d_Add(dbgi, left, right, mode);
2234 case EXPR_BINARY_SUB_ASSIGN:
2235 case EXPR_BINARY_SUB:
2236 return new_d_Sub(dbgi, left, right, mode);
2237 case EXPR_BINARY_MUL_ASSIGN:
2238 case EXPR_BINARY_MUL:
2239 return new_d_Mul(dbgi, left, right, mode);
2240 case EXPR_BINARY_BITWISE_AND:
2241 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2242 return new_d_And(dbgi, left, right, mode);
2243 case EXPR_BINARY_BITWISE_OR:
2244 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2245 return new_d_Or(dbgi, left, right, mode);
2246 case EXPR_BINARY_BITWISE_XOR:
2247 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2248 return new_d_Eor(dbgi, left, right, mode);
2249 case EXPR_BINARY_SHIFTLEFT:
2250 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2251 return new_d_Shl(dbgi, left, right, mode);
2252 case EXPR_BINARY_SHIFTRIGHT:
2253 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2254 if (mode_is_signed(mode)) {
2255 return new_d_Shrs(dbgi, left, right, mode);
2257 return new_d_Shr(dbgi, left, right, mode);
2259 case EXPR_BINARY_DIV:
2260 case EXPR_BINARY_DIV_ASSIGN: {
2261 ir_node *pin = new_Pin(new_NoMem());
2264 if (mode_is_float(mode)) {
2265 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2266 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2268 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2269 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2273 case EXPR_BINARY_MOD:
2274 case EXPR_BINARY_MOD_ASSIGN: {
2275 ir_node *pin = new_Pin(new_NoMem());
2276 assert(!mode_is_float(mode));
2277 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2278 op_pin_state_floats);
2279 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2283 panic("unexpected expression kind");
2287 static ir_node *create_lazy_op(const binary_expression_t *expression)
2289 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2290 type_t *type = expression->base.type;
2291 ir_mode *mode = get_ir_mode(type);
2293 if (is_constant_expression(expression->left)) {
2294 long val = fold_constant(expression->left);
2295 expression_kind_t ekind = expression->base.kind;
2296 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2297 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
2298 (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2299 return expression_to_firm(expression->right);
2301 return new_Const(mode, get_mode_one(mode));
2305 return produce_condition_result((const expression_t*) expression, dbgi);
2308 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2309 ir_node *right, ir_mode *mode);
2311 static ir_node *create_assign_binop(const binary_expression_t *expression)
2313 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2314 const expression_t *left_expr = expression->left;
2315 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2316 ir_node *left_addr = expression_to_addr(left_expr);
2317 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2318 ir_node *right = expression_to_firm(expression->right);
2319 ir_node *result = create_op(dbgi, expression, left, right);
2321 result = create_conv(dbgi, result, left_mode);
2322 result = do_strict_conv(dbgi, result);
2324 set_value_for_expression_addr(left_expr, result, left_addr);
2329 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2331 expression_kind_t kind = expression->base.kind;
2334 case EXPR_BINARY_EQUAL:
2335 case EXPR_BINARY_NOTEQUAL:
2336 case EXPR_BINARY_LESS:
2337 case EXPR_BINARY_LESSEQUAL:
2338 case EXPR_BINARY_GREATER:
2339 case EXPR_BINARY_GREATEREQUAL:
2340 case EXPR_BINARY_ISGREATER:
2341 case EXPR_BINARY_ISGREATEREQUAL:
2342 case EXPR_BINARY_ISLESS:
2343 case EXPR_BINARY_ISLESSEQUAL:
2344 case EXPR_BINARY_ISLESSGREATER:
2345 case EXPR_BINARY_ISUNORDERED: {
2346 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2347 ir_node *left = expression_to_firm(expression->left);
2348 ir_node *right = expression_to_firm(expression->right);
2349 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2350 long pnc = get_pnc(kind, expression->left->base.type);
2351 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2354 case EXPR_BINARY_ASSIGN: {
2355 ir_node *addr = expression_to_addr(expression->left);
2356 ir_node *right = expression_to_firm(expression->right);
2357 set_value_for_expression_addr(expression->left, right, addr);
2361 case EXPR_BINARY_ADD:
2362 case EXPR_BINARY_SUB:
2363 case EXPR_BINARY_MUL:
2364 case EXPR_BINARY_DIV:
2365 case EXPR_BINARY_MOD:
2366 case EXPR_BINARY_BITWISE_AND:
2367 case EXPR_BINARY_BITWISE_OR:
2368 case EXPR_BINARY_BITWISE_XOR:
2369 case EXPR_BINARY_SHIFTLEFT:
2370 case EXPR_BINARY_SHIFTRIGHT:
2372 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2373 ir_node *left = expression_to_firm(expression->left);
2374 ir_node *right = expression_to_firm(expression->right);
2375 return create_op(dbgi, expression, left, right);
2377 case EXPR_BINARY_LOGICAL_AND:
2378 case EXPR_BINARY_LOGICAL_OR:
2379 return create_lazy_op(expression);
2380 case EXPR_BINARY_COMMA:
2381 /* create side effects of left side */
2382 (void) expression_to_firm(expression->left);
2383 return _expression_to_firm(expression->right);
2385 case EXPR_BINARY_ADD_ASSIGN:
2386 case EXPR_BINARY_SUB_ASSIGN:
2387 case EXPR_BINARY_MUL_ASSIGN:
2388 case EXPR_BINARY_MOD_ASSIGN:
2389 case EXPR_BINARY_DIV_ASSIGN:
2390 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2391 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2392 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2393 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2394 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2395 return create_assign_binop(expression);
2396 case EXPR_BINARY_BUILTIN_EXPECT:
2397 return _expression_to_firm(expression->left);
2399 panic("TODO binexpr type");
2403 static ir_node *array_access_addr(const array_access_expression_t *expression)
2405 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2406 ir_node *base_addr = expression_to_firm(expression->array_ref);
2407 ir_node *offset = expression_to_firm(expression->index);
2409 type_t *offset_type = skip_typeref(expression->index->base.type);
2411 if (is_type_signed(offset_type)) {
2412 mode = get_ir_mode(type_ssize_t);
2414 mode = get_ir_mode(type_size_t);
2416 offset = create_conv(dbgi, offset, mode);
2418 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2419 assert(is_type_pointer(ref_type));
2420 pointer_type_t *pointer_type = &ref_type->pointer;
2422 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2423 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2424 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2426 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2431 static ir_node *array_access_to_firm(
2432 const array_access_expression_t *expression)
2434 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2435 ir_node *addr = array_access_addr(expression);
2436 type_t *type = revert_automatic_type_conversion(
2437 (const expression_t*) expression);
2438 type = skip_typeref(type);
2440 return deref_address(dbgi, type, addr);
2443 static long get_offsetof_offset(const offsetof_expression_t *expression)
2445 type_t *orig_type = expression->type;
2448 designator_t *designator = expression->designator;
2449 for( ; designator != NULL; designator = designator->next) {
2450 type_t *type = skip_typeref(orig_type);
2451 /* be sure the type is constructed */
2452 (void) get_ir_type(type);
2454 if (designator->symbol != NULL) {
2455 assert(is_type_compound(type));
2456 symbol_t *symbol = designator->symbol;
2458 declaration_t *declaration = type->compound.declaration;
2459 declaration_t *iter = declaration->scope.declarations;
2460 for( ; iter != NULL; iter = iter->next) {
2461 if (iter->symbol == symbol) {
2465 assert(iter != NULL);
2467 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2468 offset += get_entity_offset(iter->v.entity);
2470 orig_type = iter->type;
2472 expression_t *array_index = designator->array_index;
2473 assert(designator->array_index != NULL);
2474 assert(is_type_array(type));
2475 assert(is_type_valid(array_index->base.type));
2477 long index = fold_constant(array_index);
2478 ir_type *arr_type = get_ir_type(type);
2479 ir_type *elem_type = get_array_element_type(arr_type);
2480 long elem_size = get_type_size_bytes(elem_type);
2482 offset += index * elem_size;
2484 orig_type = type->array.element_type;
2491 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2493 ir_mode *mode = get_ir_mode(expression->base.type);
2494 long offset = get_offsetof_offset(expression);
2495 tarval *tv = new_tarval_from_long(offset, mode);
2496 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2498 return new_d_Const(dbgi, mode, tv);
2501 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2502 ir_entity *entity, type_t *type);
2504 static ir_node *compound_literal_to_firm(
2505 const compound_literal_expression_t *expression)
2507 type_t *type = expression->type;
2509 /* create an entity on the stack */
2510 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2512 ident *const id = id_unique("CompLit.%u");
2513 ir_type *const irtype = get_ir_type(type);
2514 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2515 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2516 set_entity_ld_ident(entity, id);
2518 set_entity_variability(entity, variability_uninitialized);
2520 /* create initialisation code */
2521 initializer_t *initializer = expression->initializer;
2522 create_local_initializer(initializer, dbgi, entity, type);
2524 /* create a sel for the compound literal address */
2525 ir_node *frame = get_local_frame(entity);
2526 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2531 * Transform a sizeof expression into Firm code.
2533 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2535 type_t *type = expression->type;
2537 type = expression->tp_expression->base.type;
2538 assert(type != NULL);
2541 type = skip_typeref(type);
2542 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2543 if (is_type_array(type) && type->array.is_vla
2544 && expression->tp_expression != NULL) {
2545 expression_to_firm(expression->tp_expression);
2548 return get_type_size(type);
2552 * Transform an alignof expression into Firm code.
2554 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2556 type_t *type = expression->type;
2558 /* beware: if expression is a variable reference, return the
2559 alignment of the variable. */
2560 const expression_t *tp_expression = expression->tp_expression;
2561 const declaration_t *declaration = expr_is_variable(tp_expression);
2562 if (declaration != NULL) {
2563 /* TODO: get the alignment of this variable. */
2565 type = tp_expression->base.type;
2566 assert(type != NULL);
2569 ir_mode *const mode = get_ir_mode(expression->base.type);
2570 symconst_symbol sym;
2571 sym.type_p = get_ir_type(type);
2572 return new_SymConst(mode, sym, symconst_type_align);
2575 static void init_ir_types(void);
2576 long fold_constant(const expression_t *expression)
2578 bool constant_folding_old = constant_folding;
2579 constant_folding = true;
2583 assert(is_constant_expression(expression));
2585 ir_graph *old_current_ir_graph = current_ir_graph;
2586 if (current_ir_graph == NULL) {
2587 current_ir_graph = get_const_code_irg();
2590 ir_node *cnst = expression_to_firm(expression);
2591 current_ir_graph = old_current_ir_graph;
2593 if (!is_Const(cnst)) {
2594 panic("couldn't fold constant\n");
2597 tarval *tv = get_Const_tarval(cnst);
2598 if (!tarval_is_long(tv)) {
2599 panic("result of constant folding is not integer\n");
2602 constant_folding = constant_folding_old;
2604 return get_tarval_long(tv);
2607 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2609 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2611 /* first try to fold a constant condition */
2612 if (is_constant_expression(expression->condition)) {
2613 long val = fold_constant(expression->condition);
2615 return expression_to_firm(expression->true_expression);
2617 return expression_to_firm(expression->false_expression);
2621 ir_node *cur_block = get_cur_block();
2623 /* create the true block */
2624 ir_node *true_block = new_immBlock();
2626 ir_node *true_val = expression_to_firm(expression->true_expression);
2627 ir_node *true_jmp = new_Jmp();
2629 /* create the false block */
2630 ir_node *false_block = new_immBlock();
2632 ir_node *false_val = expression_to_firm(expression->false_expression);
2633 ir_node *false_jmp = new_Jmp();
2635 /* create the condition evaluation */
2636 set_cur_block(cur_block);
2637 create_condition_evaluation(expression->condition, true_block, false_block);
2638 mature_immBlock(true_block);
2639 mature_immBlock(false_block);
2641 /* create the common block */
2642 ir_node *common_block = new_immBlock();
2643 add_immBlock_pred(common_block, true_jmp);
2644 add_immBlock_pred(common_block, false_jmp);
2645 mature_immBlock(common_block);
2647 /* TODO improve static semantics, so either both or no values are NULL */
2648 if (true_val == NULL || false_val == NULL)
2651 ir_node *in[2] = { true_val, false_val };
2652 ir_mode *mode = get_irn_mode(true_val);
2653 assert(get_irn_mode(false_val) == mode);
2654 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2660 * Returns an IR-node representing the address of a field.
2662 static ir_node *select_addr(const select_expression_t *expression)
2664 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2666 ir_node *compound_addr = expression_to_firm(expression->compound);
2668 /* make sure the type is constructed */
2669 type_t *type = skip_typeref(expression->compound->base.type);
2670 if (is_type_pointer(type)) {
2671 type = type->pointer.points_to;
2673 (void) get_ir_type(type);
2675 declaration_t *entry = expression->compound_entry;
2676 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2677 ir_entity *entity = entry->v.entity;
2679 assert(entity != NULL);
2681 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2686 static ir_node *select_to_firm(const select_expression_t *expression)
2688 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2689 ir_node *addr = select_addr(expression);
2690 type_t *type = revert_automatic_type_conversion(
2691 (const expression_t*) expression);
2692 type = skip_typeref(type);
2694 declaration_t *entry = expression->compound_entry;
2695 type_t *entry_type = skip_typeref(entry->type);
2697 if (entry_type->kind == TYPE_BITFIELD) {
2698 return bitfield_extract_to_firm(expression, addr);
2701 return deref_address(dbgi, type, addr);
2704 /* Values returned by __builtin_classify_type. */
2705 typedef enum gcc_type_class
2711 enumeral_type_class,
2714 reference_type_class,
2718 function_type_class,
2729 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2731 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2737 const atomic_type_t *const atomic_type = &type->atomic;
2738 switch (atomic_type->akind) {
2739 /* should not be reached */
2740 case ATOMIC_TYPE_INVALID:
2744 /* gcc cannot do that */
2745 case ATOMIC_TYPE_VOID:
2746 tc = void_type_class;
2749 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2750 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2751 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2752 case ATOMIC_TYPE_SHORT:
2753 case ATOMIC_TYPE_USHORT:
2754 case ATOMIC_TYPE_INT:
2755 case ATOMIC_TYPE_UINT:
2756 case ATOMIC_TYPE_LONG:
2757 case ATOMIC_TYPE_ULONG:
2758 case ATOMIC_TYPE_LONGLONG:
2759 case ATOMIC_TYPE_ULONGLONG:
2760 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2761 tc = integer_type_class;
2764 case ATOMIC_TYPE_FLOAT:
2765 case ATOMIC_TYPE_DOUBLE:
2766 case ATOMIC_TYPE_LONG_DOUBLE:
2767 tc = real_type_class;
2770 panic("Unexpected atomic type in classify_type_to_firm().");
2773 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2774 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2775 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2776 case TYPE_ARRAY: /* gcc handles this as pointer */
2777 case TYPE_FUNCTION: /* gcc handles this as pointer */
2778 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2779 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2780 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2782 /* gcc handles this as integer */
2783 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2786 /* typedef/typeof should be skipped already */
2793 panic("unexpected TYPE classify_type_to_firm().");
2796 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2797 ir_mode *const mode = mode_int;
2798 tarval *const tv = new_tarval_from_long(tc, mode);
2799 return new_d_Const(dbgi, mode, tv);
2802 static ir_node *function_name_to_firm(
2803 const funcname_expression_t *const expr)
2805 switch(expr->kind) {
2806 case FUNCNAME_FUNCTION:
2807 case FUNCNAME_PRETTY_FUNCTION:
2808 case FUNCNAME_FUNCDNAME:
2809 if (current_function_name == NULL) {
2810 const source_position_t *const src_pos = &expr->base.source_position;
2811 const char *const name = current_function_decl->symbol->string;
2812 const string_t string = { name, strlen(name) + 1 };
2813 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2815 return current_function_name;
2816 case FUNCNAME_FUNCSIG:
2817 if (current_funcsig == NULL) {
2818 const source_position_t *const src_pos = &expr->base.source_position;
2819 ir_entity *ent = get_irg_entity(current_ir_graph);
2820 const char *const name = get_entity_ld_name(ent);
2821 const string_t string = { name, strlen(name) + 1 };
2822 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2824 return current_funcsig;
2826 panic("Unsupported function name");
2829 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2831 statement_t *statement = expr->statement;
2833 assert(statement->kind == STATEMENT_COMPOUND);
2834 return compound_statement_to_firm(&statement->compound);
2837 static ir_node *va_start_expression_to_firm(
2838 const va_start_expression_t *const expr)
2840 ir_type *const method_type = get_ir_type(current_function_decl->type);
2841 int const n = get_method_n_params(method_type) - 1;
2842 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2843 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2844 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2845 ir_node *const no_mem = new_NoMem();
2846 ir_node *const arg_sel =
2847 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2849 ir_node *const cnst = get_type_size(expr->parameter->type);
2850 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2851 set_value_for_expression(expr->ap, add);
2856 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2858 type_t *const type = expr->base.type;
2859 expression_t *const ap_expr = expr->ap;
2860 ir_node *const ap_addr = expression_to_addr(ap_expr);
2861 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2862 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2863 ir_node *const res = deref_address(dbgi, type, ap);
2865 ir_node *const cnst = get_type_size(expr->base.type);
2866 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2868 set_value_for_expression_addr(ap_expr, add, ap_addr);
2873 static ir_node *dereference_addr(const unary_expression_t *const expression)
2875 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2876 return expression_to_firm(expression->value);
2880 * Returns a IR-node representing an lvalue of the given expression.
2882 static ir_node *expression_to_addr(const expression_t *expression)
2884 switch(expression->kind) {
2885 case EXPR_REFERENCE:
2886 return reference_addr(&expression->reference);
2887 case EXPR_ARRAY_ACCESS:
2888 return array_access_addr(&expression->array_access);
2890 return select_addr(&expression->select);
2892 return call_expression_to_firm(&expression->call);
2893 case EXPR_UNARY_DEREFERENCE: {
2894 return dereference_addr(&expression->unary);
2899 panic("trying to get address of non-lvalue");
2902 static ir_node *builtin_constant_to_firm(
2903 const builtin_constant_expression_t *expression)
2905 ir_mode *mode = get_ir_mode(expression->base.type);
2908 if (is_constant_expression(expression->value)) {
2913 return new_Const_long(mode, v);
2916 static ir_node *builtin_prefetch_to_firm(
2917 const builtin_prefetch_expression_t *expression)
2919 ir_node *adr = expression_to_firm(expression->adr);
2920 /* no Firm support for prefetch yet */
2926 * creates firm nodes for an expression. The difference between this function
2927 * and expression_to_firm is, that this version might produce mode_b nodes
2928 * instead of mode_Is.
2930 static ir_node *_expression_to_firm(const expression_t *expression)
2933 if (!constant_folding) {
2934 assert(!expression->base.transformed);
2935 ((expression_t*) expression)->base.transformed = true;
2939 switch(expression->kind) {
2940 case EXPR_CHARACTER_CONSTANT:
2941 return character_constant_to_firm(&expression->conste);
2942 case EXPR_WIDE_CHARACTER_CONSTANT:
2943 return wide_character_constant_to_firm(&expression->conste);
2945 return const_to_firm(&expression->conste);
2946 case EXPR_STRING_LITERAL:
2947 return string_literal_to_firm(&expression->string);
2948 case EXPR_WIDE_STRING_LITERAL:
2949 return wide_string_literal_to_firm(&expression->wide_string);
2950 case EXPR_REFERENCE:
2951 return reference_expression_to_firm(&expression->reference);
2953 return call_expression_to_firm(&expression->call);
2955 return unary_expression_to_firm(&expression->unary);
2957 return binary_expression_to_firm(&expression->binary);
2958 case EXPR_ARRAY_ACCESS:
2959 return array_access_to_firm(&expression->array_access);
2961 return sizeof_to_firm(&expression->typeprop);
2963 return alignof_to_firm(&expression->typeprop);
2964 case EXPR_CONDITIONAL:
2965 return conditional_to_firm(&expression->conditional);
2967 return select_to_firm(&expression->select);
2968 case EXPR_CLASSIFY_TYPE:
2969 return classify_type_to_firm(&expression->classify_type);
2971 return function_name_to_firm(&expression->funcname);
2972 case EXPR_STATEMENT:
2973 return statement_expression_to_firm(&expression->statement);
2975 return va_start_expression_to_firm(&expression->va_starte);
2977 return va_arg_expression_to_firm(&expression->va_arge);
2978 case EXPR_BUILTIN_SYMBOL:
2979 panic("unimplemented expression found");
2980 case EXPR_BUILTIN_CONSTANT_P:
2981 return builtin_constant_to_firm(&expression->builtin_constant);
2982 case EXPR_BUILTIN_PREFETCH:
2983 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
2985 return offsetof_to_firm(&expression->offsetofe);
2986 case EXPR_COMPOUND_LITERAL:
2987 return compound_literal_to_firm(&expression->compound_literal);
2993 panic("invalid expression found");
2996 static bool produces_mode_b(const expression_t *expression)
2998 switch (expression->kind) {
2999 case EXPR_BINARY_EQUAL:
3000 case EXPR_BINARY_NOTEQUAL:
3001 case EXPR_BINARY_LESS:
3002 case EXPR_BINARY_LESSEQUAL:
3003 case EXPR_BINARY_GREATER:
3004 case EXPR_BINARY_GREATEREQUAL:
3005 case EXPR_BINARY_ISGREATER:
3006 case EXPR_BINARY_ISGREATEREQUAL:
3007 case EXPR_BINARY_ISLESS:
3008 case EXPR_BINARY_ISLESSEQUAL:
3009 case EXPR_BINARY_ISLESSGREATER:
3010 case EXPR_BINARY_ISUNORDERED:
3011 case EXPR_UNARY_NOT:
3014 case EXPR_BINARY_BUILTIN_EXPECT:
3015 return produces_mode_b(expression->binary.left);
3016 case EXPR_BINARY_COMMA:
3017 return produces_mode_b(expression->binary.right);
3024 static ir_node *expression_to_firm(const expression_t *expression)
3026 if (!produces_mode_b(expression)) {
3027 ir_node *res = _expression_to_firm(expression);
3028 assert(res == NULL || get_irn_mode(res) != mode_b);
3032 if (is_constant_expression(expression)) {
3033 ir_node *res = _expression_to_firm(expression);
3034 ir_mode *mode = get_ir_mode(expression->base.type);
3035 assert(is_Const(res));
3036 if (is_Const_null(res)) {
3037 return new_Const_long(mode, 0);
3039 return new_Const_long(mode, 1);
3043 /* we have to produce a 0/1 from the mode_b expression */
3044 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3045 return produce_condition_result(expression, dbgi);
3048 static ir_node *expression_to_modeb(const expression_t *expression)
3050 ir_node *res = _expression_to_firm(expression);
3051 res = create_conv(NULL, res, mode_b);
3057 * create a short-circuit expression evaluation that tries to construct
3058 * efficient control flow structures for &&, || and ! expressions
3060 static void create_condition_evaluation(const expression_t *expression,
3061 ir_node *true_block,
3062 ir_node *false_block)
3064 switch(expression->kind) {
3065 case EXPR_UNARY_NOT: {
3066 const unary_expression_t *unary_expression = &expression->unary;
3067 create_condition_evaluation(unary_expression->value, false_block,
3071 case EXPR_BINARY_LOGICAL_AND: {
3072 const binary_expression_t *binary_expression = &expression->binary;
3074 ir_node *cur_block = get_cur_block();
3075 ir_node *extra_block = new_immBlock();
3076 set_cur_block(cur_block);
3077 create_condition_evaluation(binary_expression->left, extra_block,
3079 mature_immBlock(extra_block);
3080 set_cur_block(extra_block);
3081 create_condition_evaluation(binary_expression->right, true_block,
3085 case EXPR_BINARY_LOGICAL_OR: {
3086 const binary_expression_t *binary_expression = &expression->binary;
3088 ir_node *cur_block = get_cur_block();
3089 ir_node *extra_block = new_immBlock();
3090 set_cur_block(cur_block);
3091 create_condition_evaluation(binary_expression->left, true_block,
3093 mature_immBlock(extra_block);
3094 set_cur_block(extra_block);
3095 create_condition_evaluation(binary_expression->right, true_block,
3103 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3104 ir_node *condition = expression_to_modeb(expression);
3105 ir_node *cond = new_d_Cond(dbgi, condition);
3106 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3107 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3109 /* set branch prediction info based on __builtin_expect */
3110 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3111 long cnst = fold_constant(expression->binary.right);
3112 cond_jmp_predicate pred;
3115 pred = COND_JMP_PRED_FALSE;
3117 pred = COND_JMP_PRED_TRUE;
3119 set_Cond_jmp_pred(cond, pred);
3122 add_immBlock_pred(true_block, true_proj);
3123 if (false_block != NULL) {
3124 add_immBlock_pred(false_block, false_proj);
3127 set_cur_block(NULL);
3132 static void create_declaration_entity(declaration_t *declaration,
3133 declaration_kind_t declaration_kind,
3134 ir_type *parent_type)
3136 type_t *const type = skip_typeref(declaration->type);
3137 if (is_type_function(type)) {
3138 (void) get_function_entity(declaration);
3142 ident *const id = new_id_from_str(declaration->symbol->string);
3143 ir_type *const irtype = get_ir_type(type);
3144 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3145 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3147 declaration->declaration_kind = (unsigned char) declaration_kind;
3148 declaration->v.entity = entity;
3149 set_entity_variability(entity, variability_uninitialized);
3150 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3151 if (parent_type == get_tls_type())
3152 set_entity_allocation(entity, allocation_automatic);
3153 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3154 set_entity_allocation(entity, allocation_static);
3156 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3157 set_entity_volatility(entity, volatility_is_volatile);
3162 typedef struct type_path_entry_t type_path_entry_t;
3163 struct type_path_entry_t {
3165 ir_initializer_t *initializer;
3167 declaration_t *compound_entry;
3170 typedef struct type_path_t type_path_t;
3171 struct type_path_t {
3172 type_path_entry_t *path;
3177 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3179 size_t len = ARR_LEN(path->path);
3181 for(size_t i = 0; i < len; ++i) {
3182 const type_path_entry_t *entry = & path->path[i];
3184 type_t *type = skip_typeref(entry->type);
3185 if (is_type_compound(type)) {
3186 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3187 } else if (is_type_array(type)) {
3188 fprintf(stderr, "[%zd]", entry->index);
3190 fprintf(stderr, "-INVALID-");
3193 fprintf(stderr, " (");
3194 print_type(path->top_type);
3195 fprintf(stderr, ")");
3198 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3200 size_t len = ARR_LEN(path->path);
3202 return & path->path[len-1];
3205 static type_path_entry_t *append_to_type_path(type_path_t *path)
3207 size_t len = ARR_LEN(path->path);
3208 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3210 type_path_entry_t *result = & path->path[len];
3211 memset(result, 0, sizeof(result[0]));
3215 static size_t get_compound_size(const compound_type_t *type)
3217 declaration_t *declaration = type->declaration;
3218 declaration_t *member = declaration->scope.declarations;
3220 for( ; member != NULL; member = member->next) {
3223 /* TODO: cache results? */
3228 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3230 type_t *orig_top_type = path->top_type;
3231 type_t *top_type = skip_typeref(orig_top_type);
3233 assert(is_type_compound(top_type) || is_type_array(top_type));
3235 if (ARR_LEN(path->path) == 0) {
3238 type_path_entry_t *top = get_type_path_top(path);
3239 ir_initializer_t *initializer = top->initializer;
3240 return get_initializer_compound_value(initializer, top->index);
3244 static void descend_into_subtype(type_path_t *path)
3246 type_t *orig_top_type = path->top_type;
3247 type_t *top_type = skip_typeref(orig_top_type);
3249 assert(is_type_compound(top_type) || is_type_array(top_type));
3251 ir_initializer_t *initializer = get_initializer_entry(path);
3253 type_path_entry_t *top = append_to_type_path(path);
3254 top->type = top_type;
3258 if (is_type_compound(top_type)) {
3259 declaration_t *declaration = top_type->compound.declaration;
3260 declaration_t *entry = declaration->scope.declarations;
3262 top->compound_entry = entry;
3264 len = get_compound_size(&top_type->compound);
3266 path->top_type = entry->type;
3268 assert(is_type_array(top_type));
3269 assert(top_type->array.size > 0);
3272 path->top_type = top_type->array.element_type;
3273 len = top_type->array.size;
3275 if (initializer == NULL
3276 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3277 initializer = create_initializer_compound(len);
3278 /* we have to set the entry at the 2nd latest path entry... */
3279 size_t path_len = ARR_LEN(path->path);
3280 assert(path_len >= 1);
3282 type_path_entry_t *entry = & path->path[path_len-2];
3283 ir_initializer_t *tinitializer = entry->initializer;
3284 set_initializer_compound_value(tinitializer, entry->index,
3288 top->initializer = initializer;
3291 static void ascend_from_subtype(type_path_t *path)
3293 type_path_entry_t *top = get_type_path_top(path);
3295 path->top_type = top->type;
3297 size_t len = ARR_LEN(path->path);
3298 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3301 static void walk_designator(type_path_t *path, const designator_t *designator)
3303 /* designators start at current object type */
3304 ARR_RESIZE(type_path_entry_t, path->path, 1);
3306 for( ; designator != NULL; designator = designator->next) {
3307 type_path_entry_t *top = get_type_path_top(path);
3308 type_t *orig_type = top->type;
3309 type_t *type = skip_typeref(orig_type);
3311 if (designator->symbol != NULL) {
3312 assert(is_type_compound(type));
3314 symbol_t *symbol = designator->symbol;
3316 declaration_t *declaration = type->compound.declaration;
3317 declaration_t *iter = declaration->scope.declarations;
3318 for( ; iter != NULL; iter = iter->next, ++index) {
3319 if (iter->symbol == symbol) {
3323 assert(iter != NULL);
3325 top->type = orig_type;
3326 top->compound_entry = iter;
3328 orig_type = iter->type;
3330 expression_t *array_index = designator->array_index;
3331 assert(designator->array_index != NULL);
3332 assert(is_type_array(type));
3333 assert(is_type_valid(array_index->base.type));
3335 long index = fold_constant(array_index);
3338 if (type->array.size_constant == 1) {
3339 long array_size = type->array.size;
3340 assert(index < array_size);
3344 top->type = orig_type;
3345 top->index = (size_t) index;
3346 orig_type = type->array.element_type;
3348 path->top_type = orig_type;
3350 if (designator->next != NULL) {
3351 descend_into_subtype(path);
3355 path->invalid = false;
3358 static void advance_current_object(type_path_t *path)
3360 if (path->invalid) {
3361 /* TODO: handle this... */
3362 panic("invalid initializer in ast2firm (excessive elements)");
3366 type_path_entry_t *top = get_type_path_top(path);
3368 type_t *type = skip_typeref(top->type);
3369 if (is_type_union(type)) {
3370 top->compound_entry = NULL;
3371 } else if (is_type_struct(type)) {
3372 declaration_t *entry = top->compound_entry;
3375 entry = entry->next;
3376 top->compound_entry = entry;
3377 if (entry != NULL) {
3378 path->top_type = entry->type;
3382 assert(is_type_array(type));
3385 if (!type->array.size_constant || top->index < type->array.size) {
3390 /* we're past the last member of the current sub-aggregate, try if we
3391 * can ascend in the type hierarchy and continue with another subobject */
3392 size_t len = ARR_LEN(path->path);
3395 ascend_from_subtype(path);
3396 advance_current_object(path);
3398 path->invalid = true;
3403 static ir_initializer_t *create_ir_initializer(
3404 const initializer_t *initializer, type_t *type);
3406 static ir_initializer_t *create_ir_initializer_value(
3407 const initializer_value_t *initializer)
3409 if (is_type_compound(initializer->value->base.type)) {
3410 panic("initializer creation for compounds not implemented yet");
3412 ir_node *value = expression_to_firm(initializer->value);
3413 return create_initializer_const(value);
3416 static ir_initializer_t *create_ir_initializer_list(
3417 const initializer_list_t *initializer, type_t *type)
3420 memset(&path, 0, sizeof(path));
3421 path.top_type = type;
3422 path.path = NEW_ARR_F(type_path_entry_t, 0);
3424 descend_into_subtype(&path);
3426 for(size_t i = 0; i < initializer->len; ++i) {
3427 const initializer_t *sub_initializer = initializer->initializers[i];
3429 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3430 walk_designator(&path, sub_initializer->designator.designator);
3434 if (sub_initializer->kind == INITIALIZER_VALUE) {
3435 /* we might have to descend into types until we're at a scalar
3438 type_t *orig_top_type = path.top_type;
3439 type_t *top_type = skip_typeref(orig_top_type);
3441 if (is_type_scalar(top_type))
3443 descend_into_subtype(&path);
3447 ir_initializer_t *sub_irinitializer
3448 = create_ir_initializer(sub_initializer, path.top_type);
3450 size_t path_len = ARR_LEN(path.path);
3451 assert(path_len >= 1);
3452 type_path_entry_t *entry = & path.path[path_len-1];
3453 ir_initializer_t *tinitializer = entry->initializer;
3454 set_initializer_compound_value(tinitializer, entry->index,
3457 advance_current_object(&path);
3460 assert(ARR_LEN(path.path) >= 1);
3461 ir_initializer_t *result = path.path[0].initializer;
3462 DEL_ARR_F(path.path);
3467 static ir_initializer_t *create_ir_initializer_string(
3468 const initializer_string_t *initializer, type_t *type)
3470 type = skip_typeref(type);
3472 size_t string_len = initializer->string.size;
3473 assert(type->kind == TYPE_ARRAY);
3474 assert(type->array.size_constant);
3475 size_t len = type->array.size;
3476 ir_initializer_t *irinitializer = create_initializer_compound(len);
3478 const char *string = initializer->string.begin;
3479 ir_mode *mode = get_ir_mode(type->array.element_type);
3481 for(size_t i = 0; i < len; ++i) {
3486 tarval *tv = new_tarval_from_long(c, mode);
3487 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3489 set_initializer_compound_value(irinitializer, i, char_initializer);
3492 return irinitializer;
3495 static ir_initializer_t *create_ir_initializer_wide_string(
3496 const initializer_wide_string_t *initializer, type_t *type)
3498 size_t string_len = initializer->string.size;
3499 assert(type->kind == TYPE_ARRAY);
3500 assert(type->array.size_constant);
3501 size_t len = type->array.size;
3502 ir_initializer_t *irinitializer = create_initializer_compound(len);
3504 const wchar_rep_t *string = initializer->string.begin;
3505 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3507 for(size_t i = 0; i < len; ++i) {
3509 if (i < string_len) {
3512 tarval *tv = new_tarval_from_long(c, mode);
3513 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3515 set_initializer_compound_value(irinitializer, i, char_initializer);
3518 return irinitializer;
3521 static ir_initializer_t *create_ir_initializer(
3522 const initializer_t *initializer, type_t *type)
3524 switch(initializer->kind) {
3525 case INITIALIZER_STRING:
3526 return create_ir_initializer_string(&initializer->string, type);
3528 case INITIALIZER_WIDE_STRING:
3529 return create_ir_initializer_wide_string(&initializer->wide_string,
3532 case INITIALIZER_LIST:
3533 return create_ir_initializer_list(&initializer->list, type);
3535 case INITIALIZER_VALUE:
3536 return create_ir_initializer_value(&initializer->value);
3538 case INITIALIZER_DESIGNATOR:
3539 panic("unexpected designator initializer found");
3541 panic("unknown initializer");
3544 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3547 if (is_atomic_type(type)) {
3548 ir_mode *mode = get_type_mode(type);
3549 tarval *zero = get_mode_null(mode);
3550 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3552 /* TODO: bitfields */
3553 ir_node *mem = get_store();
3554 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3555 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3558 assert(is_compound_type(type));
3561 if (is_Array_type(type)) {
3562 assert(has_array_upper_bound(type, 0));
3563 n_members = get_array_upper_bound_int(type, 0);
3565 n_members = get_compound_n_members(type);
3568 for(int i = 0; i < n_members; ++i) {
3571 if (is_Array_type(type)) {
3572 ir_entity *entity = get_array_element_entity(type);
3573 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3574 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3575 ir_node *in[1] = { cnst };
3576 irtype = get_array_element_type(type);
3577 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3579 ir_entity *member = get_compound_member(type, i);
3581 irtype = get_entity_type(member);
3582 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3585 create_dynamic_null_initializer(irtype, dbgi, addr);
3590 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3591 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3593 switch(get_initializer_kind(initializer)) {
3594 case IR_INITIALIZER_NULL: {
3595 create_dynamic_null_initializer(type, dbgi, base_addr);
3598 case IR_INITIALIZER_CONST: {
3599 ir_node *node = get_initializer_const_value(initializer);
3600 ir_mode *mode = get_irn_mode(node);
3601 assert(get_type_mode(type) == mode);
3603 /* TODO: bitfields... */
3604 ir_node *mem = get_store();
3605 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3606 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3610 case IR_INITIALIZER_TARVAL: {
3611 tarval *tv = get_initializer_tarval_value(initializer);
3612 ir_mode *mode = get_tarval_mode(tv);
3613 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3614 assert(get_type_mode(type) == mode);
3616 /* TODO: bitfields... */
3617 ir_node *mem = get_store();
3618 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3619 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3623 case IR_INITIALIZER_COMPOUND: {
3624 assert(is_compound_type(type));
3626 if (is_Array_type(type)) {
3627 assert(has_array_upper_bound(type, 0));
3628 n_members = get_array_upper_bound_int(type, 0);
3630 n_members = get_compound_n_members(type);
3633 if (get_initializer_compound_n_entries(initializer)
3634 != (unsigned) n_members)
3635 panic("initializer doesn't match compound type");
3637 for(int i = 0; i < n_members; ++i) {
3640 if (is_Array_type(type)) {
3641 ir_entity *entity = get_array_element_entity(type);
3642 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3643 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3644 ir_node *in[1] = { cnst };
3645 irtype = get_array_element_type(type);
3646 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3648 ir_entity *member = get_compound_member(type, i);
3650 irtype = get_entity_type(member);
3651 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3654 ir_initializer_t *sub_init
3655 = get_initializer_compound_value(initializer, i);
3657 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3663 panic("invalid IR_INITIALIZER found");
3666 static void create_dynamic_initializer(ir_initializer_t *initializer,
3667 dbg_info *dbgi, ir_entity *entity)
3669 ir_node *frame = get_local_frame(entity);
3670 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3671 ir_type *type = get_entity_type(entity);
3673 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3676 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3677 ir_entity *entity, type_t *type)
3679 ir_node *memory = get_store();
3680 ir_node *nomem = new_NoMem();
3681 ir_node *frame = get_irg_frame(current_ir_graph);
3682 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3684 if (initializer->kind == INITIALIZER_VALUE) {
3685 initializer_value_t *initializer_value = &initializer->value;
3687 ir_node *value = expression_to_firm(initializer_value->value);
3688 type = skip_typeref(type);
3689 assign_value(dbgi, addr, type, value);
3693 if (!is_constant_initializer(initializer)) {
3694 ir_initializer_t *irinitializer
3695 = create_ir_initializer(initializer, type);
3697 create_dynamic_initializer(irinitializer, dbgi, entity);
3701 /* create the ir_initializer */
3702 ir_graph *const old_current_ir_graph = current_ir_graph;
3703 current_ir_graph = get_const_code_irg();
3705 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3707 assert(current_ir_graph == get_const_code_irg());
3708 current_ir_graph = old_current_ir_graph;
3710 /* create a "template" entity which is copied to the entity on the stack */
3711 ident *const id = id_unique("initializer.%u");
3712 ir_type *const irtype = get_ir_type(type);
3713 ir_type *const global_type = get_glob_type();
3714 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3715 set_entity_ld_ident(init_entity, id);
3717 set_entity_variability(init_entity, variability_initialized);
3718 set_entity_visibility(init_entity, visibility_local);
3719 set_entity_allocation(init_entity, allocation_static);
3721 set_entity_initializer(init_entity, irinitializer);
3723 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3724 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3726 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3727 set_store(copyb_mem);
3730 static void create_initializer_local_variable_entity(declaration_t *declaration)
3732 initializer_t *initializer = declaration->init.initializer;
3733 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3734 ir_entity *entity = declaration->v.entity;
3735 type_t *type = declaration->type;
3736 create_local_initializer(initializer, dbgi, entity, type);
3739 static void create_declaration_initializer(declaration_t *declaration)
3741 initializer_t *initializer = declaration->init.initializer;
3742 if (initializer == NULL)
3745 declaration_kind_t declaration_kind
3746 = (declaration_kind_t) declaration->declaration_kind;
3747 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3748 create_initializer_local_variable_entity(declaration);
3752 type_t *type = skip_typeref(declaration->type);
3754 if (initializer->kind == INITIALIZER_VALUE) {
3755 initializer_value_t *initializer_value = &initializer->value;
3757 = get_dbg_info(&declaration->source_position);
3759 ir_node *value = expression_to_firm(initializer_value->value);
3760 value = do_strict_conv(dbgi, value);
3762 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3763 set_value(declaration->v.value_number, value);
3765 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3767 ir_entity *entity = declaration->v.entity;
3769 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3770 set_entity_variability(entity, variability_constant);
3772 set_entity_variability(entity, variability_initialized);
3774 set_atomic_ent_value(entity, value);
3777 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY
3778 || declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3780 ir_entity *entity = declaration->v.entity;
3781 ir_initializer_t *irinitializer
3782 = create_ir_initializer(initializer, type);
3784 if (type->base.qualifiers & TYPE_QUALIFIER_CONST) {
3785 set_entity_variability(entity, variability_constant);
3787 set_entity_variability(entity, variability_initialized);
3789 set_entity_initializer(entity, irinitializer);
3793 static void create_variable_length_array(declaration_t *declaration)
3795 /* initializers are not allowed for VLAs */
3796 assert(declaration->init.initializer == NULL);
3798 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3799 declaration->v.vla_base = NULL;
3801 /* TODO: record VLA somewhere so we create the free node when we leave
3805 static void allocate_variable_length_array(declaration_t *declaration)
3807 /* initializers are not allowed for VLAs */
3808 assert(declaration->init.initializer == NULL);
3809 assert(get_cur_block() != NULL);
3811 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3812 type_t *type = declaration->type;
3813 ir_type *el_type = get_ir_type(type->array.element_type);
3815 /* make sure size_node is calculated */
3816 get_type_size(type);
3817 ir_node *elems = type->array.size_node;
3818 ir_node *mem = get_store();
3819 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3821 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3822 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3825 assert(declaration->declaration_kind
3826 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3827 declaration->v.vla_base = addr;
3831 * Creates a Firm local variable from a declaration.
3833 static void create_local_variable(declaration_t *declaration)
3835 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3837 bool needs_entity = declaration->address_taken;
3838 type_t *type = skip_typeref(declaration->type);
3840 /* is it a variable length array? */
3841 if (is_type_array(type) && !type->array.size_constant) {
3842 create_variable_length_array(declaration);
3844 } else if (is_type_array(type) || is_type_compound(type)) {
3845 needs_entity = true;
3846 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3847 needs_entity = true;
3851 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3852 create_declaration_entity(declaration,
3853 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3856 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3857 declaration->v.value_number = next_value_number_function;
3858 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3859 ++next_value_number_function;
3863 static void create_local_static_variable(declaration_t *declaration)
3865 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3867 type_t *const type = skip_typeref(declaration->type);
3868 ir_type *const global_type = get_glob_type();
3869 ir_type *const irtype = get_ir_type(type);
3870 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3872 size_t l = strlen(declaration->symbol->string);
3873 char buf[l + sizeof(".%u")];
3874 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3875 ident *const id = id_unique(buf);
3877 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3879 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3880 set_entity_volatility(entity, volatility_is_volatile);
3883 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3884 declaration->v.entity = entity;
3885 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3886 set_entity_variability(entity, variability_uninitialized);
3887 set_entity_visibility(entity, visibility_local);
3888 set_entity_allocation(entity, allocation_static);
3890 ir_graph *const old_current_ir_graph = current_ir_graph;
3891 current_ir_graph = get_const_code_irg();
3893 create_declaration_initializer(declaration);
3895 assert(current_ir_graph == get_const_code_irg());
3896 current_ir_graph = old_current_ir_graph;
3901 static void return_statement_to_firm(return_statement_t *statement)
3903 if (get_cur_block() == NULL)
3906 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
3907 ir_type *func_irtype = get_ir_type(current_function_decl->type);
3912 if (get_method_n_ress(func_irtype) > 0) {
3913 ir_type *res_type = get_method_res_type(func_irtype, 0);
3915 if (statement->value != NULL) {
3916 ir_node *node = expression_to_firm(statement->value);
3917 node = do_strict_conv(dbgi, node);
3921 if (is_compound_type(res_type)) {
3924 mode = get_type_mode(res_type);
3926 in[0] = new_Unknown(mode);
3930 /* build return_value for its side effects */
3931 if (statement->value != NULL) {
3932 expression_to_firm(statement->value);
3937 ir_node *store = get_store();
3938 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
3940 ir_node *end_block = get_irg_end_block(current_ir_graph);
3941 add_immBlock_pred(end_block, ret);
3943 set_cur_block(NULL);
3946 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
3948 if (get_cur_block() == NULL)
3951 return expression_to_firm(statement->expression);
3954 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
3956 declaration_t *declaration = compound->scope.declarations;
3957 for( ; declaration != NULL; declaration = declaration->next) {
3958 create_local_declaration(declaration);
3961 ir_node *result = NULL;
3962 statement_t *statement = compound->statements;
3963 for( ; statement != NULL; statement = statement->base.next) {
3964 if (statement->base.next == NULL
3965 && statement->kind == STATEMENT_EXPRESSION) {
3966 result = expression_statement_to_firm(
3967 &statement->expression);
3970 statement_to_firm(statement);
3976 static void create_global_variable(declaration_t *declaration)
3980 switch ((storage_class_tag_t)declaration->storage_class) {
3981 case STORAGE_CLASS_STATIC:
3982 vis = visibility_local;
3985 case STORAGE_CLASS_EXTERN:
3986 vis = visibility_external_allocated;
3989 case STORAGE_CLASS_NONE:
3990 vis = visibility_external_visible;
3993 case STORAGE_CLASS_THREAD:
3994 vis = visibility_external_visible;
3997 case STORAGE_CLASS_THREAD_EXTERN:
3998 vis = visibility_external_allocated;
4001 case STORAGE_CLASS_THREAD_STATIC:
4002 vis = visibility_local;
4006 var_type = get_tls_type();
4010 var_type = get_glob_type();
4014 create_declaration_entity(declaration,
4015 DECLARATION_KIND_GLOBAL_VARIABLE,
4017 if (!is_type_function(skip_typeref(declaration->type))) {
4018 set_entity_visibility(declaration->v.entity, vis);
4023 case STORAGE_CLASS_TYPEDEF:
4024 case STORAGE_CLASS_AUTO:
4025 case STORAGE_CLASS_REGISTER:
4026 case STORAGE_CLASS_ENUM_ENTRY:
4029 panic("Invalid storage class for global variable");
4032 static void create_local_declaration(declaration_t *declaration)
4034 if (declaration->namespc != NAMESPACE_NORMAL)
4036 /* construct type */
4037 (void) get_ir_type(declaration->type);
4038 if (declaration->symbol == NULL) {
4042 type_t *type = skip_typeref(declaration->type);
4044 switch ((storage_class_tag_t) declaration->storage_class) {
4045 case STORAGE_CLASS_STATIC:
4046 create_local_static_variable(declaration);
4048 case STORAGE_CLASS_EXTERN:
4049 create_global_variable(declaration);
4050 create_declaration_initializer(declaration);
4052 case STORAGE_CLASS_NONE:
4053 case STORAGE_CLASS_AUTO:
4054 case STORAGE_CLASS_REGISTER:
4055 if (is_type_function(type)) {
4056 if (declaration->init.statement != NULL) {
4057 panic("nested functions not supported yet");
4059 get_function_entity(declaration);
4062 create_local_variable(declaration);
4065 case STORAGE_CLASS_ENUM_ENTRY:
4066 /* should already be handled */
4067 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4069 case STORAGE_CLASS_TYPEDEF:
4070 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4072 case STORAGE_CLASS_THREAD:
4073 case STORAGE_CLASS_THREAD_EXTERN:
4074 case STORAGE_CLASS_THREAD_STATIC:
4077 panic("invalid storage class found");
4080 static void initialize_local_declaration(declaration_t *declaration)
4082 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4085 switch ((declaration_kind_t) declaration->declaration_kind) {
4086 case DECLARATION_KIND_LOCAL_VARIABLE:
4087 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4088 create_declaration_initializer(declaration);
4091 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4092 allocate_variable_length_array(declaration);
4095 case DECLARATION_KIND_LABEL_BLOCK:
4096 case DECLARATION_KIND_COMPOUND_MEMBER:
4097 case DECLARATION_KIND_GLOBAL_VARIABLE:
4098 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4099 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4100 case DECLARATION_KIND_FUNCTION:
4101 case DECLARATION_KIND_TYPE:
4102 case DECLARATION_KIND_ENUM_ENTRY:
4105 case DECLARATION_KIND_UNKNOWN:
4106 panic("can't initialize unknwon declaration");
4108 panic("invalid declaration kind");
4111 static void declaration_statement_to_firm(declaration_statement_t *statement)
4113 declaration_t *declaration = statement->declarations_begin;
4114 declaration_t *end = statement->declarations_end->next;
4115 for( ; declaration != end; declaration = declaration->next) {
4116 if (declaration->namespc != NAMESPACE_NORMAL)
4118 initialize_local_declaration(declaration);
4122 static void if_statement_to_firm(if_statement_t *statement)
4124 ir_node *cur_block = get_cur_block();
4126 ir_node *fallthrough_block = NULL;
4128 /* the true (blocks) */
4129 ir_node *true_block = NULL;
4130 if (statement->true_statement != NULL) {
4131 true_block = new_immBlock();
4132 statement_to_firm(statement->true_statement);
4133 if (get_cur_block() != NULL) {
4134 ir_node *jmp = new_Jmp();
4135 if (fallthrough_block == NULL)
4136 fallthrough_block = new_immBlock();
4137 add_immBlock_pred(fallthrough_block, jmp);
4141 /* the false (blocks) */
4142 ir_node *false_block = NULL;
4143 if (statement->false_statement != NULL) {
4144 false_block = new_immBlock();
4146 statement_to_firm(statement->false_statement);
4147 if (get_cur_block() != NULL) {
4148 ir_node *jmp = new_Jmp();
4149 if (fallthrough_block == NULL)
4150 fallthrough_block = new_immBlock();
4151 add_immBlock_pred(fallthrough_block, jmp);
4155 /* create the condition */
4156 if (cur_block != NULL) {
4157 if (true_block == NULL || false_block == NULL) {
4158 if (fallthrough_block == NULL)
4159 fallthrough_block = new_immBlock();
4160 if (true_block == NULL)
4161 true_block = fallthrough_block;
4162 if (false_block == NULL)
4163 false_block = fallthrough_block;
4166 set_cur_block(cur_block);
4167 create_condition_evaluation(statement->condition, true_block,
4171 mature_immBlock(true_block);
4172 if (false_block != fallthrough_block) {
4173 mature_immBlock(false_block);
4175 if (fallthrough_block != NULL) {
4176 mature_immBlock(fallthrough_block);
4179 set_cur_block(fallthrough_block);
4182 static void while_statement_to_firm(while_statement_t *statement)
4184 ir_node *jmp = NULL;
4185 if (get_cur_block() != NULL) {
4189 /* create the header block */
4190 ir_node *header_block = new_immBlock();
4192 add_immBlock_pred(header_block, jmp);
4196 ir_node *old_continue_label = continue_label;
4197 ir_node *old_break_label = break_label;
4198 continue_label = header_block;
4201 ir_node *body_block = new_immBlock();
4202 statement_to_firm(statement->body);
4203 ir_node *false_block = break_label;
4205 assert(continue_label == header_block);
4206 continue_label = old_continue_label;
4207 break_label = old_break_label;
4209 if (get_cur_block() != NULL) {
4211 add_immBlock_pred(header_block, jmp);
4214 /* shortcut for while(true) */
4215 if (is_constant_expression(statement->condition)
4216 && fold_constant(statement->condition) != 0) {
4217 set_cur_block(header_block);
4218 ir_node *header_jmp = new_Jmp();
4219 add_immBlock_pred(body_block, header_jmp);
4221 keep_alive(body_block);
4223 if (false_block == NULL) {
4224 false_block = new_immBlock();
4227 /* create the condition */
4228 set_cur_block(header_block);
4230 create_condition_evaluation(statement->condition, body_block,
4234 mature_immBlock(body_block);
4235 mature_immBlock(header_block);
4236 if (false_block != NULL) {
4237 mature_immBlock(false_block);
4240 set_cur_block(false_block);
4243 static void do_while_statement_to_firm(do_while_statement_t *statement)
4245 ir_node *jmp = NULL;
4246 if (get_cur_block() != NULL) {
4250 /* create the header block */
4251 ir_node *header_block = new_immBlock();
4254 ir_node *body_block = new_immBlock();
4256 add_immBlock_pred(body_block, jmp);
4259 ir_node *old_continue_label = continue_label;
4260 ir_node *old_break_label = break_label;
4261 continue_label = header_block;
4264 statement_to_firm(statement->body);
4265 ir_node *false_block = break_label;
4267 assert(continue_label == header_block);
4268 continue_label = old_continue_label;
4269 break_label = old_break_label;
4271 if (get_cur_block() != NULL) {
4272 ir_node *body_jmp = new_Jmp();
4273 add_immBlock_pred(header_block, body_jmp);
4274 mature_immBlock(header_block);
4277 if (false_block == NULL) {
4278 false_block = new_immBlock();
4281 /* create the condition */
4282 set_cur_block(header_block);
4284 create_condition_evaluation(statement->condition, body_block, false_block);
4285 mature_immBlock(body_block);
4286 mature_immBlock(header_block);
4287 if (false_block != NULL) {
4288 mature_immBlock(false_block);
4291 set_cur_block(false_block);
4294 static void for_statement_to_firm(for_statement_t *statement)
4296 ir_node *jmp = NULL;
4298 /* create declarations */
4299 declaration_t *declaration = statement->scope.declarations;
4300 for( ; declaration != NULL; declaration = declaration->next) {
4301 create_local_declaration(declaration);
4303 declaration = statement->scope.declarations;
4304 for( ; declaration != NULL; declaration = declaration->next) {
4305 initialize_local_declaration(declaration);
4308 if (get_cur_block() != NULL) {
4309 if (statement->initialisation != NULL) {
4310 expression_to_firm(statement->initialisation);
4317 /* create the step block */
4318 ir_node *const step_block = new_immBlock();
4319 if (statement->step != NULL) {
4320 expression_to_firm(statement->step);
4322 ir_node *const step_jmp = new_Jmp();
4324 /* create the header block */
4325 ir_node *const header_block = new_immBlock();
4327 add_immBlock_pred(header_block, jmp);
4329 add_immBlock_pred(header_block, step_jmp);
4331 /* the false block */
4332 ir_node *const false_block = new_immBlock();
4335 ir_node * body_block;
4336 if (statement->body != NULL) {
4337 ir_node *const old_continue_label = continue_label;
4338 ir_node *const old_break_label = break_label;
4339 continue_label = step_block;
4340 break_label = false_block;
4342 body_block = new_immBlock();
4343 statement_to_firm(statement->body);
4345 assert(continue_label == step_block);
4346 assert(break_label == false_block);
4347 continue_label = old_continue_label;
4348 break_label = old_break_label;
4350 if (get_cur_block() != NULL) {
4352 add_immBlock_pred(step_block, jmp);
4355 body_block = step_block;
4358 /* create the condition */
4359 set_cur_block(header_block);
4360 if (statement->condition != NULL) {
4361 create_condition_evaluation(statement->condition, body_block,
4364 keep_alive(header_block);
4366 add_immBlock_pred(body_block, jmp);
4369 mature_immBlock(body_block);
4370 mature_immBlock(false_block);
4371 mature_immBlock(step_block);
4372 mature_immBlock(header_block);
4373 mature_immBlock(false_block);
4375 set_cur_block(false_block);
4378 static void create_jump_statement(const statement_t *statement,
4379 ir_node *target_block)
4381 if (get_cur_block() == NULL)
4384 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4385 ir_node *jump = new_d_Jmp(dbgi);
4386 add_immBlock_pred(target_block, jump);
4388 set_cur_block(NULL);
4391 static ir_node *get_break_label(void)
4393 if (break_label == NULL) {
4394 ir_node *cur_block = get_cur_block();
4395 break_label = new_immBlock();
4396 set_cur_block(cur_block);
4401 static void switch_statement_to_firm(const switch_statement_t *statement)
4403 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4405 ir_node *expression = expression_to_firm(statement->expression);
4406 ir_node *cond = new_d_Cond(dbgi, expression);
4408 set_cur_block(NULL);
4410 ir_node *const old_switch_cond = current_switch_cond;
4411 ir_node *const old_break_label = break_label;
4412 const bool old_saw_default_label = saw_default_label;
4413 saw_default_label = false;
4414 current_switch_cond = cond;
4417 if (statement->body != NULL) {
4418 statement_to_firm(statement->body);
4421 if (get_cur_block() != NULL) {
4422 ir_node *jmp = new_Jmp();
4423 add_immBlock_pred(get_break_label(), jmp);
4426 if (!saw_default_label) {
4427 set_cur_block(get_nodes_block(cond));
4428 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4429 MAGIC_DEFAULT_PN_NUMBER);
4430 add_immBlock_pred(get_break_label(), proj);
4433 if (break_label != NULL) {
4434 mature_immBlock(break_label);
4436 set_cur_block(break_label);
4438 assert(current_switch_cond == cond);
4439 current_switch_cond = old_switch_cond;
4440 break_label = old_break_label;
4441 saw_default_label = old_saw_default_label;
4444 static void case_label_to_firm(const case_label_statement_t *statement)
4446 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4448 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4450 /* let's create a node and hope firm constant folding creates a Const
4453 ir_node *old_block = get_nodes_block(current_switch_cond);
4454 ir_node *block = new_immBlock();
4456 set_cur_block(old_block);
4457 if (statement->expression != NULL) {
4458 long start_pn = fold_constant(statement->expression);
4459 long end_pn = start_pn;
4460 if (statement->end_range != NULL) {
4461 end_pn = fold_constant(statement->end_range);
4463 assert(start_pn <= end_pn);
4464 /* create jumps for all cases in the given range */
4465 for (long pn = start_pn; pn <= end_pn; ++pn) {
4466 if (pn == MAGIC_DEFAULT_PN_NUMBER) {
4467 /* oops someone detected our cheating... */
4468 panic("magic default pn used");
4470 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4471 add_immBlock_pred(block, proj);
4474 saw_default_label = true;
4475 proj = new_d_defaultProj(dbgi, current_switch_cond,
4476 MAGIC_DEFAULT_PN_NUMBER);
4478 add_immBlock_pred(block, proj);
4481 if (fallthrough != NULL) {
4482 add_immBlock_pred(block, fallthrough);
4484 mature_immBlock(block);
4485 set_cur_block(block);
4487 if (statement->statement != NULL) {
4488 statement_to_firm(statement->statement);
4492 static ir_node *get_label_block(declaration_t *label)
4494 assert(label->namespc == NAMESPACE_LABEL);
4496 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
4497 return label->v.block;
4499 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
4501 ir_node *old_cur_block = get_cur_block();
4502 ir_node *block = new_immBlock();
4503 set_cur_block(old_cur_block);
4505 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
4506 label->v.block = block;
4508 ARR_APP1(ir_node *, imature_blocks, block);
4513 static void label_to_firm(const label_statement_t *statement)
4515 ir_node *block = get_label_block(statement->label);
4517 if (get_cur_block() != NULL) {
4518 ir_node *jmp = new_Jmp();
4519 add_immBlock_pred(block, jmp);
4522 set_cur_block(block);
4525 if (statement->statement != NULL) {
4526 statement_to_firm(statement->statement);
4530 static void goto_to_firm(const goto_statement_t *statement)
4532 if (get_cur_block() == NULL)
4535 ir_node *block = get_label_block(statement->label);
4536 ir_node *jmp = new_Jmp();
4537 add_immBlock_pred(block, jmp);
4539 set_cur_block(NULL);
4542 static void asm_statement_to_firm(const asm_statement_t *statement)
4544 bool needs_memory = false;
4546 if (statement->is_volatile) {
4547 needs_memory = true;
4550 size_t n_clobbers = 0;
4551 asm_clobber_t *clobber = statement->clobbers;
4552 for( ; clobber != NULL; clobber = clobber->next) {
4553 const char *clobber_str = clobber->clobber.begin;
4555 if (!be_is_valid_clobber(clobber_str)) {
4556 errorf(&statement->base.source_position,
4557 "invalid clobber '%s' specified", clobber->clobber);
4561 if (strcmp(clobber_str, "memory") == 0) {
4562 needs_memory = true;
4566 ident *id = new_id_from_str(clobber_str);
4567 obstack_ptr_grow(&asm_obst, id);
4570 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4571 ident **clobbers = NULL;
4572 if (n_clobbers > 0) {
4573 clobbers = obstack_finish(&asm_obst);
4576 size_t n_inputs = 0;
4577 asm_argument_t *argument = statement->inputs;
4578 for ( ; argument != NULL; argument = argument->next)
4580 size_t n_outputs = 0;
4581 argument = statement->outputs;
4582 for ( ; argument != NULL; argument = argument->next)
4585 unsigned next_pos = 0;
4587 ir_node *ins[n_inputs + n_outputs + 1];
4590 ir_asm_constraint tmp_in_constraints[n_outputs];
4592 const expression_t *out_exprs[n_outputs];
4593 ir_node *out_addrs[n_outputs];
4594 size_t out_size = 0;
4596 argument = statement->outputs;
4597 for ( ; argument != NULL; argument = argument->next) {
4598 const char *constraints = argument->constraints.begin;
4599 asm_constraint_flags_t asm_flags
4600 = be_parse_asm_constraints(constraints);
4602 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4603 errorf(&statement->base.source_position,
4604 "some constraints in '%s' are not supported", constraints);
4607 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4608 errorf(&statement->base.source_position,
4609 "some constraints in '%s' are invalid", constraints);
4612 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4613 errorf(&statement->base.source_position,
4614 "no write flag specified for output constraints '%s'",
4619 unsigned pos = next_pos++;
4620 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4621 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4622 expression_t *expr = argument->expression;
4623 ir_node *addr = expression_to_addr(expr);
4624 /* in+output, construct an artifical same_as constraint on the
4626 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4628 ir_node *value = get_value_from_lvalue(expr, addr);
4630 snprintf(buf, sizeof(buf), "%d", pos);
4632 ir_asm_constraint constraint;
4633 constraint.pos = pos;
4634 constraint.constraint = new_id_from_str(buf);
4635 constraint.mode = get_ir_mode(expr->base.type);
4636 tmp_in_constraints[in_size] = constraint;
4637 ins[in_size] = value;
4642 out_exprs[out_size] = expr;
4643 out_addrs[out_size] = addr;
4645 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4646 /* pure memory ops need no input (but we have to make sure we
4647 * attach to the memory) */
4648 assert(! (asm_flags &
4649 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4650 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4651 needs_memory = true;
4653 /* we need to attach the address to the inputs */
4654 expression_t *expr = argument->expression;
4656 ir_asm_constraint constraint;
4657 constraint.pos = pos;
4658 constraint.constraint = new_id_from_str(constraints);
4659 constraint.mode = NULL;
4660 tmp_in_constraints[in_size] = constraint;
4662 ins[in_size] = expression_to_addr(expr);
4666 errorf(&statement->base.source_position,
4667 "only modifiers but no place set in constraints '%s'",
4672 ir_asm_constraint constraint;
4673 constraint.pos = pos;
4674 constraint.constraint = new_id_from_str(constraints);
4675 constraint.mode = get_ir_mode(argument->expression->base.type);
4677 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4679 assert(obstack_object_size(&asm_obst)
4680 == out_size * sizeof(ir_asm_constraint));
4681 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4684 obstack_grow(&asm_obst, tmp_in_constraints,
4685 in_size * sizeof(tmp_in_constraints[0]));
4686 /* find and count input and output arguments */
4687 argument = statement->inputs;
4688 for( ; argument != NULL; argument = argument->next) {
4689 const char *constraints = argument->constraints.begin;
4690 asm_constraint_flags_t asm_flags
4691 = be_parse_asm_constraints(constraints);
4693 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4694 errorf(&statement->base.source_position,
4695 "some constraints in '%s' are not supported", constraints);
4698 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4699 errorf(&statement->base.source_position,
4700 "some constraints in '%s' are invalid", constraints);
4703 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4704 errorf(&statement->base.source_position,
4705 "write flag specified for input constraints '%s'",
4711 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4712 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4713 /* we can treat this as "normal" input */
4714 input = expression_to_firm(argument->expression);
4715 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4716 /* pure memory ops need no input (but we have to make sure we
4717 * attach to the memory) */
4718 assert(! (asm_flags &
4719 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4720 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4721 needs_memory = true;
4722 input = expression_to_addr(argument->expression);
4724 errorf(&statement->base.source_position,
4725 "only modifiers but no place set in constraints '%s'",
4730 ir_asm_constraint constraint;
4731 constraint.pos = next_pos++;
4732 constraint.constraint = new_id_from_str(constraints);
4733 constraint.mode = get_irn_mode(input);
4735 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4736 ins[in_size++] = input;
4740 ir_asm_constraint constraint;
4741 constraint.pos = next_pos++;
4742 constraint.constraint = new_id_from_str("");
4743 constraint.mode = mode_M;
4745 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4746 ins[in_size++] = get_store();
4749 assert(obstack_object_size(&asm_obst)
4750 == in_size * sizeof(ir_asm_constraint));
4751 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4753 /* create asm node */
4754 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4756 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4758 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4759 out_size, output_constraints,
4760 n_clobbers, clobbers, asm_text);
4762 if (statement->is_volatile) {
4763 set_irn_pinned(node, op_pin_state_pinned);
4765 set_irn_pinned(node, op_pin_state_floats);
4768 /* create output projs & connect them */
4770 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4775 for (i = 0; i < out_size; ++i) {
4776 const expression_t *out_expr = out_exprs[i];
4778 ir_mode *mode = get_ir_mode(out_expr->base.type);
4779 ir_node *proj = new_Proj(node, mode, pn);
4780 ir_node *addr = out_addrs[i];
4782 set_value_for_expression_addr(out_expr, proj, addr);
4786 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4787 statement_to_firm(statement->try_statement);
4788 warningf(&statement->base.source_position, "structured exception handling ignored");
4791 static void leave_statement_to_firm(leave_statement_t *statement) {
4792 errorf(&statement->base.source_position, "__leave not supported yet");
4796 * Transform a statement.
4798 static void statement_to_firm(statement_t *statement)
4801 assert(!statement->base.transformed);
4802 statement->base.transformed = true;
4805 switch(statement->kind) {
4806 case STATEMENT_INVALID:
4807 panic("invalid statement found");
4809 case STATEMENT_EMPTY:
4812 case STATEMENT_COMPOUND:
4813 compound_statement_to_firm(&statement->compound);
4815 case STATEMENT_RETURN:
4816 return_statement_to_firm(&statement->returns);
4818 case STATEMENT_EXPRESSION:
4819 expression_statement_to_firm(&statement->expression);
4822 if_statement_to_firm(&statement->ifs);
4824 case STATEMENT_WHILE:
4825 while_statement_to_firm(&statement->whiles);
4827 case STATEMENT_DO_WHILE:
4828 do_while_statement_to_firm(&statement->do_while);
4830 case STATEMENT_DECLARATION:
4831 declaration_statement_to_firm(&statement->declaration);
4833 case STATEMENT_BREAK:
4834 create_jump_statement(statement, get_break_label());
4836 case STATEMENT_CONTINUE:
4837 create_jump_statement(statement, continue_label);
4839 case STATEMENT_SWITCH:
4840 switch_statement_to_firm(&statement->switchs);
4842 case STATEMENT_CASE_LABEL:
4843 case_label_to_firm(&statement->case_label);
4846 for_statement_to_firm(&statement->fors);
4848 case STATEMENT_LABEL:
4849 label_to_firm(&statement->label);
4851 case STATEMENT_GOTO:
4852 goto_to_firm(&statement->gotos);
4855 asm_statement_to_firm(&statement->asms);
4857 case STATEMENT_MS_TRY:
4858 ms_try_statement_to_firm(&statement->ms_try);
4860 case STATEMENT_LEAVE:
4861 leave_statement_to_firm(&statement->leave);
4864 panic("Statement not implemented\n");
4867 static int count_decls_in_expression(const expression_t *expression);
4869 static int count_local_declarations(const declaration_t * decl,
4870 const declaration_t *const end)
4873 for (; decl != end; decl = decl->next) {
4874 if (decl->namespc != NAMESPACE_NORMAL)
4876 type_t *type = skip_typeref(decl->type);
4878 if (!decl->address_taken && is_type_scalar(type))
4880 const initializer_t *initializer = decl->init.initializer;
4881 /* FIXME: should walk initializer hierarchies... */
4882 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
4883 count += count_decls_in_expression(initializer->value.value);
4889 static int count_decls_in_expression(const expression_t *expression) {
4892 if (expression == NULL)
4895 switch((expression_kind_t) expression->base.kind) {
4896 case EXPR_STATEMENT:
4897 return count_decls_in_stmts(expression->statement.statement);
4899 int count_left = count_decls_in_expression(expression->binary.left);
4900 int count_right = count_decls_in_expression(expression->binary.right);
4901 return count_left + count_right;
4904 return count_decls_in_expression(expression->unary.value);
4906 call_argument_t *argument = expression->call.arguments;
4907 for( ; argument != NULL; argument = argument->next) {
4908 count += count_decls_in_expression(argument->expression);
4915 panic("unexpected expression kind");
4917 case EXPR_COMPOUND_LITERAL:
4921 case EXPR_CONDITIONAL:
4922 count += count_decls_in_expression(expression->conditional.condition);
4923 count += count_decls_in_expression(expression->conditional.true_expression);
4924 count += count_decls_in_expression(expression->conditional.false_expression);
4927 case EXPR_BUILTIN_PREFETCH:
4928 count += count_decls_in_expression(expression->builtin_prefetch.adr);
4929 count += count_decls_in_expression(expression->builtin_prefetch.rw);
4930 count += count_decls_in_expression(expression->builtin_prefetch.locality);
4933 case EXPR_BUILTIN_CONSTANT_P:
4934 count += count_decls_in_expression(expression->builtin_constant.value);
4938 count += count_decls_in_expression(expression->select.compound);
4941 case EXPR_ARRAY_ACCESS:
4942 count += count_decls_in_expression(expression->array_access.array_ref);
4943 count += count_decls_in_expression(expression->array_access.index);
4946 case EXPR_CLASSIFY_TYPE:
4947 count += count_decls_in_expression(expression->classify_type.type_expression);
4951 case EXPR_ALIGNOF: {
4952 expression_t *tp_expression = expression->typeprop.tp_expression;
4953 if (tp_expression != NULL) {
4954 count += count_decls_in_expression(tp_expression);
4960 case EXPR_REFERENCE:
4962 case EXPR_CHARACTER_CONSTANT:
4963 case EXPR_WIDE_CHARACTER_CONSTANT:
4964 case EXPR_STRING_LITERAL:
4965 case EXPR_WIDE_STRING_LITERAL:
4967 case EXPR_BUILTIN_SYMBOL:
4973 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
4974 * (or implement all the missing expressions here/implement a walker)
4980 static int count_decls_in_stmts(const statement_t *stmt)
4983 for (; stmt != NULL; stmt = stmt->base.next) {
4984 switch (stmt->kind) {
4985 case STATEMENT_EMPTY:
4988 case STATEMENT_DECLARATION: {
4989 const declaration_statement_t *const decl_stmt = &stmt->declaration;
4990 count += count_local_declarations(decl_stmt->declarations_begin,
4991 decl_stmt->declarations_end->next);
4995 case STATEMENT_COMPOUND: {
4996 const compound_statement_t *const comp =
4998 count += count_decls_in_stmts(comp->statements);
5002 case STATEMENT_IF: {
5003 const if_statement_t *const if_stmt = &stmt->ifs;
5004 count += count_decls_in_expression(if_stmt->condition);
5005 count += count_decls_in_stmts(if_stmt->true_statement);
5006 count += count_decls_in_stmts(if_stmt->false_statement);
5010 case STATEMENT_SWITCH: {
5011 const switch_statement_t *const switch_stmt = &stmt->switchs;
5012 count += count_decls_in_expression(switch_stmt->expression);
5013 count += count_decls_in_stmts(switch_stmt->body);
5017 case STATEMENT_LABEL: {
5018 const label_statement_t *const label_stmt = &stmt->label;
5019 if (label_stmt->statement != NULL) {
5020 count += count_decls_in_stmts(label_stmt->statement);
5025 case STATEMENT_WHILE: {
5026 const while_statement_t *const while_stmt = &stmt->whiles;
5027 count += count_decls_in_expression(while_stmt->condition);
5028 count += count_decls_in_stmts(while_stmt->body);
5032 case STATEMENT_DO_WHILE: {
5033 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5034 count += count_decls_in_expression(do_while_stmt->condition);
5035 count += count_decls_in_stmts(do_while_stmt->body);
5039 case STATEMENT_FOR: {
5040 const for_statement_t *const for_stmt = &stmt->fors;
5041 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5042 count += count_decls_in_expression(for_stmt->initialisation);
5043 count += count_decls_in_expression(for_stmt->condition);
5044 count += count_decls_in_expression(for_stmt->step);
5045 count += count_decls_in_stmts(for_stmt->body);
5049 case STATEMENT_CASE_LABEL: {
5050 const case_label_statement_t *label = &stmt->case_label;
5051 count += count_decls_in_expression(label->expression);
5052 if (label->statement != NULL) {
5053 count += count_decls_in_stmts(label->statement);
5059 case STATEMENT_BREAK:
5060 case STATEMENT_CONTINUE:
5063 case STATEMENT_EXPRESSION: {
5064 const expression_statement_t *expr_stmt = &stmt->expression;
5065 count += count_decls_in_expression(expr_stmt->expression);
5069 case STATEMENT_GOTO:
5070 case STATEMENT_LEAVE:
5071 case STATEMENT_INVALID:
5074 case STATEMENT_RETURN: {
5075 const return_statement_t *ret_stmt = &stmt->returns;
5076 count += count_decls_in_expression(ret_stmt->value);
5080 case STATEMENT_MS_TRY: {
5081 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5082 count += count_decls_in_stmts(try_stmt->try_statement);
5083 if (try_stmt->except_expression != NULL)
5084 count += count_decls_in_expression(try_stmt->except_expression);
5085 count += count_decls_in_stmts(try_stmt->final_statement);
5093 static int get_function_n_local_vars(declaration_t *declaration)
5097 /* count parameters */
5098 count += count_local_declarations(declaration->scope.declarations, NULL);
5100 /* count local variables declared in body */
5101 count += count_decls_in_stmts(declaration->init.statement);
5106 static void initialize_function_parameters(declaration_t *declaration)
5108 ir_graph *irg = current_ir_graph;
5109 ir_node *args = get_irg_args(irg);
5110 ir_node *start_block = get_irg_start_block(irg);
5111 ir_type *function_irtype = get_ir_type(declaration->type);
5114 declaration_t *parameter = declaration->scope.declarations;
5115 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5116 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5117 type_t *type = skip_typeref(parameter->type);
5119 bool needs_entity = parameter->address_taken;
5120 assert(!is_type_array(type));
5121 if (is_type_compound(type)) {
5122 needs_entity = true;
5126 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5127 ident *id = new_id_from_str(parameter->symbol->string);
5128 set_entity_ident(entity, id);
5130 parameter->declaration_kind
5131 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5132 parameter->v.entity = entity;
5136 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5137 ir_mode *param_mode = get_type_mode(param_irtype);
5140 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5142 ir_mode *mode = get_ir_mode(type);
5143 value = create_conv(NULL, value, mode);
5144 value = do_strict_conv(NULL, value);
5146 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5147 parameter->v.value_number = next_value_number_function;
5148 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5149 ++next_value_number_function;
5151 set_value(parameter->v.value_number, value);
5156 * Handle additional decl modifiers for IR-graphs
5158 * @param irg the IR-graph
5159 * @param dec_modifiers additional modifiers
5161 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5163 if (decl_modifiers & DM_NORETURN) {
5164 /* TRUE if the declaration includes the Microsoft
5165 __declspec(noreturn) specifier. */
5166 set_irg_additional_property(irg, mtp_property_noreturn);
5168 if (decl_modifiers & DM_NOTHROW) {
5169 /* TRUE if the declaration includes the Microsoft
5170 __declspec(nothrow) specifier. */
5171 set_irg_additional_property(irg, mtp_property_nothrow);
5173 if (decl_modifiers & DM_NAKED) {
5174 /* TRUE if the declaration includes the Microsoft
5175 __declspec(naked) specifier. */
5176 set_irg_additional_property(irg, mtp_property_naked);
5178 if (decl_modifiers & DM_FORCEINLINE) {
5179 /* TRUE if the declaration includes the
5180 Microsoft __forceinline specifier. */
5181 set_irg_inline_property(irg, irg_inline_forced);
5183 if (decl_modifiers & DM_NOINLINE) {
5184 /* TRUE if the declaration includes the Microsoft
5185 __declspec(noinline) specifier. */
5186 set_irg_inline_property(irg, irg_inline_forbidden);
5190 static void add_function_pointer(ir_type *segment, ir_entity *method,
5191 const char *unique_template)
5193 ir_type *method_type = get_entity_type(method);
5194 ident *id = id_unique(unique_template);
5195 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5197 ident *ide = id_unique(unique_template);
5198 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5199 ir_graph *irg = get_const_code_irg();
5200 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5203 set_entity_compiler_generated(ptr, 1);
5204 set_entity_variability(ptr, variability_constant);
5205 set_atomic_ent_value(ptr, val);
5209 * Create code for a function.
5211 static void create_function(declaration_t *declaration)
5213 ir_entity *function_entity = get_function_entity(declaration);
5215 if (declaration->init.statement == NULL)
5218 if (declaration->modifiers & DM_CONSTRUCTOR) {
5219 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5220 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5222 if (declaration->modifiers & DM_DESTRUCTOR) {
5223 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5224 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5227 current_function_decl = declaration;
5228 current_function_name = NULL;
5229 current_funcsig = NULL;
5231 assert(imature_blocks == NULL);
5232 imature_blocks = NEW_ARR_F(ir_node*, 0);
5234 int n_local_vars = get_function_n_local_vars(declaration);
5235 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5237 set_irg_fp_model(irg, firm_opt.fp_model);
5238 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5239 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5241 ir_node *first_block = get_cur_block();
5243 /* set inline flags */
5244 if (declaration->is_inline)
5245 set_irg_inline_property(irg, irg_inline_recomended);
5246 handle_decl_modifier_irg(irg, declaration->modifiers);
5248 next_value_number_function = 0;
5249 initialize_function_parameters(declaration);
5251 statement_to_firm(declaration->init.statement);
5253 ir_node *end_block = get_irg_end_block(irg);
5255 /* do we have a return statement yet? */
5256 if (get_cur_block() != NULL) {
5257 type_t *type = skip_typeref(declaration->type);
5258 assert(is_type_function(type));
5259 const function_type_t *func_type = &type->function;
5260 const type_t *return_type
5261 = skip_typeref(func_type->return_type);
5264 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5265 ret = new_Return(get_store(), 0, NULL);
5268 if (is_type_scalar(return_type)) {
5269 mode = get_ir_mode(func_type->return_type);
5275 /* ยง5.1.2.2.3 main implicitly returns 0 */
5276 if (strcmp(declaration->symbol->string, "main") == 0) {
5277 in[0] = new_Const(mode, get_mode_null(mode));
5279 in[0] = new_Unknown(mode);
5280 if (warning.return_type) {
5281 warningf(&declaration->source_position,
5282 "missing return statement at end of non-void function '%Y'",
5283 declaration->symbol);
5286 ret = new_Return(get_store(), 1, in);
5288 add_immBlock_pred(end_block, ret);
5291 for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
5292 mature_immBlock(imature_blocks[i]);
5294 DEL_ARR_F(imature_blocks);
5295 imature_blocks = NULL;
5297 mature_immBlock(first_block);
5298 mature_immBlock(end_block);
5300 irg_finalize_cons(irg);
5302 /* finalize the frame type */
5303 ir_type *frame_type = get_irg_frame_type(irg);
5304 int n = get_compound_n_members(frame_type);
5307 for(int i = 0; i < n; ++i) {
5308 ir_entity *entity = get_compound_member(frame_type, i);
5309 ir_type *entity_type = get_entity_type(entity);
5311 int align = get_type_alignment_bytes(entity_type);
5312 if (align > align_all)
5316 misalign = offset % align;
5318 offset += align - misalign;
5322 set_entity_offset(entity, offset);
5323 offset += get_type_size_bytes(entity_type);
5325 set_type_size_bytes(frame_type, offset);
5326 set_type_alignment_bytes(frame_type, align_all);
5331 static void scope_to_firm(scope_t *scope)
5333 /* first pass: create declarations */
5334 declaration_t *declaration = scope->declarations;
5335 for( ; declaration != NULL; declaration = declaration->next) {
5336 if (declaration->namespc != NAMESPACE_NORMAL)
5338 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5339 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5341 if (declaration->symbol == NULL)
5344 type_t *type = skip_typeref(declaration->type);
5345 if (is_type_function(type)) {
5346 get_function_entity(declaration);
5348 create_global_variable(declaration);
5352 /* second pass: create code/initializers */
5353 declaration = scope->declarations;
5354 for( ; declaration != NULL; declaration = declaration->next) {
5355 if (declaration->namespc != NAMESPACE_NORMAL)
5357 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5358 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5360 if (declaration->symbol == NULL)
5363 type_t *type = declaration->type;
5364 if (type->kind == TYPE_FUNCTION) {
5365 create_function(declaration);
5367 assert(declaration->declaration_kind
5368 == DECLARATION_KIND_GLOBAL_VARIABLE);
5369 current_ir_graph = get_const_code_irg();
5370 create_declaration_initializer(declaration);
5375 void init_ast2firm(void)
5377 obstack_init(&asm_obst);
5378 init_atomic_modes();
5380 id_underscore = new_id_from_chars("_", 1);
5381 id_imp = new_id_from_chars("__imp_", 6);
5383 /* OS option must be set to the backend */
5384 const char *s = "ia32-gasmode=linux";
5385 switch (firm_opt.os_support) {
5386 case OS_SUPPORT_MINGW:
5387 create_ld_ident = create_ld_ident_win32;
5388 s = "ia32-gasmode=mingw";
5390 case OS_SUPPORT_LINUX:
5391 create_ld_ident = create_ld_ident_linux_elf;
5392 s = "ia32-gasmode=elf";
5394 case OS_SUPPORT_MACHO:
5395 create_ld_ident = create_ld_ident_macho;
5396 s = "ia32-gasmode=macho";
5399 int res = firm_be_option(s);
5403 /* create idents for all known runtime functions */
5404 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5405 rts_idents[i] = new_id_from_str(rts_data[i].name);
5408 entitymap_init(&entitymap);
5411 static void init_ir_types(void)
5413 static int ir_types_initialized = 0;
5414 if (ir_types_initialized)
5416 ir_types_initialized = 1;
5418 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
5419 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
5420 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
5422 ir_type_int = get_ir_type(type_int);
5423 ir_type_const_char = get_ir_type(type_const_char);
5424 ir_type_wchar_t = get_ir_type(type_wchar_t);
5425 ir_type_void = get_ir_type(type_void);
5428 void exit_ast2firm(void)
5430 entitymap_destroy(&entitymap);
5431 obstack_free(&asm_obst, NULL);
5434 static void global_asm_to_firm(statement_t *s)
5436 for (; s != NULL; s = s->base.next) {
5437 assert(s->kind == STATEMENT_ASM);
5439 char const *const text = s->asms.asm_text.begin;
5440 size_t size = s->asms.asm_text.size;
5442 /* skip the last \0 */
5443 if (text[size - 1] == '\0')
5446 ident *const id = new_id_from_chars(text, size);
5451 void translation_unit_to_firm(translation_unit_t *unit)
5453 /* just to be sure */
5454 continue_label = NULL;
5456 current_switch_cond = NULL;
5460 scope_to_firm(&unit->scope);
5461 global_asm_to_firm(unit->global_asm);