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 static const declaration_t *current_function_decl;
68 static ir_node *current_function_name;
69 static ir_node *current_funcsig;
70 static switch_statement_t *current_switch;
71 static ir_graph *current_function;
73 static entitymap_t entitymap;
75 static struct obstack asm_obst;
77 typedef enum declaration_kind_t {
78 DECLARATION_KIND_UNKNOWN,
79 DECLARATION_KIND_FUNCTION,
80 DECLARATION_KIND_VARIABLE_LENGTH_ARRAY,
81 DECLARATION_KIND_GLOBAL_VARIABLE,
82 DECLARATION_KIND_LOCAL_VARIABLE,
83 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
84 DECLARATION_KIND_COMPOUND_MEMBER,
85 DECLARATION_KIND_LABEL_BLOCK,
86 DECLARATION_KIND_ENUM_ENTRY,
87 DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE,
88 DECLARATION_KIND_COMPOUND_TYPE_COMPLETE,
89 DECLARATION_KIND_TYPE,
90 DECLARATION_KIND_INNER_FUNCTION
93 static ir_type *get_ir_type(type_t *type);
94 static ir_type *get_ir_type_incomplete(type_t *type);
95 static int count_decls_in_stmts(const statement_t *stmt);
97 static void enqueue_inner_function(declaration_t *declaration) {
98 if (inner_functions == NULL) {
99 inner_functions = NEW_ARR_F(declaration_t *, 16);
100 inner_functions[0] = declaration;
101 inner_function_idx = 1;
103 int size = ARR_LEN(inner_functions);
104 if (inner_function_idx >= size) {
105 ARR_RESIZE(declaration_t *, inner_functions, size + 16);
107 inner_functions[inner_function_idx++] = declaration;
111 static declaration_t *next_inner_function(void) {
112 if (inner_function_idx == 0)
114 return inner_functions[--inner_function_idx];
117 ir_node *uninitialized_local_var(ir_graph *irg, ir_mode *mode, int pos)
119 const declaration_t *declaration = get_irg_loc_description(irg, pos);
121 if (declaration != NULL) {
122 warningf(&declaration->source_position,
123 "variable '%#T' might be used uninitialized",
124 declaration->type, declaration->symbol);
126 return new_r_Unknown(irg, mode);
129 unsigned dbg_snprint(char *buf, unsigned len, const dbg_info *dbg)
131 const source_position_t *pos = (const source_position_t*) dbg;
134 return (unsigned) snprintf(buf, len, "%s:%u", pos->input_name,
138 const char *dbg_retrieve(const dbg_info *dbg, unsigned *line)
140 const source_position_t *pos = (const source_position_t*) dbg;
145 return pos->input_name;
148 static dbg_info *get_dbg_info(const source_position_t *pos)
150 return (dbg_info*) pos;
153 static ir_mode *_atomic_modes[ATOMIC_TYPE_LAST+1];
155 static ir_mode *mode_int, *mode_uint;
157 static ir_node *_expression_to_firm(const expression_t *expression);
158 static ir_node *expression_to_firm(const expression_t *expression);
159 static inline ir_mode *get_ir_mode(type_t *type);
160 static void create_local_declaration(declaration_t *declaration);
162 static ir_mode *init_atomic_ir_mode(atomic_type_kind_t kind)
164 unsigned flags = get_atomic_type_flags(kind);
165 unsigned size = get_atomic_type_size(kind);
166 if ( (flags & (ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_FLOAT))
167 && !(flags & ATOMIC_TYPE_FLAG_COMPLEX)) {
170 unsigned bit_size = size * 8;
171 bool is_signed = (flags & ATOMIC_TYPE_FLAG_SIGNED) != 0;
172 ir_mode_arithmetic arithmetic;
173 unsigned modulo_shift;
175 if (flags & ATOMIC_TYPE_FLAG_INTEGER) {
176 assert(! (flags & ATOMIC_TYPE_FLAG_FLOAT));
177 snprintf(name, sizeof(name), "i%s%d", is_signed?"":"u", bit_size);
178 sort = irms_int_number;
179 arithmetic = irma_twos_complement;
180 modulo_shift = bit_size < machine_size ? machine_size : bit_size;
182 assert(flags & ATOMIC_TYPE_FLAG_FLOAT);
183 snprintf(name, sizeof(name), "f%d", bit_size);
184 sort = irms_float_number;
185 arithmetic = irma_ieee754;
188 return new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
196 * Initialises the atomic modes depending on the machine size.
198 static void init_atomic_modes(void)
200 for(int i = 0; i <= ATOMIC_TYPE_LAST; ++i) {
201 _atomic_modes[i] = init_atomic_ir_mode((atomic_type_kind_t) i);
203 mode_int = _atomic_modes[ATOMIC_TYPE_INT];
204 mode_uint = _atomic_modes[ATOMIC_TYPE_UINT];
206 /* there's no real void type in firm */
207 _atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
209 /* initialize pointer modes */
211 ir_mode_sort sort = irms_reference;
212 unsigned bit_size = machine_size;
214 ir_mode_arithmetic arithmetic = irma_twos_complement;
215 unsigned modulo_shift
216 = bit_size < machine_size ? machine_size : bit_size;
218 snprintf(name, sizeof(name), "p%d", machine_size);
219 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
222 set_reference_mode_signed_eq(ptr_mode, _atomic_modes[get_intptr_kind()]);
223 set_reference_mode_unsigned_eq(ptr_mode, _atomic_modes[get_uintptr_kind()]);
225 /* Hmm, pointers should be machine size */
226 set_modeP_data(ptr_mode);
227 set_modeP_code(ptr_mode);
230 static unsigned get_compound_type_size(compound_type_t *type)
232 ir_type *irtype = get_ir_type((type_t*) type);
233 return get_type_size_bytes(irtype);
236 static unsigned get_array_type_size(array_type_t *type)
238 assert(!type->is_vla);
239 ir_type *irtype = get_ir_type((type_t*) type);
240 return get_type_size_bytes(irtype);
244 static unsigned get_type_size_const(type_t *type)
248 panic("error type occurred");
250 return get_atomic_type_size(type->atomic.akind);
252 return 2 * get_atomic_type_size(type->complex.akind);
254 return get_atomic_type_size(type->imaginary.akind);
256 return get_mode_size_bytes(mode_int);
257 case TYPE_COMPOUND_UNION:
258 case TYPE_COMPOUND_STRUCT:
259 return get_compound_type_size(&type->compound);
261 /* just a pointer to the function */
262 return get_mode_size_bytes(mode_P_code);
264 return get_mode_size_bytes(mode_P_data);
266 return get_array_type_size(&type->array);
268 return get_type_size_const(type->builtin.real_type);
270 panic("type size of bitfield request");
276 panic("Trying to determine size of invalid type");
279 static ir_node *get_type_size(type_t *type)
281 type = skip_typeref(type);
283 if (is_type_array(type) && type->array.is_vla) {
284 ir_node *size_node = type->array.size_node;
285 if (size_node == NULL) {
286 size_node = expression_to_firm(type->array.size_expression);
287 assert(!is_Const(size_node));
288 type->array.size_node = size_node;
291 ir_node *elem_size = get_type_size(type->array.element_type);
292 ir_mode *mode = get_irn_mode(size_node);
293 ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
297 ir_mode *mode = get_ir_mode(type_size_t);
299 sym.type_p = get_ir_type(type);
300 return new_SymConst(mode, sym, symconst_type_size);
303 static unsigned count_parameters(const function_type_t *function_type)
307 function_parameter_t *parameter = function_type->parameters;
308 for ( ; parameter != NULL; parameter = parameter->next) {
316 * Creates a Firm type for an atomic type
318 static ir_type *create_atomic_type(const atomic_type_t *type)
320 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
321 atomic_type_kind_t kind = type->akind;
322 ir_mode *mode = _atomic_modes[kind];
323 ident *id = get_mode_ident(mode);
324 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
326 set_type_alignment_bytes(irtype, type->base.alignment);
332 * Creates a Firm type for a complex type
334 static ir_type *create_complex_type(const complex_type_t *type)
336 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
337 atomic_type_kind_t kind = type->akind;
338 ir_mode *mode = _atomic_modes[kind];
339 ident *id = get_mode_ident(mode);
344 /* FIXME: finish the array */
349 * Creates a Firm type for an imaginary type
351 static ir_type *create_imaginary_type(const imaginary_type_t *type)
353 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
354 atomic_type_kind_t kind = type->akind;
355 ir_mode *mode = _atomic_modes[kind];
356 ident *id = get_mode_ident(mode);
357 ir_type *irtype = new_d_type_primitive(id, mode, dbgi);
359 set_type_alignment_bytes(irtype, type->base.alignment);
365 * return type of a parameter (and take transparent union gnu extension into
368 static type_t *get_parameter_type(type_t *type)
370 type = skip_typeref(type);
371 if (type->base.modifiers & TYPE_MODIFIER_TRANSPARENT_UNION) {
372 declaration_t *decl = type->compound.declaration;
373 type = decl->scope.declarations->type;
379 static ir_type *create_method_type(const function_type_t *function_type)
381 type_t *return_type = function_type->return_type;
383 ident *id = id_unique("functiontype.%u");
384 int n_parameters = count_parameters(function_type);
385 int n_results = return_type == type_void ? 0 : 1;
386 dbg_info *dbgi = get_dbg_info(&function_type->base.source_position);
387 ir_type *irtype = new_d_type_method(id, n_parameters, n_results, dbgi);
389 if (return_type != type_void) {
390 ir_type *restype = get_ir_type(return_type);
391 set_method_res_type(irtype, 0, restype);
394 function_parameter_t *parameter = function_type->parameters;
396 for ( ; parameter != NULL; parameter = parameter->next) {
397 type_t *type = get_parameter_type(parameter->type);
398 ir_type *p_irtype = get_ir_type(type);
399 set_method_param_type(irtype, n, p_irtype);
403 if (function_type->variadic || function_type->unspecified_parameters) {
404 set_method_variadicity(irtype, variadicity_variadic);
407 unsigned cc = get_method_calling_convention(irtype);
408 switch (function_type->calling_convention) {
409 case CC_DEFAULT: /* unspecified calling convention, equal to one of the other, typically cdecl */
412 set_method_calling_convention(irtype, SET_CDECL(cc));
416 if (function_type->variadic || function_type->unspecified_parameters)
419 /* only non-variadic function can use stdcall, else use cdecl */
420 set_method_calling_convention(irtype, SET_STDCALL(cc));
424 if (function_type->variadic || function_type->unspecified_parameters)
426 /* only non-variadic function can use fastcall, else use cdecl */
427 set_method_calling_convention(irtype, SET_FASTCALL(cc));
431 /* Hmm, leave default, not accepted by the parser yet. */
432 warningf(&function_type->base.source_position, "THISCALL calling convention not supported yet");
438 static ir_type *create_pointer_type(pointer_type_t *type)
440 type_t *points_to = type->points_to;
441 ir_type *ir_points_to = get_ir_type_incomplete(points_to);
442 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
443 ir_type *ir_type = new_d_type_pointer(id_unique("pointer.%u"),
444 ir_points_to, mode_P_data, dbgi);
449 static ir_type *create_array_type(array_type_t *type)
451 type_t *element_type = type->element_type;
452 ir_type *ir_element_type = get_ir_type(element_type);
454 ident *id = id_unique("array.%u");
455 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
456 ir_type *ir_type = new_d_type_array(id, 1, ir_element_type, dbgi);
458 const int align = get_type_alignment_bytes(ir_element_type);
459 set_type_alignment_bytes(ir_type, align);
461 if (type->size_constant) {
462 int n_elements = type->size;
464 set_array_bounds_int(ir_type, 0, 0, n_elements);
466 size_t elemsize = get_type_size_bytes(ir_element_type);
467 if (elemsize % align > 0) {
468 elemsize += align - (elemsize % align);
470 set_type_size_bytes(ir_type, n_elements * elemsize);
472 set_array_lower_bound_int(ir_type, 0, 0);
474 set_type_state(ir_type, layout_fixed);
480 * Return the signed integer type of size bits.
482 * @param size the size
484 static ir_type *get_signed_int_type_for_bit_size(ir_type *base_tp,
487 static ir_mode *s_modes[64 + 1] = {NULL, };
491 if (size <= 0 || size > 64)
494 mode = s_modes[size];
498 snprintf(name, sizeof(name), "bf_I%u", size);
499 mode = new_ir_mode(name, irms_int_number, size, 1, irma_twos_complement,
500 size <= 32 ? 32 : size );
501 s_modes[size] = mode;
505 snprintf(name, sizeof(name), "I%u", size);
506 ident *id = new_id_from_str(name);
507 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
508 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
509 set_primitive_base_type(res, base_tp);
515 * Return the unsigned integer type of size bits.
517 * @param size the size
519 static ir_type *get_unsigned_int_type_for_bit_size(ir_type *base_tp,
522 static ir_mode *u_modes[64 + 1] = {NULL, };
526 if (size <= 0 || size > 64)
529 mode = u_modes[size];
533 snprintf(name, sizeof(name), "bf_U%u", size);
534 mode = new_ir_mode(name, irms_int_number, size, 0, irma_twos_complement,
535 size <= 32 ? 32 : size );
536 u_modes[size] = mode;
541 snprintf(name, sizeof(name), "U%u", size);
542 ident *id = new_id_from_str(name);
543 dbg_info *dbgi = get_dbg_info(&builtin_source_position);
544 res = new_d_type_primitive(mangle_u(get_type_ident(base_tp), id), mode, dbgi);
545 set_primitive_base_type(res, base_tp);
550 static ir_type *create_bitfield_type(bitfield_type_t *const type)
552 type_t *base = skip_typeref(type->base_type);
553 assert(base->kind == TYPE_ATOMIC || base->kind == TYPE_ENUM);
554 ir_type *irbase = get_ir_type(base);
556 unsigned size = type->bit_size;
558 assert(!is_type_float(base));
559 if (is_type_signed(base)) {
560 return get_signed_int_type_for_bit_size(irbase, size);
562 return get_unsigned_int_type_for_bit_size(irbase, size);
566 #define INVALID_TYPE ((ir_type_ptr)-1)
569 COMPOUND_IS_STRUCT = false,
570 COMPOUND_IS_UNION = true
574 * Construct firm type from ast struct type.
576 * As anonymous inner structs get flattened to a single firm type, we might get
577 * irtype, outer_offset and out_align passed (they represent the position of
578 * the anonymous inner struct inside the resulting firm struct)
580 static ir_type *create_compound_type(compound_type_t *type, ir_type *irtype,
581 size_t *outer_offset, size_t *outer_align,
582 bool incomplete, bool is_union)
584 declaration_t *declaration = type->declaration;
585 declaration_kind_t kind = (declaration_kind_t)declaration->declaration_kind;
587 if (kind == DECLARATION_KIND_COMPOUND_TYPE_COMPLETE
588 || (kind == DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE
590 return declaration->v.irtype;
592 size_t align_all = 1;
594 size_t bit_offset = 0;
597 if (irtype == NULL) {
598 symbol_t *symbol = declaration->symbol;
600 if (symbol != NULL) {
601 id = new_id_from_str(symbol->string);
604 id = id_unique("__anonymous_union.%u");
606 id = id_unique("__anonymous_struct.%u");
609 dbg_info *dbgi = get_dbg_info(&type->base.source_position);
612 irtype = new_d_type_union(id, dbgi);
614 irtype = new_d_type_struct(id, dbgi);
617 declaration->declaration_kind
618 = DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE;
619 declaration->v.irtype = irtype;
620 //type->base.firm_type = irtype;
622 offset = *outer_offset;
623 align_all = *outer_align;
629 declaration->declaration_kind = DECLARATION_KIND_COMPOUND_TYPE_COMPLETE;
631 declaration_t *entry = declaration->scope.declarations;
632 for( ; entry != NULL; entry = entry->next) {
633 if (entry->namespc != NAMESPACE_NORMAL)
636 size_t prev_offset = offset;
638 symbol_t *symbol = entry->symbol;
639 type_t *entry_type = skip_typeref(entry->type);
640 dbg_info *dbgi = get_dbg_info(&entry->source_position);
643 if (symbol != NULL) {
644 ident = new_id_from_str(symbol->string);
646 if (entry_type->kind == TYPE_COMPOUND_STRUCT) {
647 create_compound_type(&entry_type->compound, irtype, &offset,
648 &align_all, false, COMPOUND_IS_STRUCT);
649 goto finished_member;
650 } else if (entry_type->kind == TYPE_COMPOUND_UNION) {
651 create_compound_type(&entry_type->compound, irtype, &offset,
652 &align_all, false, COMPOUND_IS_UNION);
653 goto finished_member;
655 assert(entry_type->kind == TYPE_BITFIELD);
657 ident = id_unique("anon.%u");
660 ir_type *base_irtype;
661 if (entry_type->kind == TYPE_BITFIELD) {
662 base_irtype = get_ir_type(entry_type->bitfield.base_type);
664 base_irtype = get_ir_type(entry_type);
667 size_t entry_alignment = get_type_alignment_bytes(base_irtype);
668 size_t misalign = offset % entry_alignment;
670 ir_type *entry_irtype = get_ir_type(entry_type);
671 ir_entity *entity = new_d_entity(irtype, ident, entry_irtype, dbgi);
674 size_t bits_remainder;
675 if (entry_type->kind == TYPE_BITFIELD) {
676 size_t size_bits = entry_type->bitfield.bit_size;
677 size_t rest_size_bits = (entry_alignment - misalign)*8 - bit_offset;
679 if (size_bits > rest_size_bits) {
680 /* start a new bucket */
681 offset += entry_alignment - misalign;
687 /* put into current bucket */
688 base = offset - misalign;
689 bits_remainder = misalign * 8 + bit_offset;
692 offset += size_bits / 8;
693 bit_offset = bit_offset + (size_bits % 8);
695 size_t entry_size = get_type_size_bytes(base_irtype);
696 if (misalign > 0 || bit_offset > 0)
697 offset += entry_alignment - misalign;
701 offset += entry_size;
705 if (entry_alignment > align_all) {
706 if (entry_alignment % align_all != 0) {
707 panic("uneven alignments not supported yet");
709 align_all = entry_alignment;
712 set_entity_offset(entity, base);
713 set_entity_offset_bits_remainder(entity,
714 (unsigned char) bits_remainder);
715 //add_struct_member(irtype, entity);
716 entry->declaration_kind = DECLARATION_KIND_COMPOUND_MEMBER;
717 assert(entry->v.entity == NULL);
718 entry->v.entity = entity;
722 size_t entry_size = offset - prev_offset;
723 if (entry_size > size) {
735 size_t misalign = offset % align_all;
736 if (misalign > 0 || bit_offset > 0) {
737 size += align_all - misalign;
740 if (outer_offset != NULL) {
742 *outer_offset = offset;
744 *outer_offset += size;
747 if (align_all > *outer_align) {
748 if (align_all % *outer_align != 0) {
749 panic("uneven alignments not supported yet");
751 *outer_align = align_all;
754 set_type_alignment_bytes(irtype, align_all);
755 set_type_size_bytes(irtype, size);
756 set_type_state(irtype, layout_fixed);
762 static ir_type *create_enum_type(enum_type_t *const type)
764 type->base.firm_type = ir_type_int;
766 ir_mode *const mode = mode_int;
767 tarval *const one = get_mode_one(mode);
768 tarval * tv_next = get_tarval_null(mode);
770 bool constant_folding_old = constant_folding;
771 constant_folding = true;
773 declaration_t *declaration = type->declaration->next;
774 for (; declaration != NULL; declaration = declaration->next) {
775 if (declaration->storage_class != STORAGE_CLASS_ENUM_ENTRY)
778 declaration->declaration_kind = DECLARATION_KIND_ENUM_ENTRY;
780 expression_t *const init = declaration->init.enum_value;
782 ir_node *const cnst = expression_to_firm(init);
783 if (!is_Const(cnst)) {
784 panic("couldn't fold constant");
786 tv_next = get_Const_tarval(cnst);
788 declaration->v.enum_val = tv_next;
789 tv_next = tarval_add(tv_next, one);
792 constant_folding = constant_folding_old;
797 static ir_type *get_ir_type_incomplete(type_t *type)
799 assert(type != NULL);
800 type = skip_typeref(type);
802 if (type->base.firm_type != NULL) {
803 assert(type->base.firm_type != INVALID_TYPE);
804 return type->base.firm_type;
807 switch (type->kind) {
808 case TYPE_COMPOUND_STRUCT:
809 return create_compound_type(&type->compound, NULL, NULL, NULL,
810 true, COMPOUND_IS_STRUCT);
811 case TYPE_COMPOUND_UNION:
812 return create_compound_type(&type->compound, NULL, NULL, NULL,
813 true, COMPOUND_IS_UNION);
815 return get_ir_type(type);
819 static ir_type *get_ir_type(type_t *type)
821 assert(type != NULL);
823 type = skip_typeref(type);
825 if (type->base.firm_type != NULL) {
826 assert(type->base.firm_type != INVALID_TYPE);
827 return type->base.firm_type;
830 ir_type *firm_type = NULL;
831 switch (type->kind) {
833 /* Happens while constant folding, when there was an error */
834 return create_atomic_type(&type_void->atomic);
837 firm_type = create_atomic_type(&type->atomic);
840 firm_type = create_complex_type(&type->complex);
843 firm_type = create_imaginary_type(&type->imaginary);
846 firm_type = create_method_type(&type->function);
849 firm_type = create_pointer_type(&type->pointer);
852 firm_type = create_array_type(&type->array);
854 case TYPE_COMPOUND_STRUCT:
855 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
856 false, COMPOUND_IS_STRUCT);
858 case TYPE_COMPOUND_UNION:
859 firm_type = create_compound_type(&type->compound, NULL, NULL, NULL,
860 false, COMPOUND_IS_UNION);
863 firm_type = create_enum_type(&type->enumt);
866 firm_type = get_ir_type(type->builtin.real_type);
869 firm_type = create_bitfield_type(&type->bitfield);
877 if (firm_type == NULL)
878 panic("unknown type found");
880 type->base.firm_type = firm_type;
884 static inline ir_mode *get_ir_mode(type_t *type)
886 ir_type *irtype = get_ir_type(type);
888 /* firm doesn't report a mode for arrays somehow... */
889 if (is_Array_type(irtype)) {
893 ir_mode *mode = get_type_mode(irtype);
894 assert(mode != NULL);
898 /** Names of the runtime functions. */
899 static const struct {
900 int id; /**< the rts id */
901 int n_res; /**< number of return values */
902 const char *name; /**< the name of the rts function */
903 int n_params; /**< number of parameters */
904 unsigned flags; /**< language flags */
906 { rts_debugbreak, 0, "__debugbreak", 0, _MS },
907 { rts_abort, 0, "abort", 0, _C89 },
908 { rts_alloca, 1, "alloca", 1, _ALL },
909 { rts_abs, 1, "abs", 1, _C89 },
910 { rts_labs, 1, "labs", 1, _C89 },
911 { rts_llabs, 1, "llabs", 1, _C99 },
912 { rts_imaxabs, 1, "imaxabs", 1, _C99 },
914 { rts_fabs, 1, "fabs", 1, _C89 },
915 { rts_sqrt, 1, "sqrt", 1, _C89 },
916 { rts_cbrt, 1, "cbrt", 1, _C99 },
917 { rts_exp, 1, "exp", 1, _C89 },
918 { rts_exp2, 1, "exp2", 1, _C89 },
919 { rts_exp10, 1, "exp10", 1, _GNUC },
920 { rts_log, 1, "log", 1, _C89 },
921 { rts_log2, 1, "log2", 1, _C89 },
922 { rts_log10, 1, "log10", 1, _C89 },
923 { rts_pow, 1, "pow", 2, _C89 },
924 { rts_sin, 1, "sin", 1, _C89 },
925 { rts_cos, 1, "cos", 1, _C89 },
926 { rts_tan, 1, "tan", 1, _C89 },
927 { rts_asin, 1, "asin", 1, _C89 },
928 { rts_acos, 1, "acos", 1, _C89 },
929 { rts_atan, 1, "atan", 1, _C89 },
930 { rts_sinh, 1, "sinh", 1, _C89 },
931 { rts_cosh, 1, "cosh", 1, _C89 },
932 { rts_tanh, 1, "tanh", 1, _C89 },
934 { rts_fabsf, 1, "fabsf", 1, _C99 },
935 { rts_sqrtf, 1, "sqrtf", 1, _C99 },
936 { rts_cbrtf, 1, "cbrtf", 1, _C99 },
937 { rts_expf, 1, "expf", 1, _C99 },
938 { rts_exp2f, 1, "exp2f", 1, _C99 },
939 { rts_exp10f, 1, "exp10f", 1, _GNUC },
940 { rts_logf, 1, "logf", 1, _C99 },
941 { rts_log2f, 1, "log2f", 1, _C99 },
942 { rts_log10f, 1, "log10f", 1, _C99 },
943 { rts_powf, 1, "powf", 2, _C99 },
944 { rts_sinf, 1, "sinf", 1, _C99 },
945 { rts_cosf, 1, "cosf", 1, _C99 },
946 { rts_tanf, 1, "tanf", 1, _C99 },
947 { rts_asinf, 1, "asinf", 1, _C99 },
948 { rts_acosf, 1, "acosf", 1, _C99 },
949 { rts_atanf, 1, "atanf", 1, _C99 },
950 { rts_sinhf, 1, "sinhf", 1, _C99 },
951 { rts_coshf, 1, "coshf", 1, _C99 },
952 { rts_tanhf, 1, "tanhf", 1, _C99 },
954 { rts_fabsl, 1, "fabsl", 1, _C99 },
955 { rts_sqrtl, 1, "sqrtl", 1, _C99 },
956 { rts_cbrtl, 1, "cbrtl", 1, _C99 },
957 { rts_expl, 1, "expl", 1, _C99 },
958 { rts_exp2l, 1, "exp2l", 1, _C99 },
959 { rts_exp10l, 1, "exp10l", 1, _GNUC },
960 { rts_logl, 1, "logl", 1, _C99 },
961 { rts_log2l, 1, "log2l", 1, _C99 },
962 { rts_log10l, 1, "log10l", 1, _C99 },
963 { rts_powl, 1, "powl", 2, _C99 },
964 { rts_sinl, 1, "sinl", 1, _C99 },
965 { rts_cosl, 1, "cosl", 1, _C99 },
966 { rts_tanl, 1, "tanl", 1, _C99 },
967 { rts_asinl, 1, "asinl", 1, _C99 },
968 { rts_acosl, 1, "acosl", 1, _C99 },
969 { rts_atanl, 1, "atanl", 1, _C99 },
970 { rts_sinhl, 1, "sinhl", 1, _C99 },
971 { rts_coshl, 1, "coshl", 1, _C99 },
972 { rts_tanhl, 1, "tanhl", 1, _C99 },
974 { rts_memcpy, 1, "memcpy", 3, _C89 }, /* HMM, man say its C99 */
975 { rts_memset, 1, "memset", 3, _C89 }, /* HMM, man say its C99 */
976 { rts_strcmp, 1, "strcmp", 2, _C89 },
977 { rts_strncmp, 1, "strncmp", 3, _C89 }
980 static ident *rts_idents[sizeof(rts_data) / sizeof(rts_data[0])];
983 * Mangles an entity linker (ld) name for win32 usage.
985 * @param ent the entity to be mangled
986 * @param declaration the declaration
988 static ident *create_ld_ident_win32(ir_entity *ent, declaration_t *declaration)
992 if (is_Method_type(get_entity_type(ent)))
993 id = decorate_win32_c_fkt(ent, get_entity_ident(ent));
995 /* always add an underscore in win32 */
996 id = mangle(id_underscore, get_entity_ident(ent));
999 decl_modifiers_t decl_modifiers = declaration->modifiers;
1000 if (decl_modifiers & DM_DLLIMPORT) {
1001 /* add prefix for imported symbols */
1002 id = mangle(id_imp, id);
1008 * Mangles an entity linker (ld) name for Linux ELF usage.
1010 * @param ent the entity to be mangled
1011 * @param declaration the declaration
1013 static ident *create_ld_ident_linux_elf(ir_entity *entity,
1014 declaration_t *declaration)
1017 return get_entity_ident(entity);
1021 * Mangles an entity linker (ld) name for Mach-O usage.
1023 * @param ent the entity to be mangled
1024 * @param declaration the declaration
1026 static ident *create_ld_ident_macho(ir_entity *ent, declaration_t *declaration)
1029 ident *id = mangle(id_underscore, get_entity_ident(ent));
1033 typedef ident* (*create_ld_ident_func)(ir_entity *entity,
1034 declaration_t *declaration);
1035 create_ld_ident_func create_ld_ident = create_ld_ident_linux_elf;
1038 * Handle GNU attributes for entities
1040 * @param ent the entity
1041 * @param decl the routine declaration
1043 static void handle_gnu_attributes_ent(ir_entity *ent, declaration_t *decl)
1045 if (decl->modifiers & DM_PURE) {
1046 /* TRUE if the declaration includes the GNU
1047 __attribute__((pure)) specifier. */
1048 set_entity_additional_property(ent, mtp_property_pure);
1050 if (decl->modifiers & DM_USED) {
1051 /* TRUE if the declaration includes the GNU
1052 __attribute__((used)) specifier. */
1053 set_entity_stickyness(ent, stickyness_sticky);
1058 * Creates an entity representing a function.
1060 * @param declaration the function declaration
1062 static ir_entity *get_function_entity(declaration_t *declaration)
1064 if (declaration->declaration_kind == DECLARATION_KIND_FUNCTION ||
1065 declaration->declaration_kind == DECLARATION_KIND_INNER_FUNCTION)
1066 return declaration->v.entity;
1067 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
1069 symbol_t *symbol = declaration->symbol;
1070 ident *id = new_id_from_str(symbol->string);
1072 ir_type *global_type = get_glob_type();
1073 ir_type *ir_type_method = get_ir_type(declaration->type);
1074 assert(is_Method_type(ir_type_method));
1076 /* already an entity defined? */
1077 ir_entity *entity = entitymap_get(&entitymap, symbol);
1078 if (entity != NULL) {
1079 if (get_entity_visibility(entity) == visibility_external_allocated
1080 && declaration->init.statement != NULL) {
1081 set_entity_visibility(entity, visibility_external_visible);
1083 goto entity_created;
1086 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
1087 entity = new_d_entity(global_type, id, ir_type_method, dbgi);
1088 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
1090 handle_gnu_attributes_ent(entity, declaration);
1092 /* static inline => local
1093 * extern inline => local
1094 * inline without definition => local
1095 * inline with definition => external_visible */
1096 storage_class_tag_t const storage_class = (storage_class_tag_t)declaration->storage_class;
1097 bool const is_inline = declaration->is_inline;
1098 bool const has_body = declaration->init.statement != NULL;
1099 if (is_inline && storage_class == STORAGE_CLASS_NONE && has_body) {
1100 set_entity_visibility(entity, visibility_external_visible);
1101 } else if (storage_class == STORAGE_CLASS_STATIC ||
1102 (is_inline && has_body)) {
1104 /* this entity was declared, but is defined nowhere */
1105 set_entity_peculiarity(entity, peculiarity_description);
1107 set_entity_visibility(entity, visibility_local);
1108 } else if (has_body) {
1109 set_entity_visibility(entity, visibility_external_visible);
1111 set_entity_visibility(entity, visibility_external_allocated);
1113 set_entity_allocation(entity, allocation_static);
1115 /* We should check for file scope here, but as long as we compile C only
1116 this is not needed. */
1117 if (! firm_opt.freestanding) {
1118 /* check for a known runtime function */
1119 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
1120 if (id != rts_idents[i])
1123 /* ignore those rts functions not necessary needed for current mode */
1124 if ((c_mode & rts_data[i].flags) == 0)
1126 assert(rts_entities[rts_data[i].id] == NULL);
1127 rts_entities[rts_data[i].id] = entity;
1131 entitymap_insert(&entitymap, symbol, entity);
1134 declaration->declaration_kind = DECLARATION_KIND_FUNCTION;
1135 declaration->v.entity = entity;
1141 * Creates a Const node representing a constant.
1143 static ir_node *const_to_firm(const const_expression_t *cnst)
1145 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1146 ir_mode *mode = get_ir_mode(cnst->base.type);
1151 if (mode_is_float(mode)) {
1152 tv = new_tarval_from_double(cnst->v.float_value, mode);
1154 if (mode_is_signed(mode)) {
1155 len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
1157 len = snprintf(buf, sizeof(buf), "%llu",
1158 (unsigned long long) cnst->v.int_value);
1160 tv = new_tarval_from_str(buf, len, mode);
1163 return new_d_Const(dbgi, mode, tv);
1167 * Creates a Const node representing a character constant.
1169 static ir_node *character_constant_to_firm(const const_expression_t *cnst)
1171 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1172 ir_mode *mode = get_ir_mode(cnst->base.type);
1174 long long int v = 0;
1175 for (size_t i = 0; i < cnst->v.character.size; ++i) {
1176 if (char_is_signed) {
1177 v = (v << 8) | ((signed char)cnst->v.character.begin[i]);
1179 v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
1183 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1184 tarval *tv = new_tarval_from_str(buf, len, mode);
1186 return new_d_Const(dbgi, mode, tv);
1190 * Creates a Const node representing a wide character constant.
1192 static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
1194 dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
1195 ir_mode *mode = get_ir_mode(cnst->base.type);
1197 long long int v = cnst->v.wide_character.begin[0];
1200 size_t len = snprintf(buf, sizeof(buf), "%lld", v);
1201 tarval *tv = new_tarval_from_str(buf, len, mode);
1203 return new_d_Const(dbgi, mode, tv);
1207 * Creates a SymConst for a given entity.
1209 * @param dbgi debug info
1210 * @param mode the (reference) mode for the SymConst
1211 * @param entity the entity
1213 static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
1216 assert(entity != NULL);
1217 union symconst_symbol sym;
1218 sym.entity_p = entity;
1219 return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
1223 * Creates a SymConst node representing a string constant.
1225 * @param src_pos the source position of the string constant
1226 * @param id_prefix a prefix for the name of the generated string constant
1227 * @param value the value of the string constant
1229 static ir_node *string_to_firm(const source_position_t *const src_pos,
1230 const char *const id_prefix,
1231 const string_t *const value)
1233 ir_type *const global_type = get_glob_type();
1234 dbg_info *const dbgi = get_dbg_info(src_pos);
1235 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1236 ir_type_const_char, dbgi);
1238 ident *const id = id_unique(id_prefix);
1239 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1240 set_entity_ld_ident(entity, id);
1241 set_entity_variability(entity, variability_constant);
1242 set_entity_allocation(entity, allocation_static);
1244 ir_type *const elem_type = ir_type_const_char;
1245 ir_mode *const mode = get_type_mode(elem_type);
1247 const char* const string = value->begin;
1248 const size_t slen = value->size;
1250 set_array_lower_bound_int(type, 0, 0);
1251 set_array_upper_bound_int(type, 0, slen);
1252 set_type_size_bytes(type, slen);
1253 set_type_state(type, layout_fixed);
1255 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1256 for(size_t i = 0; i < slen; ++i) {
1257 tvs[i] = new_tarval_from_long(string[i], mode);
1260 set_array_entity_values(entity, tvs, slen);
1263 return create_symconst(dbgi, mode_P_data, entity);
1267 * Creates a SymConst node representing a string literal.
1269 * @param literal the string literal
1271 static ir_node *string_literal_to_firm(
1272 const string_literal_expression_t* literal)
1274 return string_to_firm(&literal->base.source_position, "Lstr.%u",
1279 * Creates a SymConst node representing a wide string literal.
1281 * @param literal the wide string literal
1283 static ir_node *wide_string_literal_to_firm(
1284 const wide_string_literal_expression_t* const literal)
1286 ir_type *const global_type = get_glob_type();
1287 ir_type *const elem_type = ir_type_wchar_t;
1288 dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
1289 ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
1292 ident *const id = id_unique("Lstr.%u");
1293 ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
1294 set_entity_ld_ident(entity, id);
1295 set_entity_variability(entity, variability_constant);
1296 set_entity_allocation(entity, allocation_static);
1298 ir_mode *const mode = get_type_mode(elem_type);
1300 const wchar_rep_t *const string = literal->value.begin;
1301 const size_t slen = literal->value.size;
1303 set_array_lower_bound_int(type, 0, 0);
1304 set_array_upper_bound_int(type, 0, slen);
1305 set_type_size_bytes(type, slen);
1306 set_type_state(type, layout_fixed);
1308 tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
1309 for(size_t i = 0; i < slen; ++i) {
1310 tvs[i] = new_tarval_from_long(string[i], mode);
1313 set_array_entity_values(entity, tvs, slen);
1316 return create_symconst(dbgi, mode_P_data, entity);
1319 static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
1320 ir_node *const addr)
1322 ir_type *irtype = get_ir_type(type);
1323 if (is_compound_type(irtype)
1324 || is_Method_type(irtype)
1325 || is_Array_type(irtype)) {
1329 ir_mode *const mode = get_type_mode(irtype);
1330 ir_node *const memory = get_store();
1331 ir_node *const load = new_d_Load(dbgi, memory, addr, mode);
1332 ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1333 ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1335 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1336 set_Load_volatility(load, volatility_is_volatile);
1339 set_store(load_mem);
1344 * Creates a strict Conv if neccessary.
1346 static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
1348 ir_mode *mode = get_irn_mode(node);
1350 if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
1352 if (!mode_is_float(mode))
1355 /* check if there is already a Conv */
1356 if (is_Conv(node)) {
1357 /* convert it into a strict Conv */
1358 set_Conv_strict(node, 1);
1362 /* otherwise create a new one */
1363 return new_d_strictConv(dbgi, node, mode);
1366 static ir_node *get_global_var_address(dbg_info *const dbgi,
1367 const declaration_t *const decl)
1369 assert(decl->declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
1371 ir_entity *const entity = decl->v.entity;
1372 switch ((storage_class_tag_t)decl->storage_class) {
1373 case STORAGE_CLASS_THREAD:
1374 case STORAGE_CLASS_THREAD_EXTERN:
1375 case STORAGE_CLASS_THREAD_STATIC: {
1376 ir_node *const no_mem = new_NoMem();
1377 ir_node *const tls = get_irg_tls(current_ir_graph);
1378 return new_d_simpleSel(dbgi, no_mem, tls, entity);
1382 return create_symconst(dbgi, mode_P_data, entity);
1387 * Returns the correct base address depending on whether it is a parameter or a
1388 * normal local variable.
1390 static ir_node *get_local_frame(ir_entity *const ent)
1392 ir_graph *const irg = current_ir_graph;
1393 const ir_type *const owner = get_entity_owner(ent);
1394 if (owner == get_irg_frame_type(irg)) {
1395 return get_irg_frame(irg);
1397 assert(owner == get_method_value_param_type(get_entity_type(get_irg_entity(irg))));
1398 return get_irg_value_param_base(irg);
1402 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
1404 ir_mode *value_mode = get_irn_mode(value);
1406 if (value_mode == dest_mode || is_Bad(value))
1409 if (dest_mode == mode_b) {
1410 ir_node *zero = new_Const(value_mode, get_mode_null(value_mode));
1411 ir_node *cmp = new_d_Cmp(dbgi, value, zero);
1412 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
1416 return new_d_Conv(dbgi, value, dest_mode);
1420 * Keep all memory edges of the given block.
1422 static void keep_all_memory(ir_node *block) {
1423 ir_node *old = get_cur_block();
1425 set_cur_block(block);
1426 keep_alive(get_store());
1427 /* TODO: keep all memory edges from restricted pointers */
1431 static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
1433 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1434 declaration_t *declaration = ref->declaration;
1435 type_t *type = skip_typeref(declaration->type);
1437 /* make sure the type is constructed */
1438 (void) get_ir_type(type);
1440 switch ((declaration_kind_t)declaration->declaration_kind) {
1441 case DECLARATION_KIND_TYPE:
1442 case DECLARATION_KIND_UNKNOWN:
1445 case DECLARATION_KIND_ENUM_ENTRY: {
1446 ir_mode *const mode = get_ir_mode(type);
1447 if (ref->is_outer_ref) {
1448 /* reference to an outer variable */
1449 panic("Outer variable reference not implemented");
1451 return new_Const(mode, declaration->v.enum_val);
1455 case DECLARATION_KIND_LOCAL_VARIABLE: {
1456 ir_mode *const mode = get_ir_mode(type);
1457 return get_value(declaration->v.value_number, mode);
1459 case DECLARATION_KIND_FUNCTION: {
1460 ir_mode *const mode = get_ir_mode(type);
1461 return create_symconst(dbgi, mode, declaration->v.entity);
1463 case DECLARATION_KIND_INNER_FUNCTION: {
1464 ir_mode *const mode = get_ir_mode(type);
1465 if (! declaration->goto_to_outer && !declaration->need_closure) {
1466 /* inner function not using the closure */
1467 return create_symconst(dbgi, mode, declaration->v.entity);
1469 /* TODO: need trampoline here */
1470 panic("Trampoline code not implemented");
1471 return create_symconst(dbgi, mode, declaration->v.entity);
1474 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1475 ir_node *const addr = get_global_var_address(dbgi, declaration);
1476 return deref_address(dbgi, declaration->type, addr);
1479 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1480 ir_entity *entity = declaration->v.entity;
1481 ir_node *frame = get_local_frame(entity);
1482 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1483 return deref_address(dbgi, declaration->type, sel);
1486 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1487 return declaration->v.vla_base;
1489 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1490 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1491 case DECLARATION_KIND_COMPOUND_MEMBER:
1492 case DECLARATION_KIND_LABEL_BLOCK:
1493 panic("not implemented reference type");
1496 panic("reference to declaration with unknown type found");
1499 static ir_node *reference_addr(const reference_expression_t *ref)
1501 dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
1502 declaration_t *declaration = ref->declaration;
1504 switch((declaration_kind_t) declaration->declaration_kind) {
1505 case DECLARATION_KIND_TYPE:
1506 case DECLARATION_KIND_UNKNOWN:
1508 case DECLARATION_KIND_LOCAL_VARIABLE:
1509 /* you can store to a local variable (so we don't panic but return NULL
1510 * as an indicator for no real address) */
1512 case DECLARATION_KIND_GLOBAL_VARIABLE: {
1513 ir_node *const addr = get_global_var_address(dbgi, declaration);
1516 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
1517 ir_entity *entity = declaration->v.entity;
1518 ir_node *frame = get_local_frame(entity);
1519 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
1524 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
1525 return declaration->v.vla_base;
1527 case DECLARATION_KIND_ENUM_ENTRY:
1528 panic("trying to reference enum entry");
1530 case DECLARATION_KIND_FUNCTION:
1531 case DECLARATION_KIND_INNER_FUNCTION:
1532 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
1533 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
1534 case DECLARATION_KIND_COMPOUND_MEMBER:
1535 case DECLARATION_KIND_LABEL_BLOCK:
1536 panic("not implemented reference type");
1539 panic("reference to declaration with unknown type found");
1543 * Transform calls to builtin functions.
1545 static ir_node *process_builtin_call(const call_expression_t *call)
1547 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1549 assert(call->function->kind == EXPR_BUILTIN_SYMBOL);
1550 builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
1552 type_t *type = skip_typeref(builtin->base.type);
1553 assert(is_type_pointer(type));
1555 type_t *function_type = skip_typeref(type->pointer.points_to);
1556 symbol_t *symbol = builtin->symbol;
1558 switch(symbol->ID) {
1559 case T___builtin_alloca: {
1560 if (call->arguments == NULL || call->arguments->next != NULL) {
1561 panic("invalid number of parameters on __builtin_alloca");
1563 expression_t *argument = call->arguments->expression;
1564 ir_node *size = expression_to_firm(argument);
1566 ir_node *store = get_store();
1567 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1569 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1571 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1575 case T___builtin_huge_val: {
1576 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1577 tarval *tv = get_mode_infinite(mode);
1578 ir_node *res = new_d_Const(dbgi, mode, tv);
1581 case T___builtin_nan:
1582 case T___builtin_nanf:
1583 case T___builtin_nand: {
1584 /* Ignore string for now... */
1585 assert(is_type_function(function_type));
1586 ir_mode *mode = get_ir_mode(function_type->function.return_type);
1587 tarval *tv = get_mode_NAN(mode);
1588 ir_node *res = new_d_Const(dbgi, mode, tv);
1591 case T___builtin_va_end:
1594 panic("Unsupported builtin found\n");
1599 * Transform a call expression.
1600 * Handles some special cases, like alloca() calls, which must be resolved
1601 * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
1602 * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
1605 static ir_node *call_expression_to_firm(const call_expression_t *call)
1607 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1608 assert(get_cur_block() != NULL);
1610 expression_t *function = call->function;
1611 if (function->kind == EXPR_BUILTIN_SYMBOL) {
1612 return process_builtin_call(call);
1614 if (function->kind == EXPR_REFERENCE) {
1615 const reference_expression_t *ref = &function->reference;
1616 declaration_t *declaration = ref->declaration;
1618 if ((declaration_kind_t)declaration->declaration_kind == DECLARATION_KIND_FUNCTION) {
1619 if (declaration->v.entity == rts_entities[rts_alloca]) {
1620 /* handle alloca() call */
1621 expression_t *argument = call->arguments->expression;
1622 ir_node *size = expression_to_firm(argument);
1624 size = create_conv(dbgi, size, get_ir_mode(type_size_t));
1626 ir_node *store = get_store();
1627 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1628 ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
1630 ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
1632 ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
1638 ir_node *callee = expression_to_firm(function);
1640 type_t *type = skip_typeref(function->base.type);
1641 assert(is_type_pointer(type));
1642 pointer_type_t *pointer_type = &type->pointer;
1643 type_t *points_to = skip_typeref(pointer_type->points_to);
1644 assert(is_type_function(points_to));
1645 function_type_t *function_type = &points_to->function;
1647 int n_parameters = 0;
1648 ir_type *ir_method_type = get_ir_type((type_t*) function_type);
1649 ir_type *new_method_type = NULL;
1650 if (function_type->variadic || function_type->unspecified_parameters) {
1651 const call_argument_t *argument = call->arguments;
1652 for( ; argument != NULL; argument = argument->next) {
1656 /* we need to construct a new method type matching the call
1658 int n_res = get_method_n_ress(ir_method_type);
1659 dbg_info *dbgi = get_dbg_info(&call->base.source_position);
1660 new_method_type = new_d_type_method(id_unique("calltype.%u"),
1661 n_parameters, n_res, dbgi);
1662 set_method_calling_convention(new_method_type,
1663 get_method_calling_convention(ir_method_type));
1664 set_method_additional_properties(new_method_type,
1665 get_method_additional_properties(ir_method_type));
1666 set_method_variadicity(new_method_type,
1667 get_method_variadicity(ir_method_type));
1669 for(int i = 0; i < n_res; ++i) {
1670 set_method_res_type(new_method_type, i,
1671 get_method_res_type(ir_method_type, i));
1673 argument = call->arguments;
1674 for(int i = 0; i < n_parameters; ++i, argument = argument->next) {
1675 expression_t *expression = argument->expression;
1676 ir_type *irtype = get_ir_type(expression->base.type);
1677 set_method_param_type(new_method_type, i, irtype);
1679 ir_method_type = new_method_type;
1681 n_parameters = get_method_n_params(ir_method_type);
1684 ir_node *in[n_parameters];
1686 const call_argument_t *argument = call->arguments;
1687 for(int n = 0; n < n_parameters; ++n) {
1688 expression_t *expression = argument->expression;
1689 ir_node *arg_node = expression_to_firm(expression);
1691 arg_node = do_strict_conv(dbgi, arg_node);
1695 argument = argument->next;
1698 ir_node *store = get_store();
1699 ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
1701 ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
1704 type_t *return_type = skip_typeref(function_type->return_type);
1705 ir_node *result = NULL;
1707 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
1709 if (is_type_scalar(return_type)) {
1710 mode = get_ir_mode(return_type);
1714 ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
1715 result = new_d_Proj(dbgi, resproj, mode, 0);
1718 if (function->kind == EXPR_REFERENCE &&
1719 function->reference.declaration->modifiers & DM_NORETURN) {
1720 /* A dead end: Keep the Call and the Block. Also place all further
1721 * nodes into a new and unreachable block. */
1723 keep_alive(get_cur_block());
1730 static void statement_to_firm(statement_t *statement);
1731 static ir_node *compound_statement_to_firm(compound_statement_t *compound);
1733 static ir_node *expression_to_addr(const expression_t *expression);
1734 static ir_node *create_condition_evaluation(const expression_t *expression,
1735 ir_node *true_block,
1736 ir_node *false_block);
1738 static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
1741 value = do_strict_conv(dbgi, value);
1743 ir_node *memory = get_store();
1745 if (is_type_scalar(type)) {
1746 ir_node *store = new_d_Store(dbgi, memory, addr, value);
1747 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1748 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
1749 set_Store_volatility(store, volatility_is_volatile);
1750 set_store(store_mem);
1752 ir_type *irtype = get_ir_type(type);
1753 ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
1754 ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
1755 set_store(copyb_mem);
1759 static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
1761 tarval *all_one = get_mode_all_one(mode);
1762 int mode_size = get_mode_size_bits(mode);
1764 assert(offset >= 0);
1766 assert(offset + size <= mode_size);
1767 if (size == mode_size) {
1771 long shiftr = get_mode_size_bits(mode) - size;
1772 long shiftl = offset;
1773 tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
1774 tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
1775 tarval *mask0 = tarval_shr(all_one, tv_shiftr);
1776 tarval *mask1 = tarval_shl(mask0, tv_shiftl);
1781 static void bitfield_store_to_firm(const select_expression_t *expression,
1782 ir_node *addr, ir_node *value)
1784 type_t *type = expression->base.type;
1785 ir_mode *mode = get_ir_mode(type);
1787 assert(get_irn_mode(value) == mode || is_Bad(value));
1789 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1791 /* kill upper bits of value and shift to right position */
1792 ir_entity *entity = expression->compound_entry->v.entity;
1793 int bitoffset = get_entity_offset_bits_remainder(entity);
1794 ir_type *entity_type = get_entity_type(entity);
1795 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1797 tarval *mask = create_bitfield_mask(mode, 0, bitsize);
1798 ir_node *mask_node = new_d_Const(dbgi, mode, mask);
1799 ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
1800 tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
1801 ir_node *shiftcount = new_d_Const(dbgi, mode_uint, shiftl);
1802 ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
1804 /* load current value */
1805 ir_node *mem = get_store();
1806 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1807 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1808 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1809 tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
1810 tarval *inv_mask = tarval_not(shift_mask);
1811 ir_node *inv_mask_node = new_d_Const(dbgi, mode, inv_mask);
1812 ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
1814 /* construct new value and store */
1815 ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
1816 ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val);
1817 ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
1818 set_store(store_mem);
1820 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
1821 set_Load_volatility(load, volatility_is_volatile);
1822 set_Store_volatility(store, volatility_is_volatile);
1826 static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
1829 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1830 type_t *type = expression->base.type;
1831 ir_mode *mode = get_ir_mode(type);
1832 ir_node *mem = get_store();
1833 ir_node *load = new_d_Load(dbgi, mem, addr, mode);
1834 ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
1835 ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
1837 load_res = create_conv(dbgi, load_res, mode_int);
1839 set_store(load_mem);
1841 /* kill upper bits */
1842 ir_entity *entity = expression->compound_entry->v.entity;
1843 int bitoffset = get_entity_offset_bits_remainder(entity);
1844 ir_type *entity_type = get_entity_type(entity);
1845 int bitsize = get_mode_size_bits(get_type_mode(entity_type));
1846 long shift_bitsl = machine_size - bitoffset - bitsize;
1847 assert(shift_bitsl >= 0);
1848 tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
1849 ir_node *countl = new_d_Const(dbgi, mode_uint, tvl);
1850 ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
1852 long shift_bitsr = bitoffset + shift_bitsl;
1853 assert(shift_bitsr <= (long) machine_size);
1854 tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
1855 ir_node *countr = new_d_Const(dbgi, mode_uint, tvr);
1857 if (mode_is_signed(mode)) {
1858 shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
1860 shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
1863 return create_conv(dbgi, shiftr, mode);
1866 static void set_value_for_expression_addr(const expression_t *expression,
1867 ir_node *value, ir_node *addr)
1869 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1870 value = do_strict_conv(dbgi, value);
1872 if (expression->kind == EXPR_REFERENCE) {
1873 const reference_expression_t *ref = &expression->reference;
1875 declaration_t *declaration = ref->declaration;
1876 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1877 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1878 set_value(declaration->v.value_number, value);
1884 addr = expression_to_addr(expression);
1886 type_t *type = skip_typeref(expression->base.type);
1888 if (expression->kind == EXPR_SELECT) {
1889 const select_expression_t *select = &expression->select;
1891 declaration_t *declaration = select->compound_entry;
1892 if (declaration->type->kind == TYPE_BITFIELD) {
1893 bitfield_store_to_firm(select, addr, value);
1898 assign_value(dbgi, addr, type, value);
1901 static void set_value_for_expression(const expression_t *expression,
1904 set_value_for_expression_addr(expression, value, NULL);
1907 static ir_node *get_value_from_lvalue(const expression_t *expression,
1910 if (expression->kind == EXPR_REFERENCE) {
1911 const reference_expression_t *ref = &expression->reference;
1913 declaration_t *declaration = ref->declaration;
1914 assert(declaration->declaration_kind != DECLARATION_KIND_UNKNOWN);
1915 if (declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
1916 assert(addr == NULL);
1917 ir_mode *mode = get_ir_mode(expression->base.type);
1918 return get_value(declaration->v.value_number, mode);
1922 assert(addr != NULL);
1923 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
1926 if (expression->kind == EXPR_SELECT &&
1927 expression->select.compound_entry->type->kind == TYPE_BITFIELD){
1928 value = bitfield_extract_to_firm(&expression->select, addr);
1930 value = deref_address(dbgi, expression->base.type, addr);
1937 static ir_node *create_incdec(const unary_expression_t *expression)
1939 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
1940 const expression_t *value_expr = expression->value;
1941 ir_node *addr = expression_to_addr(value_expr);
1942 ir_node *value = get_value_from_lvalue(value_expr, addr);
1944 type_t *type = skip_typeref(expression->base.type);
1945 ir_mode *mode = get_ir_mode(expression->base.type);
1948 if (is_type_pointer(type)) {
1949 pointer_type_t *pointer_type = &type->pointer;
1950 offset = get_type_size(pointer_type->points_to);
1952 assert(is_type_arithmetic(type));
1953 offset = new_Const(mode, get_mode_one(mode));
1957 ir_node *store_value;
1958 switch(expression->base.kind) {
1959 case EXPR_UNARY_POSTFIX_INCREMENT:
1961 store_value = new_d_Add(dbgi, value, offset, mode);
1963 case EXPR_UNARY_POSTFIX_DECREMENT:
1965 store_value = new_d_Sub(dbgi, value, offset, mode);
1967 case EXPR_UNARY_PREFIX_INCREMENT:
1968 result = new_d_Add(dbgi, value, offset, mode);
1969 store_value = result;
1971 case EXPR_UNARY_PREFIX_DECREMENT:
1972 result = new_d_Sub(dbgi, value, offset, mode);
1973 store_value = result;
1976 panic("no incdec expr in create_incdec");
1979 set_value_for_expression_addr(value_expr, store_value, addr);
1984 static bool is_local_variable(expression_t *expression)
1986 if (expression->kind != EXPR_REFERENCE)
1988 reference_expression_t *ref_expr = &expression->reference;
1989 declaration_t *declaration = ref_expr->declaration;
1990 return declaration->declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE;
1993 static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
1996 case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
1997 case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
1998 case EXPR_BINARY_NOTEQUAL:
1999 return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
2000 case EXPR_BINARY_ISLESS:
2001 case EXPR_BINARY_LESS: return pn_Cmp_Lt;
2002 case EXPR_BINARY_ISLESSEQUAL:
2003 case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
2004 case EXPR_BINARY_ISGREATER:
2005 case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
2006 case EXPR_BINARY_ISGREATEREQUAL:
2007 case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
2008 case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
2013 panic("trying to get pn_Cmp from non-comparison binexpr type");
2017 * Handle the assume optimizer hint: check if a Confirm
2018 * node can be created.
2020 * @param dbi debug info
2021 * @param expr the IL assume expression
2023 * we support here only some simple cases:
2028 static ir_node *handle_assume_compare(dbg_info *dbi,
2029 const binary_expression_t *expression)
2031 expression_t *op1 = expression->left;
2032 expression_t *op2 = expression->right;
2033 declaration_t *var2, *var = NULL;
2034 ir_node *res = NULL;
2037 cmp_val = get_pnc(expression->base.kind, op1->base.type);
2039 if (is_local_variable(op1) && is_local_variable(op2)) {
2040 var = op1->reference.declaration;
2041 var2 = op2->reference.declaration;
2043 type_t *const type = skip_typeref(var->type);
2044 ir_mode *const mode = get_ir_mode(type);
2046 ir_node *const irn1 = get_value(var->v.value_number, mode);
2047 ir_node *const irn2 = get_value(var2->v.value_number, mode);
2049 res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
2050 set_value(var2->v.value_number, res);
2052 res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
2053 set_value(var->v.value_number, res);
2059 if (is_local_variable(op1) && is_constant_expression(op2)) {
2060 var = op1->reference.declaration;
2062 } else if (is_constant_expression(op1) && is_local_variable(op2)) {
2063 cmp_val = get_inversed_pnc(cmp_val);
2064 var = op2->reference.declaration;
2069 type_t *const type = skip_typeref(var->type);
2070 ir_mode *const mode = get_ir_mode(type);
2072 res = get_value(var->v.value_number, mode);
2073 res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
2074 set_value(var->v.value_number, res);
2080 * Handle the assume optimizer hint.
2082 * @param dbi debug info
2083 * @param expr the IL assume expression
2085 static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression) {
2086 switch(expression->kind) {
2087 case EXPR_BINARY_EQUAL:
2088 case EXPR_BINARY_NOTEQUAL:
2089 case EXPR_BINARY_LESS:
2090 case EXPR_BINARY_LESSEQUAL:
2091 case EXPR_BINARY_GREATER:
2092 case EXPR_BINARY_GREATEREQUAL:
2093 return handle_assume_compare(dbi, &expression->binary);
2099 static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
2101 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2102 type_t *type = skip_typeref(expression->base.type);
2104 if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
2105 return expression_to_addr(expression->value);
2107 const expression_t *value = expression->value;
2109 switch(expression->base.kind) {
2110 case EXPR_UNARY_NEGATE: {
2111 ir_node *value_node = expression_to_firm(value);
2112 ir_mode *mode = get_ir_mode(type);
2113 return new_d_Minus(dbgi, value_node, mode);
2115 case EXPR_UNARY_PLUS:
2116 return expression_to_firm(value);
2117 case EXPR_UNARY_BITWISE_NEGATE: {
2118 ir_node *value_node = expression_to_firm(value);
2119 ir_mode *mode = get_ir_mode(type);
2120 return new_d_Not(dbgi, value_node, mode);
2122 case EXPR_UNARY_NOT: {
2123 ir_node *value_node = _expression_to_firm(value);
2124 value_node = create_conv(dbgi, value_node, mode_b);
2125 ir_node *res = new_d_Not(dbgi, value_node, mode_b);
2128 case EXPR_UNARY_DEREFERENCE: {
2129 ir_node *value_node = expression_to_firm(value);
2130 type_t *value_type = skip_typeref(value->base.type);
2131 assert(is_type_pointer(value_type));
2132 type_t *points_to = value_type->pointer.points_to;
2133 return deref_address(dbgi, points_to, value_node);
2135 case EXPR_UNARY_POSTFIX_INCREMENT:
2136 case EXPR_UNARY_POSTFIX_DECREMENT:
2137 case EXPR_UNARY_PREFIX_INCREMENT:
2138 case EXPR_UNARY_PREFIX_DECREMENT:
2139 return create_incdec(expression);
2140 case EXPR_UNARY_CAST: {
2141 ir_node *value_node = expression_to_firm(value);
2142 if (is_type_scalar(type)) {
2143 ir_mode *mode = get_ir_mode(type);
2144 ir_node *node = create_conv(dbgi, value_node, mode);
2145 node = do_strict_conv(dbgi, node);
2148 /* make sure firm type is constructed */
2149 (void) get_ir_type(type);
2153 case EXPR_UNARY_CAST_IMPLICIT: {
2154 ir_node *value_node = expression_to_firm(value);
2155 if (is_type_scalar(type)) {
2156 ir_mode *mode = get_ir_mode(type);
2157 return create_conv(dbgi, value_node, mode);
2162 case EXPR_UNARY_ASSUME:
2163 if (firm_opt.confirm)
2164 return handle_assume(dbgi, value);
2171 panic("invalid UNEXPR type found");
2175 * produces a 0/1 depending of the value of a mode_b node
2177 static ir_node *produce_condition_result(const expression_t *expression,
2180 ir_mode *mode = get_ir_mode(expression->base.type);
2181 ir_node *cur_block = get_cur_block();
2183 ir_node *one_block = new_immBlock();
2184 ir_node *one = new_Const(mode, get_mode_one(mode));
2185 ir_node *jmp_one = new_d_Jmp(dbgi);
2187 ir_node *zero_block = new_immBlock();
2188 ir_node *zero = new_Const(mode, get_mode_null(mode));
2189 ir_node *jmp_zero = new_d_Jmp(dbgi);
2191 set_cur_block(cur_block);
2192 create_condition_evaluation(expression, one_block, zero_block);
2193 mature_immBlock(one_block);
2194 mature_immBlock(zero_block);
2196 ir_node *in_cf[2] = { jmp_one, jmp_zero };
2197 new_Block(2, in_cf);
2199 ir_node *in[2] = { one, zero };
2200 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2205 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
2206 ir_node *value, type_t *type)
2208 pointer_type_t *const pointer_type = &type->pointer;
2209 type_t *const points_to = skip_typeref(pointer_type->points_to);
2210 unsigned elem_size = get_type_size_const(points_to);
2212 /* gcc extension: allow arithmetic with void * and function * */
2213 if ((elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) ||
2214 is_type_function(points_to)) {
2218 assert(elem_size >= 1);
2222 value = create_conv(dbgi, value, mode_int);
2223 ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
2224 ir_node *const mul = new_d_Mul(dbgi, value, cnst, mode_int);
2228 static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
2229 ir_node *left, ir_node *right)
2232 type_t *type_left = skip_typeref(expression->left->base.type);
2233 type_t *type_right = skip_typeref(expression->right->base.type);
2235 expression_kind_t kind = expression->base.kind;
2238 case EXPR_BINARY_SHIFTLEFT:
2239 case EXPR_BINARY_SHIFTRIGHT:
2240 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2241 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2242 mode = get_irn_mode(left);
2243 right = create_conv(dbgi, right, mode_uint);
2246 case EXPR_BINARY_SUB:
2247 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
2248 const pointer_type_t *const ptr_type = &type_left->pointer;
2250 mode = get_ir_mode(expression->base.type);
2251 ir_node *const elem_size = get_type_size(ptr_type->points_to);
2252 ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
2253 ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
2254 ir_node *const no_mem = new_NoMem();
2255 ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
2256 mode, op_pin_state_floats);
2257 return new_d_Proj(dbgi, div, mode, pn_Div_res);
2260 case EXPR_BINARY_SUB_ASSIGN:
2261 if (is_type_pointer(type_left)) {
2262 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2263 mode = get_ir_mode(type_left);
2268 case EXPR_BINARY_ADD:
2269 case EXPR_BINARY_ADD_ASSIGN:
2270 if (is_type_pointer(type_left)) {
2271 right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
2272 mode = get_ir_mode(type_left);
2274 } else if (is_type_pointer(type_right)) {
2275 left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
2276 mode = get_ir_mode(type_right);
2283 mode = get_irn_mode(right);
2284 left = create_conv(dbgi, left, mode);
2289 case EXPR_BINARY_ADD_ASSIGN:
2290 case EXPR_BINARY_ADD:
2291 return new_d_Add(dbgi, left, right, mode);
2292 case EXPR_BINARY_SUB_ASSIGN:
2293 case EXPR_BINARY_SUB:
2294 return new_d_Sub(dbgi, left, right, mode);
2295 case EXPR_BINARY_MUL_ASSIGN:
2296 case EXPR_BINARY_MUL:
2297 return new_d_Mul(dbgi, left, right, mode);
2298 case EXPR_BINARY_BITWISE_AND:
2299 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2300 return new_d_And(dbgi, left, right, mode);
2301 case EXPR_BINARY_BITWISE_OR:
2302 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2303 return new_d_Or(dbgi, left, right, mode);
2304 case EXPR_BINARY_BITWISE_XOR:
2305 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2306 return new_d_Eor(dbgi, left, right, mode);
2307 case EXPR_BINARY_SHIFTLEFT:
2308 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2309 return new_d_Shl(dbgi, left, right, mode);
2310 case EXPR_BINARY_SHIFTRIGHT:
2311 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2312 if (mode_is_signed(mode)) {
2313 return new_d_Shrs(dbgi, left, right, mode);
2315 return new_d_Shr(dbgi, left, right, mode);
2317 case EXPR_BINARY_DIV:
2318 case EXPR_BINARY_DIV_ASSIGN: {
2319 ir_node *pin = new_Pin(new_NoMem());
2322 if (mode_is_float(mode)) {
2323 op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
2324 res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
2326 op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
2327 res = new_d_Proj(dbgi, op, mode, pn_Div_res);
2331 case EXPR_BINARY_MOD:
2332 case EXPR_BINARY_MOD_ASSIGN: {
2333 ir_node *pin = new_Pin(new_NoMem());
2334 assert(!mode_is_float(mode));
2335 ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
2336 op_pin_state_floats);
2337 ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
2341 panic("unexpected expression kind");
2345 static ir_node *create_lazy_op(const binary_expression_t *expression)
2347 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2348 type_t *type = expression->base.type;
2349 ir_mode *mode = get_ir_mode(type);
2351 if (is_constant_expression(expression->left)) {
2352 long val = fold_constant(expression->left);
2353 expression_kind_t ekind = expression->base.kind;
2354 assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
2355 if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
2356 (ekind == EXPR_BINARY_LOGICAL_OR && val == 0)) {
2357 return expression_to_firm(expression->right);
2359 return new_Const(mode, get_mode_one(mode));
2363 return produce_condition_result((const expression_t*) expression, dbgi);
2366 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
2367 ir_node *right, ir_mode *mode);
2369 static ir_node *create_assign_binop(const binary_expression_t *expression)
2371 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2372 const expression_t *left_expr = expression->left;
2373 ir_mode *left_mode = get_ir_mode(left_expr->base.type);
2374 ir_node *right = expression_to_firm(expression->right);
2375 ir_node *left_addr = expression_to_addr(left_expr);
2376 ir_node *left = get_value_from_lvalue(left_expr, left_addr);
2377 ir_node *result = create_op(dbgi, expression, left, right);
2379 result = create_conv(dbgi, result, left_mode);
2380 result = do_strict_conv(dbgi, result);
2382 set_value_for_expression_addr(left_expr, result, left_addr);
2387 static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
2389 expression_kind_t kind = expression->base.kind;
2392 case EXPR_BINARY_EQUAL:
2393 case EXPR_BINARY_NOTEQUAL:
2394 case EXPR_BINARY_LESS:
2395 case EXPR_BINARY_LESSEQUAL:
2396 case EXPR_BINARY_GREATER:
2397 case EXPR_BINARY_GREATEREQUAL:
2398 case EXPR_BINARY_ISGREATER:
2399 case EXPR_BINARY_ISGREATEREQUAL:
2400 case EXPR_BINARY_ISLESS:
2401 case EXPR_BINARY_ISLESSEQUAL:
2402 case EXPR_BINARY_ISLESSGREATER:
2403 case EXPR_BINARY_ISUNORDERED: {
2404 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2405 ir_node *left = expression_to_firm(expression->left);
2406 ir_node *right = expression_to_firm(expression->right);
2407 ir_node *cmp = new_d_Cmp(dbgi, left, right);
2408 long pnc = get_pnc(kind, expression->left->base.type);
2409 ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
2412 case EXPR_BINARY_ASSIGN: {
2413 ir_node *addr = expression_to_addr(expression->left);
2414 ir_node *right = expression_to_firm(expression->right);
2415 set_value_for_expression_addr(expression->left, right, addr);
2419 case EXPR_BINARY_ADD:
2420 case EXPR_BINARY_SUB:
2421 case EXPR_BINARY_MUL:
2422 case EXPR_BINARY_DIV:
2423 case EXPR_BINARY_MOD:
2424 case EXPR_BINARY_BITWISE_AND:
2425 case EXPR_BINARY_BITWISE_OR:
2426 case EXPR_BINARY_BITWISE_XOR:
2427 case EXPR_BINARY_SHIFTLEFT:
2428 case EXPR_BINARY_SHIFTRIGHT:
2430 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2431 ir_node *left = expression_to_firm(expression->left);
2432 ir_node *right = expression_to_firm(expression->right);
2433 return create_op(dbgi, expression, left, right);
2435 case EXPR_BINARY_LOGICAL_AND:
2436 case EXPR_BINARY_LOGICAL_OR:
2437 return create_lazy_op(expression);
2438 case EXPR_BINARY_COMMA:
2439 /* create side effects of left side */
2440 (void) expression_to_firm(expression->left);
2441 return _expression_to_firm(expression->right);
2443 case EXPR_BINARY_ADD_ASSIGN:
2444 case EXPR_BINARY_SUB_ASSIGN:
2445 case EXPR_BINARY_MUL_ASSIGN:
2446 case EXPR_BINARY_MOD_ASSIGN:
2447 case EXPR_BINARY_DIV_ASSIGN:
2448 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2449 case EXPR_BINARY_BITWISE_OR_ASSIGN:
2450 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2451 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2452 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2453 return create_assign_binop(expression);
2454 case EXPR_BINARY_BUILTIN_EXPECT:
2455 return _expression_to_firm(expression->left);
2457 panic("TODO binexpr type");
2461 static ir_node *array_access_addr(const array_access_expression_t *expression)
2463 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2464 ir_node *base_addr = expression_to_firm(expression->array_ref);
2465 ir_node *offset = expression_to_firm(expression->index);
2467 type_t *offset_type = skip_typeref(expression->index->base.type);
2469 if (is_type_signed(offset_type)) {
2470 mode = get_ir_mode(type_ssize_t);
2472 mode = get_ir_mode(type_size_t);
2474 offset = create_conv(dbgi, offset, mode);
2476 type_t *ref_type = skip_typeref(expression->array_ref->base.type);
2477 assert(is_type_pointer(ref_type));
2478 pointer_type_t *pointer_type = &ref_type->pointer;
2480 ir_node *elem_size_const = get_type_size(pointer_type->points_to);
2481 elem_size_const = create_conv(dbgi, elem_size_const, mode);
2482 ir_node *real_offset = new_d_Mul(dbgi, offset, elem_size_const,
2484 ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
2489 static ir_node *array_access_to_firm(
2490 const array_access_expression_t *expression)
2492 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2493 ir_node *addr = array_access_addr(expression);
2494 type_t *type = revert_automatic_type_conversion(
2495 (const expression_t*) expression);
2496 type = skip_typeref(type);
2498 return deref_address(dbgi, type, addr);
2501 static long get_offsetof_offset(const offsetof_expression_t *expression)
2503 type_t *orig_type = expression->type;
2506 designator_t *designator = expression->designator;
2507 for( ; designator != NULL; designator = designator->next) {
2508 type_t *type = skip_typeref(orig_type);
2509 /* be sure the type is constructed */
2510 (void) get_ir_type(type);
2512 if (designator->symbol != NULL) {
2513 assert(is_type_compound(type));
2514 symbol_t *symbol = designator->symbol;
2516 declaration_t *declaration = type->compound.declaration;
2517 declaration_t *iter = declaration->scope.declarations;
2518 for( ; iter != NULL; iter = iter->next) {
2519 if (iter->symbol == symbol) {
2523 assert(iter != NULL);
2525 assert(iter->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2526 offset += get_entity_offset(iter->v.entity);
2528 orig_type = iter->type;
2530 expression_t *array_index = designator->array_index;
2531 assert(designator->array_index != NULL);
2532 assert(is_type_array(type));
2533 assert(is_type_valid(array_index->base.type));
2535 long index = fold_constant(array_index);
2536 ir_type *arr_type = get_ir_type(type);
2537 ir_type *elem_type = get_array_element_type(arr_type);
2538 long elem_size = get_type_size_bytes(elem_type);
2540 offset += index * elem_size;
2542 orig_type = type->array.element_type;
2549 static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
2551 ir_mode *mode = get_ir_mode(expression->base.type);
2552 long offset = get_offsetof_offset(expression);
2553 tarval *tv = new_tarval_from_long(offset, mode);
2554 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2556 return new_d_Const(dbgi, mode, tv);
2559 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
2560 ir_entity *entity, type_t *type);
2562 static ir_node *compound_literal_to_firm(
2563 const compound_literal_expression_t *expression)
2565 type_t *type = expression->type;
2567 /* create an entity on the stack */
2568 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
2570 ident *const id = id_unique("CompLit.%u");
2571 ir_type *const irtype = get_ir_type(type);
2572 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2573 ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
2574 set_entity_ld_ident(entity, id);
2576 set_entity_variability(entity, variability_uninitialized);
2578 /* create initialisation code */
2579 initializer_t *initializer = expression->initializer;
2580 create_local_initializer(initializer, dbgi, entity, type);
2582 /* create a sel for the compound literal address */
2583 ir_node *frame = get_local_frame(entity);
2584 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
2589 * Transform a sizeof expression into Firm code.
2591 static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
2593 type_t *type = expression->type;
2595 type = expression->tp_expression->base.type;
2596 assert(type != NULL);
2599 type = skip_typeref(type);
2600 /* ยง 6.5.3.4 (2) if the type is a VLA, evaluate the expression. */
2601 if (is_type_array(type) && type->array.is_vla
2602 && expression->tp_expression != NULL) {
2603 expression_to_firm(expression->tp_expression);
2606 return get_type_size(type);
2610 * Transform an alignof expression into Firm code.
2612 static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
2614 type_t *type = expression->type;
2616 /* beware: if expression is a variable reference, return the
2617 alignment of the variable. */
2618 const expression_t *tp_expression = expression->tp_expression;
2619 const declaration_t *declaration = expr_is_variable(tp_expression);
2620 if (declaration != NULL) {
2621 /* TODO: get the alignment of this variable. */
2623 type = tp_expression->base.type;
2624 assert(type != NULL);
2627 ir_mode *const mode = get_ir_mode(expression->base.type);
2628 symconst_symbol sym;
2629 sym.type_p = get_ir_type(type);
2630 return new_SymConst(mode, sym, symconst_type_align);
2633 static void init_ir_types(void);
2635 long fold_constant(const expression_t *expression)
2637 bool constant_folding_old = constant_folding;
2638 constant_folding = true;
2642 assert(is_constant_expression(expression));
2644 ir_graph *old_current_ir_graph = current_ir_graph;
2645 if (current_ir_graph == NULL) {
2646 current_ir_graph = get_const_code_irg();
2649 ir_node *cnst = expression_to_firm(expression);
2650 current_ir_graph = old_current_ir_graph;
2652 if (!is_Const(cnst)) {
2653 panic("couldn't fold constant\n");
2656 tarval *tv = get_Const_tarval(cnst);
2657 if (!tarval_is_long(tv)) {
2658 panic("result of constant folding is not integer\n");
2661 constant_folding = constant_folding_old;
2663 return get_tarval_long(tv);
2666 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
2668 dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
2670 /* first try to fold a constant condition */
2671 if (is_constant_expression(expression->condition)) {
2672 long val = fold_constant(expression->condition);
2674 expression_t *true_expression = expression->true_expression;
2675 if (true_expression == NULL)
2676 true_expression = expression->condition;
2677 return expression_to_firm(true_expression);
2679 return expression_to_firm(expression->false_expression);
2683 ir_node *cur_block = get_cur_block();
2685 /* create the true block */
2686 ir_node *true_block = new_immBlock();
2688 ir_node *true_val = expression->true_expression != NULL ?
2689 expression_to_firm(expression->true_expression) : NULL;
2690 ir_node *true_jmp = new_Jmp();
2692 /* create the false block */
2693 ir_node *false_block = new_immBlock();
2695 ir_node *false_val = expression_to_firm(expression->false_expression);
2696 ir_node *false_jmp = new_Jmp();
2698 /* create the condition evaluation */
2699 set_cur_block(cur_block);
2700 ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
2701 if (expression->true_expression == NULL) {
2702 if (cond_expr != NULL) {
2703 true_val = cond_expr;
2705 /* Condition ended with a short circuit (&&, ||, !) operation.
2706 * Generate a "1" as value for the true branch. */
2707 ir_mode *const mode = mode_Is;
2708 true_val = new_Const(mode, get_mode_one(mode));
2711 mature_immBlock(true_block);
2712 mature_immBlock(false_block);
2714 /* create the common block */
2715 ir_node *in_cf[2] = { true_jmp, false_jmp };
2716 new_Block(2, in_cf);
2718 /* TODO improve static semantics, so either both or no values are NULL */
2719 if (true_val == NULL || false_val == NULL)
2722 ir_node *in[2] = { true_val, false_val };
2723 ir_mode *mode = get_irn_mode(true_val);
2724 assert(get_irn_mode(false_val) == mode);
2725 ir_node *val = new_d_Phi(dbgi, 2, in, mode);
2731 * Returns an IR-node representing the address of a field.
2733 static ir_node *select_addr(const select_expression_t *expression)
2735 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2737 ir_node *compound_addr = expression_to_firm(expression->compound);
2739 /* make sure the type is constructed */
2740 type_t *type = skip_typeref(expression->compound->base.type);
2741 if (is_type_pointer(type)) {
2742 type = type->pointer.points_to;
2744 (void) get_ir_type(type);
2746 declaration_t *entry = expression->compound_entry;
2747 assert(entry->declaration_kind == DECLARATION_KIND_COMPOUND_MEMBER);
2748 ir_entity *entity = entry->v.entity;
2750 assert(entity != NULL);
2752 ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
2757 static ir_node *select_to_firm(const select_expression_t *expression)
2759 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
2760 ir_node *addr = select_addr(expression);
2761 type_t *type = revert_automatic_type_conversion(
2762 (const expression_t*) expression);
2763 type = skip_typeref(type);
2765 declaration_t *entry = expression->compound_entry;
2766 type_t *entry_type = skip_typeref(entry->type);
2768 if (entry_type->kind == TYPE_BITFIELD) {
2769 return bitfield_extract_to_firm(expression, addr);
2772 return deref_address(dbgi, type, addr);
2775 /* Values returned by __builtin_classify_type. */
2776 typedef enum gcc_type_class
2782 enumeral_type_class,
2785 reference_type_class,
2789 function_type_class,
2800 static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
2802 const type_t *const type = skip_typeref(expr->type_expression->base.type);
2808 const atomic_type_t *const atomic_type = &type->atomic;
2809 switch (atomic_type->akind) {
2810 /* should not be reached */
2811 case ATOMIC_TYPE_INVALID:
2815 /* gcc cannot do that */
2816 case ATOMIC_TYPE_VOID:
2817 tc = void_type_class;
2820 case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
2821 case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
2822 case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
2823 case ATOMIC_TYPE_SHORT:
2824 case ATOMIC_TYPE_USHORT:
2825 case ATOMIC_TYPE_INT:
2826 case ATOMIC_TYPE_UINT:
2827 case ATOMIC_TYPE_LONG:
2828 case ATOMIC_TYPE_ULONG:
2829 case ATOMIC_TYPE_LONGLONG:
2830 case ATOMIC_TYPE_ULONGLONG:
2831 case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
2832 tc = integer_type_class;
2835 case ATOMIC_TYPE_FLOAT:
2836 case ATOMIC_TYPE_DOUBLE:
2837 case ATOMIC_TYPE_LONG_DOUBLE:
2838 tc = real_type_class;
2841 panic("Unexpected atomic type in classify_type_to_firm().");
2844 case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
2845 case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
2846 case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
2847 case TYPE_ARRAY: /* gcc handles this as pointer */
2848 case TYPE_FUNCTION: /* gcc handles this as pointer */
2849 case TYPE_POINTER: tc = pointer_type_class; goto make_const;
2850 case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
2851 case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
2853 /* gcc handles this as integer */
2854 case TYPE_ENUM: tc = integer_type_class; goto make_const;
2857 /* typedef/typeof should be skipped already */
2864 panic("unexpected TYPE classify_type_to_firm().");
2867 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2868 ir_mode *const mode = mode_int;
2869 tarval *const tv = new_tarval_from_long(tc, mode);
2870 return new_d_Const(dbgi, mode, tv);
2873 static ir_node *function_name_to_firm(
2874 const funcname_expression_t *const expr)
2876 switch(expr->kind) {
2877 case FUNCNAME_FUNCTION:
2878 case FUNCNAME_PRETTY_FUNCTION:
2879 case FUNCNAME_FUNCDNAME:
2880 if (current_function_name == NULL) {
2881 const source_position_t *const src_pos = &expr->base.source_position;
2882 const char *const name = current_function_decl->symbol->string;
2883 const string_t string = { name, strlen(name) + 1 };
2884 current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
2886 return current_function_name;
2887 case FUNCNAME_FUNCSIG:
2888 if (current_funcsig == NULL) {
2889 const source_position_t *const src_pos = &expr->base.source_position;
2890 ir_entity *ent = get_irg_entity(current_ir_graph);
2891 const char *const name = get_entity_ld_name(ent);
2892 const string_t string = { name, strlen(name) + 1 };
2893 current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
2895 return current_funcsig;
2897 panic("Unsupported function name");
2900 static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
2902 statement_t *statement = expr->statement;
2904 assert(statement->kind == STATEMENT_COMPOUND);
2905 return compound_statement_to_firm(&statement->compound);
2908 static ir_node *va_start_expression_to_firm(
2909 const va_start_expression_t *const expr)
2911 ir_type *const method_type = get_ir_type(current_function_decl->type);
2912 int const n = get_method_n_params(method_type) - 1;
2913 ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
2914 ir_node *const arg_base = get_irg_value_param_base(current_ir_graph);
2915 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2916 ir_node *const no_mem = new_NoMem();
2917 ir_node *const arg_sel =
2918 new_d_simpleSel(dbgi, no_mem, arg_base, parm_ent);
2920 ir_node *const cnst = get_type_size(expr->parameter->type);
2921 ir_node *const add = new_d_Add(dbgi, arg_sel, cnst, mode_P_data);
2922 set_value_for_expression(expr->ap, add);
2927 static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
2929 type_t *const type = expr->base.type;
2930 expression_t *const ap_expr = expr->ap;
2931 ir_node *const ap_addr = expression_to_addr(ap_expr);
2932 ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
2933 dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
2934 ir_node *const res = deref_address(dbgi, type, ap);
2936 ir_node *const cnst = get_type_size(expr->base.type);
2937 ir_node *const add = new_d_Add(dbgi, ap, cnst, mode_P_data);
2939 set_value_for_expression_addr(ap_expr, add, ap_addr);
2944 static ir_node *dereference_addr(const unary_expression_t *const expression)
2946 assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
2947 return expression_to_firm(expression->value);
2951 * Returns a IR-node representing an lvalue of the given expression.
2953 static ir_node *expression_to_addr(const expression_t *expression)
2955 switch(expression->kind) {
2956 case EXPR_REFERENCE:
2957 return reference_addr(&expression->reference);
2958 case EXPR_ARRAY_ACCESS:
2959 return array_access_addr(&expression->array_access);
2961 return select_addr(&expression->select);
2963 return call_expression_to_firm(&expression->call);
2964 case EXPR_UNARY_DEREFERENCE: {
2965 return dereference_addr(&expression->unary);
2970 panic("trying to get address of non-lvalue");
2973 static ir_node *builtin_constant_to_firm(
2974 const builtin_constant_expression_t *expression)
2976 ir_mode *mode = get_ir_mode(expression->base.type);
2979 if (is_constant_expression(expression->value)) {
2984 return new_Const_long(mode, v);
2987 static ir_node *builtin_prefetch_to_firm(
2988 const builtin_prefetch_expression_t *expression)
2990 ir_node *adr = expression_to_firm(expression->adr);
2991 /* no Firm support for prefetch yet */
2996 static ir_node *get_label_block(declaration_t *label)
2998 assert(label->namespc == NAMESPACE_LABEL || label->namespc == NAMESPACE_LOCAL_LABEL);
3000 if (label->declaration_kind == DECLARATION_KIND_LABEL_BLOCK) {
3001 return label->v.block;
3003 assert(label->declaration_kind == DECLARATION_KIND_UNKNOWN);
3005 /* beware: might be called from create initializer with current_ir_graph
3006 * set to const_code_irg. */
3007 ir_graph *rem = current_ir_graph;
3008 current_ir_graph = current_function;
3010 ir_node *old_cur_block = get_cur_block();
3011 ir_node *block = new_immBlock();
3012 set_cur_block(old_cur_block);
3014 label->declaration_kind = DECLARATION_KIND_LABEL_BLOCK;
3015 label->v.block = block;
3017 ARR_APP1(declaration_t *, all_labels, label);
3019 current_ir_graph = rem;
3024 * Pointer to a label. This is used for the
3025 * GNU address-of-label extension.
3027 static ir_node *label_address_to_firm(
3028 const label_address_expression_t *label)
3030 ir_node *block = get_label_block(label->declaration);
3031 ir_label_t nr = get_Block_label(block);
3034 nr = get_irp_next_label_nr();
3035 set_Block_label(block, nr);
3037 symconst_symbol value;
3039 return new_SymConst(mode_P_code, value, symconst_label);
3043 * creates firm nodes for an expression. The difference between this function
3044 * and expression_to_firm is, that this version might produce mode_b nodes
3045 * instead of mode_Is.
3047 static ir_node *_expression_to_firm(const expression_t *expression)
3050 if (!constant_folding) {
3051 assert(!expression->base.transformed);
3052 ((expression_t*) expression)->base.transformed = true;
3056 switch (expression->kind) {
3057 case EXPR_CHARACTER_CONSTANT:
3058 return character_constant_to_firm(&expression->conste);
3059 case EXPR_WIDE_CHARACTER_CONSTANT:
3060 return wide_character_constant_to_firm(&expression->conste);
3062 return const_to_firm(&expression->conste);
3063 case EXPR_STRING_LITERAL:
3064 return string_literal_to_firm(&expression->string);
3065 case EXPR_WIDE_STRING_LITERAL:
3066 return wide_string_literal_to_firm(&expression->wide_string);
3067 case EXPR_REFERENCE:
3068 return reference_expression_to_firm(&expression->reference);
3070 return call_expression_to_firm(&expression->call);
3072 return unary_expression_to_firm(&expression->unary);
3074 return binary_expression_to_firm(&expression->binary);
3075 case EXPR_ARRAY_ACCESS:
3076 return array_access_to_firm(&expression->array_access);
3078 return sizeof_to_firm(&expression->typeprop);
3080 return alignof_to_firm(&expression->typeprop);
3081 case EXPR_CONDITIONAL:
3082 return conditional_to_firm(&expression->conditional);
3084 return select_to_firm(&expression->select);
3085 case EXPR_CLASSIFY_TYPE:
3086 return classify_type_to_firm(&expression->classify_type);
3088 return function_name_to_firm(&expression->funcname);
3089 case EXPR_STATEMENT:
3090 return statement_expression_to_firm(&expression->statement);
3092 return va_start_expression_to_firm(&expression->va_starte);
3094 return va_arg_expression_to_firm(&expression->va_arge);
3095 case EXPR_BUILTIN_SYMBOL:
3096 panic("unimplemented expression found");
3097 case EXPR_BUILTIN_CONSTANT_P:
3098 return builtin_constant_to_firm(&expression->builtin_constant);
3099 case EXPR_BUILTIN_PREFETCH:
3100 return builtin_prefetch_to_firm(&expression->builtin_prefetch);
3102 return offsetof_to_firm(&expression->offsetofe);
3103 case EXPR_COMPOUND_LITERAL:
3104 return compound_literal_to_firm(&expression->compound_literal);
3105 case EXPR_LABEL_ADDRESS:
3106 return label_address_to_firm(&expression->label_address);
3112 panic("invalid expression found");
3115 static bool produces_mode_b(const expression_t *expression)
3117 switch (expression->kind) {
3118 case EXPR_BINARY_EQUAL:
3119 case EXPR_BINARY_NOTEQUAL:
3120 case EXPR_BINARY_LESS:
3121 case EXPR_BINARY_LESSEQUAL:
3122 case EXPR_BINARY_GREATER:
3123 case EXPR_BINARY_GREATEREQUAL:
3124 case EXPR_BINARY_ISGREATER:
3125 case EXPR_BINARY_ISGREATEREQUAL:
3126 case EXPR_BINARY_ISLESS:
3127 case EXPR_BINARY_ISLESSEQUAL:
3128 case EXPR_BINARY_ISLESSGREATER:
3129 case EXPR_BINARY_ISUNORDERED:
3130 case EXPR_UNARY_NOT:
3133 case EXPR_BINARY_BUILTIN_EXPECT:
3134 return produces_mode_b(expression->binary.left);
3135 case EXPR_BINARY_COMMA:
3136 return produces_mode_b(expression->binary.right);
3143 static ir_node *expression_to_firm(const expression_t *expression)
3145 if (!produces_mode_b(expression)) {
3146 ir_node *res = _expression_to_firm(expression);
3147 assert(res == NULL || get_irn_mode(res) != mode_b);
3151 if (is_constant_expression(expression)) {
3152 ir_node *res = _expression_to_firm(expression);
3153 ir_mode *mode = get_ir_mode(expression->base.type);
3154 assert(is_Const(res));
3155 if (is_Const_null(res)) {
3156 return new_Const_long(mode, 0);
3158 return new_Const_long(mode, 1);
3162 /* we have to produce a 0/1 from the mode_b expression */
3163 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3164 return produce_condition_result(expression, dbgi);
3168 * create a short-circuit expression evaluation that tries to construct
3169 * efficient control flow structures for &&, || and ! expressions
3171 static ir_node *create_condition_evaluation(const expression_t *expression,
3172 ir_node *true_block,
3173 ir_node *false_block)
3175 switch(expression->kind) {
3176 case EXPR_UNARY_NOT: {
3177 const unary_expression_t *unary_expression = &expression->unary;
3178 create_condition_evaluation(unary_expression->value, false_block,
3182 case EXPR_BINARY_LOGICAL_AND: {
3183 const binary_expression_t *binary_expression = &expression->binary;
3185 ir_node *cur_block = get_cur_block();
3186 ir_node *extra_block = new_immBlock();
3187 set_cur_block(cur_block);
3188 create_condition_evaluation(binary_expression->left, extra_block,
3190 mature_immBlock(extra_block);
3191 set_cur_block(extra_block);
3192 create_condition_evaluation(binary_expression->right, true_block,
3196 case EXPR_BINARY_LOGICAL_OR: {
3197 const binary_expression_t *binary_expression = &expression->binary;
3199 ir_node *cur_block = get_cur_block();
3200 ir_node *extra_block = new_immBlock();
3201 set_cur_block(cur_block);
3202 create_condition_evaluation(binary_expression->left, true_block,
3204 mature_immBlock(extra_block);
3205 set_cur_block(extra_block);
3206 create_condition_evaluation(binary_expression->right, true_block,
3214 dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
3215 ir_node *cond_expr = _expression_to_firm(expression);
3216 ir_node *condition = create_conv(NULL, cond_expr, mode_b);
3217 ir_node *cond = new_d_Cond(dbgi, condition);
3218 ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
3219 ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
3221 /* set branch prediction info based on __builtin_expect */
3222 if (expression->kind == EXPR_BINARY_BUILTIN_EXPECT) {
3223 long cnst = fold_constant(expression->binary.right);
3224 cond_jmp_predicate pred;
3227 pred = COND_JMP_PRED_FALSE;
3229 pred = COND_JMP_PRED_TRUE;
3231 set_Cond_jmp_pred(cond, pred);
3234 add_immBlock_pred(true_block, true_proj);
3235 if (false_block != NULL) {
3236 add_immBlock_pred(false_block, false_proj);
3239 set_cur_block(NULL);
3245 static void create_declaration_entity(declaration_t *declaration,
3246 declaration_kind_t declaration_kind,
3247 ir_type *parent_type)
3249 type_t *const type = skip_typeref(declaration->type);
3250 if (is_type_function(type)) {
3251 (void) get_function_entity(declaration);
3255 ident *const id = new_id_from_str(declaration->symbol->string);
3256 ir_type *const irtype = get_ir_type(type);
3257 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3258 ir_entity *const entity = new_d_entity(parent_type, id, irtype, dbgi);
3260 declaration->declaration_kind = (unsigned char) declaration_kind;
3261 declaration->v.entity = entity;
3262 set_entity_variability(entity, variability_uninitialized);
3263 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
3264 if (parent_type == get_tls_type())
3265 set_entity_allocation(entity, allocation_automatic);
3266 else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
3267 set_entity_allocation(entity, allocation_static);
3269 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3270 set_entity_volatility(entity, volatility_is_volatile);
3275 typedef struct type_path_entry_t type_path_entry_t;
3276 struct type_path_entry_t {
3278 ir_initializer_t *initializer;
3280 declaration_t *compound_entry;
3283 typedef struct type_path_t type_path_t;
3284 struct type_path_t {
3285 type_path_entry_t *path;
3290 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
3292 size_t len = ARR_LEN(path->path);
3294 for(size_t i = 0; i < len; ++i) {
3295 const type_path_entry_t *entry = & path->path[i];
3297 type_t *type = skip_typeref(entry->type);
3298 if (is_type_compound(type)) {
3299 fprintf(stderr, ".%s", entry->compound_entry->symbol->string);
3300 } else if (is_type_array(type)) {
3301 fprintf(stderr, "[%zd]", entry->index);
3303 fprintf(stderr, "-INVALID-");
3306 fprintf(stderr, " (");
3307 print_type(path->top_type);
3308 fprintf(stderr, ")");
3311 static type_path_entry_t *get_type_path_top(const type_path_t *path)
3313 size_t len = ARR_LEN(path->path);
3315 return & path->path[len-1];
3318 static type_path_entry_t *append_to_type_path(type_path_t *path)
3320 size_t len = ARR_LEN(path->path);
3321 ARR_RESIZE(type_path_entry_t, path->path, len+1);
3323 type_path_entry_t *result = & path->path[len];
3324 memset(result, 0, sizeof(result[0]));
3328 static size_t get_compound_size(const compound_type_t *type)
3330 declaration_t *declaration = type->declaration;
3331 declaration_t *member = declaration->scope.declarations;
3333 for( ; member != NULL; member = member->next) {
3336 /* TODO: cache results? */
3341 static ir_initializer_t *get_initializer_entry(type_path_t *path)
3343 type_t *orig_top_type = path->top_type;
3344 type_t *top_type = skip_typeref(orig_top_type);
3346 assert(is_type_compound(top_type) || is_type_array(top_type));
3348 if (ARR_LEN(path->path) == 0) {
3351 type_path_entry_t *top = get_type_path_top(path);
3352 ir_initializer_t *initializer = top->initializer;
3353 return get_initializer_compound_value(initializer, top->index);
3357 static void descend_into_subtype(type_path_t *path)
3359 type_t *orig_top_type = path->top_type;
3360 type_t *top_type = skip_typeref(orig_top_type);
3362 assert(is_type_compound(top_type) || is_type_array(top_type));
3364 ir_initializer_t *initializer = get_initializer_entry(path);
3366 type_path_entry_t *top = append_to_type_path(path);
3367 top->type = top_type;
3371 if (is_type_compound(top_type)) {
3372 declaration_t *declaration = top_type->compound.declaration;
3373 declaration_t *entry = declaration->scope.declarations;
3375 top->compound_entry = entry;
3377 len = get_compound_size(&top_type->compound);
3379 path->top_type = entry->type;
3381 assert(is_type_array(top_type));
3382 assert(top_type->array.size > 0);
3385 path->top_type = top_type->array.element_type;
3386 len = top_type->array.size;
3388 if (initializer == NULL
3389 || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
3390 initializer = create_initializer_compound(len);
3391 /* we have to set the entry at the 2nd latest path entry... */
3392 size_t path_len = ARR_LEN(path->path);
3393 assert(path_len >= 1);
3395 type_path_entry_t *entry = & path->path[path_len-2];
3396 ir_initializer_t *tinitializer = entry->initializer;
3397 set_initializer_compound_value(tinitializer, entry->index,
3401 top->initializer = initializer;
3404 static void ascend_from_subtype(type_path_t *path)
3406 type_path_entry_t *top = get_type_path_top(path);
3408 path->top_type = top->type;
3410 size_t len = ARR_LEN(path->path);
3411 ARR_RESIZE(type_path_entry_t, path->path, len-1);
3414 static void walk_designator(type_path_t *path, const designator_t *designator)
3416 /* designators start at current object type */
3417 ARR_RESIZE(type_path_entry_t, path->path, 1);
3419 for( ; designator != NULL; designator = designator->next) {
3420 type_path_entry_t *top = get_type_path_top(path);
3421 type_t *orig_type = top->type;
3422 type_t *type = skip_typeref(orig_type);
3424 if (designator->symbol != NULL) {
3425 assert(is_type_compound(type));
3427 symbol_t *symbol = designator->symbol;
3429 declaration_t *declaration = type->compound.declaration;
3430 declaration_t *iter = declaration->scope.declarations;
3431 for( ; iter != NULL; iter = iter->next, ++index) {
3432 if (iter->symbol == symbol) {
3436 assert(iter != NULL);
3438 top->type = orig_type;
3439 top->compound_entry = iter;
3441 orig_type = iter->type;
3443 expression_t *array_index = designator->array_index;
3444 assert(designator->array_index != NULL);
3445 assert(is_type_array(type));
3446 assert(is_type_valid(array_index->base.type));
3448 long index = fold_constant(array_index);
3451 if (type->array.size_constant) {
3452 long array_size = type->array.size;
3453 assert(index < array_size);
3457 top->type = orig_type;
3458 top->index = (size_t) index;
3459 orig_type = type->array.element_type;
3461 path->top_type = orig_type;
3463 if (designator->next != NULL) {
3464 descend_into_subtype(path);
3468 path->invalid = false;
3471 static void advance_current_object(type_path_t *path)
3473 if (path->invalid) {
3474 /* TODO: handle this... */
3475 panic("invalid initializer in ast2firm (excessive elements)");
3479 type_path_entry_t *top = get_type_path_top(path);
3481 type_t *type = skip_typeref(top->type);
3482 if (is_type_union(type)) {
3483 top->compound_entry = NULL;
3484 } else if (is_type_struct(type)) {
3485 declaration_t *entry = top->compound_entry;
3488 entry = entry->next;
3489 top->compound_entry = entry;
3490 if (entry != NULL) {
3491 path->top_type = entry->type;
3495 assert(is_type_array(type));
3498 if (!type->array.size_constant || top->index < type->array.size) {
3503 /* we're past the last member of the current sub-aggregate, try if we
3504 * can ascend in the type hierarchy and continue with another subobject */
3505 size_t len = ARR_LEN(path->path);
3508 ascend_from_subtype(path);
3509 advance_current_object(path);
3511 path->invalid = true;
3516 static ir_initializer_t *create_ir_initializer(
3517 const initializer_t *initializer, type_t *type);
3519 static ir_initializer_t *create_ir_initializer_value(
3520 const initializer_value_t *initializer)
3522 if (is_type_compound(initializer->value->base.type)) {
3523 panic("initializer creation for compounds not implemented yet");
3525 ir_node *value = expression_to_firm(initializer->value);
3526 return create_initializer_const(value);
3529 static ir_initializer_t *create_ir_initializer_list(
3530 const initializer_list_t *initializer, type_t *type)
3533 memset(&path, 0, sizeof(path));
3534 path.top_type = type;
3535 path.path = NEW_ARR_F(type_path_entry_t, 0);
3537 descend_into_subtype(&path);
3539 for(size_t i = 0; i < initializer->len; ++i) {
3540 const initializer_t *sub_initializer = initializer->initializers[i];
3542 if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
3543 walk_designator(&path, sub_initializer->designator.designator);
3547 if (sub_initializer->kind == INITIALIZER_VALUE) {
3548 /* we might have to descend into types until we're at a scalar
3551 type_t *orig_top_type = path.top_type;
3552 type_t *top_type = skip_typeref(orig_top_type);
3554 if (is_type_scalar(top_type))
3556 descend_into_subtype(&path);
3560 ir_initializer_t *sub_irinitializer
3561 = create_ir_initializer(sub_initializer, path.top_type);
3563 size_t path_len = ARR_LEN(path.path);
3564 assert(path_len >= 1);
3565 type_path_entry_t *entry = & path.path[path_len-1];
3566 ir_initializer_t *tinitializer = entry->initializer;
3567 set_initializer_compound_value(tinitializer, entry->index,
3570 advance_current_object(&path);
3573 assert(ARR_LEN(path.path) >= 1);
3574 ir_initializer_t *result = path.path[0].initializer;
3575 DEL_ARR_F(path.path);
3580 static ir_initializer_t *create_ir_initializer_string(
3581 const initializer_string_t *initializer, type_t *type)
3583 type = skip_typeref(type);
3585 size_t string_len = initializer->string.size;
3586 assert(type->kind == TYPE_ARRAY);
3587 assert(type->array.size_constant);
3588 size_t len = type->array.size;
3589 ir_initializer_t *irinitializer = create_initializer_compound(len);
3591 const char *string = initializer->string.begin;
3592 ir_mode *mode = get_ir_mode(type->array.element_type);
3594 for(size_t i = 0; i < len; ++i) {
3599 tarval *tv = new_tarval_from_long(c, mode);
3600 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3602 set_initializer_compound_value(irinitializer, i, char_initializer);
3605 return irinitializer;
3608 static ir_initializer_t *create_ir_initializer_wide_string(
3609 const initializer_wide_string_t *initializer, type_t *type)
3611 size_t string_len = initializer->string.size;
3612 assert(type->kind == TYPE_ARRAY);
3613 assert(type->array.size_constant);
3614 size_t len = type->array.size;
3615 ir_initializer_t *irinitializer = create_initializer_compound(len);
3617 const wchar_rep_t *string = initializer->string.begin;
3618 ir_mode *mode = get_type_mode(ir_type_wchar_t);
3620 for(size_t i = 0; i < len; ++i) {
3622 if (i < string_len) {
3625 tarval *tv = new_tarval_from_long(c, mode);
3626 ir_initializer_t *char_initializer = create_initializer_tarval(tv);
3628 set_initializer_compound_value(irinitializer, i, char_initializer);
3631 return irinitializer;
3634 static ir_initializer_t *create_ir_initializer(
3635 const initializer_t *initializer, type_t *type)
3637 switch(initializer->kind) {
3638 case INITIALIZER_STRING:
3639 return create_ir_initializer_string(&initializer->string, type);
3641 case INITIALIZER_WIDE_STRING:
3642 return create_ir_initializer_wide_string(&initializer->wide_string,
3645 case INITIALIZER_LIST:
3646 return create_ir_initializer_list(&initializer->list, type);
3648 case INITIALIZER_VALUE:
3649 return create_ir_initializer_value(&initializer->value);
3651 case INITIALIZER_DESIGNATOR:
3652 panic("unexpected designator initializer found");
3654 panic("unknown initializer");
3657 static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
3660 if (is_atomic_type(type)) {
3661 ir_mode *mode = get_type_mode(type);
3662 tarval *zero = get_mode_null(mode);
3663 ir_node *cnst = new_d_Const(dbgi, mode, zero);
3665 /* TODO: bitfields */
3666 ir_node *mem = get_store();
3667 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3668 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3671 assert(is_compound_type(type));
3674 if (is_Array_type(type)) {
3675 assert(has_array_upper_bound(type, 0));
3676 n_members = get_array_upper_bound_int(type, 0);
3678 n_members = get_compound_n_members(type);
3681 for(int i = 0; i < n_members; ++i) {
3684 if (is_Array_type(type)) {
3685 ir_entity *entity = get_array_element_entity(type);
3686 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3687 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3688 ir_node *in[1] = { cnst };
3689 irtype = get_array_element_type(type);
3690 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3692 ir_entity *member = get_compound_member(type, i);
3694 irtype = get_entity_type(member);
3695 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3698 create_dynamic_null_initializer(irtype, dbgi, addr);
3703 static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
3704 ir_type *type, dbg_info *dbgi, ir_node *base_addr)
3706 switch(get_initializer_kind(initializer)) {
3707 case IR_INITIALIZER_NULL: {
3708 create_dynamic_null_initializer(type, dbgi, base_addr);
3711 case IR_INITIALIZER_CONST: {
3712 ir_node *node = get_initializer_const_value(initializer);
3713 ir_mode *mode = get_irn_mode(node);
3714 assert(get_type_mode(type) == mode);
3716 /* TODO: bitfields... */
3717 ir_node *mem = get_store();
3718 ir_node *store = new_d_Store(dbgi, mem, base_addr, node);
3719 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3723 case IR_INITIALIZER_TARVAL: {
3724 tarval *tv = get_initializer_tarval_value(initializer);
3725 ir_mode *mode = get_tarval_mode(tv);
3726 ir_node *cnst = new_d_Const(dbgi, mode, tv);
3727 assert(get_type_mode(type) == mode);
3729 /* TODO: bitfields... */
3730 ir_node *mem = get_store();
3731 ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst);
3732 ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
3736 case IR_INITIALIZER_COMPOUND: {
3737 assert(is_compound_type(type));
3739 if (is_Array_type(type)) {
3740 assert(has_array_upper_bound(type, 0));
3741 n_members = get_array_upper_bound_int(type, 0);
3743 n_members = get_compound_n_members(type);
3746 if (get_initializer_compound_n_entries(initializer)
3747 != (unsigned) n_members)
3748 panic("initializer doesn't match compound type");
3750 for(int i = 0; i < n_members; ++i) {
3753 if (is_Array_type(type)) {
3754 ir_entity *entity = get_array_element_entity(type);
3755 tarval *index_tv = new_tarval_from_long(i, mode_uint);
3756 ir_node *cnst = new_d_Const(dbgi, mode_uint, index_tv);
3757 ir_node *in[1] = { cnst };
3758 irtype = get_array_element_type(type);
3759 addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
3761 ir_entity *member = get_compound_member(type, i);
3763 irtype = get_entity_type(member);
3764 addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
3767 ir_initializer_t *sub_init
3768 = get_initializer_compound_value(initializer, i);
3770 create_dynamic_initializer_sub(sub_init, irtype, dbgi, addr);
3776 panic("invalid IR_INITIALIZER found");
3779 static void create_dynamic_initializer(ir_initializer_t *initializer,
3780 dbg_info *dbgi, ir_entity *entity)
3782 ir_node *frame = get_local_frame(entity);
3783 ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
3784 ir_type *type = get_entity_type(entity);
3786 create_dynamic_initializer_sub(initializer, type, dbgi, base_addr);
3789 static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
3790 ir_entity *entity, type_t *type)
3792 ir_node *memory = get_store();
3793 ir_node *nomem = new_NoMem();
3794 ir_node *frame = get_irg_frame(current_ir_graph);
3795 ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
3797 if (initializer->kind == INITIALIZER_VALUE) {
3798 initializer_value_t *initializer_value = &initializer->value;
3800 ir_node *value = expression_to_firm(initializer_value->value);
3801 type = skip_typeref(type);
3802 assign_value(dbgi, addr, type, value);
3806 if (!is_constant_initializer(initializer)) {
3807 ir_initializer_t *irinitializer
3808 = create_ir_initializer(initializer, type);
3810 create_dynamic_initializer(irinitializer, dbgi, entity);
3814 /* create the ir_initializer */
3815 ir_graph *const old_current_ir_graph = current_ir_graph;
3816 current_ir_graph = get_const_code_irg();
3818 ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
3820 assert(current_ir_graph == get_const_code_irg());
3821 current_ir_graph = old_current_ir_graph;
3823 /* create a "template" entity which is copied to the entity on the stack */
3824 ident *const id = id_unique("initializer.%u");
3825 ir_type *const irtype = get_ir_type(type);
3826 ir_type *const global_type = get_glob_type();
3827 ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
3828 set_entity_ld_ident(init_entity, id);
3830 set_entity_variability(init_entity, variability_initialized);
3831 set_entity_visibility(init_entity, visibility_local);
3832 set_entity_allocation(init_entity, allocation_static);
3834 set_entity_initializer(init_entity, irinitializer);
3836 ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
3837 ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
3839 ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
3840 set_store(copyb_mem);
3843 static void create_initializer_local_variable_entity(declaration_t *declaration)
3845 initializer_t *initializer = declaration->init.initializer;
3846 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3847 ir_entity *entity = declaration->v.entity;
3848 type_t *type = declaration->type;
3849 create_local_initializer(initializer, dbgi, entity, type);
3852 static void create_declaration_initializer(declaration_t *declaration)
3854 initializer_t *initializer = declaration->init.initializer;
3855 if (initializer == NULL)
3858 declaration_kind_t declaration_kind
3859 = (declaration_kind_t) declaration->declaration_kind;
3860 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
3861 create_initializer_local_variable_entity(declaration);
3865 type_t *type = declaration->type;
3866 type_qualifiers_t tq = get_type_qualifier(type, true);
3868 if (initializer->kind == INITIALIZER_VALUE) {
3869 initializer_value_t *initializer_value = &initializer->value;
3871 = get_dbg_info(&declaration->source_position);
3873 ir_node *value = expression_to_firm(initializer_value->value);
3874 value = do_strict_conv(dbgi, value);
3876 if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
3877 set_value(declaration->v.value_number, value);
3879 assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3881 ir_entity *entity = declaration->v.entity;
3883 if (tq & TYPE_QUALIFIER_CONST) {
3884 set_entity_variability(entity, variability_constant);
3886 set_entity_variability(entity, variability_initialized);
3888 set_atomic_ent_value(entity, value);
3891 assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
3892 declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
3894 ir_entity *entity = declaration->v.entity;
3895 ir_initializer_t *irinitializer
3896 = create_ir_initializer(initializer, type);
3898 if (tq & TYPE_QUALIFIER_CONST) {
3899 set_entity_variability(entity, variability_constant);
3901 set_entity_variability(entity, variability_initialized);
3903 set_entity_initializer(entity, irinitializer);
3907 static void create_variable_length_array(declaration_t *declaration)
3909 /* initializers are not allowed for VLAs */
3910 assert(declaration->init.initializer == NULL);
3912 declaration->declaration_kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
3913 declaration->v.vla_base = NULL;
3915 /* TODO: record VLA somewhere so we create the free node when we leave
3919 static void allocate_variable_length_array(declaration_t *declaration)
3921 /* initializers are not allowed for VLAs */
3922 assert(declaration->init.initializer == NULL);
3923 assert(get_cur_block() != NULL);
3925 dbg_info *dbgi = get_dbg_info(&declaration->source_position);
3926 type_t *type = declaration->type;
3927 ir_type *el_type = get_ir_type(type->array.element_type);
3929 /* make sure size_node is calculated */
3930 get_type_size(type);
3931 ir_node *elems = type->array.size_node;
3932 ir_node *mem = get_store();
3933 ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
3935 ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
3936 ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
3939 assert(declaration->declaration_kind
3940 == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
3941 declaration->v.vla_base = addr;
3945 * Creates a Firm local variable from a declaration.
3947 static void create_local_variable(declaration_t *declaration)
3949 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3951 bool needs_entity = declaration->address_taken;
3952 type_t *type = skip_typeref(declaration->type);
3954 /* is it a variable length array? */
3955 if (is_type_array(type) && !type->array.size_constant) {
3956 create_variable_length_array(declaration);
3958 } else if (is_type_array(type) || is_type_compound(type)) {
3959 needs_entity = true;
3960 } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3961 needs_entity = true;
3965 ir_type *frame_type = get_irg_frame_type(current_ir_graph);
3966 create_declaration_entity(declaration,
3967 DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
3970 declaration->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
3971 declaration->v.value_number = next_value_number_function;
3972 set_irg_loc_description(current_ir_graph, next_value_number_function, declaration);
3973 ++next_value_number_function;
3977 static void create_local_static_variable(declaration_t *declaration)
3979 assert(declaration->declaration_kind == DECLARATION_KIND_UNKNOWN);
3981 type_t *const type = skip_typeref(declaration->type);
3982 ir_type *const global_type = get_glob_type();
3983 ir_type *const irtype = get_ir_type(type);
3984 dbg_info *const dbgi = get_dbg_info(&declaration->source_position);
3986 size_t l = strlen(declaration->symbol->string);
3987 char buf[l + sizeof(".%u")];
3988 snprintf(buf, sizeof(buf), "%s.%%u", declaration->symbol->string);
3989 ident *const id = id_unique(buf);
3991 ir_entity *const entity = new_d_entity(global_type, id, irtype, dbgi);
3993 if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
3994 set_entity_volatility(entity, volatility_is_volatile);
3997 declaration->declaration_kind = DECLARATION_KIND_GLOBAL_VARIABLE;
3998 declaration->v.entity = entity;
3999 set_entity_ld_ident(entity, create_ld_ident(entity, declaration));
4000 set_entity_variability(entity, variability_uninitialized);
4001 set_entity_visibility(entity, visibility_local);
4002 set_entity_allocation(entity, allocation_static);
4004 ir_graph *const old_current_ir_graph = current_ir_graph;
4005 current_ir_graph = get_const_code_irg();
4007 create_declaration_initializer(declaration);
4009 assert(current_ir_graph == get_const_code_irg());
4010 current_ir_graph = old_current_ir_graph;
4015 static void return_statement_to_firm(return_statement_t *statement)
4017 if (get_cur_block() == NULL)
4020 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4021 ir_type *func_irtype = get_ir_type(current_function_decl->type);
4026 if (get_method_n_ress(func_irtype) > 0) {
4027 ir_type *res_type = get_method_res_type(func_irtype, 0);
4029 if (statement->value != NULL) {
4030 ir_node *node = expression_to_firm(statement->value);
4031 node = do_strict_conv(dbgi, node);
4035 if (is_compound_type(res_type)) {
4038 mode = get_type_mode(res_type);
4040 in[0] = new_Unknown(mode);
4044 /* build return_value for its side effects */
4045 if (statement->value != NULL) {
4046 expression_to_firm(statement->value);
4051 ir_node *store = get_store();
4052 ir_node *ret = new_d_Return(dbgi, store, in_len, in);
4054 ir_node *end_block = get_irg_end_block(current_ir_graph);
4055 add_immBlock_pred(end_block, ret);
4057 set_cur_block(NULL);
4060 static ir_node *expression_statement_to_firm(expression_statement_t *statement)
4062 if (get_cur_block() == NULL)
4065 return expression_to_firm(statement->expression);
4068 static ir_node *compound_statement_to_firm(compound_statement_t *compound)
4070 declaration_t *declaration = compound->scope.declarations;
4071 for( ; declaration != NULL; declaration = declaration->next) {
4072 create_local_declaration(declaration);
4075 ir_node *result = NULL;
4076 statement_t *statement = compound->statements;
4077 for( ; statement != NULL; statement = statement->base.next) {
4078 if (statement->base.next == NULL
4079 && statement->kind == STATEMENT_EXPRESSION) {
4080 result = expression_statement_to_firm(
4081 &statement->expression);
4084 statement_to_firm(statement);
4090 static void create_global_variable(declaration_t *declaration)
4094 switch ((storage_class_tag_t)declaration->storage_class) {
4095 case STORAGE_CLASS_STATIC:
4096 vis = visibility_local;
4099 case STORAGE_CLASS_EXTERN:
4100 vis = visibility_external_allocated;
4103 case STORAGE_CLASS_NONE:
4104 vis = visibility_external_visible;
4107 case STORAGE_CLASS_THREAD:
4108 vis = visibility_external_visible;
4111 case STORAGE_CLASS_THREAD_EXTERN:
4112 vis = visibility_external_allocated;
4115 case STORAGE_CLASS_THREAD_STATIC:
4116 vis = visibility_local;
4120 var_type = get_tls_type();
4124 var_type = get_glob_type();
4128 create_declaration_entity(declaration,
4129 DECLARATION_KIND_GLOBAL_VARIABLE,
4131 if (!is_type_function(skip_typeref(declaration->type))) {
4132 set_entity_visibility(declaration->v.entity, vis);
4137 case STORAGE_CLASS_TYPEDEF:
4138 case STORAGE_CLASS_AUTO:
4139 case STORAGE_CLASS_REGISTER:
4140 case STORAGE_CLASS_ENUM_ENTRY:
4143 panic("Invalid storage class for global variable");
4146 static void create_local_declaration(declaration_t *declaration)
4148 if (declaration->namespc != NAMESPACE_NORMAL)
4150 /* construct type */
4151 (void) get_ir_type(declaration->type);
4152 if (declaration->symbol == NULL) {
4156 type_t *type = skip_typeref(declaration->type);
4158 switch ((storage_class_tag_t) declaration->storage_class) {
4159 case STORAGE_CLASS_STATIC:
4160 create_local_static_variable(declaration);
4162 case STORAGE_CLASS_EXTERN:
4163 create_global_variable(declaration);
4164 create_declaration_initializer(declaration);
4166 case STORAGE_CLASS_NONE:
4167 case STORAGE_CLASS_AUTO:
4168 case STORAGE_CLASS_REGISTER:
4169 if (is_type_function(type)) {
4170 if (declaration->init.statement != NULL) {
4171 get_function_entity(declaration);
4172 declaration->declaration_kind = DECLARATION_KIND_INNER_FUNCTION;
4173 enqueue_inner_function(declaration);
4175 get_function_entity(declaration);
4178 create_local_variable(declaration);
4181 case STORAGE_CLASS_ENUM_ENTRY:
4182 /* should already be handled */
4183 assert(declaration->declaration_kind == DECLARATION_KIND_ENUM_ENTRY);
4185 case STORAGE_CLASS_TYPEDEF:
4186 declaration->declaration_kind = DECLARATION_KIND_TYPE;
4188 case STORAGE_CLASS_THREAD:
4189 case STORAGE_CLASS_THREAD_EXTERN:
4190 case STORAGE_CLASS_THREAD_STATIC:
4193 panic("invalid storage class found");
4196 static void initialize_local_declaration(declaration_t *declaration)
4198 if (declaration->symbol == NULL || declaration->namespc != NAMESPACE_NORMAL)
4201 switch ((declaration_kind_t) declaration->declaration_kind) {
4202 case DECLARATION_KIND_LOCAL_VARIABLE:
4203 case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY:
4204 create_declaration_initializer(declaration);
4207 case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
4208 allocate_variable_length_array(declaration);
4211 case DECLARATION_KIND_LABEL_BLOCK:
4212 case DECLARATION_KIND_COMPOUND_MEMBER:
4213 case DECLARATION_KIND_GLOBAL_VARIABLE:
4214 case DECLARATION_KIND_COMPOUND_TYPE_INCOMPLETE:
4215 case DECLARATION_KIND_COMPOUND_TYPE_COMPLETE:
4216 case DECLARATION_KIND_FUNCTION:
4217 case DECLARATION_KIND_TYPE:
4218 case DECLARATION_KIND_ENUM_ENTRY:
4219 case DECLARATION_KIND_INNER_FUNCTION:
4222 case DECLARATION_KIND_UNKNOWN:
4223 panic("can't initialize unknown declaration");
4225 panic("invalid declaration kind");
4228 static void declaration_statement_to_firm(declaration_statement_t *statement)
4230 declaration_t *declaration = statement->declarations_begin;
4231 declaration_t *end = statement->declarations_end->next;
4232 for( ; declaration != end; declaration = declaration->next) {
4233 if (declaration->namespc != NAMESPACE_NORMAL)
4235 initialize_local_declaration(declaration);
4239 static void if_statement_to_firm(if_statement_t *statement)
4241 ir_node *cur_block = get_cur_block();
4243 ir_node *fallthrough_block = NULL;
4245 /* the true (blocks) */
4246 ir_node *true_block = NULL;
4247 if (statement->true_statement != NULL) {
4248 true_block = new_immBlock();
4249 statement_to_firm(statement->true_statement);
4250 if (get_cur_block() != NULL) {
4251 ir_node *jmp = new_Jmp();
4252 if (fallthrough_block == NULL)
4253 fallthrough_block = new_immBlock();
4254 add_immBlock_pred(fallthrough_block, jmp);
4258 /* the false (blocks) */
4259 ir_node *false_block = NULL;
4260 if (statement->false_statement != NULL) {
4261 false_block = new_immBlock();
4263 statement_to_firm(statement->false_statement);
4264 if (get_cur_block() != NULL) {
4265 ir_node *jmp = new_Jmp();
4266 if (fallthrough_block == NULL)
4267 fallthrough_block = new_immBlock();
4268 add_immBlock_pred(fallthrough_block, jmp);
4272 /* create the condition */
4273 if (cur_block != NULL) {
4274 if (true_block == NULL || false_block == NULL) {
4275 if (fallthrough_block == NULL)
4276 fallthrough_block = new_immBlock();
4277 if (true_block == NULL)
4278 true_block = fallthrough_block;
4279 if (false_block == NULL)
4280 false_block = fallthrough_block;
4283 set_cur_block(cur_block);
4284 create_condition_evaluation(statement->condition, true_block,
4288 mature_immBlock(true_block);
4289 if (false_block != fallthrough_block && false_block != NULL) {
4290 mature_immBlock(false_block);
4292 if (fallthrough_block != NULL) {
4293 mature_immBlock(fallthrough_block);
4296 set_cur_block(fallthrough_block);
4299 static void while_statement_to_firm(while_statement_t *statement)
4301 ir_node *jmp = NULL;
4302 if (get_cur_block() != NULL) {
4306 /* create the header block */
4307 ir_node *header_block = new_immBlock();
4309 add_immBlock_pred(header_block, jmp);
4313 ir_node *old_continue_label = continue_label;
4314 ir_node *old_break_label = break_label;
4315 continue_label = header_block;
4318 ir_node *body_block = new_immBlock();
4319 statement_to_firm(statement->body);
4320 ir_node *false_block = break_label;
4322 assert(continue_label == header_block);
4323 continue_label = old_continue_label;
4324 break_label = old_break_label;
4326 if (get_cur_block() != NULL) {
4328 add_immBlock_pred(header_block, jmp);
4331 /* shortcut for while(true) */
4332 if (is_constant_expression(statement->condition)
4333 && fold_constant(statement->condition) != 0) {
4334 set_cur_block(header_block);
4335 ir_node *header_jmp = new_Jmp();
4336 add_immBlock_pred(body_block, header_jmp);
4338 keep_alive(body_block);
4339 keep_all_memory(body_block);
4340 set_cur_block(body_block);
4342 if (false_block == NULL) {
4343 false_block = new_immBlock();
4346 /* create the condition */
4347 set_cur_block(header_block);
4349 create_condition_evaluation(statement->condition, body_block,
4353 mature_immBlock(body_block);
4354 mature_immBlock(header_block);
4355 if (false_block != NULL) {
4356 mature_immBlock(false_block);
4359 set_cur_block(false_block);
4362 static void do_while_statement_to_firm(do_while_statement_t *statement)
4364 ir_node *jmp = NULL;
4365 if (get_cur_block() != NULL) {
4369 /* create the header block */
4370 ir_node *header_block = new_immBlock();
4373 ir_node *body_block = new_immBlock();
4375 add_immBlock_pred(body_block, jmp);
4378 ir_node *old_continue_label = continue_label;
4379 ir_node *old_break_label = break_label;
4380 continue_label = header_block;
4383 statement_to_firm(statement->body);
4384 ir_node *false_block = break_label;
4386 assert(continue_label == header_block);
4387 continue_label = old_continue_label;
4388 break_label = old_break_label;
4390 if (get_cur_block() != NULL) {
4391 ir_node *body_jmp = new_Jmp();
4392 add_immBlock_pred(header_block, body_jmp);
4393 mature_immBlock(header_block);
4396 if (false_block == NULL) {
4397 false_block = new_immBlock();
4400 /* create the condition */
4401 set_cur_block(header_block);
4403 create_condition_evaluation(statement->condition, body_block, false_block);
4404 mature_immBlock(body_block);
4405 mature_immBlock(header_block);
4406 if (false_block != NULL) {
4407 mature_immBlock(false_block);
4410 set_cur_block(false_block);
4413 static void for_statement_to_firm(for_statement_t *statement)
4415 ir_node *jmp = NULL;
4417 /* create declarations */
4418 declaration_t *declaration = statement->scope.declarations;
4419 for( ; declaration != NULL; declaration = declaration->next) {
4420 create_local_declaration(declaration);
4423 if (get_cur_block() != NULL) {
4424 declaration = statement->scope.declarations;
4425 for( ; declaration != NULL; declaration = declaration->next) {
4426 initialize_local_declaration(declaration);
4429 if (statement->initialisation != NULL) {
4430 expression_to_firm(statement->initialisation);
4437 /* create the step block */
4438 ir_node *const step_block = new_immBlock();
4439 if (statement->step != NULL) {
4440 expression_to_firm(statement->step);
4442 ir_node *const step_jmp = new_Jmp();
4444 /* create the header block */
4445 ir_node *const header_block = new_immBlock();
4447 add_immBlock_pred(header_block, jmp);
4449 add_immBlock_pred(header_block, step_jmp);
4451 /* the false block */
4452 ir_node *const false_block = new_immBlock();
4455 ir_node * body_block;
4456 if (statement->body != NULL) {
4457 ir_node *const old_continue_label = continue_label;
4458 ir_node *const old_break_label = break_label;
4459 continue_label = step_block;
4460 break_label = false_block;
4462 body_block = new_immBlock();
4463 statement_to_firm(statement->body);
4465 assert(continue_label == step_block);
4466 assert(break_label == false_block);
4467 continue_label = old_continue_label;
4468 break_label = old_break_label;
4470 if (get_cur_block() != NULL) {
4472 add_immBlock_pred(step_block, jmp);
4475 body_block = step_block;
4478 /* create the condition */
4479 set_cur_block(header_block);
4480 if (statement->condition != NULL) {
4481 create_condition_evaluation(statement->condition, body_block,
4484 keep_alive(header_block);
4485 keep_all_memory(header_block);
4487 add_immBlock_pred(body_block, jmp);
4490 mature_immBlock(body_block);
4491 mature_immBlock(false_block);
4492 mature_immBlock(step_block);
4493 mature_immBlock(header_block);
4494 mature_immBlock(false_block);
4496 set_cur_block(false_block);
4499 static void create_jump_statement(const statement_t *statement,
4500 ir_node *target_block)
4502 if (get_cur_block() == NULL)
4505 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4506 ir_node *jump = new_d_Jmp(dbgi);
4507 add_immBlock_pred(target_block, jump);
4509 set_cur_block(NULL);
4512 static ir_node *get_break_label(void)
4514 if (break_label == NULL) {
4515 ir_node *cur_block = get_cur_block();
4516 break_label = new_immBlock();
4517 set_cur_block(cur_block);
4522 static void switch_statement_to_firm(switch_statement_t *statement)
4524 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4526 ir_node *expression = expression_to_firm(statement->expression);
4527 ir_node *cond = new_d_Cond(dbgi, expression);
4529 set_cur_block(NULL);
4531 ir_node *const old_switch_cond = current_switch_cond;
4532 ir_node *const old_break_label = break_label;
4533 const bool old_saw_default_label = saw_default_label;
4534 saw_default_label = false;
4535 current_switch_cond = cond;
4537 switch_statement_t *const old_switch = current_switch;
4538 current_switch = statement;
4540 /* determine a free number for the default label */
4541 unsigned long num_cases = 0;
4543 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4544 if (l->expression == NULL) {
4548 if (l->last_case >= l->first_case)
4549 num_cases += l->last_case - l->first_case;
4550 if (l->last_case > def_nr)
4551 def_nr = l->last_case;
4554 if (def_nr + 1 < 0) {
4555 /* Bad: an overflow occurred, we cannot be sure that the
4556 * maximum + 1 is a free number. Scan the values a second
4557 * time to find a free number.
4559 unsigned char *bits = xmalloc((num_cases + 7) >> 3);
4562 memset(bits, 0, (num_cases + 7) >> 3);
4563 for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
4564 if (l->expression == NULL) {
4568 for (long cns = l->first_case; cns <= l->last_case; ++cns) {
4569 if (cns >= 0 && (unsigned long)cns < num_cases)
4570 bits[cns >> 3] |= (1 << (cns & 7));
4573 /* We look at the first num_cases constants:
4574 * Either they are densed, so we took the last (num_cases)
4575 * one, or they are non densed, so we will find one free
4578 for (i = 0; i < num_cases; ++i)
4579 if ((bits[i >> 3] & (i & 7)) == 0)
4587 statement->default_proj_nr = def_nr;
4589 if (statement->body != NULL) {
4590 statement_to_firm(statement->body);
4593 if (get_cur_block() != NULL) {
4594 ir_node *jmp = new_Jmp();
4595 add_immBlock_pred(get_break_label(), jmp);
4598 if (!saw_default_label) {
4599 set_cur_block(get_nodes_block(cond));
4600 ir_node *const proj = new_d_defaultProj(dbgi, cond,
4601 statement->default_proj_nr);
4602 add_immBlock_pred(get_break_label(), proj);
4605 if (break_label != NULL) {
4606 mature_immBlock(break_label);
4608 set_cur_block(break_label);
4610 assert(current_switch_cond == cond);
4611 current_switch = old_switch;
4612 current_switch_cond = old_switch_cond;
4613 break_label = old_break_label;
4614 saw_default_label = old_saw_default_label;
4617 static void case_label_to_firm(const case_label_statement_t *statement)
4619 if (statement->is_empty_range)
4622 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4624 ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
4627 ir_node *old_block = get_nodes_block(current_switch_cond);
4628 ir_node *block = new_immBlock();
4630 set_cur_block(old_block);
4631 if (statement->expression != NULL) {
4632 long pn = statement->first_case;
4633 long end_pn = statement->last_case;
4634 assert(pn <= end_pn);
4635 /* create jumps for all cases in the given range */
4637 proj = new_d_Proj(dbgi, current_switch_cond, mode_X, pn);
4638 add_immBlock_pred(block, proj);
4639 } while(pn++ < end_pn);
4641 saw_default_label = true;
4642 proj = new_d_defaultProj(dbgi, current_switch_cond,
4643 current_switch->default_proj_nr);
4645 add_immBlock_pred(block, proj);
4648 if (fallthrough != NULL) {
4649 add_immBlock_pred(block, fallthrough);
4651 mature_immBlock(block);
4652 set_cur_block(block);
4654 if (statement->statement != NULL) {
4655 statement_to_firm(statement->statement);
4659 static void label_to_firm(const label_statement_t *statement)
4661 ir_node *block = get_label_block(statement->label);
4663 if (get_cur_block() != NULL) {
4664 ir_node *jmp = new_Jmp();
4665 add_immBlock_pred(block, jmp);
4668 set_cur_block(block);
4670 keep_all_memory(block);
4672 if (statement->statement != NULL) {
4673 statement_to_firm(statement->statement);
4677 static void goto_to_firm(const goto_statement_t *statement)
4679 if (get_cur_block() == NULL)
4682 if (statement->expression) {
4683 ir_node *irn = expression_to_firm(statement->expression);
4684 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4685 ir_node *ijmp = new_d_IJmp(dbgi, irn);
4687 set_irn_link(ijmp, ijmp_list);
4690 if (statement->outer_fkt_jmp) {
4691 /* TODO: this is a outer function jmp */
4692 panic("outer function jump not implemented");
4694 ir_node *block = get_label_block(statement->label);
4695 ir_node *jmp = new_Jmp();
4696 add_immBlock_pred(block, jmp);
4699 set_cur_block(NULL);
4702 static void asm_statement_to_firm(const asm_statement_t *statement)
4704 bool needs_memory = false;
4706 if (statement->is_volatile) {
4707 needs_memory = true;
4710 size_t n_clobbers = 0;
4711 asm_clobber_t *clobber = statement->clobbers;
4712 for( ; clobber != NULL; clobber = clobber->next) {
4713 const char *clobber_str = clobber->clobber.begin;
4715 if (!be_is_valid_clobber(clobber_str)) {
4716 errorf(&statement->base.source_position,
4717 "invalid clobber '%s' specified", clobber->clobber);
4721 if (strcmp(clobber_str, "memory") == 0) {
4722 needs_memory = true;
4726 ident *id = new_id_from_str(clobber_str);
4727 obstack_ptr_grow(&asm_obst, id);
4730 assert(obstack_object_size(&asm_obst) == n_clobbers * sizeof(ident*));
4731 ident **clobbers = NULL;
4732 if (n_clobbers > 0) {
4733 clobbers = obstack_finish(&asm_obst);
4736 size_t n_inputs = 0;
4737 asm_argument_t *argument = statement->inputs;
4738 for ( ; argument != NULL; argument = argument->next)
4740 size_t n_outputs = 0;
4741 argument = statement->outputs;
4742 for ( ; argument != NULL; argument = argument->next)
4745 unsigned next_pos = 0;
4747 ir_node *ins[n_inputs + n_outputs + 1];
4750 ir_asm_constraint tmp_in_constraints[n_outputs];
4752 const expression_t *out_exprs[n_outputs];
4753 ir_node *out_addrs[n_outputs];
4754 size_t out_size = 0;
4756 argument = statement->outputs;
4757 for ( ; argument != NULL; argument = argument->next) {
4758 const char *constraints = argument->constraints.begin;
4759 asm_constraint_flags_t asm_flags
4760 = be_parse_asm_constraints(constraints);
4762 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4763 errorf(&statement->base.source_position,
4764 "some constraints in '%s' are not supported", constraints);
4767 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4768 errorf(&statement->base.source_position,
4769 "some constraints in '%s' are invalid", constraints);
4772 if (! (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE)) {
4773 errorf(&statement->base.source_position,
4774 "no write flag specified for output constraints '%s'",
4779 unsigned pos = next_pos++;
4780 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4781 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4782 expression_t *expr = argument->expression;
4783 ir_node *addr = expression_to_addr(expr);
4784 /* in+output, construct an artifical same_as constraint on the
4786 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_READ) {
4788 ir_node *value = get_value_from_lvalue(expr, addr);
4790 snprintf(buf, sizeof(buf), "%d", pos);
4792 ir_asm_constraint constraint;
4793 constraint.pos = pos;
4794 constraint.constraint = new_id_from_str(buf);
4795 constraint.mode = get_ir_mode(expr->base.type);
4796 tmp_in_constraints[in_size] = constraint;
4797 ins[in_size] = value;
4802 out_exprs[out_size] = expr;
4803 out_addrs[out_size] = addr;
4805 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4806 /* pure memory ops need no input (but we have to make sure we
4807 * attach to the memory) */
4808 assert(! (asm_flags &
4809 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4810 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4811 needs_memory = true;
4813 /* we need to attach the address to the inputs */
4814 expression_t *expr = argument->expression;
4816 ir_asm_constraint constraint;
4817 constraint.pos = pos;
4818 constraint.constraint = new_id_from_str(constraints);
4819 constraint.mode = NULL;
4820 tmp_in_constraints[in_size] = constraint;
4822 ins[in_size] = expression_to_addr(expr);
4826 errorf(&statement->base.source_position,
4827 "only modifiers but no place set in constraints '%s'",
4832 ir_asm_constraint constraint;
4833 constraint.pos = pos;
4834 constraint.constraint = new_id_from_str(constraints);
4835 constraint.mode = get_ir_mode(argument->expression->base.type);
4837 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4839 assert(obstack_object_size(&asm_obst)
4840 == out_size * sizeof(ir_asm_constraint));
4841 ir_asm_constraint *output_constraints = obstack_finish(&asm_obst);
4844 obstack_grow(&asm_obst, tmp_in_constraints,
4845 in_size * sizeof(tmp_in_constraints[0]));
4846 /* find and count input and output arguments */
4847 argument = statement->inputs;
4848 for( ; argument != NULL; argument = argument->next) {
4849 const char *constraints = argument->constraints.begin;
4850 asm_constraint_flags_t asm_flags
4851 = be_parse_asm_constraints(constraints);
4853 if (asm_flags & ASM_CONSTRAINT_FLAG_NO_SUPPORT) {
4854 errorf(&statement->base.source_position,
4855 "some constraints in '%s' are not supported", constraints);
4858 if (asm_flags & ASM_CONSTRAINT_FLAG_INVALID) {
4859 errorf(&statement->base.source_position,
4860 "some constraints in '%s' are invalid", constraints);
4863 if (asm_flags & ASM_CONSTRAINT_FLAG_MODIFIER_WRITE) {
4864 errorf(&statement->base.source_position,
4865 "write flag specified for input constraints '%s'",
4871 if ( (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE)
4872 || (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER) ) {
4873 /* we can treat this as "normal" input */
4874 input = expression_to_firm(argument->expression);
4875 } else if (asm_flags & ASM_CONSTRAINT_FLAG_SUPPORTS_MEMOP) {
4876 /* pure memory ops need no input (but we have to make sure we
4877 * attach to the memory) */
4878 assert(! (asm_flags &
4879 (ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE
4880 | ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER)));
4881 needs_memory = true;
4882 input = expression_to_addr(argument->expression);
4884 errorf(&statement->base.source_position,
4885 "only modifiers but no place set in constraints '%s'",
4890 ir_asm_constraint constraint;
4891 constraint.pos = next_pos++;
4892 constraint.constraint = new_id_from_str(constraints);
4893 constraint.mode = get_irn_mode(input);
4895 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4896 ins[in_size++] = input;
4900 ir_asm_constraint constraint;
4901 constraint.pos = next_pos++;
4902 constraint.constraint = new_id_from_str("");
4903 constraint.mode = mode_M;
4905 obstack_grow(&asm_obst, &constraint, sizeof(constraint));
4906 ins[in_size++] = get_store();
4909 assert(obstack_object_size(&asm_obst)
4910 == in_size * sizeof(ir_asm_constraint));
4911 ir_asm_constraint *input_constraints = obstack_finish(&asm_obst);
4913 /* create asm node */
4914 dbg_info *dbgi = get_dbg_info(&statement->base.source_position);
4916 ident *asm_text = new_id_from_str(statement->asm_text.begin);
4918 ir_node *node = new_d_ASM(dbgi, in_size, ins, input_constraints,
4919 out_size, output_constraints,
4920 n_clobbers, clobbers, asm_text);
4922 if (statement->is_volatile) {
4923 set_irn_pinned(node, op_pin_state_pinned);
4925 set_irn_pinned(node, op_pin_state_floats);
4928 /* create output projs & connect them */
4930 ir_node *projm = new_Proj(node, mode_M, out_size+1);
4935 for (i = 0; i < out_size; ++i) {
4936 const expression_t *out_expr = out_exprs[i];
4938 ir_mode *mode = get_ir_mode(out_expr->base.type);
4939 ir_node *proj = new_Proj(node, mode, pn);
4940 ir_node *addr = out_addrs[i];
4942 set_value_for_expression_addr(out_expr, proj, addr);
4946 static void ms_try_statement_to_firm(ms_try_statement_t *statement) {
4947 statement_to_firm(statement->try_statement);
4948 warningf(&statement->base.source_position, "structured exception handling ignored");
4951 static void leave_statement_to_firm(leave_statement_t *statement) {
4952 errorf(&statement->base.source_position, "__leave not supported yet");
4956 * Transform a statement.
4958 static void statement_to_firm(statement_t *statement)
4961 assert(!statement->base.transformed);
4962 statement->base.transformed = true;
4965 switch(statement->kind) {
4966 case STATEMENT_INVALID:
4967 panic("invalid statement found");
4969 case STATEMENT_EMPTY:
4972 case STATEMENT_COMPOUND:
4973 compound_statement_to_firm(&statement->compound);
4975 case STATEMENT_RETURN:
4976 return_statement_to_firm(&statement->returns);
4978 case STATEMENT_EXPRESSION:
4979 expression_statement_to_firm(&statement->expression);
4982 if_statement_to_firm(&statement->ifs);
4984 case STATEMENT_WHILE:
4985 while_statement_to_firm(&statement->whiles);
4987 case STATEMENT_DO_WHILE:
4988 do_while_statement_to_firm(&statement->do_while);
4990 case STATEMENT_DECLARATION:
4991 declaration_statement_to_firm(&statement->declaration);
4993 case STATEMENT_BREAK:
4994 create_jump_statement(statement, get_break_label());
4996 case STATEMENT_CONTINUE:
4997 create_jump_statement(statement, continue_label);
4999 case STATEMENT_SWITCH:
5000 switch_statement_to_firm(&statement->switchs);
5002 case STATEMENT_CASE_LABEL:
5003 case_label_to_firm(&statement->case_label);
5006 for_statement_to_firm(&statement->fors);
5008 case STATEMENT_LABEL:
5009 label_to_firm(&statement->label);
5011 case STATEMENT_GOTO:
5012 goto_to_firm(&statement->gotos);
5015 asm_statement_to_firm(&statement->asms);
5017 case STATEMENT_MS_TRY:
5018 ms_try_statement_to_firm(&statement->ms_try);
5020 case STATEMENT_LEAVE:
5021 leave_statement_to_firm(&statement->leave);
5024 panic("Statement not implemented\n");
5027 static int count_decls_in_expression(const expression_t *expression);
5029 static int count_local_declarations(const declaration_t * decl,
5030 const declaration_t *const end)
5033 for (; decl != end; decl = decl->next) {
5034 if (decl->namespc != NAMESPACE_NORMAL)
5036 type_t *type = skip_typeref(decl->type);
5038 if (!decl->address_taken && is_type_scalar(type))
5040 const initializer_t *initializer = decl->init.initializer;
5041 /* FIXME: should walk initializer hierarchies... */
5042 if (initializer != NULL && initializer->kind == INITIALIZER_VALUE) {
5043 count += count_decls_in_expression(initializer->value.value);
5049 static int count_decls_in_expression(const expression_t *expression) {
5052 if (expression == NULL)
5055 switch((expression_kind_t) expression->base.kind) {
5056 case EXPR_STATEMENT:
5057 return count_decls_in_stmts(expression->statement.statement);
5059 int count_left = count_decls_in_expression(expression->binary.left);
5060 int count_right = count_decls_in_expression(expression->binary.right);
5061 return count_left + count_right;
5064 return count_decls_in_expression(expression->unary.value);
5066 call_argument_t *argument = expression->call.arguments;
5067 for( ; argument != NULL; argument = argument->next) {
5068 count += count_decls_in_expression(argument->expression);
5075 panic("unexpected expression kind");
5077 case EXPR_COMPOUND_LITERAL:
5081 case EXPR_CONDITIONAL:
5082 count += count_decls_in_expression(expression->conditional.condition);
5083 count += count_decls_in_expression(expression->conditional.true_expression);
5084 count += count_decls_in_expression(expression->conditional.false_expression);
5087 case EXPR_BUILTIN_PREFETCH:
5088 count += count_decls_in_expression(expression->builtin_prefetch.adr);
5089 count += count_decls_in_expression(expression->builtin_prefetch.rw);
5090 count += count_decls_in_expression(expression->builtin_prefetch.locality);
5093 case EXPR_BUILTIN_CONSTANT_P:
5094 count += count_decls_in_expression(expression->builtin_constant.value);
5098 count += count_decls_in_expression(expression->select.compound);
5101 case EXPR_ARRAY_ACCESS:
5102 count += count_decls_in_expression(expression->array_access.array_ref);
5103 count += count_decls_in_expression(expression->array_access.index);
5106 case EXPR_CLASSIFY_TYPE:
5107 count += count_decls_in_expression(expression->classify_type.type_expression);
5111 case EXPR_ALIGNOF: {
5112 expression_t *tp_expression = expression->typeprop.tp_expression;
5113 if (tp_expression != NULL) {
5114 count += count_decls_in_expression(tp_expression);
5120 case EXPR_REFERENCE:
5122 case EXPR_CHARACTER_CONSTANT:
5123 case EXPR_WIDE_CHARACTER_CONSTANT:
5124 case EXPR_STRING_LITERAL:
5125 case EXPR_WIDE_STRING_LITERAL:
5127 case EXPR_BUILTIN_SYMBOL:
5130 case EXPR_LABEL_ADDRESS:
5134 /* TODO FIXME: finish/fix that firm patch that allows dynamic value numbers
5135 * (or implement all the missing expressions here/implement a walker)
5141 static int count_decls_in_stmts(const statement_t *stmt)
5144 for (; stmt != NULL; stmt = stmt->base.next) {
5145 switch (stmt->kind) {
5146 case STATEMENT_EMPTY:
5149 case STATEMENT_DECLARATION: {
5150 const declaration_statement_t *const decl_stmt = &stmt->declaration;
5151 count += count_local_declarations(decl_stmt->declarations_begin,
5152 decl_stmt->declarations_end->next);
5156 case STATEMENT_COMPOUND: {
5157 const compound_statement_t *const comp =
5159 count += count_decls_in_stmts(comp->statements);
5163 case STATEMENT_IF: {
5164 const if_statement_t *const if_stmt = &stmt->ifs;
5165 count += count_decls_in_expression(if_stmt->condition);
5166 count += count_decls_in_stmts(if_stmt->true_statement);
5167 count += count_decls_in_stmts(if_stmt->false_statement);
5171 case STATEMENT_SWITCH: {
5172 const switch_statement_t *const switch_stmt = &stmt->switchs;
5173 count += count_decls_in_expression(switch_stmt->expression);
5174 count += count_decls_in_stmts(switch_stmt->body);
5178 case STATEMENT_LABEL: {
5179 const label_statement_t *const label_stmt = &stmt->label;
5180 if (label_stmt->statement != NULL) {
5181 count += count_decls_in_stmts(label_stmt->statement);
5186 case STATEMENT_WHILE: {
5187 const while_statement_t *const while_stmt = &stmt->whiles;
5188 count += count_decls_in_expression(while_stmt->condition);
5189 count += count_decls_in_stmts(while_stmt->body);
5193 case STATEMENT_DO_WHILE: {
5194 const do_while_statement_t *const do_while_stmt = &stmt->do_while;
5195 count += count_decls_in_expression(do_while_stmt->condition);
5196 count += count_decls_in_stmts(do_while_stmt->body);
5200 case STATEMENT_FOR: {
5201 const for_statement_t *const for_stmt = &stmt->fors;
5202 count += count_local_declarations(for_stmt->scope.declarations, NULL);
5203 count += count_decls_in_expression(for_stmt->initialisation);
5204 count += count_decls_in_expression(for_stmt->condition);
5205 count += count_decls_in_expression(for_stmt->step);
5206 count += count_decls_in_stmts(for_stmt->body);
5210 case STATEMENT_CASE_LABEL: {
5211 const case_label_statement_t *label = &stmt->case_label;
5212 count += count_decls_in_expression(label->expression);
5213 if (label->statement != NULL) {
5214 count += count_decls_in_stmts(label->statement);
5220 case STATEMENT_BREAK:
5221 case STATEMENT_CONTINUE:
5224 case STATEMENT_EXPRESSION: {
5225 const expression_statement_t *expr_stmt = &stmt->expression;
5226 count += count_decls_in_expression(expr_stmt->expression);
5230 case STATEMENT_GOTO:
5231 case STATEMENT_LEAVE:
5232 case STATEMENT_INVALID:
5235 case STATEMENT_RETURN: {
5236 const return_statement_t *ret_stmt = &stmt->returns;
5237 count += count_decls_in_expression(ret_stmt->value);
5241 case STATEMENT_MS_TRY: {
5242 const ms_try_statement_t *const try_stmt = &stmt->ms_try;
5243 count += count_decls_in_stmts(try_stmt->try_statement);
5244 if (try_stmt->except_expression != NULL)
5245 count += count_decls_in_expression(try_stmt->except_expression);
5246 count += count_decls_in_stmts(try_stmt->final_statement);
5254 static int get_function_n_local_vars(declaration_t *declaration)
5258 /* count parameters */
5259 count += count_local_declarations(declaration->scope.declarations, NULL);
5261 /* count local variables declared in body */
5262 count += count_decls_in_stmts(declaration->init.statement);
5267 static void initialize_function_parameters(declaration_t *declaration)
5269 ir_graph *irg = current_ir_graph;
5270 ir_node *args = get_irg_args(irg);
5271 ir_node *start_block = get_irg_start_block(irg);
5272 ir_type *function_irtype = get_ir_type(declaration->type);
5275 declaration_t *parameter = declaration->scope.declarations;
5276 for( ; parameter != NULL; parameter = parameter->next, ++n) {
5277 assert(parameter->declaration_kind == DECLARATION_KIND_UNKNOWN);
5278 type_t *type = skip_typeref(parameter->type);
5280 bool needs_entity = parameter->address_taken;
5281 assert(!is_type_array(type));
5282 if (is_type_compound(type)) {
5283 needs_entity = true;
5287 ir_entity *entity = get_method_value_param_ent(function_irtype, n);
5288 ident *id = new_id_from_str(parameter->symbol->string);
5289 set_entity_ident(entity, id);
5291 parameter->declaration_kind
5292 = DECLARATION_KIND_LOCAL_VARIABLE_ENTITY;
5293 parameter->v.entity = entity;
5297 ir_type *param_irtype = get_method_param_type(function_irtype, n);
5298 ir_mode *param_mode = get_type_mode(param_irtype);
5301 ir_node *value = new_r_Proj(irg, start_block, args, param_mode, pn);
5303 ir_mode *mode = get_ir_mode(type);
5304 value = create_conv(NULL, value, mode);
5305 value = do_strict_conv(NULL, value);
5307 parameter->declaration_kind = DECLARATION_KIND_LOCAL_VARIABLE;
5308 parameter->v.value_number = next_value_number_function;
5309 set_irg_loc_description(current_ir_graph, next_value_number_function, parameter);
5310 ++next_value_number_function;
5312 set_value(parameter->v.value_number, value);
5317 * Handle additional decl modifiers for IR-graphs
5319 * @param irg the IR-graph
5320 * @param dec_modifiers additional modifiers
5322 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
5324 if (decl_modifiers & DM_NORETURN) {
5325 /* TRUE if the declaration includes the Microsoft
5326 __declspec(noreturn) specifier. */
5327 set_irg_additional_property(irg, mtp_property_noreturn);
5329 if (decl_modifiers & DM_NOTHROW) {
5330 /* TRUE if the declaration includes the Microsoft
5331 __declspec(nothrow) specifier. */
5332 set_irg_additional_property(irg, mtp_property_nothrow);
5334 if (decl_modifiers & DM_NAKED) {
5335 /* TRUE if the declaration includes the Microsoft
5336 __declspec(naked) specifier. */
5337 set_irg_additional_property(irg, mtp_property_naked);
5339 if (decl_modifiers & DM_FORCEINLINE) {
5340 /* TRUE if the declaration includes the
5341 Microsoft __forceinline specifier. */
5342 set_irg_inline_property(irg, irg_inline_forced);
5344 if (decl_modifiers & DM_NOINLINE) {
5345 /* TRUE if the declaration includes the Microsoft
5346 __declspec(noinline) specifier. */
5347 set_irg_inline_property(irg, irg_inline_forbidden);
5351 static void add_function_pointer(ir_type *segment, ir_entity *method,
5352 const char *unique_template)
5354 ir_type *method_type = get_entity_type(method);
5355 ident *id = id_unique(unique_template);
5356 ir_type *ptr_type = new_type_pointer(id, method_type, mode_P_code);
5358 ident *ide = id_unique(unique_template);
5359 ir_entity *ptr = new_entity(segment, ide, ptr_type);
5360 ir_graph *irg = get_const_code_irg();
5361 ir_node *val = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
5364 set_entity_compiler_generated(ptr, 1);
5365 set_entity_variability(ptr, variability_constant);
5366 set_atomic_ent_value(ptr, val);
5370 * Generate possible IJmp branches to a given label block.
5372 static void gen_ijmp_branches(ir_node *block) {
5374 for (ijmp = ijmp_list; ijmp != NULL; ijmp = get_irn_link(ijmp)) {
5375 add_immBlock_pred(block, ijmp);
5380 * Create code for a function.
5382 static void create_function(declaration_t *declaration)
5384 ir_entity *function_entity = get_function_entity(declaration);
5386 if (declaration->init.statement == NULL)
5389 if (declaration->modifiers & DM_CONSTRUCTOR) {
5390 ir_type *segment = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
5391 add_function_pointer(segment, function_entity, "constructor_ptr.%u");
5393 if (declaration->modifiers & DM_DESTRUCTOR) {
5394 ir_type *segment = get_segment_type(IR_SEGMENT_DESTRUCTORS);
5395 add_function_pointer(segment, function_entity, "destructor_ptr.%u");
5398 current_function_decl = declaration;
5399 current_function_name = NULL;
5400 current_funcsig = NULL;
5402 assert(all_labels == NULL);
5403 all_labels = NEW_ARR_F(declaration_t *, 0);
5406 int n_local_vars = get_function_n_local_vars(declaration);
5407 ir_graph *irg = new_ir_graph(function_entity, n_local_vars);
5409 ir_graph *old_current_function = current_function;
5410 current_function = irg;
5412 set_irg_fp_model(irg, firm_opt.fp_model);
5413 tarval_enable_fp_ops((firm_opt.fp_model & fp_strict_algebraic) == 0);
5414 set_irn_dbg_info(get_irg_start_block(irg), get_entity_dbg_info(function_entity));
5416 ir_node *first_block = get_cur_block();
5418 /* set inline flags */
5419 if (declaration->is_inline)
5420 set_irg_inline_property(irg, irg_inline_recomended);
5421 handle_decl_modifier_irg(irg, declaration->modifiers);
5423 next_value_number_function = 0;
5424 initialize_function_parameters(declaration);
5426 statement_to_firm(declaration->init.statement);
5428 ir_node *end_block = get_irg_end_block(irg);
5430 /* do we have a return statement yet? */
5431 if (get_cur_block() != NULL) {
5432 type_t *type = skip_typeref(declaration->type);
5433 assert(is_type_function(type));
5434 const function_type_t *func_type = &type->function;
5435 const type_t *return_type
5436 = skip_typeref(func_type->return_type);
5439 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5440 ret = new_Return(get_store(), 0, NULL);
5443 if (is_type_scalar(return_type)) {
5444 mode = get_ir_mode(func_type->return_type);
5450 /* ยง5.1.2.2.3 main implicitly returns 0 */
5451 if (strcmp(declaration->symbol->string, "main") == 0) {
5452 in[0] = new_Const(mode, get_mode_null(mode));
5454 in[0] = new_Unknown(mode);
5456 ret = new_Return(get_store(), 1, in);
5458 add_immBlock_pred(end_block, ret);
5461 bool has_computed_gotos = false;
5462 for (int i = ARR_LEN(all_labels) - 1; i >= 0; --i) {
5463 declaration_t *label = all_labels[i];
5464 if (label->address_taken) {
5465 gen_ijmp_branches(label->v.block);
5466 has_computed_gotos = true;
5468 mature_immBlock(label->v.block);
5470 if (has_computed_gotos) {
5471 /* if we have computed goto's in the function, we cannot inline it */
5472 if (get_irg_inline_property(irg) >= irg_inline_recomended) {
5473 warningf(&declaration->source_position,
5474 "function '%Y' can never be inlined because it contains a computed goto",
5475 declaration->symbol);
5477 set_irg_inline_property(irg, irg_inline_forbidden);
5480 DEL_ARR_F(all_labels);
5483 mature_immBlock(first_block);
5484 mature_immBlock(end_block);
5486 irg_finalize_cons(irg);
5488 /* finalize the frame type */
5489 ir_type *frame_type = get_irg_frame_type(irg);
5490 int n = get_compound_n_members(frame_type);
5493 for(int i = 0; i < n; ++i) {
5494 ir_entity *entity = get_compound_member(frame_type, i);
5495 ir_type *entity_type = get_entity_type(entity);
5497 int align = get_type_alignment_bytes(entity_type);
5498 if (align > align_all)
5502 misalign = offset % align;
5504 offset += align - misalign;
5508 set_entity_offset(entity, offset);
5509 offset += get_type_size_bytes(entity_type);
5511 set_type_size_bytes(frame_type, offset);
5512 set_type_alignment_bytes(frame_type, align_all);
5515 current_function = old_current_function;
5518 static void scope_to_firm(scope_t *scope)
5520 /* first pass: create declarations */
5521 declaration_t *declaration = scope->declarations;
5522 for( ; declaration != NULL; declaration = declaration->next) {
5523 if (declaration->namespc != NAMESPACE_NORMAL)
5525 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5526 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5528 if (declaration->symbol == NULL)
5531 type_t *type = skip_typeref(declaration->type);
5532 if (is_type_function(type)) {
5533 get_function_entity(declaration);
5535 create_global_variable(declaration);
5539 /* second pass: create code/initializers */
5540 declaration = scope->declarations;
5541 for( ; declaration != NULL; declaration = declaration->next) {
5542 if (declaration->namespc != NAMESPACE_NORMAL)
5544 if (declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
5545 || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5547 if (declaration->symbol == NULL)
5550 type_t *type = declaration->type;
5551 if (type->kind == TYPE_FUNCTION) {
5552 create_function(declaration);
5553 declaration_t *inner;
5554 for (inner = next_inner_function(); inner != NULL;
5555 inner = next_inner_function())
5556 create_function(inner);
5558 assert(declaration->declaration_kind
5559 == DECLARATION_KIND_GLOBAL_VARIABLE);
5560 current_ir_graph = get_const_code_irg();
5561 create_declaration_initializer(declaration);
5566 static void set_be_option(const char *option)
5568 int res = firm_be_option(option);
5572 void init_ast2firm(void)
5574 obstack_init(&asm_obst);
5575 init_atomic_modes();
5577 id_underscore = new_id_from_chars("_", 1);
5578 id_imp = new_id_from_chars("__imp_", 6);
5580 /* OS option must be set to the backend */
5581 switch (firm_opt.os_support) {
5582 case OS_SUPPORT_MINGW:
5583 create_ld_ident = create_ld_ident_win32;
5584 set_be_option("ia32-gasmode=mingw");
5586 case OS_SUPPORT_LINUX:
5587 create_ld_ident = create_ld_ident_linux_elf;
5588 set_be_option("ia32-gasmode=elf");
5590 case OS_SUPPORT_MACHO:
5591 create_ld_ident = create_ld_ident_macho;
5592 set_be_option("ia32-gasmode=macho");
5593 set_be_option("ia32-stackalign=4");
5594 set_be_option("pic");
5598 /* create idents for all known runtime functions */
5599 for (size_t i = 0; i < sizeof(rts_data) / sizeof(rts_data[0]); ++i) {
5600 rts_idents[i] = new_id_from_str(rts_data[i].name);
5603 entitymap_init(&entitymap);
5606 static void init_ir_types(void)
5608 static int ir_types_initialized = 0;
5609 if (ir_types_initialized)
5611 ir_types_initialized = 1;
5613 ir_type_int = get_ir_type(type_int);
5614 ir_type_const_char = get_ir_type(type_const_char);
5615 ir_type_wchar_t = get_ir_type(type_wchar_t);
5616 ir_type_void = get_ir_type(type_void);
5619 void exit_ast2firm(void)
5621 entitymap_destroy(&entitymap);
5622 obstack_free(&asm_obst, NULL);
5625 static void global_asm_to_firm(statement_t *s)
5627 for (; s != NULL; s = s->base.next) {
5628 assert(s->kind == STATEMENT_ASM);
5630 char const *const text = s->asms.asm_text.begin;
5631 size_t size = s->asms.asm_text.size;
5633 /* skip the last \0 */
5634 if (text[size - 1] == '\0')
5637 ident *const id = new_id_from_chars(text, size);
5642 void translation_unit_to_firm(translation_unit_t *unit)
5644 /* just to be sure */
5645 continue_label = NULL;
5647 current_switch_cond = NULL;
5651 scope_to_firm(&unit->scope);
5652 global_asm_to_firm(unit->global_asm);
5654 current_ir_graph = NULL;