2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <libfirm/firm.h>
27 #include <libfirm/adt/obst.h>
28 #include <libfirm/be.h>
32 #include "adt/error.h"
33 #include "adt/array.h"
39 #include "diagnostic.h"
40 #include "lang_features.h"
43 #include "entitymap_t.h"
44 #include "driver/firm_opt.h"
45 #include "driver/firm_cmdline.h"
47 /* some idents needed for name mangling */
48 static ident *id_underscore;
51 static ir_type *ir_type_const_char;
52 static ir_type *ir_type_wchar_t;
53 static ir_type *ir_type_void;
54 static ir_type *ir_type_int;
56 static int next_value_number_function;
57 static ir_node *continue_label;
58 static ir_node *break_label;
59 static ir_node *current_switch_cond;
60 static bool saw_default_label;
61 static declaration_t **all_labels;
62 static declaration_t **inner_functions;
63 static int inner_function_idx;
64 static ir_node *ijmp_list;
65 static bool constant_folding;
67 extern bool have_const_functions;
69 static const declaration_t *current_function_decl;
70 static ir_node *current_function_name;
71 static ir_node *current_funcsig;
72 static switch_statement_t *current_switch;
73 static ir_graph *current_function;
75 static entitymap_t entitymap;
77 static struct obstack asm_obst;
79 typedef enum declaration_kind_t {
80 DECLARATION_KIND_UNKNOWN,
81 DECLARATION_KIND_FUNCTION,
82 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
83 DECLARATION_KIND_GLOBAL_VARIABLE,
84 DECLARATION_KIND_LOCAL_VARIABLE,
85 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
86 DECLARATION_KIND_COMPOUND_MEMBER,
87 DECLARATION_KIND_LABEL_BLOCK,
88 DECLARATION_KIND_ENUM_ENTRY,
89 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
90 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
91 DECLARATION_KIND_TYPE,
92 DECLARATION_KIND_INNER_FUNCTION
95 static ir_type *get_ir_type(type_t *type);
96 static ir_type *get_ir_type_incomplete(type_t *type);
97 static int count_decls_in_stmts(const statement_t *stmt);
99 static void enqueue_inner_function(declaration_t *declaration) {
100 if (inner_functions == NULL) {
101 inner_functions = NEW_ARR_F(declaration_t *, 16);
102 inner_functions[0] = declaration;
103 inner_function_idx = 1;
105 int size = ARR_LEN(inner_functions);
106 if (inner_function_idx >= size) {
107 ARR_RESIZE(declaration_t *, inner_functions, size + 16);
109 inner_functions[inner_function_idx++] = declaration;
113 static declaration_t *next_inner_function(void) {
114 if (inner_function_idx == 0)
116 return inner_functions[--inner_function_idx];
119 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
121 const declaration_t *declaration = get_irg_loc_description(irg, pos);
123 if (declaration != NULL) {
124 warningf(&declaration->source_position,
125 "variable '%#T' might be used uninitialized",
126 declaration->type, declaration->symbol);
128 return new_r_Unknown(irg, mode);
131 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
133 const source_position_t *pos = (const source_position_t*) dbg;
136 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
140 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
142 const source_position_t *pos = (const source_position_t*) dbg;
147 return pos->input_name;
150 static dbg_info *get_dbg_info(const source_position_t *pos)
152 return (dbg_info*) pos;
155 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
157 static ir_mode *mode_int, *mode_uint;
159 static ir_node *_expression_to_firm(const expression_t *expression);
160 static ir_node *expression_to_firm(const expression_t *expression);
161 static inline ir_mode *get_ir_mode(type_t *type);
162 static void create_local_declaration(declaration_t *declaration);
164 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
166 unsigned flags = get_atomic_type_flags(kind);
167 unsigned size = get_atomic_type_size(kind);
168 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
169 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
172 unsigned bit_size = size * 8;
173 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
174 ir_mode_arithmetic arithmetic;
175 unsigned modulo_shift;
177 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
178 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
179 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
180 sort = irms_int_number;
181 arithmetic = irma_twos_complement;
182 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
184 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
185 snprintf(name, sizeof(name), "f%d", bit_size);
186 sort = irms_float_number;
187 arithmetic = irma_ieee754;
190 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
198 * Initialises the atomic modes depending on the machine size.
200 static void init_atomic_modes(void)
202 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
203 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
205 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
206 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
208 /* there's no real void type in firm */
209 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
211 /* initialize pointer modes */
213 ir_mode_sort sort = irms_reference;
214 unsigned bit_size = machine_size;
216 ir_mode_arithmetic arithmetic = irma_twos_complement;
217 unsigned modulo_shift
218 = bit_size < machine_size ? machine_size : bit_size;
220 snprintf(name, sizeof(name), "p%d", machine_size);
221 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
224 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
225 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
227 /* Hmm, pointers should be machine size */
228 set_modeP_data(ptr_mode);
229 set_modeP_code(ptr_mode);
232 static unsigned get_compound_type_size(compound_type_t *type)
234 ir_type *irtype = get_ir_type((type_t*) type);
235 return get_type_size_bytes(irtype);
238 static unsigned get_array_type_size(array_type_t *type)
240 assert(!type->is_vla);
241 ir_type *irtype = get_ir_type((type_t*) type);
242 return get_type_size_bytes(irtype);
246 static unsigned get_type_size_const(type_t *type)
250 panic("error type occurred");
252 return get_atomic_type_size(type->atomic.akind);
254 return 2 * get_atomic_type_size(type->complex.akind);
256 return get_atomic_type_size(type->imaginary.akind);
258 return get_mode_size_bytes(mode_int);
259 case TYPE_COMPOUND_UNION:
260 case TYPE_COMPOUND_STRUCT:
261 return get_compound_type_size(&type->compound);
263 /* just a pointer to the function */
264 return get_mode_size_bytes(mode_P_code);
266 return get_mode_size_bytes(mode_P_data);
268 return get_array_type_size(&type->array);
270 return get_type_size_const(type->builtin.real_type);
272 panic("type size of bitfield request");
278 panic("Trying to determine size of invalid type");
281 static ir_node *get_type_size(type_t *type)
283 type = skip_typeref(type);
285 if (is_type_array(type) && type->array.is_vla) {
286 ir_node *size_node = type->array.size_node;
287 if (size_node == NULL) {
288 size_node = expression_to_firm(type->array.size_expression);
289 assert(!is_Const(size_node));
290 type->array.size_node = size_node;
293 ir_node *elem_size = get_type_size(type->array.element_type);
294 ir_mode *mode = get_irn_mode(size_node);
295 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
299 ir_mode *mode = get_ir_mode(type_size_t);
301 sym.type_p = get_ir_type(type);
302 return new_SymConst(mode, sym, symconst_type_size);
305 static unsigned count_parameters(const function_type_t *function_type)
309 function_parameter_t *parameter = function_type->parameters;
310 for ( ; parameter != NULL; parameter = parameter->next) {
318 * Creates a Firm type for an atomic type
320 static ir_type *create_atomic_type(const atomic_type_t *type)
322 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
323 atomic_type_kind_t kind = type->akind;
324 ir_mode *mode = _atomic_modes[kind];
325 ident *id = get_mode_ident(mode);
326 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
328 set_type_alignment_bytes(irtype, type->base.alignment);
334 * Creates a Firm type for a complex type
336 static ir_type *create_complex_type(const complex_type_t *type)
338 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
339 atomic_type_kind_t kind = type->akind;
340 ir_mode *mode = _atomic_modes[kind];
341 ident *id = get_mode_ident(mode);
346 /* FIXME: finish the array */
351 * Creates a Firm type for an imaginary type
353 static ir_type *create_imaginary_type(const imaginary_type_t *type)
355 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
356 atomic_type_kind_t kind = type->akind;
357 ir_mode *mode = _atomic_modes[kind];
358 ident *id = get_mode_ident(mode);
359 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
361 set_type_alignment_bytes(irtype, type->base.alignment);
367 * return type of a parameter (and take transparent union gnu extension into
370 static type_t *get_parameter_type(type_t *type)
372 type = skip_typeref(type);
373 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
374 declaration_t *decl = type->compound.declaration;
375 type = decl->scope.declarations->type;
381 static ir_type *create_method_type(const function_type_t *function_type)
383 type_t *return_type = function_type->return_type;
385 ident *id = id_unique("functiontype.%u");
386 int n_parameters = count_parameters(function_type);
387 int n_results = return_type == type_void ? 0 : 1;
388 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
389 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
391 if (return_type != type_void) {
392 ir_type *restype = get_ir_type(return_type);
393 set_method_res_type(irtype, 0, restype);
396 function_parameter_t *parameter = function_type->parameters;
398 for ( ; parameter != NULL; parameter = parameter->next) {
399 type_t *type = get_parameter_type(parameter->type);
400 ir_type *p_irtype = get_ir_type(type);
401 set_method_param_type(irtype, n, p_irtype);
405 if (function_type->variadic || function_type->unspecified_parameters) {
406 set_method_variadicity(irtype, variadicity_variadic);
409 unsigned cc = get_method_calling_convention(irtype);
410 switch (function_type->calling_convention) {
411 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
414 set_method_calling_convention(irtype, SET_CDECL(cc));
418 if (function_type->variadic || function_type->unspecified_parameters)
421 /* only non-variadic function can use stdcall, else use cdecl */
422 set_method_calling_convention(irtype, SET_STDCALL(cc));
426 if (function_type->variadic || function_type->unspecified_parameters)
428 /* only non-variadic function can use fastcall, else use cdecl */
429 set_method_calling_convention(irtype, SET_FASTCALL(cc));
433 /* Hmm, leave default, not accepted by the parser yet. */
434 warningf(&function_type->base.source_position, "THISCALL calling convention not supported yet");
440 static ir_type *create_pointer_type(pointer_type_t *type)
442 type_t *points_to = type->points_to;
443 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
444 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
445 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
446 ir_points_to, mode_P_data, dbgi);
451 static ir_type *create_array_type(array_type_t *type)
453 type_t *element_type = type->element_type;
454 ir_type *ir_element_type = get_ir_type(element_type);
456 ident *id = id_unique("array.%u");
457 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
458 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
460 const int align = get_type_alignment_bytes(ir_element_type);
461 set_type_alignment_bytes(ir_type, align);
463 if (type->size_constant) {
464 int n_elements = type->size;
466 set_array_bounds_int(ir_type, 0, 0, n_elements);
468 size_t elemsize = get_type_size_bytes(ir_element_type);
469 if (elemsize % align > 0) {
470 elemsize += align - (elemsize % align);
472 set_type_size_bytes(ir_type, n_elements * elemsize);
474 set_array_lower_bound_int(ir_type, 0, 0);
476 set_type_state(ir_type, layout_fixed);
482 * Return the signed integer type of size bits.
484 * @param size the size
486 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
489 static ir_mode *s_modes[64 + 1] = {NULL, };
493 if (size <= 0 || size > 64)
496 mode = s_modes[size];
500 snprintf(name, sizeof(name), "bf_I%u", size);
501 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
502 size <= 32 ? 32 : size );
503 s_modes[size] = mode;
507 snprintf(name, sizeof(name), "I%u", size);
508 ident *id = new_id_from_str(name);
509 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
510 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
511 set_primitive_base_type(res, base_tp);
517 * Return the unsigned integer type of size bits.
519 * @param size the size
521 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
524 static ir_mode *u_modes[64 + 1] = {NULL, };
528 if (size <= 0 || size > 64)
531 mode = u_modes[size];
535 snprintf(name, sizeof(name), "bf_U%u", size);
536 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
537 size <= 32 ? 32 : size );
538 u_modes[size] = mode;
543 snprintf(name, sizeof(name), "U%u", size);
544 ident *id = new_id_from_str(name);
545 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
546 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
547 set_primitive_base_type(res, base_tp);
552 static ir_type *create_bitfield_type(bitfield_type_t *const type)
554 type_t *base = skip_typeref(type->base_type);
555 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
556 ir_type *irbase = get_ir_type(base);
558 unsigned size = type->bit_size;
560 assert(!is_type_float(base));
561 if (is_type_signed(base)) {
562 return get_signed_int_type_for_bit_size(irbase, size);
564 return get_unsigned_int_type_for_bit_size(irbase, size);
568 #define INVALID_TYPE ((ir_type_ptr)-1)
571 COMPOUND_IS_STRUCT = false,
572 COMPOUND_IS_UNION = true
576 * Construct firm type from ast struct type.
578 * As anonymous inner structs get flattened to a single firm type, we might get
579 * irtype, outer_offset and out_align passed (they represent the position of
580 * the anonymous inner struct inside the resulting firm struct)
582 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
583 size_t *outer_offset, size_t *outer_align,
584 bool incomplete, bool is_union)
586 declaration_t *declaration = type->declaration;
587 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
589 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
590 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
592 return declaration->v.irtype;
594 size_t align_all = 1;
596 size_t bit_offset = 0;
599 if (irtype == NULL) {
600 symbol_t *symbol = declaration->symbol;
602 if (symbol != NULL) {
603 id = new_id_from_str(symbol->string);
606 id = id_unique("__anonymous_union.%u");
608 id = id_unique("__anonymous_struct.%u");
611 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
614 irtype = new_d_type_union(id, dbgi);
616 irtype = new_d_type_struct(id, dbgi);
619 declaration->declaration_kind
620 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
621 declaration->v.irtype = irtype;
622 //type->base.firm_type = irtype;
624 offset = *outer_offset;
625 align_all = *outer_align;
631 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
633 declaration_t *entry = declaration->scope.declarations;
634 for( ; entry != NULL; entry = entry->next) {
635 if (entry->namespc != NAMESPACE_NORMAL)
638 size_t prev_offset = offset;
640 symbol_t *symbol = entry->symbol;
641 type_t *entry_type = skip_typeref(entry->type);
642 dbg_info *dbgi = get_dbg_info(&entry->source_position);
645 if (symbol != NULL) {
646 ident = new_id_from_str(symbol->string);
648 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
649 create_compound_type(&entry_type->compound, irtype, &offset,
650 &align_all, false, COMPOUND_IS_STRUCT);
651 goto finished_member;
652 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
653 create_compound_type(&entry_type->compound, irtype, &offset,
654 &align_all, false, COMPOUND_IS_UNION);
655 goto finished_member;
657 assert(entry_type->kind == TYPE_BITFIELD);
659 ident = id_unique("anon.%u");
662 ir_type *base_irtype;
663 if (entry_type->kind == TYPE_BITFIELD) {
664 base_irtype = get_ir_type(entry_type->bitfield.base_type);
666 base_irtype = get_ir_type(entry_type);
669 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
670 size_t misalign = offset % entry_alignment;
672 ir_type *entry_irtype = get_ir_type(entry_type);
673 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
676 size_t bits_remainder;
677 if (entry_type->kind == TYPE_BITFIELD) {
678 size_t size_bits = entry_type->bitfield.bit_size;
679 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
681 if (size_bits > rest_size_bits) {
682 /* start a new bucket */
683 offset += entry_alignment - misalign;
689 /* put into current bucket */
690 base = offset - misalign;
691 bits_remainder = misalign * 8 + bit_offset;
694 offset += size_bits / 8;
695 bit_offset = bit_offset + (size_bits % 8);
697 size_t entry_size = get_type_size_bytes(base_irtype);
698 if (misalign > 0 || bit_offset > 0)
699 offset += entry_alignment - misalign;
703 offset += entry_size;
707 if (entry_alignment > align_all) {
708 if (entry_alignment % align_all != 0) {
709 panic("uneven alignments not supported yet");
711 align_all = entry_alignment;
714 set_entity_offset(entity, base);
715 set_entity_offset_bits_remainder(entity,
716 (unsigned char) bits_remainder);
717 //add_struct_member(irtype, entity);
718 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
719 assert(entry->v.entity == NULL);
720 entry->v.entity = entity;
724 size_t entry_size = offset - prev_offset;
725 if (entry_size > size) {
737 size_t misalign = offset % align_all;
738 if (misalign > 0 || bit_offset > 0) {
739 size += align_all - misalign;
742 if (outer_offset != NULL) {
744 *outer_offset = offset;
746 *outer_offset += size;
749 if (align_all > *outer_align) {
750 if (align_all % *outer_align != 0) {
751 panic("uneven alignments not supported yet");
753 *outer_align = align_all;
756 set_type_alignment_bytes(irtype, align_all);
757 set_type_size_bytes(irtype, size);
758 set_type_state(irtype, layout_fixed);
764 static ir_type *create_enum_type(enum_type_t *const type)
766 type->base.firm_type = ir_type_int;
768 ir_mode *const mode = mode_int;
769 tarval *const one = get_mode_one(mode);
770 tarval * tv_next = get_tarval_null(mode);
772 bool constant_folding_old = constant_folding;
773 constant_folding = true;
775 declaration_t *declaration = type->declaration->next;
776 for (; declaration != NULL; declaration = declaration->next) {
777 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
780 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
782 expression_t *const init = declaration->init.enum_value;
784 ir_node *const cnst = expression_to_firm(init);
785 if (!is_Const(cnst)) {
786 panic("couldn't fold constant");
788 tv_next = get_Const_tarval(cnst);
790 declaration->v.enum_val = tv_next;
791 tv_next = tarval_add(tv_next, one);
794 constant_folding = constant_folding_old;
799 static ir_type *get_ir_type_incomplete(type_t *type)
801 assert(type != NULL);
802 type = skip_typeref(type);
804 if (type->base.firm_type != NULL) {
805 assert(type->base.firm_type != INVALID_TYPE);
806 return type->base.firm_type;
809 switch (type->kind) {
810 case TYPE_COMPOUND_STRUCT:
811 return create_compound_type(&type->compound, NULL, NULL, NULL,
812 true, COMPOUND_IS_STRUCT);
813 case TYPE_COMPOUND_UNION:
814 return create_compound_type(&type->compound, NULL, NULL, NULL,
815 true, COMPOUND_IS_UNION);
817 return get_ir_type(type);
821 static ir_type *get_ir_type(type_t *type)
823 assert(type != NULL);
825 type = skip_typeref(type);
827 if (type->base.firm_type != NULL) {
828 assert(type->base.firm_type != INVALID_TYPE);
829 return type->base.firm_type;
832 ir_type *firm_type = NULL;
833 switch (type->kind) {
835 /* Happens while constant folding, when there was an error */
836 return create_atomic_type(&type_void->atomic);
839 firm_type = create_atomic_type(&type->atomic);
842 firm_type = create_complex_type(&type->complex);
845 firm_type = create_imaginary_type(&type->imaginary);
848 firm_type = create_method_type(&type->function);
851 firm_type = create_pointer_type(&type->pointer);
854 firm_type = create_array_type(&type->array);
856 case TYPE_COMPOUND_STRUCT:
857 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
858 false, COMPOUND_IS_STRUCT);
860 case TYPE_COMPOUND_UNION:
861 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
862 false, COMPOUND_IS_UNION);
865 firm_type = create_enum_type(&type->enumt);
868 firm_type = get_ir_type(type->builtin.real_type);
871 firm_type = create_bitfield_type(&type->bitfield);
879 if (firm_type == NULL)
880 panic("unknown type found");
882 type->base.firm_type = firm_type;
886 static inline ir_mode *get_ir_mode(type_t *type)
888 ir_type *irtype = get_ir_type(type);
890 /* firm doesn't report a mode for arrays somehow... */
891 if (is_Array_type(irtype)) {
895 ir_mode *mode = get_type_mode(irtype);
896 assert(mode != NULL);
900 /** Names of the runtime functions. */
901 static const struct {
902 int id; /**< the rts id */
903 int n_res; /**< number of return values */
904 const char *name; /**< the name of the rts function */
905 int n_params; /**< number of parameters */
906 unsigned flags; /**< language flags */
908 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
909 { rts_abort, 0, "abort", 0, _C89 },
910 { rts_alloca, 1, "alloca", 1, _ALL },
911 { rts_abs, 1, "abs", 1, _C89 },
912 { rts_labs, 1, "labs", 1, _C89 },
913 { rts_llabs, 1, "llabs", 1, _C99 },
914 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
916 { rts_fabs, 1, "fabs", 1, _C89 },
917 { rts_sqrt, 1, "sqrt", 1, _C89 },
918 { rts_cbrt, 1, "cbrt", 1, _C99 },
919 { rts_exp, 1, "exp", 1, _C89 },
920 { rts_exp2, 1, "exp2", 1, _C89 },
921 { rts_exp10, 1, "exp10", 1, _GNUC },
922 { rts_log, 1, "log", 1, _C89 },
923 { rts_log2, 1, "log2", 1, _C89 },
924 { rts_log10, 1, "log10", 1, _C89 },
925 { rts_pow, 1, "pow", 2, _C89 },
926 { rts_sin, 1, "sin", 1, _C89 },
927 { rts_cos, 1, "cos", 1, _C89 },
928 { rts_tan, 1, "tan", 1, _C89 },
929 { rts_asin, 1, "asin", 1, _C89 },
930 { rts_acos, 1, "acos", 1, _C89 },
931 { rts_atan, 1, "atan", 1, _C89 },
932 { rts_sinh, 1, "sinh", 1, _C89 },
933 { rts_cosh, 1, "cosh", 1, _C89 },
934 { rts_tanh, 1, "tanh", 1, _C89 },
936 { rts_fabsf, 1, "fabsf", 1, _C99 },
937 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
938 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
939 { rts_expf, 1, "expf", 1, _C99 },
940 { rts_exp2f, 1, "exp2f", 1, _C99 },
941 { rts_exp10f, 1, "exp10f", 1, _GNUC },
942 { rts_logf, 1, "logf", 1, _C99 },
943 { rts_log2f, 1, "log2f", 1, _C99 },
944 { rts_log10f, 1, "log10f", 1, _C99 },
945 { rts_powf, 1, "powf", 2, _C99 },
946 { rts_sinf, 1, "sinf", 1, _C99 },
947 { rts_cosf, 1, "cosf", 1, _C99 },
948 { rts_tanf, 1, "tanf", 1, _C99 },
949 { rts_asinf, 1, "asinf", 1, _C99 },
950 { rts_acosf, 1, "acosf", 1, _C99 },
951 { rts_atanf, 1, "atanf", 1, _C99 },
952 { rts_sinhf, 1, "sinhf", 1, _C99 },
953 { rts_coshf, 1, "coshf", 1, _C99 },
954 { rts_tanhf, 1, "tanhf", 1, _C99 },
956 { rts_fabsl, 1, "fabsl", 1, _C99 },
957 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
958 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
959 { rts_expl, 1, "expl", 1, _C99 },
960 { rts_exp2l, 1, "exp2l", 1, _C99 },
961 { rts_exp10l, 1, "exp10l", 1, _GNUC },
962 { rts_logl, 1, "logl", 1, _C99 },
963 { rts_log2l, 1, "log2l", 1, _C99 },
964 { rts_log10l, 1, "log10l", 1, _C99 },
965 { rts_powl, 1, "powl", 2, _C99 },
966 { rts_sinl, 1, "sinl", 1, _C99 },
967 { rts_cosl, 1, "cosl", 1, _C99 },
968 { rts_tanl, 1, "tanl", 1, _C99 },
969 { rts_asinl, 1, "asinl", 1, _C99 },
970 { rts_acosl, 1, "acosl", 1, _C99 },
971 { rts_atanl, 1, "atanl", 1, _C99 },
972 { rts_sinhl, 1, "sinhl", 1, _C99 },
973 { rts_coshl, 1, "coshl", 1, _C99 },
974 { rts_tanhl, 1, "tanhl", 1, _C99 },
976 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
977 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
978 { rts_strcmp, 1, "strcmp", 2, _C89 },
979 { rts_strncmp, 1, "strncmp", 3, _C89 }
982 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
985 * Mangles an entity linker (ld) name for win32 usage.
987 * @param ent the entity to be mangled
988 * @param declaration the declaration
990 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
994 if (is_Method_type(get_entity_type(ent)))
995 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
997 /* always add an underscore in win32 */
998 id = mangle(id_underscore, get_entity_ident(ent));
1001 decl_modifiers_t decl_modifiers = declaration->modifiers;
1002 if (decl_modifiers & DM_DLLIMPORT) {
1003 /* add prefix for imported symbols */
1004 id = mangle(id_imp, id);
1010 * Mangles an entity linker (ld) name for Linux ELF usage.
1012 * @param ent the entity to be mangled
1013 * @param declaration the declaration
1015 static ident *create_ld_ident_linux_elf(ir_entity *entity,
1016 declaration_t *declaration)
1019 return get_entity_ident(entity);
1023 * Mangles an entity linker (ld) name for Mach-O usage.
1025 * @param ent the entity to be mangled
1026 * @param declaration the declaration
1028 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
1031 ident *id = mangle(id_underscore, get_entity_ident(ent));
1035 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
1036 declaration_t *declaration);
1037 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
1040 * Handle GNU attributes for entities
1042 * @param ent the entity
1043 * @param decl the routine declaration
1045 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
1047 if (decl->modifiers & DM_PURE) {
1048 /* TRUE if the declaration includes the GNU
1049 __attribute__((pure)) specifier. */
1050 set_entity_additional_property(ent, mtp_property_pure);
1052 if (decl->modifiers & DM_CONST) {
1053 set_entity_additional_property(ent, mtp_property_const);
1054 have_const_functions = true;
1056 if (decl->modifiers & DM_USED) {
1057 /* TRUE if the declaration includes the GNU
1058 __attribute__((used)) specifier. */
1059 set_entity_stickyness(ent, stickyness_sticky);
1064 * Creates an entity representing a function.
1066 * @param declaration the function declaration
1068 static ir_entity *get_function_entity(declaration_t *declaration)
1070 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION ||
1071 declaration->declaration_kind == DECLARATION_KIND_INNER_FUNCTION)
1072 return declaration->v.entity;
1073 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1075 symbol_t *symbol = declaration->symbol;
1076 ident *id = new_id_from_str(symbol->string);
1078 ir_type *global_type = get_glob_type();
1079 ir_type *ir_type_method = get_ir_type(declaration->type);
1080 assert(is_Method_type(ir_type_method));
1082 /* already an entity defined? */
1083 ir_entity *entity = entitymap_get(&entitymap, symbol);
1084 if (entity != NULL) {
1085 if (get_entity_visibility(entity) == visibility_external_allocated
1086 && declaration->init.statement != NULL) {
1087 set_entity_visibility(entity, visibility_external_visible);
1089 goto entity_created;
1092 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1093 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1094 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1096 handle_gnu_attributes_ent(entity, declaration);
1098 /* static inline => local
1099 * extern inline => local
1100 * inline without definition => local
1101 * inline with definition => external_visible */
1102 storage_class_tag_t const storage_class = (storage_class_tag_t)declaration->storage_class;
1103 bool const is_inline = declaration->is_inline;
1104 bool const has_body = declaration->init.statement != NULL;
1105 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1106 set_entity_visibility(entity, visibility_external_visible);
1107 } else if (storage_class == STORAGE_CLASS_STATIC ||
1108 (is_inline && has_body)) {
1110 /* this entity was declared, but is defined nowhere */
1111 set_entity_peculiarity(entity, peculiarity_description);
1113 set_entity_visibility(entity, visibility_local);
1114 } else if (has_body) {
1115 set_entity_visibility(entity, visibility_external_visible);
1117 set_entity_visibility(entity, visibility_external_allocated);
1119 set_entity_allocation(entity, allocation_static);
1121 /* We should check for file scope here, but as long as we compile C only
1122 this is not needed. */
1123 if (! firm_opt.freestanding) {
1124 /* check for a known runtime function */
1125 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1126 if (id != rts_idents[i])
1129 /* ignore those rts functions not necessary needed for current mode */
1130 if ((c_mode & rts_data[i].flags) == 0)
1132 assert(rts_entities[rts_data[i].id] == NULL);
1133 rts_entities[rts_data[i].id] = entity;
1137 entitymap_insert(&entitymap, symbol, entity);
1140 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1141 declaration->v.entity = entity;
1147 * Creates a Const node representing a constant.
1149 static ir_node *const_to_firm(const const_expression_t *cnst)
1151 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1152 ir_mode *mode = get_ir_mode(cnst->base.type);
1157 if (mode_is_float(mode)) {
1158 tv = new_tarval_from_double(cnst->v.float_value, mode);
1160 if (mode_is_signed(mode)) {
1161 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1163 len = snprintf(buf, sizeof(buf), "%llu",
1164 (unsigned long long) cnst->v.int_value);
1166 tv = new_tarval_from_str(buf, len, mode);
1169 return new_d_Const(dbgi, mode, tv);
1173 * Creates a Const node representing a character constant.
1175 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1177 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1178 ir_mode *mode = get_ir_mode(cnst->base.type);
1180 long long int v = 0;
1181 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1182 if (char_is_signed) {
1183 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1185 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1189 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1190 tarval *tv = new_tarval_from_str(buf, len, mode);
1192 return new_d_Const(dbgi, mode, tv);
1196 * Creates a Const node representing a wide character constant.
1198 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1200 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1201 ir_mode *mode = get_ir_mode(cnst->base.type);
1203 long long int v = cnst->v.wide_character.begin[0];
1206 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1207 tarval *tv = new_tarval_from_str(buf, len, mode);
1209 return new_d_Const(dbgi, mode, tv);
1213 * Creates a SymConst for a given entity.
1215 * @param dbgi debug info
1216 * @param mode the (reference) mode for the SymConst
1217 * @param entity the entity
1219 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1222 assert(entity != NULL);
1223 union symconst_symbol sym;
1224 sym.entity_p = entity;
1225 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1229 * Creates a SymConst node representing a string constant.
1231 * @param src_pos the source position of the string constant
1232 * @param id_prefix a prefix for the name of the generated string constant
1233 * @param value the value of the string constant
1235 static ir_node *string_to_firm(const source_position_t *const src_pos,
1236 const char *const id_prefix,
1237 const string_t *const value)
1239 ir_type *const global_type = get_glob_type();
1240 dbg_info *const dbgi = get_dbg_info(src_pos);
1241 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1242 ir_type_const_char, dbgi);
1244 ident *const id = id_unique(id_prefix);
1245 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1246 set_entity_ld_ident(entity, id);
1247 set_entity_variability(entity, variability_constant);
1248 set_entity_allocation(entity, allocation_static);
1250 ir_type *const elem_type = ir_type_const_char;
1251 ir_mode *const mode = get_type_mode(elem_type);
1253 const char* const string = value->begin;
1254 const size_t slen = value->size;
1256 set_array_lower_bound_int(type, 0, 0);
1257 set_array_upper_bound_int(type, 0, slen);
1258 set_type_size_bytes(type, slen);
1259 set_type_state(type, layout_fixed);
1261 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1262 for(size_t i = 0; i < slen; ++i) {
1263 tvs[i] = new_tarval_from_long(string[i], mode);
1266 set_array_entity_values(entity, tvs, slen);
1269 return create_symconst(dbgi, mode_P_data, entity);
1273 * Creates a SymConst node representing a string literal.
1275 * @param literal the string literal
1277 static ir_node *string_literal_to_firm(
1278 const string_literal_expression_t* literal)
1280 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1285 * Creates a SymConst node representing a wide string literal.
1287 * @param literal the wide string literal
1289 static ir_node *wide_string_literal_to_firm(
1290 const wide_string_literal_expression_t* const literal)
1292 ir_type *const global_type = get_glob_type();
1293 ir_type *const elem_type = ir_type_wchar_t;
1294 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1295 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1298 ident *const id = id_unique("Lstr.%u");
1299 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1300 set_entity_ld_ident(entity, id);
1301 set_entity_variability(entity, variability_constant);
1302 set_entity_allocation(entity, allocation_static);
1304 ir_mode *const mode = get_type_mode(elem_type);
1306 const wchar_rep_t *const string = literal->value.begin;
1307 const size_t slen = literal->value.size;
1309 set_array_lower_bound_int(type, 0, 0);
1310 set_array_upper_bound_int(type, 0, slen);
1311 set_type_size_bytes(type, slen);
1312 set_type_state(type, layout_fixed);
1314 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1315 for(size_t i = 0; i < slen; ++i) {
1316 tvs[i] = new_tarval_from_long(string[i], mode);
1319 set_array_entity_values(entity, tvs, slen);
1322 return create_symconst(dbgi, mode_P_data, entity);
1325 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1326 ir_node *const addr)
1328 ir_type *irtype = get_ir_type(type);
1329 if (is_compound_type(irtype)
1330 || is_Method_type(irtype)
1331 || is_Array_type(irtype)) {
1335 ir_mode *const mode = get_type_mode(irtype);
1336 ir_node *const memory = get_store();
1337 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1338 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1339 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1341 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1342 set_Load_volatility(load, volatility_is_volatile);
1345 set_store(load_mem);
1350 * Creates a strict Conv if neccessary.
1352 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1354 ir_mode *mode = get_irn_mode(node);
1356 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1358 if (!mode_is_float(mode))
1361 /* check if there is already a Conv */
1362 if (is_Conv(node)) {
1363 /* convert it into a strict Conv */
1364 set_Conv_strict(node, 1);
1368 /* otherwise create a new one */
1369 return new_d_strictConv(dbgi, node, mode);
1372 static ir_node *get_global_var_address(dbg_info *const dbgi,
1373 const declaration_t *const decl)
1375 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1377 ir_entity *const entity = decl->v.entity;
1378 switch ((storage_class_tag_t)decl->storage_class) {
1379 case STORAGE_CLASS_THREAD:
1380 case STORAGE_CLASS_THREAD_EXTERN:
1381 case STORAGE_CLASS_THREAD_STATIC: {
1382 ir_node *const no_mem = new_NoMem();
1383 ir_node *const tls = get_irg_tls(current_ir_graph);
1384 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1388 return create_symconst(dbgi, mode_P_data, entity);
1393 * Returns the correct base address depending on whether it is a parameter or a
1394 * normal local variable.
1396 static ir_node *get_local_frame(ir_entity *const ent)
1398 ir_graph *const irg = current_ir_graph;
1399 const ir_type *const owner = get_entity_owner(ent);
1400 if (owner == get_irg_frame_type(irg)) {
1401 return get_irg_frame(irg);
1403 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1404 return get_irg_value_param_base(irg);
1408 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1410 ir_mode *value_mode = get_irn_mode(value);
1412 if (value_mode == dest_mode || is_Bad(value))
1415 if (dest_mode == mode_b) {
1416 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1417 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1418 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1422 return new_d_Conv(dbgi, value, dest_mode);
1426 * Keep all memory edges of the given block.
1428 static void keep_all_memory(ir_node *block) {
1429 ir_node *old = get_cur_block();
1431 set_cur_block(block);
1432 keep_alive(get_store());
1433 /* TODO: keep all memory edges from restricted pointers */
1437 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1439 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1440 declaration_t *declaration = ref->declaration;
1441 type_t *type = skip_typeref(declaration->type);
1443 /* make sure the type is constructed */
1444 (void) get_ir_type(type);
1446 switch ((declaration_kind_t)declaration->declaration_kind) {
1447 case DECLARATION_KIND_TYPE:
1448 case DECLARATION_KIND_UNKNOWN:
1451 case DECLARATION_KIND_ENUM_ENTRY: {
1452 ir_mode *const mode = get_ir_mode(type);
1453 if (ref->is_outer_ref) {
1454 /* reference to an outer variable */
1455 panic("Outer variable reference not implemented");
1457 return new_Const(mode, declaration->v.enum_val);
1461 case DECLARATION_KIND_LOCAL_VARIABLE: {
1462 ir_mode *const mode = get_ir_mode(type);
1463 return get_value(declaration->v.value_number, mode);
1465 case DECLARATION_KIND_FUNCTION: {
1466 ir_mode *const mode = get_ir_mode(type);
1467 return create_symconst(dbgi, mode, declaration->v.entity);
1469 case DECLARATION_KIND_INNER_FUNCTION: {
1470 ir_mode *const mode = get_ir_mode(type);
1471 if (! declaration->goto_to_outer && !declaration->need_closure) {
1472 /* inner function not using the closure */
1473 return create_symconst(dbgi, mode, declaration->v.entity);
1475 /* TODO: need trampoline here */
1476 panic("Trampoline code not implemented");
1477 return create_symconst(dbgi, mode, declaration->v.entity);
1480 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1481 ir_node *const addr = get_global_var_address(dbgi, declaration);
1482 return deref_address(dbgi, declaration->type, addr);
1485 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1486 ir_entity *entity = declaration->v.entity;
1487 ir_node *frame = get_local_frame(entity);
1488 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1489 return deref_address(dbgi, declaration->type, sel);
1492 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1493 return declaration->v.vla_base;
1495 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1496 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1497 case DECLARATION_KIND_COMPOUND_MEMBER:
1498 case DECLARATION_KIND_LABEL_BLOCK:
1499 panic("not implemented reference type");
1502 panic("reference to declaration with unknown type found");
1505 static ir_node *reference_addr(const reference_expression_t *ref)
1507 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1508 declaration_t *declaration = ref->declaration;
1510 switch((declaration_kind_t) declaration->declaration_kind) {
1511 case DECLARATION_KIND_TYPE:
1512 case DECLARATION_KIND_UNKNOWN:
1514 case DECLARATION_KIND_LOCAL_VARIABLE:
1515 /* you can store to a local variable (so we don't panic but return NULL
1516 * as an indicator for no real address) */
1518 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1519 ir_node *const addr = get_global_var_address(dbgi, declaration);
1522 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1523 ir_entity *entity = declaration->v.entity;
1524 ir_node *frame = get_local_frame(entity);
1525 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1530 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1531 return declaration->v.vla_base;
1533 case DECLARATION_KIND_ENUM_ENTRY:
1534 panic("trying to reference enum entry");
1536 case DECLARATION_KIND_FUNCTION:
1537 case DECLARATION_KIND_INNER_FUNCTION:
1538 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1539 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1540 case DECLARATION_KIND_COMPOUND_MEMBER:
1541 case DECLARATION_KIND_LABEL_BLOCK:
1542 panic("not implemented reference type");
1545 panic("reference to declaration with unknown type found");
1549 * Transform calls to builtin functions.
1551 static ir_node *process_builtin_call(const call_expression_t *call)
1553 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1555 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1556 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1558 type_t *type = skip_typeref(builtin->base.type);
1559 assert(is_type_pointer(type));
1561 type_t *function_type = skip_typeref(type->pointer.points_to);
1562 symbol_t *symbol = builtin->symbol;
1564 switch(symbol->ID) {
1565 case T___builtin_alloca: {
1566 if (call->arguments == NULL || call->arguments->next != NULL) {
1567 panic("invalid number of parameters on __builtin_alloca");
1569 expression_t *argument = call->arguments->expression;
1570 ir_node *size = expression_to_firm(argument);
1572 ir_node *store = get_store();
1573 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1575 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1577 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1582 case T___builtin_huge_val:
1583 case T___builtin_inf:
1584 case T___builtin_inff:
1585 case T___builtin_infl: {
1586 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1587 tarval *tv = get_mode_infinite(mode);
1588 ir_node *res = new_d_Const(dbgi, mode, tv);
1591 case T___builtin_nan:
1592 case T___builtin_nanf:
1593 case T___builtin_nanl: {
1594 /* Ignore string for now... */
1595 assert(is_type_function(function_type));
1596 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1597 tarval *tv = get_mode_NAN(mode);
1598 ir_node *res = new_d_Const(dbgi, mode, tv);
1601 case T___builtin_va_end:
1604 panic("Unsupported builtin found\n");
1609 * Transform a call expression.
1610 * Handles some special cases, like alloca() calls, which must be resolved
1611 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1612 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1615 static ir_node *call_expression_to_firm(const call_expression_t *call)
1617 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1618 assert(get_cur_block() != NULL);
1620 expression_t *function = call->function;
1621 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1622 return process_builtin_call(call);
1624 if (function->kind == EXPR_REFERENCE) {
1625 const reference_expression_t *ref = &function->reference;
1626 declaration_t *declaration = ref->declaration;
1628 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1629 if (declaration->v.entity == rts_entities[rts_alloca]) {
1630 /* handle alloca() call */
1631 expression_t *argument = call->arguments->expression;
1632 ir_node *size = expression_to_firm(argument);
1634 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1636 ir_node *store = get_store();
1637 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1638 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1640 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1642 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1648 ir_node *callee = expression_to_firm(function);
1650 type_t *type = skip_typeref(function->base.type);
1651 assert(is_type_pointer(type));
1652 pointer_type_t *pointer_type = &type->pointer;
1653 type_t *points_to = skip_typeref(pointer_type->points_to);
1654 assert(is_type_function(points_to));
1655 function_type_t *function_type = &points_to->function;
1657 int n_parameters = 0;
1658 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1659 ir_type *new_method_type = NULL;
1660 if (function_type->variadic || function_type->unspecified_parameters) {
1661 const call_argument_t *argument = call->arguments;
1662 for( ; argument != NULL; argument = argument->next) {
1666 /* we need to construct a new method type matching the call
1668 int n_res = get_method_n_ress(ir_method_type);
1669 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1670 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1671 n_parameters, n_res, dbgi);
1672 set_method_calling_convention(new_method_type,
1673 get_method_calling_convention(ir_method_type));
1674 set_method_additional_properties(new_method_type,
1675 get_method_additional_properties(ir_method_type));
1676 set_method_variadicity(new_method_type,
1677 get_method_variadicity(ir_method_type));
1679 for(int i = 0; i < n_res; ++i) {
1680 set_method_res_type(new_method_type, i,
1681 get_method_res_type(ir_method_type, i));
1683 argument = call->arguments;
1684 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1685 expression_t *expression = argument->expression;
1686 ir_type *irtype = get_ir_type(expression->base.type);
1687 set_method_param_type(new_method_type, i, irtype);
1689 ir_method_type = new_method_type;
1691 n_parameters = get_method_n_params(ir_method_type);
1694 ir_node *in[n_parameters];
1696 const call_argument_t *argument = call->arguments;
1697 for(int n = 0; n < n_parameters; ++n) {
1698 expression_t *expression = argument->expression;
1699 ir_node *arg_node = expression_to_firm(expression);
1701 arg_node = do_strict_conv(dbgi, arg_node);
1705 argument = argument->next;
1708 ir_node *store = get_store();
1709 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1711 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1714 type_t *return_type = skip_typeref(function_type->return_type);
1715 ir_node *result = NULL;
1717 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1719 if (is_type_scalar(return_type)) {
1720 mode = get_ir_mode(return_type);
1724 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1725 result = new_d_Proj(dbgi, resproj, mode, 0);
1728 if (function->kind == EXPR_REFERENCE &&
1729 function->reference.declaration->modifiers & DM_NORETURN) {
1730 /* A dead end: Keep the Call and the Block. Also place all further
1731 * nodes into a new and unreachable block. */
1733 keep_alive(get_cur_block());
1740 static void statement_to_firm(statement_t *statement);
1741 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1743 static ir_node *expression_to_addr(const expression_t *expression);
1744 static ir_node *create_condition_evaluation(const expression_t *expression,
1745 ir_node *true_block,
1746 ir_node *false_block);
1748 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1751 value = do_strict_conv(dbgi, value);
1753 ir_node *memory = get_store();
1755 if (is_type_scalar(type)) {
1756 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1757 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1758 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1759 set_Store_volatility(store, volatility_is_volatile);
1760 set_store(store_mem);
1762 ir_type *irtype = get_ir_type(type);
1763 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1764 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1765 set_store(copyb_mem);
1769 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1771 tarval *all_one = get_mode_all_one(mode);
1772 int mode_size = get_mode_size_bits(mode);
1774 assert(offset >= 0);
1776 assert(offset + size <= mode_size);
1777 if (size == mode_size) {
1781 long shiftr = get_mode_size_bits(mode) - size;
1782 long shiftl = offset;
1783 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1784 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1785 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1786 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1791 static void bitfield_store_to_firm(const select_expression_t *expression,
1792 ir_node *addr, ir_node *value)
1794 type_t *type = expression->base.type;
1795 ir_mode *mode = get_ir_mode(type);
1797 assert(get_irn_mode(value) == mode || is_Bad(value));
1799 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1801 /* kill upper bits of value and shift to right position */
1802 ir_entity *entity = expression->compound_entry->v.entity;
1803 int bitoffset = get_entity_offset_bits_remainder(entity);
1804 ir_type *entity_type = get_entity_type(entity);
1805 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1807 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1808 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1809 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1810 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1811 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1812 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1814 /* load current value */
1815 ir_node *mem = get_store();
1816 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1817 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1818 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1819 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1820 tarval *inv_mask = tarval_not(shift_mask);
1821 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1822 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1824 /* construct new value and store */
1825 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1826 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1827 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1828 set_store(store_mem);
1830 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1831 set_Load_volatility(load, volatility_is_volatile);
1832 set_Store_volatility(store, volatility_is_volatile);
1836 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1839 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1840 type_t *type = expression->base.type;
1841 ir_mode *mode = get_ir_mode(type);
1842 ir_node *mem = get_store();
1843 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1844 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1845 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1847 load_res = create_conv(dbgi, load_res, mode_int);
1849 set_store(load_mem);
1851 /* kill upper bits */
1852 ir_entity *entity = expression->compound_entry->v.entity;
1853 int bitoffset = get_entity_offset_bits_remainder(entity);
1854 ir_type *entity_type = get_entity_type(entity);
1855 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1856 long shift_bitsl = machine_size - bitoffset - bitsize;
1857 assert(shift_bitsl >= 0);
1858 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1859 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1860 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1862 long shift_bitsr = bitoffset + shift_bitsl;
1863 assert(shift_bitsr <= (long) machine_size);
1864 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1865 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1867 if (mode_is_signed(mode)) {
1868 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1870 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1873 return create_conv(dbgi, shiftr, mode);
1876 static void set_value_for_expression_addr(const expression_t *expression,
1877 ir_node *value, ir_node *addr)
1879 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1880 value = do_strict_conv(dbgi, value);
1882 if (expression->kind == EXPR_REFERENCE) {
1883 const reference_expression_t *ref = &expression->reference;
1885 declaration_t *declaration = ref->declaration;
1886 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1887 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1888 set_value(declaration->v.value_number, value);
1894 addr = expression_to_addr(expression);
1896 type_t *type = skip_typeref(expression->base.type);
1898 if (expression->kind == EXPR_SELECT) {
1899 const select_expression_t *select = &expression->select;
1901 declaration_t *declaration = select->compound_entry;
1902 if (declaration->type->kind == TYPE_BITFIELD) {
1903 bitfield_store_to_firm(select, addr, value);
1908 assign_value(dbgi, addr, type, value);
1911 static void set_value_for_expression(const expression_t *expression,
1914 set_value_for_expression_addr(expression, value, NULL);
1917 static ir_node *get_value_from_lvalue(const expression_t *expression,
1920 if (expression->kind == EXPR_REFERENCE) {
1921 const reference_expression_t *ref = &expression->reference;
1923 declaration_t *declaration = ref->declaration;
1924 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1925 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1926 assert(addr == NULL);
1927 ir_mode *mode = get_ir_mode(expression->base.type);
1928 return get_value(declaration->v.value_number, mode);
1932 assert(addr != NULL);
1933 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1936 if (expression->kind == EXPR_SELECT &&
1937 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1938 value = bitfield_extract_to_firm(&expression->select, addr);
1940 value = deref_address(dbgi, expression->base.type, addr);
1947 static ir_node *create_incdec(const unary_expression_t *expression)
1949 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1950 const expression_t *value_expr = expression->value;
1951 ir_node *addr = expression_to_addr(value_expr);
1952 ir_node *value = get_value_from_lvalue(value_expr, addr);
1954 type_t *type = skip_typeref(expression->base.type);
1955 ir_mode *mode = get_ir_mode(expression->base.type);
1958 if (is_type_pointer(type)) {
1959 pointer_type_t *pointer_type = &type->pointer;
1960 offset = get_type_size(pointer_type->points_to);
1962 assert(is_type_arithmetic(type));
1963 offset = new_Const(mode, get_mode_one(mode));
1967 ir_node *store_value;
1968 switch(expression->base.kind) {
1969 case EXPR_UNARY_POSTFIX_INCREMENT:
1971 store_value = new_d_Add(dbgi, value, offset, mode);
1973 case EXPR_UNARY_POSTFIX_DECREMENT:
1975 store_value = new_d_Sub(dbgi, value, offset, mode);
1977 case EXPR_UNARY_PREFIX_INCREMENT:
1978 result = new_d_Add(dbgi, value, offset, mode);
1979 store_value = result;
1981 case EXPR_UNARY_PREFIX_DECREMENT:
1982 result = new_d_Sub(dbgi, value, offset, mode);
1983 store_value = result;
1986 panic("no incdec expr in create_incdec");
1989 set_value_for_expression_addr(value_expr, store_value, addr);
1994 static bool is_local_variable(expression_t *expression)
1996 if (expression->kind != EXPR_REFERENCE)
1998 reference_expression_t *ref_expr = &expression->reference;
1999 declaration_t *declaration = ref_expr->declaration;
2000 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
2003 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
2006 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
2007 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
2008 case EXPR_BINARY_NOTEQUAL:
2009 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2010 case EXPR_BINARY_ISLESS:
2011 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2012 case EXPR_BINARY_ISLESSEQUAL:
2013 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2014 case EXPR_BINARY_ISGREATER:
2015 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2016 case EXPR_BINARY_ISGREATEREQUAL:
2017 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2018 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2023 panic("trying to get pn_Cmp from non-comparison binexpr type");
2027 * Handle the assume optimizer hint: check if a Confirm
2028 * node can be created.
2030 * @param dbi debug info
2031 * @param expr the IL assume expression
2033 * we support here only some simple cases:
2038 static ir_node *handle_assume_compare(dbg_info *dbi,
2039 const binary_expression_t *expression)
2041 expression_t *op1 = expression->left;
2042 expression_t *op2 = expression->right;
2043 declaration_t *var2, *var = NULL;
2044 ir_node *res = NULL;
2047 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2049 if (is_local_variable(op1) && is_local_variable(op2)) {
2050 var = op1->reference.declaration;
2051 var2 = op2->reference.declaration;
2053 type_t *const type = skip_typeref(var->type);
2054 ir_mode *const mode = get_ir_mode(type);
2056 ir_node *const irn1 = get_value(var->v.value_number, mode);
2057 ir_node *const irn2 = get_value(var2->v.value_number, mode);
2059 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2060 set_value(var2->v.value_number, res);
2062 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2063 set_value(var->v.value_number, res);
2069 if (is_local_variable(op1) && is_constant_expression(op2)) {
2070 var = op1->reference.declaration;
2072 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2073 cmp_val = get_inversed_pnc(cmp_val);
2074 var = op2->reference.declaration;
2079 type_t *const type = skip_typeref(var->type);
2080 ir_mode *const mode = get_ir_mode(type);
2082 res = get_value(var->v.value_number, mode);
2083 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2084 set_value(var->v.value_number, res);
2090 * Handle the assume optimizer hint.
2092 * @param dbi debug info
2093 * @param expr the IL assume expression
2095 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2096 switch(expression->kind) {
2097 case EXPR_BINARY_EQUAL:
2098 case EXPR_BINARY_NOTEQUAL:
2099 case EXPR_BINARY_LESS:
2100 case EXPR_BINARY_LESSEQUAL:
2101 case EXPR_BINARY_GREATER:
2102 case EXPR_BINARY_GREATEREQUAL:
2103 return handle_assume_compare(dbi, &expression->binary);
2109 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2111 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2112 type_t *type = skip_typeref(expression->base.type);
2114 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2115 return expression_to_addr(expression->value);
2117 const expression_t *value = expression->value;
2119 switch(expression->base.kind) {
2120 case EXPR_UNARY_NEGATE: {
2121 ir_node *value_node = expression_to_firm(value);
2122 ir_mode *mode = get_ir_mode(type);
2123 return new_d_Minus(dbgi, value_node, mode);
2125 case EXPR_UNARY_PLUS:
2126 return expression_to_firm(value);
2127 case EXPR_UNARY_BITWISE_NEGATE: {
2128 ir_node *value_node = expression_to_firm(value);
2129 ir_mode *mode = get_ir_mode(type);
2130 return new_d_Not(dbgi, value_node, mode);
2132 case EXPR_UNARY_NOT: {
2133 ir_node *value_node = _expression_to_firm(value);
2134 value_node = create_conv(dbgi, value_node, mode_b);
2135 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2138 case EXPR_UNARY_DEREFERENCE: {
2139 ir_node *value_node = expression_to_firm(value);
2140 type_t *value_type = skip_typeref(value->base.type);
2141 assert(is_type_pointer(value_type));
2142 type_t *points_to = value_type->pointer.points_to;
2143 return deref_address(dbgi, points_to, value_node);
2145 case EXPR_UNARY_POSTFIX_INCREMENT:
2146 case EXPR_UNARY_POSTFIX_DECREMENT:
2147 case EXPR_UNARY_PREFIX_INCREMENT:
2148 case EXPR_UNARY_PREFIX_DECREMENT:
2149 return create_incdec(expression);
2150 case EXPR_UNARY_CAST: {
2151 ir_node *value_node = expression_to_firm(value);
2152 if (is_type_scalar(type)) {
2153 ir_mode *mode = get_ir_mode(type);
2154 ir_node *node = create_conv(dbgi, value_node, mode);
2155 node = do_strict_conv(dbgi, node);
2158 /* make sure firm type is constructed */
2159 (void) get_ir_type(type);
2163 case EXPR_UNARY_CAST_IMPLICIT: {
2164 ir_node *value_node = expression_to_firm(value);
2165 if (is_type_scalar(type)) {
2166 ir_mode *mode = get_ir_mode(type);
2167 return create_conv(dbgi, value_node, mode);
2172 case EXPR_UNARY_ASSUME:
2173 if (firm_opt.confirm)
2174 return handle_assume(dbgi, value);
2181 panic("invalid UNEXPR type found");
2185 * produces a 0/1 depending of the value of a mode_b node
2187 static ir_node *produce_condition_result(const expression_t *expression,
2190 ir_mode *mode = get_ir_mode(expression->base.type);
2191 ir_node *cur_block = get_cur_block();
2193 ir_node *one_block = new_immBlock();
2194 ir_node *one = new_Const(mode, get_mode_one(mode));
2195 ir_node *jmp_one = new_d_Jmp(dbgi);
2197 ir_node *zero_block = new_immBlock();
2198 ir_node *zero = new_Const(mode, get_mode_null(mode));
2199 ir_node *jmp_zero = new_d_Jmp(dbgi);
2201 set_cur_block(cur_block);
2202 create_condition_evaluation(expression, one_block, zero_block);
2203 mature_immBlock(one_block);
2204 mature_immBlock(zero_block);
2206 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2207 new_Block(2, in_cf);
2209 ir_node *in[2] = { one, zero };
2210 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2215 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2216 ir_node *value, type_t *type)
2218 pointer_type_t *const pointer_type = &type->pointer;
2219 type_t *const points_to = skip_typeref(pointer_type->points_to);
2220 unsigned elem_size = get_type_size_const(points_to);
2222 /* gcc extension: allow arithmetic with void * and function * */
2223 if ((elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) ||
2224 is_type_function(points_to)) {
2228 assert(elem_size >= 1);
2232 value = create_conv(dbgi, value, mode_int);
2233 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2234 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2238 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2239 ir_node *left, ir_node *right)
2242 type_t *type_left = skip_typeref(expression->left->base.type);
2243 type_t *type_right = skip_typeref(expression->right->base.type);
2245 expression_kind_t kind = expression->base.kind;
2248 case EXPR_BINARY_SHIFTLEFT:
2249 case EXPR_BINARY_SHIFTRIGHT:
2250 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2251 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2252 mode = get_irn_mode(left);
2253 right = create_conv(dbgi, right, mode_uint);
2256 case EXPR_BINARY_SUB:
2257 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2258 const pointer_type_t *const ptr_type = &type_left->pointer;
2260 mode = get_ir_mode(expression->base.type);
2261 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2262 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2263 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2264 ir_node *const no_mem = new_NoMem();
2265 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2266 mode, op_pin_state_floats);
2267 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2270 case EXPR_BINARY_SUB_ASSIGN:
2271 if (is_type_pointer(type_left)) {
2272 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2273 mode = get_ir_mode(type_left);
2278 case EXPR_BINARY_ADD:
2279 case EXPR_BINARY_ADD_ASSIGN:
2280 if (is_type_pointer(type_left)) {
2281 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2282 mode = get_ir_mode(type_left);
2284 } else if (is_type_pointer(type_right)) {
2285 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2286 mode = get_ir_mode(type_right);
2293 mode = get_irn_mode(right);
2294 left = create_conv(dbgi, left, mode);
2299 case EXPR_BINARY_ADD_ASSIGN:
2300 case EXPR_BINARY_ADD:
2301 return new_d_Add(dbgi, left, right, mode);
2302 case EXPR_BINARY_SUB_ASSIGN:
2303 case EXPR_BINARY_SUB:
2304 return new_d_Sub(dbgi, left, right, mode);
2305 case EXPR_BINARY_MUL_ASSIGN:
2306 case EXPR_BINARY_MUL:
2307 return new_d_Mul(dbgi, left, right, mode);
2308 case EXPR_BINARY_BITWISE_AND:
2309 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2310 return new_d_And(dbgi, left, right, mode);
2311 case EXPR_BINARY_BITWISE_OR:
2312 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2313 return new_d_Or(dbgi, left, right, mode);
2314 case EXPR_BINARY_BITWISE_XOR:
2315 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2316 return new_d_Eor(dbgi, left, right, mode);
2317 case EXPR_BINARY_SHIFTLEFT:
2318 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2319 return new_d_Shl(dbgi, left, right, mode);
2320 case EXPR_BINARY_SHIFTRIGHT:
2321 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2322 if (mode_is_signed(mode)) {
2323 return new_d_Shrs(dbgi, left, right, mode);
2325 return new_d_Shr(dbgi, left, right, mode);
2327 case EXPR_BINARY_DIV:
2328 case EXPR_BINARY_DIV_ASSIGN: {
2329 ir_node *pin = new_Pin(new_NoMem());
2332 if (mode_is_float(mode)) {
2333 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2334 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2336 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2337 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2341 case EXPR_BINARY_MOD:
2342 case EXPR_BINARY_MOD_ASSIGN: {
2343 ir_node *pin = new_Pin(new_NoMem());
2344 assert(!mode_is_float(mode));
2345 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2346 op_pin_state_floats);
2347 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2351 panic("unexpected expression kind");
2355 static ir_node *create_lazy_op(const binary_expression_t *expression)
2357 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2358 type_t *type = expression->base.type;
2359 ir_mode *mode = get_ir_mode(type);
2361 if (is_constant_expression(expression->left)) {
2362 long val = fold_constant(expression->left);
2363 expression_kind_t ekind = expression->base.kind;
2364 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2365 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
2366 (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2367 return expression_to_firm(expression->right);
2369 return new_Const(mode, get_mode_one(mode));
2373 return produce_condition_result((const expression_t*) expression, dbgi);
2376 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2377 ir_node *right, ir_mode *mode);
2379 static ir_node *create_assign_binop(const binary_expression_t *expression)
2381 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2382 const expression_t *left_expr = expression->left;
2383 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2384 ir_node *right = expression_to_firm(expression->right);
2385 ir_node *left_addr = expression_to_addr(left_expr);
2386 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2387 ir_node *result = create_op(dbgi, expression, left, right);
2389 result = create_conv(dbgi, result, left_mode);
2390 result = do_strict_conv(dbgi, result);
2392 set_value_for_expression_addr(left_expr, result, left_addr);
2397 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2399 expression_kind_t kind = expression->base.kind;
2402 case EXPR_BINARY_EQUAL:
2403 case EXPR_BINARY_NOTEQUAL:
2404 case EXPR_BINARY_LESS:
2405 case EXPR_BINARY_LESSEQUAL:
2406 case EXPR_BINARY_GREATER:
2407 case EXPR_BINARY_GREATEREQUAL:
2408 case EXPR_BINARY_ISGREATER:
2409 case EXPR_BINARY_ISGREATEREQUAL:
2410 case EXPR_BINARY_ISLESS:
2411 case EXPR_BINARY_ISLESSEQUAL:
2412 case EXPR_BINARY_ISLESSGREATER:
2413 case EXPR_BINARY_ISUNORDERED: {
2414 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2415 ir_node *left = expression_to_firm(expression->left);
2416 ir_node *right = expression_to_firm(expression->right);
2417 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2418 long pnc = get_pnc(kind, expression->left->base.type);
2419 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2422 case EXPR_BINARY_ASSIGN: {
2423 ir_node *addr = expression_to_addr(expression->left);
2424 ir_node *right = expression_to_firm(expression->right);
2425 set_value_for_expression_addr(expression->left, right, addr);
2429 case EXPR_BINARY_ADD:
2430 case EXPR_BINARY_SUB:
2431 case EXPR_BINARY_MUL:
2432 case EXPR_BINARY_DIV:
2433 case EXPR_BINARY_MOD:
2434 case EXPR_BINARY_BITWISE_AND:
2435 case EXPR_BINARY_BITWISE_OR:
2436 case EXPR_BINARY_BITWISE_XOR:
2437 case EXPR_BINARY_SHIFTLEFT:
2438 case EXPR_BINARY_SHIFTRIGHT:
2440 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2441 ir_node *left = expression_to_firm(expression->left);
2442 ir_node *right = expression_to_firm(expression->right);
2443 return create_op(dbgi, expression, left, right);
2445 case EXPR_BINARY_LOGICAL_AND:
2446 case EXPR_BINARY_LOGICAL_OR:
2447 return create_lazy_op(expression);
2448 case EXPR_BINARY_COMMA:
2449 /* create side effects of left side */
2450 (void) expression_to_firm(expression->left);
2451 return _expression_to_firm(expression->right);
2453 case EXPR_BINARY_ADD_ASSIGN:
2454 case EXPR_BINARY_SUB_ASSIGN:
2455 case EXPR_BINARY_MUL_ASSIGN:
2456 case EXPR_BINARY_MOD_ASSIGN:
2457 case EXPR_BINARY_DIV_ASSIGN:
2458 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2459 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2460 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2461 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2462 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2463 return create_assign_binop(expression);
2464 case EXPR_BINARY_BUILTIN_EXPECT:
2465 return _expression_to_firm(expression->left);
2467 panic("TODO binexpr type");
2471 static ir_node *array_access_addr(const array_access_expression_t *expression)
2473 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2474 ir_node *base_addr = expression_to_firm(expression->array_ref);
2475 ir_node *offset = expression_to_firm(expression->index);
2477 type_t *offset_type = skip_typeref(expression->index->base.type);
2479 if (is_type_signed(offset_type)) {
2480 mode = get_ir_mode(type_ssize_t);
2482 mode = get_ir_mode(type_size_t);
2484 offset = create_conv(dbgi, offset, mode);
2486 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2487 assert(is_type_pointer(ref_type));
2488 pointer_type_t *pointer_type = &ref_type->pointer;
2490 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2491 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2492 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2494 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2499 static ir_node *array_access_to_firm(
2500 const array_access_expression_t *expression)
2502 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2503 ir_node *addr = array_access_addr(expression);
2504 type_t *type = revert_automatic_type_conversion(
2505 (const expression_t*) expression);
2506 type = skip_typeref(type);
2508 return deref_address(dbgi, type, addr);
2511 static long get_offsetof_offset(const offsetof_expression_t *expression)
2513 type_t *orig_type = expression->type;
2516 designator_t *designator = expression->designator;
2517 for( ; designator != NULL; designator = designator->next) {
2518 type_t *type = skip_typeref(orig_type);
2519 /* be sure the type is constructed */
2520 (void) get_ir_type(type);
2522 if (designator->symbol != NULL) {
2523 assert(is_type_compound(type));
2524 symbol_t *symbol = designator->symbol;
2526 declaration_t *declaration = type->compound.declaration;
2527 declaration_t *iter = declaration->scope.declarations;
2528 for( ; iter != NULL; iter = iter->next) {
2529 if (iter->symbol == symbol) {
2533 assert(iter != NULL);
2535 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2536 offset += get_entity_offset(iter->v.entity);
2538 orig_type = iter->type;
2540 expression_t *array_index = designator->array_index;
2541 assert(designator->array_index != NULL);
2542 assert(is_type_array(type));
2544 long index = fold_constant(array_index);
2545 ir_type *arr_type = get_ir_type(type);
2546 ir_type *elem_type = get_array_element_type(arr_type);
2547 long elem_size = get_type_size_bytes(elem_type);
2549 offset += index * elem_size;
2551 orig_type = type->array.element_type;
2558 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2560 ir_mode *mode = get_ir_mode(expression->base.type);
2561 long offset = get_offsetof_offset(expression);
2562 tarval *tv = new_tarval_from_long(offset, mode);
2563 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2565 return new_d_Const(dbgi, mode, tv);
2568 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2569 ir_entity *entity, type_t *type);
2571 static ir_node *compound_literal_to_firm(
2572 const compound_literal_expression_t *expression)
2574 type_t *type = expression->type;
2576 /* create an entity on the stack */
2577 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2579 ident *const id = id_unique("CompLit.%u");
2580 ir_type *const irtype = get_ir_type(type);
2581 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2582 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2583 set_entity_ld_ident(entity, id);
2585 set_entity_variability(entity, variability_uninitialized);
2587 /* create initialisation code */
2588 initializer_t *initializer = expression->initializer;
2589 create_local_initializer(initializer, dbgi, entity, type);
2591 /* create a sel for the compound literal address */
2592 ir_node *frame = get_local_frame(entity);
2593 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2598 * Transform a sizeof expression into Firm code.
2600 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2602 type_t *type = expression->type;
2604 type = expression->tp_expression->base.type;
2605 assert(type != NULL);
2608 type = skip_typeref(type);
2609 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2610 if (is_type_array(type) && type->array.is_vla
2611 && expression->tp_expression != NULL) {
2612 expression_to_firm(expression->tp_expression);
2615 return get_type_size(type);
2619 * Transform an alignof expression into Firm code.
2621 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2623 type_t *type = expression->type;
2625 /* beware: if expression is a variable reference, return the
2626 alignment of the variable. */
2627 const expression_t *tp_expression = expression->tp_expression;
2628 const declaration_t *declaration = expr_is_variable(tp_expression);
2629 if (declaration != NULL) {
2630 /* TODO: get the alignment of this variable. */
2632 type = tp_expression->base.type;
2633 assert(type != NULL);
2636 ir_mode *const mode = get_ir_mode(expression->base.type);
2637 symconst_symbol sym;
2638 sym.type_p = get_ir_type(type);
2639 return new_SymConst(mode, sym, symconst_type_align);
2642 static void init_ir_types(void);
2644 long fold_constant(const expression_t *expression)
2646 assert(is_type_valid(skip_typeref(expression->base.type)));
2648 bool constant_folding_old = constant_folding;
2649 constant_folding = true;
2653 assert(is_constant_expression(expression));
2655 ir_graph *old_current_ir_graph = current_ir_graph;
2656 if (current_ir_graph == NULL) {
2657 current_ir_graph = get_const_code_irg();
2660 ir_node *cnst = expression_to_firm(expression);
2661 current_ir_graph = old_current_ir_graph;
2663 if (!is_Const(cnst)) {
2664 panic("couldn't fold constant\n");
2667 tarval *tv = get_Const_tarval(cnst);
2668 if (!tarval_is_long(tv)) {
2669 panic("result of constant folding is not integer\n");
2672 constant_folding = constant_folding_old;
2674 return get_tarval_long(tv);
2677 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2679 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2681 /* first try to fold a constant condition */
2682 if (is_constant_expression(expression->condition)) {
2683 long val = fold_constant(expression->condition);
2685 expression_t *true_expression = expression->true_expression;
2686 if (true_expression == NULL)
2687 true_expression = expression->condition;
2688 return expression_to_firm(true_expression);
2690 return expression_to_firm(expression->false_expression);
2694 ir_node *cur_block = get_cur_block();
2696 /* create the true block */
2697 ir_node *true_block = new_immBlock();
2699 ir_node *true_val = expression->true_expression != NULL ?
2700 expression_to_firm(expression->true_expression) : NULL;
2701 ir_node *true_jmp = new_Jmp();
2703 /* create the false block */
2704 ir_node *false_block = new_immBlock();
2706 ir_node *false_val = expression_to_firm(expression->false_expression);
2707 ir_node *false_jmp = new_Jmp();
2709 /* create the condition evaluation */
2710 set_cur_block(cur_block);
2711 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
2712 if (expression->true_expression == NULL) {
2713 if (cond_expr != NULL) {
2714 true_val = cond_expr;
2716 /* Condition ended with a short circuit (&&, ||, !) operation.
2717 * Generate a "1" as value for the true branch. */
2718 ir_mode *const mode = mode_Is;
2719 true_val = new_Const(mode, get_mode_one(mode));
2722 mature_immBlock(true_block);
2723 mature_immBlock(false_block);
2725 /* create the common block */
2726 ir_node *in_cf[2] = { true_jmp, false_jmp };
2727 new_Block(2, in_cf);
2729 /* TODO improve static semantics, so either both or no values are NULL */
2730 if (true_val == NULL || false_val == NULL)
2733 ir_node *in[2] = { true_val, false_val };
2734 ir_mode *mode = get_irn_mode(true_val);
2735 assert(get_irn_mode(false_val) == mode);
2736 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2742 * Returns an IR-node representing the address of a field.
2744 static ir_node *select_addr(const select_expression_t *expression)
2746 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2748 ir_node *compound_addr = expression_to_firm(expression->compound);
2750 /* make sure the type is constructed */
2751 type_t *type = skip_typeref(expression->compound->base.type);
2752 if (is_type_pointer(type)) {
2753 type = type->pointer.points_to;
2755 (void) get_ir_type(type);
2757 declaration_t *entry = expression->compound_entry;
2758 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2759 ir_entity *entity = entry->v.entity;
2761 assert(entity != NULL);
2763 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2768 static ir_node *select_to_firm(const select_expression_t *expression)
2770 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2771 ir_node *addr = select_addr(expression);
2772 type_t *type = revert_automatic_type_conversion(
2773 (const expression_t*) expression);
2774 type = skip_typeref(type);
2776 declaration_t *entry = expression->compound_entry;
2777 type_t *entry_type = skip_typeref(entry->type);
2779 if (entry_type->kind == TYPE_BITFIELD) {
2780 return bitfield_extract_to_firm(expression, addr);
2783 return deref_address(dbgi, type, addr);
2786 /* Values returned by __builtin_classify_type. */
2787 typedef enum gcc_type_class
2793 enumeral_type_class,
2796 reference_type_class,
2800 function_type_class,
2811 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2813 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2819 const atomic_type_t *const atomic_type = &type->atomic;
2820 switch (atomic_type->akind) {
2821 /* should not be reached */
2822 case ATOMIC_TYPE_INVALID:
2826 /* gcc cannot do that */
2827 case ATOMIC_TYPE_VOID:
2828 tc = void_type_class;
2831 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2832 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2833 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2834 case ATOMIC_TYPE_SHORT:
2835 case ATOMIC_TYPE_USHORT:
2836 case ATOMIC_TYPE_INT:
2837 case ATOMIC_TYPE_UINT:
2838 case ATOMIC_TYPE_LONG:
2839 case ATOMIC_TYPE_ULONG:
2840 case ATOMIC_TYPE_LONGLONG:
2841 case ATOMIC_TYPE_ULONGLONG:
2842 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2843 tc = integer_type_class;
2846 case ATOMIC_TYPE_FLOAT:
2847 case ATOMIC_TYPE_DOUBLE:
2848 case ATOMIC_TYPE_LONG_DOUBLE:
2849 tc = real_type_class;
2852 panic("Unexpected atomic type in classify_type_to_firm().");
2855 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2856 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2857 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2858 case TYPE_ARRAY: /* gcc handles this as pointer */
2859 case TYPE_FUNCTION: /* gcc handles this as pointer */
2860 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2861 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2862 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2864 /* gcc handles this as integer */
2865 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2868 /* typedef/typeof should be skipped already */
2875 panic("unexpected TYPE classify_type_to_firm().");
2878 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2879 ir_mode *const mode = mode_int;
2880 tarval *const tv = new_tarval_from_long(tc, mode);
2881 return new_d_Const(dbgi, mode, tv);
2884 static ir_node *function_name_to_firm(
2885 const funcname_expression_t *const expr)
2887 switch(expr->kind) {
2888 case FUNCNAME_FUNCTION:
2889 case FUNCNAME_PRETTY_FUNCTION:
2890 case FUNCNAME_FUNCDNAME:
2891 if (current_function_name == NULL) {
2892 const source_position_t *const src_pos = &expr->base.source_position;
2893 const char *const name = current_function_decl->symbol->string;
2894 const string_t string = { name, strlen(name) + 1 };
2895 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2897 return current_function_name;
2898 case FUNCNAME_FUNCSIG:
2899 if (current_funcsig == NULL) {
2900 const source_position_t *const src_pos = &expr->base.source_position;
2901 ir_entity *ent = get_irg_entity(current_ir_graph);
2902 const char *const name = get_entity_ld_name(ent);
2903 const string_t string = { name, strlen(name) + 1 };
2904 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2906 return current_funcsig;
2908 panic("Unsupported function name");
2911 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2913 statement_t *statement = expr->statement;
2915 assert(statement->kind == STATEMENT_COMPOUND);
2916 return compound_statement_to_firm(&statement->compound);
2919 static ir_node *va_start_expression_to_firm(
2920 const va_start_expression_t *const expr)
2922 ir_type *const method_type = get_ir_type(current_function_decl->type);
2923 int const n = get_method_n_params(method_type) - 1;
2924 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2925 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2926 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2927 ir_node *const no_mem = new_NoMem();
2928 ir_node *const arg_sel =
2929 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2931 ir_node *const cnst = get_type_size(expr->parameter->type);
2932 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2933 set_value_for_expression(expr->ap, add);
2938 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2940 type_t *const type = expr->base.type;
2941 expression_t *const ap_expr = expr->ap;
2942 ir_node *const ap_addr = expression_to_addr(ap_expr);
2943 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2944 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2945 ir_node *const res = deref_address(dbgi, type, ap);
2947 ir_node *const cnst = get_type_size(expr->base.type);
2948 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2950 set_value_for_expression_addr(ap_expr, add, ap_addr);
2955 static ir_node *dereference_addr(const unary_expression_t *const expression)
2957 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2958 return expression_to_firm(expression->value);
2962 * Returns a IR-node representing an lvalue of the given expression.
2964 static ir_node *expression_to_addr(const expression_t *expression)
2966 switch(expression->kind) {
2967 case EXPR_REFERENCE:
2968 return reference_addr(&expression->reference);
2969 case EXPR_ARRAY_ACCESS:
2970 return array_access_addr(&expression->array_access);
2972 return select_addr(&expression->select);
2974 return call_expression_to_firm(&expression->call);
2975 case EXPR_UNARY_DEREFERENCE: {
2976 return dereference_addr(&expression->unary);
2981 panic("trying to get address of non-lvalue");
2984 static ir_node *builtin_constant_to_firm(
2985 const builtin_constant_expression_t *expression)
2987 ir_mode *mode = get_ir_mode(expression->base.type);
2990 if (is_constant_expression(expression->value)) {
2995 return new_Const_long(mode, v);
2998 static ir_node *builtin_prefetch_to_firm(
2999 const builtin_prefetch_expression_t *expression)
3001 ir_node *adr = expression_to_firm(expression->adr);
3002 /* no Firm support for prefetch yet */
3007 static ir_node *get_label_block(declaration_t *label)
3009 assert(label->namespc == NAMESPACE_LABEL || label->namespc == NAMESPACE_LOCAL_LABEL);
3011 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3012 return label->v.block;
3014 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3016 /* beware: might be called from create initializer with current_ir_graph
3017 * set to const_code_irg. */
3018 ir_graph *rem = current_ir_graph;
3019 current_ir_graph = current_function;
3021 ir_node *old_cur_block = get_cur_block();
3022 ir_node *block = new_immBlock();
3023 set_cur_block(old_cur_block);
3025 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3026 label->v.block = block;
3028 ARR_APP1(declaration_t *, all_labels, label);
3030 current_ir_graph = rem;
3035 * Pointer to a label. This is used for the
3036 * GNU address-of-label extension.
3038 static ir_node *label_address_to_firm(
3039 const label_address_expression_t *label)
3041 ir_node *block = get_label_block(label->declaration);
3042 ir_label_t nr = get_Block_label(block);
3045 nr = get_irp_next_label_nr();
3046 set_Block_label(block, nr);
3048 symconst_symbol value;
3050 return new_SymConst(mode_P_code, value, symconst_label);
3054 * creates firm nodes for an expression. The difference between this function
3055 * and expression_to_firm is, that this version might produce mode_b nodes
3056 * instead of mode_Is.
3058 static ir_node *_expression_to_firm(const expression_t *expression)
3061 if (!constant_folding) {
3062 assert(!expression->base.transformed);
3063 ((expression_t*) expression)->base.transformed = true;
3067 switch (expression->kind) {
3068 case EXPR_CHARACTER_CONSTANT:
3069 return character_constant_to_firm(&expression->conste);
3070 case EXPR_WIDE_CHARACTER_CONSTANT:
3071 return wide_character_constant_to_firm(&expression->conste);
3073 return const_to_firm(&expression->conste);
3074 case EXPR_STRING_LITERAL:
3075 return string_literal_to_firm(&expression->string);
3076 case EXPR_WIDE_STRING_LITERAL:
3077 return wide_string_literal_to_firm(&expression->wide_string);
3078 case EXPR_REFERENCE:
3079 return reference_expression_to_firm(&expression->reference);
3081 return call_expression_to_firm(&expression->call);
3083 return unary_expression_to_firm(&expression->unary);
3085 return binary_expression_to_firm(&expression->binary);
3086 case EXPR_ARRAY_ACCESS:
3087 return array_access_to_firm(&expression->array_access);
3089 return sizeof_to_firm(&expression->typeprop);
3091 return alignof_to_firm(&expression->typeprop);
3092 case EXPR_CONDITIONAL:
3093 return conditional_to_firm(&expression->conditional);
3095 return select_to_firm(&expression->select);
3096 case EXPR_CLASSIFY_TYPE:
3097 return classify_type_to_firm(&expression->classify_type);
3099 return function_name_to_firm(&expression->funcname);
3100 case EXPR_STATEMENT:
3101 return statement_expression_to_firm(&expression->statement);
3103 return va_start_expression_to_firm(&expression->va_starte);
3105 return va_arg_expression_to_firm(&expression->va_arge);
3106 case EXPR_BUILTIN_SYMBOL:
3107 panic("unimplemented expression found");
3108 case EXPR_BUILTIN_CONSTANT_P:
3109 return builtin_constant_to_firm(&expression->builtin_constant);
3110 case EXPR_BUILTIN_PREFETCH:
3111 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
3113 return offsetof_to_firm(&expression->offsetofe);
3114 case EXPR_COMPOUND_LITERAL:
3115 return compound_literal_to_firm(&expression->compound_literal);
3116 case EXPR_LABEL_ADDRESS:
3117 return label_address_to_firm(&expression->label_address);
3123 panic("invalid expression found");
3126 static bool produces_mode_b(const expression_t *expression)
3128 switch (expression->kind) {
3129 case EXPR_BINARY_EQUAL:
3130 case EXPR_BINARY_NOTEQUAL:
3131 case EXPR_BINARY_LESS:
3132 case EXPR_BINARY_LESSEQUAL:
3133 case EXPR_BINARY_GREATER:
3134 case EXPR_BINARY_GREATEREQUAL:
3135 case EXPR_BINARY_ISGREATER:
3136 case EXPR_BINARY_ISGREATEREQUAL:
3137 case EXPR_BINARY_ISLESS:
3138 case EXPR_BINARY_ISLESSEQUAL:
3139 case EXPR_BINARY_ISLESSGREATER:
3140 case EXPR_BINARY_ISUNORDERED:
3141 case EXPR_UNARY_NOT:
3144 case EXPR_BINARY_BUILTIN_EXPECT:
3145 return produces_mode_b(expression->binary.left);
3146 case EXPR_BINARY_COMMA:
3147 return produces_mode_b(expression->binary.right);
3154 static ir_node *expression_to_firm(const expression_t *expression)
3156 if (!produces_mode_b(expression)) {
3157 ir_node *res = _expression_to_firm(expression);
3158 assert(res == NULL || get_irn_mode(res) != mode_b);
3162 if (is_constant_expression(expression)) {
3163 ir_node *res = _expression_to_firm(expression);
3164 ir_mode *mode = get_ir_mode(expression->base.type);
3165 assert(is_Const(res));
3166 if (is_Const_null(res)) {
3167 return new_Const_long(mode, 0);
3169 return new_Const_long(mode, 1);
3173 /* we have to produce a 0/1 from the mode_b expression */
3174 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3175 return produce_condition_result(expression, dbgi);
3179 * create a short-circuit expression evaluation that tries to construct
3180 * efficient control flow structures for &&, || and ! expressions
3182 static ir_node *create_condition_evaluation(const expression_t *expression,
3183 ir_node *true_block,
3184 ir_node *false_block)
3186 switch(expression->kind) {
3187 case EXPR_UNARY_NOT: {
3188 const unary_expression_t *unary_expression = &expression->unary;
3189 create_condition_evaluation(unary_expression->value, false_block,
3193 case EXPR_BINARY_LOGICAL_AND: {
3194 const binary_expression_t *binary_expression = &expression->binary;
3196 ir_node *cur_block = get_cur_block();
3197 ir_node *extra_block = new_immBlock();
3198 set_cur_block(cur_block);
3199 create_condition_evaluation(binary_expression->left, extra_block,
3201 mature_immBlock(extra_block);
3202 set_cur_block(extra_block);
3203 create_condition_evaluation(binary_expression->right, true_block,
3207 case EXPR_BINARY_LOGICAL_OR: {
3208 const binary_expression_t *binary_expression = &expression->binary;
3210 ir_node *cur_block = get_cur_block();
3211 ir_node *extra_block = new_immBlock();
3212 set_cur_block(cur_block);
3213 create_condition_evaluation(binary_expression->left, true_block,
3215 mature_immBlock(extra_block);
3216 set_cur_block(extra_block);
3217 create_condition_evaluation(binary_expression->right, true_block,
3225 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3226 ir_node *cond_expr = _expression_to_firm(expression);
3227 ir_node *condition = create_conv(NULL, cond_expr, mode_b);
3228 ir_node *cond = new_d_Cond(dbgi, condition);
3229 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3230 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3232 /* set branch prediction info based on __builtin_expect */
3233 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3234 long cnst = fold_constant(expression->binary.right);
3235 cond_jmp_predicate pred;
3238 pred = COND_JMP_PRED_FALSE;
3240 pred = COND_JMP_PRED_TRUE;
3242 set_Cond_jmp_pred(cond, pred);
3245 add_immBlock_pred(true_block, true_proj);
3246 if (false_block != NULL) {
3247 add_immBlock_pred(false_block, false_proj);
3250 set_cur_block(NULL);
3256 static void create_declaration_entity(declaration_t *declaration,
3257 declaration_kind_t declaration_kind,
3258 ir_type *parent_type)
3260 type_t *const type = skip_typeref(declaration->type);
3261 if (is_type_function(type)) {
3262 (void) get_function_entity(declaration);
3266 ident *const id = new_id_from_str(declaration->symbol->string);
3267 ir_type *const irtype = get_ir_type(type);
3268 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3269 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3271 handle_gnu_attributes_ent(entity, declaration);
3273 declaration->declaration_kind = (unsigned char) declaration_kind;
3274 declaration->v.entity = entity;
3275 set_entity_variability(entity, variability_uninitialized);
3276 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3277 if (parent_type == get_tls_type())
3278 set_entity_allocation(entity, allocation_automatic);
3279 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3280 set_entity_allocation(entity, allocation_static);
3282 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3283 set_entity_volatility(entity, volatility_is_volatile);
3288 typedef struct type_path_entry_t type_path_entry_t;
3289 struct type_path_entry_t {
3291 ir_initializer_t *initializer;
3293 declaration_t *compound_entry;
3296 typedef struct type_path_t type_path_t;
3297 struct type_path_t {
3298 type_path_entry_t *path;
3303 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3305 size_t len = ARR_LEN(path->path);
3307 for(size_t i = 0; i < len; ++i) {
3308 const type_path_entry_t *entry = & path->path[i];
3310 type_t *type = skip_typeref(entry->type);
3311 if (is_type_compound(type)) {
3312 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3313 } else if (is_type_array(type)) {
3314 fprintf(stderr, "[%zd]", entry->index);
3316 fprintf(stderr, "-INVALID-");
3319 fprintf(stderr, " (");
3320 print_type(path->top_type);
3321 fprintf(stderr, ")");
3324 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3326 size_t len = ARR_LEN(path->path);
3328 return & path->path[len-1];
3331 static type_path_entry_t *append_to_type_path(type_path_t *path)
3333 size_t len = ARR_LEN(path->path);
3334 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3336 type_path_entry_t *result = & path->path[len];
3337 memset(result, 0, sizeof(result[0]));
3341 static size_t get_compound_size(const compound_type_t *type)
3343 declaration_t *declaration = type->declaration;
3344 declaration_t *member = declaration->scope.declarations;
3346 for( ; member != NULL; member = member->next) {
3349 /* TODO: cache results? */
3354 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3356 type_t *orig_top_type = path->top_type;
3357 type_t *top_type = skip_typeref(orig_top_type);
3359 assert(is_type_compound(top_type) || is_type_array(top_type));
3361 if (ARR_LEN(path->path) == 0) {
3364 type_path_entry_t *top = get_type_path_top(path);
3365 ir_initializer_t *initializer = top->initializer;
3366 return get_initializer_compound_value(initializer, top->index);
3370 static void descend_into_subtype(type_path_t *path)
3372 type_t *orig_top_type = path->top_type;
3373 type_t *top_type = skip_typeref(orig_top_type);
3375 assert(is_type_compound(top_type) || is_type_array(top_type));
3377 ir_initializer_t *initializer = get_initializer_entry(path);
3379 type_path_entry_t *top = append_to_type_path(path);
3380 top->type = top_type;
3384 if (is_type_compound(top_type)) {
3385 declaration_t *declaration = top_type->compound.declaration;
3386 declaration_t *entry = declaration->scope.declarations;
3388 top->compound_entry = entry;
3390 len = get_compound_size(&top_type->compound);
3392 path->top_type = entry->type;
3394 assert(is_type_array(top_type));
3395 assert(top_type->array.size > 0);
3398 path->top_type = top_type->array.element_type;
3399 len = top_type->array.size;
3401 if (initializer == NULL
3402 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3403 initializer = create_initializer_compound(len);
3404 /* we have to set the entry at the 2nd latest path entry... */
3405 size_t path_len = ARR_LEN(path->path);
3406 assert(path_len >= 1);
3408 type_path_entry_t *entry = & path->path[path_len-2];
3409 ir_initializer_t *tinitializer = entry->initializer;
3410 set_initializer_compound_value(tinitializer, entry->index,
3414 top->initializer = initializer;
3417 static void ascend_from_subtype(type_path_t *path)
3419 type_path_entry_t *top = get_type_path_top(path);
3421 path->top_type = top->type;
3423 size_t len = ARR_LEN(path->path);
3424 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3427 static void walk_designator(type_path_t *path, const designator_t *designator)
3429 /* designators start at current object type */
3430 ARR_RESIZE(type_path_entry_t, path->path, 1);
3432 for( ; designator != NULL; designator = designator->next) {
3433 type_path_entry_t *top = get_type_path_top(path);
3434 type_t *orig_type = top->type;
3435 type_t *type = skip_typeref(orig_type);
3437 if (designator->symbol != NULL) {
3438 assert(is_type_compound(type));
3440 symbol_t *symbol = designator->symbol;
3442 declaration_t *declaration = type->compound.declaration;
3443 declaration_t *iter = declaration->scope.declarations;
3444 for( ; iter != NULL; iter = iter->next, ++index) {
3445 if (iter->symbol == symbol) {
3449 assert(iter != NULL);
3451 top->type = orig_type;
3452 top->compound_entry = iter;
3454 orig_type = iter->type;
3456 expression_t *array_index = designator->array_index;
3457 assert(designator->array_index != NULL);
3458 assert(is_type_array(type));
3460 long index = fold_constant(array_index);
3463 if (type->array.size_constant) {
3464 long array_size = type->array.size;
3465 assert(index < array_size);
3469 top->type = orig_type;
3470 top->index = (size_t) index;
3471 orig_type = type->array.element_type;
3473 path->top_type = orig_type;
3475 if (designator->next != NULL) {
3476 descend_into_subtype(path);
3480 path->invalid = false;
3483 static void advance_current_object(type_path_t *path)
3485 if (path->invalid) {
3486 /* TODO: handle this... */
3487 panic("invalid initializer in ast2firm (excessive elements)");
3491 type_path_entry_t *top = get_type_path_top(path);
3493 type_t *type = skip_typeref(top->type);
3494 if (is_type_union(type)) {
3495 top->compound_entry = NULL;
3496 } else if (is_type_struct(type)) {
3497 declaration_t *entry = top->compound_entry;
3500 entry = entry->next;
3501 top->compound_entry = entry;
3502 if (entry != NULL) {
3503 path->top_type = entry->type;
3507 assert(is_type_array(type));
3510 if (!type->array.size_constant || top->index < type->array.size) {
3515 /* we're past the last member of the current sub-aggregate, try if we
3516 * can ascend in the type hierarchy and continue with another subobject */
3517 size_t len = ARR_LEN(path->path);
3520 ascend_from_subtype(path);
3521 advance_current_object(path);
3523 path->invalid = true;
3528 static ir_initializer_t *create_ir_initializer(
3529 const initializer_t *initializer, type_t *type);
3531 static ir_initializer_t *create_ir_initializer_value(
3532 const initializer_value_t *initializer)
3534 if (is_type_compound(initializer->value->base.type)) {
3535 panic("initializer creation for compounds not implemented yet");
3537 ir_node *value = expression_to_firm(initializer->value);
3538 return create_initializer_const(value);
3541 static ir_initializer_t *create_ir_initializer_list(
3542 const initializer_list_t *initializer, type_t *type)
3545 memset(&path, 0, sizeof(path));
3546 path.top_type = type;
3547 path.path = NEW_ARR_F(type_path_entry_t, 0);
3549 descend_into_subtype(&path);
3551 for(size_t i = 0; i < initializer->len; ++i) {
3552 const initializer_t *sub_initializer = initializer->initializers[i];
3554 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3555 walk_designator(&path, sub_initializer->designator.designator);
3559 if (sub_initializer->kind == INITIALIZER_VALUE) {
3560 /* we might have to descend into types until we're at a scalar
3563 type_t *orig_top_type = path.top_type;
3564 type_t *top_type = skip_typeref(orig_top_type);
3566 if (is_type_scalar(top_type))
3568 descend_into_subtype(&path);
3572 ir_initializer_t *sub_irinitializer
3573 = create_ir_initializer(sub_initializer, path.top_type);
3575 size_t path_len = ARR_LEN(path.path);
3576 assert(path_len >= 1);
3577 type_path_entry_t *entry = & path.path[path_len-1];
3578 ir_initializer_t *tinitializer = entry->initializer;
3579 set_initializer_compound_value(tinitializer, entry->index,
3582 advance_current_object(&path);
3585 assert(ARR_LEN(path.path) >= 1);
3586 ir_initializer_t *result = path.path[0].initializer;
3587 DEL_ARR_F(path.path);
3592 static ir_initializer_t *create_ir_initializer_string(
3593 const initializer_string_t *initializer, type_t *type)
3595 type = skip_typeref(type);
3597 size_t string_len = initializer->string.size;
3598 assert(type->kind == TYPE_ARRAY);
3599 assert(type->array.size_constant);
3600 size_t len = type->array.size;
3601 ir_initializer_t *irinitializer = create_initializer_compound(len);
3603 const char *string = initializer->string.begin;
3604 ir_mode *mode = get_ir_mode(type->array.element_type);
3606 for(size_t i = 0; i < len; ++i) {
3611 tarval *tv = new_tarval_from_long(c, mode);
3612 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3614 set_initializer_compound_value(irinitializer, i, char_initializer);
3617 return irinitializer;
3620 static ir_initializer_t *create_ir_initializer_wide_string(
3621 const initializer_wide_string_t *initializer, type_t *type)
3623 size_t string_len = initializer->string.size;
3624 assert(type->kind == TYPE_ARRAY);
3625 assert(type->array.size_constant);
3626 size_t len = type->array.size;
3627 ir_initializer_t *irinitializer = create_initializer_compound(len);
3629 const wchar_rep_t *string = initializer->string.begin;
3630 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3632 for(size_t i = 0; i < len; ++i) {
3634 if (i < string_len) {
3637 tarval *tv = new_tarval_from_long(c, mode);
3638 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3640 set_initializer_compound_value(irinitializer, i, char_initializer);
3643 return irinitializer;
3646 static ir_initializer_t *create_ir_initializer(
3647 const initializer_t *initializer, type_t *type)
3649 switch(initializer->kind) {
3650 case INITIALIZER_STRING:
3651 return create_ir_initializer_string(&initializer->string, type);
3653 case INITIALIZER_WIDE_STRING:
3654 return create_ir_initializer_wide_string(&initializer->wide_string,
3657 case INITIALIZER_LIST:
3658 return create_ir_initializer_list(&initializer->list, type);
3660 case INITIALIZER_VALUE:
3661 return create_ir_initializer_value(&initializer->value);
3663 case INITIALIZER_DESIGNATOR:
3664 panic("unexpected designator initializer found");
3666 panic("unknown initializer");
3669 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3672 if (is_atomic_type(type)) {
3673 ir_mode *mode = get_type_mode(type);
3674 tarval *zero = get_mode_null(mode);
3675 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3677 /* TODO: bitfields */
3678 ir_node *mem = get_store();
3679 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3680 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3683 assert(is_compound_type(type));
3686 if (is_Array_type(type)) {
3687 assert(has_array_upper_bound(type, 0));
3688 n_members = get_array_upper_bound_int(type, 0);
3690 n_members = get_compound_n_members(type);
3693 for(int i = 0; i < n_members; ++i) {
3696 if (is_Array_type(type)) {
3697 ir_entity *entity = get_array_element_entity(type);
3698 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3699 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3700 ir_node *in[1] = { cnst };
3701 irtype = get_array_element_type(type);
3702 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3704 ir_entity *member = get_compound_member(type, i);
3706 irtype = get_entity_type(member);
3707 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3710 create_dynamic_null_initializer(irtype, dbgi, addr);
3715 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3716 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3718 switch(get_initializer_kind(initializer)) {
3719 case IR_INITIALIZER_NULL: {
3720 create_dynamic_null_initializer(type, dbgi, base_addr);
3723 case IR_INITIALIZER_CONST: {
3724 ir_node *node = get_initializer_const_value(initializer);
3725 ir_mode *mode = get_irn_mode(node);
3726 assert(get_type_mode(type) == mode);
3728 /* TODO: bitfields... */
3729 ir_node *mem = get_store();
3730 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3731 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3735 case IR_INITIALIZER_TARVAL: {
3736 tarval *tv = get_initializer_tarval_value(initializer);
3737 ir_mode *mode = get_tarval_mode(tv);
3738 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3739 assert(get_type_mode(type) == mode);
3741 /* TODO: bitfields... */
3742 ir_node *mem = get_store();
3743 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3744 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3748 case IR_INITIALIZER_COMPOUND: {
3749 assert(is_compound_type(type));
3751 if (is_Array_type(type)) {
3752 assert(has_array_upper_bound(type, 0));
3753 n_members = get_array_upper_bound_int(type, 0);
3755 n_members = get_compound_n_members(type);
3758 if (get_initializer_compound_n_entries(initializer)
3759 != (unsigned) n_members)
3760 panic("initializer doesn't match compound type");
3762 for(int i = 0; i < n_members; ++i) {
3765 if (is_Array_type(type)) {
3766 ir_entity *entity = get_array_element_entity(type);
3767 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3768 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3769 ir_node *in[1] = { cnst };
3770 irtype = get_array_element_type(type);
3771 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3773 ir_entity *member = get_compound_member(type, i);
3775 irtype = get_entity_type(member);
3776 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3779 ir_initializer_t *sub_init
3780 = get_initializer_compound_value(initializer, i);
3782 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3788 panic("invalid IR_INITIALIZER found");
3791 static void create_dynamic_initializer(ir_initializer_t *initializer,
3792 dbg_info *dbgi, ir_entity *entity)
3794 ir_node *frame = get_local_frame(entity);
3795 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3796 ir_type *type = get_entity_type(entity);
3798 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3801 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3802 ir_entity *entity, type_t *type)
3804 ir_node *memory = get_store();
3805 ir_node *nomem = new_NoMem();
3806 ir_node *frame = get_irg_frame(current_ir_graph);
3807 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3809 if (initializer->kind == INITIALIZER_VALUE) {
3810 initializer_value_t *initializer_value = &initializer->value;
3812 ir_node *value = expression_to_firm(initializer_value->value);
3813 type = skip_typeref(type);
3814 assign_value(dbgi, addr, type, value);
3818 if (!is_constant_initializer(initializer)) {
3819 ir_initializer_t *irinitializer
3820 = create_ir_initializer(initializer, type);
3822 create_dynamic_initializer(irinitializer, dbgi, entity);
3826 /* create the ir_initializer */
3827 ir_graph *const old_current_ir_graph = current_ir_graph;
3828 current_ir_graph = get_const_code_irg();
3830 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3832 assert(current_ir_graph == get_const_code_irg());
3833 current_ir_graph = old_current_ir_graph;
3835 /* create a "template" entity which is copied to the entity on the stack */
3836 ident *const id = id_unique("initializer.%u");
3837 ir_type *const irtype = get_ir_type(type);
3838 ir_type *const global_type = get_glob_type();
3839 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3840 set_entity_ld_ident(init_entity, id);
3842 set_entity_variability(init_entity, variability_initialized);
3843 set_entity_visibility(init_entity, visibility_local);
3844 set_entity_allocation(init_entity, allocation_static);
3846 set_entity_initializer(init_entity, irinitializer);
3848 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3849 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3851 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3852 set_store(copyb_mem);
3855 static void create_initializer_local_variable_entity(declaration_t *declaration)
3857 initializer_t *initializer = declaration->init.initializer;
3858 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3859 ir_entity *entity = declaration->v.entity;
3860 type_t *type = declaration->type;
3861 create_local_initializer(initializer, dbgi, entity, type);
3864 static void create_declaration_initializer(declaration_t *declaration)
3866 initializer_t *initializer = declaration->init.initializer;
3867 if (initializer == NULL)
3870 declaration_kind_t declaration_kind
3871 = (declaration_kind_t) declaration->declaration_kind;
3872 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3873 create_initializer_local_variable_entity(declaration);
3877 type_t *type = declaration->type;
3878 type_qualifiers_t tq = get_type_qualifier(type, true);
3880 if (initializer->kind == INITIALIZER_VALUE) {
3881 initializer_value_t *initializer_value = &initializer->value;
3883 = get_dbg_info(&declaration->source_position);
3885 ir_node *value = expression_to_firm(initializer_value->value);
3886 value = do_strict_conv(dbgi, value);
3888 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3889 set_value(declaration->v.value_number, value);
3891 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3893 ir_entity *entity = declaration->v.entity;
3895 if (tq & TYPE_QUALIFIER_CONST) {
3896 set_entity_variability(entity, variability_constant);
3898 set_entity_variability(entity, variability_initialized);
3900 set_atomic_ent_value(entity, value);
3903 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
3904 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3906 ir_entity *entity = declaration->v.entity;
3907 ir_initializer_t *irinitializer
3908 = create_ir_initializer(initializer, type);
3910 if (tq & TYPE_QUALIFIER_CONST) {
3911 set_entity_variability(entity, variability_constant);
3913 set_entity_variability(entity, variability_initialized);
3915 set_entity_initializer(entity, irinitializer);
3919 static void create_variable_length_array(declaration_t *declaration)
3921 /* initializers are not allowed for VLAs */
3922 assert(declaration->init.initializer == NULL);
3924 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3925 declaration->v.vla_base = NULL;
3927 /* TODO: record VLA somewhere so we create the free node when we leave
3931 static void allocate_variable_length_array(declaration_t *declaration)
3933 /* initializers are not allowed for VLAs */
3934 assert(declaration->init.initializer == NULL);
3935 assert(get_cur_block() != NULL);
3937 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3938 type_t *type = declaration->type;
3939 ir_type *el_type = get_ir_type(type->array.element_type);
3941 /* make sure size_node is calculated */
3942 get_type_size(type);
3943 ir_node *elems = type->array.size_node;
3944 ir_node *mem = get_store();
3945 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3947 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3948 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3951 assert(declaration->declaration_kind
3952 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3953 declaration->v.vla_base = addr;
3957 * Creates a Firm local variable from a declaration.
3959 static void create_local_variable(declaration_t *declaration)
3961 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3963 bool needs_entity = declaration->address_taken;
3964 type_t *type = skip_typeref(declaration->type);
3966 /* is it a variable length array? */
3967 if (is_type_array(type) && !type->array.size_constant) {
3968 create_variable_length_array(declaration);
3970 } else if (is_type_array(type) || is_type_compound(type)) {
3971 needs_entity = true;
3972 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3973 needs_entity = true;
3977 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3978 create_declaration_entity(declaration,
3979 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3982 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3983 declaration->v.value_number = next_value_number_function;
3984 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3985 ++next_value_number_function;
3989 static void create_local_static_variable(declaration_t *declaration)
3991 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3993 type_t *const type = skip_typeref(declaration->type);
3994 ir_type *const global_type = get_glob_type();
3995 ir_type *const irtype = get_ir_type(type);
3996 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3998 size_t l = strlen(declaration->symbol->string);
3999 char buf[l + sizeof(".%u")];
4000 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
4001 ident *const id = id_unique(buf);
4003 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
4005 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
4006 set_entity_volatility(entity, volatility_is_volatile);
4009 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
4010 declaration->v.entity = entity;
4011 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
4012 set_entity_variability(entity, variability_uninitialized);
4013 set_entity_visibility(entity, visibility_local);
4014 set_entity_allocation(entity, allocation_static);
4016 ir_graph *const old_current_ir_graph = current_ir_graph;
4017 current_ir_graph = get_const_code_irg();
4019 create_declaration_initializer(declaration);
4021 assert(current_ir_graph == get_const_code_irg());
4022 current_ir_graph = old_current_ir_graph;
4027 static void return_statement_to_firm(return_statement_t *statement)
4029 if (get_cur_block() == NULL)
4032 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4033 ir_type *func_irtype = get_ir_type(current_function_decl->type);
4038 if (get_method_n_ress(func_irtype) > 0) {
4039 ir_type *res_type = get_method_res_type(func_irtype, 0);
4041 if (statement->value != NULL) {
4042 ir_node *node = expression_to_firm(statement->value);
4043 node = do_strict_conv(dbgi, node);
4047 if (is_compound_type(res_type)) {
4050 mode = get_type_mode(res_type);
4052 in[0] = new_Unknown(mode);
4056 /* build return_value for its side effects */
4057 if (statement->value != NULL) {
4058 expression_to_firm(statement->value);
4063 ir_node *store = get_store();
4064 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4066 ir_node *end_block = get_irg_end_block(current_ir_graph);
4067 add_immBlock_pred(end_block, ret);
4069 set_cur_block(NULL);
4072 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4074 if (get_cur_block() == NULL)
4077 return expression_to_firm(statement->expression);
4080 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4082 declaration_t *declaration = compound->scope.declarations;
4083 for( ; declaration != NULL; declaration = declaration->next) {
4084 create_local_declaration(declaration);
4087 ir_node *result = NULL;
4088 statement_t *statement = compound->statements;
4089 for( ; statement != NULL; statement = statement->base.next) {
4090 if (statement->base.next == NULL
4091 && statement->kind == STATEMENT_EXPRESSION) {
4092 result = expression_statement_to_firm(
4093 &statement->expression);
4096 statement_to_firm(statement);
4102 static void create_global_variable(declaration_t *declaration)
4106 switch ((storage_class_tag_t)declaration->storage_class) {
4107 case STORAGE_CLASS_STATIC:
4108 vis = visibility_local;
4111 case STORAGE_CLASS_EXTERN:
4112 vis = visibility_external_allocated;
4115 case STORAGE_CLASS_NONE:
4116 vis = visibility_external_visible;
4119 case STORAGE_CLASS_THREAD:
4120 vis = visibility_external_visible;
4123 case STORAGE_CLASS_THREAD_EXTERN:
4124 vis = visibility_external_allocated;
4127 case STORAGE_CLASS_THREAD_STATIC:
4128 vis = visibility_local;
4132 var_type = get_tls_type();
4136 var_type = get_glob_type();
4140 create_declaration_entity(declaration,
4141 DECLARATION_KIND_GLOBAL_VARIABLE,
4143 if (!is_type_function(skip_typeref(declaration->type))) {
4144 set_entity_visibility(declaration->v.entity, vis);
4149 case STORAGE_CLASS_TYPEDEF:
4150 case STORAGE_CLASS_AUTO:
4151 case STORAGE_CLASS_REGISTER:
4152 case STORAGE_CLASS_ENUM_ENTRY:
4155 panic("Invalid storage class for global variable");
4158 static void create_local_declaration(declaration_t *declaration)
4160 if (declaration->namespc != NAMESPACE_NORMAL)
4162 /* construct type */
4163 (void) get_ir_type(declaration->type);
4164 if (declaration->symbol == NULL) {
4168 type_t *type = skip_typeref(declaration->type);
4170 switch ((storage_class_tag_t) declaration->storage_class) {
4171 case STORAGE_CLASS_STATIC:
4172 create_local_static_variable(declaration);
4174 case STORAGE_CLASS_EXTERN:
4175 create_global_variable(declaration);
4176 create_declaration_initializer(declaration);
4178 case STORAGE_CLASS_NONE:
4179 case STORAGE_CLASS_AUTO:
4180 case STORAGE_CLASS_REGISTER:
4181 if (is_type_function(type)) {
4182 if (declaration->init.statement != NULL) {
4183 get_function_entity(declaration);
4184 declaration->declaration_kind = DECLARATION_KIND_INNER_FUNCTION;
4185 enqueue_inner_function(declaration);
4187 get_function_entity(declaration);
4190 create_local_variable(declaration);
4193 case STORAGE_CLASS_ENUM_ENTRY:
4194 /* should already be handled */
4195 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4197 case STORAGE_CLASS_TYPEDEF:
4198 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4200 case STORAGE_CLASS_THREAD:
4201 case STORAGE_CLASS_THREAD_EXTERN:
4202 case STORAGE_CLASS_THREAD_STATIC:
4205 panic("invalid storage class found");
4208 static void initialize_local_declaration(declaration_t *declaration)
4210 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4213 switch ((declaration_kind_t) declaration->declaration_kind) {
4214 case DECLARATION_KIND_LOCAL_VARIABLE:
4215 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4216 create_declaration_initializer(declaration);
4219 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4220 allocate_variable_length_array(declaration);
4223 case DECLARATION_KIND_LABEL_BLOCK:
4224 case DECLARATION_KIND_COMPOUND_MEMBER:
4225 case DECLARATION_KIND_GLOBAL_VARIABLE:
4226 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4227 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4228 case DECLARATION_KIND_FUNCTION:
4229 case DECLARATION_KIND_TYPE:
4230 case DECLARATION_KIND_ENUM_ENTRY:
4231 case DECLARATION_KIND_INNER_FUNCTION:
4234 case DECLARATION_KIND_UNKNOWN:
4235 panic("can't initialize unknown declaration");
4237 panic("invalid declaration kind");
4240 static void declaration_statement_to_firm(declaration_statement_t *statement)
4242 declaration_t *declaration = statement->declarations_begin;
4243 declaration_t *end = statement->declarations_end->next;
4244 for( ; declaration != end; declaration = declaration->next) {
4245 if (declaration->namespc != NAMESPACE_NORMAL)
4247 initialize_local_declaration(declaration);
4251 static void if_statement_to_firm(if_statement_t *statement)
4253 ir_node *cur_block = get_cur_block();
4255 ir_node *fallthrough_block = NULL;
4257 /* the true (blocks) */
4258 ir_node *true_block = NULL;
4259 if (statement->true_statement != NULL) {
4260 true_block = new_immBlock();
4261 statement_to_firm(statement->true_statement);
4262 if (get_cur_block() != NULL) {
4263 ir_node *jmp = new_Jmp();
4264 if (fallthrough_block == NULL)
4265 fallthrough_block = new_immBlock();
4266 add_immBlock_pred(fallthrough_block, jmp);
4270 /* the false (blocks) */
4271 ir_node *false_block = NULL;
4272 if (statement->false_statement != NULL) {
4273 false_block = new_immBlock();
4275 statement_to_firm(statement->false_statement);
4276 if (get_cur_block() != NULL) {
4277 ir_node *jmp = new_Jmp();
4278 if (fallthrough_block == NULL)
4279 fallthrough_block = new_immBlock();
4280 add_immBlock_pred(fallthrough_block, jmp);
4284 /* create the condition */
4285 if (cur_block != NULL) {
4286 if (true_block == NULL || false_block == NULL) {
4287 if (fallthrough_block == NULL)
4288 fallthrough_block = new_immBlock();
4289 if (true_block == NULL)
4290 true_block = fallthrough_block;
4291 if (false_block == NULL)
4292 false_block = fallthrough_block;
4295 set_cur_block(cur_block);
4296 create_condition_evaluation(statement->condition, true_block,
4300 mature_immBlock(true_block);
4301 if (false_block != fallthrough_block && false_block != NULL) {
4302 mature_immBlock(false_block);
4304 if (fallthrough_block != NULL) {
4305 mature_immBlock(fallthrough_block);
4308 set_cur_block(fallthrough_block);
4311 static void while_statement_to_firm(while_statement_t *statement)
4313 ir_node *jmp = NULL;
4314 if (get_cur_block() != NULL) {
4318 /* create the header block */
4319 ir_node *header_block = new_immBlock();
4321 add_immBlock_pred(header_block, jmp);
4325 ir_node *old_continue_label = continue_label;
4326 ir_node *old_break_label = break_label;
4327 continue_label = header_block;
4330 ir_node *body_block = new_immBlock();
4331 statement_to_firm(statement->body);
4332 ir_node *false_block = break_label;
4334 assert(continue_label == header_block);
4335 continue_label = old_continue_label;
4336 break_label = old_break_label;
4338 if (get_cur_block() != NULL) {
4340 add_immBlock_pred(header_block, jmp);
4343 /* shortcut for while(true) */
4344 if (is_constant_expression(statement->condition)
4345 && fold_constant(statement->condition) != 0) {
4346 set_cur_block(header_block);
4347 ir_node *header_jmp = new_Jmp();
4348 add_immBlock_pred(body_block, header_jmp);
4350 keep_alive(body_block);
4351 keep_all_memory(body_block);
4352 set_cur_block(body_block);
4354 if (false_block == NULL) {
4355 false_block = new_immBlock();
4358 /* create the condition */
4359 set_cur_block(header_block);
4361 create_condition_evaluation(statement->condition, body_block,
4365 mature_immBlock(body_block);
4366 mature_immBlock(header_block);
4367 if (false_block != NULL) {
4368 mature_immBlock(false_block);
4371 set_cur_block(false_block);
4374 static void do_while_statement_to_firm(do_while_statement_t *statement)
4376 ir_node *jmp = NULL;
4377 if (get_cur_block() != NULL) {
4381 /* create the header block */
4382 ir_node *header_block = new_immBlock();
4385 ir_node *body_block = new_immBlock();
4387 add_immBlock_pred(body_block, jmp);
4390 ir_node *old_continue_label = continue_label;
4391 ir_node *old_break_label = break_label;
4392 continue_label = header_block;
4395 statement_to_firm(statement->body);
4396 ir_node *false_block = break_label;
4398 assert(continue_label == header_block);
4399 continue_label = old_continue_label;
4400 break_label = old_break_label;
4402 if (get_cur_block() != NULL) {
4403 ir_node *body_jmp = new_Jmp();
4404 add_immBlock_pred(header_block, body_jmp);
4405 mature_immBlock(header_block);
4408 if (false_block == NULL) {
4409 false_block = new_immBlock();
4412 /* create the condition */
4413 set_cur_block(header_block);
4415 create_condition_evaluation(statement->condition, body_block, false_block);
4416 mature_immBlock(body_block);
4417 mature_immBlock(header_block);
4418 if (false_block != NULL) {
4419 mature_immBlock(false_block);
4422 set_cur_block(false_block);
4425 static void for_statement_to_firm(for_statement_t *statement)
4427 ir_node *jmp = NULL;
4429 /* create declarations */
4430 declaration_t *declaration = statement->scope.declarations;
4431 for( ; declaration != NULL; declaration = declaration->next) {
4432 create_local_declaration(declaration);
4435 if (get_cur_block() != NULL) {
4436 declaration = statement->scope.declarations;
4437 for( ; declaration != NULL; declaration = declaration->next) {
4438 initialize_local_declaration(declaration);
4441 if (statement->initialisation != NULL) {
4442 expression_to_firm(statement->initialisation);
4449 /* create the step block */
4450 ir_node *const step_block = new_immBlock();
4451 if (statement->step != NULL) {
4452 expression_to_firm(statement->step);
4454 ir_node *const step_jmp = new_Jmp();
4456 /* create the header block */
4457 ir_node *const header_block = new_immBlock();
4459 add_immBlock_pred(header_block, jmp);
4461 add_immBlock_pred(header_block, step_jmp);
4463 /* the false block */
4464 ir_node *const false_block = new_immBlock();
4467 ir_node * body_block;
4468 if (statement->body != NULL) {
4469 ir_node *const old_continue_label = continue_label;
4470 ir_node *const old_break_label = break_label;
4471 continue_label = step_block;
4472 break_label = false_block;
4474 body_block = new_immBlock();
4475 statement_to_firm(statement->body);
4477 assert(continue_label == step_block);
4478 assert(break_label == false_block);
4479 continue_label = old_continue_label;
4480 break_label = old_break_label;
4482 if (get_cur_block() != NULL) {
4484 add_immBlock_pred(step_block, jmp);
4487 body_block = step_block;
4490 /* create the condition */
4491 set_cur_block(header_block);
4492 if (statement->condition != NULL) {
4493 create_condition_evaluation(statement->condition, body_block,
4496 keep_alive(header_block);
4497 keep_all_memory(header_block);
4499 add_immBlock_pred(body_block, jmp);
4502 mature_immBlock(body_block);
4503 mature_immBlock(false_block);
4504 mature_immBlock(step_block);
4505 mature_immBlock(header_block);
4506 mature_immBlock(false_block);
4508 set_cur_block(false_block);
4511 static void create_jump_statement(const statement_t *statement,
4512 ir_node *target_block)
4514 if (get_cur_block() == NULL)
4517 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4518 ir_node *jump = new_d_Jmp(dbgi);
4519 add_immBlock_pred(target_block, jump);
4521 set_cur_block(NULL);
4524 static ir_node *get_break_label(void)
4526 if (break_label == NULL) {
4527 ir_node *cur_block = get_cur_block();
4528 break_label = new_immBlock();
4529 set_cur_block(cur_block);
4534 static void switch_statement_to_firm(switch_statement_t *statement)
4536 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4538 ir_node *expression = expression_to_firm(statement->expression);
4539 ir_node *cond = new_d_Cond(dbgi, expression);
4541 set_cur_block(NULL);
4543 ir_node *const old_switch_cond = current_switch_cond;
4544 ir_node *const old_break_label = break_label;
4545 const bool old_saw_default_label = saw_default_label;
4546 saw_default_label = false;
4547 current_switch_cond = cond;
4549 switch_statement_t *const old_switch = current_switch;
4550 current_switch = statement;
4552 /* determine a free number for the default label */
4553 unsigned long num_cases = 0;
4555 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4556 if (l->expression == NULL) {
4560 if (l->last_case >= l->first_case)
4561 num_cases += l->last_case - l->first_case;
4562 if (l->last_case > def_nr)
4563 def_nr = l->last_case;
4566 if (def_nr + 1 < 0) {
4567 /* Bad: an overflow occurred, we cannot be sure that the
4568 * maximum + 1 is a free number. Scan the values a second
4569 * time to find a free number.
4571 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
4574 memset(bits, 0, (num_cases + 7) >> 3);
4575 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4576 if (l->expression == NULL) {
4580 for (long cns = l->first_case; cns <= l->last_case; ++cns) {
4581 if (cns >= 0 && (unsigned long)cns < num_cases)
4582 bits[cns >> 3] |= (1 << (cns & 7));
4585 /* We look at the first num_cases constants:
4586 * Either they are densed, so we took the last (num_cases)
4587 * one, or they are non densed, so we will find one free
4590 for (i = 0; i < num_cases; ++i)
4591 if ((bits[i >> 3] & (i & 7)) == 0)
4599 statement->default_proj_nr = def_nr;
4601 if (statement->body != NULL) {
4602 statement_to_firm(statement->body);
4605 if (get_cur_block() != NULL) {
4606 ir_node *jmp = new_Jmp();
4607 add_immBlock_pred(get_break_label(), jmp);
4610 if (!saw_default_label) {
4611 set_cur_block(get_nodes_block(cond));
4612 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4613 statement->default_proj_nr);
4614 add_immBlock_pred(get_break_label(), proj);
4617 if (break_label != NULL) {
4618 mature_immBlock(break_label);
4620 set_cur_block(break_label);
4622 assert(current_switch_cond == cond);
4623 current_switch = old_switch;
4624 current_switch_cond = old_switch_cond;
4625 break_label = old_break_label;
4626 saw_default_label = old_saw_default_label;
4629 static void case_label_to_firm(const case_label_statement_t *statement)
4631 if (statement->is_empty_range)
4634 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4636 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4639 ir_node *old_block = get_nodes_block(current_switch_cond);
4640 ir_node *block = new_immBlock();
4642 set_cur_block(old_block);
4643 if (statement->expression != NULL) {
4644 long pn = statement->first_case;
4645 long end_pn = statement->last_case;
4646 assert(pn <= end_pn);
4647 /* create jumps for all cases in the given range */
4649 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4650 add_immBlock_pred(block, proj);
4651 } while(pn++ < end_pn);
4653 saw_default_label = true;
4654 proj = new_d_defaultProj(dbgi, current_switch_cond,
4655 current_switch->default_proj_nr);
4657 add_immBlock_pred(block, proj);
4660 if (fallthrough != NULL) {
4661 add_immBlock_pred(block, fallthrough);
4663 mature_immBlock(block);
4664 set_cur_block(block);
4666 if (statement->statement != NULL) {
4667 statement_to_firm(statement->statement);
4671 static void label_to_firm(const label_statement_t *statement)
4673 ir_node *block = get_label_block(statement->label);
4675 if (get_cur_block() != NULL) {
4676 ir_node *jmp = new_Jmp();
4677 add_immBlock_pred(block, jmp);
4680 set_cur_block(block);
4682 keep_all_memory(block);
4684 if (statement->statement != NULL) {
4685 statement_to_firm(statement->statement);
4689 static void goto_to_firm(const goto_statement_t *statement)
4691 if (get_cur_block() == NULL)
4694 if (statement->expression) {
4695 ir_node *irn = expression_to_firm(statement->expression);
4696 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4697 ir_node *ijmp = new_d_IJmp(dbgi, irn);
4699 set_irn_link(ijmp, ijmp_list);
4702 if (statement->outer_fkt_jmp) {
4703 /* TODO: this is a outer function jmp */
4704 panic("outer function jump not implemented");
4706 ir_node *block = get_label_block(statement->label);
4707 ir_node *jmp = new_Jmp();
4708 add_immBlock_pred(block, jmp);
4711 set_cur_block(NULL);
4714 static void asm_statement_to_firm(const asm_statement_t *statement)
4716 bool needs_memory = false;
4718 if (statement->is_volatile) {
4719 needs_memory = true;
4722 size_t n_clobbers = 0;
4723 asm_clobber_t *clobber = statement->clobbers;
4724 for( ; clobber != NULL; clobber = clobber->next) {
4725 const char *clobber_str = clobber->clobber.begin;
4727 if (!be_is_valid_clobber(clobber_str)) {
4728 errorf(&statement->base.source_position,
4729 "invalid clobber '%s' specified", clobber->clobber);
4733 if (strcmp(clobber_str, "memory") == 0) {
4734 needs_memory = true;
4738 ident *id = new_id_from_str(clobber_str);
4739 obstack_ptr_grow(&asm_obst, id);
4742 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4743 ident **clobbers = NULL;
4744 if (n_clobbers > 0) {
4745 clobbers = obstack_finish(&asm_obst);
4748 size_t n_inputs = 0;
4749 asm_argument_t *argument = statement->inputs;
4750 for ( ; argument != NULL; argument = argument->next)
4752 size_t n_outputs = 0;
4753 argument = statement->outputs;
4754 for ( ; argument != NULL; argument = argument->next)
4757 unsigned next_pos = 0;
4759 ir_node *ins[n_inputs + n_outputs + 1];
4762 ir_asm_constraint tmp_in_constraints[n_outputs];
4764 const expression_t *out_exprs[n_outputs];
4765 ir_node *out_addrs[n_outputs];
4766 size_t out_size = 0;
4768 argument = statement->outputs;
4769 for ( ; argument != NULL; argument = argument->next) {
4770 const char *constraints = argument->constraints.begin;
4771 asm_constraint_flags_t asm_flags
4772 = be_parse_asm_constraints(constraints);
4774 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4775 errorf(&statement->base.source_position,
4776 "some constraints in '%s' are not supported", constraints);
4779 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4780 errorf(&statement->base.source_position,
4781 "some constraints in '%s' are invalid", constraints);
4784 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4785 errorf(&statement->base.source_position,
4786 "no write flag specified for output constraints '%s'",
4791 unsigned pos = next_pos++;
4792 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4793 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4794 expression_t *expr = argument->expression;
4795 ir_node *addr = expression_to_addr(expr);
4796 /* in+output, construct an artifical same_as constraint on the
4798 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4800 ir_node *value = get_value_from_lvalue(expr, addr);
4802 snprintf(buf, sizeof(buf), "%d", pos);
4804 ir_asm_constraint constraint;
4805 constraint.pos = pos;
4806 constraint.constraint = new_id_from_str(buf);
4807 constraint.mode = get_ir_mode(expr->base.type);
4808 tmp_in_constraints[in_size] = constraint;
4809 ins[in_size] = value;
4814 out_exprs[out_size] = expr;
4815 out_addrs[out_size] = addr;
4817 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4818 /* pure memory ops need no input (but we have to make sure we
4819 * attach to the memory) */
4820 assert(! (asm_flags &
4821 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4822 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4823 needs_memory = true;
4825 /* we need to attach the address to the inputs */
4826 expression_t *expr = argument->expression;
4828 ir_asm_constraint constraint;
4829 constraint.pos = pos;
4830 constraint.constraint = new_id_from_str(constraints);
4831 constraint.mode = NULL;
4832 tmp_in_constraints[in_size] = constraint;
4834 ins[in_size] = expression_to_addr(expr);
4838 errorf(&statement->base.source_position,
4839 "only modifiers but no place set in constraints '%s'",
4844 ir_asm_constraint constraint;
4845 constraint.pos = pos;
4846 constraint.constraint = new_id_from_str(constraints);
4847 constraint.mode = get_ir_mode(argument->expression->base.type);
4849 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4851 assert(obstack_object_size(&asm_obst)
4852 == out_size * sizeof(ir_asm_constraint));
4853 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4856 obstack_grow(&asm_obst, tmp_in_constraints,
4857 in_size * sizeof(tmp_in_constraints[0]));
4858 /* find and count input and output arguments */
4859 argument = statement->inputs;
4860 for( ; argument != NULL; argument = argument->next) {
4861 const char *constraints = argument->constraints.begin;
4862 asm_constraint_flags_t asm_flags
4863 = be_parse_asm_constraints(constraints);
4865 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4866 errorf(&statement->base.source_position,
4867 "some constraints in '%s' are not supported", constraints);
4870 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4871 errorf(&statement->base.source_position,
4872 "some constraints in '%s' are invalid", constraints);
4875 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4876 errorf(&statement->base.source_position,
4877 "write flag specified for input constraints '%s'",
4883 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4884 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4885 /* we can treat this as "normal" input */
4886 input = expression_to_firm(argument->expression);
4887 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4888 /* pure memory ops need no input (but we have to make sure we
4889 * attach to the memory) */
4890 assert(! (asm_flags &
4891 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4892 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4893 needs_memory = true;
4894 input = expression_to_addr(argument->expression);
4896 errorf(&statement->base.source_position,
4897 "only modifiers but no place set in constraints '%s'",
4902 ir_asm_constraint constraint;
4903 constraint.pos = next_pos++;
4904 constraint.constraint = new_id_from_str(constraints);
4905 constraint.mode = get_irn_mode(input);
4907 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4908 ins[in_size++] = input;
4912 ir_asm_constraint constraint;
4913 constraint.pos = next_pos++;
4914 constraint.constraint = new_id_from_str("");
4915 constraint.mode = mode_M;
4917 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4918 ins[in_size++] = get_store();
4921 assert(obstack_object_size(&asm_obst)
4922 == in_size * sizeof(ir_asm_constraint));
4923 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4925 /* create asm node */
4926 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4928 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4930 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4931 out_size, output_constraints,
4932 n_clobbers, clobbers, asm_text);
4934 if (statement->is_volatile) {
4935 set_irn_pinned(node, op_pin_state_pinned);
4937 set_irn_pinned(node, op_pin_state_floats);
4940 /* create output projs & connect them */
4942 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4947 for (i = 0; i < out_size; ++i) {
4948 const expression_t *out_expr = out_exprs[i];
4950 ir_mode *mode = get_ir_mode(out_expr->base.type);
4951 ir_node *proj = new_Proj(node, mode, pn);
4952 ir_node *addr = out_addrs[i];
4954 set_value_for_expression_addr(out_expr, proj, addr);
4958 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4959 statement_to_firm(statement->try_statement);
4960 warningf(&statement->base.source_position, "structured exception handling ignored");
4963 static void leave_statement_to_firm(leave_statement_t *statement) {
4964 errorf(&statement->base.source_position, "__leave not supported yet");
4968 * Transform a statement.
4970 static void statement_to_firm(statement_t *statement)
4973 assert(!statement->base.transformed);
4974 statement->base.transformed = true;
4977 switch(statement->kind) {
4978 case STATEMENT_INVALID:
4979 panic("invalid statement found");
4981 case STATEMENT_EMPTY:
4984 case STATEMENT_COMPOUND:
4985 compound_statement_to_firm(&statement->compound);
4987 case STATEMENT_RETURN:
4988 return_statement_to_firm(&statement->returns);
4990 case STATEMENT_EXPRESSION:
4991 expression_statement_to_firm(&statement->expression);
4994 if_statement_to_firm(&statement->ifs);
4996 case STATEMENT_WHILE:
4997 while_statement_to_firm(&statement->whiles);
4999 case STATEMENT_DO_WHILE:
5000 do_while_statement_to_firm(&statement->do_while);
5002 case STATEMENT_DECLARATION:
5003 declaration_statement_to_firm(&statement->declaration);
5005 case STATEMENT_BREAK:
5006 create_jump_statement(statement, get_break_label());
5008 case STATEMENT_CONTINUE:
5009 create_jump_statement(statement, continue_label);
5011 case STATEMENT_SWITCH:
5012 switch_statement_to_firm(&statement->switchs);
5014 case STATEMENT_CASE_LABEL:
5015 case_label_to_firm(&statement->case_label);
5018 for_statement_to_firm(&statement->fors);
5020 case STATEMENT_LABEL:
5021 label_to_firm(&statement->label);
5023 case STATEMENT_GOTO:
5024 goto_to_firm(&statement->gotos);
5027 asm_statement_to_firm(&statement->asms);
5029 case STATEMENT_MS_TRY:
5030 ms_try_statement_to_firm(&statement->ms_try);
5032 case STATEMENT_LEAVE:
5033 leave_statement_to_firm(&statement->leave);
5036 panic("Statement not implemented\n");
5039 static int count_decls_in_expression(const expression_t *expression);
5041 static int count_local_declarations(const declaration_t * decl,
5042 const declaration_t *const end)
5045 for (; decl != end; decl = decl->next) {
5046 if (decl->namespc != NAMESPACE_NORMAL)
5048 type_t *type = skip_typeref(decl->type);
5050 if (!decl->address_taken && is_type_scalar(type))
5052 const initializer_t *initializer = decl->init.initializer;
5053 /* FIXME: should walk initializer hierarchies... */
5054 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
5055 count += count_decls_in_expression(initializer->value.value);
5061 static int count_decls_in_expression(const expression_t *expression) {
5064 if (expression == NULL)
5067 switch((expression_kind_t) expression->base.kind) {
5068 case EXPR_STATEMENT:
5069 return count_decls_in_stmts(expression->statement.statement);
5071 int count_left = count_decls_in_expression(expression->binary.left);
5072 int count_right = count_decls_in_expression(expression->binary.right);
5073 return count_left + count_right;
5076 return count_decls_in_expression(expression->unary.value);
5078 call_argument_t *argument = expression->call.arguments;
5079 for( ; argument != NULL; argument = argument->next) {
5080 count += count_decls_in_expression(argument->expression);
5087 panic("unexpected expression kind");
5089 case EXPR_COMPOUND_LITERAL:
5093 case EXPR_CONDITIONAL:
5094 count += count_decls_in_expression(expression->conditional.condition);
5095 count += count_decls_in_expression(expression->conditional.true_expression);
5096 count += count_decls_in_expression(expression->conditional.false_expression);
5099 case EXPR_BUILTIN_PREFETCH:
5100 count += count_decls_in_expression(expression->builtin_prefetch.adr);
5101 count += count_decls_in_expression(expression->builtin_prefetch.rw);
5102 count += count_decls_in_expression(expression->builtin_prefetch.locality);
5105 case EXPR_BUILTIN_CONSTANT_P:
5106 count += count_decls_in_expression(expression->builtin_constant.value);
5110 count += count_decls_in_expression(expression->select.compound);
5113 case EXPR_ARRAY_ACCESS:
5114 count += count_decls_in_expression(expression->array_access.array_ref);
5115 count += count_decls_in_expression(expression->array_access.index);
5118 case EXPR_CLASSIFY_TYPE:
5119 count += count_decls_in_expression(expression->classify_type.type_expression);
5123 case EXPR_ALIGNOF: {
5124 expression_t *tp_expression = expression->typeprop.tp_expression;
5125 if (tp_expression != NULL) {
5126 count += count_decls_in_expression(tp_expression);
5132 case EXPR_REFERENCE:
5134 case EXPR_CHARACTER_CONSTANT:
5135 case EXPR_WIDE_CHARACTER_CONSTANT:
5136 case EXPR_STRING_LITERAL:
5137 case EXPR_WIDE_STRING_LITERAL:
5139 case EXPR_BUILTIN_SYMBOL:
5142 case EXPR_LABEL_ADDRESS:
5146 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
5147 * (or implement all the missing expressions here/implement a walker)
5153 static int count_decls_in_stmts(const statement_t *stmt)
5156 for (; stmt != NULL; stmt = stmt->base.next) {
5157 switch (stmt->kind) {
5158 case STATEMENT_EMPTY:
5161 case STATEMENT_DECLARATION: {
5162 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5163 count += count_local_declarations(decl_stmt->declarations_begin,
5164 decl_stmt->declarations_end->next);
5168 case STATEMENT_COMPOUND: {
5169 const compound_statement_t *const comp =
5171 count += count_decls_in_stmts(comp->statements);
5175 case STATEMENT_IF: {
5176 const if_statement_t *const if_stmt = &stmt->ifs;
5177 count += count_decls_in_expression(if_stmt->condition);
5178 count += count_decls_in_stmts(if_stmt->true_statement);
5179 count += count_decls_in_stmts(if_stmt->false_statement);
5183 case STATEMENT_SWITCH: {
5184 const switch_statement_t *const switch_stmt = &stmt->switchs;
5185 count += count_decls_in_expression(switch_stmt->expression);
5186 count += count_decls_in_stmts(switch_stmt->body);
5190 case STATEMENT_LABEL: {
5191 const label_statement_t *const label_stmt = &stmt->label;
5192 if (label_stmt->statement != NULL) {
5193 count += count_decls_in_stmts(label_stmt->statement);
5198 case STATEMENT_WHILE: {
5199 const while_statement_t *const while_stmt = &stmt->whiles;
5200 count += count_decls_in_expression(while_stmt->condition);
5201 count += count_decls_in_stmts(while_stmt->body);
5205 case STATEMENT_DO_WHILE: {
5206 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5207 count += count_decls_in_expression(do_while_stmt->condition);
5208 count += count_decls_in_stmts(do_while_stmt->body);
5212 case STATEMENT_FOR: {
5213 const for_statement_t *const for_stmt = &stmt->fors;
5214 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5215 count += count_decls_in_expression(for_stmt->initialisation);
5216 count += count_decls_in_expression(for_stmt->condition);
5217 count += count_decls_in_expression(for_stmt->step);
5218 count += count_decls_in_stmts(for_stmt->body);
5222 case STATEMENT_CASE_LABEL: {
5223 const case_label_statement_t *label = &stmt->case_label;
5224 count += count_decls_in_expression(label->expression);
5225 if (label->statement != NULL) {
5226 count += count_decls_in_stmts(label->statement);
5232 case STATEMENT_BREAK:
5233 case STATEMENT_CONTINUE:
5236 case STATEMENT_EXPRESSION: {
5237 const expression_statement_t *expr_stmt = &stmt->expression;
5238 count += count_decls_in_expression(expr_stmt->expression);
5242 case STATEMENT_GOTO:
5243 case STATEMENT_LEAVE:
5244 case STATEMENT_INVALID:
5247 case STATEMENT_RETURN: {
5248 const return_statement_t *ret_stmt = &stmt->returns;
5249 count += count_decls_in_expression(ret_stmt->value);
5253 case STATEMENT_MS_TRY: {
5254 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5255 count += count_decls_in_stmts(try_stmt->try_statement);
5256 if (try_stmt->except_expression != NULL)
5257 count += count_decls_in_expression(try_stmt->except_expression);
5258 count += count_decls_in_stmts(try_stmt->final_statement);
5266 static int get_function_n_local_vars(declaration_t *declaration)
5270 /* count parameters */
5271 count += count_local_declarations(declaration->scope.declarations, NULL);
5273 /* count local variables declared in body */
5274 count += count_decls_in_stmts(declaration->init.statement);
5279 static void initialize_function_parameters(declaration_t *declaration)
5281 ir_graph *irg = current_ir_graph;
5282 ir_node *args = get_irg_args(irg);
5283 ir_node *start_block = get_irg_start_block(irg);
5284 ir_type *function_irtype = get_ir_type(declaration->type);
5287 declaration_t *parameter = declaration->scope.declarations;
5288 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5289 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5290 type_t *type = skip_typeref(parameter->type);
5292 bool needs_entity = parameter->address_taken;
5293 assert(!is_type_array(type));
5294 if (is_type_compound(type)) {
5295 needs_entity = true;
5299 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5300 ident *id = new_id_from_str(parameter->symbol->string);
5301 set_entity_ident(entity, id);
5303 parameter->declaration_kind
5304 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5305 parameter->v.entity = entity;
5309 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5310 ir_mode *param_mode = get_type_mode(param_irtype);
5313 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5315 ir_mode *mode = get_ir_mode(type);
5316 value = create_conv(NULL, value, mode);
5317 value = do_strict_conv(NULL, value);
5319 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5320 parameter->v.value_number = next_value_number_function;
5321 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5322 ++next_value_number_function;
5324 set_value(parameter->v.value_number, value);
5329 * Handle additional decl modifiers for IR-graphs
5331 * @param irg the IR-graph
5332 * @param dec_modifiers additional modifiers
5334 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5336 if (decl_modifiers & DM_NORETURN) {
5337 /* TRUE if the declaration includes the Microsoft
5338 __declspec(noreturn) specifier. */
5339 set_irg_additional_property(irg, mtp_property_noreturn);
5341 if (decl_modifiers & DM_NOTHROW) {
5342 /* TRUE if the declaration includes the Microsoft
5343 __declspec(nothrow) specifier. */
5344 set_irg_additional_property(irg, mtp_property_nothrow);
5346 if (decl_modifiers & DM_NAKED) {
5347 /* TRUE if the declaration includes the Microsoft
5348 __declspec(naked) specifier. */
5349 set_irg_additional_property(irg, mtp_property_naked);
5351 if (decl_modifiers & DM_FORCEINLINE) {
5352 /* TRUE if the declaration includes the
5353 Microsoft __forceinline specifier. */
5354 set_irg_inline_property(irg, irg_inline_forced);
5356 if (decl_modifiers & DM_NOINLINE) {
5357 /* TRUE if the declaration includes the Microsoft
5358 __declspec(noinline) specifier. */
5359 set_irg_inline_property(irg, irg_inline_forbidden);
5363 static void add_function_pointer(ir_type *segment, ir_entity *method,
5364 const char *unique_template)
5366 ir_type *method_type = get_entity_type(method);
5367 ident *id = id_unique(unique_template);
5368 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5370 ident *ide = id_unique(unique_template);
5371 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5372 ir_graph *irg = get_const_code_irg();
5373 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5376 set_entity_compiler_generated(ptr, 1);
5377 set_entity_variability(ptr, variability_constant);
5378 set_atomic_ent_value(ptr, val);
5382 * Generate possible IJmp branches to a given label block.
5384 static void gen_ijmp_branches(ir_node *block) {
5386 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5387 add_immBlock_pred(block, ijmp);
5392 * Create code for a function.
5394 static void create_function(declaration_t *declaration)
5396 ir_entity *function_entity = get_function_entity(declaration);
5398 if (declaration->init.statement == NULL)
5401 if (declaration->modifiers & DM_CONSTRUCTOR) {
5402 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5403 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5405 if (declaration->modifiers & DM_DESTRUCTOR) {
5406 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5407 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5410 current_function_decl = declaration;
5411 current_function_name = NULL;
5412 current_funcsig = NULL;
5414 assert(all_labels == NULL);
5415 all_labels = NEW_ARR_F(declaration_t *, 0);
5418 int n_local_vars = get_function_n_local_vars(declaration);
5419 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5421 ir_graph *old_current_function = current_function;
5422 current_function = irg;
5424 set_irg_fp_model(irg, firm_opt.fp_model);
5425 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5426 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5428 ir_node *first_block = get_cur_block();
5430 /* set inline flags */
5431 if (declaration->is_inline)
5432 set_irg_inline_property(irg, irg_inline_recomended);
5433 handle_decl_modifier_irg(irg, declaration->modifiers);
5435 next_value_number_function = 0;
5436 initialize_function_parameters(declaration);
5438 statement_to_firm(declaration->init.statement);
5440 ir_node *end_block = get_irg_end_block(irg);
5442 /* do we have a return statement yet? */
5443 if (get_cur_block() != NULL) {
5444 type_t *type = skip_typeref(declaration->type);
5445 assert(is_type_function(type));
5446 const function_type_t *func_type = &type->function;
5447 const type_t *return_type
5448 = skip_typeref(func_type->return_type);
5451 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5452 ret = new_Return(get_store(), 0, NULL);
5455 if (is_type_scalar(return_type)) {
5456 mode = get_ir_mode(func_type->return_type);
5462 /* ยง5.1.2.2.3 main implicitly returns 0 */
5463 if (strcmp(declaration->symbol->string, "main") == 0) {
5464 in[0] = new_Const(mode, get_mode_null(mode));
5466 in[0] = new_Unknown(mode);
5468 ret = new_Return(get_store(), 1, in);
5470 add_immBlock_pred(end_block, ret);
5473 bool has_computed_gotos = false;
5474 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5475 declaration_t *label = all_labels[i];
5476 if (label->address_taken) {
5477 gen_ijmp_branches(label->v.block);
5478 has_computed_gotos = true;
5480 mature_immBlock(label->v.block);
5482 if (has_computed_gotos) {
5483 /* if we have computed goto's in the function, we cannot inline it */
5484 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5485 warningf(&declaration->source_position,
5486 "function '%Y' can never be inlined because it contains a computed goto",
5487 declaration->symbol);
5489 set_irg_inline_property(irg, irg_inline_forbidden);
5492 DEL_ARR_F(all_labels);
5495 mature_immBlock(first_block);
5496 mature_immBlock(end_block);
5498 irg_finalize_cons(irg);
5500 /* finalize the frame type */
5501 ir_type *frame_type = get_irg_frame_type(irg);
5502 int n = get_compound_n_members(frame_type);
5505 for(int i = 0; i < n; ++i) {
5506 ir_entity *entity = get_compound_member(frame_type, i);
5507 ir_type *entity_type = get_entity_type(entity);
5509 int align = get_type_alignment_bytes(entity_type);
5510 if (align > align_all)
5514 misalign = offset % align;
5516 offset += align - misalign;
5520 set_entity_offset(entity, offset);
5521 offset += get_type_size_bytes(entity_type);
5523 set_type_size_bytes(frame_type, offset);
5524 set_type_alignment_bytes(frame_type, align_all);
5527 current_function = old_current_function;
5530 static void scope_to_firm(scope_t *scope)
5532 /* first pass: create declarations */
5533 declaration_t *declaration = scope->declarations;
5534 for( ; declaration != NULL; declaration = declaration->next) {
5535 if (declaration->namespc != NAMESPACE_NORMAL)
5537 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5538 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5540 if (declaration->symbol == NULL)
5543 type_t *type = skip_typeref(declaration->type);
5544 if (is_type_function(type)) {
5545 get_function_entity(declaration);
5547 create_global_variable(declaration);
5551 /* second pass: create code/initializers */
5552 declaration = scope->declarations;
5553 for( ; declaration != NULL; declaration = declaration->next) {
5554 if (declaration->namespc != NAMESPACE_NORMAL)
5556 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5557 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5559 if (declaration->symbol == NULL)
5562 type_t *type = declaration->type;
5563 if (type->kind == TYPE_FUNCTION) {
5564 create_function(declaration);
5565 declaration_t *inner;
5566 for (inner = next_inner_function(); inner != NULL;
5567 inner = next_inner_function())
5568 create_function(inner);
5570 assert(declaration->declaration_kind
5571 == DECLARATION_KIND_GLOBAL_VARIABLE);
5572 current_ir_graph = get_const_code_irg();
5573 create_declaration_initializer(declaration);
5578 static void set_be_option(const char *option)
5580 int res = firm_be_option(option);
5584 void init_ast2firm(void)
5586 obstack_init(&asm_obst);
5587 init_atomic_modes();
5589 id_underscore = new_id_from_chars("_", 1);
5590 id_imp = new_id_from_chars("__imp_", 6);
5592 /* OS option must be set to the backend */
5593 switch (firm_opt.os_support) {
5594 case OS_SUPPORT_MINGW:
5595 create_ld_ident = create_ld_ident_win32;
5596 set_be_option("ia32-gasmode=mingw");
5598 case OS_SUPPORT_LINUX:
5599 create_ld_ident = create_ld_ident_linux_elf;
5600 set_be_option("ia32-gasmode=elf");
5602 case OS_SUPPORT_MACHO:
5603 create_ld_ident = create_ld_ident_macho;
5604 set_be_option("ia32-gasmode=macho");
5605 set_be_option("ia32-stackalign=4");
5606 set_be_option("pic");
5610 /* create idents for all known runtime functions */
5611 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5612 rts_idents[i] = new_id_from_str(rts_data[i].name);
5615 entitymap_init(&entitymap);
5618 static void init_ir_types(void)
5620 static int ir_types_initialized = 0;
5621 if (ir_types_initialized)
5623 ir_types_initialized = 1;
5625 ir_type_int = get_ir_type(type_int);
5626 ir_type_const_char = get_ir_type(type_const_char);
5627 ir_type_wchar_t = get_ir_type(type_wchar_t);
5628 ir_type_void = get_ir_type(type_void);
5631 void exit_ast2firm(void)
5633 entitymap_destroy(&entitymap);
5634 obstack_free(&asm_obst, NULL);
5637 static void global_asm_to_firm(statement_t *s)
5639 for (; s != NULL; s = s->base.next) {
5640 assert(s->kind == STATEMENT_ASM);
5642 char const *const text = s->asms.asm_text.begin;
5643 size_t size = s->asms.asm_text.size;
5645 /* skip the last \0 */
5646 if (text[size - 1] == '\0')
5649 ident *const id = new_id_from_chars(text, size);
5654 void translation_unit_to_firm(translation_unit_t *unit)
5656 /* just to be sure */
5657 continue_label = NULL;
5659 current_switch_cond = NULL;
5663 scope_to_firm(&unit->scope);
5664 global_asm_to_firm(unit->global_asm);
5666 current_ir_graph = NULL;